Skip to content

Commit

Permalink
Returned number of active members count when signing
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Apr 8, 2024
1 parent 531d62a commit d3ec5a9
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 23 deletions.
8 changes: 6 additions & 2 deletions pkg/tbtc/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type heartbeatSigningExecutor interface {
ctx context.Context,
message *big.Int,
startBlock uint64,
) (*tecdsa.Signature, uint64, error)
) (*tecdsa.Signature, uint32, uint64, error)
}

// heartbeatAction is a walletAction implementation handling heartbeat requests
Expand Down Expand Up @@ -123,7 +123,11 @@ func (ha *heartbeatAction) execute() error {
)
defer cancelHeartbeatCtx()

signature, _, err := ha.signingExecutor.sign(heartbeatCtx, messageToSign, ha.startBlock)
signature, _, _, err := ha.signingExecutor.sign(
heartbeatCtx,
messageToSign,
ha.startBlock,
)
if err != nil {
return fmt.Errorf("cannot sign heartbeat message: [%v]", err)
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/tbtc/heartbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"context"
"encoding/hex"
"fmt"
"github.com/keep-network/keep-core/internal/testutils"
"github.com/keep-network/keep-core/pkg/tecdsa"
"math/big"
"testing"

"github.com/keep-network/keep-core/internal/testutils"
"github.com/keep-network/keep-core/pkg/tecdsa"
)

func TestHeartbeatAction_HappyPath(t *testing.T) {
Expand Down Expand Up @@ -136,13 +137,14 @@ func (mhse *mockHeartbeatSigningExecutor) sign(
ctx context.Context,
message *big.Int,
startBlock uint64,
) (*tecdsa.Signature, uint64, error) {
) (*tecdsa.Signature, uint32, uint64, error) {
mhse.requestedMessage = message
mhse.requestedStartBlock = startBlock

if mhse.shouldFail {
return nil, 0, fmt.Errorf("oofta")
return nil, 0, 0, fmt.Errorf("oofta")
}

return &tecdsa.Signature{}, startBlock + 1, nil
// TODO: Return the active members count and use it in unit tests.
return &tecdsa.Signature{}, 0, startBlock + 1, nil
}
27 changes: 15 additions & 12 deletions pkg/tbtc/signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (se *signingExecutor) signBatch(
signingStartBlock = endBlocks[i-1] + signingBatchInterludeBlocks
}

signature, endBlock, err := se.sign(ctx, message, signingStartBlock)
signature, _, endBlock, err := se.sign(ctx, message, signingStartBlock)
if err != nil {
return nil, err
}
Expand All @@ -167,23 +167,24 @@ func (se *signingExecutor) signBatch(
// triggered according to the given start block. If the message cannot be signed
// within a limited time window, an error is returned. If the message was
// signed successfully, this function returns the signature along with the
// block at which the signature was calculated. This end block is common for
// all wallet signers so can be used as a synchronization point.
// number of active members that participated in signing, the block at which the
// signature was calculated. The end block is common for all wallet signers so
// can be used as a synchronization point.
func (se *signingExecutor) sign(
ctx context.Context,
message *big.Int,
startBlock uint64,
) (*tecdsa.Signature, uint64, error) {
) (*tecdsa.Signature, uint32, uint64, error) {
if lockAcquired := se.lock.TryAcquire(1); !lockAcquired {
return nil, 0, errSigningExecutorBusy
return nil, 0, 0, errSigningExecutorBusy
}
defer se.lock.Release(1)

wallet := se.wallet()

walletPublicKeyBytes, err := marshalPublicKey(wallet.publicKey)
if err != nil {
return nil, 0, fmt.Errorf("cannot marshal wallet public key: [%v]", err)
return nil, 0, 0, fmt.Errorf("cannot marshal wallet public key: [%v]", err)
}

loopTimeoutBlock := startBlock +
Expand All @@ -197,8 +198,9 @@ func (se *signingExecutor) sign(
)

type signingOutcome struct {
signature *tecdsa.Signature
endBlock uint64
signature *tecdsa.Signature
activeMembersCount uint32
endBlock uint64
}

wg := sync.WaitGroup{}
Expand Down Expand Up @@ -365,8 +367,9 @@ func (se *signingExecutor) sign(
)

signingOutcomeChan <- &signingOutcome{
signature: loopResult.result.Signature,
endBlock: loopResult.latestEndBlock,
signature: loopResult.result.Signature,
activeMembersCount: loopResult.activeMembersCount,
endBlock: loopResult.latestEndBlock,
}
}(currentSigner)
}
Expand All @@ -383,9 +386,9 @@ func (se *signingExecutor) sign(
// signer, that means all signers failed and have not produced a signature.
select {
case outcome := <-signingOutcomeChan:
return outcome.signature, outcome.endBlock, nil
return outcome.signature, outcome.activeMembersCount, outcome.endBlock, nil
default:
return nil, 0, fmt.Errorf("all signers failed")
return nil, 0, 0, fmt.Errorf("all signers failed")
}
}

Expand Down
7 changes: 6 additions & 1 deletion pkg/tbtc/signing_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"crypto/sha256"
"encoding/binary"
"fmt"
"github.com/keep-network/keep-core/pkg/protocol/announcer"
"math/big"
"math/rand"
"sort"

"github.com/keep-network/keep-core/pkg/protocol/announcer"

"github.com/ipfs/go-log/v2"
"github.com/keep-network/keep-core/pkg/chain"
"github.com/keep-network/keep-core/pkg/protocol/group"
Expand Down Expand Up @@ -143,6 +144,9 @@ type signingAttemptFn func(*signingAttemptParams) (*signing.Result, uint64, erro
type signingRetryLoopResult struct {
// result is the outcome of the signing process.
result *signing.Result
// activeMembersCount is the number of members that participated in the
// signing process.
activeMembersCount uint32
// latestEndBlock is the block at which the slowest signer of the successful
// signing attempt completed signature computation. This block is also
// the common end block accepted by all other members of the signing group.
Expand Down Expand Up @@ -407,6 +411,7 @@ func (srl *signingRetryLoop) start(

return &signingRetryLoopResult{
result: result,
activeMembersCount: uint32(len(readyMembersIndexes)),
latestEndBlock: latestEndBlock,
attemptTimeoutBlock: timeoutBlock,
}, nil
Expand Down
8 changes: 8 additions & 0 deletions pkg/tbtc/signing_loop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func TestSigningRetryLoop(t *testing.T) {
expectedErr: nil,
expectedResult: &signingRetryLoopResult{
result: testResult,
activeMembersCount: 10,
latestEndBlock: 215, // the end block resolved by the done check phase
attemptTimeoutBlock: 236, // start block of the first attempt + 30
},
Expand Down Expand Up @@ -151,6 +152,7 @@ func TestSigningRetryLoop(t *testing.T) {
expectedErr: nil,
expectedResult: &signingRetryLoopResult{
result: testResult,
activeMembersCount: 6,
latestEndBlock: 215, // the end block resolved by the done check phase
attemptTimeoutBlock: 236, // start block of the first attempt + 30
},
Expand Down Expand Up @@ -205,6 +207,7 @@ func TestSigningRetryLoop(t *testing.T) {
expectedErr: nil,
expectedResult: &signingRetryLoopResult{
result: testResult,
activeMembersCount: 10,
latestEndBlock: 260, // the end block resolved by the done check phase
attemptTimeoutBlock: 277, // start block of the second attempt + 30
},
Expand Down Expand Up @@ -261,6 +264,7 @@ func TestSigningRetryLoop(t *testing.T) {
expectedErr: nil,
expectedResult: &signingRetryLoopResult{
result: testResult,
activeMembersCount: 10,
latestEndBlock: 260, // the end block resolved by the done check phase
attemptTimeoutBlock: 277, // start block of the second attempt + 30
},
Expand Down Expand Up @@ -317,6 +321,7 @@ func TestSigningRetryLoop(t *testing.T) {
expectedErr: nil,
expectedResult: &signingRetryLoopResult{
result: testResult,
activeMembersCount: 10,
latestEndBlock: 260, // the end block resolved by the done check phase
attemptTimeoutBlock: 277, // start block of the second attempt + 30
},
Expand Down Expand Up @@ -365,6 +370,7 @@ func TestSigningRetryLoop(t *testing.T) {
expectedErr: nil,
expectedResult: &signingRetryLoopResult{
result: testResult,
activeMembersCount: 10,
latestEndBlock: 260, // the end block resolved by the done check phase
attemptTimeoutBlock: 277, // start block of the second attempt + 30
},
Expand Down Expand Up @@ -436,6 +442,7 @@ func TestSigningRetryLoop(t *testing.T) {
expectedErr: nil,
expectedResult: &signingRetryLoopResult{
result: testResult,
activeMembersCount: 10,
latestEndBlock: 260, // the end block resolved by the done check phase
attemptTimeoutBlock: 277, // start block of the second attempt + 30
},
Expand Down Expand Up @@ -541,6 +548,7 @@ func TestSigningRetryLoop(t *testing.T) {
expectedErr: nil,
expectedResult: &signingRetryLoopResult{
result: testResult,
activeMembersCount: 10,
latestEndBlock: 260, // the end block resolved by the done check phase
attemptTimeoutBlock: 277, // start block of the second attempt + 30
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/tbtc/signing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestSigningExecutor_Sign(t *testing.T) {
message := big.NewInt(100)
startBlock := uint64(0)

signature, endBlock, err := executor.sign(ctx, message, startBlock)
signature, _, endBlock, err := executor.sign(ctx, message, startBlock)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -59,13 +59,13 @@ func TestSigningExecutor_Sign_Busy(t *testing.T) {

errChan := make(chan error, 1)
go func() {
_, _, err := executor.sign(ctx, message, startBlock)
_, _, _, err := executor.sign(ctx, message, startBlock)
errChan <- err
}()

time.Sleep(100 * time.Millisecond)

_, _, err := executor.sign(ctx, message, startBlock)
_, _, _, err := executor.sign(ctx, message, startBlock)
testutils.AssertErrorsSame(t, errSigningExecutorBusy, err)

err = <-errChan
Expand Down

0 comments on commit d3ec5a9

Please sign in to comment.