• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang util.ZeroTime函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/skycoin/skycoin/src/util.ZeroTime函数的典型用法代码示例。如果您正苦于以下问题:Golang ZeroTime函数的具体用法?Golang ZeroTime怎么用?Golang ZeroTime使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了ZeroTime函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: TestCullInvalidConnections

func TestCullInvalidConnections(t *testing.T) {
	d := newDefaultDaemon()
	// Is fine
	d.ExpectingIntroductions[addr] = time.Now()
	// Is expired
	d.ExpectingIntroductions[addrb] = util.ZeroTime()
	// Is not in pool
	d.ExpectingIntroductions[addrc] = util.ZeroTime()
	d.Peers.Peers.AddPeer(addr)
	d.Peers.Peers.AddPeer(addrb)
	d.Peers.Peers.AddPeer(addrc)
	d.Pool.Pool.Addresses[addr] = gnetConnection(addr)
	d.Pool.Pool.Addresses[addrb] = gnetConnection(addrb)
	d.Pool.Pool.Addresses[addrb].Id = 2
	d.Pool.Pool.Pool[1] = d.Pool.Pool.Addresses[addr]
	d.Pool.Pool.Pool[2] = d.Pool.Pool.Addresses[addrb]

	assert.NotPanics(t, d.cullInvalidConnections)

	assert.Equal(t, len(d.ExpectingIntroductions), 1)
	assert.Equal(t, len(d.Peers.Peers.Peerlist), 2)
	assert.Equal(t, len(d.Pool.Pool.DisconnectQueue), 1)
	if len(d.Pool.Pool.DisconnectQueue) == 0 {
		t.Fatal("pool.Pool.DisconnectQueue not empty, would block")
	}
	de := <-d.Pool.Pool.DisconnectQueue
	assert.Equal(t, de.ConnId, 2)
	assert.Equal(t, de.Reason, DisconnectIntroductionTimeout)
	shutdown(d)
}
开发者ID:kinghuabg,项目名称:skycoin,代码行数:30,代码来源:daemon_test.go


示例2: gnetConnection

func gnetConnection(addr string) *gnet.Connection {
	return &gnet.Connection{
		Id:           1,
		Conn:         NewDummyConn(addr),
		LastSent:     util.ZeroTime(),
		LastReceived: util.ZeroTime(),
		Buffer:       &bytes.Buffer{},
		WriteQueue:   make(chan gnet.Message, 16),
	}
}
开发者ID:up4k,项目名称:skycoin,代码行数:10,代码来源:messages_test.go


示例3: createUnconfirmedTxn

// Creates an unconfirmed transaction
func (self *UnconfirmedTxnPool) createUnconfirmedTxn(bcUnsp *coin.UnspentPool,
	t coin.Transaction, addrs map[coin.Address]byte) UnconfirmedTxn {
	now := util.Now()
	ut := UnconfirmedTxn{
		Txn:          t,
		Received:     now,
		Checked:      now,
		Announced:    util.ZeroTime(),
		IsOurReceive: false,
		IsOurSpend:   false,
	}

	// Check if this unspent is related to us
	if addrs != nil {
		// Check if this is one of our receiving txns
		for i, _ := range t.Out {
			if _, ok := addrs[t.Out[i].Address]; ok {
				ut.IsOurReceive = true
				break
			}
		}
		// Check if this is one of our spending txns
		for i, _ := range t.In {
			if ux, ok := bcUnsp.Get(t.In[i]); ok {
				if _, ok := addrs[ux.Body.Address]; ok {
					ut.IsOurSpend = true
					break
				}
			}
		}
	}

	return ut
}
开发者ID:up4k,项目名称:skycoin,代码行数:35,代码来源:unconfirmed.go


示例4: TestGetPeersMessage

func TestGetPeersMessage(t *testing.T) {
	d := newDefaultDaemon()
	defer shutdown(d)
	p := d.Pool
	m := NewGetPeersMessage()
	testSimpleMessageHandler(t, d, m)
	d.Peers.Peers.AddPeer(addr)
	q, err := d.Peers.Peers.AddPeer(addrb)
	assert.Nil(t, err)
	q.Private = true
	d.Peers.Config.ReplyCount = 100
	m.c = messageContext(addr)

	// Peers disabled
	d.Peers.Config.Disabled = true
	assert.NotPanics(t, func() { m.Process(d) })
	wait()
	assert.Equal(t, len(p.Pool.SendResults), 0)
	assert.True(t, m.c.Conn.LastSent.IsZero())

	// Peers enabled
	d.Peers.Config.Disabled = false
	m.c = messageContext(addr)
	defer m.c.Conn.Close()
	go p.Pool.ConnectionWriteLoop(m.c.Conn)
	assert.NotPanics(t, func() { m.Process(d) })
	wait()
	assert.Equal(t, len(p.Pool.SendResults), 1)
	if len(p.Pool.SendResults) == 0 {
		t.Fatal("SendResults empty, would block")
	}
	sr := <-p.Pool.SendResults
	assert.Nil(t, sr.Error)
	assert.Equal(t, sr.Connection, m.c.Conn)
	msg, ok := sr.Message.(*GivePeersMessage)
	assert.True(t, ok)
	// Private peer should not be included
	ipAddr, err := NewIPAddr(addr)
	assert.Nil(t, err)
	assert.Equal(t, msg.Peers, []IPAddr{ipAddr})
	assert.False(t, m.c.Conn.LastSent.IsZero())

	// If no peers, nothing should happen
	m.c.Conn.LastSent = util.ZeroTime()
	delete(d.Peers.Peers.Peerlist, addr)
	assert.NotPanics(t, func() { m.Process(d) })
	wait()
	assert.Equal(t, len(p.Pool.SendResults), 0)
	assert.True(t, m.c.Conn.LastSent.IsZero())

	// Test serialization
	m = NewGetPeersMessage()
	b := encoder.Serialize(m)
	m2 := GetPeersMessage{}
	assert.Nil(t, encoder.DeserializeRaw(b, &m2))
	assert.Equal(t, *m, m2)

	gnet.EraseMessages()
}
开发者ID:Chao-Jia,项目名称:skycoin,代码行数:59,代码来源:messages_test.go


示例5: createUnconfirmedTxn

func createUnconfirmedTxn() UnconfirmedTxn {
	ut := UnconfirmedTxn{}
	ut.Txn = coin.Transaction{}
	ut.Txn.Head.Hash = randSHA256()
	ut.Received = util.Now()
	ut.Checked = ut.Received
	ut.Announced = util.ZeroTime()
	return ut
}
开发者ID:JmAbuDabi,项目名称:skycoin,代码行数:9,代码来源:readable_test.go


示例6: createUnconfirmedTxn

// Creates an unconfirmed transaction
func (utp *UnconfirmedTxnPool) createUnconfirmedTxn(bcUnsp *coin.UnspentPool,
	t coin.Transaction) UnconfirmedTxn {
	now := util.Now()
	return UnconfirmedTxn{
		Txn:       t,
		Received:  now,
		Checked:   now,
		Announced: util.ZeroTime(),
	}
}
开发者ID:skycoin,项目名称:skycoin,代码行数:11,代码来源:unconfirmed.go


示例7: testDaemonLoopClearOldPeersTicker

func testDaemonLoopClearOldPeersTicker(t *testing.T, d *Daemon, quit chan int,
	count int) {
	p := pex.NewPeer(addr)
	p.LastSeen = util.ZeroTime()
	d.Peers.Peers.Peerlist[addr] = p
	d.Peers.Config.CullRate = time.Millisecond * 10
	go d.Start(quit)
	time.Sleep(time.Millisecond * 15)
	assert.Equal(t, len(d.Peers.Peers.Peerlist), count)
}
开发者ID:kinghuabg,项目名称:skycoin,代码行数:10,代码来源:daemon_test.go


示例8: testDaemonLoopClearStaleConnectionsTicker

func testDaemonLoopClearStaleConnectionsTicker(t *testing.T, d *Daemon,
	quit chan int, poolCount int) {
	c := gnetConnection(addr)
	c.LastReceived = util.ZeroTime()
	d.Pool.Pool.Pool[c.Id] = c
	d.Pool.Config.ClearStaleRate = time.Millisecond * 10
	go d.Start(quit)
	time.Sleep(time.Millisecond * 15)
	assert.Equal(t, len(d.Pool.Pool.Pool), poolCount)
}
开发者ID:kinghuabg,项目名称:skycoin,代码行数:10,代码来源:daemon_test.go


示例9: createUnconfirmedTxn

// Creates an unconfirmed transaction
func (self *UnconfirmedTxnPool) createUnconfirmedTxn(bcUnsp *coin.UnspentPool,
	t coin.Transaction, addrs map[coin.Address]byte) UnconfirmedTxn {
	now := util.Now()
	return UnconfirmedTxn{
		Txn:       t,
		Received:  now,
		Checked:   now,
		Announced: util.ZeroTime(),
	}
}
开发者ID:kinghuabg,项目名称:skycoin,代码行数:11,代码来源:unconfirmed.go


示例10: createUnconfirmedTxn

func createUnconfirmedTxn() visor.UnconfirmedTxn {
	now := util.Now()
	return visor.UnconfirmedTxn{
		Txn: coin.Transaction{
			Head: coin.TransactionHeader{
				Hash: coin.SumSHA256([]byte("cascas")),
			},
		},
		Received:  now,
		Checked:   now,
		Announced: util.ZeroTime(),
	}
}
开发者ID:notsoshifty,项目名称:skycoin,代码行数:13,代码来源:visor_test.go


示例11: TestAnnounceTxnsMessageProcess

func TestAnnounceTxnsMessageProcess(t *testing.T) {
	v, _ := setupVisor()
	d, _ := newVisorDaemon(v)
	defer shutdown(d)
	gc := setupExistingPool(d.Pool)
	go d.Pool.Pool.ConnectionWriteLoop(gc)

	tx := createUnconfirmedTxn()
	txns := []coin.SHA256{tx.Txn.Hash()}
	m := NewAnnounceTxnsMessage(txns)
	m.c = messageContext(addr)
	go d.Pool.Pool.ConnectionWriteLoop(m.c.Conn)
	defer m.c.Conn.Close()

	// Disabled, nothing should happen
	d.Visor.Config.Disabled = true
	assert.NotPanics(t, func() { m.Process(d) })
	wait()
	assert.Equal(t, len(d.Pool.Pool.SendResults), 0)
	assert.True(t, m.c.Conn.LastSent.IsZero())
	assert.True(t, gc.LastSent.IsZero())

	// We don't know some, request them
	d.Visor.Config.Disabled = false
	assert.NotPanics(t, func() { m.Process(d) })
	wait()
	assert.Equal(t, len(d.Pool.Pool.SendResults), 1)
	if len(d.Pool.Pool.SendResults) == 0 {
		t.Fatal("SendResults empty, would block")
	}
	sr := <-d.Pool.Pool.SendResults
	assert.Equal(t, sr.Connection, m.c.Conn)
	assert.Nil(t, sr.Error)
	_, ok := sr.Message.(*GetTxnsMessage)
	assert.True(t, ok)
	assert.False(t, m.c.Conn.LastSent.IsZero())
	// Should not have been broadcast
	assert.True(t, gc.LastSent.IsZero())

	// We know all the reported txns, nothing should be sent
	d.Visor.Visor.Unconfirmed.Txns[tx.Txn.Hash()] = tx
	m.c.Conn.Conn = NewDummyConn(addr)
	m.c.Conn.LastSent = util.ZeroTime()
	assert.NotPanics(t, func() { m.Process(d) })
	wait()
	assert.Equal(t, len(d.Pool.Pool.SendResults), 0)
	assert.True(t, m.c.Conn.LastSent.IsZero())
	assert.True(t, gc.LastSent.IsZero())
}
开发者ID:up4k,项目名称:skycoin,代码行数:49,代码来源:visor_test.go


示例12: TestVisorRequestBlocksFromAddr

func TestVisorRequestBlocksFromAddr(t *testing.T) {
	defer cleanupVisor()
	defer gnet.EraseMessages()
	p, gc := setupPool()
	vc, _ := setupVisor()
	go p.Pool.ConnectionWriteLoop(gc)

	// Disabled
	vc.Disabled = true
	v := NewVisor(vc)
	assert.NotPanics(t, func() {
		err := v.RequestBlocksFromAddr(p, addr)
		assert.NotNil(t, err)
		assert.Equal(t, err.Error(), "Visor disabled")
	})
	wait()
	assert.Equal(t, len(p.Pool.SendResults), 0)
	assert.True(t, gc.LastSent.IsZero())

	vc.Disabled = false
	v = NewVisor(vc)
	assert.NotPanics(t, func() {
		assert.Nil(t, v.RequestBlocksFromAddr(p, addr))
	})
	wait()
	assert.Equal(t, len(p.Pool.SendResults), 1)
	if len(p.Pool.SendResults) == 0 {
		t.Fatal("SendResults empty, would block")
	}
	sr := <-p.Pool.SendResults
	assert.Nil(t, sr.Error)
	assert.Equal(t, sr.Connection, gc)
	_, ok := sr.Message.(*GetBlocksMessage)
	assert.True(t, ok)
	assert.False(t, gc.LastSent.IsZero())

	// No connection found for addr
	gc.LastSent = util.ZeroTime()
	gc.Conn = NewDummyConn(addr)
	delete(p.Pool.Pool, gc.Id)
	delete(p.Pool.Addresses, gc.Addr())
	assert.NotPanics(t, func() {
		assert.NotNil(t, v.RequestBlocksFromAddr(p, addr))
	})
	wait()
	assert.Equal(t, len(p.Pool.SendResults), 0)
	assert.True(t, gc.LastSent.IsZero())
}
开发者ID:up4k,项目名称:skycoin,代码行数:48,代码来源:visor_test.go


示例13: testDaemonLoopPingCheckTicker

func testDaemonLoopPingCheckTicker(t *testing.T, d *Daemon, quit chan int,
	sent bool) {
	c := gnetConnection(addr)
	go d.Pool.Pool.ConnectionWriteLoop(c)
	c.LastSent = util.ZeroTime()
	d.Pool.Pool.Pool[c.Id] = c
	d.Pool.Config.IdleCheckRate = time.Millisecond * 10
	go d.Start(quit)
	time.Sleep(time.Millisecond * 15)
	if sent {
		assert.False(t, c.LastSent.IsZero())
	} else {
		assert.True(t, c.LastSent.IsZero())
	}
	c.Close()
	wait()
}
开发者ID:kinghuabg,项目名称:skycoin,代码行数:17,代码来源:daemon_test.go


示例14: TestClearStaleConnections

func TestClearStaleConnections(t *testing.T) {
	dm := newDefaultDaemon()
	defer shutdown(dm)
	c := gnetConnection(addr)
	d := gnetConnection(addrb)
	c.LastReceived = util.ZeroTime()
	d.LastReceived = time.Now()
	dm.Pool.Pool.Pool[1] = c
	dm.Pool.Pool.Pool[2] = d
	assert.NotPanics(t, dm.Pool.clearStaleConnections)
	assert.Equal(t, len(dm.Pool.Pool.DisconnectQueue), 1)
	if len(dm.Pool.Pool.DisconnectQueue) == 0 {
		t.Fatalf("Empty DisconnectQueue, would block")
	}
	de := <-dm.Pool.Pool.DisconnectQueue
	assert.Equal(t, de.ConnId, 1)
	assert.Equal(t, de.Reason, DisconnectIdle)
}
开发者ID:kinghuabg,项目名称:skycoin,代码行数:18,代码来源:daemon_test.go


示例15: TestOnConnect

func TestOnConnect(t *testing.T) {
	d := newDefaultDaemon()

	// Test a valid connection, unsolicited
	e := ConnectEvent{addr, false}
	p, _ := d.Peers.Peers.AddPeer(addr)
	c := setupExistingPool(d.Pool)
	go d.Pool.Pool.ConnectionWriteLoop(c)
	d.pendingConnections[addr] = p
	assert.NotPanics(t, func() { d.onConnect(e) })
	wait()
	assert.Equal(t, len(d.Pool.Pool.SendResults), 1)
	if len(d.Pool.Pool.SendResults) == 0 {
		t.Fatalf("SendResults empty, would block")
	}
	sr := <-d.Pool.Pool.SendResults
	assert.Equal(t, sr.Connection, c)
	assert.Nil(t, sr.Error)
	_, ok := sr.Message.(*IntroductionMessage)
	assert.True(t, ok)
	// This connection should no longer be pending
	assert.Equal(t, len(d.pendingConnections), 0)
	// This is not an outgoing connection, we did not solicit it
	assert.Equal(t, len(d.OutgoingConnections), 0)
	// We should be expecting its version
	assert.Equal(t, len(d.ExpectingIntroductions), 1)
	_, exists := d.ExpectingIntroductions[addr]
	assert.True(t, exists)
	// An introduction should have been sent
	assert.False(t, c.LastSent.IsZero())
	// d.ipCounts should be 1
	assert.Equal(t, d.ipCounts[addrIP], 1)
	// Cleanup
	delete(d.ipCounts, addrIP)
	delete(d.ExpectingIntroductions, addr)
	c.Close()

	// Test a valid connection, solicited
	e = ConnectEvent{addr, true}
	c = gnetConnection(addr)
	go d.Pool.Pool.ConnectionWriteLoop(c)
	d.pendingConnections[addr] = p
	d.Pool.Pool.Addresses[addr] = c
	d.Pool.Pool.Pool[c.Id] = c
	assert.NotPanics(t, func() { d.onConnect(e) })
	wait()
	assert.Equal(t, len(d.Pool.Pool.SendResults), 1)
	if len(d.Pool.Pool.SendResults) == 0 {
		t.Fatalf("SendResults empty, would block")
	}
	sr = <-d.Pool.Pool.SendResults
	assert.Equal(t, sr.Connection, c)
	assert.Nil(t, sr.Error)
	_, ok = sr.Message.(*IntroductionMessage)
	assert.True(t, ok)
	// This connection should no longer be pending
	assert.Equal(t, len(d.pendingConnections), 0)
	// We should mark this as an outgoing connection since we solicited it
	assert.Equal(t, len(d.OutgoingConnections), 1)
	assert.NotNil(t, d.OutgoingConnections[addr])
	// We should be expecting its version
	assert.Equal(t, len(d.ExpectingIntroductions), 1)
	_, exists = d.ExpectingIntroductions[addr]
	assert.True(t, exists)
	// An introduction should have been sent
	assert.False(t, c.LastSent.IsZero())
	// d.ipCounts should be 1
	assert.Equal(t, d.ipCounts[addrIP], 1)
	// Cleanup
	c.Close()
	delete(d.ExpectingIntroductions, addr)
	delete(d.OutgoingConnections, addr)
	delete(d.ipCounts, addrIP)

	// Test a connection that is not connected by the time of processing
	c.LastSent = util.ZeroTime()
	e = ConnectEvent{addr, true}
	delete(d.Pool.Pool.Addresses, addr)
	d.pendingConnections[addr] = p
	assert.NotPanics(t, func() { d.onConnect(e) })
	wait()
	assert.Equal(t, len(d.Pool.Pool.SendResults), 0)
	// This connection should no longer be pending
	assert.Equal(t, len(d.pendingConnections), 0)
	// No message should have been sent
	assert.True(t, c.LastSent.IsZero())
	// We should not be expecting its version
	assert.Equal(t, len(d.ExpectingIntroductions), 0)
	// We should not have recorded it to ipCount
	assert.Equal(t, d.ipCounts[addrIP], 0)

	// Test a connection that is blacklisted
	e = ConnectEvent{addr, true}
	c = gnetConnection(addr)
	go d.Pool.Pool.ConnectionWriteLoop(c)
	d.Peers.Peers.AddBlacklistEntry(addr, time.Hour)
	d.pendingConnections[addr] = p
	d.Pool.Pool.Addresses[addr] = c
	d.Pool.Pool.Pool[c.Id] = c
	assert.NotPanics(t, func() { d.onConnect(e) })
//.........这里部分代码省略.........
开发者ID:kinghuabg,项目名称:skycoin,代码行数:101,代码来源:daemon_test.go


示例16: TestRecordTxn

func TestRecordTxn(t *testing.T) {
	defer cleanupVisor()
	// Test with invalid txn
	mv := setupMasterVisor()
	ut := NewUnconfirmedTxnPool()
	txn, err := makeInvalidTxn(mv)
	assert.Nil(t, err)
	err, known := ut.RecordTxn(mv.blockchain, txn, nil, testBlockSize, 0)
	assert.NotNil(t, err)
	assert.False(t, known)
	assert.Equal(t, len(ut.Txns), 0)

	// Test didAnnounce=false
	mv = setupMasterVisor()
	ut = NewUnconfirmedTxnPool()
	txn, err = makeValidTxn(mv)
	assert.Nil(t, err)
	err, known = ut.RecordTxn(mv.blockchain, txn, nil, testBlockSize, 0)
	assert.Nil(t, err)
	assert.False(t, known)
	assertValidUnspent(t, mv.blockchain, ut.Unspent, txn)
	assertValidUnconfirmed(t, ut.Txns, txn)

	// Test didAnnounce=true
	mv = setupMasterVisor()
	ut = NewUnconfirmedTxnPool()
	txn, err = makeValidTxn(mv)
	assert.Nil(t, err)
	err, known = ut.RecordTxn(mv.blockchain, txn, nil, testBlockSize, 0)
	assert.Nil(t, err)
	assert.False(t, known)
	assertValidUnspent(t, mv.blockchain, ut.Unspent, txn)
	assertValidUnconfirmed(t, ut.Txns, txn)

	// Test txn too large
	mv = setupMasterVisor()
	ut = NewUnconfirmedTxnPool()
	txn, err = makeValidTxn(mv)
	assert.Nil(t, err)
	err, known = ut.RecordTxn(mv.blockchain, txn, nil, txn.Size()-1, 1)
	assertError(t, err, "Transaction too large")
	assert.False(t, known)
	assert.Equal(t, len(ut.Txns), 0)

	// Test where we are receiver of ux outputs
	mv = setupMasterVisor()
	assert.Equal(t, len(mv.blockchain.Unspent.Pool), 1)
	ut = NewUnconfirmedTxnPool()
	txn, err = makeValidTxn(mv)
	assert.Nil(t, err)
	addrs := make(map[cipher.Address]byte, 1)
	addrs[txn.Out[1].Address] = byte(1)
	err, known = ut.RecordTxn(mv.blockchain, txn, addrs, testBlockSize, 0)
	assert.Nil(t, err)
	assert.False(t, known)
	assertValidUnspent(t, mv.blockchain, ut.Unspent, txn)
	assertValidUnconfirmed(t, ut.Txns, txn)

	// Test where we are spender of ux outputs
	mv = setupMasterVisor()
	assert.Equal(t, len(mv.blockchain.Unspent.Pool), 1)
	ut = NewUnconfirmedTxnPool()
	txn, err = makeValidTxnNoChange(mv)
	assert.Nil(t, err)
	addrs = make(map[cipher.Address]byte, 1)
	ux, ok := mv.blockchain.Unspent.Get(txn.In[0])
	assert.True(t, ok)
	addrs[ux.Body.Address] = byte(1)
	err, known = ut.RecordTxn(mv.blockchain, txn, addrs, testBlockSize, 0)
	assert.Nil(t, err)
	assert.False(t, known)
	assertValidUnspent(t, mv.blockchain, ut.Unspent, txn)
	assertValidUnconfirmed(t, ut.Txns, txn)

	// Test where we are both spender and receiver of ux outputs
	mv = setupMasterVisor()
	ut = NewUnconfirmedTxnPool()
	txn, err = makeValidTxn(mv)
	assert.Nil(t, err)
	addrs = make(map[cipher.Address]byte, 2)
	addrs[txn.Out[0].Address] = byte(1)
	ux, ok = mv.blockchain.Unspent.Get(txn.In[0])
	assert.True(t, ok)
	addrs[ux.Body.Address] = byte(1)
	err, known = ut.RecordTxn(mv.blockchain, txn, addrs, testBlockSize, 0)
	assert.Nil(t, err)
	assert.False(t, known)
	assertValidUnspent(t, mv.blockchain, ut.Unspent, txn)
	assertValidUnconfirmed(t, ut.Txns, txn)
	assert.Equal(t, len(ut.Txns), 1)
	assert.Equal(t, len(ut.Unspent), 1)
	for _, uxs := range ut.Unspent {
		assert.Equal(t, len(uxs), 2)
	}

	// Test duplicate Record, should be no-op besides state change
	utx := ut.Txns[txn.Hash()]
	// Set a placeholder value on the utx to check if we overwrote it
	utx.Announced = util.ZeroTime().Add(time.Minute)
	ut.Txns[txn.Hash()] = utx
//.........这里部分代码省略.........
开发者ID:JmAbuDabi,项目名称:skycoin,代码行数:101,代码来源:unconfirmed_test.go


示例17: testRefresh

func testRefresh(t *testing.T, mv *Visor,
	refresh func(checkPeriod, maxAge time.Duration)) {
	up := mv.Unconfirmed
	// Add a transaction that is invalid, but will not be checked yet
	// Add a transaction that is invalid, and will be checked and removed
	invalidTxUnchecked, err := makeValidTxn(mv)
	assert.Nil(t, err)
	invalidTxChecked, err := makeValidTxn(mv)
	assert.Nil(t, err)
	assert.Nil(t, invalidTxUnchecked.Verify())
	assert.Nil(t, invalidTxChecked.Verify())
	// Invalidate it by spending the output that this txn references
	invalidator, err := makeValidTxn(mv)
	assert.Nil(t, err)
	assert.Nil(t, up.InjectTxn(mv.blockchain, invalidator, nil, false))
	assert.Equal(t, len(up.Txns), 1)
	_, err = mv.CreateAndExecuteBlock()
	assert.Nil(t, err)
	assert.Equal(t, len(up.Txns), 0)
	assert.NotNil(t, mv.blockchain.VerifyTransaction(invalidTxUnchecked))
	assert.NotNil(t, mv.blockchain.VerifyTransaction(invalidTxChecked))

	invalidUtxUnchecked := UnconfirmedTxn{
		Txn:       invalidTxUnchecked,
		Received:  util.Now(),
		Checked:   util.Now(),
		Announced: util.ZeroTime(),
	}
	invalidUtxChecked := invalidUtxUnchecked
	invalidUtxChecked.Txn = invalidTxChecked
	invalidUtxUnchecked.Checked = util.Now().Add(time.Hour)
	invalidUtxChecked.Checked = util.Now().Add(-time.Hour)
	up.Txns[invalidUtxUnchecked.Hash()] = invalidUtxUnchecked
	up.Txns[invalidUtxChecked.Hash()] = invalidUtxChecked
	assert.Equal(t, len(up.Txns), 2)
	bh := coin.BlockHeader{}
	for _, ux := range mv.blockchain.TxUxOut(invalidTxUnchecked, bh) {
		up.Unspent.Add(ux)
	}
	for _, ux := range mv.blockchain.TxUxOut(invalidTxChecked, bh) {
		up.Unspent.Add(ux)
	}
	// Add a transaction that is valid, and will not be checked yet
	validTxUnchecked, err := makeValidTxn(mv)
	assert.Nil(t, err)
	assert.Nil(t, up.InjectTxn(mv.blockchain, validTxUnchecked, nil, false))
	assert.Equal(t, len(up.Txns), 3)
	validUtxUnchecked := up.Txns[validTxUnchecked.Hash()]
	validUtxUnchecked.Checked = util.Now().Add(time.Hour)
	up.Txns[validUtxUnchecked.Hash()] = validUtxUnchecked
	// Add a transaction that is valid, and will be checked
	validTxChecked, err := makeValidTxn(mv)
	assert.Nil(t, err)
	assert.Nil(t, up.InjectTxn(mv.blockchain, validTxChecked, nil, false))
	assert.Equal(t, len(up.Txns), 4)
	validUtxChecked := up.Txns[validTxChecked.Hash()]
	validUtxChecked.Checked = util.Now().Add(-time.Hour)
	up.Txns[validUtxChecked.Hash()] = validUtxChecked
	// Add a transaction that is expired
	validTxExpired, err := makeValidTxn(mv)
	assert.Nil(t, err)
	assert.Nil(t, up.InjectTxn(mv.blockchain, validTxExpired, nil, false))
	assert.Equal(t, len(up.Txns), 5)
	validUtxExpired := up.Txns[validTxExpired.Hash()]
	validUtxExpired.Received = util.Now().Add(-time.Hour)
	up.Txns[validTxExpired.Hash()] = validUtxExpired

	// Pre-sanity check
	assert.Equal(t, len(up.Unspent.Arr), 2*5)
	assert.Equal(t, len(up.Txns), 5)

	// Refresh
	checkPeriod := time.Second * 2
	maxAge := time.Second * 4
	refresh(checkPeriod, maxAge)

	// All utxns that are unchecked should be exactly the same
	assert.Equal(t, up.Txns[validUtxUnchecked.Hash()], validUtxUnchecked)
	assert.Equal(t, up.Txns[invalidUtxUnchecked.Hash()], invalidUtxUnchecked)
	// The valid one that is checked should have its checked status updated
	validUtxCheckedUpdated := up.Txns[validUtxChecked.Hash()]
	assert.True(t, validUtxCheckedUpdated.Checked.After(validUtxChecked.Checked))
	validUtxChecked.Checked = validUtxCheckedUpdated.Checked
	assert.Equal(t, validUtxChecked, validUtxCheckedUpdated)
	// The invalid checked one and the expired one should be removed
	_, ok := up.Txns[invalidUtxChecked.Hash()]
	assert.False(t, ok)
	_, ok = up.Txns[validUtxExpired.Hash()]
	assert.False(t, ok)
	// Also, the unspents should have 2 * nRemaining
	assert.Equal(t, len(up.Unspent.Arr), 2*3)
	assert.Equal(t, len(up.Txns), 3)
}
开发者ID:skycoin,项目名称:skycoin,代码行数:93,代码来源:unconfirmed_test.go


示例18: gnetConnection

func gnetConnection(addr string) *gnet.Connection {
	c := gnet.NewConnection(nil, 1, NewDummyConn(addr), 16)
	c.LastSent = util.ZeroTime()
	c.LastReceived = util.ZeroTime()
	return c
}
开发者ID:Chao-Jia,项目名称:skycoin,代码行数:6,代码来源:messages_test.go


示例19: TestGiveBlocksMessageProcess

func TestGiveBlocksMessageProcess(t *testing.T) {
	v, mv := setupVisor()
	d, _ := newVisorDaemon(v)
	defer shutdown(d)
	gc := setupExistingPool(d.Pool)
	go d.Pool.Pool.ConnectionWriteLoop(gc)

	blocks, err := makeBlocks(mv, 2)
	assert.Nil(t, err)
	assert.Equal(t, len(blocks), 2)
	m := NewGiveBlocksMessage(blocks)
	m.c = messageContext(addr)

	// Disabled should have nothing happen
	d.Visor.Config.Disabled = true
	m.Process(d)
	wait()
	assert.Equal(t, len(d.Pool.Pool.SendResults), 0)
	assert.Equal(t, d.Visor.Visor.MostRecentBkSeq(), uint64(0))
	assert.True(t, gc.LastSent.IsZero())

	// Not disabled and blocks were reannounced
	d.Visor.Config.Disabled = false
	gc.Conn = NewDummyConn(addr)
	assert.Equal(t, len(blocks), 2)
	m = NewGiveBlocksMessage(blocks)
	assert.Equal(t, len(m.Blocks), 2)
	m.c = messageContext(addr)
	m.Process(d)
	wait()
	assert.Equal(t, len(d.Pool.Pool.SendResults), 1)
	if len(d.Pool.Pool.SendResults) == 0 {
		t.Fatal("SendResults empty, would block")
	}
	sr := <-d.Pool.Pool.SendResults
	assert.Nil(t, sr.Error)
	assert.Equal(t, sr.Connection, gc)
	_, ok := sr.Message.(*AnnounceBlocksMessage)
	assert.True(t, ok)
	assert.Equal(t, d.Visor.Visor.MostRecentBkSeq(), uint64(2))
	assert.False(t, gc.LastSent.IsZero())

	// Send blocks we have and some we dont, as long as they are in order
	// we can use the ones at the end
	gc.LastSent = util.ZeroTime()
	moreBlocks, err := makeBlocks(mv, 2)
	assert.Nil(t, err)
	blocks = append(blocks, moreBlocks...)
	m = NewGiveBlocksMessage(blocks)
	m.c = messageContext(addr)
	m.Process(d)
	wait()
	assert.Equal(t, len(d.Pool.Pool.SendResults), 1)
	if len(d.Pool.Pool.SendResults) == 0 {
		t.Fatal("SendResults empty, would block")
	}
	sr = <-d.Pool.Pool.SendResults
	assert.Nil(t, sr.Error)
	assert.Equal(t, sr.Connection, gc)
	_, ok = sr.Message.(*AnnounceBlocksMessage)
	assert.True(t, ok)
	assert.Equal(t, d.Visor.Visor.MostRecentBkSeq(), uint64(4))
	assert.False(t, gc.LastSent.IsZero())

	// Send invalid blocks
	gc.LastSent = util.ZeroTime()
	bb := visor.SignedBlock{
		Block: coin.Block{
			Head: coin.BlockHeader{
				BkSeq: uint64(7),
			}}}
	m = NewGiveBlocksMessage([]visor.SignedBlock{bb})
	m.c = messageContext(addr)
	m.Process(d)
	assert.Equal(t, len(d.Pool.Pool.SendResults), 0)
	assert.Equal(t, d.Visor.Visor.MostRecentBkSeq(), uint64(4))
	assert.True(t, gc.LastSent.IsZero())
}
开发者ID:up4k,项目名称:skycoin,代码行数:78,代码来源:visor_test.go


示例20: TestVisorResendTransaction

func TestVisorResendTransaction(t *testing.T) {
	defer cleanupVisor()
	defer gnet.EraseMessages()
	p, gc := setupPool()
	go p.Pool.ConnectionWriteLoop(gc)
	vc, mv := setupVisor()
	v := NewVisor(vc)
	assert.Equal(t, len(v.Visor.Unconfirmed.Txns), 0)

	// Nothing should happen if txn unknown
	v.ResendTransaction(coin.SumSHA256([]byte("garbage")), p)
	wait()
	assert.Equal(t, len(p.Pool.SendResults), 0)
	assert.Equal(t, len(p.Pool.SendResults), 0)
	assert.Equal(t, len(v.Visor.Unconfirmed.Txns), 0)
	assert.True(t, gc.LastSent.IsZero())

	// give the visor some coins, and make a spend to add a txn
	assert.Nil(t, transferCoins(mv, v.Visor))
	tx, err := v.Spend(visor.Balance{10e6, 0}, 0,
		mv.Wallet.GetAddresses()[0], p)
	assert.Nil(t, err)
	wait()
	assert.Equal(t, len(p.Pool.SendResults), 1)
	if len(p.Pool.SendResults) == 0 {
		t.Fatal("SendResults empty, would block")
	}
	<-p.Pool.SendResults
	assert.Equal(t, len(v.Visor.Unconfirmed.Txns), 1)
	h := tx.Hash()
	ut := v.Visor.Unconfirmed.Txns[h]
	ut.Announced = util.ZeroTime()
	v.Visor.Unconfirmed.Txns[h] = ut
	assert.True(t, v.Visor.Unconfirmed.Txns[h].Announced.IsZero())
	// Reset the sent timer since we made a successful spend
	gc.LastSent = util.ZeroTime()

	// Nothing should send if disabled
	v.Config.Disabled = true
	v.ResendTransaction(h, p)
	wait()
	assert.Equal(t, len(p.Pool.SendResults), 0)
	ann := v.Visor.Unconfirmed.Txns[h].Announced
	assert.True(t, ann.IsZero())
	assert.True(t, gc.LastSent.IsZero())

	// Should have resent
	v.Config.Disabled = false
	gc.Conn = NewDummyConn(addr)
	v.ResendTransaction(h, p)
	wait()
	assert.Equal(t, len(p.Pool.SendResults), 1)
	if len(p.Pool.SendResults) == 0 {
		t.Fatal("SendResults empty, would block")
	}
	sr := <-p.Pool.SendResults
	assert.Nil(t, sr.Error)
	assert.Equal(t, sr.Connection, gc)
	_, ok := sr.Message.(*GiveTxnsMessage)
	assert.True(t, ok)
	ann = v.Visor.Unconfirmed.Txns[h].Announced
	// Announced state should not be updated until we process it
	assert.True(t, ann.IsZero())
	assert.False(t, gc.LastSent.IsZero())
}
开发者ID:up4k,项目名称:skycoin,代码行数:65,代码来源:visor_test.go



注:本文中的github.com/skycoin/skycoin/src/util.ZeroTime函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang visor.Visor类代码示例发布时间:2022-05-28
下一篇:
Golang util.UnixNow函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap