forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 142
/
taiko_api_test.go
128 lines (99 loc) 路 3.21 KB
/
taiko_api_test.go
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package ethclient
import (
"context"
"crypto/rand"
"math/big"
"testing"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"github.com/stretchr/testify/require"
)
func newTaikoAPITestClient(t *testing.T) (*Client, []*types.Block, ethdb.Database) {
// Generate test chain.
blocks := generateTestChain()
// Create node
n, err := node.New(&node.Config{})
require.Nil(t, err)
// Create Ethereum Service
config := ðconfig.Config{Genesis: genesis}
config.Ethash.PowMode = ethash.ModeFake
ethservice, err := eth.New(n, config)
require.Nil(t, err)
n.RegisterAPIs([]rpc.API{
{
Namespace: "taiko",
Version: params.VersionWithMeta,
Service: eth.NewTaikoAPIBackend(ethservice),
Public: true,
},
})
// Start node
require.Nil(t, n.Start())
// Insert test blocks
_, err = ethservice.BlockChain().InsertChain(blocks[1:])
require.Nil(t, err)
rpcClient, _ := n.Attach()
return NewClient(rpcClient), blocks, ethservice.ChainDb()
}
func TestHeadL1Origin(t *testing.T) {
ec, blocks, db := newTaikoAPITestClient(t)
headerHash := blocks[len(blocks)-1].Hash()
l1OriginFound, err := ec.HeadL1Origin(context.Background())
require.Equal(t, ethereum.NotFound.Error(), err.Error())
require.Nil(t, l1OriginFound)
testL1Origin := &rawdb.L1Origin{
BlockID: randomBigInt(),
L2BlockHash: headerHash,
L1BlockHeight: randomBigInt(),
L1BlockHash: randomHash(),
}
rawdb.WriteL1Origin(db, testL1Origin.BlockID, testL1Origin)
rawdb.WriteHeadL1Origin(db, testL1Origin.BlockID)
l1OriginFound, err = ec.HeadL1Origin(context.Background())
require.Nil(t, err)
require.Equal(t, testL1Origin, l1OriginFound)
}
func TestL1OriginByID(t *testing.T) {
ec, blocks, db := newTaikoAPITestClient(t)
headerHash := blocks[len(blocks)-1].Hash()
testL1Origin := &rawdb.L1Origin{
BlockID: randomBigInt(),
L2BlockHash: headerHash,
L1BlockHeight: randomBigInt(),
L1BlockHash: randomHash(),
}
l1OriginFound, err := ec.L1OriginByID(context.Background(), testL1Origin.BlockID)
require.Equal(t, ethereum.NotFound.Error(), err.Error())
require.Nil(t, l1OriginFound)
rawdb.WriteL1Origin(db, testL1Origin.BlockID, testL1Origin)
rawdb.WriteHeadL1Origin(db, testL1Origin.BlockID)
l1OriginFound, err = ec.L1OriginByID(context.Background(), testL1Origin.BlockID)
require.Nil(t, err)
require.Equal(t, testL1Origin, l1OriginFound)
}
// randomHash generates a random blob of data and returns it as a hash.
func randomHash() common.Hash {
var hash common.Hash
if n, err := rand.Read(hash[:]); n != common.HashLength || err != nil {
panic(err)
}
return hash
}
// randomBigInt generates a random big integer.
func randomBigInt() *big.Int {
randomBigInt, err := rand.Int(rand.Reader, common.Big256)
if err != nil {
log.Crit(err.Error())
}
return randomBigInt
}