From c4b504e97ffb0d1eed733418a1638fba43711035 Mon Sep 17 00:00:00 2001 From: Tomasz Slabon Date: Fri, 3 May 2024 18:10:10 +0200 Subject: [PATCH] Fixed issues related to launching inactivity submission --- pkg/chain/ethereum/tbtc.go | 20 ++++++++++++++++++++ pkg/tbtc/chain.go | 19 +++++++++++++++++-- pkg/tbtc/chain_test.go | 6 ++++++ pkg/tbtc/inactivity.go | 31 +++++++++++++++++++++++++++---- pkg/tbtc/inactivity_submit.go | 2 +- pkg/tbtc/node.go | 3 ++- 6 files changed, 73 insertions(+), 8 deletions(-) diff --git a/pkg/chain/ethereum/tbtc.go b/pkg/chain/ethereum/tbtc.go index 65146c71e7..99af445809 100644 --- a/pkg/chain/ethereum/tbtc.go +++ b/pkg/chain/ethereum/tbtc.go @@ -994,6 +994,26 @@ func (tc *TbtcChain) DKGParameters() (*tbtc.DKGParameters, error) { }, nil } +func (tc *TbtcChain) OnInactivityClaimed( + handler func(event *tbtc.InactivityClaimedEvent), +) subscription.EventSubscription { + onEvent := func( + walletID [32]byte, + nonce *big.Int, + notifier common.Address, + blockNumber uint64, + ) { + handler(&tbtc.InactivityClaimedEvent{ + WalletID: walletID, + Nonce: nonce, + Notifier: chain.Address(notifier.Hex()), + BlockNumber: blockNumber, + }) + } + + return tc.walletRegistry.InactivityClaimedEvent(nil, nil).OnEvent(onEvent) +} + func (tc *TbtcChain) AssembleInactivityClaim( walletID [32]byte, inactiveMembersIndices []group.MemberIndex, diff --git a/pkg/tbtc/chain.go b/pkg/tbtc/chain.go index fd548b7ffd..f156ae68d4 100644 --- a/pkg/tbtc/chain.go +++ b/pkg/tbtc/chain.go @@ -121,6 +121,15 @@ type DistributedKeyGenerationChain interface { DKGParameters() (*DKGParameters, error) } +// InactivityClaimedEvent represents an inactivity claimed event. It is emitted +// after a submitted inactivity claim lands on the chain. +type InactivityClaimedEvent struct { + WalletID [32]byte + Nonce *big.Int + Notifier chain.Address + BlockNumber uint64 +} + // InactivityChainClaim represents an inactivity claim submitted to the chain. type InactivityChainClaim struct { WalletID [32]byte @@ -131,8 +140,14 @@ type InactivityChainClaim struct { } type InactivityClaimChain interface { - // AssembleDKGResult assembles the inactivity chain claim according to the - // rules expected by the given chain. + // OnInactivityClaimed registers a callback that is invoked when an on-chain + // notification of the inactivity claim submission is seen. + OnInactivityClaimed( + func(event *InactivityClaimedEvent), + ) subscription.EventSubscription + + // AssembleInactivityClaim assembles the inactivity chain claim according to + // the rules expected by the given chain. AssembleInactivityClaim( walletID [32]byte, inactiveMembersIndices []group.MemberIndex, diff --git a/pkg/tbtc/chain_test.go b/pkg/tbtc/chain_test.go index da2efc4ec9..f4b7463243 100644 --- a/pkg/tbtc/chain_test.go +++ b/pkg/tbtc/chain_test.go @@ -552,6 +552,12 @@ func (lc *localChain) DKGParameters() (*DKGParameters, error) { }, nil } +func (lc *localChain) OnInactivityClaimed( + func(event *InactivityClaimedEvent), +) subscription.EventSubscription { + panic("unsupported") +} + func (lc *localChain) AssembleInactivityClaim( walletID [32]byte, inactiveMembersIndices []group.MemberIndex, diff --git a/pkg/tbtc/inactivity.go b/pkg/tbtc/inactivity.go index 937d02ada9..fd5a175457 100644 --- a/pkg/tbtc/inactivity.go +++ b/pkg/tbtc/inactivity.go @@ -88,8 +88,12 @@ func (ice *inactivityClaimExecutor) publishClaim( return fmt.Errorf("cannot marshal wallet public key: [%v]", err) } + timeoutBlock := startBlock + inactivityClaimMaximumSubmissionBlocks + execLogger := logger.With( zap.String("wallet", fmt.Sprintf("0x%x", walletPublicKeyBytes)), + zap.Uint64("inactivityClaimStartBlock", startBlock), + zap.Uint64("inactivityClaimTimeoutBlock", timeoutBlock), ) walletRegistryData, err := ice.chain.GetWallet(walletPublicKeyHash) @@ -125,16 +129,36 @@ func (ice *inactivityClaimExecutor) publishClaim( defer wg.Done() - inactivityClaimTimeoutBlock := startBlock + inactivityClaimMaximumSubmissionBlocks - go func(signer *signer) { + execLogger.Info( + "[member:%v] starting inactivity claim publishing", + signer.signingGroupMemberIndex, + ) + ctx, cancelCtx := withCancelOnBlock( context.Background(), - inactivityClaimTimeoutBlock, + timeoutBlock, ice.waitForBlockFn, ) defer cancelCtx() + subscription := ice.chain.OnInactivityClaimed( + func(event *InactivityClaimedEvent) { + defer cancelCtx() + + execLogger.Infof( + "[member:%v] Inactivity claim submitted for wallet "+ + "with ID [0x%x] and nonce [%v] by notifier [%v] "+ + "at block [%v]", + signer.signingGroupMemberIndex, + event.WalletID, + event.Nonce, + event.Notifier, + event.BlockNumber, + ) + }) + defer subscription.Unsubscribe() + err := ice.publish( ctx, execLogger, @@ -148,7 +172,6 @@ func (ice *inactivityClaimExecutor) publishClaim( ice.membershipValidator, claim, ) - if err != nil { if errors.Is(err, context.Canceled) { execLogger.Infof( diff --git a/pkg/tbtc/inactivity_submit.go b/pkg/tbtc/inactivity_submit.go index 21879ed6d4..2058142948 100644 --- a/pkg/tbtc/inactivity_submit.go +++ b/pkg/tbtc/inactivity_submit.go @@ -177,7 +177,7 @@ func (ics *inactivityClaimSubmitter) SubmitClaim( err = ics.waitForBlockFn(ctx, submissionBlock) if err != nil { return fmt.Errorf( - "error while waiting for DKG result submission block: [%v]", + "error while waiting for inactivity claim submission block: [%v]", err, ) } diff --git a/pkg/tbtc/node.go b/pkg/tbtc/node.go index 9a6121ed93..f916ae4159 100644 --- a/pkg/tbtc/node.go +++ b/pkg/tbtc/node.go @@ -134,6 +134,7 @@ func newNode( protocolLatch: latch, heartbeatFailureCounters: make(map[string]*uint), signingExecutors: make(map[string]*signingExecutor), + inactivityClaimExecutors: make(map[string]*inactivityClaimExecutor), coordinationExecutors: make(map[string]*coordinationExecutor), proposalGenerator: proposalGenerator, } @@ -512,7 +513,7 @@ func (n *node) getInactivityClaimExecutor( } executorLogger.Infof( - "signing executor created; controlling [%v] signers", + "inactivity executor created; controlling [%v] signers", len(signers), )