Skip to content

Commit

Permalink
Fixed issues related to launching inactivity submission
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed May 3, 2024
1 parent 4db1804 commit c4b504e
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 8 deletions.
20 changes: 20 additions & 0 deletions pkg/chain/ethereum/tbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 17 additions & 2 deletions pkg/tbtc/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions pkg/tbtc/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 27 additions & 4 deletions pkg/tbtc/inactivity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -148,7 +172,6 @@ func (ice *inactivityClaimExecutor) publishClaim(
ice.membershipValidator,
claim,
)

if err != nil {
if errors.Is(err, context.Canceled) {
execLogger.Infof(
Expand Down
2 changes: 1 addition & 1 deletion pkg/tbtc/inactivity_submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/tbtc/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -512,7 +513,7 @@ func (n *node) getInactivityClaimExecutor(
}

executorLogger.Infof(
"signing executor created; controlling [%v] signers",
"inactivity executor created; controlling [%v] signers",
len(signers),
)

Expand Down

0 comments on commit c4b504e

Please sign in to comment.