Skip to content
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

Simplify serialization #596

Merged
merged 22 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
bba2f96
Change serializer to serialize time.Time with second instead of nanos…
jonastheis Oct 18, 2023
cff8c81
Refactor and streamline API of serialization methods
jonastheis Oct 23, 2023
c58d131
Add ByteBuffer (WriteSeeker) and ByteReader (ReadSeeker) for easy in-…
jonastheis Oct 23, 2023
d783cb6
Rename and slight refactor
jonastheis Oct 24, 2023
8eefc5e
Revert "Change serializer to serialize time.Time with second instead …
jonastheis Oct 24, 2023
d0361f2
Remove generic model wrapper
jonastheis Oct 24, 2023
edf2b69
Delete marshalutil
jonastheis Oct 24, 2023
def6406
Remove typedkey and generictype wrapper
jonastheis Oct 25, 2023
0a86023
Add typeutils for basic types to easily use with our serialization in…
jonastheis Oct 25, 2023
5520a10
Fix ads data structures after removing typedkey
jonastheis Oct 25, 2023
dac26be
Merge remote-tracking branch 'origin/develop' into feat/simplify-seri…
jonastheis Oct 27, 2023
f64d9d9
Get rid of FromByter and MarshalablePtr interface
jonastheis Oct 27, 2023
ba12012
Extend functionality of stream package to support more use cases
jonastheis Oct 27, 2023
a5ebefe
Rename Write and Read methods, introduce `ReadBytes` to enable readin…
jonastheis Oct 31, 2023
6a0b43d
Merge remote-tracking branch 'origin/develop' into feat/simplify-seri…
jonastheis Oct 31, 2023
9711af4
Rename PeekCollection to Peek
jonastheis Nov 2, 2023
deb6d44
Remove unused array sizes and slices of arrays
jonastheis Nov 6, 2023
e6c1696
Please doggo
jonastheis Nov 6, 2023
7f3042d
Update go.mods
jonastheis Nov 6, 2023
2659fb1
Add tests for stream package
jonastheis Nov 6, 2023
5731e50
Address review comments
jonastheis Nov 8, 2023
98e0fa3
Update go.mods
jonastheis Nov 8, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions ads/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ module github.com/iotaledger/hive.go/ads
go 1.21

require (
github.com/iotaledger/hive.go/ds v0.0.0-20231019081410-32f61b05bebe
github.com/iotaledger/hive.go/ierrors v0.0.0-20231019081410-32f61b05bebe
github.com/iotaledger/hive.go/kvstore v0.0.0-20231019081410-32f61b05bebe
github.com/iotaledger/hive.go/lo v0.0.0-20231019081410-32f61b05bebe
github.com/iotaledger/hive.go/ds v0.0.0-20231108044237-5731e50d3660
github.com/iotaledger/hive.go/ierrors v0.0.0-20231108044237-5731e50d3660
github.com/iotaledger/hive.go/kvstore v0.0.0-20231108044237-5731e50d3660
github.com/iotaledger/hive.go/lo v0.0.0-20231108044237-5731e50d3660
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231108044237-5731e50d3660
github.com/pokt-network/smt v0.6.1
github.com/stretchr/testify v1.8.4
)
Expand All @@ -15,10 +16,9 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/ethereum/go-ethereum v1.13.4 // indirect
github.com/iancoleman/orderedmap v0.3.0 // indirect
github.com/iotaledger/hive.go/constraints v0.0.0-20231019081410-32f61b05bebe // indirect
github.com/iotaledger/hive.go/runtime v0.0.0-20231019081410-32f61b05bebe // indirect
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231019081410-32f61b05bebe // indirect
github.com/iotaledger/hive.go/stringify v0.0.0-20231019081410-32f61b05bebe // indirect
github.com/iotaledger/hive.go/constraints v0.0.0-20231108044237-5731e50d3660 // indirect
github.com/iotaledger/hive.go/runtime v0.0.0-20231108044237-5731e50d3660 // indirect
github.com/iotaledger/hive.go/stringify v0.0.0-20231108044237-5731e50d3660 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/petermattis/goid v0.0.0-20230904192822-1876fd5063bc // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
32 changes: 16 additions & 16 deletions ads/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ github.com/holiman/uint256 v1.2.3 h1:K8UWO1HUJpRMXBxbmaY1Y8IAMZC/RsKB+ArEnnK4l5o
github.com/holiman/uint256 v1.2.3/go.mod h1:SC8Ryt4n+UBbPbIBKaG9zbbDlp4jOru9xFZmPzLUTxw=
github.com/iancoleman/orderedmap v0.3.0 h1:5cbR2grmZR/DiVt+VJopEhtVs9YGInGIxAoMJn+Ichc=
github.com/iancoleman/orderedmap v0.3.0/go.mod h1:XuLcCUkdL5owUCQeF2Ue9uuw1EptkJDkXXS7VoV7XGE=
github.com/iotaledger/hive.go/constraints v0.0.0-20231019081410-32f61b05bebe h1:pxdm32mBjIuTcgdHcsAfAkLW2NfU1lcE7YwKTqxUi34=
github.com/iotaledger/hive.go/constraints v0.0.0-20231019081410-32f61b05bebe/go.mod h1:dOBOM2s4se3HcWefPe8sQLUalGXJ8yVXw58oK8jke3s=
github.com/iotaledger/hive.go/ds v0.0.0-20231019081410-32f61b05bebe h1:d5l51OWdWowRk1ofeIaHJ5PIfQPkbEQJx/GNTVBrFTc=
github.com/iotaledger/hive.go/ds v0.0.0-20231019081410-32f61b05bebe/go.mod h1:slaS6myKh0HoE5pyjYH9nHGJ7+toGadh81NPuyZz+Ys=
github.com/iotaledger/hive.go/ierrors v0.0.0-20231019081410-32f61b05bebe h1:Zk5RKuwN9IqAz346DRs+oJxrMOlQ2qc13WZWWq4alpE=
github.com/iotaledger/hive.go/ierrors v0.0.0-20231019081410-32f61b05bebe/go.mod h1:HcE8B5lP96enc/OALTb2/rIIi+yOLouRoHOKRclKmC8=
github.com/iotaledger/hive.go/kvstore v0.0.0-20231019081410-32f61b05bebe h1:SAWe7GNMu/wd6EuFGTrv66z5/ID22Umv6hDgDlLEOT0=
github.com/iotaledger/hive.go/kvstore v0.0.0-20231019081410-32f61b05bebe/go.mod h1:PkWqAmvPlUhpOXLs252QXWiFxZz0Vuy6aTnrJcdo/nY=
github.com/iotaledger/hive.go/lo v0.0.0-20231019081410-32f61b05bebe h1:s8QbwSyRWlxFw6vI3WKIE3v+FEG/HVPJcXeq4bevUSM=
github.com/iotaledger/hive.go/lo v0.0.0-20231019081410-32f61b05bebe/go.mod h1:AZoZhT1qXgx1fZZOygYE1IUSFW2ldQuT+JaguMsYzXE=
github.com/iotaledger/hive.go/runtime v0.0.0-20231019081410-32f61b05bebe h1:yrP/uUWCW5u8Yum5IR/yjYVtR8pU6FpB7/Oi/a87ixc=
github.com/iotaledger/hive.go/runtime v0.0.0-20231019081410-32f61b05bebe/go.mod h1:FIE6S23rLrMYTcc4KCesXhjlSRWCeG9Fz42R1d6zxME=
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231019081410-32f61b05bebe h1:IzfUWQnlKYNlk4UoSa2uq65tSErEdF/ZjfgGmuHbjKg=
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231019081410-32f61b05bebe/go.mod h1:vFpOT7yWKLITVCz8KdZ7gNjYMYdqFpKnAUNKwhabP7A=
github.com/iotaledger/hive.go/stringify v0.0.0-20231019081410-32f61b05bebe h1:LAfxRCUunAoOc0iOf9NcYCZDY7XjFQAH1/HVGH7gkus=
github.com/iotaledger/hive.go/stringify v0.0.0-20231019081410-32f61b05bebe/go.mod h1:FTo/UWzNYgnQ082GI9QVM9HFDERqf9rw9RivNpqrnTs=
github.com/iotaledger/hive.go/constraints v0.0.0-20231108044237-5731e50d3660 h1:x176vs7HnHwEaE/9aU8N/tGW+2dKoD+xFTXQmvTtZlc=
github.com/iotaledger/hive.go/constraints v0.0.0-20231108044237-5731e50d3660/go.mod h1:dOBOM2s4se3HcWefPe8sQLUalGXJ8yVXw58oK8jke3s=
github.com/iotaledger/hive.go/ds v0.0.0-20231108044237-5731e50d3660 h1:FPvlnahvCscsTyTPbU7DVTWuntfCGb4hgOHEyg75zQc=
github.com/iotaledger/hive.go/ds v0.0.0-20231108044237-5731e50d3660/go.mod h1:RnYZNMRIXKt/SXVsFA18uUBNDZMQm0h5BykMXjv8/8A=
github.com/iotaledger/hive.go/ierrors v0.0.0-20231108044237-5731e50d3660 h1:v6Edaosj7yifZCLrKRJb0QmCNAluG39VTVScDwGyaRE=
github.com/iotaledger/hive.go/ierrors v0.0.0-20231108044237-5731e50d3660/go.mod h1:HcE8B5lP96enc/OALTb2/rIIi+yOLouRoHOKRclKmC8=
github.com/iotaledger/hive.go/kvstore v0.0.0-20231108044237-5731e50d3660 h1:B69S6Vr1Lnns3b/hyZf20cmgIZs7HAkmLObAB06Wlxg=
github.com/iotaledger/hive.go/kvstore v0.0.0-20231108044237-5731e50d3660/go.mod h1:gQWd99xA2Wl4DpCqHIeieNUAKbRJDZo1VNnswAbibyA=
github.com/iotaledger/hive.go/lo v0.0.0-20231108044237-5731e50d3660 h1:uLsdYCIByMT8I9008iuOEyj5JWZFOJuPctdTSi55A3E=
github.com/iotaledger/hive.go/lo v0.0.0-20231108044237-5731e50d3660/go.mod h1:lFKoK3stQVscy4k6+Cqd3Chq6u65JoTRhfhzJsmKyY8=
github.com/iotaledger/hive.go/runtime v0.0.0-20231108044237-5731e50d3660 h1:FGlELFpJCAhSVKJ/TlANoe9Bg4mafcBwXQfocY0jG6s=
github.com/iotaledger/hive.go/runtime v0.0.0-20231108044237-5731e50d3660/go.mod h1:6WKyYAkvdY08iW4/w30goNq0GaBTljT0MXXilnQ33wk=
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231108044237-5731e50d3660 h1:Ci3JeZw00scHCyYHSbUHRof67KsA6NKAI/YO31vgqTA=
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231108044237-5731e50d3660/go.mod h1:fW/lA6n0C/jf9B9NMX46eJXsJTU8Nzs+9oBWMTNzPzE=
github.com/iotaledger/hive.go/stringify v0.0.0-20231108044237-5731e50d3660 h1:2ASLmmmTXl2st0y23n9ug95NQ7ZOX7J+kJLGHfYobQ0=
github.com/iotaledger/hive.go/stringify v0.0.0-20231108044237-5731e50d3660/go.mod h1:FTo/UWzNYgnQ082GI9QVM9HFDERqf9rw9RivNpqrnTs=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down
12 changes: 10 additions & 2 deletions ads/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ type Map[IdentifierType types.IdentifierType, K, V any] interface {
}

// NewMap creates a new AuthenticatedMap.
func NewMap[IdentifierType types.IdentifierType, K, V any](store kvstore.KVStore, keyToBytes kvstore.ObjectToBytes[K], bytesToKey kvstore.BytesToObject[K], valueToBytes kvstore.ObjectToBytes[V], bytesToValue kvstore.BytesToObject[V]) Map[IdentifierType, K, V] {
return newAuthenticatedMap[IdentifierType](store, keyToBytes, bytesToKey, valueToBytes, bytesToValue)
func NewMap[IdentifierType types.IdentifierType, K, V any](
store kvstore.KVStore,
identifierToBytes kvstore.ObjectToBytes[IdentifierType],
bytesToIdentifier kvstore.BytesToObject[IdentifierType],
keyToBytes kvstore.ObjectToBytes[K],
bytesToKey kvstore.BytesToObject[K],
valueToBytes kvstore.ObjectToBytes[V],
bytesToValue kvstore.BytesToObject[V],
) Map[IdentifierType, K, V] {
return newAuthenticatedMap[IdentifierType](store, identifierToBytes, bytesToIdentifier, keyToBytes, bytesToKey, valueToBytes, bytesToValue)
}
55 changes: 40 additions & 15 deletions ads/map_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/iotaledger/hive.go/ds/types"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/kvstore"
"github.com/iotaledger/hive.go/kvstore/typedkey"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/serializer/v2/typeutils"
)

const (
Expand All @@ -24,8 +24,8 @@ const (
type authenticatedMap[IdentifierType types.IdentifierType, K, V any] struct {
rawKeysStore *kvstore.TypedStore[K, types.Empty]
tree *smt.SMT
size *typedkey.Number[uint64]
root *typedkey.Bytes
size *kvstore.TypedValue[uint64]
root *kvstore.TypedValue[IdentifierType]
mutex sync.RWMutex

keyToBytes kvstore.ObjectToBytes[K]
Expand All @@ -36,23 +36,25 @@ type authenticatedMap[IdentifierType types.IdentifierType, K, V any] struct {
// NewAuthenticatedMap creates a new authenticated map.
func newAuthenticatedMap[IdentifierType types.IdentifierType, K, V any](
store kvstore.KVStore,
identifierToBytes kvstore.ObjectToBytes[IdentifierType],
bytesToIdentifier kvstore.BytesToObject[IdentifierType],
keyToBytes kvstore.ObjectToBytes[K],
bytesToKey kvstore.BytesToObject[K],
valueToBytes kvstore.ObjectToBytes[V],
bytesToValue kvstore.BytesToObject[V],
) *authenticatedMap[IdentifierType, K, V] {
newMap := &authenticatedMap[IdentifierType, K, V]{
rawKeysStore: kvstore.NewTypedStore(lo.PanicOnErr(store.WithExtendedRealm([]byte{prefixRawKeysStorage})), keyToBytes, bytesToKey, types.Empty.Bytes, types.EmptyFromBytes),
size: typedkey.NewNumber[uint64](store, prefixSizeKey),
root: typedkey.NewBytes(store, prefixRootKey),
size: kvstore.NewTypedValue(store, []byte{prefixSizeKey}, typeutils.Uint64ToBytes, typeutils.Uint64FromBytes),
root: kvstore.NewTypedValue(store, []byte{prefixRootKey}, identifierToBytes, bytesToIdentifier),

keyToBytes: keyToBytes,
valueToBytes: valueToBytes,
bytesToValue: bytesToValue,
}

if root := newMap.root.Get(); len(root) != 0 {
newMap.tree = smt.ImportSparseMerkleTree(lo.PanicOnErr(store.WithExtendedRealm([]byte{prefixTreeStorage})), sha256.New(), root, smt.WithValueHasher(nil))
if root, err := newMap.root.Get(); err == nil {
newMap.tree = smt.ImportSparseMerkleTree(lo.PanicOnErr(store.WithExtendedRealm([]byte{prefixTreeStorage})), sha256.New(), root[:], smt.WithValueHasher(nil))
} else {
newMap.tree = smt.NewSparseMerkleTree(lo.PanicOnErr(store.WithExtendedRealm([]byte{prefixTreeStorage})), sha256.New(), smt.WithValueHasher(nil))
}
Expand All @@ -62,17 +64,16 @@ func newAuthenticatedMap[IdentifierType types.IdentifierType, K, V any](

// WasRestoredFromStorage returns true if the map has been restored from storage.
func (m *authenticatedMap[IdentifierType, K, V]) WasRestoredFromStorage() bool {
return len(m.root.Get()) != 0
_, err := m.root.Get()
return !ierrors.Is(err, kvstore.ErrKeyNotFound)
}

// Root returns the root of the state sparse merkle tree at the latest committed slot.
func (m *authenticatedMap[IdentifierType, K, V]) Root() (root IdentifierType) {
m.mutex.Lock()
defer m.mutex.Unlock()

copy(root[:], m.tree.Root())

return
return IdentifierType(m.tree.Root())
}

// Set sets the output to unspent outputs set.
Expand Down Expand Up @@ -104,7 +105,9 @@ func (m *authenticatedMap[IdentifierType, K, V]) Set(key K, value V) error {
}

if !has {
m.size.Inc()
if err := m.addSize(1); err != nil {
return ierrors.Wrap(err, "failed to increase size")
}
}

return nil
Expand All @@ -115,15 +118,22 @@ func (m *authenticatedMap[IdentifierType, K, V]) Size() int {
m.mutex.RLock()
defer m.mutex.RUnlock()

return int(m.size.Get())
size, err := m.size.Get()
if err != nil {
return 0
}

return int(size)
}

// Commit persists the current state of the map to the storage.
func (m *authenticatedMap[IdentifierType, K, V]) Commit() error {
m.mutex.Lock()
defer m.mutex.Unlock()

m.root.Set(m.tree.Root())
if err := m.root.Set(IdentifierType(m.tree.Root())); err != nil {
return ierrors.Wrap(err, "failed to set root")
}

return m.tree.Commit()
}
Expand Down Expand Up @@ -156,7 +166,9 @@ func (m *authenticatedMap[IdentifierType, K, V]) Delete(key K) (deleted bool, er
}

if has {
m.size.Dec()
if err := m.addSize(-1); err != nil {
return false, ierrors.Wrap(err, "failed to decrease size")
}
}

return true, nil
Expand Down Expand Up @@ -257,3 +269,16 @@ func (m *authenticatedMap[IdentifierType, K, V]) has(keyBytes []byte) (has bool,

return value != nil, nil
}

func (m *authenticatedMap[IdentifierType, K, V]) addSize(delta int) error {
size, err := m.size.Get()
if err != nil && !ierrors.Is(err, kvstore.ErrKeyNotFound) {
alexsporn marked this conversation as resolved.
Show resolved Hide resolved
return ierrors.Wrap(err, "failed to get size")
}

if err := m.size.Set(uint64(int(size) + delta)); err != nil {
return ierrors.Wrap(err, "failed to set size")
}

return nil
}
13 changes: 10 additions & 3 deletions ads/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import (
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/kvstore/mapdb"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/serializer/v2/typeutils"
)

var ErrStopIteration = ierrors.New("stop")

func TestMap(t *testing.T) {
store := mapdb.NewMapDB()
newMap := newAuthenticatedMap[[32]byte](store,
typeutils.ByteArray32ToBytes,
typeutils.ByteArray32FromBytes,
testKey.Bytes,
testKeyFromBytes,
testValue.Bytes,
Expand All @@ -27,7 +30,7 @@ func TestMap(t *testing.T) {
require.False(t, newMap.WasRestoredFromStorage())

for i, k := range keys {
newMap.Set(k, values[i])
require.NoError(t, newMap.Set(k, values[i]))
}

for i, k := range keys {
Expand All @@ -53,7 +56,7 @@ func TestMap(t *testing.T) {

// overwrite the value of keys[0]
newValue := testValueFromString("test")
newMap.Set(keys[0], newValue)
require.NoError(t, newMap.Set(keys[0], newValue))
gotValue, exists, err = newMap.Get(keys[0])
require.NoError(t, err)
require.True(t, exists)
Expand Down Expand Up @@ -81,6 +84,8 @@ func TestMap(t *testing.T) {

// The root should be same if loading the same store to map
newMap1 := newAuthenticatedMap[[32]byte](store,
typeutils.ByteArray32ToBytes,
typeutils.ByteArray32FromBytes,
testKey.Bytes,
testKeyFromBytes,
testValue.Bytes,
Expand All @@ -94,6 +99,8 @@ func TestMap(t *testing.T) {
func TestStreamMap(t *testing.T) {
store := mapdb.NewMapDB()
newMap := newAuthenticatedMap[[32]byte, testKey, testValue](store,
typeutils.ByteArray32ToBytes,
typeutils.ByteArray32FromBytes,
testKey.Bytes,
testKeyFromBytes,
testValue.Bytes,
Expand All @@ -105,7 +112,7 @@ func TestStreamMap(t *testing.T) {
testKey([]byte{'c'}): testValueFromString("test value 2"),
}
for k, v := range kvMap {
newMap.Set(k, v)
require.NoError(t, newMap.Set(k, v))
}

seen := make(map[testKey]testValue)
Expand Down
10 changes: 8 additions & 2 deletions ads/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ type Set[IdentifierType types.IdentifierType, K any] interface {
}

// NewSet creates a new sparse merkle tree based set.
func NewSet[IdentifierType types.IdentifierType, K any](store kvstore.KVStore, keyToBytes kvstore.ObjectToBytes[K], bytesToKey kvstore.BytesToObject[K]) Set[IdentifierType, K] {
return newAuthenticatedSet[IdentifierType](store, keyToBytes, bytesToKey)
func NewSet[IdentifierType types.IdentifierType, K any](
store kvstore.KVStore,
identifierToBytes kvstore.ObjectToBytes[IdentifierType],
bytesToIdentifier kvstore.BytesToObject[IdentifierType],
keyToBytes kvstore.ObjectToBytes[K],
bytesToKey kvstore.BytesToObject[K],
) Set[IdentifierType, K] {
return newAuthenticatedSet[IdentifierType](store, identifierToBytes, bytesToIdentifier, keyToBytes, bytesToKey)
}
10 changes: 8 additions & 2 deletions ads/set_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ type authenticatedSet[IdentifierType types.IdentifierType, K any] struct {
}

// NewAuthenticatedSet creates a new sparse merkle tree based set.
func newAuthenticatedSet[IdentifierType types.IdentifierType, K any](store kvstore.KVStore, keyToBytes kvstore.ObjectToBytes[K], bytesToKey kvstore.BytesToObject[K]) *authenticatedSet[IdentifierType, K] {
func newAuthenticatedSet[IdentifierType types.IdentifierType, K any](
store kvstore.KVStore,
identifierToBytes kvstore.ObjectToBytes[IdentifierType],
bytesToIdentifier kvstore.BytesToObject[IdentifierType],
keyToBytes kvstore.ObjectToBytes[K],
bytesToKey kvstore.BytesToObject[K],
) Set[IdentifierType, K] {
return &authenticatedSet[IdentifierType, K]{
authenticatedMap: newAuthenticatedMap[IdentifierType](store, keyToBytes, bytesToKey, types.Empty.Bytes, types.EmptyFromBytes),
authenticatedMap: newAuthenticatedMap[IdentifierType](store, identifierToBytes, bytesToIdentifier, keyToBytes, bytesToKey, types.Empty.Bytes, types.EmptyFromBytes),
}
}

Expand Down
18 changes: 11 additions & 7 deletions ads/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ import (

"github.com/stretchr/testify/require"

"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/kvstore/mapdb"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/serializer/v2/typeutils"
)

var errStopIteration = ierrors.New("stop")

func TestSet(t *testing.T) {
store := mapdb.NewMapDB()
newSet := newAuthenticatedSet[[32]byte](
store,
typeutils.ByteArray32ToBytes,
typeutils.ByteArray32FromBytes,
testKey.Bytes,
testKeyFromBytes,
)

key := testKey([]byte{'a'})
newSet.Add(key)
require.NoError(t, newSet.Add(key))
exist, err := newSet.Has(key)
require.NoError(t, err)
require.True(t, exist)

// add the same key again
newSet.Add(key)
require.NoError(t, newSet.Add(key))
exist, err = newSet.Has(key)
require.NoError(t, err)
require.True(t, exist)
Expand All @@ -50,6 +50,8 @@ func TestSet(t *testing.T) {

// new set from old store, make sure the root is correct
newSet1 := newAuthenticatedSet[[32]byte](store,
typeutils.ByteArray32ToBytes,
typeutils.ByteArray32FromBytes,
testKey.Bytes,
testKeyFromBytes,
)
Expand All @@ -59,14 +61,16 @@ func TestSet(t *testing.T) {
func TestStreamSet(t *testing.T) {
store := mapdb.NewMapDB()
newSet := newAuthenticatedSet[[32]byte](store,
typeutils.ByteArray32ToBytes,
typeutils.ByteArray32FromBytes,
testKey.Bytes,
testKeyFromBytes,
)

key1 := testKey([]byte{'b'})
key2 := testKey([]byte{'c'})
newSet.Add(key1)
newSet.Add(key2)
require.NoError(t, newSet.Add(key1))
require.NoError(t, newSet.Add(key2))
require.Equal(t, 2, newSet.Size())

seen := make(map[testKey]bool)
Expand Down
Loading
Loading