nwaku/waku/v2/protocol/waku_rln_relay/waku_rln_relay_types.nim

72 lines
16 KiB
Nim
Raw Normal View History

2021-07-22 08:43:41 +00:00
{.push raises: [Defect].}
import
2021-07-22 08:43:41 +00:00
options, chronos, stint,
web3,
eth/keys
## Bn256 and RLN are Nim wrappers for the data types used in
## the rln library https://github.com/kilic/rln/blob/3bbec368a4adc68cd5f9bfae80b17e1bbb4ef373/src/ffi.rs
type Bn256* = pointer
type RLN*[E] = pointer
type IDKey* = array[32, byte]
type IDCommitment* = array[32, byte]
# represents a Merkle tree node which is the output of
# Poseidon hash function implemented by rln lib
type MerkleNode* = array[32,byte]
# Custom data types defined for waku rln relay -------------------------
type MembershipKeyPair* = object
# node's identity key (a secret key) which is selected randomly
idKey*: IDKey
# hash of node's identity key generated by
# Poseidon hash function implemented in rln lib
idCommitment*: IDCommitment
type WakuRLNRelay* = object
membershipKeyPair*: MembershipKeyPair
membershipIndex*: uint # index of peers in the Merkle tree
membershipContractAddress*: Address
ethClientAddress*: string
ethAccountAddress*: Address
# this field is required for signing transactions
# TODO may need to erase this ethAccountPrivateKey when is not used
# TODO may need to make ethAccountPrivateKey mandatory
ethAccountPrivateKey*: Option[PrivateKey]
rlnInstance*: RLN[Bn256]
type MembershipIndex* = uint
# inputs of the membership contract constructor
# TODO may be able to make these constants private and put them inside the waku_rln_relay_utils
const
Off-chain group construction and management (#718) * WIP * WIP: fixes a bug * adds test for static group formation * adds static group creation when rln-relay is enabled * adds createStatic group * wip: adds group formation to mount rlnrelay * adds createMembershipList utility function * adds doc strings and todos * cleans up the code and add comments * defaults createRLNInstance depth argument to 32 * renames Depth * distinguishes between onchain and offchain modes * updates index boundaries * updates log levels * updates docstring * updates log level of displayed membership keys * relocates a todo * activates all the tests * fixes some comments and todos * extracts some utils procs for better debugging * adds todo * moves calculateMerkleRoot and toMembersipKeyPairs to the rln utils * makes calls to the utils functions * adds unit test for createMembershipList * adds unittest for toMembershipKeyPairs and calcMerkleRoot * cleans up the code and fixes tree root value * reverts an unwanted change * minor * adds comments and cleans up the code * updates config message * adds more comments * fixes a minor value mismatch * edits the size of group * minor rewording * defines a const var for the group keys * replaces the sequence literal with the StaticGroupKeys const * converts var to let when applicable * replaces hardcoded value with well-defined constants * moves createMembershipList to the rln relay utils module * renames HashSize to HashHexSize * minor updates on the comments * reorganizes the consts * indicates that rlnRelayMemIndex is an experimental option * fixes a type conversion bug * revises the unittest of "mount waku rln-relay off-chain" * clarifies the use of index * updates a docstring * removes redundant constants and capitalize all of them * deletes the ETH_CLIENT const from the test file * renames a few vars for the sake of clarity * reorganizes unittest into blocks of execution, debug messages, and checks * adds more comments * more comments and clarifications * cleans up the tests * minor * adds a minor fix * replaces a var usage with let * fixes a bug
2021-09-17 17:31:25 +00:00
MEMBERSHIP_FEE* = 5.u256
# the current implementation of the rln lib only supports a circuit for Merkle tree with depth 32
MERKLE_TREE_DEPTH* = 32
# TODO the ETH_CLIENT should be an input to the rln-relay, though hardcoded for now
# the current address is the address of ganache-cli when run locally
ETH_CLIENT* = "ws://localhost:8540/"
const
# the size of poseidon hash output in bits
HASH_BIT_SIZE* = 256
# the size of poseidon hash output as the number hex digits
HASH_HEX_SIZE* = int(HASH_BIT_SIZE/4)
# temporary variables to test waku-rln-relay performance in the static group mode
const
STATIC_GROUP_SIZE* = 100
# STATIC_GROUP_KEYS is a static list of 100 membership keys in the form of (identity key, identity commitment)
# keys are created locally, using createMembershipList proc from waku_rln_relay_utils module, and the results are hardcoded in here
# this list is temporary and is created to test the performance of waku-rln-relay for the static groups
# in the later versions, this static hardcoded group will be replaced with a dynamic one
STATIC_GROUP_KEYS* = @[("e9a4d05b1f539d65c59015a079ee89aabeafbcfc9734342d9559f81601e85417", "b74d3a5b3200ab1126fbee393496f33da497d4d9a7c56693f44d6155c0c34e13"), ("27b2bfc25257e53819beaf36ce1070007e04e7aad2e440a1f1fc066f59a61123", "522ce51aff96041e79a8476f508fb9661f146f189e288f83cb4837517cfc0127"), ("66392eaae6674267c55fe393d39443ba90317a709d6e8f92a9f3e4abc18eff1d", "e3dc235e48c1811943fc249fecd0f1415a50ebe839ccefb0bd820a76fb77ba2a"), ("e7462eebb81405230db8014b052d65fe7b269c3870e40b12cf64668ed6c2d40e", "727df0965e34144ea637be18208cc81e57e423010b3159c20f0ccff45c42212a"), ("1ad8528b4c7075013a2d6561a02517b0482c0733dc018ac68774db857deb5004", "5df3a77577135784da2a9ee78f5026092b7d6bb9e6e95882d6cb172c0cb62208"), ("aa0ff53bfc50861f871d94df18c3ac0b97f44ceb13436b33490cec5f6ce8e700", "30421d05b905aeaec0473ba29ace034bf73c406866d7dc23007eb9c34a596827"), ("0448c0a6ed57b177c4c45de478b58d29f24e7ea842814305443e87188ae24324", "5ea1a704d8972af5a028367c8e3fb48ee61a603c6ea3a4c9247b0f611a6ab002"), ("4b6a6edbdd11e69befe3f4a3c976baca320c4bcb188f129b603ebb198f663000", "451d6185e8ad2c8873f034683b9caca43ea7ceb1b839abd3e01c3f19f3e6bd1e"), ("c3fba34855b33f025696326d2980ddc3fb47d90459ed6a4488fbb2e4e12ccf2b", "74674a86144ea866ad8fe633e256783bda4a07b997cb412c53a5eaf4cc7b6a0e"), ("0c35b8b94a720f1c26d7c6241c9f3ea5332a87cf3730b25ef31b68854c10e405", "6915df8d8ad19ec17be37c299eed762f9b63e841cd7963e13e8db6890dba082d"), ("a60b021677da95ba46c8c3411ac77f3e3b06937a8d189517111c045880029909", "bbfcd22ecb44cec6fd0717cd0f21b26e6e8b2c91e1a6cb5d8610e2f2ad41c90f"), ("2f4d662e66fbe754b708b87ea3d75a01d2ea4d7bf33c615c2376211dcc3b560f", "c67f7e622c3293028b9f86571e82c49551eb5fd308a35eb663498cffac208810"), ("d90a1afa96c14c8d3b989a9cf23d6e8b9907da42724e44a3ac74ae015b6ce22e", "f0795b1bdd0a907252b6ab047642d97be076a16ea69d463f1a4bec00c817202e"), ("dfa0764d89c8da10777504e5274f1baccf8b4145deba72b26503474318fb6410", "9f8aa8e833ea2f13cccfb6d9f2f04fd7be9c9f3019540c05c1986b3bce254e25"), ("e17490013b6b53a40964ff1067b922d4d73521e32fe394527b39c1bfd4a5e712", "7a8fa23a0e4b14a36f2818a7d98639f6e4934c028da780a6cc658fbf76e80a26"), ("7a9328d1075373dddb1b1100e8217ffaa1f9b632911b95a8fdc08870b15a8410", "94b2ae70c046b94873098c19fe18e7b17db2d31fe6a7eb73fea8168395e3c122"), ("ce319bb1447da5bf51a88ab3379dbea539b5a431d3c4f131048cf7b05c52161d", "cdfa264ab8a51bbc9fff5732cf544bb06abba7e807a8c252a5f9ce785c6ff22c"), ("d3818503bfcbef9ca03fc4472be77cb4936a1720001fc5e54852f769448fd313", "e72792abd906976c75a5670de514894a720c5293cf74338a9c2987640a949c0b"), ("4f9b8d118460736eb62602d12d3aed62938d4d4374b8c88704cf40c415c0901e", "2ecbe4588ade31924a1053fe0204950a0b4924878b312e56b2d0522f92a5f01c"), ("39b59fd96adaf9633edcc8cef10049cddf5f7df8ac80af8aafa436d62ee7f905", "b94f7a979df8a95fc2766a9a96308ff39a14daf7b7d6bc48591d2ea4c764bf00"), ("73cb0d25995d182b361c8237852bafeab8bb951de99f730da2913a239400c322", "da04e6e4446b6bcd54667b741444d826abbc5b76572d28474dfa94db91144606"), ("2753423b83bba5bb8b2799bc58125c46fb03ec05e8579d772cdc6e75b0875009", "ee040010ec20e7293431a3692d06416f71162d176a316b0329a76edf6f3fb30d"), ("cb73ec9b67355ce6275b51144a0759de28bb9390aab20514ee49a3bd8ce5361b", "c7c6e2ad3efe1f03b398241bddb67b008dd5e8caf1a17db9c33d2e2388d58e0d"), ("bdb386d9f3fe5613ede926d80246682a5d32392f7f6c9d818a80d8e7a12e371b", "21d48c74f422ef72cb9db18799916b3c11d0cb99cec808563e2db22047840902"), ("60f2fcfe341034449046c1fa330f6aceff737a9837a7d6d3a4885f8afce4a809", "e4c8f26f9e4127511b5ab21f1705913d7beaa2767cb7d033564e36bd2693370c"), ("1223d64fb5c44921c1ae66d91543780634c2f7bae7e184e09c4e5447e6d1180b", "290ab84714fc3534c5ba22b2aa7696ac03ed12c9cf1c409a777bac05475ea406"), ("b79c593316c42280d316bb7f3b43c7a5f5e29786d1ca6c0424463470eefe2b2f", "f4e44ac9574c4d32ab403e71dd00554eee0d8e34d04611b66ab2e59c49bff025"), ("e6548f669ebe655cb6600432e1af14c43da7286e3620289b51cc947517db9c14", "ace261e3964a1dcb389a693f52104018cd475e6856ae37fe4892df9482954902"), ("81be54409c9364f4842209e0f79b190afd9df017cc9790e11196d4bf5108f100", "d986b22e422703e065f12b6fd608813028ff913d4ffeb54b19e6537456391b23"), ("940ff0d3b4549d1baf7ad900cf6aedde02833b777e39c411e6efe7bcdd2ef305",
# STATIC_GROUP_MERKLE_ROOT is the root of the Merkle tree constructed from the STATIC_GROUP_KEYS above
# only identity commitments are used for the Merkle tree construction
# the root is created locally, using createMembershipList proc from waku_rln_relay_utils module, and the result is hardcoded in here
STATIC_GROUP_MERKLE_ROOT* = "25e03b7d551c3016cb21c057a113be5bf27eaa9b2529e806715bc0a1ef221e07"
Off-chain group construction and management (#718) * WIP * WIP: fixes a bug * adds test for static group formation * adds static group creation when rln-relay is enabled * adds createStatic group * wip: adds group formation to mount rlnrelay * adds createMembershipList utility function * adds doc strings and todos * cleans up the code and add comments * defaults createRLNInstance depth argument to 32 * renames Depth * distinguishes between onchain and offchain modes * updates index boundaries * updates log levels * updates docstring * updates log level of displayed membership keys * relocates a todo * activates all the tests * fixes some comments and todos * extracts some utils procs for better debugging * adds todo * moves calculateMerkleRoot and toMembersipKeyPairs to the rln utils * makes calls to the utils functions * adds unit test for createMembershipList * adds unittest for toMembershipKeyPairs and calcMerkleRoot * cleans up the code and fixes tree root value * reverts an unwanted change * minor * adds comments and cleans up the code * updates config message * adds more comments * fixes a minor value mismatch * edits the size of group * minor rewording * defines a const var for the group keys * replaces the sequence literal with the StaticGroupKeys const * converts var to let when applicable * replaces hardcoded value with well-defined constants * moves createMembershipList to the rln relay utils module * renames HashSize to HashHexSize * minor updates on the comments * reorganizes the consts * indicates that rlnRelayMemIndex is an experimental option * fixes a type conversion bug * revises the unittest of "mount waku rln-relay off-chain" * clarifies the use of index * updates a docstring * removes redundant constants and capitalize all of them * deletes the ETH_CLIENT const from the test file * renames a few vars for the sake of clarity * reorganizes unittest into blocks of execution, debug messages, and checks * adds more comments * more comments and clarifications * cleans up the tests * minor * adds a minor fix * replaces a var usage with let * fixes a bug
2021-09-17 17:31:25 +00:00