From b1530de9c0235abdc05a05728778f87f9f5f568a Mon Sep 17 00:00:00 2001 From: fabioberger Date: Tue, 17 Sep 2019 15:02:13 +0200 Subject: [PATCH] Pass in a context with a timeout to calls to WaitMined so that they are guarenteed to eventually return --- scenario/scenario.go | 36 ++++++++++++++++--------- zeroex/orderwatch/order_watcher_test.go | 36 ++++++++++++++++--------- 2 files changed, 48 insertions(+), 24 deletions(-) diff --git a/scenario/scenario.go b/scenario/scenario.go index a4ef95582..6e1fdec28 100644 --- a/scenario/scenario.go +++ b/scenario/scenario.go @@ -62,7 +62,8 @@ func CreateZRXForWETHSignedTestOrder(t *testing.T, ethClient *ethclient.Client, } txn, err := weth9.Deposit(opts) require.NoError(t, err) - receipt, err := bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err := bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) require.Equal(t, receipt.Status, uint64(1)) @@ -76,7 +77,8 @@ func CreateZRXForWETHSignedTestOrder(t *testing.T, ethClient *ethclient.Client, } txn, err = zrx.Transfer(opts, makerAddress, zrxAmount) require.NoError(t, err) - receipt, err = bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ = context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err = bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) require.Equal(t, receipt.Status, uint64(1)) @@ -87,7 +89,8 @@ func CreateZRXForWETHSignedTestOrder(t *testing.T, ethClient *ethclient.Client, } txn, err = zrx.Approve(opts, ganacheAddresses.ERC20Proxy, zrxAmount) require.NoError(t, err) - receipt, err = bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ = context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err = bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) require.Equal(t, receipt.Status, uint64(1)) @@ -98,7 +101,8 @@ func CreateZRXForWETHSignedTestOrder(t *testing.T, ethClient *ethclient.Client, } txn, err = weth9.Approve(opts, ganacheAddresses.ERC20Proxy, wethAmount) require.NoError(t, err) - receipt, err = bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ = context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err = bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) require.Equal(t, receipt.Status, uint64(1)) @@ -149,7 +153,8 @@ func CreateWETHForZRXSignedTestOrder(t *testing.T, ethClient *ethclient.Client, } txn, err := weth9.Deposit(opts) require.NoError(t, err) - receipt, err := bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err := bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) require.Equal(t, receipt.Status, uint64(1)) @@ -163,7 +168,8 @@ func CreateWETHForZRXSignedTestOrder(t *testing.T, ethClient *ethclient.Client, } txn, err = zrx.Transfer(opts, takerAddress, zrxAmount) require.NoError(t, err) - receipt, err = bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ = context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err = bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) require.Equal(t, receipt.Status, uint64(1)) @@ -174,7 +180,8 @@ func CreateWETHForZRXSignedTestOrder(t *testing.T, ethClient *ethclient.Client, } txn, err = zrx.Approve(opts, ganacheAddresses.ERC20Proxy, zrxAmount) require.NoError(t, err) - receipt, err = bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ = context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err = bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) require.Equal(t, receipt.Status, uint64(1)) @@ -185,7 +192,8 @@ func CreateWETHForZRXSignedTestOrder(t *testing.T, ethClient *ethclient.Client, } txn, err = weth9.Approve(opts, ganacheAddresses.ERC20Proxy, wethAmount) require.NoError(t, err) - receipt, err = bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ = context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err = bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) require.Equal(t, receipt.Status, uint64(1)) @@ -203,7 +211,8 @@ func CreateNFTForZRXSignedTestOrder(t *testing.T, ethClient *ethclient.Client, m } txn, err := dummyERC721Token.Mint(makerOpts, makerAddress, tokenID) require.NoError(t, err) - receipt, err := bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err := bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) require.Equal(t, receipt.Status, uint64(1)) @@ -240,7 +249,8 @@ func CreateNFTForZRXSignedTestOrder(t *testing.T, ethClient *ethclient.Client, m // SET NFT allowance txn, err = dummyERC721Token.SetApprovalForAll(makerOpts, ganacheAddresses.ERC721Proxy, true) require.NoError(t, err) - receipt, err = bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ = context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err = bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) require.Equal(t, receipt.Status, uint64(1)) @@ -260,7 +270,8 @@ func CreateNFTForZRXSignedTestOrder(t *testing.T, ethClient *ethclient.Client, m } txn, err = zrx.Transfer(opts, takerAddress, zrxAmount) require.NoError(t, err) - receipt, err = bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ = context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err = bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) require.Equal(t, receipt.Status, uint64(1)) @@ -271,7 +282,8 @@ func CreateNFTForZRXSignedTestOrder(t *testing.T, ethClient *ethclient.Client, m } txn, err = zrx.Approve(opts, ganacheAddresses.ERC20Proxy, zrxAmount) require.NoError(t, err) - receipt, err = bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ = context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err = bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) require.Equal(t, receipt.Status, uint64(1)) diff --git a/zeroex/orderwatch/order_watcher_test.go b/zeroex/orderwatch/order_watcher_test.go index bf537754a..2ea2c92c1 100644 --- a/zeroex/orderwatch/order_watcher_test.go +++ b/zeroex/orderwatch/order_watcher_test.go @@ -92,7 +92,8 @@ func TestOrderWatcherUnfundedInsufficientERC20Balance(t *testing.T) { } txn, err := zrx.Transfer(opts, constants.GanacheAccount4, zrxAmount) require.NoError(t, err) - receipt, err := bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err := bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) assert.Equal(t, receipt.Status, uint64(1)) @@ -132,7 +133,8 @@ func TestOrderWatcherUnfundedInsufficientERC721Balance(t *testing.T) { } txn, err := dummyERC721Token.TransferFrom(opts, makerAddress, constants.GanacheAccount4, tokenID) require.NoError(t, err) - receipt, err := bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err := bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) assert.Equal(t, receipt.Status, uint64(1)) @@ -173,7 +175,8 @@ func TestOrderWatcherUnfundedInsufficientERC721Allowance(t *testing.T) { ganacheAddresses := ethereum.NetworkIDToContractAddresses[constants.TestNetworkID] txn, err := dummyERC721Token.SetApprovalForAll(opts, ganacheAddresses.ERC721Proxy, false) require.NoError(t, err) - receipt, err := bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err := bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) assert.Equal(t, receipt.Status, uint64(1)) @@ -214,7 +217,8 @@ func TestOrderWatcherUnfundedInsufficientERC20Allowance(t *testing.T) { } txn, err := zrx.Approve(opts, ganacheAddresses.ERC20Proxy, big.NewInt(0)) require.NoError(t, err) - receipt, err := bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err := bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) assert.Equal(t, receipt.Status, uint64(1)) @@ -254,7 +258,8 @@ func TestOrderWatcherUnfundedThenFundedAgain(t *testing.T) { } txn, err := zrx.Transfer(opts, constants.GanacheAccount4, zrxAmount) require.NoError(t, err) - receipt, err := bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err := bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) assert.Equal(t, receipt.Status, uint64(1)) @@ -284,7 +289,8 @@ func TestOrderWatcherUnfundedThenFundedAgain(t *testing.T) { } txn, err = zrx.Transfer(opts, makerAddress, zrxAmount) require.NoError(t, err) - receipt, err = bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ = context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err = bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) assert.Equal(t, receipt.Status, uint64(1)) @@ -326,7 +332,8 @@ func TestOrderWatcherNoChange(t *testing.T) { } txn, err := zrx.Transfer(opts, makerAddress, zrxAmount) require.NoError(t, err) - receipt, err := bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err := bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) assert.Equal(t, receipt.Status, uint64(1)) @@ -359,7 +366,8 @@ func TestOrderWatcherWETHWithdrawAndDeposit(t *testing.T) { } txn, err := weth.Withdraw(opts, wethAmount) require.NoError(t, err) - receipt, err := bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err := bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) assert.Equal(t, receipt.Status, uint64(1)) @@ -388,7 +396,8 @@ func TestOrderWatcherWETHWithdrawAndDeposit(t *testing.T) { } txn, err = weth.Deposit(opts) require.NoError(t, err) - receipt, err = bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ = context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err = bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) assert.Equal(t, receipt.Status, uint64(1)) @@ -423,7 +432,8 @@ func TestOrderWatcherCanceled(t *testing.T) { orderWithoutExchangeAddress := signedOrder.ConvertToOrderWithoutExchangeAddress() txn, err := exchange.CancelOrder(opts, orderWithoutExchangeAddress) require.NoError(t, err) - receipt, err := bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err := bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) assert.Equal(t, receipt.Status, uint64(1)) @@ -464,7 +474,8 @@ func TestOrderWatcherCancelUpTo(t *testing.T) { targetOrderEpoch := signedOrder.Salt txn, err := exchange.CancelOrdersUpTo(opts, targetOrderEpoch) require.NoError(t, err) - receipt, err := bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err := bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) assert.Equal(t, receipt.Status, uint64(1)) @@ -505,7 +516,8 @@ func TestOrderWatcherERC20Filled(t *testing.T) { orderWithoutExchangeAddress := signedOrder.ConvertToOrderWithoutExchangeAddress() txn, err := exchange.FillOrder(opts, orderWithoutExchangeAddress, wethAmount, signedOrder.Signature) require.NoError(t, err) - receipt, err := bind.WaitMined(context.Background(), ethClient, txn) + ctx, _ := context.WithTimeout(context.Background(), 4 * time.Second) + receipt, err := bind.WaitMined(ctx, ethClient, txn) require.NoError(t, err) assert.Equal(t, receipt.Status, uint64(1))