-
Notifications
You must be signed in to change notification settings - Fork 807
Expose merkledb defaults #3748
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
Expose merkledb defaults #3748
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,22 +14,18 @@ import ( | |
"testing" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/avalanchego/database" | ||
"github.com/ava-labs/avalanchego/database/dbtest" | ||
"github.com/ava-labs/avalanchego/database/memdb" | ||
"github.com/ava-labs/avalanchego/ids" | ||
"github.com/ava-labs/avalanchego/trace" | ||
"github.com/ava-labs/avalanchego/utils/hashing" | ||
"github.com/ava-labs/avalanchego/utils/maybe" | ||
"github.com/ava-labs/avalanchego/utils/set" | ||
"github.com/ava-labs/avalanchego/utils/units" | ||
) | ||
|
||
const defaultHistoryLength = 300 | ||
|
||
// newDB returns a new merkle database with the underlying type so that tests can access unexported fields | ||
func newDB(ctx context.Context, db database.Database, config Config) (*merkleDB, error) { | ||
db, err := New(ctx, db, config) | ||
|
@@ -39,22 +35,6 @@ func newDB(ctx context.Context, db database.Database, config Config) (*merkleDB, | |
return db.(*merkleDB), nil | ||
} | ||
|
||
func newDefaultConfig() Config { | ||
return Config{ | ||
BranchFactor: BranchFactor16, | ||
Hasher: DefaultHasher, | ||
RootGenConcurrency: 0, | ||
HistoryLength: defaultHistoryLength, | ||
ValueNodeCacheSize: units.MiB, | ||
IntermediateNodeCacheSize: units.MiB, | ||
IntermediateWriteBufferSize: units.KiB, | ||
IntermediateWriteBatchSize: 256 * units.KiB, | ||
Reg: prometheus.NewRegistry(), | ||
TraceLevel: InfoTrace, | ||
Tracer: trace.Noop, | ||
} | ||
} | ||
|
||
func Test_MerkleDB_Get_Safety(t *testing.T) { | ||
require := require.New(t) | ||
|
||
|
@@ -134,7 +114,7 @@ func Test_MerkleDB_DB_Load_Root_From_DB(t *testing.T) { | |
db, err := New( | ||
context.Background(), | ||
baseDB, | ||
newDefaultConfig(), | ||
NewConfig(), | ||
) | ||
require.NoError(err) | ||
|
||
|
@@ -162,7 +142,7 @@ func Test_MerkleDB_DB_Load_Root_From_DB(t *testing.T) { | |
db, err = New( | ||
context.Background(), | ||
baseDB, | ||
newDefaultConfig(), | ||
NewConfig(), | ||
) | ||
require.NoError(err) | ||
|
||
|
@@ -176,7 +156,7 @@ func Test_MerkleDB_DB_Rebuild(t *testing.T) { | |
|
||
initialSize := 5_000 | ||
|
||
config := newDefaultConfig() | ||
config := NewConfig() | ||
config.ValueNodeCacheSize = uint(initialSize) | ||
config.IntermediateNodeCacheSize = uint(initialSize) | ||
|
||
|
@@ -233,7 +213,7 @@ func Test_MerkleDB_Failed_Batch_Commit(t *testing.T) { | |
db, err := New( | ||
context.Background(), | ||
memDB, | ||
newDefaultConfig(), | ||
NewConfig(), | ||
) | ||
require.NoError(err) | ||
|
||
|
@@ -254,7 +234,7 @@ func Test_MerkleDB_Value_Cache(t *testing.T) { | |
db, err := New( | ||
context.Background(), | ||
memDB, | ||
newDefaultConfig(), | ||
NewConfig(), | ||
) | ||
require.NoError(err) | ||
|
||
|
@@ -903,13 +883,13 @@ const ( | |
) | ||
|
||
func runRandDBTest(require *require.Assertions, r *rand.Rand, rt randTest, tokenSize int) { | ||
db, err := getBasicDBWithBranchFactor(tokenSizeToBranchFactor[tokenSize]) | ||
config := NewConfig() | ||
config.BranchFactor = tokenSizeToBranchFactor[tokenSize] | ||
db, err := New(context.Background(), memdb.New(), config) | ||
require.NoError(err) | ||
|
||
const ( | ||
maxProofLen = 100 | ||
maxPastRoots = defaultHistoryLength | ||
) | ||
maxProofLen := 100 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (No action required) Are you sure you wouldn't rather satisfy the gods of var()? ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know when we're supposed to use var blocks... it seems to make sense for package-level vars, when you want a short-hand for the zero value, or where you want to choose the type of something, but when those criteria aren't met I usually don't use them (and for zero values I usually explicitly assign to the zero value...). |
||
maxPastRoots := int(config.HistoryLength) | ||
|
||
var ( | ||
values = make(map[Key][]byte) // tracks content of the trie | ||
|
@@ -966,7 +946,7 @@ func runRandDBTest(require *require.Assertions, r *rand.Rand, rt randTest, token | |
end, | ||
root, | ||
tokenSize, | ||
db.hasher, | ||
config.Hasher, | ||
)) | ||
case opGenerateChangeProof: | ||
root, err := db.GetMerkleRoot(context.Background()) | ||
|
@@ -1301,7 +1281,7 @@ func TestCrashRecovery(t *testing.T) { | |
merkleDB, err := newDatabase( | ||
context.Background(), | ||
baseDB, | ||
newDefaultConfig(), | ||
NewConfig(), | ||
&mockMetrics{}, | ||
) | ||
require.NoError(err) | ||
|
@@ -1319,7 +1299,7 @@ func TestCrashRecovery(t *testing.T) { | |
newMerkleDB, err := newDatabase( | ||
context.Background(), | ||
baseDB, | ||
newDefaultConfig(), | ||
NewConfig(), | ||
&mockMetrics{}, | ||
) | ||
require.NoError(err) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import ( | |
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/avalanchego/database" | ||
"github.com/ava-labs/avalanchego/database/memdb" | ||
"github.com/ava-labs/avalanchego/ids" | ||
"github.com/ava-labs/avalanchego/utils/hashing" | ||
"github.com/ava-labs/avalanchego/utils/maybe" | ||
|
@@ -675,11 +676,17 @@ func Test_ChangeProof_Missing_History_For_EndRoot(t *testing.T) { | |
t.Logf("Seed: %d", seed) | ||
rand := rand.New(rand.NewSource(seed)) // #nosec G404 | ||
|
||
db, err := getBasicDB() | ||
config := NewConfig() | ||
db, err := newDatabase( | ||
context.Background(), | ||
memdb.New(), | ||
config, | ||
&mockMetrics{}, | ||
) | ||
require.NoError(err) | ||
|
||
roots := []ids.ID{} | ||
for i := 0; i < defaultHistoryLength+1; i++ { | ||
for i := 0; i < int(config.HistoryLength)+1; i++ { | ||
key := make([]byte, 16) | ||
_, _ = rand.Read(key) | ||
require.NoError(db.Put(key, nil)) | ||
|
@@ -1879,11 +1886,6 @@ func FuzzProofVerification(f *testing.F) { | |
|
||
// Generate change proofs and verify that they are valid. | ||
func FuzzChangeProofVerification(f *testing.F) { | ||
const ( | ||
numKeyValues = defaultHistoryLength / 2 | ||
deletePortion = 0.25 | ||
) | ||
|
||
f.Fuzz(func( | ||
t *testing.T, | ||
startBytes []byte, | ||
|
@@ -1894,7 +1896,13 @@ func FuzzChangeProofVerification(f *testing.F) { | |
require := require.New(t) | ||
rand := rand.New(rand.NewSource(randSeed)) // #nosec G404 | ||
|
||
db, err := getBasicDB() | ||
config := NewConfig() | ||
db, err := newDatabase( | ||
context.Background(), | ||
memdb.New(), | ||
config, | ||
&mockMetrics{}, | ||
) | ||
require.NoError(err) | ||
|
||
startRootID, err := db.GetMerkleRoot(context.Background()) | ||
|
@@ -1906,8 +1914,8 @@ func FuzzChangeProofVerification(f *testing.F) { | |
require, | ||
rand, | ||
[]database.Database{db}, | ||
numKeyValues, | ||
deletePortion, | ||
config.HistoryLength/2, | ||
0.25, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (No action required) Maybe continue to use a named var for this magic number so that it can be self-documenting? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just moved it into the function signature because the function signature self-documents what these variables mean |
||
) | ||
|
||
endRootID, err := db.GetMerkleRoot(context.Background()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels a bit odd to default to populating
mockMetrics
- I'm wondering if thenewDefaultConfig
needs to exist at all or if we should just use this. (and populate the metrics reg here)..Although that feels a bit odd as well... so 🤷 maybe the issue is with the
mockMetrics
as a whole...It feels like whoever made
mockMetrics
didn't know that"github.com/prometheus/client_golang/prometheus/testutil"
existed.