diff --git a/core/blockchain.go b/core/blockchain.go index 619730123931..5cd9d5662710 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -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) @@ -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 } diff --git a/go.mod b/go.mod index 586800ff6011..2973d4cf4e05 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 138cfea5c0df..f280ab16f1f3 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/trie/verkle.go b/trie/verkle.go index a3f188392a1f..3394f55be37d 100644 --- a/trie/verkle.go +++ b/trie/verkle.go @@ -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 @@ -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) { @@ -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 @@ -303,6 +287,7 @@ func (trie *VerkleTrie) Copy(db *Database) *VerkleTrie { db: db, } } + func (trie *VerkleTrie) IsVerkle() bool { return true }