From 7a643079013394a2e17c0c2401e21f3b918c97c9 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Sun, 3 Jul 2022 12:37:37 -0400 Subject: [PATCH] refactor: use time.Duration instead of uint/float --- rln/rln.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++-- rln/rln_test.go | 11 +--------- rln/types.go | 11 +++++----- 3 files changed, 60 insertions(+), 18 deletions(-) diff --git a/rln/rln.go b/rln/rln.go index fd8cabd..e2841fa 100644 --- a/rln/rln.go +++ b/rln/rln.go @@ -220,8 +220,9 @@ func (r *RLN) InsertMember(idComm IDCommitment) bool { return bool(res) } -// index is the position of the id commitment key to be deleted from the tree -// the deleted id commitment key is replaced with a zero leaf +// DeleteMember removes an IDCommitment key from the tree. The index +// parameter is the position of the id commitment key to be deleted from the tree. +// The deleted id commitment key is replaced with a zero leaf func (r *RLN) DeleteMember(index MembershipIndex) bool { deletionSuccess := bool(C.delete_member(r.ptr, C.ulong(index))) return deletionSuccess @@ -266,3 +267,54 @@ func ToRLNSignal(wakuMessage *pb.WakuMessage) []byte { contentTopicBytes := []byte(wakuMessage.ContentTopic) return append(wakuMessage.Payload, contentTopicBytes...) } + +// CalcMerkleRoot returns the root of the Merkle tree that is computed from the supplied list +func CalcMerkleRoot(list []IDCommitment) (MerkleNode, error) { + rln, err := NewRLN() + if err != nil { + return MerkleNode{}, err + } + + // create a Merkle tree + for _, c := range list { + if !rln.InsertMember(c) { + return MerkleNode{}, errors.New("could not add member") + } + } + + return rln.GetMerkleRoot() +} + +// CreateMembershipList produces a list of membership key pairs and also returns the root of a Merkle tree constructed +// out of the identity commitment keys of the generated list. The output of this function is used to initialize a static +// group keys (to test waku-rln-relay in the off-chain mode) +func CreateMembershipList(n int) ([]MembershipKeyPair, MerkleNode, error) { + // initialize a Merkle tree + rln, err := NewRLN() + if err != nil { + return nil, MerkleNode{}, err + } + + var output []MembershipKeyPair + for i := 0; i < n; i++ { + // generate a keypair + keypair, err := rln.MembershipKeyGen() + if err != nil { + return nil, MerkleNode{}, err + } + + output = append(output, *keypair) + + // insert the key to the Merkle tree + if !rln.InsertMember(keypair.IDCommitment) { + return nil, MerkleNode{}, errors.New("could not insert member") + } + } + + root, err := rln.GetMerkleRoot() + if err != nil { + return nil, MerkleNode{}, err + } + + return output, root, nil +} diff --git a/rln/rln_test.go b/rln/rln_test.go index 668c7eb..bc70e84 100644 --- a/rln/rln_test.go +++ b/rln/rln_test.go @@ -17,15 +17,6 @@ type WakuRLNRelaySuite struct { suite.Suite } -// SetupTest is used here for reinitializing the mock before every -// test function to avoid faulty execution. -func (s *WakuRLNRelaySuite) SetupTest() { -} - -func (s *WakuRLNRelaySuite) TearDownTest() { - // -} - func (s *WakuRLNRelaySuite) TestMembershipKeyGen() { rln, err := NewRLNWithDepth(32) s.NoError(err) @@ -127,7 +118,7 @@ func (s *WakuRLNRelaySuite) TestHash() { func (s *WakuRLNRelaySuite) TestCreateListMembershipKeysAndCreateMerkleTreeFromList() { groupSize := 100 - list, root, err := createMembershipList(groupSize) + list, root, err := CreateMembershipList(groupSize) s.NoError(err) s.Len(list, groupSize) s.Len(root, HASH_HEX_SIZE) // check the size of the calculated tree root diff --git a/rln/types.go b/rln/types.go index 9d81471..86e9b4b 100644 --- a/rln/types.go +++ b/rln/types.go @@ -107,8 +107,8 @@ const ETH_CLIENT = "ws://localhost:8540/" // HASH_BIT_SIZE is the size of poseidon hash output in bits const HASH_BIT_SIZE = 256 -// HASH_HEX_SIZE is the size of poseidon hash output as the number hex digits -const HASH_HEX_SIZE = int(HASH_BIT_SIZE / 4) +// HASH_HEX_SIZE is the size of poseidon hash output as the number of bytes +const HASH_HEX_SIZE = int(HASH_BIT_SIZE / 8) // temporary variables to test waku-rln-relay performance in the static group mode @@ -232,11 +232,11 @@ const STATIC_GROUP_MERKLE_ROOT = "a1877a553eff12e1b21632a0545a916a5c5b8060ad7cc6 // the rln-relay epoch length in seconds // TODO: change data type -const EPOCH_UNIT_SECONDS = float64(10) +const EPOCH_UNIT_SECONDS = time.Second * 10 // the maximum clock difference between peers in seconds // TODO: change data type -const MAX_CLOCK_GAP_SECONDS = 20.0 +const MAX_CLOCK_GAP_SECONDS = 20 * time.Second // maximum allowed gap between the epochs of messages' RateLimitProofs const MAX_EPOCH_GAP = int64(MAX_CLOCK_GAP_SECONDS / EPOCH_UNIT_SECONDS) @@ -261,8 +261,7 @@ func (e Epoch) Uint64() uint64 { // CalcEpoch gets time `t` as `float64` with subseconds resolution in the fractional part // and returns its corresponding rln `Epoch` value -// TODO: change data type -func CalcEpoch(t float64) Epoch { +func CalcEpoch(t time.Duration) Epoch { e := uint64(t / EPOCH_UNIT_SECONDS) return ToEpoch(e) }