From 5aa4d578aa9bab5f845604ea21462701d18f7b0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?rich=CE=9Brd?= Date: Thu, 9 Nov 2023 16:17:41 -0400 Subject: [PATCH] fix: panic if it is not possible to obtain the merkle root (#873) --- .../rln/group_manager/dynamic/dynamic.go | 5 +---- .../rln/group_manager/dynamic/handler_test.go | 3 +-- .../dynamic/membership_fetcher_test.go | 6 +++--- .../protocol/rln/group_manager/root_tracker.go | 17 ++++++++--------- .../protocol/rln/group_manager/static/static.go | 5 +---- waku/v2/protocol/rln/onchain_test.go | 3 +-- waku/v2/protocol/rln/rln_relay_test.go | 10 ++++------ waku/v2/protocol/rln/waku_rln_relay.go | 6 ++---- 8 files changed, 21 insertions(+), 34 deletions(-) diff --git a/waku/v2/protocol/rln/group_manager/dynamic/dynamic.go b/waku/v2/protocol/rln/group_manager/dynamic/dynamic.go index 4e688735..8faf29d1 100644 --- a/waku/v2/protocol/rln/group_manager/dynamic/dynamic.go +++ b/waku/v2/protocol/rln/group_manager/dynamic/dynamic.go @@ -244,10 +244,7 @@ func (gm *DynamicGroupManager) InsertMembers(toInsert *om.OrderedMap) error { gm.metrics.RecordRegisteredMembership(gm.rln.LeavesSet()) - _, err = gm.rootTracker.UpdateLatestRoot(pair.Key.(uint64)) - if err != nil { - return err - } + gm.rootTracker.UpdateLatestRoot(pair.Key.(uint64)) } return nil } diff --git a/waku/v2/protocol/rln/group_manager/dynamic/handler_test.go b/waku/v2/protocol/rln/group_manager/dynamic/handler_test.go index 271e5699..8474c25d 100644 --- a/waku/v2/protocol/rln/group_manager/dynamic/handler_test.go +++ b/waku/v2/protocol/rln/group_manager/dynamic/handler_test.go @@ -31,8 +31,7 @@ func TestHandler(t *testing.T) { rlnInstance, err := rln.NewRLN() require.NoError(t, err) - rootTracker, err := group_manager.NewMerkleRootTracker(5, rlnInstance) - require.NoError(t, err) + rootTracker := group_manager.NewMerkleRootTracker(5, rlnInstance) _, cancel := context.WithCancel(context.TODO()) defer cancel() diff --git a/waku/v2/protocol/rln/group_manager/dynamic/membership_fetcher_test.go b/waku/v2/protocol/rln/group_manager/dynamic/membership_fetcher_test.go index 1cf0017b..4c70fba0 100644 --- a/waku/v2/protocol/rln/group_manager/dynamic/membership_fetcher_test.go +++ b/waku/v2/protocol/rln/group_manager/dynamic/membership_fetcher_test.go @@ -22,9 +22,9 @@ func TestFetchingLogic(t *testing.T) { require.NoError(t, err) rlnInstance, err := rln.NewRLN() require.NoError(t, err) - rootTracker, err := group_manager.NewMerkleRootTracker(1, rlnInstance) - require.NoError(t, err) - // + + rootTracker := group_manager.NewMerkleRootTracker(1, rlnInstance) + mf := MembershipFetcher{ web3Config: &web3.Config{ RLNContract: web3.RLNContract{ diff --git a/waku/v2/protocol/rln/group_manager/root_tracker.go b/waku/v2/protocol/rln/group_manager/root_tracker.go index 3ec7221a..5e914382 100644 --- a/waku/v2/protocol/rln/group_manager/root_tracker.go +++ b/waku/v2/protocol/rln/group_manager/root_tracker.go @@ -4,7 +4,9 @@ import ( "bytes" "sync" + "github.com/waku-org/go-waku/waku/v2/utils" "github.com/waku-org/go-zerokit-rln/rln" + "go.uber.org/zap" ) // RootsPerBlock stores the merkle root generated at N block number @@ -27,18 +29,15 @@ type MerkleRootTracker struct { const maxBufferSize = 20 // NewMerkleRootTracker creates an instance of MerkleRootTracker -func NewMerkleRootTracker(acceptableRootWindowSize int, rlnInstance *rln.RLN) (*MerkleRootTracker, error) { +func NewMerkleRootTracker(acceptableRootWindowSize int, rlnInstance *rln.RLN) *MerkleRootTracker { result := &MerkleRootTracker{ acceptableRootWindowSize: acceptableRootWindowSize, rln: rlnInstance, } - _, err := result.UpdateLatestRoot(0) - if err != nil { - return nil, err - } + result.UpdateLatestRoot(0) - return result, nil + return result } // Backfill is used to pop merkle roots when there is a chain fork @@ -102,18 +101,18 @@ func (m *MerkleRootTracker) IndexOf(root [32]byte) int { // UpdateLatestRoot should be called when a block containing a new // IDCommitment is received so we can keep track of the merkle root change -func (m *MerkleRootTracker) UpdateLatestRoot(blockNumber uint64) (rln.MerkleNode, error) { +func (m *MerkleRootTracker) UpdateLatestRoot(blockNumber uint64) rln.MerkleNode { m.Lock() defer m.Unlock() root, err := m.rln.GetMerkleRoot() if err != nil { - return [32]byte{}, err + utils.Logger().Named("root-tracker").Panic("could not retrieve merkle root", zap.Error(err)) } m.pushRoot(blockNumber, root) - return root, nil + return root } func (m *MerkleRootTracker) pushRoot(blockNumber uint64, root [32]byte) { diff --git a/waku/v2/protocol/rln/group_manager/static/static.go b/waku/v2/protocol/rln/group_manager/static/static.go index e4c5889a..cca91cad 100644 --- a/waku/v2/protocol/rln/group_manager/static/static.go +++ b/waku/v2/protocol/rln/group_manager/static/static.go @@ -68,10 +68,7 @@ func (gm *StaticGroupManager) insertMembers(idCommitments []rln.IDCommitment) er latestIndex := gm.nextIndex + uint64(len(idCommitments)) - _, err = gm.rootTracker.UpdateLatestRoot(latestIndex) - if err != nil { - return err - } + gm.rootTracker.UpdateLatestRoot(latestIndex) gm.nextIndex = latestIndex + 1 diff --git a/waku/v2/protocol/rln/onchain_test.go b/waku/v2/protocol/rln/onchain_test.go index 16e32d20..e603b0e5 100644 --- a/waku/v2/protocol/rln/onchain_test.go +++ b/waku/v2/protocol/rln/onchain_test.go @@ -140,8 +140,7 @@ func (s *WakuRLNRelayDynamicSuite) TestDynamicGroupManagement() { rlnInstance, err := rln.NewRLN() s.Require().NoError(err) - rt, err := group_manager.NewMerkleRootTracker(5, rlnInstance) - s.Require().NoError(err) + rt := group_manager.NewMerkleRootTracker(5, rlnInstance) u1Credentials := s.generateCredentials(rlnInstance) appKeystore, err := keystore.New(s.tmpKeystorePath(), dynamic.RLNAppInfo, utils.Logger()) diff --git a/waku/v2/protocol/rln/rln_relay_test.go b/waku/v2/protocol/rln/rln_relay_test.go index 0553e3f7..e9f8ed1f 100644 --- a/waku/v2/protocol/rln/rln_relay_test.go +++ b/waku/v2/protocol/rln/rln_relay_test.go @@ -88,8 +88,7 @@ func (s *WakuRLNRelaySuite) TestUpdateLogAndHasDuplicate() { rlnInstance, err := r.NewRLN() s.Require().NoError(err) - rootTracker, err := group_manager.NewMerkleRootTracker(acceptableRootWindowSize, rlnInstance) - s.Require().NoError(err) + rootTracker := group_manager.NewMerkleRootTracker(acceptableRootWindowSize, rlnInstance) rlnRelay := &WakuRLNRelay{ nullifierLog: NewNullifierLog(context.TODO(), utils.Logger()), @@ -184,10 +183,9 @@ func (s *WakuRLNRelaySuite) TestValidateMessage() { // Create a RLN instance rlnInstance, err := r.NewRLN() s.Require().NoError(err) - // - rootTracker, err := group_manager.NewMerkleRootTracker(acceptableRootWindowSize, rlnInstance) - s.Require().NoError(err) - // + + rootTracker := group_manager.NewMerkleRootTracker(acceptableRootWindowSize, rlnInstance) + idCredential := groupKeyPairs[index] groupManager, err := static.NewStaticGroupManager(groupIDCommitments, idCredential, index, rlnInstance, rootTracker, utils.Logger()) s.Require().NoError(err) diff --git a/waku/v2/protocol/rln/waku_rln_relay.go b/waku/v2/protocol/rln/waku_rln_relay.go index 0a907341..77b6e2da 100644 --- a/waku/v2/protocol/rln/waku_rln_relay.go +++ b/waku/v2/protocol/rln/waku_rln_relay.go @@ -47,10 +47,8 @@ func GetRLNInstanceAndRootTracker(treePath string) (*rln.RLN, *group_manager.Mer return nil, nil, err } - rootTracker, err := group_manager.NewMerkleRootTracker(acceptableRootWindowSize, rlnInstance) - if err != nil { - return nil, nil, err - } + rootTracker := group_manager.NewMerkleRootTracker(acceptableRootWindowSize, rlnInstance) + return rlnInstance, rootTracker, nil } func New(