Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Improve scenario package (#779)
Browse files Browse the repository at this point in the history
* WIP Improve scenario package

* Create orderopts package. Update scenario and ordervalidator to use it

* Fix remaining ordervalidator tests

* Update most orderwatcher tests

* Support setting taker state and enable remaining orderwatcher tests

* Fix remaining tests (only MultiAsset disabled)

* Uncomment test that depends on on-chain state for a StaticCall order

* Fix browser integration test

* Remove unneeded ethClient parameter in a few places

* Move sleep statement in order_watcher_test.go

* Address Alex Towle's feedback

* Remove old comment
  • Loading branch information
albrow authored Apr 7, 2020
1 parent 5c42d07 commit 9613b42
Show file tree
Hide file tree
Showing 10 changed files with 645 additions and 707 deletions.
7 changes: 2 additions & 5 deletions core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package core
import (
"context"
"flag"
"math/big"
"sync"
"testing"
"time"
Expand All @@ -14,9 +13,9 @@ import (
"github.com/0xProject/0x-mesh/ethereum"
"github.com/0xProject/0x-mesh/meshdb"
"github.com/0xProject/0x-mesh/scenario"
"github.com/0xProject/0x-mesh/scenario/orderopts"
"github.com/0xProject/0x-mesh/zeroex"
"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/ethclient"
ethrpc "github.com/ethereum/go-ethereum/rpc"
"github.com/google/uuid"
"github.com/libp2p/go-libp2p-core/peer"
Expand Down Expand Up @@ -119,7 +118,6 @@ func newTestApp(t *testing.T) *App {

var (
rpcClient *ethrpc.Client
ethClient *ethclient.Client
blockchainLifecycle *ethereum.BlockchainLifecycle
)

Expand All @@ -137,7 +135,6 @@ func init() {
if err != nil {
panic(err)
}
ethClient = ethclient.NewClient(rpcClient)
blockchainLifecycle, err = ethereum.NewBlockchainLifecycle(rpcClient)
if err != nil {
panic(err)
Expand Down Expand Up @@ -206,7 +203,7 @@ func TestOrderSync(t *testing.T) {
// Manually add some orders to originalNode.
originalOrders := make([]*zeroex.SignedOrder, 10)
for i := range originalOrders {
originalOrders[i] = scenario.CreateWETHForZRXSignedTestOrder(t, ethClient, constants.GanacheAccount1, constants.GanacheAccount2, big.NewInt(20), big.NewInt(5))
originalOrders[i] = scenario.NewSignedTestOrder(t, orderopts.SetupMakerState(true))
}

// We have to wait for latest block to be processed by the Mesh node.
Expand Down
18 changes: 15 additions & 3 deletions integration-tests/browser_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package integrationtests
import (
"context"
"fmt"
"math/big"
"net/http"
"net/http/httptest"
"strconv"
Expand All @@ -15,11 +16,12 @@ import (
"testing"
"time"

"github.com/0xProject/0x-mesh/constants"
"github.com/0xProject/0x-mesh/rpc"
"github.com/0xProject/0x-mesh/scenario"
"github.com/0xProject/0x-mesh/scenario/orderopts"
"github.com/0xProject/0x-mesh/zeroex"
"github.com/chromedp/chromedp"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -65,8 +67,18 @@ func TestBrowserIntegration(t *testing.T) {

// standaloneOrder is an order that will be sent to the network by the
// standalone node.
ethClient := ethclient.NewClient(ethRPCClient)
standaloneOrder := scenario.CreateZRXForWETHSignedTestOrder(t, ethClient, makerAddress, takerAddress, wethAmount, zrxAmount)
standaloneOrder := scenario.NewSignedTestOrder(t, orderopts.SetupMakerState(true))

// We also need to set up the maker state for the order that will be created in the browser (we don't care
// if this order exactly matches the one created in the browser, we just care about makerAddress,
// makerAssetData, and makerAssetAmount).
scenario.NewSignedTestOrder(t,
orderopts.SetupMakerState(true),
orderopts.MakerAddress(constants.GanacheAccount1),
orderopts.MakerAssetData(scenario.ZRXAssetData),
orderopts.MakerAssetAmount(big.NewInt(1000)),
)

// Creating a valid order involves transferring sufficient funds to the maker, and setting their allowance for
// the maker asset. These transactions must be mined and Mesh's BlockWatcher poller must process these blocks
// in order for the order validation run at order submission to occur at a block number equal or higher then
Expand Down
14 changes: 0 additions & 14 deletions integration-tests/constants.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package integrationtests

import (
"math/big"

"github.com/0xProject/0x-mesh/constants"
)

const (
ethereumRPCURL = "http://localhost:8545"
ethereumChainID = 1337
Expand All @@ -27,11 +21,3 @@ const (
bootstrapList = "/ip4/127.0.0.1/tcp/60500/ws/ipfs/16Uiu2HAmGd949LwaV4KNvK2WDSiMVy7xEmW983VH75CMmefmMpP7"
bootstrapDataDir = "./data/bootstrap-0"
)

var (
makerAddress = constants.GanacheAccount1
takerAddress = constants.GanacheAccount2
eighteenDecimalsInBaseUnits = new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)
wethAmount = new(big.Int).Mul(big.NewInt(50), eighteenDecimalsInBaseUnits)
zrxAmount = new(big.Int).Mul(big.NewInt(100), eighteenDecimalsInBaseUnits)
)
18 changes: 5 additions & 13 deletions integration-tests/rpc_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@ import (
"context"
"encoding/json"
"fmt"
"math/big"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"

"github.com/0xProject/0x-mesh/common/types"

"github.com/0xProject/0x-mesh/constants"
"github.com/0xProject/0x-mesh/ethereum/ratelimit"
"github.com/0xProject/0x-mesh/rpc"
"github.com/0xProject/0x-mesh/scenario"
"github.com/0xProject/0x-mesh/scenario/orderopts"
"github.com/0xProject/0x-mesh/zeroex"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -61,8 +59,7 @@ func runAddOrdersSuccessTest(t *testing.T, rpcEndpointPrefix, rpcServerType stri
require.NoError(t, err)

// Create a new valid order.
ethClient := ethclient.NewClient(ethRPCClient)
signedTestOrder := scenario.CreateZRXForWETHSignedTestOrder(t, ethClient, makerAddress, takerAddress, wethAmount, zrxAmount)
signedTestOrder := scenario.NewSignedTestOrder(t, orderopts.SetupMakerState(true))
// Creating a valid order involves transferring sufficient funds to the maker, and setting their allowance for
// the maker asset. These transactions must be mined and Mesh's BlockWatcher poller must process these blocks
// in order for the order validation run at order submission to occur at a block number equal or higher then
Expand Down Expand Up @@ -130,15 +127,11 @@ func runGetOrdersTest(t *testing.T, rpcEndpointPrefix, rpcServerType string, rpc
require.NoError(t, err)

// Create 10 new valid orders.
ethClient := ethclient.NewClient(ethRPCClient)
// NOTE(jalextowle): The default balances are not sufficient to create 10 valid
// orders, so we modify the zrx and weth amounts for this test
// TODO(albrow): Update this when scenario supports creating orders in a batch.
numOrders := 10
newWethAmount := new(big.Int).Div(wethAmount, big.NewInt(int64(numOrders)))
newZrxAmount := new(big.Int).Div(zrxAmount, big.NewInt(int64(numOrders)))
signedTestOrders := make([]*zeroex.SignedOrder, numOrders)
for i := 0; i < numOrders; i++ {
signedTestOrders[i] = scenario.CreateZRXForWETHSignedTestOrder(t, ethClient, makerAddress, takerAddress, newWethAmount, newZrxAmount)
signedTestOrders[i] = scenario.NewSignedTestOrder(t, orderopts.SetupMakerState(true))
}
// Creating a valid order involves transferring sufficient funds to the maker, and setting their allowance for
// the maker asset. These transactions must be mined and Mesh's BlockWatcher poller must process these blocks
Expand Down Expand Up @@ -312,8 +305,7 @@ func TestOrdersSubscription(t *testing.T) {
assert.NotNil(t, clientSubscription, "clientSubscription not nil")

// Create a valid order and send it to the rpc client's "AddOrders" endpoint.
ethClient := ethclient.NewClient(ethRPCClient)
signedTestOrder := scenario.CreateZRXForWETHSignedTestOrder(t, ethClient, makerAddress, takerAddress, wethAmount, zrxAmount)
signedTestOrder := scenario.NewSignedTestOrder(t, orderopts.SetupMakerState(true))
// Creating a valid order involves transferring sufficient funds to the maker, and setting their allowance for
// the maker asset. These transactions must be mined and Mesh's BlockWatcher poller must process these blocks
// in order for the order validation run at order submission to occur at a block number equal or higher then
Expand Down
4 changes: 2 additions & 2 deletions packages/integration-tests/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ provider.start();
makerAddress: '0x6ecbe1db9ef729cbe972c83fb886247691fb6beb',
makerAssetData: '0xf47261b0000000000000000000000000871dd7c2b4b25e1aa18728e9d5f2af4c4e431f5c',
makerFeeAssetData: '0x',
makerAssetAmount: new BigNumber('100000000000000000000'),
makerAssetAmount: new BigNumber('1000'),
makerFee: new BigNumber('0'),
takerAddress: '0x0000000000000000000000000000000000000000',
takerAssetData: '0xf47261b00000000000000000000000000b1ba0af832d7c05fd64161e0db78e85978e8082',
takerFeeAssetData: '0x',
takerAssetAmount: new BigNumber('50000000000000000000'),
takerAssetAmount: new BigNumber('5000'),
takerFee: new BigNumber('0'),
senderAddress: '0x0000000000000000000000000000000000000000',
exchangeAddress: '0x48bacb9266a570d521063ef5dd96e61686dbe788',
Expand Down
114 changes: 114 additions & 0 deletions scenario/orderopts/orderopts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package orderopts

import (
"math/big"

"github.com/0xProject/0x-mesh/zeroex"
"github.com/ethereum/go-ethereum/common"
)

type Config struct {
Order *zeroex.Order
SetupMakerState bool
SetupTakerAddress common.Address
}

type Option func(order *Config) error

// Apply applies the given options to the config, returning the first error
// encountered (if any).
func (cfg *Config) Apply(opts ...Option) error {
for _, opt := range opts {
if opt == nil {
continue
}
if err := opt(cfg); err != nil {
return err
}
}
return nil
}

func MakerAddress(address common.Address) Option {
return func(cfg *Config) error {
cfg.Order.MakerAddress = address
return nil
}
}

func MakerAssetData(assetData []byte) Option {
return func(cfg *Config) error {
cfg.Order.MakerAssetData = assetData
return nil
}
}

func MakerAssetAmount(amount *big.Int) Option {
return func(cfg *Config) error {
cfg.Order.MakerAssetAmount = amount
return nil
}
}

func TakerAssetData(assetData []byte) Option {
return func(cfg *Config) error {
cfg.Order.TakerAssetData = assetData
return nil
}
}

func TakerAssetAmount(amount *big.Int) Option {
return func(cfg *Config) error {
cfg.Order.TakerAssetAmount = amount
return nil
}
}

func ExpirationTimeSeconds(expirationTimeSeconds *big.Int) Option {
return func(cfg *Config) error {
cfg.Order.ExpirationTimeSeconds = expirationTimeSeconds
return nil
}
}

func MakerFeeAssetData(assetData []byte) Option {
return func(cfg *Config) error {
cfg.Order.MakerFeeAssetData = assetData
return nil
}
}

func MakerFee(amount *big.Int) Option {
return func(cfg *Config) error {
cfg.Order.MakerFee = amount
return nil
}
}

func SenderAddress(address common.Address) Option {
return func(cfg *Config) error {
cfg.Order.SenderAddress = address
return nil
}
}

func FeeRecipientAddress(address common.Address) Option {
return func(cfg *Config) error {
cfg.Order.FeeRecipientAddress = address
return nil
}
}

func SetupMakerState(b bool) Option {
return func(cfg *Config) error {
cfg.SetupMakerState = b
return nil
}
}

func SetupTakerAddress(takerAddress common.Address) Option {
return func(cfg *Config) error {
cfg.SetupTakerAddress = takerAddress
return nil
}
}
Loading

0 comments on commit 9613b42

Please sign in to comment.