From c4a5e1261564b42be8d81afed19a6507212704e0 Mon Sep 17 00:00:00 2001 From: noot <36753753+noot@users.noreply.github.com> Date: Wed, 19 Apr 2023 18:23:36 +0200 Subject: [PATCH] small additions to contract unit tests (#388) --- ethereum/swap_creator_test.go | 38 ++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/ethereum/swap_creator_test.go b/ethereum/swap_creator_test.go index 22bcc932..57420747 100644 --- a/ethereum/swap_creator_test.go +++ b/ethereum/swap_creator_test.go @@ -279,6 +279,11 @@ func testClaim(t *testing.T, asset ethcommon.Address, newLogIndex int, value *bi Nonce: nonce, } + // ensure we can't claim before setting contract to Ready + txOpts = *authOrig + _, err = contract.Claim(&txOpts, swap, proof.Secret()) + require.ErrorContains(t, err, "VM Exception while processing transaction: revert") + // set contract to Ready txOpts = *authOrig tx, err = contract.SetReady(&txOpts, swap) @@ -288,11 +293,8 @@ func testClaim(t *testing.T, asset ethcommon.Address, newLogIndex int, value *bi require.NoError(t, err) // now let's try to claim - var s [32]byte - secret := proof.Secret() - copy(s[:], secret[:]) txOpts = *authOrig - tx, err = contract.Claim(&txOpts, swap, s) + tx, err = contract.Claim(&txOpts, swap, proof.Secret()) require.NoError(t, err) receipt, err = block.WaitForReceipt(context.Background(), conn, tx.Hash()) require.NoError(t, err) @@ -364,10 +366,7 @@ func testRefundBeforeT0(t *testing.T, asset ethcommon.Address, erc20Contract *Te } // now let's try to refund - var s [32]byte - secret := proof.Secret() - copy(s[:], secret[:]) - tx, err = contract.Refund(auth, swap, s) + tx, err = contract.Refund(auth, swap, proof.Secret()) require.NoError(t, err) receipt, err = block.WaitForReceipt(context.Background(), conn, tx.Hash()) require.NoError(t, err) @@ -409,7 +408,7 @@ func testRefundAfterT1(t *testing.T, asset ethcommon.Address, erc20Contract *Tes } nonce := big.NewInt(0) - timeout := big.NewInt(1) // T1 expires before we get the receipt for new_swap TX + timeout := big.NewInt(3) auth.Value = defaultSwapValue tx, err = contract.NewSwap(auth, [32]byte{}, cmt, addr, timeout, timeout, asset, defaultSwapValue, nonce) @@ -427,8 +426,8 @@ func testRefundAfterT1(t *testing.T, asset ethcommon.Address, erc20Contract *Tes t0, t1, err := GetTimeoutsFromLog(receipt.Logs[newLogIndex]) require.NoError(t, err) - <-time.After(time.Until(time.Unix(t1.Int64()+1, 0))) - + // ensure we can't refund between T0 and T1 + <-time.After(time.Until(time.Unix(t0.Int64()+1, 0))) swap := SwapCreatorSwap{ Owner: addr, Claimer: addr, @@ -441,11 +440,16 @@ func testRefundAfterT1(t *testing.T, asset ethcommon.Address, erc20Contract *Tes Nonce: nonce, } - // now let's try to refund - var s [32]byte secret := proof.Secret() - copy(s[:], secret[:]) - tx, err = contract.Refund(auth, swap, s) + tx, err = contract.Refund(auth, swap, secret) + require.NoError(t, err) + _, err = block.WaitForReceipt(context.Background(), conn, tx.Hash()) + require.ErrorContains(t, err, "VM Exception while processing transaction: revert") + + <-time.After(time.Until(time.Unix(t1.Int64()+1, 0))) + + // now let's try to refund + tx, err = contract.Refund(auth, swap, secret) require.NoError(t, err) receipt, err = block.WaitForReceipt(context.Background(), conn, tx.Hash()) require.NoError(t, err) @@ -508,9 +512,7 @@ func TestSwapCreator_MultipleSwaps(t *testing.T) { res, err := dleq.Verify(proof) require.NoError(t, err) - secret := proof.Secret() - copy(sc.secret[:], secret[:]) - + sc.secret = proof.Secret() sc.walletKey = tests.GetTestKeyByIndex(t, i) addrSwap := crypto.PubkeyToAddress(*sc.walletKey.Public().(*ecdsa.PublicKey))