From a2923f1185294e1c82abba5302eb2471f37fb3f7 Mon Sep 17 00:00:00 2001 From: Tomasz Slabon Date: Wed, 24 Apr 2024 19:47:25 +0200 Subject: [PATCH] Implemented inactivity claim submitter --- pkg/tbtc/inactivity.go | 8 +++-- pkg/tbtc/inactivity_submit.go | 55 +++++++++++++++++++++++++++++++---- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/pkg/tbtc/inactivity.go b/pkg/tbtc/inactivity.go index 99827853e3..6ad5896125 100644 --- a/pkg/tbtc/inactivity.go +++ b/pkg/tbtc/inactivity.go @@ -30,7 +30,7 @@ type inactivityClaimExecutor struct { broadcastChannel net.BroadcastChannel membershipValidator *group.MembershipValidator groupParameters *GroupParameters - protocolLatch *generator.ProtocolLatch + protocolLatch *generator.ProtocolLatch waitForBlockFn waitForBlockFn } @@ -159,7 +159,11 @@ func (ice *inactivityClaimExecutor) publish( dishonestThreshold, membershipValidator, newInactivityClaimSigner(ice.chain), - newInactivityClaimSubmitter(), + newInactivityClaimSubmitter( + inactivityLogger, + ice.chain, + ice.groupParameters, + ), inactivityClaim, ) } diff --git a/pkg/tbtc/inactivity_submit.go b/pkg/tbtc/inactivity_submit.go index 8e7ad856a4..9dc0688083 100644 --- a/pkg/tbtc/inactivity_submit.go +++ b/pkg/tbtc/inactivity_submit.go @@ -4,6 +4,8 @@ import ( "context" "fmt" + "github.com/ipfs/go-log/v2" + "github.com/keep-network/keep-core/pkg/bitcoin" "github.com/keep-network/keep-core/pkg/protocol/group" "github.com/keep-network/keep-core/pkg/tecdsa/inactivity" ) @@ -71,12 +73,22 @@ func (ics *inactivityClaimSigner) VerifySignature( } type inactivityClaimSubmitter struct { - // TODO: Implement + inactivityLogger log.StandardLogger + + chain Chain + groupParameters *GroupParameters } -func newInactivityClaimSubmitter() *inactivityClaimSubmitter { - // TODO: Implement - return &inactivityClaimSubmitter{} +func newInactivityClaimSubmitter( + inactivityLogger log.StandardLogger, + chain Chain, + groupParameters *GroupParameters, +) *inactivityClaimSubmitter { + return &inactivityClaimSubmitter{ + inactivityLogger: inactivityLogger, + chain: chain, + groupParameters: groupParameters, + } } func (ics *inactivityClaimSubmitter) SubmitClaim( @@ -85,6 +97,39 @@ func (ics *inactivityClaimSubmitter) SubmitClaim( claim *inactivity.Claim, signatures map[group.MemberIndex][]byte, ) error { - // TODO: Implement + if len(signatures) < ics.groupParameters.HonestThreshold { + return fmt.Errorf( + "could not submit inactivity claim with [%v] signatures for "+ + "group honest threshold [%v]", + len(signatures), + ics.groupParameters.HonestThreshold, + ) + } + + walletPublicKeyHash := bitcoin.PublicKeyHash(claim.WalletPublicKey) + + walletRegistryData, err := ics.chain.GetWallet(walletPublicKeyHash) + if err != nil { + return fmt.Errorf("could not get registry data on wallet: [%v]", err) + } + + currentNonce, err := ics.chain.GetInactivityClaimNonce( + walletRegistryData.EcdsaWalletID, + ) + if err != nil { + return fmt.Errorf("could not get nonce for wallet: [%v]", err) + } + + if currentNonce.Cmp(claim.Nonce) > 0 { + // Someone who was ahead of us in the queue submitted the claim. Giving up. + ics.inactivityLogger.Infof( + "[member:%v] inactivity claim already submitted; "+ + "aborting inactivity claim on-chain submission", + memberIndex, + ) + return nil + } + + // TODO: Continue with the implementation return nil }