7 dbm "github.com/vapor/database/leveldb"
8 "github.com/vapor/database/storage"
9 "github.com/vapor/protocol"
10 "github.com/vapor/protocol/bc"
11 "github.com/vapor/protocol/bc/types"
12 "github.com/vapor/protocol/state"
13 "github.com/vapor/testutil"
16 func TestSaveChainStatus(t *testing.T) {
17 testDB := dbm.NewDB("testdb", "leveldb", "temp")
23 store := NewStore(testDB)
25 blockHeader := &types.BlockHeader{Height: 100}
26 blockHash := blockHeader.Hash() //Hash: bc.Hash{V0: 0, V1: 1, V2: 2, V3: 3}
27 view := &state.UtxoViewpoint{
28 Entries: map[bc.Hash]*storage.UtxoEntry{
29 bc.Hash{V0: 1, V1: 2, V2: 3, V3: 4}: &storage.UtxoEntry{Type: storage.NormalUTXOType, BlockHeight: 100, Spent: false},
30 bc.Hash{V0: 1, V1: 2, V2: 3, V3: 4}: &storage.UtxoEntry{Type: storage.CoinbaseUTXOType, BlockHeight: 100, Spent: true},
31 bc.Hash{V0: 1, V1: 1, V2: 3, V3: 4}: &storage.UtxoEntry{Type: storage.NormalUTXOType, BlockHeight: 100, Spent: true},
32 bc.Hash{V0: 1, V1: 1, V2: 3, V3: 5}: &storage.UtxoEntry{Type: storage.CrosschainUTXOType, BlockHeight: 100, Spent: false},
33 bc.Hash{V0: 1, V1: 1, V2: 3, V3: 6}: &storage.UtxoEntry{Type: storage.CrosschainUTXOType, BlockHeight: 100, Spent: true},
34 bc.Hash{V0: 1, V1: 3, V2: 3, V3: 7}: &storage.UtxoEntry{Type: storage.VoteUTXOType, BlockHeight: 100, Spent: false},
35 bc.Hash{V0: 1, V1: 3, V2: 3, V3: 7}: &storage.UtxoEntry{Type: storage.VoteUTXOType, BlockHeight: 100, Spent: true},
39 if err := store.SaveChainStatus(blockHeader, blockHeader, []*types.BlockHeader{blockHeader}, view, []*state.ConsensusResult{}); err != nil {
43 expectStatus := &protocol.BlockStoreState{Height: blockHeader.Height, Hash: &blockHash, IrreversibleHeight: blockHeader.Height, IrreversibleHash: &blockHash}
44 if !testutil.DeepEqual(store.GetStoreStatus(), expectStatus) {
45 t.Errorf("got block status:%v, expect block status:%v", store.GetStoreStatus(), expectStatus)
48 for hash, utxo := range view.Entries {
49 if (utxo.Type == storage.NormalUTXOType) && utxo.Spent {
52 if (utxo.Type == storage.CrosschainUTXOType) && (!utxo.Spent) {
55 if (utxo.Type == storage.VoteUTXOType) && (utxo.Spent) {
59 gotUtxo, err := store.GetUtxo(&hash)
64 if !testutil.DeepEqual(utxo, gotUtxo) {
65 t.Errorf("got utxo entry:%v, expect utxo entry:%v", gotUtxo, utxo)
70 func TestSaveBlock(t *testing.T) {
71 testDB := dbm.NewDB("testdb", "leveldb", "temp")
77 store := NewStore(testDB)
78 block := mockGenesisBlock()
79 status := &bc.TransactionStatus{VerifyStatus: []*bc.TxVerifyResult{{StatusFail: true}}}
80 if err := store.SaveBlock(block, status); err != nil {
84 blockHash := block.Hash()
85 gotBlock, err := store.GetBlock(&blockHash)
90 gotBlock.Transactions[0].Tx.SerializedSize = 0
91 gotBlock.Transactions[0].SerializedSize = 0
92 if !testutil.DeepEqual(block, gotBlock) {
93 t.Errorf("got block:%v, expect block:%v", gotBlock, block)
96 gotStatus, err := store.GetTransactionStatus(&blockHash)
101 if !testutil.DeepEqual(status, gotStatus) {
102 t.Errorf("got status:%v, expect status:%v", gotStatus, status)
105 data := store.db.Get(calcBlockHeaderKey(&blockHash))
106 gotBlockHeader := types.BlockHeader{}
107 if err := gotBlockHeader.UnmarshalText(data); err != nil {
111 if !testutil.DeepEqual(block.BlockHeader, gotBlockHeader) {
112 t.Errorf("got block header:%v, expect block header:%v", gotBlockHeader, block.BlockHeader)
116 func mockGenesisBlock() *types.Block {
117 txData := types.TxData{
119 Inputs: []*types.TxInput{
120 types.NewCoinbaseInput([]byte("Information is power. -- Jan/11/2013. Computing is power. -- Apr/24/2018.")),
122 Outputs: []*types.TxOutput{
123 types.NewVoteOutput(bc.AssetID{V0: 1}, uint64(10000), []byte{0x51}, []byte{0x51}),
126 tx := types.NewTx(txData)
127 txStatus := bc.NewTransactionStatus()
128 txStatus.SetStatus(0, false)
129 txStatusHash, _ := types.TxStatusMerkleRoot(txStatus.VerifyStatus)
130 merkleRoot, _ := types.TxMerkleRoot([]*bc.Tx{tx.Tx})
131 block := &types.Block{
132 BlockHeader: types.BlockHeader{
135 Timestamp: 1528945000,
136 BlockCommitment: types.BlockCommitment{
137 TransactionsMerkleRoot: merkleRoot,
138 TransactionStatusHash: txStatusHash,
141 Transactions: []*types.Tx{tx},