Skip to content

Commit

Permalink
Use new go-verkle batch serialization (ethereum#172)
Browse files Browse the repository at this point in the history
* core: fix converted tree hash

Signed-off-by: Ignacio Hagopian <[email protected]>

* trie/verkle: apply batch serialization in VKT nodes

Signed-off-by: Ignacio Hagopian <[email protected]>

* trie/verkle: also batch key bytes

Signed-off-by: Ignacio Hagopian <[email protected]>

* use tentative go-verkle pr

Signed-off-by: Ignacio Hagopian <[email protected]>

* update go-verkle tentative pr

Signed-off-by: Ignacio Hagopian <[email protected]>

---------

Signed-off-by: Ignacio Hagopian <[email protected]>
  • Loading branch information
jsign authored Mar 7, 2023
1 parent faf21ec commit 7d1fb48
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 39 deletions.
4 changes: 2 additions & 2 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
// a background routine to re-indexed all indices in [ancients - txlookupLimit, ancients)
// range. In this case, all tx indices of newly imported blocks should be
// generated.
var batch = bc.db.NewBatch()
batch := bc.db.NewBatch()
for i, block := range blockChain {
if bc.txLookupLimit == 0 || ancientLimit <= bc.txLookupLimit || block.NumberU64() >= ancientLimit-bc.txLookupLimit {
rawdb.WriteTxLookupEntriesByBlock(batch, block)
Expand Down Expand Up @@ -2267,7 +2267,7 @@ func (bc *BlockChain) maintainTxIndex(ancients uint64) {
// need to reindex all necessary transactions before starting to process any
// pruning requests.
if ancients > 0 {
var from = uint64(0)
from := uint64(0)
if bc.txLookupLimit != 0 && ancients > bc.txLookupLimit {
from = ancients - bc.txLookupLimit
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/fjl/gencodec v0.0.0-20220412091415-8bb9e558978c
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff
github.com/gballet/go-verkle v0.0.0-20230224151942-0236f337641c
github.com/gballet/go-verkle v0.0.0-20230306134419-0d414350453b
github.com/go-stack/stack v1.8.0
github.com/golang-jwt/jwt/v4 v4.3.0
github.com/golang/protobuf v1.5.2
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ github.com/consensys/gnark-crypto v0.4.1-0.20210426202927-39ac3d4b3f1f/go.mod h1
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/crate-crypto/go-ipa v0.0.0-20221111143132-9aa5d42120bc/go.mod h1:gFnFS95y8HstDP6P9pPwzrxOOC5TRDkwbM+ao15ChAI=
github.com/crate-crypto/go-ipa v0.0.0-20230202201618-2e6f5bfc5401 h1:TSXRL74LZ7R2xWOI1M0mz9E56PiPKGlSw0drgR8g7CE=
github.com/crate-crypto/go-ipa v0.0.0-20230202201618-2e6f5bfc5401/go.mod h1:gFnFS95y8HstDP6P9pPwzrxOOC5TRDkwbM+ao15ChAI=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down Expand Up @@ -137,8 +136,8 @@ github.com/garslo/gogen v0.0.0-20170306192744-1d203ffc1f61 h1:IZqZOB2fydHte3kUgx
github.com/garslo/gogen v0.0.0-20170306192744-1d203ffc1f61/go.mod h1:Q0X6pkwTILDlzrGEckF6HKjXe48EgsY/l7K7vhY4MW8=
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff h1:tY80oXqGNY4FhTFhk+o9oFHGINQ/+vhlm8HFzi6znCI=
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff/go.mod h1:x7DCsMOv1taUwEWCzT4cmDeAkigA5/QCwUodaVOe8Ww=
github.com/gballet/go-verkle v0.0.0-20230224151942-0236f337641c h1:VQ6XsQHZzynoHGd9ef7KCs9WsMAUc4/OnhtIZmyIYag=
github.com/gballet/go-verkle v0.0.0-20230224151942-0236f337641c/go.mod h1:DMDd04jjQgdynaAwbEgiRERIGpC8fDjx0+y06an7Psg=
github.com/gballet/go-verkle v0.0.0-20230306134419-0d414350453b h1:xCCWzSggGQY77ybfs+f63kl+rAy/5JWERFbqcQjN7z8=
github.com/gballet/go-verkle v0.0.0-20230306134419-0d414350453b/go.mod h1:NR+n/LUx+m5SyVTRObiuNdJ50q309MGPD0VrIMDZJuc=
github.com/getkin/kin-openapi v0.53.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4=
github.com/getkin/kin-openapi v0.61.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down
51 changes: 18 additions & 33 deletions trie/verkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ func (t *VerkleTrie) TryGetAccount(key []byte) (*types.StateAccount, error) {
ck, err := t.TryGet(ckkey[:])
if err != nil {
return nil, fmt.Errorf("updateStateObject (%x) error: %v", key, err)

}
acc.CodeHash = ck

Expand Down Expand Up @@ -156,7 +155,6 @@ func (t *VerkleTrie) TryUpdateAccount(key []byte, acc *types.StateAccount) error
}

func (trie *VerkleTrie) TryUpdateStem(key []byte, values [][]byte) {

switch root := trie.root.(type) {
case *verkle.StatelessNode:
root.InsertAtStem(key, values, func(h []byte) ([]byte, error) {
Expand Down Expand Up @@ -239,45 +237,31 @@ func nodeToDBKey(n verkle.VerkleNode) []byte {
// Commit writes all nodes to the trie's memory database, tracking the internal
// and external (for account tries) references.
func (trie *VerkleTrie) Commit(onleaf LeafCallback) (common.Hash, int, error) {
flush := make(chan verkle.VerkleNode)
flusher := func(n verkle.VerkleNode) {
root, ok := trie.root.(*verkle.InternalNode)
if !ok {
return common.Hash{}, 0, errors.New("unexpected root node type")
}
nodes, err := root.BatchSerialize()
if err != nil {
return common.Hash{}, 0, fmt.Errorf("serializing tree nodes: %s", err)
}

for _, node := range nodes {
if onleaf != nil {
if leaf, isLeaf := n.(*verkle.LeafNode); isLeaf {
for i := 0; i < verkle.NodeWidth; i++ {
if leaf.Value(i) != nil {
comm := n.Commitment().Bytes()
onleaf(nil, nil, leaf.Value(i), common.BytesToHash(comm[:]))
if leaf, isLeaf := node.Node.(*verkle.LeafNode); isLeaf {
for j := 0; j < verkle.NodeWidth; j++ {
if leaf.Value(j) != nil {
onleaf(nil, nil, leaf.Value(j), common.BytesToHash(node.CommitmentBytes[:]))
}
}
}
}
flush <- n
}
go func() {
switch root := trie.root.(type) {
case *verkle.StatelessNode:
root.Flush(flusher)
case *verkle.InternalNode:
root.Flush(flusher)
default:
log.Crit("root was not flushed")
}
close(flush)
}()
var commitCount int
for n := range flush {
commitCount += 1
value, err := n.Serialize()
if err != nil {
panic(err)
}

if err := trie.db.DiskDB().Put(nodeToDBKey(n), value); err != nil {
return common.Hash{}, commitCount, err
if err := trie.db.DiskDB().Put(node.CommitmentBytes[:], node.SerializedBytes); err != nil {
return common.Hash{}, 0, fmt.Errorf("put node to disk: %s", err)
}
}

return trie.Hash(), commitCount, nil
return nodes[0].CommitmentBytes, len(nodes), nil
}

// NodeIterator returns an iterator that returns nodes of the trie. Iteration
Expand All @@ -303,6 +287,7 @@ func (trie *VerkleTrie) Copy(db *Database) *VerkleTrie {
db: db,
}
}

func (trie *VerkleTrie) IsVerkle() bool {
return true
}
Expand Down

0 comments on commit 7d1fb48

Please sign in to comment.