github.com/datachainlab/burrow@v0.25.0/execution/state/dump_test.go (about) 1 package state 2 3 import ( 4 "fmt" 5 "math/rand" 6 "testing" 7 8 "github.com/hyperledger/burrow/acm" 9 "github.com/hyperledger/burrow/genesis" 10 "github.com/stretchr/testify/require" 11 dbm "github.com/tendermint/tendermint/libs/db" 12 13 "github.com/hyperledger/burrow/binary" 14 "github.com/hyperledger/burrow/crypto" 15 "github.com/hyperledger/burrow/dump" 16 "github.com/hyperledger/burrow/execution/exec" 17 "github.com/hyperledger/burrow/execution/names" 18 ) 19 20 type MockDumpReader struct { 21 accounts int 22 storage int 23 names int 24 events int 25 } 26 27 func (m *MockDumpReader) Next() (*dump.Dump, error) { 28 // acccounts 29 row := dump.Dump{Height: 102} 30 31 if m.accounts > 0 { 32 var addr crypto.Address 33 binary.PutUint64BE(addr.Bytes(), uint64(m.accounts)) 34 35 row.Account = &acm.Account{ 36 Address: addr, 37 Balance: 102, 38 } 39 40 if m.accounts%2 > 0 { 41 row.Account.Code = make([]byte, rand.Int()%10000) 42 } else { 43 row.Account.PublicKey = crypto.PublicKey{} 44 } 45 m.accounts-- 46 } else if m.storage > 0 { 47 var addr crypto.Address 48 binary.PutUint64BE(addr.Bytes(), uint64(m.storage)) 49 storagelen := rand.Int() % 25 50 51 row.AccountStorage = &dump.AccountStorage{ 52 Address: addr, 53 Storage: make([]*dump.Storage, storagelen), 54 } 55 56 for i := 0; i < storagelen; i++ { 57 row.AccountStorage.Storage[i] = &dump.Storage{} 58 } 59 60 m.storage-- 61 } else if m.names > 0 { 62 row.Name = &names.Entry{ 63 Name: fmt.Sprintf("name%d", m.names), 64 Data: fmt.Sprintf("data%x", m.names), 65 Owner: crypto.ZeroAddress, 66 Expires: 1337, 67 } 68 m.names-- 69 } else if m.events > 0 { 70 datalen := rand.Int() % 10 71 data := make([]byte, datalen*32) 72 topiclen := rand.Int() % 5 73 topics := make([]binary.Word256, topiclen) 74 row.EVMEvent = &dump.EVMEvent{ 75 ChainID: "MockyChain", 76 Event: &exec.LogEvent{ 77 Address: crypto.ZeroAddress, 78 Data: data, 79 Topics: topics, 80 }, 81 } 82 m.events-- 83 } else { 84 return nil, nil 85 } 86 87 return &row, nil 88 } 89 90 func BenchmarkLoadDump(b *testing.B) { 91 for n := 0; n < b.N; n++ { 92 mock := MockDumpReader{ 93 accounts: 2000, 94 storage: 1000, 95 names: 100, 96 events: 100000, 97 } 98 st, err := MakeGenesisState(dbm.NewMemDB(), &genesis.GenesisDoc{}) 99 require.NoError(b, err) 100 err = st.LoadDump(&mock) 101 require.NoError(b, err) 102 err = st.InitialCommit() 103 require.NoError(b, err) 104 } 105 }