From b9fd7aa1ee683f551088f29b284120742f94f4b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Negovanovi=C4=87?= Date: Tue, 25 Jun 2024 14:33:59 +0200 Subject: [PATCH] Implement AsSlice and remove Range function --- synchronizer/batches.go | 7 ++---- synchronizer/committee_map.go | 13 +++++----- synchronizer/committee_map_test.go | 40 +++++++++++++----------------- 3 files changed, 25 insertions(+), 35 deletions(-) diff --git a/synchronizer/batches.go b/synchronizer/batches.go index ad7cb6e..aa31ef7 100644 --- a/synchronizer/batches.go +++ b/synchronizer/batches.go @@ -86,6 +86,7 @@ func (bs *BatchSynchronizer) resolveCommittee() error { filteredMembers = append(filteredMembers, m) } } + bs.committee = NewCommitteeMapSafe() bs.committee.StoreBatch(filteredMembers) return nil @@ -324,11 +325,7 @@ func (bs *BatchSynchronizer) resolve(batch types.BatchKey) (*types.OffChainData, } // pull out the members, iterating will change the map on error - members := make([]etherman.DataCommitteeMember, bs.committee.Length()) - bs.committee.Range(func(_ common.Address, member etherman.DataCommitteeMember) bool { - members = append(members, member) - return true - }) + members := bs.committee.AsSlice() // iterate through them randomly until data is resolved for _, r := range rand.Perm(len(members)) { diff --git a/synchronizer/committee_map.go b/synchronizer/committee_map.go index 91999ce..20cc2da 100644 --- a/synchronizer/committee_map.go +++ b/synchronizer/committee_map.go @@ -56,17 +56,16 @@ func (t *CommitteeMapSafe) Delete(key common.Address) { delete(t.members, key) } -// Range calls f sequentially for each key and value present in the map. -// If f returns false, range stops the iteration. -func (t *CommitteeMapSafe) Range(f func(key common.Address, value etherman.DataCommitteeMember) bool) { +// AsSlice returns a slice of data committee members. +func (t *CommitteeMapSafe) AsSlice() []etherman.DataCommitteeMember { t.mu.RLock() defer t.mu.RUnlock() - for k, v := range t.members { - if !f(k, v) { - break - } + membersSlice := make([]etherman.DataCommitteeMember, 0, len(t.members)) + for _, m := range t.members { + membersSlice = append(membersSlice, m) } + return membersSlice } // Length returns the current length of the map. diff --git a/synchronizer/committee_map_test.go b/synchronizer/committee_map_test.go index b33a906..10af912 100644 --- a/synchronizer/committee_map_test.go +++ b/synchronizer/committee_map_test.go @@ -67,41 +67,35 @@ func TestStoreBatch(t *testing.T) { } } -func TestRange(t *testing.T) { +func TestAsSlice(t *testing.T) { committee := NewCommitteeMapSafe() - - members := []etherman.DataCommitteeMember{ - {Addr: common.HexToAddress("0x1"), URL: "Member 1"}, - {Addr: common.HexToAddress("0x2"), URL: "Member 2"}, - {Addr: common.HexToAddress("0x3"), URL: "Member 3"}, - } - - committee.StoreBatch(members) - - foundMembers := make(map[common.Address]etherman.DataCommitteeMember) - committee.Range(func(key common.Address, value etherman.DataCommitteeMember) bool { - foundMembers[key] = value - return true - }) - - require.Equal(t, len(members), len(foundMembers)) - for _, member := range members { - require.Equal(t, member, foundMembers[member.Addr]) + committee.StoreBatch( + []etherman.DataCommitteeMember{ + {Addr: common.HexToAddress("0x1"), URL: "Member 1"}, + {Addr: common.HexToAddress("0x2"), URL: "Member 2"}, + {Addr: common.HexToAddress("0x3"), URL: "Member 3"}, + {Addr: common.HexToAddress("0x4"), URL: "Member 4"}, + }) + + membersSlice := committee.AsSlice() + + require.Equal(t, committee.Length(), len(membersSlice)) + for _, member := range membersSlice { + foundMember, ok := committee.Load(member.Addr) + require.True(t, ok) + require.Equal(t, foundMember, member) } } -// Test for Length method func TestLength(t *testing.T) { committee := NewCommitteeMapSafe() members := []etherman.DataCommitteeMember{ {Addr: common.HexToAddress("0x1"), URL: "http://localhost:1001"}, {Addr: common.HexToAddress("0x2"), URL: "http://localhost:1002"}, - {Addr: common.HexToAddress("0x3"), URL: "http://localhost:1003"}, } committee.StoreBatch(members) - length := committee.Length() - require.Equal(t, len(members), length) + require.Equal(t, len(members), committee.Length()) }