aboutsummaryrefslogtreecommitdiffstats
path: root/core/rawdb/accessors_indexes_test.go
blob: ff59fad4ca015463c56fc77f98aa15e301128081 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2018 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package rawdb

import (
    "math/big"
    "testing"

    "github.com/dexon-foundation/dexon/common"
    "github.com/dexon-foundation/dexon/core/types"
    "github.com/dexon-foundation/dexon/ethdb"
)

// Tests that positional lookup metadata can be stored and retrieved.
func TestLookupStorage(t *testing.T) {
    db := ethdb.NewMemDatabase()

    tx1 := types.NewTransaction(1, common.BytesToAddress([]byte{0x11}), big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11})
    tx2 := types.NewTransaction(2, common.BytesToAddress([]byte{0x22}), big.NewInt(222), 2222, big.NewInt(22222), []byte{0x22, 0x22, 0x22})
    tx3 := types.NewTransaction(3, common.BytesToAddress([]byte{0x33}), big.NewInt(333), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33})
    txs := []*types.Transaction{tx1, tx2, tx3}

    block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, txs, nil, nil)

    // Check that no transactions entries are in a pristine database
    for i, tx := range txs {
        if txn, _, _, _ := ReadTransaction(db, tx.Hash()); txn != nil {
            t.Fatalf("tx #%d [%x]: non existent transaction returned: %v", i, tx.Hash(), txn)
        }
    }
    // Insert all the transactions into the database, and verify contents
    WriteBlock(db, block)
    WriteTxLookupEntries(db, block)

    for i, tx := range txs {
        if txn, hash, number, index := ReadTransaction(db, tx.Hash()); txn == nil {
            t.Fatalf("tx #%d [%x]: transaction not found", i, tx.Hash())
        } else {
            if hash != block.Hash() || number != block.NumberU64() || index != uint64(i) {
                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)
            }
            if tx.Hash() != txn.Hash() {
                t.Fatalf("tx #%d [%x]: transaction mismatch: have %v, want %v", i, tx.Hash(), txn, tx)
            }
        }
    }
    // Delete the transactions and check purge
    for i, tx := range txs {
        DeleteTxLookupEntry(db, tx.Hash())
        if txn, _, _, _ := ReadTransaction(db, tx.Hash()); txn != nil {
            t.Fatalf("tx #%d [%x]: deleted transaction returned: %v", i, tx.Hash(), txn)
        }
    }
}