@@ -18,12 +18,15 @@ package era
18
18
19
19
import (
20
20
"bytes"
21
+ "fmt"
21
22
"io"
22
23
"math/big"
23
24
"os"
24
25
"testing"
25
26
26
27
"github.com/ethereum/go-ethereum/common"
28
+ "github.com/ethereum/go-ethereum/core/types"
29
+ "github.com/ethereum/go-ethereum/rlp"
27
30
)
28
31
29
32
type testchain struct {
@@ -48,9 +51,9 @@ func TestEra1Builder(t *testing.T) {
48
51
chain = testchain {}
49
52
)
50
53
for i := 0 ; i < 128 ; i ++ {
51
- chain .headers = append (chain .headers , [] byte { byte ( 'h' ), byte ( i )} )
52
- chain .bodies = append (chain .bodies , [] byte { byte ( 'b' ), byte (i )})
53
- chain .receipts = append (chain .receipts , [] byte { byte ( 'r' ), byte (i )})
54
+ chain .headers = append (chain .headers , mustEncode ( & types. Header { Number : big . NewInt ( int64 ( i ))}) )
55
+ chain .bodies = append (chain .bodies , mustEncode ( & types. Body { Transactions : [] * types. Transaction { types . NewTransaction ( 0 , common. Address { byte (i )}, nil , 0 , nil , nil )}}) )
56
+ chain .receipts = append (chain .receipts , mustEncode ( & types. Receipts {{ CumulativeGasUsed : uint64 (i )}}) )
54
57
chain .tds = append (chain .tds , big .NewInt (int64 (i )))
55
58
}
56
59
@@ -91,13 +94,25 @@ func TestEra1Builder(t *testing.T) {
91
94
t .Fatalf ("unexpected error %v" , it .Error ())
92
95
}
93
96
// Check headers.
94
- header , err := io .ReadAll (it .Header )
97
+ rawHeader , err := io .ReadAll (it .Header )
98
+ if err != nil {
99
+ t .Fatalf ("error reading header from iterator: %v" , err )
100
+ }
101
+ if ! bytes .Equal (rawHeader , chain .headers [i ]) {
102
+ t .Fatalf ("mismatched header: want %s, got %s" , chain .headers [i ], rawHeader )
103
+ }
104
+ header , err := e .GetHeaderByNumber (i )
95
105
if err != nil {
96
106
t .Fatalf ("error reading header: %v" , err )
97
107
}
98
- if ! bytes .Equal (header , chain .headers [i ]) {
99
- t .Fatalf ("mismatched header: want %s, got %s" , chain .headers [i ], header )
108
+ encHeader , err := rlp .EncodeToBytes (header )
109
+ if err != nil {
110
+ t .Fatalf ("error encoding header: %v" , err )
111
+ }
112
+ if ! bytes .Equal (encHeader , chain .headers [i ]) {
113
+ t .Fatalf ("mismatched header: want %s, got %s" , chain .headers [i ], encHeader )
100
114
}
115
+
101
116
// Check bodies.
102
117
body , err := io .ReadAll (it .Body )
103
118
if err != nil {
@@ -106,13 +121,25 @@ func TestEra1Builder(t *testing.T) {
106
121
if ! bytes .Equal (body , chain .bodies [i ]) {
107
122
t .Fatalf ("mismatched body: want %s, got %s" , chain .bodies [i ], body )
108
123
}
124
+
109
125
// Check receipts.
110
- receipts , err := io .ReadAll (it .Receipts )
126
+ rawReceipts , err := io .ReadAll (it .Receipts )
127
+ if err != nil {
128
+ t .Fatalf ("error reading receipts from iterator: %v" , err )
129
+ }
130
+ if ! bytes .Equal (rawReceipts , chain .receipts [i ]) {
131
+ t .Fatalf ("mismatched receipts: want %s, got %s" , chain .receipts [i ], rawReceipts )
132
+ }
133
+ receipts , err := e .GetReceiptsByNumber (i )
111
134
if err != nil {
112
135
t .Fatalf ("error reading receipts: %v" , err )
113
136
}
114
- if ! bytes .Equal (receipts , chain .receipts [i ]) {
115
- t .Fatalf ("mismatched receipts: want %s, got %s" , chain .receipts [i ], receipts )
137
+ encReceipts , err := rlp .EncodeToBytes (receipts )
138
+ if err != nil {
139
+ t .Fatalf ("error encoding receipts: %v" , err )
140
+ }
141
+ if ! bytes .Equal (encReceipts , chain .receipts [i ]) {
142
+ t .Fatalf ("mismatched receipts: want %s, got %s" , chain .receipts [i ], encReceipts )
116
143
}
117
144
118
145
// Check total difficulty.
@@ -144,3 +171,11 @@ func TestEraFilename(t *testing.T) {
144
171
}
145
172
}
146
173
}
174
+
175
+ func mustEncode (obj any ) []byte {
176
+ b , err := rlp .EncodeToBytes (obj )
177
+ if err != nil {
178
+ panic (fmt .Sprintf ("failed in encode obj: %v" , err ))
179
+ }
180
+ return b
181
+ }
0 commit comments