fix: return error while serializing rln metadata in case chainID is 0 (#985)

This commit is contained in:
Prem Chaitanya Prathi 2024-01-03 21:09:59 +05:30 committed by GitHub
parent 6141f94b40
commit a327e56377
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 8 deletions

View File

@ -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)
}

View File

@ -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())
}