func setupMasterVisor() VisorConfig { cleanupVisor() coin.SetAddressVersion("test") c := NewVisorConfig() c.Config.IsMaster = true c.Config.MasterKeys = visor.NewWalletEntry() return c }
func setupMasterVisor() VisorConfig { cleanupVisor() coin.SetAddressVersion("test") c := NewVisorConfig() c.Config.IsMaster = true mw := visor.NewWalletEntry() c.Config.MasterKeys = mw c.Config.GenesisSignature = createGenesisSignature(mw) return c }
func newDefaultDaemon() *Daemon { cleanupPeers() c := NewConfig() we := visor.NewWalletEntry() c.Visor.Config.MasterKeys = we c.Visor.Config.GenesisSignature = createGenesisSignature(we) c.Visor.Disabled = true c.DHT.Disabled = true return NewDaemon(c) }
func makeBlocks(mv *visor.Visor, n int) ([]visor.SignedBlock, error) { dest := visor.NewWalletEntry() blocks := make([]visor.SignedBlock, n) for i := 0; i < n; i++ { tx, err := mv.Spend(visor.Balance{10 * 1e6, 0}, 0, dest.Address) if err != nil { return nil, err } mv.RecordTxn(tx) sb, err := mv.CreateAndExecuteBlock() if err != nil { return nil, err } blocks[i] = sb } return blocks, nil }
// Returns an appropriate VisorConfig and a master visor func setupVisor() (VisorConfig, *visor.Visor) { coin.SetAddressVersion("test") // Make a new master visor + blockchain // Get the signed genesis block, mw := visor.NewWalletEntry() mvc := visor.NewVisorConfig() mvc.IsMaster = true mvc.MasterKeys = mw mvc.CoinHourBurnFactor = 0 mv := visor.NewVisor(mvc) sb := mv.GetGenesisBlock() // Use the master values for a client configuration c := NewVisorConfig() c.Config.IsMaster = false c.Config.MasterKeys = mw c.Config.MasterKeys.Secret = coin.SecKey{} c.Config.GenesisTimestamp = sb.Block.Head.Time c.Config.GenesisSignature = sb.Sig return c, mv }
// Writes a wallet entry to disk at filename func writeMasterKeysFile() (visor.WalletEntry, error) { we := visor.NewWalletEntry() rwe := visor.NewReadableWalletEntry(&we) err := rwe.Save(testMasterKeysFile) return we, err }
func TestCreateAndPublishBlock(t *testing.T) { defer cleanupVisor() defer gnet.EraseMessages() p, gc := setupPool() vc, mv := setupVisor() dest := visor.NewWalletEntry() go p.Pool.ConnectionWriteLoop(gc) // Disabled vc.Disabled = true vc.Config = mv.Config v := NewVisor(vc) v.Visor = mv err := v.CreateAndPublishBlock(p) assert.NotNil(t, err) wait() assert.Equal(t, err.Error(), "Visor disabled") assert.Equal(t, len(p.Pool.SendResults), 0) assert.Equal(t, v.Visor.MostRecentBkSeq(), uint64(0)) // Created and sent vc.Disabled = false vc.Config.IsMaster = true vc.Config = mv.Config v = NewVisor(vc) gc.Conn = NewDummyConn(addr) _, err = v.Spend(visor.Balance{10 * 1e6, 0}, 0, dest.Address, 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 err = v.CreateAndPublishBlock(p) wait() 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") } sr := <-p.Pool.SendResults assert.Nil(t, sr.Error) assert.Equal(t, sr.Connection, gc) _, ok := sr.Message.(*GiveBlocksMessage) assert.True(t, ok) assert.Equal(t, v.Visor.MostRecentBkSeq(), uint64(1)) // Can't create, don't have coins // First, spend all of our coins vc, _ = setupVisor() vc.Config.IsMaster = true vc.Config.MasterKeys = visor.NewWalletEntry() vc.Disabled = false v = NewVisor(vc) tx, err := v.Spend(visor.Balance{100 * 1e6 * 1e6, 0}, 1024*1024, dest.Address, p) mv.RecordTxn(tx) wait() assert.Nil(t, err) assert.Equal(t, len(p.Pool.SendResults), 1) for len(p.Pool.SendResults) > 0 { <-p.Pool.SendResults } err = v.CreateAndPublishBlock(p) assert.Nil(t, err) wait() assert.Equal(t, len(p.Pool.SendResults), 1) for len(p.Pool.SendResults) > 0 { <-p.Pool.SendResults } // No coins to spend, fail assert.Equal(t, v.Visor.MostRecentBkSeq(), uint64(1)) _, err = v.Spend(visor.Balance{10 * 1e6, 0}, 0, dest.Address, p) assert.NotNil(t, err) wait() assert.Equal(t, len(p.Pool.SendResults), 0) err = v.CreateAndPublishBlock(p) assert.NotNil(t, err) wait() assert.Equal(t, len(p.Pool.SendResults), 0) assert.Equal(t, v.Visor.MostRecentBkSeq(), uint64(1)) }
func testBlockCreationTicker(t *testing.T, vcfg VisorConfig, master bool, mv *visor.Visor) { vcfg.Config.BlockCreationInterval = 1 defer gnet.EraseMessages() c := NewConfig() c.Visor = vcfg c.Daemon.DisableNetworking = false d := NewDaemon(c) if !master { err := transferCoins(mv, d.Visor.Visor) assert.Nil(t, err) } quit := make(chan int) defer closeDaemon(d, quit) gc := setupExistingPool(d.Pool) go d.Pool.Pool.ConnectionWriteLoop(gc) assert.True(t, gc.LastSent.IsZero()) assert.Equal(t, len(d.Pool.Pool.Pool), 1) assert.Equal(t, d.Pool.Pool.Pool[gc.Id], gc) assert.Equal(t, len(d.Pool.Pool.Addresses), 1) start := 0 if !master { start = 1 } assert.Equal(t, d.Visor.Visor.MostRecentBkSeq(), uint64(start)) go d.Start(quit) time.Sleep(time.Second + (time.Millisecond * 50)) // Creation should not have occured, because no transaction assert.Equal(t, d.Visor.Visor.MostRecentBkSeq(), uint64(start)) assert.Equal(t, len(d.Pool.Pool.SendResults), 0) // Creation should occur with a transaction, if not a master // Make a transaction assert.False(t, d.Visor.Config.Disabled) assert.True(t, gc.LastSent.IsZero()) dest := visor.NewWalletEntry() tx, err := d.Visor.Spend(visor.Balance{10 * 1e6, 0}, 0, dest.Address, d.Pool) wait() assert.Nil(t, err) assert.Equal(t, d.Pool.Pool.Pool[gc.Id], gc) assert.Equal(t, len(d.Pool.Pool.DisconnectQueue), 0) assert.Equal(t, len(d.Pool.Pool.Pool), 1) assert.Equal(t, len(d.Pool.Pool.Addresses), 1) // Since the daemon loop is running, it will have processed the SendResult // Instead we can check if the txn was announced assert.Equal(t, len(d.Pool.Pool.SendResults), 0) ut := d.Visor.Visor.Unconfirmed.Txns[tx.Hash()] assert.False(t, ut.Announced.IsZero()) assert.False(t, gc.LastSent.IsZero()) ls := gc.LastSent // Now, block should be created time.Sleep(time.Second + (time.Millisecond * 50)) final := start if master { final += 1 } assert.Equal(t, len(d.Pool.Pool.Pool), 1) // Again, we can't check SendResults since the daemon loop is running. // We can only check that LastSent was updated, if its the master and it // created the block. if master { assert.True(t, gc.LastSent.After(ls)) } else { assert.Equal(t, gc.LastSent, ls) } assert.Equal(t, d.Visor.Visor.MostRecentBkSeq(), uint64(final)) assert.False(t, gc.LastSent.IsZero()) }
func makeValidTxnNoError(t *testing.T, mv *visor.Visor) coin.Transaction { we := visor.NewWalletEntry() tx, err := mv.Spend(visor.Balance{10 * 1e6, 0}, 0, we.Address) assert.Nil(t, err) return tx }
func makeValidTxn(mv *visor.Visor) (coin.Transaction, error) { we := visor.NewWalletEntry() return mv.Spend(visor.Balance{10 * 1e6, 0}, 0, we.Address) }