fix: panic if it is not possible to obtain the merkle root (#873)

This commit is contained in:
richΛrd 2023-11-09 16:17:41 -04:00 committed by GitHub
parent ff94b1faf0
commit 5aa4d578aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 21 additions and 34 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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