forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashbuilder.go
744 lines (686 loc) · 21.6 KB
/
hashbuilder.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
// Copyright 2024 The Erigon Authors
// This file is part of Erigon.
//
// Erigon 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.
//
// Erigon 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 Erigon. If not, see <http://www.gnu.org/licenses/>.
package trie
import (
"bytes"
"errors"
"fmt"
"io"
"math/bits"
"github.com/holiman/uint256"
"golang.org/x/crypto/sha3"
libcommon "github.com/erigontech/erigon-lib/common"
length2 "github.com/erigontech/erigon-lib/common/length"
"github.com/erigontech/erigon/core/types/accounts"
"github.com/erigontech/erigon/crypto"
"github.com/erigontech/erigon/rlp"
"github.com/erigontech/erigon/turbo/rlphacks"
)
const hashStackStride = length2.Hash + 1 // + 1 byte for RLP encoding
var EmptyCodeHash = crypto.Keccak256Hash(nil) //c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470
// HashBuilder implements the interface `structInfoReceiver` and opcodes that the structural information of the trie
// is comprised of
// DESCRIBED: docs/programmers_guide/guide.md#separation-of-keys-and-the-structure
type HashBuilder struct {
byteArrayWriter *ByteArrayWriter
hashStack []byte // Stack of sub-slices, each 33 bytes each, containing RLP encodings of node hashes (or of nodes themselves, if shorter than 32 bytes)
nodeStack []node // Stack of nodes
acc accounts.Account // Working account instance (to avoid extra allocations)
sha keccakState // Keccak primitive that can absorb data (Write), and get squeezed to the hash out (Read)
hashBuf [hashStackStride]byte // RLP representation of hash (or un-hashes value)
keyPrefix [1]byte
lenPrefix [4]byte
valBuf [128]byte // Enough to accommodate hash encoding of any account
b [1]byte // Buffer for single byte
prefixBuf [8]byte
trace bool // Set to true when HashBuilder is required to print trace information for diagnostics
topHashesCopy []byte
// proofElement is set when the next element computation should have its RLP
// encoding retained. Additionally, the account root storage hash and storage
// values are stored into this field when set and in the relavent codepath.
proofElement *proofElement
}
// NewHashBuilder creates a new HashBuilder
func NewHashBuilder(trace bool) *HashBuilder {
return &HashBuilder{
sha: sha3.NewLegacyKeccak256().(keccakState),
byteArrayWriter: &ByteArrayWriter{},
trace: trace,
}
}
// Reset makes the HashBuilder suitable for reuse
func (hb *HashBuilder) Reset() {
if len(hb.hashStack) > 0 {
hb.hashStack = hb.hashStack[:0]
}
if len(hb.nodeStack) > 0 {
hb.nodeStack = hb.nodeStack[:0]
}
hb.topHashesCopy = hb.topHashesCopy[:0]
hb.proofElement = nil
}
// setProofElement sets the proofElement field in which the relevant methods
// will check and additionally write the proof bytes to during trie computation.
func (hb *HashBuilder) setProofElement(pe *proofElement) {
hb.proofElement = pe
}
func (hb *HashBuilder) leaf(length int, keyHex []byte, val rlphacks.RlpSerializable) error {
if hb.trace {
fmt.Printf("LEAF %d\n", length)
}
if length < 0 {
return fmt.Errorf("length %d", length)
}
if hb.proofElement != nil {
hb.proofElement.storageKey = libcommon.CopyBytes(keyHex[:len(keyHex)-1])
hb.proofElement.storageValue = new(uint256.Int).SetBytes(val.RawBytes())
}
key := keyHex[len(keyHex)-length:]
s := &shortNode{Key: libcommon.CopyBytes(key), Val: valueNode(libcommon.CopyBytes(val.RawBytes()))}
hb.nodeStack = append(hb.nodeStack, s)
if err := hb.leafHashWithKeyVal(key, val); err != nil {
return err
}
copy(s.ref.data[:], hb.hashStack[len(hb.hashStack)-length2.Hash:])
s.ref.len = hb.hashStack[len(hb.hashStack)-length2.Hash-1] - 0x80
if s.ref.len > 32 {
s.ref.len = hb.hashStack[len(hb.hashStack)-length2.Hash-1] - 0xc0 + 1
copy(s.ref.data[:], hb.hashStack[len(hb.hashStack)-length2.Hash-1:])
}
if hb.trace {
fmt.Printf("Stack depth: %d\n", len(hb.nodeStack))
}
return nil
}
// To be called internally
func (hb *HashBuilder) leafHashWithKeyVal(key []byte, val rlphacks.RlpSerializable) error {
// Compute the total length of binary representation
var kp, kl int
// Write key
var compactLen int
var ni int
var compact0 byte
if hasTerm(key) {
compactLen = (len(key)-1)/2 + 1
if len(key)&1 == 0 {
compact0 = 0x30 + key[0] // Odd: (3<<4) + first nibble
ni = 1
} else {
compact0 = 0x20
}
} else {
compactLen = len(key)/2 + 1
if len(key)&1 == 1 {
compact0 = 0x10 + key[0] // Odd: (1<<4) + first nibble
ni = 1
}
}
if compactLen > 1 {
hb.keyPrefix[0] = 0x80 + byte(compactLen)
kp = 1
kl = compactLen
} else {
kl = 1
}
err := hb.completeLeafHash(kp, kl, compactLen, key, compact0, ni, val)
if err != nil {
return err
}
if hb.trace {
fmt.Printf("leafHashWithKeyVal [%x]=>[%x]\nHash [%x]\n", key, val, hb.hashBuf[:])
}
hb.hashStack = append(hb.hashStack, hb.hashBuf[:]...)
if len(hb.hashStack) > hashStackStride*len(hb.nodeStack) {
hb.nodeStack = append(hb.nodeStack, nil)
}
return nil
}
func (hb *HashBuilder) completeLeafHash(kp, kl, compactLen int, key []byte, compact0 byte, ni int, val rlphacks.RlpSerializable) error {
totalLen := kp + kl + val.DoubleRLPLen()
pt := rlphacks.GenerateStructLen(hb.lenPrefix[:], totalLen)
var writer io.Writer
var reader io.Reader
if totalLen+pt < length2.Hash {
// Embedded node
hb.byteArrayWriter.Setup(hb.hashBuf[:], 0)
writer = hb.byteArrayWriter
} else {
hb.sha.Reset()
writer = hb.sha
reader = hb.sha
}
// Collect a copy of the hash input if needed for an eth_getProof
if hb.proofElement != nil {
writer = io.MultiWriter(writer, &hb.proofElement.proof)
}
if _, err := writer.Write(hb.lenPrefix[:pt]); err != nil {
return err
}
if _, err := writer.Write(hb.keyPrefix[:kp]); err != nil {
return err
}
hb.b[0] = compact0
if _, err := writer.Write(hb.b[:]); err != nil {
return err
}
for i := 1; i < compactLen; i++ {
hb.b[0] = key[ni]*16 + key[ni+1]
if _, err := writer.Write(hb.b[:]); err != nil {
return err
}
ni += 2
}
if err := val.ToDoubleRLP(writer, hb.prefixBuf[:]); err != nil {
return err
}
if reader != nil {
hb.hashBuf[0] = 0x80 + length2.Hash
if _, err := reader.Read(hb.hashBuf[1:]); err != nil {
return err
}
}
return nil
}
func (hb *HashBuilder) leafHash(length int, keyHex []byte, val rlphacks.RlpSerializable) error {
if hb.trace {
fmt.Printf("LEAFHASH %d\n", length)
}
if length < 0 {
return fmt.Errorf("length %d", length)
}
key := keyHex[len(keyHex)-length:]
return hb.leafHashWithKeyVal(key, val)
}
func (hb *HashBuilder) accountLeaf(length int, keyHex []byte, balance *uint256.Int, nonce uint64, incarnation uint64, fieldSet uint32, accountCodeSize int) (err error) {
if hb.trace {
fmt.Printf("ACCOUNTLEAF %d (%b)\n", length, fieldSet)
}
fullKey := keyHex[:len(keyHex)-1]
key := keyHex[len(keyHex)-length:]
copy(hb.acc.Root[:], EmptyRoot[:])
copy(hb.acc.CodeHash[:], EmptyCodeHash[:])
hb.acc.Nonce = nonce
hb.acc.Balance.Set(balance)
hb.acc.Initialised = true
hb.acc.Incarnation = incarnation
popped := 0
var root node
if fieldSet&uint32(4) != 0 {
copy(hb.acc.Root[:], hb.hashStack[len(hb.hashStack)-popped*hashStackStride-length2.Hash:len(hb.hashStack)-popped*hashStackStride])
if hb.acc.Root != EmptyRoot {
// Root is on top of the stack
root = hb.nodeStack[len(hb.nodeStack)-popped-1]
if root == nil {
root = hashNode{hash: libcommon.CopyBytes(hb.acc.Root[:])}
}
}
popped++
}
var accountCode codeNode
if fieldSet&uint32(8) != 0 {
copy(hb.acc.CodeHash[:], hb.hashStack[len(hb.hashStack)-popped*hashStackStride-length2.Hash:len(hb.hashStack)-popped*hashStackStride])
var ok bool
if !bytes.Equal(hb.acc.CodeHash[:], EmptyCodeHash[:]) {
stackTop := hb.nodeStack[len(hb.nodeStack)-popped-1]
if stackTop != nil { // if we don't have any stack top it might be okay because we didn't resolve the code yet (stateful resolver)
// but if we have something on top of the stack that isn't `nil`, it has to be a codeNode
accountCode, ok = stackTop.(codeNode)
if !ok {
return fmt.Errorf("unexpected node type on the node stack, wanted codeNode, got %T:%s", stackTop, stackTop)
}
}
}
popped++
}
if hb.proofElement != nil {
// The storageRoot is not stored with the account info, therefore
// we capture it with the account proof element. Note, we also store the
// full key as this root could be for a different account in the negative
// case.
hb.proofElement.storageRootKey = libcommon.CopyBytes(fullKey)
hb.proofElement.storageRoot = hb.acc.Root
}
var accCopy accounts.Account
accCopy.Copy(&hb.acc)
if !bytes.Equal(accCopy.CodeHash[:], EmptyCodeHash[:]) && accountCode != nil {
accountCodeSize = len(accountCode)
}
a := &accountNode{accCopy, root, true, accountCode, accountCodeSize}
s := &shortNode{Key: libcommon.CopyBytes(key), Val: a}
// this invocation will take care of the popping given number of items from both hash stack and node stack,
// pushing resulting hash to the hash stack, and nil to the node stack
if err = hb.accountLeafHashWithKey(key, popped); err != nil {
return err
}
copy(s.ref.data[:], hb.hashStack[len(hb.hashStack)-length2.Hash:])
s.ref.len = 32
// Replace top of the stack
hb.nodeStack[len(hb.nodeStack)-1] = s
if hb.trace {
fmt.Printf("Stack depth: %d\n", len(hb.nodeStack))
}
return nil
}
func (hb *HashBuilder) accountLeafHash(length int, keyHex []byte, balance *uint256.Int, nonce uint64, incarnation uint64, fieldSet uint32) (err error) {
if hb.trace {
fmt.Printf("ACCOUNTLEAFHASH %d (%b)\n", length, fieldSet)
}
key := keyHex[len(keyHex)-length:]
hb.acc.Nonce = nonce
hb.acc.Balance.Set(balance)
hb.acc.Initialised = true
hb.acc.Incarnation = incarnation
popped := 0
if fieldSet&AccountFieldStorageOnly != 0 {
copy(hb.acc.Root[:], hb.hashStack[len(hb.hashStack)-popped*hashStackStride-length2.Hash:len(hb.hashStack)-popped*hashStackStride])
popped++
} else {
copy(hb.acc.Root[:], EmptyRoot[:])
}
if fieldSet&AccountFieldCodeOnly != 0 {
copy(hb.acc.CodeHash[:], hb.hashStack[len(hb.hashStack)-popped*hashStackStride-length2.Hash:len(hb.hashStack)-popped*hashStackStride])
popped++
} else {
copy(hb.acc.CodeHash[:], EmptyCodeHash[:])
}
return hb.accountLeafHashWithKey(key, popped)
}
// To be called internally
func (hb *HashBuilder) accountLeafHashWithKey(key []byte, popped int) error {
// Compute the total length of binary representation
var kp, kl int
// Write key
var compactLen int
var ni int
var compact0 byte
if hasTerm(key) {
compactLen = (len(key)-1)/2 + 1
if len(key)&1 == 0 {
compact0 = 48 + key[0] // Odd (1<<4) + first nibble
ni = 1
} else {
compact0 = 32
}
} else {
compactLen = len(key)/2 + 1
if len(key)&1 == 1 {
compact0 = 16 + key[0] // Odd (1<<4) + first nibble
ni = 1
}
}
if compactLen > 1 {
hb.keyPrefix[0] = byte(128 + compactLen)
kp = 1
kl = compactLen
} else {
kl = 1
}
valLen := hb.acc.EncodingLengthForHashing()
hb.acc.EncodeForHashing(hb.valBuf[:])
val := rlphacks.RlpEncodedBytes(hb.valBuf[:valLen])
err := hb.completeLeafHash(kp, kl, compactLen, key, compact0, ni, val)
if err != nil {
return err
}
if popped > 0 {
hb.hashStack = hb.hashStack[:len(hb.hashStack)-popped*hashStackStride]
hb.nodeStack = hb.nodeStack[:len(hb.nodeStack)-popped]
}
if hb.trace {
fmt.Printf("accountLeafHashWithKey [%x]=>[%x]\nHash [%x]\n", key, val, hb.hashBuf[:])
}
hb.hashStack = append(hb.hashStack, hb.hashBuf[:]...)
hb.nodeStack = append(hb.nodeStack, nil)
if hb.trace {
fmt.Printf("Stack depth: %d\n", len(hb.nodeStack))
}
return nil
}
func (hb *HashBuilder) extension(key []byte) error {
if hb.trace {
fmt.Printf("EXTENSION %x\n", key)
}
nd := hb.nodeStack[len(hb.nodeStack)-1]
var s *shortNode
switch n := nd.(type) {
case nil:
branchHash := libcommon.CopyBytes(hb.hashStack[len(hb.hashStack)-length2.Hash:])
s = &shortNode{Key: libcommon.CopyBytes(key), Val: hashNode{hash: branchHash}}
case *fullNode:
s = &shortNode{Key: libcommon.CopyBytes(key), Val: n}
default:
return fmt.Errorf("wrong Val type for an extension: %T", nd)
}
hb.nodeStack[len(hb.nodeStack)-1] = s
if err := hb.extensionHash(key); err != nil {
return err
}
copy(s.ref.data[:], hb.hashStack[len(hb.hashStack)-length2.Hash:])
s.ref.len = 32
if hb.trace {
fmt.Printf("Stack depth: %d\n", len(hb.nodeStack))
}
return nil
}
func (hb *HashBuilder) extensionHash(key []byte) error {
writer := io.Writer(hb.sha)
if hb.proofElement != nil {
writer = io.MultiWriter(hb.sha, &hb.proofElement.proof)
}
if hb.trace {
fmt.Printf("EXTENSIONHASH %x\n", key)
}
branchHash := hb.hashStack[len(hb.hashStack)-hashStackStride:]
// Compute the total length of binary representation
var kp, kl int
// Write key
var compactLen int
var ni int
var compact0 byte
// https://github.com/ethereum/wiki/wiki/Patricia-Tree#specification-compact-encoding-of-hex-sequence-with-optional-terminator
if hasTerm(key) {
compactLen = (len(key)-1)/2 + 1
if len(key)&1 == 0 {
compact0 = 0x30 + key[0] // Odd: (3<<4) + first nibble
ni = 1
} else {
compact0 = 0x20
}
} else {
compactLen = len(key)/2 + 1
if len(key)&1 == 1 {
compact0 = 0x10 + key[0] // Odd: (1<<4) + first nibble
ni = 1
}
}
if compactLen > 1 {
hb.keyPrefix[0] = 0x80 + byte(compactLen)
kp = 1
kl = compactLen
} else {
kl = 1
}
totalLen := kp + kl + 33
pt := rlphacks.GenerateStructLen(hb.lenPrefix[:], totalLen)
hb.sha.Reset()
if _, err := writer.Write(hb.lenPrefix[:pt]); err != nil {
return err
}
if _, err := writer.Write(hb.keyPrefix[:kp]); err != nil {
return err
}
hb.b[0] = compact0
if _, err := writer.Write(hb.b[:]); err != nil {
return err
}
for i := 1; i < compactLen; i++ {
hb.b[0] = key[ni]*16 + key[ni+1]
if _, err := writer.Write(hb.b[:]); err != nil {
return err
}
ni += 2
}
var capture []byte //nolint: used for tracing
if hb.trace {
capture = libcommon.CopyBytes(branchHash[:length2.Hash+1])
}
if _, err := writer.Write(branchHash[:length2.Hash+1]); err != nil {
return err
}
// Replace previous hash with the new one
if _, err := hb.sha.Read(hb.hashStack[len(hb.hashStack)-length2.Hash:]); err != nil {
return err
}
hb.hashStack[len(hb.hashStack)-hashStackStride] = 0x80 + length2.Hash
if hb.trace {
fmt.Printf("extensionHash [%x]=>[%x]\nHash [%x]\n", key, capture, hb.hashStack[len(hb.hashStack)-hashStackStride:len(hb.hashStack)])
}
if _, ok := hb.nodeStack[len(hb.nodeStack)-1].(*fullNode); ok {
return errors.New("extensionHash cannot be emitted when a node is on top of the stack")
}
return nil
}
func (hb *HashBuilder) branch(set uint16) error {
if hb.trace {
fmt.Printf("BRANCH (%b)\n", set)
}
if hb.trace {
fmt.Printf("Stack depth: %d\n", len(hb.nodeStack))
}
f := &fullNode{}
digits := bits.OnesCount16(set)
if len(hb.nodeStack) < digits {
return fmt.Errorf("len(hb.nodeStask) %d < digits %d", len(hb.nodeStack), digits)
}
nodes := hb.nodeStack[len(hb.nodeStack)-digits:]
hashes := hb.hashStack[len(hb.hashStack)-hashStackStride*digits:]
var i int
for digit := uint(0); digit < 16; digit++ {
if ((1 << digit) & set) != 0 {
if nodes[i] == nil {
f.Children[digit] = hashNode{hash: libcommon.CopyBytes(hashes[hashStackStride*i+1 : hashStackStride*(i+1)])}
} else {
f.Children[digit] = nodes[i]
}
i++
}
}
hb.nodeStack = hb.nodeStack[:len(hb.nodeStack)-digits+1]
hb.nodeStack[len(hb.nodeStack)-1] = f
if err := hb.branchHash(set); err != nil {
return err
}
copy(f.ref.data[:], hb.hashStack[len(hb.hashStack)-length2.Hash:])
f.ref.len = 32
if hb.trace {
fmt.Printf("Stack depth: %d\n", len(hb.nodeStack))
}
return nil
}
func (hb *HashBuilder) branchHash(set uint16) error {
writer := io.Writer(hb.sha)
if hb.proofElement != nil {
writer = io.MultiWriter(hb.sha, &hb.proofElement.proof)
}
if hb.trace {
fmt.Printf("BRANCHHASH (%b)\n", set)
}
digits := bits.OnesCount16(set)
if len(hb.hashStack) < hashStackStride*digits {
return fmt.Errorf("len(hb.hashStack) %d < hashStackStride*digits %d", len(hb.hashStack), hashStackStride*digits)
}
hashes := hb.hashStack[len(hb.hashStack)-hashStackStride*digits:]
// Calculate the size of the resulting RLP
totalSize := 17 // These are 17 length prefixes
var i int
for digit := uint(0); digit < 16; digit++ {
if ((1 << digit) & set) != 0 {
if hashes[hashStackStride*i] == 0x80+length2.Hash {
totalSize += length2.Hash
} else {
// Embedded node
totalSize += int(hashes[hashStackStride*i] - rlp.EmptyListCode)
}
i++
}
}
hb.sha.Reset()
pt := rlphacks.GenerateStructLen(hb.lenPrefix[:], totalSize)
if _, err := writer.Write(hb.lenPrefix[:pt]); err != nil {
return err
}
// Output hasState hashes or embedded RLPs
i = 0
if hb.trace {
fmt.Printf("branchHash {\n")
}
hb.b[0] = rlp.EmptyStringCode
for digit := uint(0); digit < 17; digit++ {
if ((1 << digit) & set) != 0 {
if hashes[hashStackStride*i] == byte(0x80+length2.Hash) {
if _, err := writer.Write(hashes[hashStackStride*i : hashStackStride*i+hashStackStride]); err != nil {
return err
}
if hb.trace {
fmt.Printf("%x: [%x]\n", digit, hashes[hashStackStride*i:hashStackStride*i+hashStackStride])
}
} else {
// Embedded node
size := int(hashes[hashStackStride*i]) - rlp.EmptyListCode
if _, err := writer.Write(hashes[hashStackStride*i : hashStackStride*i+size+1]); err != nil {
return err
}
if hb.trace {
fmt.Printf("%x: embedded [%x]\n", digit, hashes[hashStackStride*i:hashStackStride*i+size+1])
}
}
i++
} else {
if _, err := writer.Write(hb.b[:]); err != nil {
return err
}
if hb.trace {
fmt.Printf("%x: empty\n", digit)
}
}
}
hb.hashStack = hb.hashStack[:len(hb.hashStack)-hashStackStride*digits+hashStackStride]
hb.hashStack[len(hb.hashStack)-hashStackStride] = 0x80 + length2.Hash
if _, err := hb.sha.Read(hb.hashStack[len(hb.hashStack)-length2.Hash:]); err != nil {
return err
}
if hb.trace {
fmt.Printf("} [%x]\n", hb.hashStack[len(hb.hashStack)-hashStackStride:])
}
if hashStackStride*len(hb.nodeStack) > len(hb.hashStack) {
hb.nodeStack = hb.nodeStack[:len(hb.nodeStack)-digits+1]
hb.nodeStack[len(hb.nodeStack)-1] = nil
if hb.trace {
fmt.Printf("Setting hb.nodeStack[%d] to nil\n", len(hb.nodeStack)-1)
}
}
if hb.trace {
fmt.Printf("Stack depth: %d\n", len(hb.nodeStack))
}
return nil
}
func (hb *HashBuilder) hash(hash []byte) error {
if hb.trace {
fmt.Printf("HASH\n")
}
hb.hashStack = append(hb.hashStack, 0x80+length2.Hash)
hb.hashStack = append(hb.hashStack, hash...)
hb.nodeStack = append(hb.nodeStack, nil)
if hb.trace {
fmt.Printf("Stack depth: %d\n", len(hb.nodeStack))
}
return nil
}
func (hb *HashBuilder) code(code []byte) error {
if hb.trace {
fmt.Printf("CODE\n")
}
codeCopy := libcommon.CopyBytes(code)
n := codeNode(codeCopy)
hb.nodeStack = append(hb.nodeStack, n)
hb.sha.Reset()
if _, err := hb.sha.Write(codeCopy); err != nil {
return err
}
var hash [hashStackStride]byte // RLP representation of hash (or un-hashes value)
hash[0] = 0x80 + length2.Hash
if _, err := hb.sha.Read(hash[1:]); err != nil {
return err
}
hb.hashStack = append(hb.hashStack, hash[:]...)
return nil
}
func (hb *HashBuilder) emptyRoot() {
if hb.trace {
fmt.Printf("EMPTYROOT\n")
}
hb.nodeStack = append(hb.nodeStack, nil)
var hash [hashStackStride]byte // RLP representation of hash (or un-hashes value)
hash[0] = 0x80 + length2.Hash
copy(hash[1:], EmptyRoot[:])
hb.hashStack = append(hb.hashStack, hash[:]...)
}
func (hb *HashBuilder) RootHash() (libcommon.Hash, error) {
if !hb.hasRoot() {
return libcommon.Hash{}, errors.New("no root in the tree")
}
return hb.rootHash(), nil
}
func (hb *HashBuilder) rootHash() libcommon.Hash {
var hash libcommon.Hash
top := hb.topHash()
if len(top) == 33 {
copy(hash[:], top[1:])
} else {
hb.sha.Reset()
if _, err := hb.sha.Write(top); err != nil {
panic(err)
}
if _, err := hb.sha.Read(hash[:]); err != nil {
panic(err)
}
}
return hash
}
func (hb *HashBuilder) topHash() []byte {
pos := len(hb.hashStack) - hashStackStride
len := hb.hashStack[pos] - 0x80
if len > 32 {
// node itself (RLP list), not its hash
len = hb.hashStack[pos] - 0xc0
}
return hb.hashStack[pos : pos+1+int(len)]
}
func (hb *HashBuilder) printTopHashes(prefix []byte, _, children uint16) {
digits := bits.OnesCount16(children)
hashes := hb.hashStack[len(hb.hashStack)-hashStackStride*digits:]
var i int
for digit := uint(0); digit < 16; digit++ {
if ((1 << digit) & children) != 0 {
fmt.Printf("topHash: %x%02x, %x\n", prefix, digit, hashes[hashStackStride*i+1:hashStackStride*(i+1)])
i++
}
}
}
func (hb *HashBuilder) topHashes(prefix []byte, hasHash, hasState uint16) []byte {
digits := bits.OnesCount16(hasState)
hashes := hb.hashStack[len(hb.hashStack)-hashStackStride*digits:]
hb.topHashesCopy = hb.topHashesCopy[:0]
for i := 0; hasHash > 0; hasState, hasHash = hasState>>1, hasHash>>1 {
if 1&hasState == 0 {
continue
}
if 1&hasHash != 0 {
hb.topHashesCopy = append(hb.topHashesCopy, hashes[hashStackStride*i+1:hashStackStride*(i+1)]...)
}
i++
}
return hb.topHashesCopy
}
func (hb *HashBuilder) root() node {
if hb.trace && len(hb.nodeStack) > 0 {
fmt.Printf("len(hb.nodeStack)=%d\n", len(hb.nodeStack))
}
return hb.nodeStack[len(hb.nodeStack)-1]
}
func (hb *HashBuilder) hasRoot() bool {
return len(hb.nodeStack) > 0
}