Skip to content

Commit

Permalink
Pass wallet PKH to the proposal generator
Browse files Browse the repository at this point in the history
Proposal generation must have a way to identify the wallet. Code currently
used by the wallet maintainer (`pkg/maintainer/wallet`) uses 20-byte
wallet public key hashes for that purpose. As we plan to reuse it in the
new mechanism, we should use the same identifier.
  • Loading branch information
lukasz-zimnoch committed Nov 28, 2023
1 parent 33a1475 commit 1be0521
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
7 changes: 5 additions & 2 deletions pkg/tbtc/coordination.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ func (cf *coordinationFault) String() string {
// wallet's state. If none of the actions are valid, the generator
// should return a noopProposal.
type coordinationProposalGenerator func(
walletPublicKeyHash [20]byte,
actionsChecklist []WalletActionType,
) (coordinationProposal, error)

Expand Down Expand Up @@ -506,7 +507,9 @@ func (ce *coordinationExecutor) leaderRoutine(
coordinationBlock uint64,
actionsChecklist []WalletActionType,
) (coordinationProposal, error) {
proposal, err := ce.proposalGenerator(actionsChecklist)
walletPublicKeyHash := ce.walletPublicKeyHash()

proposal, err := ce.proposalGenerator(walletPublicKeyHash, actionsChecklist)
if err != nil {
return nil, fmt.Errorf("failed to generate proposal: [%v]", err)
}
Expand All @@ -520,7 +523,7 @@ func (ce *coordinationExecutor) leaderRoutine(
message := &coordinationMessage{
senderID: senderID,
coordinationBlock: coordinationBlock,
walletPublicKeyHash: ce.walletPublicKeyHash(),
walletPublicKeyHash: walletPublicKeyHash,
proposal: proposal,
}

Expand Down
24 changes: 14 additions & 10 deletions pkg/tbtc/coordination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,14 @@ func TestCoordinationExecutor_LeaderRoutine(t *testing.T) {
t.Fatal(err)
}

// 20-byte public key hash corresponding to the public key above.
buffer, err := hex.DecodeString("aa768412ceed10bd423c025542ca90071f9fb62d")
if err != nil {
t.Fatal(err)
}
var publicKeyHash [20]byte
copy(publicKeyHash[:], buffer)

coordinatedWallet := wallet{
// Set only relevant fields.
publicKey: unmarshalPublicKey(publicKeyHex),
Expand All @@ -402,12 +410,15 @@ func TestCoordinationExecutor_LeaderRoutine(t *testing.T) {
// sender.
membersIndexes := []group.MemberIndex{77, 5, 10}

proposalGenerator := func(actionsChecklist []WalletActionType) (
proposalGenerator := func(
walletPublicKeyHash [20]byte,
actionsChecklist []WalletActionType,
) (
coordinationProposal,
error,
) {
for _, action := range actionsChecklist {
if action == ActionHeartbeat {
if walletPublicKeyHash == publicKeyHash && action == ActionHeartbeat {
return &HeartbeatProposal{
Message: []byte("heartbeat message"),
}, nil
Expand Down Expand Up @@ -482,17 +493,10 @@ func TestCoordinationExecutor_LeaderRoutine(t *testing.T) {
)
}

buffer, err := hex.DecodeString("aa768412ceed10bd423c025542ca90071f9fb62d")
if err != nil {
t.Fatal(err)
}
var expectedWalletPublicKeyHash [20]byte
copy(expectedWalletPublicKeyHash[:], buffer)

expectedMessage := &coordinationMessage{
senderID: 5,
coordinationBlock: 900,
walletPublicKeyHash: expectedWalletPublicKeyHash,
walletPublicKeyHash: publicKeyHash,
proposal: expectedProposal,
}

Expand Down
10 changes: 9 additions & 1 deletion pkg/tbtc/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,20 @@ func (n *node) getCoordinationExecutor(
return nil, false, fmt.Errorf("failed to get operator address: [%v]", err)
}

proposalGenerator := func(
walletPublicKeyHash [20]byte,
actionsChecklist []WalletActionType,
) (coordinationProposal, error) {
// TODO: Implement proposal generation.
return &noopProposal{}, nil
}

executor := newCoordinationExecutor(
n.chain,
wallet,
membersIndexes,
operatorAddress,
nil, // TODO: Set a proper proposal generator.
proposalGenerator,
broadcastChannel,
membershipValidator,
n.protocolLatch,
Expand Down

0 comments on commit 1be0521

Please sign in to comment.