Skip to content

Commit

Permalink
Implement AsSlice and remove Range function
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Jun 25, 2024
1 parent d12a236 commit b9fd7aa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 35 deletions.
7 changes: 2 additions & 5 deletions synchronizer/batches.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func (bs *BatchSynchronizer) resolveCommittee() error {
filteredMembers = append(filteredMembers, m)
}
}

bs.committee = NewCommitteeMapSafe()
bs.committee.StoreBatch(filteredMembers)
return nil
Expand Down Expand Up @@ -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)) {
Expand Down
13 changes: 6 additions & 7 deletions synchronizer/committee_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
40 changes: 17 additions & 23 deletions synchronizer/committee_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

0 comments on commit b9fd7aa

Please sign in to comment.