Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanTinianov committed Dec 20, 2024
1 parent 5fcfde3 commit 7f778a6
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 26 deletions.
15 changes: 8 additions & 7 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,28 @@ linters:
enable:
- containedctx
- depguard
#- errorlint
- errname
- errorlint
- exhaustive
#- exportloopref
- exportloopref
- fatcontext
- ginkgolinter
#- gocritic
- gocritic
- goimports
#- gosec
- gosec
- loggercheck
- mirror
- misspell
- noctx
- nolintlint
#- prealloc
- prealloc
- revive
- rowserrcheck
- spancheck
- sqlclosecheck
#- testifylint
- testifylint
- unconvert
#- whitespace
- whitespace
linters-settings:
exhaustive:
default-signifies-exhaustive: true
Expand Down
2 changes: 1 addition & 1 deletion multinode/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (s syncStatus) String() string {
return "Synced"
}
var result bytes.Buffer
for i := syncStatusNotInSyncWithPool; i < syncStatusLen; i = i << 1 {
for i := syncStatusNotInSyncWithPool; i < syncStatusLen; i <<= 1 {
if i&s == 0 {
continue
}
Expand Down
6 changes: 3 additions & 3 deletions multinode/multi_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
Name: "multi_node_states",
Help: "The number of RPC nodes currently in the given state for the given chain",
}, []string{"network", "chainId", "state"})
ErroringNodeError = fmt.Errorf("no live nodes available")
ErrNodeError = fmt.Errorf("no live nodes available")
)

// MultiNode is a generalized multi node client interface that includes methods to interact with different chains.
Expand Down Expand Up @@ -120,7 +120,7 @@ func (c *MultiNode[CHAIN_ID, RPC]) DoAll(ctx context.Context, do func(ctx contex
}
}
if callsCompleted == 0 {
return ErroringNodeError
return ErrNodeError
}
return nil
})
Expand Down Expand Up @@ -217,7 +217,7 @@ func (c *MultiNode[CHAIN_ID, RPC]) selectNode() (node Node[CHAIN_ID, RPC], err e
if c.activeNode == nil {
c.lggr.Criticalw("No live RPC nodes available", "NodeSelectionMode", c.nodeSelector.Name())
c.eng.EmitHealthErr(fmt.Errorf("no live nodes available for chain %s", c.chainID.String()))
return nil, ErroringNodeError
return nil, ErrNodeError
}

c.lggr.Debugw("Switched to a new active node due to prev node heath issues", "prevNode", prevNodeName, "newNode", c.activeNode.String())
Expand Down
3 changes: 2 additions & 1 deletion multinode/multi_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func newNodeWithState(t *testing.T, chainID ID, state nodeState) *mockNode[ID, m
node.On("ConfiguredChainID").Return(chainID).Once()
node.On("Start", mock.Anything).Return(nil).Once()
node.On("Close").Return(nil).Once()
// #nosec G404
node.On("String").Return(fmt.Sprintf("healthy_node_%d", rand.Int())).Maybe()
node.On("SetPoolChainInfoProvider", mock.Anything).Once()
node.On("State").Return(state).Maybe()
Expand Down Expand Up @@ -390,7 +391,7 @@ func TestMultiNode_selectNode(t *testing.T) {
nodeSelector.On("Name").Return("MockedNodeSelector").Once()
mn.nodeSelector = nodeSelector
node, err := mn.selectNode()
require.EqualError(t, err, ErroringNodeError.Error())
require.EqualError(t, err, ErrNodeError.Error())
require.Nil(t, node)
tests.RequireLogMessage(t, observedLogs, "No live RPC nodes available")
})
Expand Down
4 changes: 2 additions & 2 deletions multinode/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ func NewNode[
func (n *node[CHAIN_ID, HEAD, RPC]) String() string {
s := fmt.Sprintf("(%s)%s", Primary.String(), n.name)
if n.ws != nil {
s = s + fmt.Sprintf(":%s", n.ws.String())
s += fmt.Sprintf(":%s", n.ws.String())
}
if n.http != nil {
s = s + fmt.Sprintf(":%s", n.http.String())
s += fmt.Sprintf(":%s", n.http.String())
}
return s
}
Expand Down
4 changes: 2 additions & 2 deletions multinode/node_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ func TestUnit_NodeLifecycle_start(t *testing.T) {
rpc.On("IsSyncing", mock.Anything).Return(false, errors.New("failed to check syncing status"))
rpc.On("Dial", mock.Anything).Return(errors.New("failed to redial"))
err := node.Start(tests.Context(t))
assert.NoError(t, err)
require.NoError(t, err)
tests.AssertLogEventually(t, observedLogs, "Unexpected error while verifying RPC node synchronization status")
tests.AssertEventually(t, func() bool {
return node.State() == nodeStateUnreachable
Expand Down Expand Up @@ -1579,7 +1579,7 @@ func TestUnit_NodeLifecycle_start(t *testing.T) {
setupRPCForAliveLoop(t, rpc)

err := node.Start(tests.Context(t))
assert.NoError(t, err)
require.NoError(t, err)
tests.AssertEventually(t, func() bool {
return node.State() == nodeStateAlive
})
Expand Down
7 changes: 4 additions & 3 deletions multinode/node_selector_highest_head_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ func TestHighestHeadNodeSelector(t *testing.T) {

for i := 0; i < 3; i++ {
node := newMockNode[ID, nodeClient](t)
if i == 0 {
switch i {
case 0:
// first node is out of sync
node.On("StateAndLatest").Return(nodeStateOutOfSync, ChainInfo{BlockNumber: int64(-1)})
} else if i == 1 {
case 1:
// second node is alive, LatestReceivedBlockNumber = 1
node.On("StateAndLatest").Return(nodeStateAlive, ChainInfo{BlockNumber: int64(1)})
} else {
default:
// third node is alive, LatestReceivedBlockNumber = 2 (best node)
node.On("StateAndLatest").Return(nodeStateAlive, ChainInfo{BlockNumber: int64(2)})
}
Expand Down
1 change: 1 addition & 0 deletions multinode/node_selector_priority_level.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (s priorityLevelNodeSelector[CHAIN_ID, RPC]) Select() Node[CHAIN_ID, RPC] {

// NOTE: Inc returns the number after addition, so we must -1 to get the "current" counter
count := s.roundRobinCount[priorityLevel].Add(1) - 1
// #nosec G115
idx := int(count % uint32(len(nodes)))

return nodes[idx].node
Expand Down
1 change: 1 addition & 0 deletions multinode/node_selector_round_robin.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (s *roundRobinSelector[CHAIN_ID, RPC]) Select() Node[CHAIN_ID, RPC] {

// NOTE: Inc returns the number after addition, so we must -1 to get the "current" counter
count := s.roundRobinCount.Add(1) - 1
// #nosec G115
idx := int(count % uint32(nNodes))

return liveNodes[idx]
Expand Down
2 changes: 1 addition & 1 deletion multinode/node_selector_total_difficulty.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (s totalDifficultyNodeSelector[CHAIN_ID, RPC]) Select() Node[CHAIN_ID, RPC]
// NodeNoNewHeadsThreshold may not be enabled, in this case all nodes have td == nil
var highestTD *big.Int
var nodes []Node[CHAIN_ID, RPC]
var aliveNodes []Node[CHAIN_ID, RPC]
var aliveNodes = make([]Node[CHAIN_ID, RPC], 0)

for _, n := range s {
state, currentChainInfo := n.StateAndLatest()
Expand Down
7 changes: 4 additions & 3 deletions multinode/node_selector_total_difficulty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ func TestTotalDifficultyNodeSelector(t *testing.T) {

for i := 0; i < 3; i++ {
node := newMockNode[ID, nodeClient](t)
if i == 0 {
switch i {
case 0:
// first node is out of sync
node.On("StateAndLatest").Return(nodeStateOutOfSync, ChainInfo{BlockNumber: -1})
} else if i == 1 {
case 1:
// second node is alive
node.On("StateAndLatest").Return(nodeStateAlive, ChainInfo{BlockNumber: 1, TotalDifficulty: big.NewInt(7)})
} else {
default:
// third node is alive and best
node.On("StateAndLatest").Return(nodeStateAlive, ChainInfo{BlockNumber: 2, TotalDifficulty: big.NewInt(8)})
}
Expand Down
2 changes: 1 addition & 1 deletion multinode/transaction_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (txSender *TransactionSender[TX, RESULT, CHAIN_ID, RPC]) SendTransaction(ct
}

if healthyNodesNum == 0 {
result = txSender.newResult(ErroringNodeError)
result = txSender.newResult(ErrNodeError)
return
}

Expand Down
4 changes: 2 additions & 2 deletions multinode/transaction_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestTransactionSender_SendTransaction(t *testing.T) {
lggr := logger.Test(t)
_, txSender := newTestTransactionSender(t, RandomID(), lggr, nil, nil)
result := txSender.SendTransaction(tests.Context(t), nil)
assert.EqualError(t, result.Error(), ErroringNodeError.Error())
assert.EqualError(t, result.Error(), ErrNodeError.Error())
})

t.Run("Transaction failure happy path", func(t *testing.T) {
Expand Down Expand Up @@ -269,7 +269,7 @@ func TestTransactionSender_SendTransaction(t *testing.T) {
[]SendOnlyNode[ID, TestSendTxRPCClient]{sendOnly})

result := txSender.SendTransaction(tests.Context(t), nil)
assert.EqualError(t, result.Error(), ErroringNodeError.Error())
assert.EqualError(t, result.Error(), ErrNodeError.Error())
})

t.Run("Transaction success even if one of the nodes is unhealthy", func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions multinode/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

func RandomID() ID {
// #nosec G404
id := rand.Int63n(math.MaxInt32) + 10000
return big.NewInt(id)
}
Expand Down

0 comments on commit 7f778a6

Please sign in to comment.