Skip to content

Commit

Permalink
Minor refactor of unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Jan 25, 2024
1 parent ddbde93 commit 5bee22a
Showing 1 changed file with 51 additions and 74 deletions.
125 changes: 51 additions & 74 deletions pkg/tbtcpg/moving_funds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ import (
)

func TestMovingFundsAction_FindTargetWallets_CommitmentNotSubmittedYet(t *testing.T) {
walletPublicKeyHash := hexToByte20(
"ffb3f7538bfa98a511495dd96027cfbd57baf2fa",
)

var tests = map[string]struct {
sourceWalletPublicKeyHash [20]byte
walletBalance uint64
walletMaxBtcTransfer uint64
registeredWallets []walletInfo
liveWalletsCount uint32
expectedTargetWallets [][20]byte
expectedError error
walletBalance uint64
walletMaxBtcTransfer uint64
registeredWallets []walletInfo
liveWalletsCount uint32
expectedTargetWallets [][20]byte
expectedError error
}{
"success scenario": {
sourceWalletPublicKeyHash: hexToByte20(
"ffb3f7538bfa98a511495dd96027cfbd57baf2fa",
),
walletBalance: 2000000,
walletMaxBtcTransfer: 300000,
registeredWallets: []walletInfo{
Expand Down Expand Up @@ -85,9 +85,6 @@ func TestMovingFundsAction_FindTargetWallets_CommitmentNotSubmittedYet(t *testin
expectedError: nil,
},
"wallet max BTC transfer is zero": {
sourceWalletPublicKeyHash: hexToByte20(
"ffb3f7538bfa98a511495dd96027cfbd57baf2fa",
),
walletBalance: 10000,
walletMaxBtcTransfer: 0, // Set to zero.
registeredWallets: []walletInfo{},
Expand All @@ -96,9 +93,6 @@ func TestMovingFundsAction_FindTargetWallets_CommitmentNotSubmittedYet(t *testin
expectedError: tbtcpg.ErrMaxBtcTransferZero,
},
"not enough live wallets": {
sourceWalletPublicKeyHash: hexToByte20(
"ffb3f7538bfa98a511495dd96027cfbd57baf2fa",
),
walletBalance: 2000000,
walletMaxBtcTransfer: 300000,
// Simulate there should be two Live wallets, but set only one Live
Expand Down Expand Up @@ -165,7 +159,7 @@ func TestMovingFundsAction_FindTargetWallets_CommitmentNotSubmittedYet(t *testin

targetWallets, alreadySubmitted, err := task.FindTargetWallets(
&testutils.MockLogger{},
test.sourceWalletPublicKeyHash,
walletPublicKeyHash,
targetWalletsCommitmentHash,
test.walletBalance,
test.liveWalletsCount,
Expand Down Expand Up @@ -199,19 +193,21 @@ func TestMovingFundsAction_FindTargetWallets_CommitmentNotSubmittedYet(t *testin
}

func TestMovingFundsAction_FindTargetWallets_CommitmentAlreadySubmitted(t *testing.T) {
walletPublicKeyHash := hexToByte20(
"ffb3f7538bfa98a511495dd96027cfbd57baf2fa",
)

currentBlock := uint64(1000000)

averageBlockTime := 10 * time.Second

var tests = map[string]struct {
sourceWalletPublicKeyHash [20]byte
targetWalletsCommitmentHash [32]byte
targetWallets []walletInfo
currentBlock uint64
averageBlockTime time.Duration
expectedTargetWallets [][20]byte
expectedError error
}{
"success scenario": {
sourceWalletPublicKeyHash: hexToByte20(
"ffb3f7538bfa98a511495dd96027cfbd57baf2fa",
),
targetWalletsCommitmentHash: hexToByte32(
"9d9368117956680760fa27bb9542ceba2d4fcc398d640a5a0769f5a9593afb0e",
),
Expand All @@ -235,8 +231,6 @@ func TestMovingFundsAction_FindTargetWallets_CommitmentAlreadySubmitted(t *testi
state: tbtc.StateLive,
},
},
currentBlock: 1000000,
averageBlockTime: 10 * time.Second,
expectedTargetWallets: [][20]byte{
hexToByte20("92a6ec889a8fa34f731e639edede4c75e184307c"),
hexToByte20("c7302d75072d78be94eb8d36c4b77583c7abb06e"),
Expand All @@ -245,9 +239,6 @@ func TestMovingFundsAction_FindTargetWallets_CommitmentAlreadySubmitted(t *testi
expectedError: nil,
},
"target wallet is not live": {
sourceWalletPublicKeyHash: hexToByte20(
"ffb3f7538bfa98a511495dd96027cfbd57baf2fa",
),
targetWalletsCommitmentHash: hexToByte32(
"9d9368117956680760fa27bb9542ceba2d4fcc398d640a5a0769f5a9593afb0e",
),
Expand All @@ -271,15 +262,10 @@ func TestMovingFundsAction_FindTargetWallets_CommitmentAlreadySubmitted(t *testi
state: tbtc.StateLive,
},
},
currentBlock: 1000000,
averageBlockTime: 10 * time.Second,
expectedTargetWallets: nil,
expectedError: tbtcpg.ErrTargetWalletNotLive,
},
"target wallet commitment hash mismatch": {
sourceWalletPublicKeyHash: hexToByte20(
"ffb3f7538bfa98a511495dd96027cfbd57baf2fa",
),
targetWalletsCommitmentHash: hexToByte32(
"9d9368117956680760fa27bb9542ceba2d4fcc398d640a5a0769f5a9593afb0e",
),
Expand All @@ -291,8 +277,6 @@ func TestMovingFundsAction_FindTargetWallets_CommitmentAlreadySubmitted(t *testi
state: tbtc.StateLive,
},
},
currentBlock: 1000000,
averageBlockTime: 10 * time.Second,
expectedTargetWallets: nil,
expectedError: tbtcpg.ErrWrongCommitmentHash,
},
Expand All @@ -302,15 +286,15 @@ func TestMovingFundsAction_FindTargetWallets_CommitmentAlreadySubmitted(t *testi
t.Run(testName, func(t *testing.T) {
tbtcChain := tbtcpg.NewLocalChain()

tbtcChain.SetAverageBlockTime(test.averageBlockTime)
tbtcChain.SetAverageBlockTime(averageBlockTime)

blockCounter := tbtcpg.NewMockBlockCounter()
blockCounter.SetCurrentBlock(test.currentBlock)
blockCounter.SetCurrentBlock(currentBlock)
tbtcChain.SetBlockCounter(blockCounter)

startBlock := test.currentBlock - uint64(
startBlock := currentBlock - uint64(
tbtcpg.MovingFundsCommitmentLookBackPeriod.Seconds(),
)/uint64(test.averageBlockTime.Seconds())
)/uint64(averageBlockTime.Seconds())

targetWallets := [][20]byte{}
for _, walletInfo := range test.targetWallets {
Expand All @@ -323,10 +307,8 @@ func TestMovingFundsAction_FindTargetWallets_CommitmentAlreadySubmitted(t *testi

err := tbtcChain.AddPastMovingFundsCommitmentSubmittedEvent(
&tbtc.MovingFundsCommitmentSubmittedEventFilter{
StartBlock: startBlock,
WalletPublicKeyHash: [][20]byte{
test.sourceWalletPublicKeyHash,
},
StartBlock: startBlock,
WalletPublicKeyHash: [][20]byte{walletPublicKeyHash},
},
&tbtc.MovingFundsCommitmentSubmittedEvent{
TargetWallets: targetWallets,
Expand All @@ -345,7 +327,7 @@ func TestMovingFundsAction_FindTargetWallets_CommitmentAlreadySubmitted(t *testi

targetWallets, alreadySubmitted, err := task.FindTargetWallets(
&testutils.MockLogger{},
test.sourceWalletPublicKeyHash,
walletPublicKeyHash,
test.targetWalletsCommitmentHash,
walletBalance,
liveWalletsCount,
Expand Down Expand Up @@ -458,55 +440,50 @@ func TestMovingFundsAction_GetWalletMembersInfo(t *testing.T) {
}

func TestMovingFundsAction_SubmitMovingFundsCommitment(t *testing.T) {
walletPublicKeyHash := hexToByte20(
"ffb3f7538bfa98a511495dd96027cfbd57baf2fa",
)

walletMainUtxo := bitcoin.UnspentTransactionOutput{
Outpoint: &bitcoin.TransactionOutpoint{
TransactionHash: hexToByte32(
"102414558e061ea6e73d5a7bdbf1159b1518c071c22005475d0215ec78a0b911",
),
OutputIndex: 11,
},
Value: 111,
}

currentBlock := uint64(200000)

var tests = map[string]struct {
sourceWalletPublicKeyHash [20]byte
targetWalletsCommitmentHash [32]byte
mainUtxo bitcoin.UnspentTransactionOutput
currentBlock uint64
walletMemberIDs []uint32
walletMemberIndex uint32
targetWallets [][20]byte
expectedError error
}{
"submission successful": {
sourceWalletPublicKeyHash: hexToByte20(
"ffb3f7538bfa98a511495dd96027cfbd57baf2fa",
),
// Simulate the commitment has updated.
targetWalletsCommitmentHash: hexToByte32(
"9d9368117956680760fa27bb9542ceba2d4fcc398d640a5a0769f5a9593afb0e",
),
mainUtxo: bitcoin.UnspentTransactionOutput{
Outpoint: &bitcoin.TransactionOutpoint{
TransactionHash: hexToByte32(
"102414558e061ea6e73d5a7bdbf1159b1518c071c22005475d0215ec78a0b911",
),
OutputIndex: 11,
},
Value: 111,
},
walletMemberIDs: []uint32{11, 22, 33, 44},
walletMemberIndex: 1,
targetWallets: [][20]byte{
hexToByte20("92a6ec889a8fa34f731e639edede4c75e184307c"),
hexToByte20("fdfa28e238734271f5e0d4f53d3843ae6cc09b24"),
},
currentBlock: 200000,
expectedError: nil,
},
"submission unsuccessful": {
sourceWalletPublicKeyHash: hexToByte20(
"ffb3f7538bfa98a511495dd96027cfbd57baf2fa",
),
// Simulate the commitment has not been updated by setting target
// wallets commitment has to zero. The rest of the parameters is
// not important.
targetWalletsCommitmentHash: [32]byte{},
mainUtxo: bitcoin.UnspentTransactionOutput{},
walletMemberIDs: []uint32{},
walletMemberIndex: 0,
targetWallets: [][20]byte{},
currentBlock: 200000,
expectedError: tbtcpg.ErrTransactionNotIncluded,
},
}
Expand All @@ -516,21 +493,21 @@ func TestMovingFundsAction_SubmitMovingFundsCommitment(t *testing.T) {
tbtcChain := tbtcpg.NewLocalChain()

tbtcChain.SetWallet(
test.sourceWalletPublicKeyHash,
walletPublicKeyHash,
&tbtc.WalletChainData{
MovingFundsTargetWalletsCommitmentHash: test.targetWalletsCommitmentHash,
},
)

blockCounter := tbtcpg.NewMockBlockCounter()
blockCounter.SetCurrentBlock(test.currentBlock)
blockCounter.SetCurrentBlock(currentBlock)
tbtcChain.SetBlockCounter(blockCounter)

task := tbtcpg.NewMovingFundsTask(tbtcChain, nil)

err := task.SubmitMovingFundsCommitment(
test.sourceWalletPublicKeyHash,
&test.mainUtxo,
walletPublicKeyHash,
&walletMainUtxo,
test.walletMemberIDs,
test.walletMemberIndex,
test.targetWallets,
Expand All @@ -552,15 +529,15 @@ func TestMovingFundsAction_SubmitMovingFundsCommitment(t *testing.T) {

submittedMovingFundsCommitment := submittedMovingFundsCommitments[0]

expectedWalletPublicKeyHash := test.sourceWalletPublicKeyHash
expectedWalletPublicKeyHash := walletPublicKeyHash
actualWalletPublicKeyHash := submittedMovingFundsCommitment.WalletPublicKeyHash
testutils.AssertBytesEqual(
t,
expectedWalletPublicKeyHash[:],
actualWalletPublicKeyHash[:],
)

expectedWalletMainUtxo := &test.mainUtxo
expectedWalletMainUtxo := &walletMainUtxo
actualWalletMainUtxo := submittedMovingFundsCommitment.WalletMainUtxo
if diff := deep.Equal(expectedWalletMainUtxo, actualWalletMainUtxo); diff != nil {
t.Errorf("invalid wallet main utxo: %v", diff)
Expand Down Expand Up @@ -598,7 +575,7 @@ func TestMovingFundsAction_ProposeMovingFunds(t *testing.T) {
hexToByte20("c7302d75072d78be94eb8d36c4b77583c7abb06e"),
}

mainUtxo := &bitcoin.UnspentTransactionOutput{
walletMainUtxo := &bitcoin.UnspentTransactionOutput{
Outpoint: &bitcoin.TransactionOutpoint{
TransactionHash: hexToByte32(
"102414558e061ea6e73d5a7bdbf1159b1518c071c22005475d0215ec78a0b911",
Expand Down Expand Up @@ -653,7 +630,7 @@ func TestMovingFundsAction_ProposeMovingFunds(t *testing.T) {

err := tbtcChain.SetMovingFundsProposalValidationResult(
walletPublicKeyHash,
mainUtxo,
walletMainUtxo,
test.expectedProposal,
true,
)
Expand All @@ -666,7 +643,7 @@ func TestMovingFundsAction_ProposeMovingFunds(t *testing.T) {
proposal, err := task.ProposeMovingFunds(
&testutils.MockLogger{},
walletPublicKeyHash,
mainUtxo,
walletMainUtxo,
targetWallets,
test.fee,
)
Expand Down

0 comments on commit 5bee22a

Please sign in to comment.