Skip to content

Commit

Permalink
Fix: fixed bug with default options
Browse files Browse the repository at this point in the history
  • Loading branch information
hmoog committed Apr 6, 2022
1 parent a7b8ca5 commit 6e399cd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
4 changes: 4 additions & 0 deletions packages/ledger/branchdag/branchdag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,17 @@ func TestBranchDAG_ConflictMembers(t *testing.T) {
require.NoError(t, branchID4.FromRandomness())
require.NoError(t, conflictID0.FromRandomness())

return

// create initial branches
assert.True(t, branchDAG.CreateBranch(branchID2, NewBranchIDs(MasterBranchID), NewConflictIDs(conflictID0)))
cachedBranch2 := branchDAG.Storage.CachedBranch(branchID2)
defer cachedBranch2.Release()
branch2, exists := cachedBranch2.Unwrap()
assert.True(t, exists)

return

assert.True(t, branchDAG.CreateBranch(branchID3, NewBranchIDs(MasterBranchID), NewConflictIDs(conflictID0)))
cachedBranch3 := branchDAG.Storage.CachedBranch(branchID3)
defer cachedBranch3.Release()
Expand Down
20 changes: 8 additions & 12 deletions packages/ledger/branchdag/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,17 @@ type options struct {
conflictMemberCacheTime time.Duration
}

// defaultOptions contains the default configuration parameters of the BranchDAG.
var defaultOptions = options{
store: mapdb.NewMapDB(),
cacheTimeProvider: database.NewCacheTimeProvider(0),
branchCacheTime: 60 * time.Second,
childBranchCacheTime: 60 * time.Second,
conflictCacheTime: 60 * time.Second,
conflictMemberCacheTime: 10 * time.Second,
}

// newOptions returns a new options object that corresponds to the handed in options and which is derived from the
// default options.
func newOptions(option ...Option) (new *options) {
clonedDefaultOptions := defaultOptions
return clonedDefaultOptions.apply(option...)
return (&options{
store: mapdb.NewMapDB(),
cacheTimeProvider: database.NewCacheTimeProvider(0),
branchCacheTime: 60 * time.Second,
childBranchCacheTime: 60 * time.Second,
conflictCacheTime: 60 * time.Second,
conflictMemberCacheTime: 10 * time.Second,
}).apply(option...)
}

// apply modifies the options object by overriding the handed in options.
Expand Down
21 changes: 17 additions & 4 deletions packages/ledger/branchdag/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package branchdag

import (
"sync"
"time"

"github.com/cockroachdb/errors"
"github.com/iotaledger/hive.go/byteutils"
Expand Down Expand Up @@ -33,25 +34,37 @@ func newStorage(options *options) (new *Storage) {
branchStorage: objectstorage.New[*Branch](
options.store.WithRealm([]byte{database.PrefixBranchDAG, PrefixBranchStorage}),
options.cacheTimeProvider.CacheTime(options.branchCacheTime),
objectstorage.LeakDetectionEnabled(true),
objectstorage.LeakDetectionEnabled(true, objectstorage.LeakDetectionOptions{
MaxConsumersPerObject: 10,
MaxConsumerHoldTime: 5 * time.Second,
}),
),
childBranchStorage: objectstorage.New[*ChildBranch](
options.store.WithRealm([]byte{database.PrefixBranchDAG, PrefixChildBranchStorage}),
childBranchKeyPartition,
options.cacheTimeProvider.CacheTime(options.childBranchCacheTime),
objectstorage.LeakDetectionEnabled(true),
objectstorage.LeakDetectionEnabled(true, objectstorage.LeakDetectionOptions{
MaxConsumersPerObject: 10,
MaxConsumerHoldTime: 5 * time.Second,
}),
objectstorage.StoreOnCreation(true),
),
conflictStorage: objectstorage.New[*Conflict](
options.store.WithRealm([]byte{database.PrefixBranchDAG, PrefixConflictStorage}),
options.cacheTimeProvider.CacheTime(options.conflictCacheTime),
objectstorage.LeakDetectionEnabled(true),
objectstorage.LeakDetectionEnabled(true, objectstorage.LeakDetectionOptions{
MaxConsumersPerObject: 10,
MaxConsumerHoldTime: 5 * time.Second,
}),
),
conflictMemberStorage: objectstorage.New[*ConflictMember](
options.store.WithRealm([]byte{database.PrefixBranchDAG, PrefixConflictMemberStorage}),
ConflictMemberKeyPartition,
options.cacheTimeProvider.CacheTime(options.conflictCacheTime),
objectstorage.LeakDetectionEnabled(true),
objectstorage.LeakDetectionEnabled(true, objectstorage.LeakDetectionOptions{
MaxConsumersPerObject: 10,
MaxConsumerHoldTime: 5 * time.Second,
}),
objectstorage.StoreOnCreation(true),
),
}
Expand Down

0 comments on commit 6e399cd

Please sign in to comment.