Skip to content

Commit

Permalink
Added moving funds message unmarshaling
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Jan 4, 2024
1 parent 590e84d commit d943730
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 28 deletions.
105 changes: 95 additions & 10 deletions pkg/tbtc/gen/pb/message.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion pkg/tbtc/gen/pb/message.proto
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ message DepositSweepProposal {
message RedemptionProposal {
repeated bytes redeemersOutputScripts = 1;
bytes redemptionTxFee = 2;
}
}

message MovingFundsProposal {
bytes walletPublicKeyHash = 1;
repeated bytes targetWallets = 2;
bytes movingFundsTxFee = 3;
}
33 changes: 29 additions & 4 deletions pkg/tbtc/marshaling.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,13 +398,38 @@ func (rp *RedemptionProposal) Unmarshal(bytes []byte) error {

// Marshal converts the movingFundsProposal to a byte array.
func (mfp *MovingFundsProposal) Marshal() ([]byte, error) {
// TODO: Implement
return nil, nil
targetWallets := make([][]byte, len(mfp.TargetWallets))

for i, wallet := range mfp.TargetWallets {
targetWallet := make([]byte, len(wallet))
copy(targetWallet, wallet[:])

targetWallets[i] = targetWallet
}

return proto.Marshal(
&pb.MovingFundsProposal{
WalletPublicKeyHash: mfp.WalletPublicKeyHash[:],
TargetWallets: targetWallets,
MovingFundsTxFee: mfp.MovingFundsTxFee.Bytes(),
})
}

// Unmarshal converts a byte array back to the movingFundsProposal.
func (mfp *MovingFundsProposal) Unmarshal(bytes []byte) error {
// TODO: Implement
func (mfp *MovingFundsProposal) Unmarshal(data []byte) error {
pbMsg := pb.MovingFundsProposal{}
if err := proto.Unmarshal(data, &pbMsg); err != nil {
return fmt.Errorf("failed to unmarshal MovingFundsProposal: [%v]", err)
}

copy(mfp.WalletPublicKeyHash[:], pbMsg.WalletPublicKeyHash)

mfp.TargetWallets = make([][20]byte, len(pbMsg.TargetWallets))
for i, wallet := range pbMsg.TargetWallets {
copy(mfp.TargetWallets[i][:], wallet)
}

mfp.MovingFundsTxFee = new(big.Int).SetBytes(pbMsg.MovingFundsTxFee)
return nil
}

Expand Down
73 changes: 60 additions & 13 deletions pkg/tbtc/marshaling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"crypto/ecdsa"
"crypto/elliptic"
"encoding/hex"
"github.com/keep-network/keep-core/pkg/bitcoin"
"math/big"
"reflect"
"testing"

"github.com/keep-network/keep-core/pkg/bitcoin"

fuzz "github.com/google/gofuzz"

"github.com/keep-network/keep-core/internal/testutils"
Expand Down Expand Up @@ -126,6 +127,21 @@ func TestCoordinationMessage_MarshalingRoundtrip(t *testing.T) {
return parsed
}

toByte20 := func(s string) [20]byte {
bytes, err := hex.DecodeString(s)
if err != nil {
t.Fatal(err)
}

if len(bytes) != 20 {
t.Fatal("incorrect hexstring length")
}

var result [20]byte
copy(result[:], bytes[:])
return result
}

tests := map[string]struct {
proposal CoordinationProposal
}{
Expand Down Expand Up @@ -168,23 +184,25 @@ func TestCoordinationMessage_MarshalingRoundtrip(t *testing.T) {
RedemptionTxFee: big.NewInt(10000),
},
},
// TODO: Uncomment when moving funds support is implemented.
// "with moving funds proposal": {
// proposal: &MovingFundsProposal{},
// },
"with moving funds proposal": {
proposal: &MovingFundsProposal{
WalletPublicKeyHash: toByte20(
"da7c1fb602db1931a3b815563b6f6fae6a58f224",
),
TargetWallets: [][20]byte{
toByte20("cb7d88a87c37aff0c1535fa4efe6f0a2406ea5e9"),
toByte20("f87eb7ec3b15a3fdd7b57754d765694b3e0b4bf4"),
},
MovingFundsTxFee: big.NewInt(10000),
},
},
// TODO: Uncomment when moved funds sweep support is implemented.
// "with moved funds sweep proposal": {
// proposal: &MovedFundsSweepProposal{},
// },
}

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

for testName, test := range tests {
t.Run(testName, func(t *testing.T) {
Expand Down Expand Up @@ -295,6 +313,35 @@ func TestFuzzCoordinationMessage_MarshalingRoundtrip_WithRedemptionProposal(t *t
}
}

func TestFuzzCoordinationMessage_MarshalingRoundtrip_WithMovingFundsProposal(t *testing.T) {
for i := 0; i < 10; i++ {
var (
senderID group.MemberIndex
coordinationBlock uint64
walletPublicKeyHash [20]byte
proposal MovingFundsProposal
)

f := fuzz.New().NilChance(0.1).
NumElements(0, 512).
Funcs(pbutils.FuzzFuncs()...)

f.Fuzz(&senderID)
f.Fuzz(&coordinationBlock)
f.Fuzz(&walletPublicKeyHash)
f.Fuzz(&proposal)

doneMessage := &coordinationMessage{
senderID: senderID,
coordinationBlock: coordinationBlock,
walletPublicKeyHash: walletPublicKeyHash,
proposal: &proposal,
}

_ = pbutils.RoundTrip(doneMessage, &coordinationMessage{})
}
}

func TestFuzzCoordinationMessage_MarshalingRoundtrip_WithNoopProposal(t *testing.T) {
for i := 0; i < 10; i++ {
var (
Expand Down
3 changes: 3 additions & 0 deletions pkg/tbtc/moving_funds.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tbtc

import (
"math/big"

"github.com/ipfs/go-log/v2"

"github.com/keep-network/keep-core/pkg/bitcoin"
Expand All @@ -18,6 +20,7 @@ type MovingFundsProposal struct {
// TODO: Remove WalletPublicKeyHash field.
WalletPublicKeyHash [20]byte
TargetWallets [][20]byte
MovingFundsTxFee *big.Int
}

func (mfp *MovingFundsProposal) ActionType() WalletActionType {
Expand Down

0 comments on commit d943730

Please sign in to comment.