added new waitForNewSwapReceipt method for xmrmaker (#455)

This commit is contained in:
Dmitry Holodov
2023-05-03 11:08:00 -05:00
committed by GitHub
parent e3c24b5e4a
commit 998347ded0
14 changed files with 257 additions and 83 deletions

View File

@@ -21,7 +21,7 @@ import (
func getContractCode(t *testing.T) []byte {
ec, _ := tests.NewEthClient(t)
pk := tests.GetMakerTestKey(t)
contractAddr, _ := deploySwapCreator(t, ec, pk)
contractAddr, _ := DevDeploySwapCreator(t, ec, pk)
code, err := ec.CodeAt(context.Background(), contractAddr, nil)
require.NoError(t, err)
return code
@@ -42,7 +42,7 @@ func TestCheckSwapCreatorContractCode(t *testing.T) {
ec, _ := tests.NewEthClient(t)
pk := tests.GetMakerTestKey(t)
contractAddr, _ := deploySwapCreator(t, ec, pk)
contractAddr, _ := DevDeploySwapCreator(t, ec, pk)
err := CheckSwapCreatorContractCode(context.Background(), ec, contractAddr)
require.NoError(t, err)
}

View File

@@ -13,3 +13,9 @@ const (
MaxRefundTokenGas = 47294
MaxTokenApproveGas = 47000 // 46223 with our contract
)
// constants that are interesting to track, but not used by swaps
const (
maxSwapCreatorDeployGas = 1094089
maxTestERC20DeployGas = 798286 // using long token names or symbols will increase this
)

View File

@@ -1,8 +0,0 @@
package contracts
// We don't deploy SwapCreator contracts or ERC20 token contracts in swaps, so
// these constants are only compiled in for test files.
const (
maxSwapCreatorDeployGas = 1094089
maxTestERC20DeployGas = 798286 // using long token names or symbols will increase this
)

View File

@@ -31,7 +31,7 @@ func DeploySwapCreatorWithKey(
}
log.Infof("deploying SwapCreator.sol")
address, tx, sf, err := DeploySwapCreator(txOpts, ec)
address, tx, sc, err := DeploySwapCreator(txOpts, ec)
if err != nil {
return ethcommon.Address{}, nil, fmt.Errorf("failed to deploy swap creator: %w", err)
}
@@ -42,7 +42,7 @@ func DeploySwapCreatorWithKey(
}
log.Infof("deployed SwapCreator.sol: address=%s tx hash=%s", address, tx.Hash())
return address, sf, nil
return address, sc, nil
}
func newTXOpts(ctx context.Context, ec *ethclient.Client, privkey *ecdsa.PrivateKey) (*bind.TransactOpts, error) {

View File

@@ -63,23 +63,11 @@ func approveERC20(t *testing.T,
require.GreaterOrEqual(t, MaxTokenApproveGas, int(receipt.GasUsed), "Token Approve")
}
func deploySwapCreator(t *testing.T, ec *ethclient.Client, pk *ecdsa.PrivateKey) (ethcommon.Address, *SwapCreator) {
swapCreatorAddr, tx, swapCreator, err := DeploySwapCreator(getAuth(t, pk), ec)
require.NoError(t, err)
receipt := getReceipt(t, ec, tx)
t.Logf("gas cost to deploy SwapCreator.sol: %d (delta %d)",
receipt.GasUsed, maxSwapCreatorDeployGas-int(receipt.GasUsed))
require.GreaterOrEqual(t, maxSwapCreatorDeployGas, int(receipt.GasUsed), "deploy SwapCreator")
return swapCreatorAddr, swapCreator
}
func testNewSwap(t *testing.T, asset types.EthAsset, erc20Contract *TestERC20) {
pk := tests.GetTakerTestKey(t)
ec, _ := tests.NewEthClient(t)
swapCreatorAddr, swapCreator := deploySwapCreator(t, ec, pk)
swapCreatorAddr, swapCreator := DevDeploySwapCreator(t, ec, pk)
owner := crypto.PubkeyToAddress(pk.PublicKey)
claimer := common.EthereumPrivateKeyToAddress(tests.GetMakerTestKey(t))
@@ -179,7 +167,7 @@ func TestSwapCreator_Claim_vec(t *testing.T) {
ec, _ := tests.NewEthClient(t)
addr := crypto.PubkeyToAddress(pkA.PublicKey)
_, swapCreator := deploySwapCreator(t, ec, pkA)
_, swapCreator := DevDeploySwapCreator(t, ec, pkA)
txOpts := getAuth(t, pkA)
txOpts.Value = defaultSwapValue
@@ -248,7 +236,7 @@ func testClaim(t *testing.T, asset types.EthAsset, newLogIndex int, value *big.I
ec, _ := tests.NewEthClient(t)
addr := crypto.PubkeyToAddress(pkA.PublicKey)
swapCreatorAddr, swapCreator := deploySwapCreator(t, ec, pkA)
swapCreatorAddr, swapCreator := DevDeploySwapCreator(t, ec, pkA)
if asset.IsToken() {
approveERC20(t, ec, pkA, erc20Contract, swapCreatorAddr, value)
@@ -344,7 +332,7 @@ func testRefundBeforeT1(t *testing.T, asset types.EthAsset, erc20Contract *TestE
ec, _ := tests.NewEthClient(t)
addr := crypto.PubkeyToAddress(pkA.PublicKey)
swapCreatorAddr, swapCreator := deploySwapCreator(t, ec, pkA)
swapCreatorAddr, swapCreator := DevDeploySwapCreator(t, ec, pkA)
if asset.IsToken() {
approveERC20(t, ec, pkA, erc20Contract, swapCreatorAddr, defaultSwapValue)
@@ -430,7 +418,7 @@ func testRefundAfterT2(t *testing.T, asset types.EthAsset, erc20Contract *TestER
ec, _ := tests.NewEthClient(t)
addr := crypto.PubkeyToAddress(pkA.PublicKey)
swapCreatorAddr, swapCreator := deploySwapCreator(t, ec, pkA)
swapCreatorAddr, swapCreator := DevDeploySwapCreator(t, ec, pkA)
if asset.IsToken() {
approveERC20(t, ec, pkA, erc20Contract, swapCreatorAddr, defaultSwapValue)
@@ -515,7 +503,7 @@ func TestSwapCreator_MultipleSwaps(t *testing.T) {
pkContractCreator := tests.GetTestKeyByIndex(t, 0)
ec, _ := tests.NewEthClient(t)
_, swapCreator := deploySwapCreator(t, ec, pkContractCreator)
_, swapCreator := DevDeploySwapCreator(t, ec, pkContractCreator)
const numSwaps = 16
type swapCase struct {

56
ethereum/test_support.go Normal file
View File

@@ -0,0 +1,56 @@
// Copyright 2023 The AthanorLabs/atomic-swap Authors
// SPDX-License-Identifier: LGPL-3.0-only
//go:build !prod
package contracts
import (
"context"
"crypto/ecdsa"
"sync"
"testing"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/require"
"github.com/athanorlabs/atomic-swap/ethereum/block"
)
//
// FUNCTIONS ONLY FOR UNIT TESTS
//
// these variables should only be accessed by DevDeploySwapCreator
var _swapCreator *SwapCreator
var _swapCreatorAddr *ethcommon.Address
var _swapCreatorAddrMu sync.Mutex
// DevDeploySwapCreator deploys and returns the swapCreator address and contract
// binding for unit tests, returning a cached result if available.
func DevDeploySwapCreator(t *testing.T, ec *ethclient.Client, pk *ecdsa.PrivateKey) (ethcommon.Address, *SwapCreator) {
ctx := context.Background()
_swapCreatorAddrMu.Lock()
defer _swapCreatorAddrMu.Unlock()
if _swapCreatorAddr == nil {
txOpts, err := newTXOpts(ctx, ec, pk)
require.NoError(t, err)
swapCreatorAddr, tx, swapCreator, err := DeploySwapCreator(txOpts, ec)
require.NoError(t, err)
receipt, err := block.WaitForReceipt(ctx, ec, tx.Hash())
require.NoError(t, err)
t.Logf("gas cost to deploy SwapCreator.sol: %d (delta %d)",
receipt.GasUsed, maxSwapCreatorDeployGas-int(receipt.GasUsed))
require.GreaterOrEqual(t, maxSwapCreatorDeployGas, int(receipt.GasUsed), "deploy SwapCreator")
_swapCreatorAddr = &swapCreatorAddr
_swapCreator = swapCreator
}
return *_swapCreatorAddr, _swapCreator
}