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 }
func NewEmptySimpleWallet() Wallet { idHash := coin.SumSHA256(secp256k1.RandByte(256)) id := WalletID(hex.EncodeToString(idHash[:16])) return &SimpleWallet{ Filename: NewWalletFilename(id), Entries: WalletEntries{}, ID: id, } }
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(), } }
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()) }
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()) }
func randSHA256(t *testing.T) coin.SHA256 { return coin.SumSHA256(randBytes(t, 32)) }
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])) }
func NewDeterministicWalletSeed() DeterministicWalletSeed { seed := coin.SumSHA256(secp256k1.RandByte(DeterministicSeedLength)) return DeterministicWalletSeed(seed) }
func randSHA256() coin.SHA256 { b := make([]byte, 128) rand.Read(b) return coin.SumSHA256(b) }