github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/core/rawdb/accessors_indexes_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:34</date> 10 //</624342616997105664> 11 12 13 package rawdb 14 15 import ( 16 "math/big" 17 "testing" 18 19 "github.com/ethereum/go-ethereum/common" 20 "github.com/ethereum/go-ethereum/core/types" 21 "github.com/ethereum/go-ethereum/ethdb" 22 ) 23 24 //可以存储和检索位置查找元数据的测试。 25 func TestLookupStorage(t *testing.T) { 26 db := ethdb.NewMemDatabase() 27 28 tx1 := types.NewTransaction(1, common.BytesToAddress([]byte{0x11}), big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11}) 29 tx2 := types.NewTransaction(2, common.BytesToAddress([]byte{0x22}), big.NewInt(222), 2222, big.NewInt(22222), []byte{0x22, 0x22, 0x22}) 30 tx3 := types.NewTransaction(3, common.BytesToAddress([]byte{0x33}), big.NewInt(333), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33}) 31 txs := []*types.Transaction{tx1, tx2, tx3} 32 33 block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, txs, nil, nil) 34 35 //检查原始数据库中是否没有事务条目 36 for i, tx := range txs { 37 if txn, _, _, _ := ReadTransaction(db, tx.Hash()); txn != nil { 38 t.Fatalf("tx #%d [%x]: non existent transaction returned: %v", i, tx.Hash(), txn) 39 } 40 } 41 //将所有事务插入数据库,并验证内容 42 WriteBlock(db, block) 43 WriteTxLookupEntries(db, block) 44 45 for i, tx := range txs { 46 if txn, hash, number, index := ReadTransaction(db, tx.Hash()); txn == nil { 47 t.Fatalf("tx #%d [%x]: transaction not found", i, tx.Hash()) 48 } else { 49 if hash != block.Hash() || number != block.NumberU64() || index != uint64(i) { 50 t.Fatalf("tx #%d [%x]: positional metadata mismatch: have %x/%d/%d, want %x/%v/%v", i, tx.Hash(), hash, number, index, block.Hash(), block.NumberU64(), i) 51 } 52 if tx.Hash() != txn.Hash() { 53 t.Fatalf("tx #%d [%x]: transaction mismatch: have %v, want %v", i, tx.Hash(), txn, tx) 54 } 55 } 56 } 57 //删除交易记录并检查清除 58 for i, tx := range txs { 59 DeleteTxLookupEntry(db, tx.Hash()) 60 if txn, _, _, _ := ReadTransaction(db, tx.Hash()); txn != nil { 61 t.Fatalf("tx #%d [%x]: deleted transaction returned: %v", i, tx.Hash(), txn) 62 } 63 } 64 } 65