Esempio n. 1
0
func makeUxBodyWithSecret(t *testing.T) (coin.UxBody, coin.SecKey) {
	p, s := coin.GenerateKeyPair()
	return coin.UxBody{
		SrcTransaction: coin.SumSHA256(randBytes(t, 128)),
		Address:        coin.AddressFromPubKey(p),
		Coins:          10e6,
		Hours:          100,
	}, s
}
Esempio n. 2
0
func NewEmptySimpleWallet() Wallet {
	idHash := coin.SumSHA256(secp256k1.RandByte(256))
	id := WalletID(hex.EncodeToString(idHash[:16]))
	return &SimpleWallet{
		Filename: NewWalletFilename(id),
		Entries:  WalletEntries{},
		ID:       id,
	}
}
Esempio n. 3
0
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(),
	}
}
Esempio n. 4
0
func TestGetTxnsMessageProcess(t *testing.T) {
	v, _ := setupVisor()
	d, _ := newVisorDaemon(v)
	defer shutdown(d)
	gc := setupExistingPool(d.Pool)
	p := d.Pool
	go p.Pool.ConnectionWriteLoop(gc)
	tx := createUnconfirmedTxn()
	tx.Txn.Head.Hash = coin.SumSHA256([]byte("asdadwadwada"))
	txns := []coin.SHA256{tx.Txn.Hash()}
	m := NewGetTxnsMessage(txns)
	m.c = messageContext(addr)
	go p.Pool.ConnectionWriteLoop(m.c.Conn)
	defer m.c.Conn.Close()

	// We don't have any to reply with
	assert.NotPanics(t, func() { m.Process(d) })
	assert.True(t, m.c.Conn.LastSent.IsZero())

	// Disabled, nothing should happen
	d.Visor.Visor.Unconfirmed.Txns[tx.Txn.Hash()] = tx
	d.Visor.Config.Disabled = true
	assert.NotPanics(t, func() { m.Process(d) })
	assert.True(t, m.c.Conn.LastSent.IsZero())

	// We have some to reply with
	d.Visor.Config.Disabled = false
	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.Equal(t, sr.Connection, m.c.Conn)
	assert.Nil(t, sr.Error)
	_, ok := sr.Message.(*GiveTxnsMessage)
	assert.True(t, ok)
	assert.False(t, m.c.Conn.LastSent.IsZero())
	// Should not be broadcast to others
	assert.True(t, gc.LastSent.IsZero())
}
Esempio n. 5
0
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())
}
Esempio n. 6
0
func randSHA256(t *testing.T) coin.SHA256 {
	return coin.SumSHA256(randBytes(t, 32))
}
Esempio n. 7
0
func (self *DeterministicWalletSeed) toWalletID() WalletID {
	// Uses the first 16 bytes of SHA256(seed) as id
	shaid := coin.SumSHA256(self[:])
	return WalletID(hex.EncodeToString(shaid[:16]))
}
Esempio n. 8
0
func NewDeterministicWalletSeed() DeterministicWalletSeed {
	seed := coin.SumSHA256(secp256k1.RandByte(DeterministicSeedLength))
	return DeterministicWalletSeed(seed)
}
Esempio n. 9
0
func randSHA256() coin.SHA256 {
	b := make([]byte, 128)
	rand.Read(b)
	return coin.SumSHA256(b)
}