From 7c94b9444a21bcc46049b77887edbd763bfe0186 Mon Sep 17 00:00:00 2001 From: Lukasz Zimnoch Date: Tue, 3 Nov 2020 11:45:35 +0100 Subject: [PATCH 01/15] Make `waitForChainConfirmation` function public Function `waitForChainConfirmation` has been moved to a separate file and made public. This is because it can be reused in some other places. --- pkg/chain/chain.go | 3 +++ pkg/chain/confirmation.go | 32 +++++++++++++++++++++++++++++ pkg/client/client.go | 42 +++++++++----------------------------- pkg/client/registration.go | 3 ++- 4 files changed, 47 insertions(+), 33 deletions(-) create mode 100644 pkg/chain/confirmation.go diff --git a/pkg/chain/chain.go b/pkg/chain/chain.go index 5036c7bd4..c9382daa3 100644 --- a/pkg/chain/chain.go +++ b/pkg/chain/chain.go @@ -7,11 +7,14 @@ import ( "time" "github.com/ethereum/go-ethereum/common" + "github.com/ipfs/go-log" "github.com/keep-network/keep-common/pkg/subscription" "github.com/keep-network/keep-core/pkg/chain" "github.com/keep-network/keep-ecdsa/pkg/ecdsa" ) +var logger = log.Logger("keep-chain") + // Handle represents a handle to an ethereum blockchain. type Handle interface { // Address returns client's ethereum address. diff --git a/pkg/chain/confirmation.go b/pkg/chain/confirmation.go new file mode 100644 index 000000000..7f4c983b1 --- /dev/null +++ b/pkg/chain/confirmation.go @@ -0,0 +1,32 @@ +package eth + +import ( + "fmt" +) + +// WaitForChainConfirmation ensures that after receiving specific number of block +// confirmations the state of the chain is actually as expected. It waits for +// predefined number of blocks since the start block number provided. After the +// required block number is reached it performs a check of the chain state with +// a provided function returning a boolean value. +func WaitForChainConfirmation( + ethereumChain Handle, + startBlockNumber uint64, + blockConfirmations uint64, + stateCheck func() (bool, error), +) (bool, error) { + blockHeight := startBlockNumber + blockConfirmations + logger.Infof("waiting for block [%d] to confirm chain state", blockHeight) + + err := ethereumChain.BlockCounter().WaitForBlockHeight(blockHeight) + if err != nil { + return false, fmt.Errorf("failed to wait for block height: [%v]", err) + } + + result, err := stateCheck() + if err != nil { + return false, fmt.Errorf("failed to get chain state confirmation: [%v]", err) + } + + return result, nil +} diff --git a/pkg/client/client.go b/pkg/client/client.go index 56955e6bc..8dd833edc 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -3,7 +3,6 @@ package client import ( "context" - "fmt" "math/big" "sync" "time" @@ -60,9 +59,10 @@ func Initialize( return false } - isKeepActive, err := waitForChainConfirmation( + isKeepActive, err := eth.WaitForChainConfirmation( ethereumChain, currentBlock, + blockConfirmations, func() (bool, error) { return ethereumChain.IsActive(keepAddress) }, @@ -531,9 +531,10 @@ func monitorSigningRequests( } defer requestedSignatures.remove(keepAddress, event.Digest) - isAwaitingSignature, err := waitForChainConfirmation( + isAwaitingSignature, err := eth.WaitForChainConfirmation( ethereumChain, event.BlockNumber, + blockConfirmations, func() (bool, error) { return ethereumChain.IsAwaitingSignature(keepAddress, event.Digest) }, @@ -618,9 +619,10 @@ func checkAwaitingSignature( return } - isStillAwaitingSignature, err := waitForChainConfirmation( + isStillAwaitingSignature, err := eth.WaitForChainConfirmation( ethereumChain, startBlock, + blockConfirmations, func() (bool, error) { isAwaitingSignature, err := ethereumChain.IsAwaitingSignature(keepAddress, latestDigest) if err != nil { @@ -704,9 +706,10 @@ func monitorKeepClosedEvents( event.BlockNumber, ) - isKeepActive, err := waitForChainConfirmation( + isKeepActive, err := eth.WaitForChainConfirmation( ethereumChain, event.BlockNumber, + blockConfirmations, func() (bool, error) { return ethereumChain.IsActive(keepAddress) }, @@ -767,9 +770,10 @@ func monitorKeepTerminatedEvent( event.BlockNumber, ) - isKeepActive, err := waitForChainConfirmation( + isKeepActive, err := eth.WaitForChainConfirmation( ethereumChain, event.BlockNumber, + blockConfirmations, func() (bool, error) { return ethereumChain.IsActive(keepAddress) }, @@ -809,29 +813,3 @@ func monitorKeepTerminatedEvent( logger.Info("unsubscribing from events on keep terminated") } - -// waitForChainConfirmation ensures that after receiving specific number of block -// confirmations the state of the chain is actually as expected. It waits for -// predefined number of blocks since the start block number provided. After the -// required block number is reached it performs a check of the chain state with -// a provided function returning a boolean value. -func waitForChainConfirmation( - ethereumChain eth.Handle, - startBlockNumber uint64, - stateCheck func() (bool, error), -) (bool, error) { - blockHeight := startBlockNumber + blockConfirmations - logger.Infof("waiting for block [%d] to confirm chain state", blockHeight) - - err := ethereumChain.BlockCounter().WaitForBlockHeight(blockHeight) - if err != nil { - return false, fmt.Errorf("failed to wait for block height: [%v]", err) - } - - result, err := stateCheck() - if err != nil { - return false, fmt.Errorf("failed to get chain state confirmation: [%v]", err) - } - - return result, nil -} diff --git a/pkg/client/registration.go b/pkg/client/registration.go index 8d07f18cc..8e423065f 100644 --- a/pkg/client/registration.go +++ b/pkg/client/registration.go @@ -286,9 +286,10 @@ func monitorSignerPoolStatus( ) } - isRegistered, err := waitForChainConfirmation( + isRegistered, err := eth.WaitForChainConfirmation( ethereumChain, statusCheckBlock, + blockConfirmations, func() (bool, error) { return ethereumChain.IsRegisteredForApplication( application, From 7cde7c9df7af326613ded48444417cce0fae93df Mon Sep 17 00:00:00 2001 From: Lukasz Zimnoch Date: Tue, 3 Nov 2020 13:41:50 +0100 Subject: [PATCH 02/15] Add confirmations to retrieve pubkey monitoring Implemented deposit state confirmations upon monitoring stop event and action execution for the retrieve pubkey monitoring. --- pkg/chain/ethereum/tbtc.go | 17 +++++ pkg/chain/local/tbtc.go | 21 ++++++ pkg/chain/tbtc.go | 21 ++++++ pkg/extensions/tbtc/tbtc.go | 108 ++++++++++++++++++++++++++++--- pkg/extensions/tbtc/tbtc_test.go | 28 +++++++- 5 files changed, 184 insertions(+), 11 deletions(-) diff --git a/pkg/chain/ethereum/tbtc.go b/pkg/chain/ethereum/tbtc.go index e681c3c7a..a156e0285 100644 --- a/pkg/chain/ethereum/tbtc.go +++ b/pkg/chain/ethereum/tbtc.go @@ -364,6 +364,23 @@ func (tec *TBTCEthereumChain) ProvideRedemptionProof( return nil } +// CurrentState returns the current state for the provided deposit. +func (tec *TBTCEthereumChain) CurrentState( + depositAddress string, +) (chain.DepositState, error) { + deposit, err := tec.getDepositContract(depositAddress) + if err != nil { + return 0, err + } + + state, err := deposit.CurrentState() + if err != nil { + return 0, err + } + + return chain.DepositState(state.Uint64()), err +} + func (tec *TBTCEthereumChain) getDepositContract( address string, ) (*contract.Deposit, error) { diff --git a/pkg/chain/local/tbtc.go b/pkg/chain/local/tbtc.go index ae1504ea3..07bd24fc5 100644 --- a/pkg/chain/local/tbtc.go +++ b/pkg/chain/local/tbtc.go @@ -22,6 +22,7 @@ const ( type localDeposit struct { keepAddress string pubkey []byte + state chain.DepositState utxoValue *big.Int redemptionDigest [32]byte @@ -114,6 +115,7 @@ func (tlc *TBTCLocalChain) CreateDeposit(depositAddress string) { tlc.deposits[depositAddress] = &localDeposit{ keepAddress: keepAddress.Hex(), + state: chain.AwaitingSignerSetup, utxoValue: big.NewInt(defaultUTXOValue), redemptionRequestedEvents: make([]*chain.DepositRedemptionRequestedEvent, 0), } @@ -188,6 +190,7 @@ func (tlc *TBTCLocalChain) RedeemDeposit(depositAddress string) error { return err } + deposit.state = chain.AwaitingWithdrawalSignature deposit.redemptionDigest = randomDigest deposit.redemptionFee = big.NewInt(defaultInitialRedemptionFee) @@ -346,6 +349,7 @@ func (tlc *TBTCLocalChain) RetrieveSignerPubkey(depositAddress string) error { } deposit.pubkey = keep.publicKey[:] + deposit.state = chain.AwaitingBtcFundingProof for _, handler := range tlc.depositRegisteredPubkeyHandlers { go func(handler func(depositAddress string), depositAddress string) { @@ -387,6 +391,7 @@ func (tlc *TBTCLocalChain) ProvideRedemptionSignature( ) } + deposit.state = chain.AwaitingWithdrawalProof deposit.redemptionSignature = &Signature{ V: v, R: r, @@ -454,6 +459,7 @@ func (tlc *TBTCLocalChain) IncreaseRedemptionFee( return err } + deposit.state = chain.AwaitingWithdrawalSignature deposit.redemptionDigest = randomDigest deposit.redemptionFee = new(big.Int).Sub(deposit.utxoValue, newOutputValue) deposit.redemptionSignature = nil @@ -518,6 +524,7 @@ func (tlc *TBTCLocalChain) ProvideRedemptionProof( ) } + deposit.state = chain.Redeemed deposit.redemptionProof = &TxProof{} for _, handler := range tlc.depositRedeemedHandlers { @@ -529,6 +536,20 @@ func (tlc *TBTCLocalChain) ProvideRedemptionProof( return nil } +func (tlc *TBTCLocalChain) CurrentState( + depositAddress string, +) (chain.DepositState, error) { + tlc.mutex.Lock() + defer tlc.mutex.Unlock() + + deposit, ok := tlc.deposits[depositAddress] + if !ok { + return 0, fmt.Errorf("no deposit with address [%v]", depositAddress) + } + + return deposit.state, nil +} + func (tlc *TBTCLocalChain) DepositPubkey( depositAddress string, ) ([]byte, error) { diff --git a/pkg/chain/tbtc.go b/pkg/chain/tbtc.go index ee248a1c7..3e110365c 100644 --- a/pkg/chain/tbtc.go +++ b/pkg/chain/tbtc.go @@ -54,6 +54,9 @@ type Deposit interface { txIndexInBlock *big.Int, bitcoinHeaders []uint8, ) error + + // CurrentState returns the current state for the provided deposit. + CurrentState(depositAddress string) (DepositState, error) } // TBTCSystem is an interface that provides ability to interact @@ -112,3 +115,21 @@ type DepositRedemptionRequestedEvent struct { Outpoint []byte BlockNumber uint64 } + +// DepositState represents the deposit state. +type DepositState int + +const ( + Start DepositState = iota + AwaitingSignerSetup + AwaitingBtcFundingProof + FailedSetup + Active + AwaitingWithdrawalSignature + AwaitingWithdrawalProof + Redeemed + CourtesyCall + FraudLiquidationInProgress + LiquidationInProgress + Liquidated +) diff --git a/pkg/extensions/tbtc/tbtc.go b/pkg/extensions/tbtc/tbtc.go index c0a135b7a..6c10da35d 100644 --- a/pkg/extensions/tbtc/tbtc.go +++ b/pkg/extensions/tbtc/tbtc.go @@ -20,8 +20,9 @@ import ( var logger = log.Logger("tbtc-extension") const ( - maxActAttempts = 3 - pastEventsLookbackBlocks = 10000 + maxActAttempts = 3 + pastEventsLookbackBlocks = 10000 + defaultBlockConfirmations = 12 ) type KeepsRegistry interface { @@ -42,6 +43,7 @@ func Initialize( ctx, exponentialBackoff, 165*time.Minute, // 15 minutes before the 3 hours on-chain timeout + defaultBlockConfirmations, ) if err != nil { return fmt.Errorf( @@ -54,6 +56,7 @@ func Initialize( ctx, exponentialBackoff, 105*time.Minute, // 15 minutes before the 2 hours on-chain timeout + defaultBlockConfirmations, ) if err != nil { return fmt.Errorf( @@ -67,6 +70,7 @@ func Initialize( ctx, exponentialBackoff, 345*time.Minute, // 15 minutes before the 6 hours on-chain timeout + defaultBlockConfirmations, ) if err != nil { return fmt.Errorf( @@ -99,19 +103,58 @@ func (t *tbtc) monitorRetrievePubKey( ctx context.Context, actBackoffFn backoffFn, timeout time.Duration, + blockConfirmations uint64, ) error { + monitoringStartFn := func( + handler depositEventHandler, + ) (subscription.EventSubscription, error) { + return t.chain.OnDepositCreated(handler) + } + + monitoringStopFn := func( + handler depositEventHandler, + ) (subscription.EventSubscription, error) { + handlerWithConfirmation := func(depositAddress string) { + if t.waitDepositStateConfirmation( + depositAddress, + chain.AwaitingBtcFundingProof, + blockConfirmations, + ) { + handler(depositAddress) + } + } + + return t.chain.OnDepositRegisteredPubkey(handlerWithConfirmation) + } + + actFn := func(depositAddress string) error { + err := t.chain.RetrieveSignerPubkey(depositAddress) + if err != nil { + return err + } + + if !t.waitDepositStateConfirmation( + depositAddress, + chain.AwaitingBtcFundingProof, + blockConfirmations, + ) { + return fmt.Errorf( + "deposit [%v] is not in desired state", + depositAddress, + ) + } + + return nil + } + monitoringSubscription, err := t.monitorAndAct( ctx, "retrieve pubkey", t.shouldMonitorDeposit, - func(handler depositEventHandler) (subscription.EventSubscription, error) { - return t.chain.OnDepositCreated(handler) - }, - func(handler depositEventHandler) (subscription.EventSubscription, error) { - return t.chain.OnDepositRegisteredPubkey(handler) - }, + monitoringStartFn, + monitoringStopFn, t.watchKeepClosed, - t.chain.RetrieveSignerPubkey, + actFn, actBackoffFn, func(_ string) (time.Duration, error) { return timeout, nil @@ -136,6 +179,7 @@ func (t *tbtc) monitorProvideRedemptionSignature( ctx context.Context, actBackoffFn backoffFn, timeout time.Duration, + blockConfirmations uint64, ) error { monitoringStartFn := func( handler depositEventHandler, @@ -259,6 +303,7 @@ func (t *tbtc) monitorProvideRedemptionProof( ctx context.Context, actBackoffFn backoffFn, timeout time.Duration, + blockConfirmations uint64, ) error { monitoringStartFn := func( handler depositEventHandler, @@ -641,6 +686,51 @@ func (t *tbtc) shouldMonitorDeposit( return t.keepsRegistry.HasSigner(common.HexToAddress(keepAddress)) } +func (t *tbtc) waitDepositStateConfirmation( + depositAddress string, + depositState chain.DepositState, + blockConfirmations uint64, +) bool { + stateCheck := func() (bool, error) { + currentState, err := t.chain.CurrentState(depositAddress) + if err != nil { + return false, err + } + + return currentState == depositState, nil + } + + currentBlock, err := t.chain.BlockCounter().CurrentBlock() + if err != nil { + logger.Errorf( + "could not get current block while confirming "+ + "state [%v] for deposit [%v]: [%v]", + depositState, + depositAddress, + err, + ) + return false + } + + confirmed, err := chain.WaitForChainConfirmation( + t.chain, + currentBlock, + blockConfirmations, + stateCheck, + ) + if err != nil { + logger.Errorf( + "could not confirm state [%v] for deposit [%v]: [%v]", + depositState, + depositAddress, + err, + ) + return false + } + + return confirmed +} + func (t *tbtc) pastEventsLookupStartBlock() uint64 { currentBlock, err := t.chain.BlockCounter().CurrentBlock() if err != nil { diff --git a/pkg/extensions/tbtc/tbtc_test.go b/pkg/extensions/tbtc/tbtc_test.go index 4073e1610..1dc8c2bed 100644 --- a/pkg/extensions/tbtc/tbtc_test.go +++ b/pkg/extensions/tbtc/tbtc_test.go @@ -18,8 +18,9 @@ import ( ) const ( - timeout = 500 * time.Millisecond - depositAddress = "0xa5FA806723A7c7c8523F33c39686f20b52612877" + timeout = 500 * time.Millisecond + depositAddress = "0xa5FA806723A7c7c8523F33c39686f20b52612877" + defaultLocalBlockConfirmations = 0 ) func TestRetrievePubkey_TimeoutElapsed(t *testing.T) { @@ -31,6 +32,7 @@ func TestRetrievePubkey_TimeoutElapsed(t *testing.T) { ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -88,6 +90,7 @@ func TestRetrievePubkey_StopEventOccurred(t *testing.T) { ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -155,6 +158,7 @@ func TestRetrievePubkey_KeepClosedEventOccurred(t *testing.T) { ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -219,6 +223,7 @@ func TestRetrievePubkey_KeepTerminatedEventOccurred(t *testing.T) { ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -283,6 +288,7 @@ func TestRetrievePubkey_ActionFailed(t *testing.T) { ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -320,6 +326,7 @@ func TestRetrievePubkey_ContextCancelled_WithoutWorkingMonitoring(t *testing.T) ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -357,6 +364,7 @@ func TestRetrievePubkey_ContextCancelled_WithWorkingMonitoring(t *testing.T) { ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -400,6 +408,7 @@ func TestProvideRedemptionSignature_TimeoutElapsed(t *testing.T) { ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -472,6 +481,7 @@ func TestProvideRedemptionSignature_StopEventOccurred_DepositGotRedemptionSignat ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -559,6 +569,7 @@ func TestProvideRedemptionSignature_StopEventOccurred_DepositRedeemed( ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -637,6 +648,7 @@ func TestProvideRedemptionSignature_KeepClosedEventOccurred(t *testing.T) { ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -712,6 +724,7 @@ func TestProvideRedemptionSignature_KeepTerminatedEventOccurred(t *testing.T) { ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -787,6 +800,7 @@ func TestProvideRedemptionSignature_ActionFailed(t *testing.T) { ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -842,6 +856,7 @@ func TestProvideRedemptionSignature_ContextCancelled_WithoutWorkingMonitoring( ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -892,6 +907,7 @@ func TestProvideRedemptionSignature_ContextCancelled_WithWorkingMonitoring( ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -945,6 +961,7 @@ func TestProvideRedemptionProof_TimeoutElapsed(t *testing.T) { ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -1035,6 +1052,7 @@ func TestProvideRedemptionProof_StopEventOccurred_DepositRedemptionRequested( ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -1139,6 +1157,7 @@ func TestProvideRedemptionProof_StopEventOccurred_DepositRedeemed( ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -1226,6 +1245,7 @@ func TestProvideRedemptionProof_KeepClosedEventOccurred(t *testing.T) { ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -1318,6 +1338,7 @@ func TestProvideRedemptionProof_KeepTerminatedEventOccurred(t *testing.T) { ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -1410,6 +1431,7 @@ func TestProvideRedemptionProof_ActionFailed(t *testing.T) { ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -1474,6 +1496,7 @@ func TestProvideRedemptionProof_ContextCancelled_WithoutWorkingMonitoring( ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -1538,6 +1561,7 @@ func TestProvideRedemptionProof_ContextCancelled_WithWorkingMonitoring( ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) From f4f2f6d089ae6756fc8e1613ec382c1c7b2df78c Mon Sep 17 00:00:00 2001 From: Lukasz Zimnoch Date: Tue, 3 Nov 2020 14:31:36 +0100 Subject: [PATCH 03/15] Add confirmations to remaining instances Implemented deposit state confirmations upon monitoring stop event and action execution for remaining monitoring instances. --- pkg/extensions/tbtc/tbtc.go | 122 +++++++++++++++++++++++++++--------- 1 file changed, 92 insertions(+), 30 deletions(-) diff --git a/pkg/extensions/tbtc/tbtc.go b/pkg/extensions/tbtc/tbtc.go index 6c10da35d..955a57969 100644 --- a/pkg/extensions/tbtc/tbtc.go +++ b/pkg/extensions/tbtc/tbtc.go @@ -114,7 +114,7 @@ func (t *tbtc) monitorRetrievePubKey( monitoringStopFn := func( handler depositEventHandler, ) (subscription.EventSubscription, error) { - handlerWithConfirmation := func(depositAddress string) { + return t.chain.OnDepositRegisteredPubkey(func(depositAddress string) { if t.waitDepositStateConfirmation( depositAddress, chain.AwaitingBtcFundingProof, @@ -122,9 +122,7 @@ func (t *tbtc) monitorRetrievePubKey( ) { handler(depositAddress) } - } - - return t.chain.OnDepositRegisteredPubkey(handlerWithConfirmation) + }) } actFn := func(depositAddress string) error { @@ -138,10 +136,7 @@ func (t *tbtc) monitorRetrievePubKey( chain.AwaitingBtcFundingProof, blockConfirmations, ) { - return fmt.Errorf( - "deposit [%v] is not in desired state", - depositAddress, - ) + return fmt.Errorf("deposit state is not confirmed") } return nil @@ -193,13 +188,33 @@ func (t *tbtc) monitorProvideRedemptionSignature( handler depositEventHandler, ) (subscription.EventSubscription, error) { // Stop in case the redemption signature has been provided by someone else. - signatureSubscription, err := t.chain.OnDepositGotRedemptionSignature(handler) + signatureSubscription, err := t.chain.OnDepositGotRedemptionSignature( + func(depositAddress string) { + if t.waitDepositStateConfirmation( + depositAddress, + chain.AwaitingWithdrawalProof, + blockConfirmations, + ) { + handler(depositAddress) + } + }, + ) if err != nil { return nil, err } // Stop in case the redemption proof has been provided by someone else. - redeemedSubscription, err := t.chain.OnDepositRedeemed(handler) + redeemedSubscription, err := t.chain.OnDepositRedeemed( + func(depositAddress string) { + if t.waitDepositStateConfirmation( + depositAddress, + chain.Redeemed, + blockConfirmations, + ) { + handler(depositAddress) + } + }, + ) if err != nil { return nil, err } @@ -246,29 +261,45 @@ func (t *tbtc) monitorProvideRedemptionSignature( depositDigest := latestRedemptionRequestedEvent.Digest + var signatureSubmittedEvent *chain.SignatureSubmittedEvent + // Start iterating from the latest event. for i := len(signatureSubmittedEvents) - 1; i >= 0; i-- { - signatureSubmittedEvent := signatureSubmittedEvents[i] - - if bytes.Equal(signatureSubmittedEvent.Digest[:], depositDigest[:]) { - // We add 27 to the recovery ID to align it with ethereum and - // bitcoin protocols where 27 is added to recovery ID to - // indicate usage of uncompressed public keys. - v := 27 + signatureSubmittedEvent.RecoveryID - - return t.chain.ProvideRedemptionSignature( - depositAddress, - v, - signatureSubmittedEvent.R, - signatureSubmittedEvent.S, - ) + if bytes.Equal(signatureSubmittedEvents[i].Digest[:], depositDigest[:]) { + signatureSubmittedEvent = signatureSubmittedEvents[i] + break } } - return fmt.Errorf( - "could not find signature for digest: [%v]", - depositDigest, + if signatureSubmittedEvent == nil { + return fmt.Errorf( + "could not find signature for digest: [%v]", + depositDigest, + ) + } + + // We add 27 to the recovery ID to align it with ethereum and + // bitcoin protocols where 27 is added to recovery ID to + // indicate usage of uncompressed public keys. + err = t.chain.ProvideRedemptionSignature( + depositAddress, + 27+signatureSubmittedEvent.RecoveryID, + signatureSubmittedEvent.R, + signatureSubmittedEvent.S, ) + if err != nil { + return err + } + + if !t.waitDepositStateConfirmation( + depositAddress, + chain.AwaitingWithdrawalProof, + blockConfirmations, + ) { + return fmt.Errorf("deposit state is not confirmed") + } + + return nil } monitoringSubscription, err := t.monitorAndAct( @@ -317,14 +348,32 @@ func (t *tbtc) monitorProvideRedemptionProof( ) (subscription.EventSubscription, error) { // Stop in case the redemption fee has been increased by someone else. redemptionRequestedSubscription, err := t.chain.OnDepositRedemptionRequested( - handler, + func(depositAddress string) { + if t.waitDepositStateConfirmation( + depositAddress, + chain.AwaitingWithdrawalSignature, + blockConfirmations, + ) { + handler(depositAddress) + } + }, ) if err != nil { return nil, err } // Stop in case the redemption proof has been provided by someone else. - redeemedSubscription, err := t.chain.OnDepositRedeemed(handler) + redeemedSubscription, err := t.chain.OnDepositRedeemed( + func(depositAddress string) { + if t.waitDepositStateConfirmation( + depositAddress, + chain.Redeemed, + blockConfirmations, + ) { + handler(depositAddress) + } + }, + ) if err != nil { return nil, err } @@ -370,11 +419,24 @@ func (t *tbtc) monitorProvideRedemptionProof( redemptionRequestedEvents[0].RequestedFee, // initial fee ) - return t.chain.IncreaseRedemptionFee( + err = t.chain.IncreaseRedemptionFee( depositAddress, toLittleEndianBytes(previousOutputValue), toLittleEndianBytes(newOutputValue), ) + if err != nil { + return err + } + + if !t.waitDepositStateConfirmation( + depositAddress, + chain.AwaitingWithdrawalSignature, + blockConfirmations, + ) { + return fmt.Errorf("deposit state is not confirmed") + } + + return nil } timeoutFn := func(depositAddress string) (time.Duration, error) { From a402d852f92742381de78c323dea8d1e8d4d86ac Mon Sep 17 00:00:00 2001 From: Lukasz Zimnoch Date: Tue, 3 Nov 2020 14:33:54 +0100 Subject: [PATCH 04/15] Remove TODOs --- pkg/extensions/tbtc/tbtc.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkg/extensions/tbtc/tbtc.go b/pkg/extensions/tbtc/tbtc.go index 955a57969..ac079a9cb 100644 --- a/pkg/extensions/tbtc/tbtc.go +++ b/pkg/extensions/tbtc/tbtc.go @@ -528,9 +528,6 @@ type backoffFn func(iteration int) time.Duration type timeoutFn func(depositAddress string) (time.Duration, error) -// TODO: -// 1. Handle chain reorgs (keep-ecdsa/pull/585#discussion_r511760283 and keep-ecdsa/pull/585#discussion_r513447505) -// 2. Resume monitoring after client restart. func (t *tbtc) monitorAndAct( ctx context.Context, monitoringName string, From 9660819dade7c4f4d8de5b6fb202edbeb1cef8ff Mon Sep 17 00:00:00 2001 From: Lukasz Zimnoch Date: Mon, 9 Nov 2020 15:04:39 +0100 Subject: [PATCH 05/15] Fix failing unit test --- pkg/chain/local/tbtc.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/chain/local/tbtc.go b/pkg/chain/local/tbtc.go index 9c07fd551..b41ec44ea 100644 --- a/pkg/chain/local/tbtc.go +++ b/pkg/chain/local/tbtc.go @@ -540,8 +540,8 @@ func (tlc *TBTCLocalChain) ProvideRedemptionProof( func (tlc *TBTCLocalChain) CurrentState( depositAddress string, ) (chain.DepositState, error) { - tlc.mutex.Lock() - defer tlc.mutex.Unlock() + tlc.tbtcLocalChainMutex.Lock() + defer tlc.tbtcLocalChainMutex.Unlock() deposit, ok := tlc.deposits[depositAddress] if !ok { From 7332ff5a40a6052367d6136ea6917f9519b22d84 Mon Sep 17 00:00:00 2001 From: Lukasz Zimnoch Date: Mon, 9 Nov 2020 15:05:33 +0100 Subject: [PATCH 06/15] Fix failing unit test - part 2 --- pkg/extensions/tbtc/tbtc_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/extensions/tbtc/tbtc_test.go b/pkg/extensions/tbtc/tbtc_test.go index 11ef8d038..ad7052ff3 100644 --- a/pkg/extensions/tbtc/tbtc_test.go +++ b/pkg/extensions/tbtc/tbtc_test.go @@ -428,6 +428,7 @@ func TestRetrievePubkey_NoSignerInKeepsRegistry(t *testing.T) { ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -1038,6 +1039,7 @@ func TestProvideRedemptionSignature_NoSignerInKeepsRegistry( ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -1778,6 +1780,7 @@ func TestProvideRedemptionProof_NoSignerInKeepsRegistry( ctx, constantBackoff, timeout, + defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) From 110e44c7352af9c03687af8768443bde19c0df30 Mon Sep 17 00:00:00 2001 From: Lukasz Zimnoch Date: Tue, 10 Nov 2020 11:26:32 +0100 Subject: [PATCH 07/15] Confirm keep inactivity in TBTC extension Keep closed and terminated events should be confirmed similarly as regular monitoring stop events to handle chain reorgs properly. --- pkg/extensions/tbtc/tbtc.go | 67 +++++++++++++++------- pkg/extensions/tbtc/tbtc_test.go | 97 +++++++++++++------------------- 2 files changed, 86 insertions(+), 78 deletions(-) diff --git a/pkg/extensions/tbtc/tbtc.go b/pkg/extensions/tbtc/tbtc.go index 29955ad51..1db2979ba 100644 --- a/pkg/extensions/tbtc/tbtc.go +++ b/pkg/extensions/tbtc/tbtc.go @@ -40,7 +40,6 @@ func Initialize( ctx, exponentialBackoff, 165*time.Minute, // 15 minutes before the 3 hours on-chain timeout - defaultBlockConfirmations, ) if err != nil { return fmt.Errorf( @@ -53,7 +52,6 @@ func Initialize( ctx, exponentialBackoff, 105*time.Minute, // 15 minutes before the 2 hours on-chain timeout - defaultBlockConfirmations, ) if err != nil { return fmt.Errorf( @@ -67,7 +65,6 @@ func Initialize( ctx, exponentialBackoff, 345*time.Minute, // 15 minutes before the 6 hours on-chain timeout - defaultBlockConfirmations, ) if err != nil { return fmt.Errorf( @@ -83,13 +80,15 @@ func Initialize( } type tbtc struct { - chain chain.TBTCHandle - monitoringLocks sync.Map + chain chain.TBTCHandle + monitoringLocks sync.Map + blockConfirmations uint64 } func newTBTC(chain chain.TBTCHandle) *tbtc { return &tbtc{ - chain: chain, + chain: chain, + blockConfirmations: defaultBlockConfirmations, } } @@ -97,7 +96,6 @@ func (t *tbtc) monitorRetrievePubKey( ctx context.Context, actBackoffFn backoffFn, timeout time.Duration, - blockConfirmations uint64, ) error { monitoringStartFn := func( handler depositEventHandler, @@ -112,7 +110,6 @@ func (t *tbtc) monitorRetrievePubKey( if t.waitDepositStateConfirmation( depositAddress, chain.AwaitingBtcFundingProof, - blockConfirmations, ) { handler(depositAddress) } @@ -128,7 +125,6 @@ func (t *tbtc) monitorRetrievePubKey( if !t.waitDepositStateConfirmation( depositAddress, chain.AwaitingBtcFundingProof, - blockConfirmations, ) { return fmt.Errorf("deposit state is not confirmed") } @@ -168,7 +164,6 @@ func (t *tbtc) monitorProvideRedemptionSignature( ctx context.Context, actBackoffFn backoffFn, timeout time.Duration, - blockConfirmations uint64, ) error { monitoringStartFn := func( handler depositEventHandler, @@ -187,7 +182,6 @@ func (t *tbtc) monitorProvideRedemptionSignature( if t.waitDepositStateConfirmation( depositAddress, chain.AwaitingWithdrawalProof, - blockConfirmations, ) { handler(depositAddress) } @@ -203,7 +197,6 @@ func (t *tbtc) monitorProvideRedemptionSignature( if t.waitDepositStateConfirmation( depositAddress, chain.Redeemed, - blockConfirmations, ) { handler(depositAddress) } @@ -288,7 +281,6 @@ func (t *tbtc) monitorProvideRedemptionSignature( if !t.waitDepositStateConfirmation( depositAddress, chain.AwaitingWithdrawalProof, - blockConfirmations, ) { return fmt.Errorf("deposit state is not confirmed") } @@ -328,7 +320,6 @@ func (t *tbtc) monitorProvideRedemptionProof( ctx context.Context, actBackoffFn backoffFn, timeout time.Duration, - blockConfirmations uint64, ) error { monitoringStartFn := func( handler depositEventHandler, @@ -346,7 +337,6 @@ func (t *tbtc) monitorProvideRedemptionProof( if t.waitDepositStateConfirmation( depositAddress, chain.AwaitingWithdrawalSignature, - blockConfirmations, ) { handler(depositAddress) } @@ -362,7 +352,6 @@ func (t *tbtc) monitorProvideRedemptionProof( if t.waitDepositStateConfirmation( depositAddress, chain.Redeemed, - blockConfirmations, ) { handler(depositAddress) } @@ -425,7 +414,6 @@ func (t *tbtc) monitorProvideRedemptionProof( if !t.waitDepositStateConfirmation( depositAddress, chain.AwaitingWithdrawalSignature, - blockConfirmations, ) { return fmt.Errorf("deposit state is not confirmed") } @@ -703,7 +691,9 @@ func (t *tbtc) watchKeepClosed( keepClosedSubscription, err := t.chain.OnKeepClosed( common.HexToAddress(keepAddress), func(_ *chain.KeepClosedEvent) { - signalChan <- struct{}{} + if t.waitKeepInactivityConfirmation(keepAddress) { + signalChan <- struct{}{} + } }, ) if err != nil { @@ -713,7 +703,9 @@ func (t *tbtc) watchKeepClosed( keepTerminatedSubscription, err := t.chain.OnKeepTerminated( common.HexToAddress(keepAddress), func(_ *chain.KeepTerminatedEvent) { - signalChan <- struct{}{} + if t.waitKeepInactivityConfirmation(keepAddress) { + signalChan <- struct{}{} + } }, ) if err != nil { @@ -753,7 +745,6 @@ func (t *tbtc) shouldMonitorDeposit( func (t *tbtc) waitDepositStateConfirmation( depositAddress string, depositState chain.DepositState, - blockConfirmations uint64, ) bool { stateCheck := func() (bool, error) { currentState, err := t.chain.CurrentState(depositAddress) @@ -779,7 +770,7 @@ func (t *tbtc) waitDepositStateConfirmation( confirmed, err := chain.WaitForChainConfirmation( t.chain, currentBlock, - blockConfirmations, + t.blockConfirmations, stateCheck, ) if err != nil { @@ -795,6 +786,40 @@ func (t *tbtc) waitDepositStateConfirmation( return confirmed } +func (t *tbtc) waitKeepInactivityConfirmation( + keepAddress string, +) bool { + currentBlock, err := t.chain.BlockCounter().CurrentBlock() + if err != nil { + logger.Errorf( + "could not get current block while confirming "+ + "inactivity for keep [%v]: [%v]", + keepAddress, + err, + ) + return false + } + + isKeepActive, err := chain.WaitForChainConfirmation( + t.chain, + currentBlock, + t.blockConfirmations, + func() (bool, error) { + return t.chain.IsActive(common.HexToAddress(keepAddress)) + }, + ) + if err != nil { + logger.Errorf( + "could not confirm inactivity for keep [%v]: [%v]", + keepAddress, + err, + ) + return false + } + + return !isKeepActive +} + func (t *tbtc) pastEventsLookupStartBlock() uint64 { currentBlock, err := t.chain.BlockCounter().CurrentBlock() if err != nil { diff --git a/pkg/extensions/tbtc/tbtc_test.go b/pkg/extensions/tbtc/tbtc_test.go index 2927e5c93..13af785ba 100644 --- a/pkg/extensions/tbtc/tbtc_test.go +++ b/pkg/extensions/tbtc/tbtc_test.go @@ -16,6 +16,7 @@ import ( "github.com/keep-network/keep-ecdsa/pkg/utils/byteutils" "github.com/ethereum/go-ethereum/common" + chain "github.com/keep-network/keep-ecdsa/pkg/chain" "github.com/keep-network/keep-ecdsa/pkg/chain/local" ) @@ -25,18 +26,25 @@ const ( defaultLocalBlockConfirmations = 0 ) +func newTestTBTC(chain chain.TBTCHandle) *tbtc { + tbtc := newTBTC(chain) + + tbtc.blockConfirmations = defaultLocalBlockConfirmations + + return tbtc +} + func TestRetrievePubkey_TimeoutElapsed(t *testing.T) { ctx, cancelCtx := context.WithCancel(context.Background()) defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorRetrievePubKey( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -92,13 +100,12 @@ func TestRetrievePubkey_StopEventOccurred(t *testing.T) { defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorRetrievePubKey( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -164,13 +171,12 @@ func TestRetrievePubkey_KeepClosedEventOccurred(t *testing.T) { defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorRetrievePubKey( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -233,13 +239,12 @@ func TestRetrievePubkey_KeepTerminatedEventOccurred(t *testing.T) { defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorRetrievePubKey( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -302,13 +307,12 @@ func TestRetrievePubkey_ActionFailed(t *testing.T) { defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorRetrievePubKey( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -344,13 +348,12 @@ func TestRetrievePubkey_ContextCancelled_WithoutWorkingMonitoring(t *testing.T) defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorRetrievePubKey( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -386,13 +389,12 @@ func TestRetrievePubkey_ContextCancelled_WithWorkingMonitoring(t *testing.T) { defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorRetrievePubKey( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -434,13 +436,12 @@ func TestRetrievePubkey_OperatorNotInSigningGroup(t *testing.T) { defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorRetrievePubKey( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -474,13 +475,12 @@ func TestProvideRedemptionSignature_TimeoutElapsed(t *testing.T) { defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorProvideRedemptionSignature( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -551,13 +551,12 @@ func TestProvideRedemptionSignature_StopEventOccurred_DepositGotRedemptionSignat defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorProvideRedemptionSignature( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -643,13 +642,12 @@ func TestProvideRedemptionSignature_StopEventOccurred_DepositRedeemed( defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorProvideRedemptionSignature( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -726,13 +724,12 @@ func TestProvideRedemptionSignature_KeepClosedEventOccurred(t *testing.T) { defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorProvideRedemptionSignature( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -806,13 +803,12 @@ func TestProvideRedemptionSignature_KeepTerminatedEventOccurred(t *testing.T) { defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorProvideRedemptionSignature( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -886,13 +882,12 @@ func TestProvideRedemptionSignature_ActionFailed(t *testing.T) { defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorProvideRedemptionSignature( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -946,13 +941,12 @@ func TestProvideRedemptionSignature_ContextCancelled_WithoutWorkingMonitoring( defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorProvideRedemptionSignature( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -1001,13 +995,12 @@ func TestProvideRedemptionSignature_ContextCancelled_WithWorkingMonitoring( defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorProvideRedemptionSignature( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -1061,13 +1054,12 @@ func TestProvideRedemptionSignature_OperatorNotInSigningGroup( defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorProvideRedemptionSignature( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -1116,13 +1108,12 @@ func TestProvideRedemptionProof_TimeoutElapsed(t *testing.T) { defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorProvideRedemptionProof( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -1211,13 +1202,12 @@ func TestProvideRedemptionProof_StopEventOccurred_DepositRedemptionRequested( defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorProvideRedemptionProof( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -1323,13 +1313,12 @@ func TestProvideRedemptionProof_StopEventOccurred_DepositRedeemed( defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorProvideRedemptionProof( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -1415,13 +1404,12 @@ func TestProvideRedemptionProof_KeepClosedEventOccurred(t *testing.T) { defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorProvideRedemptionProof( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -1512,13 +1500,12 @@ func TestProvideRedemptionProof_KeepTerminatedEventOccurred(t *testing.T) { defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorProvideRedemptionProof( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -1609,13 +1596,12 @@ func TestProvideRedemptionProof_ActionFailed(t *testing.T) { defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorProvideRedemptionProof( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -1678,13 +1664,12 @@ func TestProvideRedemptionProof_ContextCancelled_WithoutWorkingMonitoring( defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorProvideRedemptionProof( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -1747,13 +1732,12 @@ func TestProvideRedemptionProof_ContextCancelled_WithWorkingMonitoring( defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorProvideRedemptionProof( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -1821,13 +1805,12 @@ func TestProvideRedemptionProof_OperatorNotInSigningGroup( defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) err := tbtc.monitorProvideRedemptionProof( ctx, constantBackoff, timeout, - defaultLocalBlockConfirmations, ) if err != nil { t.Fatal(err) @@ -1885,7 +1868,7 @@ func TestMonitorAndActDeduplication(t *testing.T) { defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) monitoringName := "monitoring" @@ -1960,7 +1943,7 @@ func TestAcquireMonitoringLock(t *testing.T) { defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) if !tbtc.acquireMonitoringLock("0xAA", "monitoring one") { t.Errorf("monitoring wasn't started before; should be locked successfully") @@ -1984,7 +1967,7 @@ func TestAcquireMonitoringLock_Duplicate(t *testing.T) { defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) if !tbtc.acquireMonitoringLock("0xAA", "monitoring one") { t.Errorf("monitoring wasn't started before; should be locked successfully") @@ -2000,7 +1983,7 @@ func TestReleaseMonitoringLock(t *testing.T) { defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) if !tbtc.acquireMonitoringLock("0xAA", "monitoring one") { t.Errorf("monitoring wasn't started before; should be locked successfully") @@ -2018,7 +2001,7 @@ func TestReleaseMonitoringLock_WhenEmpty(t *testing.T) { defer cancelCtx() tbtcChain := local.NewTBTCLocalChain(ctx) - tbtc := newTBTC(tbtcChain) + tbtc := newTestTBTC(tbtcChain) tbtc.releaseMonitoringLock("0xAA", "monitoring one") From b47de845193386b9878ae6116111c7414e61dd72 Mon Sep 17 00:00:00 2001 From: Lukasz Zimnoch Date: Tue, 10 Nov 2020 16:11:59 +0100 Subject: [PATCH 08/15] Improve deposit state confirmation logic --- pkg/extensions/tbtc/tbtc.go | 54 ++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/pkg/extensions/tbtc/tbtc.go b/pkg/extensions/tbtc/tbtc.go index 1db2979ba..50caa5af3 100644 --- a/pkg/extensions/tbtc/tbtc.go +++ b/pkg/extensions/tbtc/tbtc.go @@ -97,6 +97,8 @@ func (t *tbtc) monitorRetrievePubKey( actBackoffFn backoffFn, timeout time.Duration, ) error { + initialDepositState := chain.AwaitingSignerSetup + monitoringStartFn := func( handler depositEventHandler, ) (subscription.EventSubscription, error) { @@ -107,9 +109,9 @@ func (t *tbtc) monitorRetrievePubKey( handler depositEventHandler, ) (subscription.EventSubscription, error) { return t.chain.OnDepositRegisteredPubkey(func(depositAddress string) { - if t.waitDepositStateConfirmation( + if t.waitDepositStateChangeConfirmation( depositAddress, - chain.AwaitingBtcFundingProof, + initialDepositState, ) { handler(depositAddress) } @@ -122,11 +124,11 @@ func (t *tbtc) monitorRetrievePubKey( return err } - if !t.waitDepositStateConfirmation( + if !t.waitDepositStateChangeConfirmation( depositAddress, - chain.AwaitingBtcFundingProof, + initialDepositState, ) { - return fmt.Errorf("deposit state is not confirmed") + return fmt.Errorf("deposit state change is not confirmed") } return nil @@ -165,6 +167,8 @@ func (t *tbtc) monitorProvideRedemptionSignature( actBackoffFn backoffFn, timeout time.Duration, ) error { + initialDepositState := chain.AwaitingWithdrawalSignature + monitoringStartFn := func( handler depositEventHandler, ) (subscription.EventSubscription, error) { @@ -179,9 +183,9 @@ func (t *tbtc) monitorProvideRedemptionSignature( // Stop in case the redemption signature has been provided by someone else. signatureSubscription, err := t.chain.OnDepositGotRedemptionSignature( func(depositAddress string) { - if t.waitDepositStateConfirmation( + if t.waitDepositStateChangeConfirmation( depositAddress, - chain.AwaitingWithdrawalProof, + initialDepositState, ) { handler(depositAddress) } @@ -194,9 +198,9 @@ func (t *tbtc) monitorProvideRedemptionSignature( // Stop in case the redemption proof has been provided by someone else. redeemedSubscription, err := t.chain.OnDepositRedeemed( func(depositAddress string) { - if t.waitDepositStateConfirmation( + if t.waitDepositStateChangeConfirmation( depositAddress, - chain.Redeemed, + initialDepositState, ) { handler(depositAddress) } @@ -278,9 +282,9 @@ func (t *tbtc) monitorProvideRedemptionSignature( return err } - if !t.waitDepositStateConfirmation( + if !t.waitDepositStateChangeConfirmation( depositAddress, - chain.AwaitingWithdrawalProof, + initialDepositState, ) { return fmt.Errorf("deposit state is not confirmed") } @@ -321,6 +325,8 @@ func (t *tbtc) monitorProvideRedemptionProof( actBackoffFn backoffFn, timeout time.Duration, ) error { + initialDepositState := chain.AwaitingWithdrawalProof + monitoringStartFn := func( handler depositEventHandler, ) (subscription.EventSubscription, error) { @@ -334,9 +340,9 @@ func (t *tbtc) monitorProvideRedemptionProof( // Stop in case the redemption fee has been increased by someone else. redemptionRequestedSubscription, err := t.chain.OnDepositRedemptionRequested( func(depositAddress string) { - if t.waitDepositStateConfirmation( + if t.waitDepositStateChangeConfirmation( depositAddress, - chain.AwaitingWithdrawalSignature, + initialDepositState, ) { handler(depositAddress) } @@ -349,9 +355,9 @@ func (t *tbtc) monitorProvideRedemptionProof( // Stop in case the redemption proof has been provided by someone else. redeemedSubscription, err := t.chain.OnDepositRedeemed( func(depositAddress string) { - if t.waitDepositStateConfirmation( + if t.waitDepositStateChangeConfirmation( depositAddress, - chain.Redeemed, + initialDepositState, ) { handler(depositAddress) } @@ -411,9 +417,9 @@ func (t *tbtc) monitorProvideRedemptionProof( return err } - if !t.waitDepositStateConfirmation( + if !t.waitDepositStateChangeConfirmation( depositAddress, - chain.AwaitingWithdrawalSignature, + initialDepositState, ) { return fmt.Errorf("deposit state is not confirmed") } @@ -742,9 +748,9 @@ func (t *tbtc) shouldMonitorDeposit( return false } -func (t *tbtc) waitDepositStateConfirmation( +func (t *tbtc) waitDepositStateChangeConfirmation( depositAddress string, - depositState chain.DepositState, + initialDepositState chain.DepositState, ) bool { stateCheck := func() (bool, error) { currentState, err := t.chain.CurrentState(depositAddress) @@ -752,15 +758,15 @@ func (t *tbtc) waitDepositStateConfirmation( return false, err } - return currentState == depositState, nil + return currentState != initialDepositState, nil } currentBlock, err := t.chain.BlockCounter().CurrentBlock() if err != nil { logger.Errorf( "could not get current block while confirming "+ - "state [%v] for deposit [%v]: [%v]", - depositState, + "state [%v] change for deposit [%v]: [%v]", + initialDepositState, depositAddress, err, ) @@ -775,8 +781,8 @@ func (t *tbtc) waitDepositStateConfirmation( ) if err != nil { logger.Errorf( - "could not confirm state [%v] for deposit [%v]: [%v]", - depositState, + "could not confirm state [%v] change for deposit [%v]: [%v]", + initialDepositState, depositAddress, err, ) From e34a0cccbb6a2034ad01b289d634c08c706ec98d Mon Sep 17 00:00:00 2001 From: Lukasz Zimnoch Date: Tue, 10 Nov 2020 16:14:36 +0100 Subject: [PATCH 09/15] Improve deposit state confirmation error messages --- pkg/extensions/tbtc/tbtc.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/extensions/tbtc/tbtc.go b/pkg/extensions/tbtc/tbtc.go index 50caa5af3..9a202103f 100644 --- a/pkg/extensions/tbtc/tbtc.go +++ b/pkg/extensions/tbtc/tbtc.go @@ -286,7 +286,7 @@ func (t *tbtc) monitorProvideRedemptionSignature( depositAddress, initialDepositState, ) { - return fmt.Errorf("deposit state is not confirmed") + return fmt.Errorf("deposit state change is not confirmed") } return nil @@ -421,7 +421,7 @@ func (t *tbtc) monitorProvideRedemptionProof( depositAddress, initialDepositState, ) { - return fmt.Errorf("deposit state is not confirmed") + return fmt.Errorf("deposit state change is not confirmed") } return nil From bae4b4aef48cac02975697d7ae2ebf2f2fa157f4 Mon Sep 17 00:00:00 2001 From: Lukasz Zimnoch Date: Thu, 12 Nov 2020 13:18:12 +0100 Subject: [PATCH 10/15] Use the confirmation waiter from keep-common --- go.mod | 2 +- go.sum | 4 ++-- pkg/chain/chain.go | 3 --- pkg/chain/confirmation.go | 32 -------------------------------- pkg/extensions/tbtc/tbtc.go | 10 ++++++---- 5 files changed, 9 insertions(+), 42 deletions(-) delete mode 100644 pkg/chain/confirmation.go diff --git a/go.mod b/go.mod index d064a5a96..752cb9bb0 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,7 @@ require ( github.com/gogo/protobuf v1.3.1 github.com/google/gofuzz v1.1.0 github.com/ipfs/go-log v1.0.4 - github.com/keep-network/keep-common v1.2.1-0.20201103151750-c52f5d480c10 + github.com/keep-network/keep-common v1.2.1-0.20201112121304-d43f4165b8fa github.com/keep-network/keep-core v1.3.0 github.com/keep-network/tbtc v1.1.1-0.20201026093513-cb9246987718 github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index c03839b0f..b0eec235a 100644 --- a/go.sum +++ b/go.sum @@ -335,8 +335,8 @@ github.com/keep-network/keep-common v1.2.0 h1:hVd2tTd7vL+9CQP5Ntk5kjs+GYvkgrRNBc github.com/keep-network/keep-common v1.2.0/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo= github.com/keep-network/keep-common v1.2.1-0.20201020114759-19c123cbd4f4 h1:CivupPSFswHACua5xZGKdeYxsCQ2cmRomTIBh8kfk70= github.com/keep-network/keep-common v1.2.1-0.20201020114759-19c123cbd4f4/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo= -github.com/keep-network/keep-common v1.2.1-0.20201103151750-c52f5d480c10 h1:KuZP1ArRAK1X4PunfteJX+QnP/XSfvfmi/ZR2a6kXTU= -github.com/keep-network/keep-common v1.2.1-0.20201103151750-c52f5d480c10/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo= +github.com/keep-network/keep-common v1.2.1-0.20201112121304-d43f4165b8fa h1:OyKF/ha+P0nznQaL0GttWeZ8m+ZEHzrvkzudaNT+V8Y= +github.com/keep-network/keep-common v1.2.1-0.20201112121304-d43f4165b8fa/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo= github.com/keep-network/keep-core v1.3.0 h1:7Tb33EmO/ntHOEbOiYciRlBhqu5Ln6KemWCaYK0Z6LA= github.com/keep-network/keep-core v1.3.0/go.mod h1:1KsSSTQoN754TrFLW7kLy50pOG2CQ4BOfnJqdvEG7FA= github.com/keep-network/tbtc v1.1.1-0.20201026093513-cb9246987718 h1:/ZNMBY7y6hfzCYA8mgtHnspGO26OmWV3sDehyGnqRyY= diff --git a/pkg/chain/chain.go b/pkg/chain/chain.go index c9382daa3..5036c7bd4 100644 --- a/pkg/chain/chain.go +++ b/pkg/chain/chain.go @@ -7,14 +7,11 @@ import ( "time" "github.com/ethereum/go-ethereum/common" - "github.com/ipfs/go-log" "github.com/keep-network/keep-common/pkg/subscription" "github.com/keep-network/keep-core/pkg/chain" "github.com/keep-network/keep-ecdsa/pkg/ecdsa" ) -var logger = log.Logger("keep-chain") - // Handle represents a handle to an ethereum blockchain. type Handle interface { // Address returns client's ethereum address. diff --git a/pkg/chain/confirmation.go b/pkg/chain/confirmation.go deleted file mode 100644 index 7f4c983b1..000000000 --- a/pkg/chain/confirmation.go +++ /dev/null @@ -1,32 +0,0 @@ -package eth - -import ( - "fmt" -) - -// WaitForChainConfirmation ensures that after receiving specific number of block -// confirmations the state of the chain is actually as expected. It waits for -// predefined number of blocks since the start block number provided. After the -// required block number is reached it performs a check of the chain state with -// a provided function returning a boolean value. -func WaitForChainConfirmation( - ethereumChain Handle, - startBlockNumber uint64, - blockConfirmations uint64, - stateCheck func() (bool, error), -) (bool, error) { - blockHeight := startBlockNumber + blockConfirmations - logger.Infof("waiting for block [%d] to confirm chain state", blockHeight) - - err := ethereumChain.BlockCounter().WaitForBlockHeight(blockHeight) - if err != nil { - return false, fmt.Errorf("failed to wait for block height: [%v]", err) - } - - result, err := stateCheck() - if err != nil { - return false, fmt.Errorf("failed to get chain state confirmation: [%v]", err) - } - - return result, nil -} diff --git a/pkg/extensions/tbtc/tbtc.go b/pkg/extensions/tbtc/tbtc.go index 11578b870..ab0c882b3 100644 --- a/pkg/extensions/tbtc/tbtc.go +++ b/pkg/extensions/tbtc/tbtc.go @@ -15,6 +15,8 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ipfs/go-log" + + "github.com/keep-network/keep-common/pkg/chain/ethereum/ethutil" "github.com/keep-network/keep-common/pkg/subscription" chain "github.com/keep-network/keep-ecdsa/pkg/chain" ) @@ -767,8 +769,8 @@ func (t *tbtc) waitDepositStateChangeConfirmation( return false } - confirmed, err := chain.WaitForChainConfirmation( - t.chain, + confirmed, err := ethutil.WaitForChainConfirmation( + t.chain.BlockCounter(), currentBlock, t.blockConfirmations, stateCheck, @@ -800,8 +802,8 @@ func (t *tbtc) waitKeepInactivityConfirmation( return false } - isKeepActive, err := chain.WaitForChainConfirmation( - t.chain, + isKeepActive, err := ethutil.WaitForChainConfirmation( + t.chain.BlockCounter(), currentBlock, t.blockConfirmations, func() (bool, error) { From 09e80a90e7868cacf34ebc3b020926ec06baa4fe Mon Sep 17 00:00:00 2001 From: Lukasz Zimnoch Date: Thu, 12 Nov 2020 13:21:55 +0100 Subject: [PATCH 11/15] Use the confirmation waiter from keep-common II --- pkg/client/client.go | 22 ++++++++++++---------- pkg/client/registration.go | 6 ++++-- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 10f6b7c8a..fcdc14f9a 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -7,6 +7,8 @@ import ( "sync" "time" + "github.com/keep-network/keep-common/pkg/chain/ethereum/ethutil" + "github.com/ethereum/go-ethereum/common" "github.com/ipfs/go-log" @@ -65,8 +67,8 @@ func Initialize( return false } - isKeepActive, err := eth.WaitForChainConfirmation( - ethereumChain, + isKeepActive, err := ethutil.WaitForChainConfirmation( + ethereumChain.BlockCounter(), currentBlock, blockConfirmations, func() (bool, error) { @@ -537,8 +539,8 @@ func monitorSigningRequests( } defer requestedSignatures.remove(keepAddress, event.Digest) - isAwaitingSignature, err := eth.WaitForChainConfirmation( - ethereumChain, + isAwaitingSignature, err := ethutil.WaitForChainConfirmation( + ethereumChain.BlockCounter(), event.BlockNumber, blockConfirmations, func() (bool, error) { @@ -625,8 +627,8 @@ func checkAwaitingSignature( return } - isStillAwaitingSignature, err := eth.WaitForChainConfirmation( - ethereumChain, + isStillAwaitingSignature, err := ethutil.WaitForChainConfirmation( + ethereumChain.BlockCounter(), startBlock, blockConfirmations, func() (bool, error) { @@ -712,8 +714,8 @@ func monitorKeepClosedEvents( event.BlockNumber, ) - isKeepActive, err := eth.WaitForChainConfirmation( - ethereumChain, + isKeepActive, err := ethutil.WaitForChainConfirmation( + ethereumChain.BlockCounter(), event.BlockNumber, blockConfirmations, func() (bool, error) { @@ -776,8 +778,8 @@ func monitorKeepTerminatedEvent( event.BlockNumber, ) - isKeepActive, err := eth.WaitForChainConfirmation( - ethereumChain, + isKeepActive, err := ethutil.WaitForChainConfirmation( + ethereumChain.BlockCounter(), event.BlockNumber, blockConfirmations, func() (bool, error) { diff --git a/pkg/client/registration.go b/pkg/client/registration.go index 8e423065f..88cac417f 100644 --- a/pkg/client/registration.go +++ b/pkg/client/registration.go @@ -5,6 +5,8 @@ import ( "fmt" "time" + "github.com/keep-network/keep-common/pkg/chain/ethereum/ethutil" + "github.com/ethereum/go-ethereum/common" eth "github.com/keep-network/keep-ecdsa/pkg/chain" ) @@ -286,8 +288,8 @@ func monitorSignerPoolStatus( ) } - isRegistered, err := eth.WaitForChainConfirmation( - ethereumChain, + isRegistered, err := ethutil.WaitForChainConfirmation( + ethereumChain.BlockCounter(), statusCheckBlock, blockConfirmations, func() (bool, error) { From 29d815a003aeba2fdfcd06acba9fc691b38a93e5 Mon Sep 17 00:00:00 2001 From: Lukasz Zimnoch Date: Thu, 12 Nov 2020 13:27:33 +0100 Subject: [PATCH 12/15] Update keep-common version --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 752cb9bb0..34ce22ec7 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,7 @@ require ( github.com/gogo/protobuf v1.3.1 github.com/google/gofuzz v1.1.0 github.com/ipfs/go-log v1.0.4 - github.com/keep-network/keep-common v1.2.1-0.20201112121304-d43f4165b8fa + github.com/keep-network/keep-common v1.2.1-0.20201112122623-70d5d647ce08 github.com/keep-network/keep-core v1.3.0 github.com/keep-network/tbtc v1.1.1-0.20201026093513-cb9246987718 github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index b0eec235a..30eaf4a5b 100644 --- a/go.sum +++ b/go.sum @@ -335,8 +335,8 @@ github.com/keep-network/keep-common v1.2.0 h1:hVd2tTd7vL+9CQP5Ntk5kjs+GYvkgrRNBc github.com/keep-network/keep-common v1.2.0/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo= github.com/keep-network/keep-common v1.2.1-0.20201020114759-19c123cbd4f4 h1:CivupPSFswHACua5xZGKdeYxsCQ2cmRomTIBh8kfk70= github.com/keep-network/keep-common v1.2.1-0.20201020114759-19c123cbd4f4/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo= -github.com/keep-network/keep-common v1.2.1-0.20201112121304-d43f4165b8fa h1:OyKF/ha+P0nznQaL0GttWeZ8m+ZEHzrvkzudaNT+V8Y= -github.com/keep-network/keep-common v1.2.1-0.20201112121304-d43f4165b8fa/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo= +github.com/keep-network/keep-common v1.2.1-0.20201112122623-70d5d647ce08 h1:MSYhLMSkfMm60pcQbFPgG2twST4xMVvYyvEm7+0bXIU= +github.com/keep-network/keep-common v1.2.1-0.20201112122623-70d5d647ce08/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo= github.com/keep-network/keep-core v1.3.0 h1:7Tb33EmO/ntHOEbOiYciRlBhqu5Ln6KemWCaYK0Z6LA= github.com/keep-network/keep-core v1.3.0/go.mod h1:1KsSSTQoN754TrFLW7kLy50pOG2CQ4BOfnJqdvEG7FA= github.com/keep-network/tbtc v1.1.1-0.20201026093513-cb9246987718 h1:/ZNMBY7y6hfzCYA8mgtHnspGO26OmWV3sDehyGnqRyY= From a300b7723ce9a0272ed75c2b6c59630371a12143 Mon Sep 17 00:00:00 2001 From: Lukasz Zimnoch Date: Thu, 12 Nov 2020 14:50:56 +0100 Subject: [PATCH 13/15] Display warning if stop event is not confirmed --- pkg/extensions/tbtc/tbtc.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/pkg/extensions/tbtc/tbtc.go b/pkg/extensions/tbtc/tbtc.go index ab0c882b3..301d7febe 100644 --- a/pkg/extensions/tbtc/tbtc.go +++ b/pkg/extensions/tbtc/tbtc.go @@ -113,6 +113,13 @@ func (t *tbtc) monitorRetrievePubKey( initialDepositState, ) { handler(depositAddress) + } else { + logger.Warningf( + "retrieve pubkey monitoring stop event for "+ + "deposit [%v] is not confirmed; "+ + "monitoring will be continued", + depositAddress, + ) } }) } @@ -187,6 +194,13 @@ func (t *tbtc) monitorProvideRedemptionSignature( initialDepositState, ) { handler(depositAddress) + } else { + logger.Warningf( + "provide redemption signature monitoring stop "+ + "event for deposit [%v] is not confirmed; "+ + "monitoring will be continued", + depositAddress, + ) } }, ) @@ -202,6 +216,13 @@ func (t *tbtc) monitorProvideRedemptionSignature( initialDepositState, ) { handler(depositAddress) + } else { + logger.Warningf( + "provide redemption signature monitoring stop "+ + "event for deposit [%v] is not confirmed; "+ + "monitoring will be continued", + depositAddress, + ) } }, ) @@ -344,6 +365,13 @@ func (t *tbtc) monitorProvideRedemptionProof( initialDepositState, ) { handler(depositAddress) + } else { + logger.Warningf( + "provide redemption proof monitoring stop "+ + "event for deposit [%v] is not confirmed; "+ + "monitoring will be continued", + depositAddress, + ) } }, ) @@ -359,6 +387,13 @@ func (t *tbtc) monitorProvideRedemptionProof( initialDepositState, ) { handler(depositAddress) + } else { + logger.Warningf( + "provide redemption proof monitoring stop "+ + "event for deposit [%v] is not confirmed; "+ + "monitoring will be continued", + depositAddress, + ) } }, ) From 677bce8c66a3db6cbd275df7b0ec1ede34a86b8b Mon Sep 17 00:00:00 2001 From: Lukasz Zimnoch Date: Thu, 12 Nov 2020 15:16:28 +0100 Subject: [PATCH 14/15] Log messages and naming improvements --- pkg/extensions/tbtc/tbtc.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pkg/extensions/tbtc/tbtc.go b/pkg/extensions/tbtc/tbtc.go index 301d7febe..a4d14a460 100644 --- a/pkg/extensions/tbtc/tbtc.go +++ b/pkg/extensions/tbtc/tbtc.go @@ -29,6 +29,7 @@ const ( defaultBlockConfirmations = 12 ) +// TODO: Resume monitoring after client restart // Initialize initializes extension specific to the TBTC application. func Initialize(ctx context.Context, chain chain.TBTCHandle) error { logger.Infof("initializing tbtc extension") @@ -730,7 +731,7 @@ func (t *tbtc) watchKeepClosed( keepClosedSubscription, err := t.chain.OnKeepClosed( common.HexToAddress(keepAddress), func(_ *chain.KeepClosedEvent) { - if t.waitKeepInactivityConfirmation(keepAddress) { + if t.waitKeepNotActiveConfirmation(keepAddress) { signalChan <- struct{}{} } }, @@ -742,7 +743,7 @@ func (t *tbtc) watchKeepClosed( keepTerminatedSubscription, err := t.chain.OnKeepTerminated( common.HexToAddress(keepAddress), func(_ *chain.KeepTerminatedEvent) { - if t.waitKeepInactivityConfirmation(keepAddress) { + if t.waitKeepNotActiveConfirmation(keepAddress) { signalChan <- struct{}{} } }, @@ -823,14 +824,14 @@ func (t *tbtc) waitDepositStateChangeConfirmation( return confirmed } -func (t *tbtc) waitKeepInactivityConfirmation( +func (t *tbtc) waitKeepNotActiveConfirmation( keepAddress string, ) bool { currentBlock, err := t.chain.BlockCounter().CurrentBlock() if err != nil { logger.Errorf( "could not get current block while confirming "+ - "inactivity for keep [%v]: [%v]", + "keep [%v] is not active: [%v]", keepAddress, err, ) @@ -847,7 +848,7 @@ func (t *tbtc) waitKeepInactivityConfirmation( ) if err != nil { logger.Errorf( - "could not confirm inactivity for keep [%v]: [%v]", + "could not confirm if keep [%v] is not active: [%v]", keepAddress, err, ) From 229a51e967eaa6292939e5de35f5f4ff55aa875b Mon Sep 17 00:00:00 2001 From: Lukasz Zimnoch Date: Thu, 12 Nov 2020 15:26:11 +0100 Subject: [PATCH 15/15] Use `chainutil.WaitForBlockConfirmations` --- go.mod | 2 +- go.sum | 4 ++-- pkg/client/client.go | 12 ++++++------ pkg/client/registration.go | 4 ++-- pkg/extensions/tbtc/tbtc.go | 6 +++--- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index 34ce22ec7..41da76c5b 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,7 @@ require ( github.com/gogo/protobuf v1.3.1 github.com/google/gofuzz v1.1.0 github.com/ipfs/go-log v1.0.4 - github.com/keep-network/keep-common v1.2.1-0.20201112122623-70d5d647ce08 + github.com/keep-network/keep-common v1.2.1-0.20201112133003-054c43201b4b github.com/keep-network/keep-core v1.3.0 github.com/keep-network/tbtc v1.1.1-0.20201026093513-cb9246987718 github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index 30eaf4a5b..0bbe5fd48 100644 --- a/go.sum +++ b/go.sum @@ -335,8 +335,8 @@ github.com/keep-network/keep-common v1.2.0 h1:hVd2tTd7vL+9CQP5Ntk5kjs+GYvkgrRNBc github.com/keep-network/keep-common v1.2.0/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo= github.com/keep-network/keep-common v1.2.1-0.20201020114759-19c123cbd4f4 h1:CivupPSFswHACua5xZGKdeYxsCQ2cmRomTIBh8kfk70= github.com/keep-network/keep-common v1.2.1-0.20201020114759-19c123cbd4f4/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo= -github.com/keep-network/keep-common v1.2.1-0.20201112122623-70d5d647ce08 h1:MSYhLMSkfMm60pcQbFPgG2twST4xMVvYyvEm7+0bXIU= -github.com/keep-network/keep-common v1.2.1-0.20201112122623-70d5d647ce08/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo= +github.com/keep-network/keep-common v1.2.1-0.20201112133003-054c43201b4b h1:ErsUI/oSwXPUHxNq7j/ttZXn3WOn4bOCf2QJoo2gvZE= +github.com/keep-network/keep-common v1.2.1-0.20201112133003-054c43201b4b/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo= github.com/keep-network/keep-core v1.3.0 h1:7Tb33EmO/ntHOEbOiYciRlBhqu5Ln6KemWCaYK0Z6LA= github.com/keep-network/keep-core v1.3.0/go.mod h1:1KsSSTQoN754TrFLW7kLy50pOG2CQ4BOfnJqdvEG7FA= github.com/keep-network/tbtc v1.1.1-0.20201026093513-cb9246987718 h1:/ZNMBY7y6hfzCYA8mgtHnspGO26OmWV3sDehyGnqRyY= diff --git a/pkg/client/client.go b/pkg/client/client.go index fcdc14f9a..ee55d69d7 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -7,7 +7,7 @@ import ( "sync" "time" - "github.com/keep-network/keep-common/pkg/chain/ethereum/ethutil" + "github.com/keep-network/keep-common/pkg/chain/chainutil" "github.com/ethereum/go-ethereum/common" "github.com/ipfs/go-log" @@ -67,7 +67,7 @@ func Initialize( return false } - isKeepActive, err := ethutil.WaitForChainConfirmation( + isKeepActive, err := chainutil.WaitForBlockConfirmations( ethereumChain.BlockCounter(), currentBlock, blockConfirmations, @@ -539,7 +539,7 @@ func monitorSigningRequests( } defer requestedSignatures.remove(keepAddress, event.Digest) - isAwaitingSignature, err := ethutil.WaitForChainConfirmation( + isAwaitingSignature, err := chainutil.WaitForBlockConfirmations( ethereumChain.BlockCounter(), event.BlockNumber, blockConfirmations, @@ -627,7 +627,7 @@ func checkAwaitingSignature( return } - isStillAwaitingSignature, err := ethutil.WaitForChainConfirmation( + isStillAwaitingSignature, err := chainutil.WaitForBlockConfirmations( ethereumChain.BlockCounter(), startBlock, blockConfirmations, @@ -714,7 +714,7 @@ func monitorKeepClosedEvents( event.BlockNumber, ) - isKeepActive, err := ethutil.WaitForChainConfirmation( + isKeepActive, err := chainutil.WaitForBlockConfirmations( ethereumChain.BlockCounter(), event.BlockNumber, blockConfirmations, @@ -778,7 +778,7 @@ func monitorKeepTerminatedEvent( event.BlockNumber, ) - isKeepActive, err := ethutil.WaitForChainConfirmation( + isKeepActive, err := chainutil.WaitForBlockConfirmations( ethereumChain.BlockCounter(), event.BlockNumber, blockConfirmations, diff --git a/pkg/client/registration.go b/pkg/client/registration.go index 88cac417f..8620cf66d 100644 --- a/pkg/client/registration.go +++ b/pkg/client/registration.go @@ -5,7 +5,7 @@ import ( "fmt" "time" - "github.com/keep-network/keep-common/pkg/chain/ethereum/ethutil" + "github.com/keep-network/keep-common/pkg/chain/chainutil" "github.com/ethereum/go-ethereum/common" eth "github.com/keep-network/keep-ecdsa/pkg/chain" @@ -288,7 +288,7 @@ func monitorSignerPoolStatus( ) } - isRegistered, err := ethutil.WaitForChainConfirmation( + isRegistered, err := chainutil.WaitForBlockConfirmations( ethereumChain.BlockCounter(), statusCheckBlock, blockConfirmations, diff --git a/pkg/extensions/tbtc/tbtc.go b/pkg/extensions/tbtc/tbtc.go index a4d14a460..311ce5a79 100644 --- a/pkg/extensions/tbtc/tbtc.go +++ b/pkg/extensions/tbtc/tbtc.go @@ -16,7 +16,7 @@ import ( "github.com/ipfs/go-log" - "github.com/keep-network/keep-common/pkg/chain/ethereum/ethutil" + "github.com/keep-network/keep-common/pkg/chain/chainutil" "github.com/keep-network/keep-common/pkg/subscription" chain "github.com/keep-network/keep-ecdsa/pkg/chain" ) @@ -805,7 +805,7 @@ func (t *tbtc) waitDepositStateChangeConfirmation( return false } - confirmed, err := ethutil.WaitForChainConfirmation( + confirmed, err := chainutil.WaitForBlockConfirmations( t.chain.BlockCounter(), currentBlock, t.blockConfirmations, @@ -838,7 +838,7 @@ func (t *tbtc) waitKeepNotActiveConfirmation( return false } - isKeepActive, err := ethutil.WaitForChainConfirmation( + isKeepActive, err := chainutil.WaitForBlockConfirmations( t.chain.BlockCounter(), currentBlock, t.blockConfirmations,