refactor: use time.Duration instead of uint/float

This commit is contained in:
Richard Ramos 2022-07-03 12:37:37 -04:00
parent 7ffa648d9b
commit 7a64307901
No known key found for this signature in database
GPG Key ID: BD36D48BC9FFC88C
3 changed files with 60 additions and 18 deletions

View File

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

View File

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

View File

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