-
Notifications
You must be signed in to change notification settings - Fork 4
/
marshal.go
347 lines (323 loc) · 8.23 KB
/
marshal.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package header
import (
"encoding/binary"
"fmt"
"io"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/multiformats/go-multihash"
dageth "github.com/vulcanize/go-codec-dageth"
"github.com/vulcanize/go-codec-dageth/shared"
)
// Encode provides an IPLD codec encode interface for eth header IPLDs.
// This function is registered via the go-ipld-prime link loader for multicodec
// code 0x90 when this package is invoked via init.
func Encode(node ipld.Node, w io.Writer) error {
// 1KiB can be allocated on the stack, and covers most small nodes
// without having to grow the buffer and cause allocations.
enc := make([]byte, 0, 1024)
enc, err := AppendEncode(enc, node)
if err != nil {
return err
}
_, err = w.Write(enc)
return err
}
// AppendEncode is like Encode, but it uses a destination buffer directly.
// This means less copying of bytes, and if the destination has enough capacity,
// fewer allocations.
func AppendEncode(enc []byte, inNode ipld.Node) ([]byte, error) {
header := new(types.Header)
if err := EncodeHeader(header, inNode); err != nil {
return enc, err
}
wbs := shared.NewWriteableByteSlice(&enc)
if err := rlp.Encode(wbs, header); err != nil {
return enc, fmt.Errorf("invalid DAG-ETH Header form (unable to RLP encode header: %v)", err)
}
return enc, nil
}
// EncodeHeader packs the node into the provided go-ethereum Header
func EncodeHeader(header *types.Header, inNode ipld.Node) error {
// Wrap in a typed node for some basic schema form checking
builder := dageth.Type.Header.NewBuilder()
if err := builder.AssignNode(inNode); err != nil {
return err
}
node := builder.Build()
for _, pFunc := range requiredPackFuncs {
if err := pFunc(header, node); err != nil {
return fmt.Errorf("invalid DAG-ETH Header form (%v)", err)
}
}
return nil
}
var requiredPackFuncs = []func(*types.Header, ipld.Node) error{
packParentCID,
packUnclesCID,
packCoinbase,
packStateRootCID,
packTxRootCID,
packRctRootCID,
packBloom,
packDifficulty,
packNumber,
packGasLimit,
packGasUsed,
packTime,
packExtra,
packMixDigest,
packNonce,
packBaseFee,
}
func packNonce(header *types.Header, node ipld.Node) error {
n, err := node.LookupByString("Nonce")
if err != nil {
return err
}
nBytes, err := n.AsBytes()
if err != nil {
return err
}
if len(nBytes) != len(types.BlockNonce{}) {
return fmt.Errorf("header must have a %d byte Nonce", len(types.BlockNonce{}))
}
copy(header.Nonce[:], nBytes)
return nil
}
func packMixDigest(header *types.Header, node ipld.Node) error {
md, err := node.LookupByString("MixDigest")
if err != nil {
return err
}
mdBytes, err := md.AsBytes()
if err != nil {
return err
}
header.MixDigest = common.BytesToHash(mdBytes)
return nil
}
func packExtra(header *types.Header, node ipld.Node) error {
e, err := node.LookupByString("Extra")
if err != nil {
return err
}
extra, err := e.AsBytes()
if err != nil {
return err
}
header.Extra = extra
return nil
}
func packTime(header *types.Header, node ipld.Node) error {
t, err := node.LookupByString("Time")
if err != nil {
return err
}
tBytes, err := t.AsBytes()
if err != nil {
return err
}
header.Time = binary.BigEndian.Uint64(tBytes)
return nil
}
func packGasUsed(header *types.Header, node ipld.Node) error {
gu, err := node.LookupByString("GasUsed")
if err != nil {
return err
}
guBytes, err := gu.AsBytes()
if err != nil {
return err
}
header.GasUsed = binary.BigEndian.Uint64(guBytes)
return nil
}
func packGasLimit(header *types.Header, node ipld.Node) error {
gl, err := node.LookupByString("GasLimit")
if err != nil {
return err
}
glBytes, err := gl.AsBytes()
if err != nil {
return err
}
header.GasLimit = binary.BigEndian.Uint64(glBytes)
return nil
}
func packNumber(header *types.Header, node ipld.Node) error {
num, err := node.LookupByString("Number")
if err != nil {
return err
}
numBytes, err := num.AsBytes()
if err != nil {
return err
}
header.Number = new(big.Int).SetBytes(numBytes)
return nil
}
func packDifficulty(header *types.Header, node ipld.Node) error {
diff, err := node.LookupByString("Difficulty")
if err != nil {
return err
}
diffBytes, err := diff.AsBytes()
if err != nil {
return err
}
header.Difficulty = new(big.Int).SetBytes(diffBytes)
return nil
}
func packBloom(header *types.Header, node ipld.Node) error {
blm, err := node.LookupByString("Bloom")
if err != nil {
return err
}
blmBytes, err := blm.AsBytes()
if err != nil {
return err
}
// prevent any chance of BytesToBloom panicing on wrong bytes length
if len(blmBytes) != types.BloomByteLength {
return fmt.Errorf("header must have a 256 byte Bloom")
}
header.Bloom = types.BytesToBloom(blmBytes)
return nil
}
func packRctRootCID(header *types.Header, node ipld.Node) error {
rctCID, err := node.LookupByString("RctRootCID")
if err != nil {
return err
}
rctLink, err := rctCID.AsLink()
if err != nil {
return err
}
rctCIDLink, ok := rctLink.(cidlink.Link)
if !ok {
return fmt.Errorf("header must have a RctRootCID")
}
rctMh := rctCIDLink.Hash()
decodedRctMh, err := multihash.Decode(rctMh)
if err != nil {
return fmt.Errorf("unable to decode RctRootCID multihash: %v", err)
}
header.ReceiptHash = common.BytesToHash(decodedRctMh.Digest)
return nil
}
func packTxRootCID(header *types.Header, node ipld.Node) error {
txCID, err := node.LookupByString("TxRootCID")
if err != nil {
return err
}
txLink, err := txCID.AsLink()
if err != nil {
return err
}
txCIDLink, ok := txLink.(cidlink.Link)
if !ok {
return fmt.Errorf("header must have a TxRootCID")
}
txMh := txCIDLink.Hash()
decodedTxMh, err := multihash.Decode(txMh)
if err != nil {
return fmt.Errorf("unable to decode TxRootCID multihash: %v", err)
}
header.TxHash = common.BytesToHash(decodedTxMh.Digest)
return nil
}
func packStateRootCID(header *types.Header, node ipld.Node) error {
srCID, err := node.LookupByString("StateRootCID")
if err != nil {
return err
}
srLink, err := srCID.AsLink()
if err != nil {
return err
}
srCIDLink, ok := srLink.(cidlink.Link)
if !ok {
return fmt.Errorf("header must have a StateRootCID")
}
srMh := srCIDLink.Hash()
decodedSrMh, err := multihash.Decode(srMh)
if err != nil {
return fmt.Errorf("unable to decode StateRootCID multihash: %v", err)
}
header.Root = common.BytesToHash(decodedSrMh.Digest)
return nil
}
func packCoinbase(header *types.Header, node ipld.Node) error {
coinbase, err := node.LookupByString("Coinbase")
if err != nil {
return err
}
coinbaseBytes, err := coinbase.AsBytes()
if err != nil {
return err
}
header.Coinbase = common.BytesToAddress(coinbaseBytes)
return nil
}
func packUnclesCID(header *types.Header, node ipld.Node) error {
uncleCID, err := node.LookupByString("UnclesCID")
if err != nil {
return err
}
unclesLink, err := uncleCID.AsLink()
if err != nil {
return err
}
unclesCIDLink, ok := unclesLink.(cidlink.Link)
if !ok {
return fmt.Errorf("header must have an UnclesCID")
}
unclesMh := unclesCIDLink.Hash()
decodedUnclesMh, err := multihash.Decode(unclesMh)
if err != nil {
return fmt.Errorf("unable to decode UnclesCID multihash: %v", err)
}
header.UncleHash = common.BytesToHash(decodedUnclesMh.Digest)
return nil
}
func packParentCID(header *types.Header, node ipld.Node) error {
parentCID, err := node.LookupByString("ParentCID")
if err != nil {
return err
}
parentLink, err := parentCID.AsLink()
if err != nil {
return err
}
parentCIDLink, ok := parentLink.(cidlink.Link)
if !ok {
return fmt.Errorf("header must have a ParentCID")
}
parentMh := parentCIDLink.Hash()
decodedParentMh, err := multihash.Decode(parentMh)
if err != nil {
return fmt.Errorf("unable to decode ParentCID multihash: %v", err)
}
header.ParentHash = common.BytesToHash(decodedParentMh.Digest)
return nil
}
func packBaseFee(header *types.Header, node ipld.Node) error {
baseFeeNode, err := node.LookupByString("BaseFee")
if err != nil {
return err
}
if baseFeeNode.IsNull() {
return nil
}
baseFeeBytes, err := baseFeeNode.AsBytes()
if err != nil {
return err
}
header.BaseFee = new(big.Int).SetBytes(baseFeeBytes)
return nil
}