-
Notifications
You must be signed in to change notification settings - Fork 881
Expand file tree
/
Copy pathheight_availability_test.go
More file actions
172 lines (147 loc) · 5.44 KB
/
height_availability_test.go
File metadata and controls
172 lines (147 loc) · 5.44 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package evmrpc
import (
"context"
"encoding/hex"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/eth/filters"
"github.com/ethereum/go-ethereum/rpc"
"github.com/sei-protocol/sei-chain/sei-cosmos/client"
sdk "github.com/sei-protocol/sei-chain/sei-cosmos/types"
"github.com/sei-protocol/sei-chain/sei-tendermint/libs/bytes"
"github.com/sei-protocol/sei-chain/sei-tendermint/rpc/client/mock"
"github.com/sei-protocol/sei-chain/sei-tendermint/rpc/coretypes"
tmtypes "github.com/sei-protocol/sei-chain/sei-tendermint/types"
evmtypes "github.com/sei-protocol/sei-chain/x/evm/types"
"github.com/stretchr/testify/require"
)
const highBlockHashHex = "0xfeedfeedfeedfeedfeedfeedfeedfeedfeedfeedfeedfeedfeedfeedfeedfeed"
type heightTestClient struct {
mock.Client
highHash bytes.HexBytes
highBlock *coretypes.ResultBlock
earliest int64
latest int64
}
func newHeightTestClient(highHeight, earliest, latest int64) *heightTestClient {
return &heightTestClient{
Client: mock.Client{},
highHash: bytes.HexBytes(mustDecodeHex(highBlockHashHex[2:])),
highBlock: &coretypes.ResultBlock{
Block: &tmtypes.Block{
Header: tmtypes.Header{Height: highHeight},
},
},
earliest: earliest,
latest: latest,
}
}
func (c *heightTestClient) BlockByHash(ctx context.Context, hash bytes.HexBytes) (*coretypes.ResultBlock, error) {
if hash.String() == c.highHash.String() {
return c.highBlock, nil
}
return &coretypes.ResultBlock{
Block: &tmtypes.Block{Header: tmtypes.Header{Height: c.latest}},
}, nil
}
func (c *heightTestClient) Block(ctx context.Context, height *int64) (*coretypes.ResultBlock, error) {
h := c.latest
if height != nil {
h = *height
}
return &coretypes.ResultBlock{Block: &tmtypes.Block{Header: tmtypes.Header{Height: h}}}, nil
}
func (c *heightTestClient) BlockResults(context.Context, *int64) (*coretypes.ResultBlockResults, error) {
return &coretypes.ResultBlockResults{}, nil
}
func (c *heightTestClient) Status(context.Context) (*coretypes.ResultStatus, error) {
return &coretypes.ResultStatus{
SyncInfo: coretypes.SyncInfo{
LatestBlockHeight: c.latest,
EarliestBlockHeight: c.earliest,
},
}, nil
}
func mustDecodeHex(h string) []byte {
bz, err := hex.DecodeString(h)
if err != nil {
panic(err)
}
return bz
}
func testTxConfigProvider(int64) client.TxConfig { return nil }
func testCtxProvider(int64) sdk.Context { return sdk.Context{} }
func TestBlockAPIEnsureHeightUnavailable(t *testing.T) {
t.Parallel()
earliest := int64(1)
latest := int64(100)
highHeight := latest + 5
client := newHeightTestClient(highHeight, earliest, latest)
watermarks := NewWatermarkManager(client, testCtxProvider, nil, nil)
api := NewBlockAPI(client, nil, testCtxProvider, testTxConfigProvider, ConnectionTypeHTTP, watermarks, nil, nil)
_, err := api.GetBlockByHash(context.Background(), common.HexToHash(highBlockHashHex), false)
require.Error(t, err)
require.Contains(t, err.Error(), "requested height")
}
// TestGetBlockTransactionCountByHashGenesis verifies that the genesis block hash returned by
// eth_getBlockByNumber("0x0") is accepted by eth_getBlockTransactionCountByHash (consistency).
func TestGetBlockTransactionCountByHashGenesis(t *testing.T) {
t.Parallel()
api := NewBlockAPI(nil, nil, testCtxProvider, testTxConfigProvider, ConnectionTypeHTTP, nil, nil, nil)
count, err := api.GetBlockTransactionCountByHash(context.Background(), GenesisBlockHash)
require.NoError(t, err)
require.NotNil(t, count)
require.Equal(t, hexutil.Uint(0), *count)
}
func TestLogFetcherSkipsUnavailableCachedBlock(t *testing.T) {
t.Parallel()
earliest := int64(1)
latest := int64(90)
highHeight := latest + 3
client := newHeightTestClient(highHeight, earliest, latest)
watermarks := NewWatermarkManager(client, testCtxProvider, nil, nil)
cache := NewBlockCache(2)
cache.Add(highHeight, &BlockCacheEntry{
Block: client.highBlock,
Receipts: make(map[common.Hash]*evmtypes.Receipt),
})
fetcher := &LogFetcher{
tmClient: client,
k: nil,
txConfigProvider: testTxConfigProvider,
ctxProvider: testCtxProvider,
filterConfig: &FilterConfig{maxLog: DefaultMaxLogLimit, maxBlock: DefaultMaxBlockRange},
includeSyntheticReceipts: false,
dbReadSemaphore: make(chan struct{}, 1),
globalBlockCache: cache,
globalLogSlicePool: NewLogSlicePool(),
watermarks: watermarks,
}
resCh := make(chan *coretypes.ResultBlock, 1)
errCh := make(chan error, 1)
fetcher.processBatch(context.Background(), highHeight, highHeight, filters.FilterCriteria{}, nil, resCh, errCh)
select {
case <-resCh:
t.Fatalf("expected no block to be emitted for height %d", highHeight)
default:
}
select {
case err := <-errCh:
t.Fatalf("unexpected error: %v", err)
default:
}
}
func TestStateAPIGetProofUnavailableHeight(t *testing.T) {
t.Parallel()
earliest := int64(2)
latest := int64(80)
highHeight := latest + 4
client := newHeightTestClient(highHeight, earliest, latest)
watermarks := NewWatermarkManager(client, testCtxProvider, nil, nil)
api := NewStateAPI(client, nil, testCtxProvider, ConnectionTypeHTTP, watermarks)
blockParam := rpc.BlockNumberOrHashWithHash(common.HexToHash(highBlockHashHex), true)
_, err := api.GetProof(context.Background(), common.Address{}, []string{}, blockParam)
require.Error(t, err)
require.Contains(t, err.Error(), "requested height")
}