Skip to content

Commit

Permalink
makes use of GetEncoders, public function of config (#1027)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjezek authored Sep 17, 2024
1 parent 03afd27 commit 8b4fd1b
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 63 deletions.
32 changes: 30 additions & 2 deletions go/database/mpt/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

package mpt

import "github.com/Fantom-foundation/Carmen/go/backend/stock"
import (
"fmt"
"github.com/Fantom-foundation/Carmen/go/backend/stock"
)

// MptConfig defines a set of configuration options for customizing the MPT
// implementation. It is mainly intended to facilitate the accurate modeling
Expand Down Expand Up @@ -94,7 +97,32 @@ func (c MptConfig) GetEncoders() (
stock.ValueEncoder[ValueNode],
) {

return getEncoder(c)
switch c.HashStorageLocation {
case HashStoredWithParent:
if c.TrackSuffixLengthsInLeafNodes {
return AccountNodeWithPathLengthEncoderWithChildHash{},
BranchNodeEncoderWithChildHashes{},
ExtensionNodeEncoderWithChildHash{},
ValueNodeWithPathLengthEncoderWithoutNodeHash{}
}
return AccountNodeEncoderWithChildHash{},
BranchNodeEncoderWithChildHashes{},
ExtensionNodeEncoderWithChildHash{},
ValueNodeEncoderWithoutNodeHash{}
case HashStoredWithNode:
if c.TrackSuffixLengthsInLeafNodes {
return AccountNodeWithPathLengthEncoderWithNodeHash{},
BranchNodeEncoderWithNodeHash{},
ExtensionNodeEncoderWithNodeHash{},
ValueNodeWithPathLengthEncoderWithNodeHash{}
}
return AccountNodeEncoderWithNodeHash{},
BranchNodeEncoderWithNodeHash{},
ExtensionNodeEncoderWithNodeHash{},
ValueNodeEncoderWithNodeHash{}
default:
panic(fmt.Sprintf("unknown mode: %v", c.HashStorageLocation))
}
}

type HashStorageLocation bool
Expand Down
42 changes: 40 additions & 2 deletions go/database/mpt/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@

package mpt

import "testing"
import (
"fmt"
"github.com/Fantom-foundation/Carmen/go/backend/stock"
"testing"
)

func TestMptConfig_GetEncoders(t *testing.T) {
for _, config := range allMptConfigs {
a1, b1, e1, v1 := config.GetEncoders()
a2, b2, e2, v2 := getEncoder(config)
a2, b2, e2, v2 := getExpectedEncoders(config)

if a1 != a2 {
t.Errorf("unexpected account node encoder, got %v, want %v", a1, a2)
Expand All @@ -31,3 +35,37 @@ func TestMptConfig_GetEncoders(t *testing.T) {
}
}
}

func getExpectedEncoders(config MptConfig) (
stock.ValueEncoder[AccountNode],
stock.ValueEncoder[BranchNode],
stock.ValueEncoder[ExtensionNode],
stock.ValueEncoder[ValueNode],
) {
switch config.HashStorageLocation {
case HashStoredWithParent:
if config.TrackSuffixLengthsInLeafNodes {
return AccountNodeWithPathLengthEncoderWithChildHash{},
BranchNodeEncoderWithChildHashes{},
ExtensionNodeEncoderWithChildHash{},
ValueNodeWithPathLengthEncoderWithoutNodeHash{}
}
return AccountNodeEncoderWithChildHash{},
BranchNodeEncoderWithChildHashes{},
ExtensionNodeEncoderWithChildHash{},
ValueNodeEncoderWithoutNodeHash{}
case HashStoredWithNode:
if config.TrackSuffixLengthsInLeafNodes {
return AccountNodeWithPathLengthEncoderWithNodeHash{},
BranchNodeEncoderWithNodeHash{},
ExtensionNodeEncoderWithNodeHash{},
ValueNodeWithPathLengthEncoderWithNodeHash{}
}
return AccountNodeEncoderWithNodeHash{},
BranchNodeEncoderWithNodeHash{},
ExtensionNodeEncoderWithNodeHash{},
ValueNodeEncoderWithNodeHash{}
default:
panic(fmt.Sprintf("unknown mode: %v", config.HashStorageLocation))
}
}
38 changes: 2 additions & 36 deletions go/database/mpt/forest.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func OpenInMemoryForest(directory string, mptConfig MptConfig, forestConfig Fore
}
}()

accountEncoder, branchEncoder, extensionEncoder, valueEncoder := getEncoder(mptConfig)
accountEncoder, branchEncoder, extensionEncoder, valueEncoder := mptConfig.GetEncoders()
branches, err := memory.OpenStock[uint64, BranchNode](branchEncoder, directory+"/branches")
if err != nil {
return nil, err
Expand Down Expand Up @@ -193,7 +193,7 @@ func OpenFileForest(directory string, mptConfig MptConfig, forestConfig ForestCo
}()

accountsDir, branchsDir, extensionsDir, valuesDir := getForestDirectories(directory)
accountEncoder, branchEncoder, extensionEncoder, valueEncoder := getEncoder(mptConfig)
accountEncoder, branchEncoder, extensionEncoder, valueEncoder := mptConfig.GetEncoders()
branches, err := file.OpenStock[uint64, BranchNode](branchEncoder, branchsDir)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1045,40 +1045,6 @@ func getForestDirectories(root string) (
filepath.Join(root, "values")
}

func getEncoder(config MptConfig) (
stock.ValueEncoder[AccountNode],
stock.ValueEncoder[BranchNode],
stock.ValueEncoder[ExtensionNode],
stock.ValueEncoder[ValueNode],
) {
switch config.HashStorageLocation {
case HashStoredWithParent:
if config.TrackSuffixLengthsInLeafNodes {
return AccountNodeWithPathLengthEncoderWithChildHash{},
BranchNodeEncoderWithChildHashes{},
ExtensionNodeEncoderWithChildHash{},
ValueNodeWithPathLengthEncoderWithoutNodeHash{}
}
return AccountNodeEncoderWithChildHash{},
BranchNodeEncoderWithChildHashes{},
ExtensionNodeEncoderWithChildHash{},
ValueNodeEncoderWithoutNodeHash{}
case HashStoredWithNode:
if config.TrackSuffixLengthsInLeafNodes {
return AccountNodeWithPathLengthEncoderWithNodeHash{},
BranchNodeEncoderWithNodeHash{},
ExtensionNodeEncoderWithNodeHash{},
ValueNodeWithPathLengthEncoderWithNodeHash{}
}
return AccountNodeEncoderWithNodeHash{},
BranchNodeEncoderWithNodeHash{},
ExtensionNodeEncoderWithNodeHash{},
ValueNodeEncoderWithNodeHash{}
default:
panic(fmt.Sprintf("unknown mode: %v", config.HashStorageLocation))
}
}

type writeBufferSink struct {
forest *Forest
}
Expand Down
2 changes: 1 addition & 1 deletion go/database/mpt/forest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1610,7 +1610,7 @@ func testForest_WriteBufferRecoveryIsThreadSafe(t *testing.T, withConcurrentNode
// synchronization features of the file-based stock which have caused problems
// in the past.
func openFileShadowForest(directory string, mptConfig MptConfig, forestConfig ForestConfig) (*Forest, error) {
accountEncoder, branchEncoder, extensionEncoder, valueEncoder := getEncoder(mptConfig)
accountEncoder, branchEncoder, extensionEncoder, valueEncoder := mptConfig.GetEncoders()
branchesA, err := file.OpenStock[uint64, BranchNode](branchEncoder, directory+"/A/branches")
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions go/database/mpt/verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func verifyForest(directory string, config MptConfig, roots []Root, source *veri

// Verify stock data structures.
observer.Progress("Checking meta-data ...")
accountEncoder, branchEncoder, extensionEncoder, valueEncoder := getEncoder(config)
accountEncoder, branchEncoder, extensionEncoder, valueEncoder := config.GetEncoders()
if err := file.VerifyStock[uint64](directory+"/accounts", accountEncoder); err != nil {
return err
}
Expand Down Expand Up @@ -694,7 +694,7 @@ func openVerificationNodeSource(ctx context.Context, directory string, config Mp
}

success := false
accountEncoder, branchEncoder, extensionEncoder, valueEncoder := getEncoder(config)
accountEncoder, branchEncoder, extensionEncoder, valueEncoder := config.GetEncoders()
branches, err := file.OpenStock[uint64, BranchNode](branchEncoder, directory+"/branches")
if err != nil {
return nil, err
Expand Down
40 changes: 20 additions & 20 deletions go/database/mpt/verification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func TestVerification_ModifiedFileIsDetected(t *testing.T) {

func TestVerification_ModifiedRootIsDetected(t *testing.T) {
runVerificationTest(t, func(t *testing.T, dir string, config MptConfig, roots []Root) {
_, encoder, _, _ := getEncoder(config)
_, encoder, _, _ := config.GetEncoders()

root := NewNodeReference(EmptyId())
for i := 0; i < len(roots); i++ {
Expand Down Expand Up @@ -175,7 +175,7 @@ func TestVerification_ModifiedRootIsDetected(t *testing.T) {

func TestVerification_AccountBalanceModificationIsDetected(t *testing.T) {
runVerificationTest(t, func(t *testing.T, dir string, config MptConfig, roots []Root) {
encoder, _, _, _ := getEncoder(config)
encoder, _, _, _ := config.GetEncoders()

modifyNode(t, dir+"/accounts", encoder, func(node *AccountNode) {
node.info.Balance = amount.Add(node.info.Balance, amount.New(1))
Expand All @@ -189,7 +189,7 @@ func TestVerification_AccountBalanceModificationIsDetected(t *testing.T) {

func TestVerification_AccountNonceModificationIsDetected(t *testing.T) {
runVerificationTest(t, func(t *testing.T, dir string, config MptConfig, roots []Root) {
encoder, _, _, _ := getEncoder(config)
encoder, _, _, _ := config.GetEncoders()

modifyNode(t, dir+"/accounts", encoder, func(node *AccountNode) {
node.info.Nonce[2]++
Expand All @@ -203,7 +203,7 @@ func TestVerification_AccountNonceModificationIsDetected(t *testing.T) {

func TestVerification_AccountCodeHashModificationIsDetected(t *testing.T) {
runVerificationTest(t, func(t *testing.T, dir string, config MptConfig, roots []Root) {
encoder, _, _, _ := getEncoder(config)
encoder, _, _, _ := config.GetEncoders()

modifyNode(t, dir+"/accounts", encoder, func(node *AccountNode) {
node.info.CodeHash[2]++
Expand All @@ -217,7 +217,7 @@ func TestVerification_AccountCodeHashModificationIsDetected(t *testing.T) {

func TestVerification_AccountStorageModificationIsDetected(t *testing.T) {
runVerificationTest(t, func(t *testing.T, dir string, config MptConfig, roots []Root) {
encoder, _, _, _ := getEncoder(config)
encoder, _, _, _ := config.GetEncoders()

modifyNode(t, dir+"/accounts", encoder, func(node *AccountNode) {
node.storage = NewNodeReference(ValueId(123456789)) // invalid in test forest
Expand All @@ -234,7 +234,7 @@ func TestVerification_AccountNodeHashModificationIsDetected(t *testing.T) {
if config.HashStorageLocation != HashStoredWithNode {
return
}
encoder, _, _, _ := getEncoder(config)
encoder, _, _, _ := config.GetEncoders()

modifyNode(t, dir+"/accounts", encoder, func(node *AccountNode) {
node.hash[3]++
Expand All @@ -251,7 +251,7 @@ func TestVerification_AccountStorageHashModificationIsDetected(t *testing.T) {
if config.HashStorageLocation != HashStoredWithParent {
return
}
encoder, _, _, _ := getEncoder(config)
encoder, _, _, _ := config.GetEncoders()

modifyNode(t, dir+"/accounts", encoder, func(node *AccountNode) {
node.storageHash[3]++
Expand All @@ -265,7 +265,7 @@ func TestVerification_AccountStorageHashModificationIsDetected(t *testing.T) {

func TestVerification_BranchChildIdModificationIsDetected(t *testing.T) {
runVerificationTest(t, func(t *testing.T, dir string, config MptConfig, roots []Root) {
_, encoder, _, _ := getEncoder(config)
_, encoder, _, _ := config.GetEncoders()

modifyNode(t, dir+"/branches", encoder, func(node *BranchNode) {
node.children[8] = NewNodeReference(ValueId(123456789)) // does not exist in test forest
Expand All @@ -282,7 +282,7 @@ func TestVerification_BranchNodeHashModificationIsDetected(t *testing.T) {
if config.HashStorageLocation != HashStoredWithNode {
return
}
_, encoder, _, _ := getEncoder(config)
_, encoder, _, _ := config.GetEncoders()

modifyNode(t, dir+"/branches", encoder, func(node *BranchNode) {
node.hash[4]++
Expand All @@ -299,7 +299,7 @@ func TestVerification_BranchChildHashModificationIsDetected(t *testing.T) {
if config.HashStorageLocation != HashStoredWithParent {
return
}
_, encoder, _, _ := getEncoder(config)
_, encoder, _, _ := config.GetEncoders()

modifyNode(t, dir+"/branches", encoder, func(node *BranchNode) {
for i, child := range node.children {
Expand All @@ -318,7 +318,7 @@ func TestVerification_BranchChildHashModificationIsDetected(t *testing.T) {

func TestVerification_ExtensionPathModificationIsDetected(t *testing.T) {
runVerificationTest(t, func(t *testing.T, dir string, config MptConfig, roots []Root) {
_, _, encoder, _ := getEncoder(config)
_, _, encoder, _ := config.GetEncoders()

modifyNode(t, dir+"/extensions", encoder, func(node *ExtensionNode) {
node.path.path[0] = ^node.path.path[0]
Expand All @@ -332,7 +332,7 @@ func TestVerification_ExtensionPathModificationIsDetected(t *testing.T) {

func TestVerification_ExtensionNextModificationIsDetected(t *testing.T) {
runVerificationTest(t, func(t *testing.T, dir string, config MptConfig, roots []Root) {
_, _, encoder, _ := getEncoder(config)
_, _, encoder, _ := config.GetEncoders()

modifyNode(t, dir+"/extensions", encoder, func(node *ExtensionNode) {
node.next = NewNodeReference(BranchId(123456789))
Expand All @@ -349,7 +349,7 @@ func TestVerification_ExtensionNodeHashModificationIsDetected(t *testing.T) {
if config.HashStorageLocation != HashStoredWithNode {
return
}
_, _, encoder, _ := getEncoder(config)
_, _, encoder, _ := config.GetEncoders()

modifyNode(t, dir+"/extensions", encoder, func(node *ExtensionNode) {
node.hash[24]++
Expand All @@ -366,7 +366,7 @@ func TestVerification_ExtensionNextHashModificationIsDetected(t *testing.T) {
if config.HashStorageLocation != HashStoredWithParent {
return
}
_, _, encoder, _ := getEncoder(config)
_, _, encoder, _ := config.GetEncoders()

modifyNode(t, dir+"/extensions", encoder, func(node *ExtensionNode) {
node.nextHash[24]++
Expand All @@ -380,7 +380,7 @@ func TestVerification_ExtensionNextHashModificationIsDetected(t *testing.T) {

func TestVerification_ValueKeyModificationIsDetected(t *testing.T) {
runVerificationTest(t, func(t *testing.T, dir string, config MptConfig, roots []Root) {
_, _, _, encoder := getEncoder(config)
_, _, _, encoder := config.GetEncoders()

modifyNode(t, dir+"/values", encoder, func(node *ValueNode) {
node.key[5]++
Expand All @@ -394,7 +394,7 @@ func TestVerification_ValueKeyModificationIsDetected(t *testing.T) {

func TestVerification_ValueModificationIsDetected(t *testing.T) {
runVerificationTest(t, func(t *testing.T, dir string, config MptConfig, roots []Root) {
_, _, _, encoder := getEncoder(config)
_, _, _, encoder := config.GetEncoders()

modifyNode(t, dir+"/values", encoder, func(node *ValueNode) {
node.value[12]++
Expand All @@ -411,7 +411,7 @@ func TestVerification_ValueNodeHashModificationIsDetected(t *testing.T) {
if config.HashStorageLocation != HashStoredWithNode {
return
}
_, _, _, encoder := getEncoder(config)
_, _, _, encoder := config.GetEncoders()

modifyNode(t, dir+"/values", encoder, func(node *ValueNode) {
node.hash[12]++
Expand All @@ -425,7 +425,7 @@ func TestVerification_ValueNodeHashModificationIsDetected(t *testing.T) {

func TestVerification_MissingCodeHashInCodeFileIsDetected(t *testing.T) {
runVerificationTest(t, func(t *testing.T, dir string, config MptConfig, roots []Root) {
encoder, _, _, _ := getEncoder(config)
encoder, _, _, _ := config.GetEncoders()

missingHash := common.Keccak256([]byte{2})

Expand Down Expand Up @@ -641,7 +641,7 @@ func TestVerification_ForestVerificationObserverReportsError(t *testing.T) {
observer.EXPECT().EndVerification(gomock.Not(nil)),
)

encoder, _, _, _ := getEncoder(config)
encoder, _, _, _ := config.GetEncoders()

modifyNode(t, dir+"/accounts", encoder, func(node *AccountNode) {
node.info.Balance = amount.Add(node.info.Balance, amount.New(1))
Expand All @@ -664,7 +664,7 @@ func TestVerification_VerificationObserverReportsError(t *testing.T) {
observer.EXPECT().EndVerification(gomock.Not(nil)),
)

encoder, _, _, _ := getEncoder(config)
encoder, _, _, _ := config.GetEncoders()

modifyNode(t, dir+"/accounts", encoder, func(node *AccountNode) {
node.info.Balance = amount.Add(node.info.Balance, amount.New(1))
Expand Down

0 comments on commit 8b4fd1b

Please sign in to comment.