erigon-pulse/rlp/commitment.go
ledgerwatch ae6f7202d2
[erigon2] Introduce commitment files and table (#201)
* Introduce commitment files and table

* Introduce commitment files and table

* Introduce words buffer

* Move changes out of aggregator into the writer

* Start calc commitment

* Add commitment package

* More on hex patricia

* More

* account decorator

* More

* Add test

* More

* More to the test

* Use hex in tests

* More

* More

* More

* More

* Simplified cells

* Add delBitmap

* More advanced MockState

* More compact unfolding

* Separation of hashed Keys

* Sepatate downHashedKey and upHashedKey

* Carry extension node through accounts

* optimised row allocations

* Fix encoding/decoding, add trace flag

* Added account and storage into the cells

* Make accountKeyLen global setting

* Remove BranchNodeUpdaqte intermediate

* Started on computeHash

* Initial hash calculations

* Fix lint

* Change account encoding

* Fix commitment issues

* Fix lint

* Fix lint

* Fix lint

* Trace

* Small fix and tracing

* Print branch hashes, fix empty

* Fix

* Fix

* Fix

* Fix

* Print accountFn

* Trigger accountFn

* Trigger accountFn

* Another fix for extension nodes

* Return root hash and set trace

* Print depth for computeCellHash

* Fix for storage leaves

* Fix for storage leaves

* Fix for storage leaves

* Fix for storage leaves

* Fix for storage leaves

* Fix for storage leaves

* Ineffectual deletes

* Trace needUnfolding

* Fix

* Fix

* Fix

* trace deletes

* trace deletes

* trace deletes

* trace deletes

* trace deletes

* not overwrite downHashedKey in updateAccount

* simplify needUnfolding

* simplify needUnfolding

* simplify needUnfolding

* simplify needUnfolding

* simplify needUnfolding

* propagate deletes when unfolding

* remove deletes when folding

* remove deletes when folding

* remove upHashedKey when delete cell

* remove upHashedKey when delete cell

* aggregate storage changes later

* remove del bit when updating account

* remove del bit when updating account

* remove del bit when updating account

* perform delete after code update

* perform delete after code update

* perform delete after code update

* perform delete after code update

* perform delete after code update

* perform delete after code update

* latest update has precedence when aggregating

* Revert "latest update has precedence when aggregating"

This reverts commit 0234ea6c3e1a0b2e7599df7c470d4b8b8d56736a.

* latest update has precedence when aggregating

* Revert "latest update has precedence when aggregating"

This reverts commit cfa7b75327e4d5d1236f905c3c04eb640b21838d.

* Introduce FinishTx

* Separate commitment separation from computation

* Prevent spurious delete

* Spurious deletes

* Trace param

* Preserve upHashedKey when unfolding

* Fix

* Fix

* Correctly compute firstInsert

* Try to fix delete + fold

* Remove commented out

* updateStorage to remove deleted flag

* rename upHashedKey to extension, fix clear-up

* Carry extension up if account plain key is present

* Carry extension up if account plain key is present

* Not to fail for deleteAccount

* Not to fail for deleteAccount

* Not to fail for deleteAccount

* Print

* Print

* Print

* Print

* Print

* Print

* Print

* Print

* Copy keys before putting them into commTree

* Preserve changes to AccountData

Co-authored-by: Alex Sharp <alexsharp@Alexs-MacBook-Pro.local>
Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local>
2022-01-12 11:21:52 +00:00

285 lines
6.6 KiB
Go

/*
Copyright 2022 Erigon contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package rlp
import "io"
// RLP-related utilities necessary for computing commitments for state root hash
// generateRlpPrefixLenDouble calculates the length of RLP prefix to encode a string of bytes of length l "twice",
// meaning that it is the prefix for rlp(rlp(data))
func generateRlpPrefixLenDouble(l int, firstByte byte) int {
if l < 2 {
// firstByte only matters when there is 1 byte to encode
if firstByte >= 0x80 {
return 2
}
return 0
}
if l < 55 {
return 2
}
if l < 56 { // 2 + 1
return 3
}
if l < 254 {
return 4
}
if l < 256 {
return 5
}
if l < 65533 {
return 6
}
if l < 65536 {
return 7
}
return 8
}
func multiByteHeaderPrefixOfLen(l int) byte {
// > If a string is more than 55 bytes long, the
// > RLP encoding consists of a single byte with value 0xB7 plus the length
// > of the length of the string in binary form, followed by the length of
// > the string, followed by the string. For example, a length-1024 string
// > would be encoded as 0xB90400 followed by the string. The range of
// > the first byte is thus [0xB8, 0xBF].
//
// see package rlp/decode.go:887
return byte(0xB7 + l)
}
func generateByteArrayLen(buffer []byte, pos int, l int) int {
if l < 56 {
buffer[pos] = byte(0x80 + l)
pos++
} else if l < 256 {
// len(vn) can be encoded as 1 byte
buffer[pos] = multiByteHeaderPrefixOfLen(1)
pos++
buffer[pos] = byte(l)
pos++
} else if l < 65536 {
// len(vn) is encoded as two bytes
buffer[pos] = multiByteHeaderPrefixOfLen(2)
pos++
buffer[pos] = byte(l >> 8)
pos++
buffer[pos] = byte(l & 255)
pos++
} else {
// len(vn) is encoded as three bytes
buffer[pos] = multiByteHeaderPrefixOfLen(3)
pos++
buffer[pos] = byte(l >> 16)
pos++
buffer[pos] = byte((l >> 8) & 255)
pos++
buffer[pos] = byte(l & 255)
pos++
}
return pos
}
func generateByteArrayLenDouble(buffer []byte, pos int, l int) int {
if l < 55 {
// After first wrapping, the length will be l + 1 < 56
buffer[pos] = byte(0x80 + l + 1)
pos++
buffer[pos] = byte(0x80 + l)
pos++
} else if l < 56 {
buffer[pos] = multiByteHeaderPrefixOfLen(1)
pos++
buffer[pos] = byte(l + 1)
pos++
buffer[pos] = byte(0x80 + l)
pos++
} else if l < 254 {
// After first wrapping, the length will be l + 2 < 256
buffer[pos] = multiByteHeaderPrefixOfLen(1)
pos++
buffer[pos] = byte(l + 2)
pos++
buffer[pos] = multiByteHeaderPrefixOfLen(1)
pos++
buffer[pos] = byte(l)
pos++
} else if l < 256 {
// First wrapping is 2 bytes, second wrapping 3 bytes
buffer[pos] = multiByteHeaderPrefixOfLen(2)
pos++
buffer[pos] = byte((l + 2) >> 8)
pos++
buffer[pos] = byte((l + 2) & 255)
pos++
buffer[pos] = multiByteHeaderPrefixOfLen(1)
pos++
buffer[pos] = byte(l)
pos++
} else if l < 65533 {
// Both wrappings are 3 bytes
buffer[pos] = multiByteHeaderPrefixOfLen(2)
pos++
buffer[pos] = byte((l + 3) >> 8)
pos++
buffer[pos] = byte((l + 3) & 255)
pos++
buffer[pos] = multiByteHeaderPrefixOfLen(2)
pos++
buffer[pos] = byte(l >> 8)
pos++
buffer[pos] = byte(l & 255)
pos++
} else if l < 65536 {
// First wrapping is 3 bytes, second wrapping is 4 bytes
buffer[pos] = multiByteHeaderPrefixOfLen(3)
pos++
buffer[pos] = byte((l + 3) >> 16)
pos++
buffer[pos] = byte(((l + 3) >> 8) & 255)
pos++
buffer[pos] = byte((l + 3) & 255)
pos++
buffer[pos] = multiByteHeaderPrefixOfLen(2)
pos++
buffer[pos] = byte((l >> 8) & 255)
pos++
buffer[pos] = byte(l & 255)
pos++
} else {
// Both wrappings are 4 bytes
buffer[pos] = multiByteHeaderPrefixOfLen(3)
pos++
buffer[pos] = byte((l + 4) >> 16)
pos++
buffer[pos] = byte(((l + 4) >> 8) & 255)
pos++
buffer[pos] = byte((l + 4) & 255)
pos++
buffer[pos] = multiByteHeaderPrefixOfLen(3)
pos++
buffer[pos] = byte(l >> 16)
pos++
buffer[pos] = byte((l >> 8) & 255)
pos++
buffer[pos] = byte(l & 255)
pos++
}
return pos
}
func generateRlpPrefixLen(l int) int {
if l < 2 {
return 0
}
if l < 56 {
return 1
}
if l < 256 {
return 2
}
if l < 65536 {
return 3
}
return 4
}
// RlpSerializable is a value that can be double-RLP coded.
type RlpSerializable interface {
ToDoubleRLP(io.Writer, []byte) error
DoubleRLPLen() int
RawBytes() []byte
}
type RlpSerializableBytes []byte
func (b RlpSerializableBytes) ToDoubleRLP(w io.Writer, prefixBuf []byte) error {
return encodeBytesAsRlpToWriter(b, w, generateByteArrayLenDouble, prefixBuf)
}
func (b RlpSerializableBytes) RawBytes() []byte {
return b
}
func (b RlpSerializableBytes) DoubleRLPLen() int {
if len(b) < 1 {
return 0
}
return generateRlpPrefixLenDouble(len(b), b[0]) + len(b)
}
type RlpEncodedBytes []byte
func (b RlpEncodedBytes) ToDoubleRLP(w io.Writer, prefixBuf []byte) error {
return encodeBytesAsRlpToWriter(b, w, generateByteArrayLen, prefixBuf)
}
func (b RlpEncodedBytes) RawBytes() []byte {
return b
}
func (b RlpEncodedBytes) DoubleRLPLen() int {
return generateRlpPrefixLen(len(b)) + len(b)
}
func encodeBytesAsRlpToWriter(source []byte, w io.Writer, prefixGenFunc func([]byte, int, int) int, prefixBuf []byte) error {
// > 1 byte, write a prefix or prefixes first
if len(source) > 1 || (len(source) == 1 && source[0] >= 0x80) {
prefixLen := prefixGenFunc(prefixBuf, 0, len(source))
if _, err := w.Write(prefixBuf[:prefixLen]); err != nil {
return err
}
}
_, err := w.Write(source)
return err
}
func EncodeByteArrayAsRlp(raw []byte, w io.Writer, prefixBuf []byte) (int, error) {
err := encodeBytesAsRlpToWriter(raw, w, generateByteArrayLen, prefixBuf)
if err != nil {
return 0, err
}
return generateRlpPrefixLen(len(raw)) + len(raw), nil
}
func GenerateStructLen(buffer []byte, l int) int {
if l < 56 {
buffer[0] = byte(192 + l)
return 1
}
if l < 256 {
// l can be encoded as 1 byte
buffer[1] = byte(l)
buffer[0] = byte(247 + 1)
return 2
}
if l < 65536 {
buffer[2] = byte(l & 255)
buffer[1] = byte(l >> 8)
buffer[0] = byte(247 + 2)
return 3
}
buffer[3] = byte(l & 255)
buffer[2] = byte((l >> 8) & 255)
buffer[1] = byte(l >> 16)
buffer[0] = byte(247 + 3)
return 4
}