diff --git a/merkle/merkle.go b/merkle/merkle.go index c2a7088fea..b5df63a5f1 100644 --- a/merkle/merkle.go +++ b/merkle/merkle.go @@ -5,14 +5,13 @@ import ( "github.com/ava-labs/avalanchego/database" "github.com/ava-labs/avalanchego/database/memdb" - "github.com/ava-labs/avalanchego/trace" "github.com/ava-labs/avalanchego/x/merkledb" "github.com/ava-labs/hypersdk/utils" "github.com/ava-labs/avalanchego/ids" ) // Generate merkle root for a set of items -func GenerateMerkleRoot(ctx context.Context, config merkledb.Config, tracer trace.Tracer, merkleItems [][]byte, consumeBytes bool) (ids.ID, merkledb.MerkleDB, error) { +func GenerateMerkleRoot(ctx context.Context, config merkledb.Config, merkleItems [][]byte, consumeBytes bool) (ids.ID, merkledb.MerkleDB, error) { batchOps := make([]database.BatchOp, 0, len(merkleItems)) for _, item := range merkleItems { @@ -32,11 +31,8 @@ func GenerateMerkleRoot(ctx context.Context, config merkledb.Config, tracer trac if err != nil { return ids.Empty, nil, err } - if err := view.CommitToDB(ctx); err != nil { - return ids.Empty, nil, err - } - root, err := db.GetMerkleRoot(ctx) + root, err := view.GetMerkleRoot(ctx) if err != nil { return ids.Empty, nil, err } diff --git a/merkle/merkle_test.go b/merkle/merkle_test.go index 176411209c..99853f0b33 100644 --- a/merkle/merkle_test.go +++ b/merkle/merkle_test.go @@ -11,6 +11,7 @@ import ( "github.com/ava-labs/avalanchego/trace" "github.com/ava-labs/avalanchego/utils/units" "github.com/ava-labs/avalanchego/x/merkledb" + "github.com/stretchr/testify/require" ) var resRoot ids.ID @@ -19,43 +20,41 @@ var resErr error func BenchmarkMerkleTxRoot(b *testing.B) { for _, size := range []int{10, 100, 1000, 10000} { - ctx := context.TODO() - tracer := trace.Noop - merkleItems := make([][]byte, 0, size) - for i := 0; i < size; i++ { - item := make([]byte, 32) - _, err := rand.Read(item) - if err != nil { - b.Fatal(err) - } - merkleItems = append(merkleItems, item) - } - - var root ids.ID - var db merkledb.MerkleDB - var err error - - defaultConfig := merkledb.Config{ - BranchFactor: merkledb.BranchFactor16, - HistoryLength: 100, - IntermediateNodeCacheSize: units.MiB, - ValueNodeCacheSize: units.MiB, - Tracer: tracer, - } + ctx := context.TODO() + tracer := trace.Noop + merkleItems := make([][]byte, 0, size) + for i := 0; i < size; i++ { + item := make([]byte, 32) + _, err := rand.Read(item) + require.NoError(b, err) + merkleItems = append(merkleItems, item) + } + + var root ids.ID + var db merkledb.MerkleDB + var err error + + defaultConfig := merkledb.Config{ + BranchFactor: merkledb.BranchFactor16, + HistoryLength: 100, + IntermediateNodeCacheSize: units.MiB, + ValueNodeCacheSize: units.MiB, + Tracer: tracer, + } b.Run(strconv.Itoa(size), func(b *testing.B) { for n := 0; n < b.N; n++ { for i := 0; i < size; i++ { - root, db, err = GenerateMerkleRoot(ctx, defaultConfig, tracer, merkleItems, false) + root, db, err = GenerateMerkleRoot(ctx, defaultConfig, merkleItems, false) } } }) - // avoid compiler optimizations to cancel out the bench - resRoot = root - resDb = db - resErr = err + // avoid compiler optimizations to cancel out the bench + resRoot = root + resDb = db + resErr = err } - b.ReportAllocs() + b.ReportAllocs() }