go-waku/waku/v2/protocol/rln/onchain_test.go

316 lines
12 KiB
Go
Raw Normal View History

2022-08-10 17:10:23 +00:00
//go:build include_onchain_tests
// +build include_onchain_tests
2022-08-10 13:03:25 +00:00
package rln
import (
"context"
"crypto/ecdsa"
2023-08-31 15:49:38 +00:00
"crypto/rand"
"encoding/hex"
2022-08-10 13:03:25 +00:00
"math/big"
2023-04-26 16:13:13 +00:00
"os"
2023-08-31 15:49:38 +00:00
"path/filepath"
2022-08-10 13:03:25 +00:00
"testing"
"time"
2023-08-22 19:30:04 +00:00
"github.com/prometheus/client_golang/prometheus"
2023-04-26 15:55:03 +00:00
"github.com/waku-org/go-zerokit-rln/rln"
2022-08-10 13:03:25 +00:00
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
2022-08-10 13:03:25 +00:00
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/suite"
"github.com/waku-org/go-waku/waku/v2/protocol/rln/contracts"
2023-04-26 15:55:03 +00:00
"github.com/waku-org/go-waku/waku/v2/protocol/rln/group_manager"
"github.com/waku-org/go-waku/waku/v2/protocol/rln/group_manager/dynamic"
"github.com/waku-org/go-waku/waku/v2/protocol/rln/keystore"
2023-08-31 15:49:38 +00:00
"github.com/waku-org/go-waku/waku/v2/protocol/rln/web3"
2023-01-06 22:37:57 +00:00
"github.com/waku-org/go-waku/waku/v2/timesource"
"github.com/waku-org/go-waku/waku/v2/utils"
2022-08-10 13:03:25 +00:00
)
const keystorePassword = "test"
2023-04-26 15:55:03 +00:00
2022-08-10 13:03:25 +00:00
func TestWakuRLNRelayDynamicSuite(t *testing.T) {
suite.Run(t, new(WakuRLNRelayDynamicSuite))
}
type WakuRLNRelayDynamicSuite struct {
suite.Suite
2023-08-31 15:49:38 +00:00
web3Config *web3.Config
u1PrivKey *ecdsa.PrivateKey
u2PrivKey *ecdsa.PrivateKey
2022-08-10 13:03:25 +00:00
}
2023-08-31 15:49:38 +00:00
func TempFileName(prefix, suffix string) string {
randBytes := make([]byte, 16)
rand.Read(randBytes)
return filepath.Join(os.TempDir(), prefix+hex.EncodeToString(randBytes)+suffix)
}
2022-08-10 13:03:25 +00:00
func (s *WakuRLNRelayDynamicSuite) SetupTest() {
2023-04-26 16:13:13 +00:00
2023-08-31 15:49:38 +00:00
clientAddr := os.Getenv("GANACHE_NETWORK_RPC_URL")
if clientAddr == "" {
clientAddr = "ws://localhost:8545"
2023-04-26 16:13:13 +00:00
}
2023-08-31 15:49:38 +00:00
backend, err := ethclient.Dial(clientAddr)
2022-08-10 13:03:25 +00:00
s.Require().NoError(err)
2023-08-31 15:49:38 +00:00
defer backend.Close()
2022-08-10 13:03:25 +00:00
chainID, err := backend.ChainID(context.TODO())
s.Require().NoError(err)
// TODO: obtain account list from ganache mnemonic or from eth_accounts
s.u1PrivKey, err = crypto.ToECDSA(common.FromHex("0x156ec84a451d8a2d0062993242b6c4e863647f5544ff8030f23578d4142f43f8"))
s.Require().NoError(err)
s.u2PrivKey, err = crypto.ToECDSA(common.FromHex("0xa00da43843ad6b5161ddbace48f293ac3f82f8a8257af34de4c32900bb6e9a97"))
s.Require().NoError(err)
// Deploying contracts
auth, err := bind.NewKeyedTransactorWithChainID(s.u1PrivKey, chainID)
s.Require().NoError(err)
poseidonHasherAddr, _, _, err := contracts.DeployPoseidonHasher(auth, backend)
s.Require().NoError(err)
2023-08-31 15:49:38 +00:00
registryAddress, tx, rlnRegistry, err := contracts.DeployRLNRegistry(auth, backend, poseidonHasherAddr)
s.Require().NoError(err)
txReceipt, err := bind.WaitMined(context.TODO(), backend, tx)
2022-08-10 13:03:25 +00:00
s.Require().NoError(err)
2023-08-31 15:49:38 +00:00
s.Require().Equal(txReceipt.Status, types.ReceiptStatusSuccessful)
2022-08-10 13:03:25 +00:00
2023-08-31 15:49:38 +00:00
tx, err = rlnRegistry.NewStorage(auth)
s.Require().NoError(err)
txReceipt, err = bind.WaitMined(context.TODO(), backend, tx)
s.Require().NoError(err)
s.Require().Equal(txReceipt.Status, types.ReceiptStatusSuccessful)
2022-08-10 13:03:25 +00:00
2023-08-31 15:49:38 +00:00
s.web3Config = web3.NewConfig(clientAddr, registryAddress)
err = s.web3Config.Build(context.TODO())
s.Require().NoError(err)
}
func (s *WakuRLNRelayDynamicSuite) generateCredentials(rlnInstance *rln.RLN) *rln.IdentityCredential {
identityCredential, err := rlnInstance.MembershipKeyGen()
s.Require().NoError(err)
return identityCredential
}
func (s *WakuRLNRelayDynamicSuite) register(appKeystore *keystore.AppKeystore, identityCredential *rln.IdentityCredential, privKey *ecdsa.PrivateKey) rln.MembershipIndex {
2023-08-31 15:49:38 +00:00
membershipFee, err := s.web3Config.RLNContract.MEMBERSHIPDEPOSIT(&bind.CallOpts{Context: context.TODO()})
s.Require().NoError(err)
2023-08-31 15:49:38 +00:00
auth, err := bind.NewKeyedTransactorWithChainID(privKey, s.web3Config.ChainID)
2022-08-10 13:03:25 +00:00
s.Require().NoError(err)
auth.Value = membershipFee
2022-08-10 13:03:25 +00:00
auth.Context = context.TODO()
2023-08-31 15:49:38 +00:00
tx, err := s.web3Config.RegistryContract.Register(auth, s.web3Config.RLNContract.StorageIndex, rln.Bytes32ToBigInt(identityCredential.IDCommitment))
2023-04-26 15:55:03 +00:00
s.Require().NoError(err)
2023-08-31 15:49:38 +00:00
txReceipt, err := bind.WaitMined(context.TODO(), s.web3Config.ETHClient, tx)
s.Require().NoError(err)
s.Require().Equal(txReceipt.Status, types.ReceiptStatusSuccessful)
2023-08-31 15:49:38 +00:00
evt, err := s.web3Config.RLNContract.ParseMemberRegistered(*txReceipt.Logs[0])
2022-08-10 13:03:25 +00:00
s.Require().NoError(err)
2023-04-26 15:55:03 +00:00
membershipIndex := rln.MembershipIndex(uint(evt.Index.Int64()))
membershipCredential := keystore.MembershipCredentials{
IdentityCredential: identityCredential,
TreeIndex: membershipIndex,
2023-08-31 15:49:38 +00:00
MembershipContractInfo: keystore.NewMembershipContractInfo(s.web3Config.ChainID, s.web3Config.RegistryContract.Address),
2023-04-26 15:55:03 +00:00
}
err = appKeystore.AddMembershipCredentials(membershipCredential, keystorePassword)
s.Require().NoError(err)
return membershipIndex
2022-08-10 13:03:25 +00:00
}
func (s *WakuRLNRelayDynamicSuite) TestDynamicGroupManagement() {
// Create a RLN instance
rlnInstance, err := rln.NewRLN()
2022-08-10 13:03:25 +00:00
s.Require().NoError(err)
2023-04-26 15:55:03 +00:00
rt, err := group_manager.NewMerkleRootTracker(5, rlnInstance)
s.Require().NoError(err)
u1Credentials := s.generateCredentials(rlnInstance)
2023-08-31 15:49:38 +00:00
appKeystore, err := keystore.New(s.tmpKeystorePath(), dynamic.RLNAppInfo, utils.Logger())
s.Require().NoError(err)
membershipIndex := s.register(appKeystore, u1Credentials, s.u1PrivKey)
2023-08-31 15:49:38 +00:00
gm, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, membershipIndex, appKeystore, keystorePassword, prometheus.DefaultRegisterer, utils.Logger())
2022-08-10 13:03:25 +00:00
s.Require().NoError(err)
// initialize the WakuRLNRelay
2023-04-26 15:55:03 +00:00
rlnRelay := &WakuRLNRelay{
rootTracker: rt,
groupManager: gm,
RLN: rlnInstance,
log: utils.Logger(),
nullifierLog: make(map[rln.MerkleNode][]rln.ProofMetadata),
2022-08-10 13:03:25 +00:00
}
err = rlnRelay.Start(context.TODO())
2023-04-26 15:55:03 +00:00
s.Require().NoError(err)
u2Credentials := s.generateCredentials(rlnInstance)
2023-08-31 15:49:38 +00:00
appKeystore2, err := keystore.New(s.tmpKeystorePath(), dynamic.RLNAppInfo, utils.Logger())
s.Require().NoError(err)
membershipIndex = s.register(appKeystore2, u2Credentials, s.u2PrivKey)
2022-08-10 13:03:25 +00:00
time.Sleep(1 * time.Second)
2022-08-10 13:03:25 +00:00
treeCommitment, err := rlnInstance.GetLeaf(membershipIndex)
s.Require().NoError(err)
s.Require().Equal(u2Credentials.IDCommitment, treeCommitment)
2022-08-10 13:03:25 +00:00
}
func (s *WakuRLNRelayDynamicSuite) TestInsertKeyMembershipContract() {
// Create a RLN instance
rlnInstance, err := rln.NewRLN()
s.Require().NoError(err)
2022-08-10 13:03:25 +00:00
credentials1 := s.generateCredentials(rlnInstance)
credentials2 := s.generateCredentials(rlnInstance)
credentials3 := s.generateCredentials(rlnInstance)
2023-08-31 15:49:38 +00:00
appKeystore, err := keystore.New(s.tmpKeystorePath(), dynamic.RLNAppInfo, utils.Logger())
s.Require().NoError(err)
s.register(appKeystore, credentials1, s.u1PrivKey)
2022-08-10 13:03:25 +00:00
// Batch Register
2023-08-31 15:49:38 +00:00
auth, err := bind.NewKeyedTransactorWithChainID(s.u2PrivKey, s.web3Config.ChainID)
s.Require().NoError(err)
membershipFee, err := s.web3Config.RLNContract.MEMBERSHIPDEPOSIT(&bind.CallOpts{Context: context.TODO()})
2022-08-10 13:03:25 +00:00
s.Require().NoError(err)
auth.Value = membershipFee.Mul(big.NewInt(2), membershipFee)
2022-08-10 13:03:25 +00:00
auth.Context = context.TODO()
2023-08-31 15:49:38 +00:00
tx, err := s.web3Config.RegistryContract.Register1(auth, s.web3Config.RLNContract.StorageIndex, []*big.Int{rln.Bytes32ToBigInt(credentials2.IDCommitment), rln.Bytes32ToBigInt(credentials3.IDCommitment)})
2022-08-10 13:03:25 +00:00
s.Require().NoError(err)
2023-08-31 15:49:38 +00:00
txReceipt, err := bind.WaitMined(context.TODO(), s.web3Config.ETHClient, tx)
2022-08-10 13:03:25 +00:00
s.Require().NoError(err)
s.Require().Equal(txReceipt.Status, types.ReceiptStatusSuccessful)
2022-08-10 13:03:25 +00:00
}
func (s *WakuRLNRelayDynamicSuite) TestMerkleTreeConstruction() {
// Create a RLN instance
rlnInstance, err := rln.NewRLN()
2022-08-10 13:03:25 +00:00
s.Require().NoError(err)
credentials1 := s.generateCredentials(rlnInstance)
credentials2 := s.generateCredentials(rlnInstance)
2022-08-10 13:03:25 +00:00
2023-08-31 15:49:38 +00:00
err = rlnInstance.InsertMembers(1, []rln.IDCommitment{credentials1.IDCommitment, credentials2.IDCommitment})
2022-10-04 23:15:39 +00:00
s.Require().NoError(err)
2022-08-10 13:03:25 +00:00
// get the Merkle root
expectedRoot, err := rlnInstance.GetMerkleRoot()
s.Require().NoError(err)
// register the members to the contract
2023-08-31 15:49:38 +00:00
appKeystore, err := keystore.New(s.tmpKeystorePath(), dynamic.RLNAppInfo, utils.Logger())
s.Require().NoError(err)
membershipIndex := s.register(appKeystore, credentials1, s.u1PrivKey)
membershipIndex = s.register(appKeystore, credentials2, s.u1PrivKey)
2022-08-10 13:03:25 +00:00
// mount the rln relay protocol in the on-chain/dynamic mode
2023-08-31 15:49:38 +00:00
gm, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, membershipIndex, appKeystore, keystorePassword, prometheus.DefaultRegisterer, utils.Logger())
2023-04-26 15:55:03 +00:00
s.Require().NoError(err)
2023-08-31 15:49:38 +00:00
rlnRelay, err := New(gm, s.tmpRLNDBPath(), timesource.NewDefaultClock(), prometheus.DefaultRegisterer, utils.Logger())
2023-04-26 15:55:03 +00:00
s.Require().NoError(err)
err = rlnRelay.Start(context.TODO())
2022-08-10 13:03:25 +00:00
s.Require().NoError(err)
// wait for the event to reach the group handler
2023-01-06 22:37:57 +00:00
time.Sleep(2 * time.Second)
2022-08-10 13:03:25 +00:00
// rln pks are inserted into the rln peer's Merkle tree and the resulting root
// is expected to be the same as the calculatedRoot i.e., the one calculated outside of the mountRlnRelayDynamic proc
calculatedRoot, err := rlnRelay.RLN.GetMerkleRoot()
s.Require().NoError(err)
s.Require().Equal(expectedRoot, calculatedRoot)
}
func (s *WakuRLNRelayDynamicSuite) TestCorrectRegistrationOfPeers() {
// Creating an RLN instance (just for generating membership keys)
rlnInstance, err := rln.NewRLN()
s.Require().NoError(err)
2022-08-10 13:03:25 +00:00
// Node 1 ============================================================
// Register credentials1 in contract and keystore1
credentials1 := s.generateCredentials(rlnInstance)
2023-08-31 15:49:38 +00:00
appKeystore, err := keystore.New(s.tmpKeystorePath(), dynamic.RLNAppInfo, utils.Logger())
s.Require().NoError(err)
membershipGroupIndex := s.register(appKeystore, credentials1, s.u1PrivKey)
2022-08-10 13:03:25 +00:00
// mount the rln relay protocol in the on-chain/dynamic mode
2023-08-31 15:49:38 +00:00
gm1, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, membershipGroupIndex, appKeystore, keystorePassword, prometheus.DefaultRegisterer, utils.Logger())
2023-04-26 15:55:03 +00:00
s.Require().NoError(err)
2023-08-31 15:49:38 +00:00
rlnRelay1, err := New(gm1, s.tmpRLNDBPath(), timesource.NewDefaultClock(), prometheus.DefaultRegisterer, utils.Logger())
2023-04-26 15:55:03 +00:00
s.Require().NoError(err)
err = rlnRelay1.Start(context.TODO())
2022-08-10 13:03:25 +00:00
s.Require().NoError(err)
// Node 2 ============================================================
// Register credentials2 in contract and keystore2
credentials2 := s.generateCredentials(rlnInstance)
2023-08-31 15:49:38 +00:00
appKeystore2, err := keystore.New(s.tmpKeystorePath(), dynamic.RLNAppInfo, utils.Logger())
s.Require().NoError(err)
2023-08-31 15:49:38 +00:00
membershipGroupIndex = s.register(appKeystore2, credentials2, s.u2PrivKey)
2022-08-10 13:03:25 +00:00
// mount the rln relay protocol in the on-chain/dynamic mode
2023-08-31 15:49:38 +00:00
gm2, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, membershipGroupIndex, appKeystore2, keystorePassword, prometheus.DefaultRegisterer, utils.Logger())
2023-04-26 15:55:03 +00:00
s.Require().NoError(err)
2023-08-31 15:49:38 +00:00
rlnRelay2, err := New(gm2, s.tmpRLNDBPath(), timesource.NewDefaultClock(), prometheus.DefaultRegisterer, utils.Logger())
2023-04-26 15:55:03 +00:00
s.Require().NoError(err)
err = rlnRelay2.Start(context.TODO())
2022-08-10 13:03:25 +00:00
s.Require().NoError(err)
// ==================================
// the two nodes should be registered into the contract
// since nodes are spun up sequentially
// the first node has index 0 whereas the second node gets index 1
idx1 := rlnRelay1.groupManager.MembershipIndex()
idx2 := rlnRelay2.groupManager.MembershipIndex()
2023-04-26 15:55:03 +00:00
s.Require().NoError(err)
2023-08-31 15:49:38 +00:00
s.Require().Equal(rln.MembershipIndex(1), idx1)
s.Require().Equal(rln.MembershipIndex(2), idx2)
}
2023-04-26 15:55:03 +00:00
2023-08-31 15:49:38 +00:00
func (s *WakuRLNRelayDynamicSuite) tmpKeystorePath() string {
keystoreDir, err := os.MkdirTemp("", "keystore_dir")
s.Require().NoError(err)
return filepath.Join(keystoreDir, "keystore.json")
}
func (s *WakuRLNRelayDynamicSuite) tmpRLNDBPath() string {
dbPath, err := os.MkdirTemp("", "rln_db")
s.Require().NoError(err)
return dbPath
2022-08-10 13:03:25 +00:00
}