Skip to content

Commit

Permalink
Added counter of consecutive heartbeat failure
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Apr 15, 2024
1 parent d3ec5a9 commit 6c57105
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 18 deletions.
74 changes: 66 additions & 8 deletions pkg/tbtc/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ const (
// another action has been already requested by the coordinator.
// The value of 25 blocks is roughly 5 minutes, assuming 12 seconds per block.
heartbeatRequestTimeoutSafetyMarginBlocks = 25
// heartbeatSigningMinimumActiveOperators determines the minimum number of
// active operators during signing for a heartbeat to be considered valid.
heartbeatSigningMinimumActiveOperators = 70
// heartbeatConsecutiveFailuresThreshold determines the number of consecutive
// heartbeat failures required to trigger inactivity operator notification.
heartbeatConsecutiveFailureThreshold = 3
)

type HeartbeatProposal struct {
Expand Down Expand Up @@ -57,7 +63,9 @@ type heartbeatAction struct {
executingWallet wallet
signingExecutor heartbeatSigningExecutor

proposal *HeartbeatProposal
proposal *HeartbeatProposal
failureCounter *uint

startBlock uint64
expiryBlock uint64

Expand All @@ -70,6 +78,7 @@ func newHeartbeatAction(
executingWallet wallet,
signingExecutor heartbeatSigningExecutor,
proposal *HeartbeatProposal,
failureCounter *uint,
startBlock uint64,
expiryBlock uint64,
waitForBlockFn waitForBlockFn,
Expand All @@ -80,6 +89,7 @@ func newHeartbeatAction(
executingWallet: executingWallet,
signingExecutor: signingExecutor,
proposal: proposal,
failureCounter: failureCounter,
startBlock: startBlock,
expiryBlock: expiryBlock,
waitForBlockFn: waitForBlockFn,
Expand Down Expand Up @@ -123,21 +133,69 @@ func (ha *heartbeatAction) execute() error {
)
defer cancelHeartbeatCtx()

signature, _, _, err := ha.signingExecutor.sign(
signature, activeOperatorsCount, _, err := ha.signingExecutor.sign(
heartbeatCtx,
messageToSign,
ha.startBlock,
)

// If there was no error and the number of active operators during signing
// was enough, we can consider the heartbeat procedure as successful.
if err == nil && activeOperatorsCount >= heartbeatSigningMinimumActiveOperators {
logger.Infof(
"successfully generated signature [%s] for heartbeat message [0x%x]",
signature,
ha.proposal.Message[:],
)

// Reset the counter for consecutive heartbeat failure.
*ha.failureCounter = 0

return nil
}

// If there was an error or the number of active operators during signing
// was not enough, we must consider the heartbeat procedure as a failure.
if err != nil {
return fmt.Errorf("cannot sign heartbeat message: [%v]", err)
logger.Infof("error while generating heartbeat signature: [%v]", err)
} else {
logger.Infof(
"not enough active operators during signing; required [%d]: "+
"actual [%d]",
activeOperatorsCount,
heartbeatSigningMinimumActiveOperators,
)
}

logger.Infof(
"generated signature [%s] for heartbeat message [0x%x]",
signature,
ha.proposal.Message[:],
)
// Increment the heartbeat failure counter.
*ha.failureCounter++

// If the number of consecutive heartbeat failures does not exceed the
// threshold do not notify about operator inactivity.
if *ha.failureCounter < heartbeatConsecutiveFailureThreshold {
logger.Infof(
"leaving without notifying about operator inactivity; current "+
"heartbeat failure count is [%d]",
*ha.failureCounter,
)
return nil
}

// The value of consecutive heartbeat failures exceeds the threshold.
// Proceed with operator inactivity notification.
err = ha.notifyOperatorInactivity()
if err != nil {
return fmt.Errorf(
"error while notifying about operator inactivity [%v]]",
err,
)
}

return nil
}

func (ha *heartbeatAction) notifyOperatorInactivity() error {
// TODO: Implement
return nil
}

Expand Down
28 changes: 18 additions & 10 deletions pkg/tbtc/heartbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func TestHeartbeatAction_HappyPath(t *testing.T) {
},
}

heartbeatFailureCounter := uint(0)

// sha256(sha256(messageToSign))
sha256d, err := hex.DecodeString("38d30dacec5083c902952ce99fc0287659ad0b1ca2086827a8e78b0bef2c8bc1")
if err != nil {
Expand All @@ -48,6 +50,7 @@ func TestHeartbeatAction_HappyPath(t *testing.T) {
},
mockExecutor,
proposal,
&heartbeatFailureCounter,
startBlock,
expiryBlock,
func(ctx context.Context, blockHeight uint64) error {
Expand Down Expand Up @@ -93,6 +96,8 @@ func TestHeartbeatAction_SigningError(t *testing.T) {
},
}

heartbeatFailureCounter := uint(0)

hostChain := Connect()
hostChain.setHeartbeatProposalValidationResult(proposal, true)

Expand All @@ -107,23 +112,26 @@ func TestHeartbeatAction_SigningError(t *testing.T) {
},
mockExecutor,
proposal,
&heartbeatFailureCounter,
startBlock,
expiryBlock,
func(ctx context.Context, blockHeight uint64) error {
return nil
},
)

err = action.execute()
if err == nil {
t.Fatal("expected error to be returned")
}
testutils.AssertStringsEqual(
t,
"error message",
"cannot sign heartbeat message: [oofta]",
err.Error(),
)
action.execute()
// TODO: Uncomment
// err = action.execute()
// if err == nil {
// t.Fatal("expected error to be returned")
// }
// testutils.AssertStringsEqual(
// t,
// "error message",
// "cannot sign heartbeat message: [oofta]",
// err.Error(),
// )
}

type mockHeartbeatSigningExecutor struct {
Expand Down
6 changes: 6 additions & 0 deletions pkg/tbtc/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ type node struct {
// dkgExecutor MUST NOT be used outside this struct.
dkgExecutor *dkgExecutor

// heartbeatFailureCounter is the counter keeping track of consecutive
// heartbeat failure. It reset to zero after each successful heartbeat
// procedure.
heartbeatFailureCounter uint

signingExecutorsMutex sync.Mutex
// signingExecutors is the cache holding signing executors for specific wallets.
// The cache key is the uncompressed public key (with 04 prefix) of the wallet.
Expand Down Expand Up @@ -458,6 +463,7 @@ func (n *node) handleHeartbeatProposal(
wallet,
signingExecutor,
proposal,
&n.heartbeatFailureCounter,
startBlock,
expiryBlock,
n.waitForBlockHeight,
Expand Down

0 comments on commit 6c57105

Please sign in to comment.