From a327e56377165f3d5deb3e1fdc4a86022484897d Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Wed, 3 Jan 2024 21:09:59 +0530 Subject: [PATCH] fix: return error while serializing rln metadata in case chainID is 0 (#985) --- .../protocol/rln/group_manager/dynamic/metadata.go | 11 +++++++---- .../rln/group_manager/dynamic/metadata_test.go | 14 ++++++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/waku/v2/protocol/rln/group_manager/dynamic/metadata.go b/waku/v2/protocol/rln/group_manager/dynamic/metadata.go index a430a810..f391fe3b 100644 --- a/waku/v2/protocol/rln/group_manager/dynamic/metadata.go +++ b/waku/v2/protocol/rln/group_manager/dynamic/metadata.go @@ -18,10 +18,10 @@ type RLNMetadata struct { } // Serialize converts a RLNMetadata into a binary format expected by zerokit's RLN -func (r RLNMetadata) Serialize() []byte { +func (r RLNMetadata) Serialize() ([]byte, error) { chainID := r.ChainID if chainID == nil { - chainID = big.NewInt(0) + return nil, errors.New("chain-id not specified and cannot be 0") } var result []byte @@ -34,7 +34,7 @@ func (r RLNMetadata) Serialize() []byte { result = binary.LittleEndian.AppendUint64(result, v.BlockNumber) } - return result + return result, nil } const lastProcessedBlockOffset = 0 @@ -76,6 +76,9 @@ func DeserializeMetadata(b []byte) (RLNMetadata, error) { // SetMetadata stores some metadata into the zerokit's RLN database func (gm *DynamicGroupManager) SetMetadata(meta RLNMetadata) error { - b := meta.Serialize() + b, err := meta.Serialize() + if err != nil { + return err + } return gm.rln.SetMetadata(b) } diff --git a/waku/v2/protocol/rln/group_manager/dynamic/metadata_test.go b/waku/v2/protocol/rln/group_manager/dynamic/metadata_test.go index 35c449e0..dad5eddc 100644 --- a/waku/v2/protocol/rln/group_manager/dynamic/metadata_test.go +++ b/waku/v2/protocol/rln/group_manager/dynamic/metadata_test.go @@ -18,8 +18,8 @@ func TestMetadata(t *testing.T) { ValidRootsPerBlock: []group_manager.RootsPerBlock{{Root: [32]byte{1}, BlockNumber: 100}, {Root: [32]byte{2}, BlockNumber: 200}}, } - serializedMetadata := metadata.Serialize() - + serializedMetadata, err := metadata.Serialize() + require.NoError(t, err) unserializedMetadata, err := DeserializeMetadata(serializedMetadata) require.NoError(t, err) require.Equal(t, metadata.ChainID.Uint64(), unserializedMetadata.ChainID.Uint64()) @@ -34,8 +34,14 @@ func TestMetadata(t *testing.T) { // Handle cases where the chainId is not specified (for some reason?) or no valid roots were specified metadata.ChainID = nil metadata.ValidRootsPerBlock = nil - serializedMetadata = metadata.Serialize() + _, err = metadata.Serialize() + require.Error(t, err) + + metadata.ChainID = big.NewInt(1) + serializedMetadata, err = metadata.Serialize() + require.NoError(t, err) + unserializedMetadata, err = DeserializeMetadata(serializedMetadata) require.NoError(t, err) - require.Equal(t, uint64(0), unserializedMetadata.ChainID.Uint64()) + require.Equal(t, uint64(1), unserializedMetadata.ChainID.Uint64()) }