feat: update SwapCreater.sol claimRelayer to no longer use forwarder (#449)

Co-authored-by: Dmitry Holodov <dimalinux@protonmail.com>
This commit is contained in:
noot
2023-05-01 19:23:17 -04:00
committed by GitHub
parent b3dcb96074
commit fb95751dda
45 changed files with 878 additions and 1126 deletions

View File

@@ -31,7 +31,6 @@ var (
type contractAddresses struct {
SwapCreatorAddr ethcommon.Address `json:"swapCreatorAddr" validate:"required"`
ForwarderAddr ethcommon.Address `json:"forwarderAddr" validate:"required"`
}
func getOrDeploySwapCreator(
@@ -40,7 +39,6 @@ func getOrDeploySwapCreator(
env common.Environment,
dataDir string,
ec extethclient.EthClient,
forwarderAddr ethcommon.Address,
) (ethcommon.Address, error) {
var err error
if (swapCreatorAddr == ethcommon.Address{}) {
@@ -49,7 +47,7 @@ func getOrDeploySwapCreator(
time.Sleep(10 * time.Second)
}
swapCreatorAddr, _, err = deploySwapCreator(ctx, ec.Raw(), ec.PrivateKey(), forwarderAddr, dataDir)
swapCreatorAddr, err = deploySwapCreator(ctx, ec.Raw(), ec.PrivateKey(), dataDir)
if err != nil {
return ethcommon.Address{}, fmt.Errorf("failed to deploy swap creator: %w", err)
}
@@ -57,7 +55,7 @@ func getOrDeploySwapCreator(
// otherwise, load the contract from the given address
// and check that its bytecode is valid (ie. matches the
// bytecode of this repo's swap contract)
_, err = contracts.CheckSwapCreatorContractCode(ctx, ec.Raw(), swapCreatorAddr)
err = contracts.CheckSwapCreatorContractCode(ctx, ec.Raw(), swapCreatorAddr)
if err != nil {
return ethcommon.Address{}, err
}
@@ -70,31 +68,15 @@ func deploySwapCreator(
ctx context.Context,
ec *ethclient.Client,
privkey *ecdsa.PrivateKey,
forwarderAddr ethcommon.Address,
dataDir string,
) (ethcommon.Address, *contracts.SwapCreator, error) {
) (ethcommon.Address, error) {
if privkey == nil {
return ethcommon.Address{}, nil, errNoEthPrivateKey
return ethcommon.Address{}, errNoEthPrivateKey
}
if (forwarderAddr == ethcommon.Address{}) {
// deploy forwarder contract as well
var err error
forwarderAddr, err = contracts.DeployGSNForwarderWithKey(ctx, ec, privkey)
if err != nil {
return ethcommon.Address{}, nil, err
}
} else {
// TODO: ignore this if the forwarderAddr is the one that's hardcoded for this network
if err := contracts.CheckForwarderContractCode(ctx, ec, forwarderAddr); err != nil {
return ethcommon.Address{}, nil, err
}
}
swapCreatorAddr, sf, err := contracts.DeploySwapCreatorWithKey(ctx, ec, privkey, forwarderAddr)
swapCreatorAddr, _, err := contracts.DeploySwapCreatorWithKey(ctx, ec, privkey)
if err != nil {
return ethcommon.Address{}, nil, err
return ethcommon.Address{}, err
}
// store the contract addresses on disk
@@ -102,14 +84,13 @@ func deploySwapCreator(
path.Join(dataDir, contractAddressesFile),
&contractAddresses{
SwapCreatorAddr: swapCreatorAddr,
ForwarderAddr: forwarderAddr,
},
)
if err != nil {
return ethcommon.Address{}, nil, fmt.Errorf("failed to write contract address to file: %w", err)
return ethcommon.Address{}, fmt.Errorf("failed to write contract address to file: %w", err)
}
return swapCreatorAddr, sf, nil
return swapCreatorAddr, nil
}
// writeContractAddressesToFile writes the contract addresses to the given file

View File

@@ -8,7 +8,6 @@ import (
"testing"
"github.com/athanorlabs/atomic-swap/common"
contracts "github.com/athanorlabs/atomic-swap/ethereum"
"github.com/athanorlabs/atomic-swap/ethereum/extethclient"
"github.com/athanorlabs/atomic-swap/tests"
@@ -16,26 +15,7 @@ import (
"github.com/stretchr/testify/require"
)
func TestGetOrDeploySwapCreator_DeployNoForwarder(t *testing.T) {
pk := tests.GetTakerTestKey(t)
ec := extethclient.CreateTestClient(t, pk)
tmpDir := t.TempDir()
forwarder, err := contracts.DeployGSNForwarderWithKey(context.Background(), ec.Raw(), pk)
require.NoError(t, err)
_, err = getOrDeploySwapCreator(
context.Background(),
ethcommon.Address{},
common.Development,
tmpDir,
ec,
forwarder,
)
require.NoError(t, err)
}
func TestGetOrDeploySwapCreator_DeployForwarderAlso(t *testing.T) {
func TestGetOrDeploySwapCreator_Deploy(t *testing.T) {
pk := tests.GetTakerTestKey(t)
ec := extethclient.CreateTestClient(t, pk)
tmpDir := t.TempDir()
@@ -46,7 +26,6 @@ func TestGetOrDeploySwapCreator_DeployForwarderAlso(t *testing.T) {
common.Development,
tmpDir,
ec,
ethcommon.Address{},
)
require.NoError(t, err)
}
@@ -56,10 +35,6 @@ func TestGetOrDeploySwapCreator_Get(t *testing.T) {
ec := extethclient.CreateTestClient(t, pk)
tmpDir := t.TempDir()
forwarder, err := contracts.DeployGSNForwarderWithKey(context.Background(), ec.Raw(), pk)
require.NoError(t, err)
t.Log(forwarder)
// deploy and get address
address, err := getOrDeploySwapCreator(
context.Background(),
@@ -67,7 +42,6 @@ func TestGetOrDeploySwapCreator_Get(t *testing.T) {
common.Development,
tmpDir,
ec,
forwarder,
)
require.NoError(t, err)
@@ -77,7 +51,6 @@ func TestGetOrDeploySwapCreator_Get(t *testing.T) {
common.Development,
tmpDir,
ec,
ethcommon.Address{},
)
require.NoError(t, err)
require.Equal(t, address, addr2)

View File

@@ -71,11 +71,10 @@ const (
flagUseExternalSigner = "external-signer"
flagRelayer = "relayer"
flagDevXMRTaker = "dev-xmrtaker"
flagDevXMRMaker = "dev-xmrmaker"
flagDeploy = "deploy"
flagForwarderAddress = "forwarder-address"
flagNoTransferBack = "no-transfer-back"
flagDevXMRTaker = "dev-xmrtaker"
flagDevXMRMaker = "dev-xmrmaker"
flagDeploy = "deploy"
flagNoTransferBack = "no-transfer-back"
flagLogLevel = cliutil.FlagLogLevel
flagProfile = "profile"
@@ -187,10 +186,6 @@ func cliApp() *cli.App {
Name: flagDeploy,
Usage: "Deploy an instance of the swap contract",
},
&cli.StringFlag{
Name: flagForwarderAddress,
Usage: "Ethereum address of the trusted forwarder contract to use when deploying the swap contract",
},
&cli.BoolFlag{
Name: flagNoTransferBack,
Usage: "Leave XMR in generated swap wallet instead of sweeping funds to primary.",
@@ -369,36 +364,18 @@ func validateOrDeployContracts(c *cli.Context, envConf *common.Config, ec exteth
panic("contract address should have been zeroed when envConf was initialized")
}
// forwarderAddr is set only if we're deploying the swap creator contract
// and the --forwarder-address flag is set. Otherwise, if we're deploying
// and this flag isn't set, we deploy both the forwarder and the swap
// creator contracts.
var forwarderAddr ethcommon.Address
forwarderAddrStr := c.String(flagForwarderAddress)
if deploy && forwarderAddrStr != "" {
if !ethcommon.IsHexAddress(forwarderAddrStr) {
return fmt.Errorf("%q requires a valid ethereum address", flagForwarderAddress)
}
forwarderAddr = ethcommon.HexToAddress(forwarderAddrStr)
} else if !deploy && forwarderAddrStr != "" {
return fmt.Errorf("using flag %q requires the %q flag", flagForwarderAddress, flagDeploy)
}
swapCreatorAddr, err := getOrDeploySwapCreator(
c.Context,
envConf.SwapCreatorAddr,
envConf.Env,
envConf.DataDir,
ec,
forwarderAddr,
)
if err != nil {
return err
}
envConf.SwapCreatorAddr = swapCreatorAddr
return nil
}

View File

@@ -9,7 +9,6 @@ import (
"fmt"
"os"
"path"
"strings"
"sync"
"testing"
"time"
@@ -89,8 +88,8 @@ func TestDaemon_DevXMRTaker(t *testing.T) {
}
//
// Validate that --deploy created a contract address file and that we
// deployed a forwarder. At some future point, we will ask the RPC endpoint
// Validate that --deploy created a contract address file.
// At some future point, we will ask the RPC endpoint
// what the contract addresses are instead of using this file.
//
data, err := os.ReadFile(path.Join(dataDir, contractAddressesFile))
@@ -99,19 +98,10 @@ func TestDaemon_DevXMRTaker(t *testing.T) {
require.NoError(t, json.Unmarshal(data, &m))
swapCreatorAddr, ok := m["swapCreatorAddr"]
require.True(t, ok)
forwarderAddr, ok := m["forwarderAddr"]
require.True(t, ok)
ec, _ := tests.NewEthClient(t)
ecCtx := context.Background()
discoveredForwarderAddr, err :=
contracts.CheckSwapCreatorContractCode(ecCtx, ec, ethcommon.HexToAddress(swapCreatorAddr))
require.NoError(t, err)
require.Equal(t, strings.ToLower(discoveredForwarderAddr.Hex()), forwarderAddr)
// something is seriously wrong if this next check fails, as CheckSwapCreatorContractCode
// should have already validated the forwarder bytecode
err = contracts.CheckForwarderContractCode(ecCtx, ec, ethcommon.HexToAddress(forwarderAddr))
err = contracts.CheckSwapCreatorContractCode(ecCtx, ec, ethcommon.HexToAddress(swapCreatorAddr))
require.NoError(t, err)
}
@@ -121,7 +111,7 @@ func TestDaemon_DevXMRMaker(t *testing.T) {
ec, _ := tests.NewEthClient(t)
// We tested --deploy with the taker, so test passing the contract address here
swapCreatorAddr, _, err := deploySwapCreator(context.Background(), ec, key, ethcommon.Address{}, t.TempDir())
swapCreatorAddr, err := deploySwapCreator(context.Background(), ec, key, t.TempDir())
require.NoError(t, err)
flags := []string{
@@ -158,9 +148,7 @@ func TestDaemon_BadFlags(t *testing.T) {
ec, _ := tests.NewEthClient(t)
ctx, _ := newTestContext(t)
swapCreatorAddr, swapCreator, err := deploySwapCreator(ctx, ec, key, ethcommon.Address{}, t.TempDir())
require.NoError(t, err)
forwarderAddr, err := swapCreator.TrustedForwarder(nil)
swapCreatorAddr, err := deploySwapCreator(ctx, ec, key, t.TempDir())
require.NoError(t, err)
baseFlags := []string{
@@ -184,35 +172,10 @@ func TestDaemon_BadFlags(t *testing.T) {
extraFlags: nil,
expectErr: `flag "deploy" or "contract-address" is required for env=dev`,
},
{
description: "deploy SwapCreator with invalid forwarder",
extraFlags: []string{
fmt.Sprintf("--%s", flagDeploy),
fmt.Sprintf("--%s=%s", flagForwarderAddress, swapCreatorAddr), // passing wrong contract
},
expectErr: "does not contain correct Forwarder code",
},
{
description: "pass invalid forwarder address (wrong length)",
extraFlags: []string{
fmt.Sprintf("--%s", flagDeploy),
fmt.Sprintf("--%s=%sAB", flagForwarderAddress, forwarderAddr), // one byte too long
},
expectErr: fmt.Sprintf(`"%s" requires a valid ethereum address`, flagForwarderAddress),
},
{
description: "pass forwarder address without deploy flag",
extraFlags: []string{
fmt.Sprintf("--%s=%s", flagForwarderAddress, forwarderAddr),
// next flag is needed, or we fail on a different error first
fmt.Sprintf("--%s=%s", flagContractAddress, swapCreatorAddr),
},
expectErr: fmt.Sprintf(`using flag "%s" requires the "%s" flag`, flagForwarderAddress, flagDeploy),
},
{
description: "pass invalid SwapCreator contract",
extraFlags: []string{
fmt.Sprintf("--%s=%s", flagContractAddress, forwarderAddr), // passing wrong contract
fmt.Sprintf("--%s=%s", flagContractAddress, ethcommon.Address{9}), // passing wrong contract
},
expectErr: "does not contain correct SwapCreator code",
},