Skip to content

Commit

Permalink
node: Bug fix in changes to governor monitoring
Browse files Browse the repository at this point in the history
- Fix issue where stats weren't being populated unless flow cancel was
  enabled
- Fix wrong return value used in unit test
- Fix typo in proto variable name
- Move sorting outside of a for loop for efficiency
- Restore unit test that was deleted in the process of rebasing
  • Loading branch information
johnsaigle committed Jul 17, 2024
1 parent c6bc141 commit 8642348
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 63 deletions.
33 changes: 17 additions & 16 deletions node/pkg/governor/governor_monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,13 @@ func sumValue(transfers []transfer, startTime time.Time) (netNotional int64, sma
if t.dbTransfer.Timestamp.Before(startTime) {
continue
}
checkedSum, err := CheckedAddInt64(netNotional, t.value)
netNotional, err = CheckedAddInt64(netNotional, t.value)
if err != nil {
// We have to stop and return an error here (rather than saturate, for example). The
// transfers are not sorted by value so we can't make any guarantee on the final value
// if we hit the upper or lower bound. We don't expect this to happen in any case.
return 0, 0, 0, err
}
netNotional = checkedSum
if t.value < 0 {
// If a transfer is negative then it is an incoming, flow-cancelling transfer.
// We can use the dbTransfer.Value for calculating the sum because it is the unsigned version
Expand Down Expand Up @@ -338,19 +337,21 @@ func (gov *ChainGovernor) GetAvailableNotionalByChain() (resp []*publicrpcv1.Gov
zap.Error(err))
}

resp = append(resp, &publicrpcv1.GovernorGetAvailableNotionalByChainResponse_Entry{
ChainId: uint32(ce.emitterChainId),
RemainingAvailableNotional: remaining,
NotionalLimit: ce.dailyLimit,
BigTransactionSize: ce.bigTransactionSize,
})
}

sort.SliceStable(resp, func(i, j int) bool {
return (resp[i].ChainId < resp[j].ChainId)
resp = append(resp, &publicrpcv1.GovernorGetAvailableNotionalByChainResponse_Entry{
ChainId: uint32(ce.emitterChainId),
RemainingAvailableNotional: remaining,
NotionalLimit: ce.dailyLimit,
BigTransactionSize: ce.bigTransactionSize,
})

}

sort.SliceStable(resp, func(i, j int) bool {
return (resp[i].ChainId < resp[j].ChainId)
})

return resp
}

Expand Down Expand Up @@ -649,12 +650,12 @@ func (gov *ChainGovernor) publishStatus(hb *gossipv1.Heartbeat, sendC chan<- []b
}

emitter := gossipv1.ChainGovernorStatus_Emitter{
EmitterAddress: "0x" + ce.emitterAddr.String(),
TotalEnqueuedVaas: uint64(len(ce.pending)),
EnqueuedVaas: enqueuedVaas,
SmallTxNetNotionalValue: netUsage,
SmallTxOutgingNotionalValue: smallTxNotional,
FlowCancelNotionalValue: flowCancelNotional,
EmitterAddress: "0x" + ce.emitterAddr.String(),
TotalEnqueuedVaas: uint64(len(ce.pending)),
EnqueuedVaas: enqueuedVaas,
SmallTxNetNotionalValue: netUsage,
SmallTxOutgoingNotionalValue: smallTxNotional,
FlowCancelNotionalValue: flowCancelNotional,
}

chains = append(chains, &gossipv1.ChainGovernorStatus_Chain{
Expand Down
65 changes: 64 additions & 1 deletion node/pkg/governor/governor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2116,6 +2116,69 @@ func TestTestnetConfigIsValid(t *testing.T) {
require.NoError(t, err)
}

func TestNumDaysForReleaseTimerReset(t *testing.T) {
ctx := context.Background()
gov, err := newChainGovernorForTest(ctx)

require.NoError(t, err)

tokenAddrStr := "0xDDb64fE46a91D46ee29420539FC25FD07c5FEa3E" //nolint:gosec
toAddrStr := "0x707f9118e33a9b8998bea41dd0d46f38bb963fc8"
tokenBridgeAddrStr := "0x0290fb167208af455bb137780163b7b7a9a10c16" //nolint:gosec
tokenBridgeAddr, err := vaa.StringToAddress(tokenBridgeAddrStr)
require.NoError(t, err)

gov.setDayLengthInMinutes(24 * 60)
err = gov.setChainForTesting(vaa.ChainIDEthereum, tokenBridgeAddrStr, 1000000, 100000)
require.NoError(t, err)
err = gov.setTokenForTesting(vaa.ChainIDEthereum, tokenAddrStr, "WETH", 1774.62, false)
require.NoError(t, err)

now := time.Now()
messageTimestamp := now.Add(-5) // 5 seconds ago

// message that, when processed, should exceed the big transfer size
msg := common.MessagePublication{
TxHash: hashFromString("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"),
Timestamp: messageTimestamp,
Nonce: uint32(1),
Sequence: uint64(3),
EmitterChain: vaa.ChainIDEthereum,
EmitterAddress: tokenBridgeAddr,
ConsistencyLevel: uint8(32),
Payload: buildMockTransferPayloadBytes(1,
vaa.ChainIDEthereum,
tokenAddrStr,
vaa.ChainIDPolygon,
toAddrStr,
100,
),
}

canPost, err := gov.ProcessMsgForTime(&msg, now)
require.NoError(t, err)
assert.Equal(t, false, canPost)

msg.MessageIDString()

// check that the enqueued vaa's release date is now + 1 day
expectedReleaseTime := uint32(now.Add(24 * time.Hour).Unix())
enqueuedVaas := gov.GetEnqueuedVAAs()
assert.Equal(t, len(enqueuedVaas), 1)
assert.Equal(t, enqueuedVaas[0].ReleaseTime, expectedReleaseTime)

// the release timer gets reset to 5 days
_, err = gov.resetReleaseTimerForTime(msg.MessageIDString(), now, 5)
require.NoError(t, err)

// check that the enqueued vaa's release date is now + 5 days
enqueuedVaas = gov.GetEnqueuedVAAs()
assert.Equal(t, len(enqueuedVaas), 1)
expectedReleaseTime = uint32(now.Add(5 * 24 * time.Hour).Unix())
assert.Equal(t, enqueuedVaas[0].ReleaseTime, expectedReleaseTime)

}

func TestLargeTransactionGetsEnqueuedAndReleasedWhenTheTimerExpires(t *testing.T) {
ctx := context.Background()
gov, err := newChainGovernorForTest(ctx)
Expand Down Expand Up @@ -2322,7 +2385,7 @@ func TestLargeTransactionGetsEnqueuedAndReleasedWhenTheTimerExpires(t *testing.T
// But the big transaction should not affect the daily notional.
ce, exists := gov.chains[vaa.ChainIDEthereum]
require.Equal(t, true, exists)
_, _, outgoing, err := sumValue(ce.transfers, now)
_, outgoing, _, err := sumValue(ce.transfers, now)
require.NoError(t, err)
assert.Equal(t, uint64(0), outgoing)
}
Expand Down
92 changes: 46 additions & 46 deletions node/pkg/proto/gossip/v1/gossip.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8642348

Please sign in to comment.