Skip to content

Commit

Permalink
fix: account for PR suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh-98 committed Sep 6, 2023
1 parent 0e0af05 commit 81c9919
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 70 deletions.
4 changes: 2 additions & 2 deletions waku/v2/node/wakunode2_rln.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (w *WakuNode) setupRLNRelay() error {
return nil
}

var groupManager group_manager.GroupManagerI
var groupManager group_manager.GroupManager

rlnInstance, rootTracker, err := rln.GetRLNInstanceAndRootTracker(w.opts.rlnTreePath)
if err != nil {
Expand Down Expand Up @@ -73,7 +73,7 @@ func (w *WakuNode) setupRLNRelay() error {
}
}

rlnRelay := rln.New(group_manager.GMDetails{
rlnRelay := rln.New(group_manager.Details{
GroupManager: groupManager,
RootTracker: rootTracker,
RLN: rlnInstance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,25 @@ import (
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
"github.com/waku-org/go-waku/waku/v2/protocol/rln/contracts"
"github.com/waku-org/go-waku/waku/v2/protocol/rln/group_manager"
"github.com/waku-org/go-waku/waku/v2/protocol/rln/web3"
"github.com/waku-org/go-waku/waku/v2/utils"
"github.com/waku-org/go-zerokit-rln/rln"
)

var NULL_ADDR common.Address = common.HexToAddress("0x0000000000000000000000000000000000000000")

func TestFetchingLogic(t *testing.T) {
client := NewMockClient(t, "membership_fetcher.json")

rlnContract, err := contracts.NewRLN(NULL_ADDR, client)
if err != nil {
t.Fatal(err)
}
rlnContract, err := contracts.NewRLN(common.Address{}, client)
require.NoError(t, err)
rlnInstance, err := rln.NewRLN()
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
rootTracker, err := group_manager.NewMerkleRootTracker(1, rlnInstance)
if err != nil {
t.Fatal(err)
}
dgm := MembershipFetcher{
require.NoError(t, err)
//
mf := MembershipFetcher{
web3Config: &web3.Config{
RLNContract: web3.RLNContract{
RLN: rlnContract,
Expand All @@ -53,25 +47,22 @@ func TestFetchingLogic(t *testing.T) {
// loadOldEvents will check till 10010
client.SetLatestBlockNumber(10010)
// watchNewEvents will check till 10012
if err := dgm.HandleGroupUpdates(context.TODO(), mockFn); err != nil {
ctx, cancel := context.WithCancel(context.Background())
if err := mf.HandleGroupUpdates(ctx, mockFn); err != nil {
t.Fatal(err)
}
go func() {
for {
if client.latestBlockNum.Load() == 10012 {
time.Sleep(time.Second)
cancel()
return
}
time.Sleep(time.Second)
}
}()
mf.Stop()
// sleep so that watchNewEvents can finish
time.Sleep(time.Second)
// check whether all the events are fetched or not.
if !sameArr(counts, []int{1, 3, 2, 1, 1}) {
t.Fatal("wrong no of events fetched per cycle", counts)
}
}

func sameArr(a, b []int) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
require.Equal(t, counts, []int{1, 3, 2, 1, 1})
}
20 changes: 12 additions & 8 deletions waku/v2/protocol/rln/group_manager/dynamic/mock_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"math/big"
"sort"
"sync/atomic"
"testing"

"github.com/ethereum/go-ethereum"
Expand All @@ -21,16 +22,19 @@ type ErrCount struct {
type MockClient struct {
ethclient.Client
blockChain MockBlockChain
latestBlockNum int64
latestBlockNum atomic.Int64
errOnBlock map[int64]*ErrCount
}

func (c *MockClient) SetLatestBlockNumber(num int64) {
c.latestBlockNum = num
c.latestBlockNum.Store(num)
}

func (c MockClient) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
return types.NewBlock(&types.Header{Number: big.NewInt(c.latestBlockNum)}, nil, nil, nil, nil), nil
func (c *MockClient) Close() {

}
func (c *MockClient) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
return types.NewBlock(&types.Header{Number: big.NewInt(c.latestBlockNum.Load())}, nil, nil, nil, nil), nil
}
func NewMockClient(t *testing.T, blockFile string) *MockClient {
blockChain := MockBlockChain{}
Expand All @@ -48,7 +52,7 @@ func (client *MockClient) SetErrorOnBlock(blockNum int64, err error, count int)
client.errOnBlock[blockNum] = &ErrCount{err: err, count: count}
}

func (c MockClient) getFromAndToRange(query ethereum.FilterQuery) (int64, int64) {
func (c *MockClient) getFromAndToRange(query ethereum.FilterQuery) (int64, int64) {
var fromBlock int64
if query.FromBlock == nil {
fromBlock = 0
Expand All @@ -64,7 +68,7 @@ func (c MockClient) getFromAndToRange(query ethereum.FilterQuery) (int64, int64)
}
return fromBlock, toBlock
}
func (c MockClient) FilterLogs(ctx context.Context, query ethereum.FilterQuery) (allTxLogs []types.Log, err error) {
func (c *MockClient) FilterLogs(ctx context.Context, query ethereum.FilterQuery) (allTxLogs []types.Log, err error) {
fromBlock, toBlock := c.getFromAndToRange(query)
for block, details := range c.blockChain.Blocks {
if block >= fromBlock && block <= toBlock {
Expand All @@ -86,10 +90,10 @@ func (c MockClient) FilterLogs(ctx context.Context, query ethereum.FilterQuery)

func (c *MockClient) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) {
for {
next := c.latestBlockNum + 1
next := c.latestBlockNum.Load() + 1
if c.blockChain.Blocks[next] != nil {
ch <- &types.Header{Number: big.NewInt(next)}
c.latestBlockNum = next
c.latestBlockNum.Store(next)
} else {
break
}
Expand Down
6 changes: 3 additions & 3 deletions waku/v2/protocol/rln/group_manager/group_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (
"github.com/waku-org/go-zerokit-rln/rln"
)

type GroupManagerI interface {
type GroupManager interface {
Start(ctx context.Context) error
IdentityCredentials() (rln.IdentityCredential, error)
MembershipIndex() rln.MembershipIndex
Stop() error
}

type GMDetails struct {
GroupManager GroupManagerI
type Details struct {
GroupManager GroupManager
RootTracker *MerkleRootTracker

RLN *rln.RLN
Expand Down
26 changes: 13 additions & 13 deletions waku/v2/protocol/rln/onchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (s *WakuRLNRelayDynamicSuite) TestDynamicGroupManagement() {

// initialize the WakuRLNRelay
rlnRelay := &WakuRLNRelay{
GMDetails: group_manager.GMDetails{
Details: group_manager.Details{
RootTracker: rt,
GroupManager: gm,
RLN: rlnInstance,
Expand Down Expand Up @@ -239,7 +239,7 @@ func (s *WakuRLNRelayDynamicSuite) TestMerkleTreeConstruction() {
gm, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, membershipIndex, appKeystore, keystorePassword, prometheus.DefaultRegisterer, rlnInstance, rootTracker, utils.Logger())
s.Require().NoError(err)

rlnRelay := New(group_manager.GMDetails{
rlnRelay := New(group_manager.Details{
RLN: rlnInstance,
RootTracker: rootTracker,
GroupManager: gm,
Expand Down Expand Up @@ -273,15 +273,15 @@ func (s *WakuRLNRelayDynamicSuite) TestCorrectRegistrationOfPeers() {
membershipGroupIndex := s.register(appKeystore, credentials1, s.u1PrivKey)

// mount the rln relay protocol in the on-chain/dynamic mode
rootIntance, rootTracker, err := GetRLNInstanceAndRootTracker(s.tmpRLNDBPath())
rootInstance, rootTracker, err := GetRLNInstanceAndRootTracker(s.tmpRLNDBPath())
s.Require().NoError(err)
gm1, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, membershipGroupIndex, appKeystore, keystorePassword, prometheus.DefaultRegisterer, rootIntance, rootTracker, utils.Logger())
gm1, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, membershipGroupIndex, appKeystore, keystorePassword, prometheus.DefaultRegisterer, rootInstance, rootTracker, utils.Logger())
s.Require().NoError(err)

rlnRelay1 := New(group_manager.GMDetails{
rlnRelay1 := New(group_manager.Details{
GroupManager: gm1,
RootTracker: rootTracker,
RLN: rootIntance,
RLN: rootInstance,
}, timesource.NewDefaultClock(), prometheus.DefaultRegisterer, utils.Logger())

err = rlnRelay1.Start(context.TODO())
Expand All @@ -297,15 +297,15 @@ func (s *WakuRLNRelayDynamicSuite) TestCorrectRegistrationOfPeers() {
membershipGroupIndex = s.register(appKeystore2, credentials2, s.u2PrivKey)

// mount the rln relay protocol in the on-chain/dynamic mode
rootIntance, rootTracker, err := GetRLNInstanceAndRootTracker(s.tmpRLNDBPath())
rootInstance, rootTracker, err = GetRLNInstanceAndRootTracker(s.tmpRLNDBPath())
s.Require().NoError(err)
gm2, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, membershipGroupIndex, appKeystore2, keystorePassword, prometheus.DefaultRegisterer, rootIntance, rootTracker, utils.Logger())
gm2, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, membershipGroupIndex, appKeystore2, keystorePassword, prometheus.DefaultRegisterer, rootInstance, rootTracker, utils.Logger())
s.Require().NoError(err)

rlnRelay2 := New(group_manager.GMDetails{
GroupManager: gm1,
rlnRelay2 := New(group_manager.Details{
GroupManager: gm2,
RootTracker: rootTracker,
RLN: rootIntance,
RLN: rootInstance,
}, timesource.NewDefaultClock(), prometheus.DefaultRegisterer, utils.Logger())
err = rlnRelay2.Start(context.TODO())
s.Require().NoError(err)
Expand All @@ -314,8 +314,8 @@ func (s *WakuRLNRelayDynamicSuite) TestCorrectRegistrationOfPeers() {
// 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()
idx1 := rlnRelay1.GroupManager.MembershipIndex()
idx2 := rlnRelay2.GroupManager.MembershipIndex()
s.Require().NoError(err)
s.Require().Equal(rln.MembershipIndex(1), idx1)
s.Require().Equal(rln.MembershipIndex(2), idx2)
Expand Down
6 changes: 3 additions & 3 deletions waku/v2/protocol/rln/rln_relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (s *WakuRLNRelaySuite) TestOffchainMode() {
groupManager, err := static.NewStaticGroupManager(groupIDCommitments, idCredential, index, rlnInstance, rootTracker, utils.Logger())
s.Require().NoError(err)

wakuRLNRelay := New(group_manager.GMDetails{
wakuRLNRelay := New(group_manager.Details{
GroupManager: groupManager,
RootTracker: rootTracker,
RLN: rlnInstance,
Expand Down Expand Up @@ -92,7 +92,7 @@ func (s *WakuRLNRelaySuite) TestUpdateLogAndHasDuplicate() {

rlnRelay := &WakuRLNRelay{
nullifierLog: make(map[r.Nullifier][]r.ProofMetadata),
GMDetails: group_manager.GMDetails{
Details: group_manager.Details{
RootTracker: rootTracker,
},
}
Expand Down Expand Up @@ -184,7 +184,7 @@ func (s *WakuRLNRelaySuite) TestValidateMessage() {
s.Require().NoError(err)

rlnRelay := &WakuRLNRelay{
GMDetails: group_manager.GMDetails{
Details: group_manager.Details{
GroupManager: groupManager,
RootTracker: rootTracker,
RLN: rlnInstance,
Expand Down
12 changes: 4 additions & 8 deletions waku/v2/protocol/rln/waku_rln_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type WakuRLNRelay struct {
timesource timesource.Timesource
metrics Metrics

group_manager.GMDetails
group_manager.Details

// the log of nullifiers and Shamir shares of the past messages grouped per epoch
nullifierLogLock sync.RWMutex
Expand Down Expand Up @@ -60,19 +60,15 @@ func GetRLNInstanceAndRootTracker(treePath string) (*rln.RLN, *group_manager.Mer
return rlnInstance, rootTracker, nil
}
func New(
gmDetails group_manager.GMDetails,
Details group_manager.Details,
timesource timesource.Timesource,
reg prometheus.Registerer,
log *zap.Logger) *WakuRLNRelay {

start := time.Now()
metrics := newMetrics(reg)
metrics.RecordInstanceCreation(time.Since(start))

// create the WakuRLNRelay
rlnPeer := &WakuRLNRelay{
GMDetails: gmDetails,
metrics: metrics,
Details: Details,
metrics: newMetrics(reg),
log: log,
timesource: timesource,
nullifierLog: make(map[rln.MerkleNode][]rln.ProofMetadata),
Expand Down
6 changes: 3 additions & 3 deletions waku/v2/protocol/rln/web3/web3.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type RLNContract struct {
DeployedBlockNumber uint64
}

// EthClientI is an interface for the ethclient.Client, so that we can pass mock client for testing
type EthClientI interface {
// EthClient is an interface for the ethclient.Client, so that we can pass mock client for testing
type EthClient interface {
bind.ContractBackend
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error)
Expand All @@ -42,7 +42,7 @@ type Config struct {
configured bool

ETHClientAddress string
ETHClient EthClientI
ETHClient EthClient
ChainID *big.Int
RegistryContract RegistryContract
RLNContract RLNContract
Expand Down

0 comments on commit 81c9919

Please sign in to comment.