2023-08-23 14:45:30 +00:00
|
|
|
package dynamic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-09-01 15:04:49 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/rln/group_manager"
|
2023-08-23 14:45:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMetadata(t *testing.T) {
|
|
|
|
|
|
|
|
metadata := &RLNMetadata{
|
|
|
|
LastProcessedBlock: 128,
|
|
|
|
ChainID: big.NewInt(1155511),
|
|
|
|
ContractAddress: common.HexToAddress("0x9c09146844c1326c2dbc41c451766c7138f88155"),
|
2023-09-01 15:04:49 +00:00
|
|
|
ValidRootsPerBlock: []group_manager.RootsPerBlock{{Root: [32]byte{1}, BlockNumber: 100}, {Root: [32]byte{2}, BlockNumber: 200}},
|
2023-08-23 14:45:30 +00:00
|
|
|
}
|
|
|
|
|
2024-01-03 15:39:59 +00:00
|
|
|
serializedMetadata, err := metadata.Serialize()
|
|
|
|
require.NoError(t, err)
|
2023-08-23 14:45:30 +00:00
|
|
|
unserializedMetadata, err := DeserializeMetadata(serializedMetadata)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, metadata.ChainID.Uint64(), unserializedMetadata.ChainID.Uint64())
|
|
|
|
require.Equal(t, metadata.LastProcessedBlock, unserializedMetadata.LastProcessedBlock)
|
|
|
|
require.Equal(t, metadata.ContractAddress.Hex(), unserializedMetadata.ContractAddress.Hex())
|
2023-09-01 15:04:49 +00:00
|
|
|
require.Len(t, unserializedMetadata.ValidRootsPerBlock, len(metadata.ValidRootsPerBlock))
|
|
|
|
require.Equal(t, metadata.ValidRootsPerBlock[0].BlockNumber, unserializedMetadata.ValidRootsPerBlock[0].BlockNumber)
|
|
|
|
require.Equal(t, metadata.ValidRootsPerBlock[0].Root, unserializedMetadata.ValidRootsPerBlock[0].Root)
|
|
|
|
require.Equal(t, metadata.ValidRootsPerBlock[1].BlockNumber, unserializedMetadata.ValidRootsPerBlock[1].BlockNumber)
|
|
|
|
require.Equal(t, metadata.ValidRootsPerBlock[1].Root, unserializedMetadata.ValidRootsPerBlock[1].Root)
|
2023-08-23 14:45:30 +00:00
|
|
|
|
2023-09-01 15:04:49 +00:00
|
|
|
// Handle cases where the chainId is not specified (for some reason?) or no valid roots were specified
|
2023-08-23 14:45:30 +00:00
|
|
|
metadata.ChainID = nil
|
2023-09-01 15:04:49 +00:00
|
|
|
metadata.ValidRootsPerBlock = nil
|
2024-01-03 15:39:59 +00:00
|
|
|
_, err = metadata.Serialize()
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
metadata.ChainID = big.NewInt(1)
|
|
|
|
serializedMetadata, err = metadata.Serialize()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-08-23 14:45:30 +00:00
|
|
|
unserializedMetadata, err = DeserializeMetadata(serializedMetadata)
|
|
|
|
require.NoError(t, err)
|
2024-01-03 15:39:59 +00:00
|
|
|
require.Equal(t, uint64(1), unserializedMetadata.ChainID.Uint64())
|
2023-08-23 14:45:30 +00:00
|
|
|
}
|