From b693e94400fdcd89740791cb97dd1b2a9d79954e Mon Sep 17 00:00:00 2001 From: Andrea V <1577639+karimodm@users.noreply.github.com> Date: Tue, 1 Aug 2023 14:22:50 +0200 Subject: [PATCH] Flatten ads package to avoid exporting concrete types --- ads/ads.go | 6 ++-- ads/aset/authenticated_set.go | 31 --------------------- ads/{amap => }/authenticated_map.go | 30 ++++++++++---------- ads/{amap => }/authenticated_map_test.go | 11 ++++---- ads/authenticated_set.go | 30 ++++++++++++++++++++ ads/{aset => }/authenticated_set_test.go | 35 ++++-------------------- 6 files changed, 57 insertions(+), 86 deletions(-) delete mode 100644 ads/aset/authenticated_set.go rename ads/{amap => }/authenticated_map.go (88%) rename ads/{amap => }/authenticated_map_test.go (94%) create mode 100644 ads/authenticated_set.go rename ads/{aset => }/authenticated_set_test.go (74%) diff --git a/ads/ads.go b/ads/ads.go index 85869703c..63b8cece1 100644 --- a/ads/ads.go +++ b/ads/ads.go @@ -1,8 +1,6 @@ package ads import ( - "github.com/iotaledger/hive.go/ads/amap" - "github.com/iotaledger/hive.go/ads/aset" "github.com/iotaledger/hive.go/ds/types" "github.com/iotaledger/hive.go/kvstore" ) @@ -40,7 +38,7 @@ type Map[K, V any] interface { // NewMap creates a new AuthenticatedMap. func NewMap[K, V any](store kvstore.KVStore, kToBytes kvstore.ObjectToBytes[K], bytesToK kvstore.BytesToObject[K], vToBytes kvstore.ObjectToBytes[V], bytesToV kvstore.BytesToObject[V]) Map[K, V] { - return amap.NewAuthenticatedMap(store, kToBytes, bytesToK, vToBytes, bytesToV) + return newAuthenticatedMap(store, kToBytes, bytesToK, vToBytes, bytesToV) } // Set is a sparse merkle tree based set. @@ -57,5 +55,5 @@ type Set[K any] interface { // NewSet creates a new sparse merkle tree based map. func NewSet[K any](store kvstore.KVStore, kToBytes kvstore.ObjectToBytes[K], bytesToK kvstore.BytesToObject[K]) Set[K] { - return aset.NewAuthenticatedSet(store, kToBytes, bytesToK) + return newAuthenticatedSet(store, kToBytes, bytesToK) } diff --git a/ads/aset/authenticated_set.go b/ads/aset/authenticated_set.go deleted file mode 100644 index 5cd4610c3..000000000 --- a/ads/aset/authenticated_set.go +++ /dev/null @@ -1,31 +0,0 @@ -package aset - -import ( - "github.com/iotaledger/hive.go/ads/amap" - "github.com/iotaledger/hive.go/ds/types" - "github.com/iotaledger/hive.go/kvstore" -) - -// Set is a sparse merkle tree based set. -type AuthenticatedSet[K any] struct { - *amap.AuthenticatedMap[K, types.Empty] -} - -// NewAuthenticatedSet creates a new sparse merkle tree based set. -func NewAuthenticatedSet[K any](store kvstore.KVStore, kToBytes kvstore.ObjectToBytes[K], bytesToK kvstore.BytesToObject[K]) *AuthenticatedSet[K] { - return &AuthenticatedSet[K]{ - AuthenticatedMap: amap.NewAuthenticatedMap(store, kToBytes, bytesToK, types.Empty.Bytes, types.EmptyFromBytes), - } -} - -// Add adds the key to the set. -func (s *AuthenticatedSet[K]) Add(key K) error { - return s.Set(key, types.Void) -} - -// Stream iterates over the set and calls the callback for each element. -func (s *AuthenticatedSet[K]) Stream(callback func(key K) error) error { - return s.AuthenticatedMap.Stream(func(key K, _ types.Empty) error { - return callback(key) - }) -} diff --git a/ads/amap/authenticated_map.go b/ads/authenticated_map.go similarity index 88% rename from ads/amap/authenticated_map.go rename to ads/authenticated_map.go index 93b80c924..cafcd52ee 100644 --- a/ads/amap/authenticated_map.go +++ b/ads/authenticated_map.go @@ -1,4 +1,4 @@ -package amap +package ads import ( "crypto/sha256" @@ -22,7 +22,7 @@ const ( ) // AuthenticatedMap is a sparse merkle tree based map. -type AuthenticatedMap[K, V any] struct { +type authenticatedMap[K, V any] struct { rawKeysStore *kvstore.TypedStore[K, types.Empty] tree *smt.SMT size *typedkey.Number[uint64] @@ -36,14 +36,14 @@ type AuthenticatedMap[K, V any] struct { } // NewAuthenticatedMap creates a new authenticated map. -func NewAuthenticatedMap[K, V any]( +func newAuthenticatedMap[K, V any]( store kvstore.KVStore, kToBytes kvstore.ObjectToBytes[K], bytesToK kvstore.BytesToObject[K], vToBytes kvstore.ObjectToBytes[V], bytesToV kvstore.BytesToObject[V], -) *AuthenticatedMap[K, V] { - newMap := &AuthenticatedMap[K, V]{ +) *authenticatedMap[K, V] { + newMap := &authenticatedMap[K, V]{ rawKeysStore: kvstore.NewTypedStore(lo.PanicOnErr(store.WithExtendedRealm([]byte{prefixRawKeysStorage})), kToBytes, bytesToK, types.Empty.Bytes, types.EmptyFromBytes), size: typedkey.NewNumber[uint64](store, prefixSizeKey), root: typedkey.NewBytes(store, prefixRootKey), @@ -64,12 +64,12 @@ func NewAuthenticatedMap[K, V any]( } // WasRestoredFromStorage returns true if the map has been restored from storage. -func (m *AuthenticatedMap[K, V]) WasRestoredFromStorage() bool { +func (m *authenticatedMap[K, V]) WasRestoredFromStorage() bool { return len(m.root.Get()) != 0 } // Root returns the root of the state sparse merkle tree at the latest committed slot. -func (m *AuthenticatedMap[K, V]) Root() (root types.Identifier) { +func (m *authenticatedMap[K, V]) Root() (root types.Identifier) { m.mutex.RLock() defer m.mutex.RUnlock() @@ -79,7 +79,7 @@ func (m *AuthenticatedMap[K, V]) Root() (root types.Identifier) { } // Set sets the output to unspent outputs set. -func (m *AuthenticatedMap[K, V]) Set(key K, value V) error { +func (m *authenticatedMap[K, V]) Set(key K, value V) error { m.mutex.Lock() defer m.mutex.Unlock() @@ -114,7 +114,7 @@ func (m *AuthenticatedMap[K, V]) Set(key K, value V) error { } // Size returns the number of elements in the map. -func (m *AuthenticatedMap[K, V]) Size() int { +func (m *authenticatedMap[K, V]) Size() int { m.mutex.RLock() defer m.mutex.RUnlock() @@ -122,7 +122,7 @@ func (m *AuthenticatedMap[K, V]) Size() int { } // Commit persists the current state of the map to the storage. -func (m *AuthenticatedMap[K, V]) Commit() error { +func (m *authenticatedMap[K, V]) Commit() error { m.mutex.Lock() defer m.mutex.Unlock() @@ -132,7 +132,7 @@ func (m *AuthenticatedMap[K, V]) Commit() error { } // Delete removes the key from the map. -func (m *AuthenticatedMap[K, V]) Delete(key K) (deleted bool, err error) { +func (m *authenticatedMap[K, V]) Delete(key K) (deleted bool, err error) { m.mutex.Lock() defer m.mutex.Unlock() @@ -166,7 +166,7 @@ func (m *AuthenticatedMap[K, V]) Delete(key K) (deleted bool, err error) { } // Has returns true if the key is in the set. -func (m *AuthenticatedMap[K, V]) Has(key K) (has bool, err error) { +func (m *authenticatedMap[K, V]) Has(key K) (has bool, err error) { m.mutex.Lock() defer m.mutex.Unlock() @@ -179,7 +179,7 @@ func (m *AuthenticatedMap[K, V]) Has(key K) (has bool, err error) { } // Get returns the value for the given key. -func (m *AuthenticatedMap[K, V]) Get(key K) (value V, exists bool, err error) { +func (m *authenticatedMap[K, V]) Get(key K) (value V, exists bool, err error) { m.mutex.Lock() defer m.mutex.Unlock() @@ -210,7 +210,7 @@ func (m *AuthenticatedMap[K, V]) Get(key K) (value V, exists bool, err error) { } // Stream streams all the keys and values. -func (m *AuthenticatedMap[K, V]) Stream(callback func(key K, value V) error) error { +func (m *authenticatedMap[K, V]) Stream(callback func(key K, value V) error) error { m.mutex.Lock() defer m.mutex.Unlock() @@ -252,7 +252,7 @@ func (m *AuthenticatedMap[K, V]) Stream(callback func(key K, value V) error) err } // has returns true if the key is in the map. -func (m *AuthenticatedMap[K, V]) has(keyBytes []byte) (has bool, err error) { +func (m *authenticatedMap[K, V]) has(keyBytes []byte) (has bool, err error) { value, err := m.tree.Get(keyBytes) if err != nil { return false, ierrors.Wrap(err, "failed to get from tree") diff --git a/ads/amap/authenticated_map_test.go b/ads/authenticated_map_test.go similarity index 94% rename from ads/amap/authenticated_map_test.go rename to ads/authenticated_map_test.go index 83f89c376..c35a73b30 100644 --- a/ads/amap/authenticated_map_test.go +++ b/ads/authenticated_map_test.go @@ -1,11 +1,10 @@ -package amap_test +package ads import ( "testing" "github.com/stretchr/testify/require" - "github.com/iotaledger/hive.go/ads/amap" "github.com/iotaledger/hive.go/ierrors" "github.com/iotaledger/hive.go/kvstore/mapdb" "github.com/iotaledger/hive.go/lo" @@ -15,7 +14,7 @@ var ErrStopIteration = ierrors.New("stop") func TestMap(t *testing.T) { store := mapdb.NewMapDB() - newMap := amap.NewAuthenticatedMap(store, + newMap := newAuthenticatedMap(store, testKey.Bytes, testKeyFromBytes, testValue.Bytes, @@ -81,20 +80,20 @@ func TestMap(t *testing.T) { require.NoError(t, newMap.Commit()) // The root should be same if loading the same store to map - newMap1 := amap.NewAuthenticatedMap(store, + newMap1 := newAuthenticatedMap(store, testKey.Bytes, testKeyFromBytes, testValue.Bytes, testValueFromBytes, ) - + require.True(t, newMap.WasRestoredFromStorage()) require.EqualValues(t, newMap.Root(), newMap1.Root()) } func TestStreamMap(t *testing.T) { store := mapdb.NewMapDB() - newMap := amap.NewAuthenticatedMap[testKey, testValue](store, + newMap := newAuthenticatedMap[testKey, testValue](store, testKey.Bytes, testKeyFromBytes, testValue.Bytes, diff --git a/ads/authenticated_set.go b/ads/authenticated_set.go new file mode 100644 index 000000000..b85ba678e --- /dev/null +++ b/ads/authenticated_set.go @@ -0,0 +1,30 @@ +package ads + +import ( + "github.com/iotaledger/hive.go/ds/types" + "github.com/iotaledger/hive.go/kvstore" +) + +// Set is a sparse merkle tree based set. +type authenticatedSet[K any] struct { + *authenticatedMap[K, types.Empty] +} + +// NewAuthenticatedSet creates a new sparse merkle tree based set. +func newAuthenticatedSet[K any](store kvstore.KVStore, kToBytes kvstore.ObjectToBytes[K], bytesToK kvstore.BytesToObject[K]) *authenticatedSet[K] { + return &authenticatedSet[K]{ + authenticatedMap: newAuthenticatedMap(store, kToBytes, bytesToK, types.Empty.Bytes, types.EmptyFromBytes), + } +} + +// Add adds the key to the set. +func (s *authenticatedSet[K]) Add(key K) error { + return s.Set(key, types.Void) +} + +// Stream iterates over the set and calls the callback for each element. +func (s *authenticatedSet[K]) Stream(callback func(key K) error) error { + return s.authenticatedMap.Stream(func(key K, _ types.Empty) error { + return callback(key) + }) +} diff --git a/ads/aset/authenticated_set_test.go b/ads/authenticated_set_test.go similarity index 74% rename from ads/aset/authenticated_set_test.go rename to ads/authenticated_set_test.go index 414b03454..98481a5fc 100644 --- a/ads/aset/authenticated_set_test.go +++ b/ads/authenticated_set_test.go @@ -1,21 +1,20 @@ -package aset_test +package ads import ( "testing" "github.com/stretchr/testify/require" - "github.com/iotaledger/hive.go/ads/aset" "github.com/iotaledger/hive.go/ierrors" "github.com/iotaledger/hive.go/kvstore/mapdb" "github.com/iotaledger/hive.go/lo" ) -var ErrStopIteration = ierrors.New("stop") +var errStopIteration = ierrors.New("stop") func TestSet(t *testing.T) { store := mapdb.NewMapDB() - newSet := aset.NewAuthenticatedSet( + newSet := newAuthenticatedSet( store, testKey.Bytes, testKeyFromBytes, @@ -50,7 +49,7 @@ func TestSet(t *testing.T) { require.NotEqualValues(t, root, root1) // new set from old store, make sure the root is correct - newSet1 := aset.NewAuthenticatedSet(store, + newSet1 := newAuthenticatedSet(store, testKey.Bytes, testKeyFromBytes, ) @@ -59,7 +58,7 @@ func TestSet(t *testing.T) { func TestStreamSet(t *testing.T) { store := mapdb.NewMapDB() - newSet := aset.NewAuthenticatedSet(store, + newSet := newAuthenticatedSet(store, testKey.Bytes, testKeyFromBytes, ) @@ -90,27 +89,3 @@ func TestStreamSet(t *testing.T) { require.ErrorIs(t, err, ErrStopIteration) require.Equal(t, 1, len(firstSeen)) } - -type testKey [1]byte - -func (t testKey) Bytes() ([]byte, error) { - return t[:], nil -} - -func testKeyFromBytes(b []byte) (testKey, int, error) { - return testKey(b), 1, nil -} - -type testValue []byte - -func testValueFromString(s string) testValue { - return testValue(s) -} - -func (t testValue) Bytes() ([]byte, error) { - return t[:], nil -} - -func testValueFromBytes(b []byte) (testValue, int, error) { - return b, len(b), nil -}