Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion and TransitionTrie fixes #346

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions core/overlay/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var zeroTreeIndex uint256.Int
// collect the key-values in a way that is efficient.
type keyValueMigrator struct {
// leafData contains the values for the future leaf for a particular VKT branch.
leafData []migratedKeyValue
leafData map[branchKey]*migratedKeyValue

// When prepare() is called, it will start a background routine that will process the leafData
// saving the result in newLeaves to be used by migrateCollectedKeyValues(). The background
Expand All @@ -66,7 +66,7 @@ func newKeyValueMigrator() *keyValueMigrator {
_ = verkle.GetConfig()
return &keyValueMigrator{
processingReady: make(chan struct{}),
leafData: make([]migratedKeyValue, 0, 10_000),
leafData: make(map[branchKey]*migratedKeyValue, 10_000),
}
}

Expand Down Expand Up @@ -138,19 +138,17 @@ func (kvm *keyValueMigrator) addAccountCode(addr []byte, codeSize uint64, chunks
}

func (kvm *keyValueMigrator) getOrInitLeafNodeData(bk branchKey) *verkle.BatchNewLeafNodeData {
// Remember that keyValueMigration receives actions ordered by (address, subtreeIndex).
// This means that we can assume that the last element of leafData is the one that we
// are looking for, or that we need to create a new one.
if len(kvm.leafData) == 0 || kvm.leafData[len(kvm.leafData)-1].branchKey != bk {
kvm.leafData = append(kvm.leafData, migratedKeyValue{
branchKey: bk,
leafNodeData: verkle.BatchNewLeafNodeData{
Stem: nil, // It will be calculated in the prepare() phase, since it's CPU heavy.
Values: make(map[byte][]byte),
},
})
if ld, ok := kvm.leafData[bk]; ok {
return &ld.leafNodeData
}
return &kvm.leafData[len(kvm.leafData)-1].leafNodeData
kvm.leafData[bk] = &migratedKeyValue{
branchKey: bk,
leafNodeData: verkle.BatchNewLeafNodeData{
Stem: nil, // It will be calculated in the prepare() phase, since it's CPU heavy.
Values: make(map[byte][]byte, 256),
},
}
return &kvm.leafData[bk].leafNodeData
}

func (kvm *keyValueMigrator) prepare() {
Expand All @@ -159,6 +157,10 @@ func (kvm *keyValueMigrator) prepare() {
go func() {
// Step 1: We split kvm.leafData in numBatches batches, and we process each batch in a separate goroutine.
// This fills each leafNodeData.Stem with the correct value.
leafData := make([]migratedKeyValue, 0, len(kvm.leafData))
for _, v := range kvm.leafData {
leafData = append(leafData, *v)
}
var wg sync.WaitGroup
batchNum := runtime.NumCPU()
batchSize := (len(kvm.leafData) + batchNum - 1) / batchNum
Expand All @@ -170,7 +172,7 @@ func (kvm *keyValueMigrator) prepare() {
}
wg.Add(1)

batch := kvm.leafData[start:end]
batch := leafData[start:end]
go func() {
defer wg.Done()
var currAddr common.Address
Expand All @@ -190,8 +192,8 @@ func (kvm *keyValueMigrator) prepare() {

// Step 2: Now that we have all stems (i.e: tree keys) calculated, we can create the new leaves.
nodeValues := make([]verkle.BatchNewLeafNodeData, len(kvm.leafData))
for i := range kvm.leafData {
nodeValues[i] = kvm.leafData[i].leafNodeData
for i := range leafData {
nodeValues[i] = leafData[i].leafNodeData
}

// Create all leaves in batch mode so we can optimize cryptography operations.
Expand Down
8 changes: 7 additions & 1 deletion trie/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package trie

import (
"fmt"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb"
Expand Down Expand Up @@ -62,7 +64,11 @@ func (t *TransitionTrie) GetKey(key []byte) []byte {
// not be modified by the caller. If a node was not found in the database, a
// trie.MissingNodeError is returned.
func (t *TransitionTrie) GetStorage(addr common.Address, key []byte) ([]byte, error) {
if val, err := t.overlay.GetStorage(addr, key); len(val) != 0 || err != nil {
jsign marked this conversation as resolved.
Show resolved Hide resolved
val, err := t.overlay.GetStorage(addr, key)
if err != nil {
return nil, fmt.Errorf("get storage from overlay: %s", err)
}
if len(val) != 0 {
jsign marked this conversation as resolved.
Show resolved Hide resolved
return val, nil
}
// TODO also insert value into overlay
Expand Down