Skip to content

Commit

Permalink
use a set instead of map[T]struct{} (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkouv authored Nov 30, 2023
1 parent 7d332c9 commit d7b5f27
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 28 deletions.
15 changes: 8 additions & 7 deletions core/services/ocr2/plugins/ccip/config/type_and_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"

"github.com/Masterminds/semver/v3"
mapset "github.com/deckarep/golang-set/v2"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
Expand All @@ -18,12 +19,12 @@ var (
EVM2EVMOffRamp ContractType = "EVM2EVMOffRamp"
CommitStore ContractType = "CommitStore"
PriceRegistry ContractType = "PriceRegistry"
ContractTypes = map[ContractType]struct{}{
EVM2EVMOffRamp: {},
EVM2EVMOnRamp: {},
CommitStore: {},
PriceRegistry: {},
}
ContractTypes = mapset.NewSet[ContractType](
EVM2EVMOffRamp,
EVM2EVMOnRamp,
CommitStore,
PriceRegistry,
)
)

func VerifyTypeAndVersion(addr common.Address, client bind.ContractBackend, expectedType ContractType) (semver.Version, error) {
Expand Down Expand Up @@ -56,7 +57,7 @@ func TypeAndVersion(addr common.Address, client bind.ContractBackend) (ContractT
return "", semver.Version{}, err
}

if _, ok := ContractTypes[ContractType(contractType)]; !ok {
if !ContractTypes.Contains(ContractType(contractType)) {
return "", semver.Version{}, errors.Errorf("unrecognized contract type %v", contractType)
}
return ContractType(contractType), *v, nil
Expand Down
9 changes: 5 additions & 4 deletions core/services/ocr2/plugins/ccip/execution_reporting_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync"
"time"

mapset "github.com/deckarep/golang-set/v2"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -481,7 +482,7 @@ func (r *ExecutionReportingPlugin) buildBatch(
msgLggr.Infow("Skipping message already executed", "seqNr", msg.SequenceNumber)
continue
}
if _, isInflight := inflightSeqNrs[msg.SequenceNumber]; isInflight {
if inflightSeqNrs.Contains(msg.SequenceNumber) {
msgLggr.Infow("Skipping message already inflight", "seqNr", msg.SequenceNumber)
continue
}
Expand Down Expand Up @@ -1066,15 +1067,15 @@ func inflightAggregates(
inflight []InflightInternalExecutionReport,
destTokenPrices map[common.Address]*big.Int,
sourceToDest map[common.Address]common.Address,
) (map[uint64]struct{}, *big.Int, map[common.Address]uint64, map[common.Address]*big.Int, error) {
inflightSeqNrs := make(map[uint64]struct{})
) (mapset.Set[uint64], *big.Int, map[common.Address]uint64, map[common.Address]*big.Int, error) {
inflightSeqNrs := mapset.NewSet[uint64]()
inflightAggregateValue := big.NewInt(0)
maxInflightSenderNonces := make(map[common.Address]uint64)
inflightTokenAmounts := make(map[common.Address]*big.Int)

for _, rep := range inflight {
for _, message := range rep.messages {
inflightSeqNrs[message.SequenceNumber] = struct{}{}
inflightSeqNrs.Add(message.SequenceNumber)
msgValue, err := aggregateTokenValue(destTokenPrices, sourceToDest, message.TokenAmounts)
if err != nil {
return nil, nil, nil, nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/cometbft/cometbft/libs/rand"
mapset "github.com/deckarep/golang-set/v2"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"github.com/smartcontractkit/libocr/commontypes"
Expand Down Expand Up @@ -1425,7 +1426,7 @@ func Test_inflightAggregates(t *testing.T) {
destTokenPrices map[common.Address]*big.Int
sourceToDest map[common.Address]common.Address

expInflightSeqNrs map[uint64]struct{}
expInflightSeqNrs mapset.Set[uint64]
expInflightAggrVal *big.Int
expMaxInflightSenderNonces map[common.Address]uint64
expInflightTokenAmounts map[common.Address]*big.Int
Expand Down Expand Up @@ -1466,10 +1467,7 @@ func Test_inflightAggregates(t *testing.T) {
tokenAddrs[0]: tokenAddrs[1],
tokenAddrs[2]: tokenAddrs[3],
},
expInflightSeqNrs: map[uint64]struct{}{
100: {},
106: {},
},
expInflightSeqNrs: mapset.NewSet[uint64](100, 106),
expInflightAggrVal: big.NewInt(9*1000 + 5*500),
expMaxInflightSenderNonces: map[common.Address]uint64{
addrs[0]: 4,
Expand Down Expand Up @@ -1507,7 +1505,7 @@ func Test_inflightAggregates(t *testing.T) {
{
name: "nothing inflight",
inflight: []InflightInternalExecutionReport{},
expInflightSeqNrs: map[uint64]struct{}{},
expInflightSeqNrs: mapset.NewSet[uint64](),
expInflightAggrVal: big.NewInt(0),
expMaxInflightSenderNonces: map[common.Address]uint64{},
expInflightTokenAmounts: map[common.Address]*big.Int{},
Expand All @@ -1525,7 +1523,7 @@ func Test_inflightAggregates(t *testing.T) {
return
}
assert.NoError(t, err)
assert.True(t, reflect.DeepEqual(tc.expInflightSeqNrs, inflightSeqNrs))
assert.True(t, tc.expInflightSeqNrs.Equal(inflightSeqNrs))
assert.True(t, reflect.DeepEqual(tc.expInflightAggrVal, inflightAggrVal))
assert.True(t, reflect.DeepEqual(tc.expMaxInflightSenderNonces, maxInflightSenderNonces))
assert.True(t, reflect.DeepEqual(tc.expInflightTokenAmounts, inflightTokenAmounts))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync"
"time"

mapset "github.com/deckarep/golang-set/v2"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -176,12 +177,12 @@ func (o *OffRampV1_0_0) GetDestinationTokensFromSourceTokens(ctx context.Context
return nil, fmt.Errorf("parse outputs: %w", err)
}

seenDestTokens := make(map[common.Address]struct{})
seenDestTokens := mapset.NewSet[common.Address]()
for _, destToken := range destTokens {
if _, exists := seenDestTokens[destToken]; exists {
if seenDestTokens.Contains(destToken) {
return nil, fmt.Errorf("offRamp misconfig, destination token %s already exists", destToken)
}
seenDestTokens[destToken] = struct{}{}
seenDestTokens.Add(destToken)
}

return destTokens, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math/big"
"time"

mapset "github.com/deckarep/golang-set/v2"
"github.com/ethereum/go-ethereum/common"
"github.com/google/uuid"
"github.com/pkg/errors"
Expand Down Expand Up @@ -65,11 +66,7 @@ func (d *PipelineGetter) TokenPricesUSD(ctx context.Context, tokens []common.Add
return nil, errors.Errorf("expected map output of price pipeline, got %T", finalResult.Values[0])
}

providedTokensSet := make(map[common.Address]struct{}, len(tokens))
for _, token := range tokens {
providedTokensSet[token] = struct{}{}
}

providedTokensSet := mapset.NewSet[common.Address](tokens...)
tokenPrices := make(map[common.Address]*big.Int)
for tokenAddressStr, rawPrice := range prices {
castedPrice, err := parseutil.ParseBigIntFromAny(rawPrice)
Expand All @@ -78,7 +75,7 @@ func (d *PipelineGetter) TokenPricesUSD(ctx context.Context, tokens []common.Add
}

tokenAddress := common.HexToAddress(tokenAddressStr)
if _, exists := providedTokensSet[tokenAddress]; exists {
if providedTokensSet.Contains(tokenAddress) {
tokenPrices[tokenAddress] = castedPrice
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/cometbft/cometbft v0.37.2
github.com/cosmos/cosmos-sdk v0.47.4
github.com/danielkov/gin-helmet v0.0.0-20171108135313-1387e224435e
github.com/deckarep/golang-set/v2 v2.3.0
github.com/esote/minmaxheap v1.0.0
github.com/ethereum/go-ethereum v1.12.0
github.com/fatih/color v1.15.0
Expand Down Expand Up @@ -165,7 +166,6 @@ require (
github.com/danieljoos/wincred v1.1.2 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect
github.com/deckarep/golang-set/v2 v2.3.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/dfuse-io/logging v0.0.0-20210109005628-b97a57253f70 // indirect
github.com/dgraph-io/badger/v2 v2.2007.4 // indirect
Expand Down

0 comments on commit d7b5f27

Please sign in to comment.