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

Commit

Permalink
Move OrderValidator into it's own module
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioberger committed Sep 14, 2019
1 parent 3f3a1f3 commit bda3547
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 142 deletions.
3 changes: 2 additions & 1 deletion cmd/mesh/rpc_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/0xProject/0x-mesh/core"
"github.com/0xProject/0x-mesh/rpc"
"github.com/0xProject/0x-mesh/zeroex"
"github.com/0xProject/0x-mesh/zeroex/ordervalidate"
ethRpc "github.com/ethereum/go-ethereum/rpc"
peerstore "github.com/libp2p/go-libp2p-peerstore"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -70,7 +71,7 @@ func (handler *rpcHandler) GetOrders(page, perPage int, snapshotID string) (*rpc
}

// AddOrders is called when an RPC client calls AddOrders.
func (handler *rpcHandler) AddOrders(signedOrdersRaw []*json.RawMessage) (*zeroex.ValidationResults, error) {
func (handler *rpcHandler) AddOrders(signedOrdersRaw []*json.RawMessage) (*ordervalidate.ValidationResults, error) {
log.WithField("count", len(signedOrdersRaw)).Debug("received AddOrders request via RPC")
validationResults, err := handler.app.AddOrders(signedOrdersRaw)
if err != nil {
Expand Down
33 changes: 17 additions & 16 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/0xProject/0x-mesh/p2p"
"github.com/0xProject/0x-mesh/rpc"
"github.com/0xProject/0x-mesh/zeroex"
"github.com/0xProject/0x-mesh/zeroex/ordervalidate"
"github.com/0xProject/0x-mesh/zeroex/orderwatch"
"github.com/albrow/stringset"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -111,7 +112,7 @@ type App struct {
blockWatcher *blockwatch.Watcher
orderWatcher *orderwatch.Watcher
ethWatcher *ethereum.ETHWatcher
orderValidator *zeroex.OrderValidator
orderValidator *ordervalidate.OrderValidator
orderJSONSchema *gojsonschema.Schema
meshMessageJSONSchema *gojsonschema.Schema
snapshotExpirationWatcher *expirationwatch.Watcher
Expand All @@ -138,8 +139,8 @@ func New(config Config) (*App, error) {
}
log.AddHook(loghooks.NewPeerIDHook(peerID))

if config.EthereumRPCMaxContentLength < zeroex.MaxOrderSizeInBytes {
return nil, fmt.Errorf("Cannot set `EthereumRPCMaxContentLength` to be less then MaxOrderSizeInBytes: %d", zeroex.MaxOrderSizeInBytes)
if config.EthereumRPCMaxContentLength < ordervalidate.MaxOrderSizeInBytes {
return nil, fmt.Errorf("Cannot set `EthereumRPCMaxContentLength` to be less then MaxOrderSizeInBytes: %d", ordervalidate.MaxOrderSizeInBytes)
}
config = unquoteConfig(config)

Expand Down Expand Up @@ -179,7 +180,7 @@ func New(config Config) (*App, error) {
blockWatcher := blockwatch.New(blockWatcherConfig)

// Initialize the order validator
orderValidator, err := zeroex.NewOrderValidator(ethClient, config.EthereumNetworkID, config.EthereumRPCMaxContentLength, config.OrderExpirationBuffer)
orderValidator, err := ordervalidate.NewOrderValidator(ethClient, config.EthereumNetworkID, config.EthereumRPCMaxContentLength, config.OrderExpirationBuffer)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -555,10 +556,10 @@ func (app *App) GetOrders(page, perPage int, snapshotID string) (*rpc.GetOrdersR

// AddOrders can be used to add orders to Mesh. It validates the given orders
// and if they are valid, will store and eventually broadcast the orders to peers.
func (app *App) AddOrders(signedOrdersRaw []*json.RawMessage) (*zeroex.ValidationResults, error) {
allValidationResults := &zeroex.ValidationResults{
Accepted: []*zeroex.AcceptedOrderInfo{},
Rejected: []*zeroex.RejectedOrderInfo{},
func (app *App) AddOrders(signedOrdersRaw []*json.RawMessage) (*ordervalidate.ValidationResults, error) {
allValidationResults := &ordervalidate.ValidationResults{
Accepted: []*ordervalidate.AcceptedOrderInfo{},
Rejected: []*ordervalidate.RejectedOrderInfo{},
}
orderHashesSeen := map[common.Hash]struct{}{}
schemaValidOrders := []*zeroex.SignedOrder{}
Expand All @@ -571,29 +572,29 @@ func (app *App) AddOrders(signedOrdersRaw []*json.RawMessage) (*zeroex.Validatio
signedOrder = nil
}
log.WithField("signedOrderRaw", string(signedOrderBytes)).Info("Unexpected error while attempting to validate signedOrderJSON against schema")
allValidationResults.Rejected = append(allValidationResults.Rejected, &zeroex.RejectedOrderInfo{
allValidationResults.Rejected = append(allValidationResults.Rejected, &ordervalidate.RejectedOrderInfo{
SignedOrder: signedOrder,
Kind: zeroex.MeshValidation,
Status: zeroex.RejectedOrderStatus{
Code: zeroex.ROInvalidSchemaCode,
Kind: ordervalidate.MeshValidation,
Status: ordervalidate.RejectedOrderStatus{
Code: ordervalidate.ROInvalidSchemaCode,
Message: "order did not pass JSON-schema validation: Malformed JSON or empty payload",
},
})
continue
}
if !result.Valid() {
log.WithField("signedOrderRaw", string(signedOrderBytes)).Info("Order failed schema validation")
status := zeroex.RejectedOrderStatus{
Code: zeroex.ROInvalidSchemaCode,
status := ordervalidate.RejectedOrderStatus{
Code: ordervalidate.ROInvalidSchemaCode,
Message: fmt.Sprintf("order did not pass JSON-schema validation: %s", result.Errors()),
}
signedOrder := &zeroex.SignedOrder{}
if err := signedOrder.UnmarshalJSON(signedOrderBytes); err != nil {
signedOrder = nil
}
allValidationResults.Rejected = append(allValidationResults.Rejected, &zeroex.RejectedOrderInfo{
allValidationResults.Rejected = append(allValidationResults.Rejected, &ordervalidate.RejectedOrderInfo{
SignedOrder: signedOrder,
Kind: zeroex.MeshValidation,
Kind: ordervalidate.MeshValidation,
Status: status,
})
continue
Expand Down
5 changes: 3 additions & 2 deletions core/message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/0xProject/0x-mesh/meshdb"
"github.com/0xProject/0x-mesh/p2p"
"github.com/0xProject/0x-mesh/zeroex"
"github.com/0xProject/0x-mesh/zeroex/ordervalidate"
"github.com/ethereum/go-ethereum/common"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -76,7 +77,7 @@ func (app *App) HandleMessages(messages []*p2p.Message) error {
log.WithFields(map[string]interface{}{
"error": err,
"from": msg.From,
"maxOrderSizeInBytes": zeroex.MaxOrderSizeInBytes,
"maxOrderSizeInBytes": ordervalidate.MaxOrderSizeInBytes,
"actualSizeInBytes": len(msg.Data),
}).Trace("received message that exceeds maximum size")
app.handlePeerScoreEvent(msg.From, psInvalidMessage)
Expand Down Expand Up @@ -168,7 +169,7 @@ func (app *App) HandleMessages(messages []*p2p.Message) error {
"from": msg.From.String(),
}).Trace("not storing rejected order received from peer")
switch rejectedOrderInfo.Status {
case zeroex.ROInternalError, zeroex.ROEthRPCRequestFailed, zeroex.ROCoordinatorRequestFailed:
case ordervalidate.ROInternalError, ordervalidate.ROEthRPCRequestFailed, ordervalidate.ROCoordinatorRequestFailed:
// Don't incur a negative score for these status types (it might not be
// their fault).
default:
Expand Down
49 changes: 25 additions & 24 deletions core/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
"github.com/0xProject/0x-mesh/meshdb"
"github.com/0xProject/0x-mesh/p2p"
"github.com/0xProject/0x-mesh/zeroex"
"github.com/0xProject/0x-mesh/zeroex/ordervalidate"
log "github.com/sirupsen/logrus"
"github.com/xeipuuv/gojsonschema"
)

var errMaxSize = fmt.Errorf("message exceeds maximum size of %d bytes", zeroex.MaxOrderSizeInBytes)
var errMaxSize = fmt.Errorf("message exceeds maximum size of %d bytes", ordervalidate.MaxOrderSizeInBytes)

// JSON-schema schemas
var (
Expand Down Expand Up @@ -94,8 +95,8 @@ func (app *App) schemaValidateMeshMessage(o []byte) (*gojsonschema.Result, error

// validateOrders applies general 0x validation and Mesh-specific validation to
// the given orders.
func (app *App) validateOrders(orders []*zeroex.SignedOrder) (*zeroex.ValidationResults, error) {
results := &zeroex.ValidationResults{}
func (app *App) validateOrders(orders []*zeroex.SignedOrder) (*ordervalidate.ValidationResults, error) {
results := &ordervalidate.ValidationResults{}
validMeshOrders := []*zeroex.SignedOrder{}
contractAddresses, err := ethereum.GetContractAddressesForNetworkID(app.networkID)
if err != nil {
Expand All @@ -105,11 +106,11 @@ func (app *App) validateOrders(orders []*zeroex.SignedOrder) (*zeroex.Validation
orderHash, err := order.ComputeOrderHash()
if err != nil {
log.WithField("error", err).Error("could not compute order hash")
results.Rejected = append(results.Rejected, &zeroex.RejectedOrderInfo{
results.Rejected = append(results.Rejected, &ordervalidate.RejectedOrderInfo{
OrderHash: orderHash,
SignedOrder: order,
Kind: zeroex.MeshError,
Status: zeroex.ROInternalError,
Kind: ordervalidate.MeshError,
Status: ordervalidate.ROInternalError,
})
continue
}
Expand All @@ -120,39 +121,39 @@ func (app *App) validateOrders(orders []*zeroex.SignedOrder) (*zeroex.Validation
// validating Coordinator orders. What we're missing is a way to effeciently
// remove orders that are soft-canceled via the Coordinator API).
if order.SenderAddress != constants.NullAddress {
results.Rejected = append(results.Rejected, &zeroex.RejectedOrderInfo{
results.Rejected = append(results.Rejected, &ordervalidate.RejectedOrderInfo{
OrderHash: orderHash,
SignedOrder: order,
Kind: zeroex.MeshValidation,
Status: zeroex.ROSenderAddressNotAllowed,
Kind: ordervalidate.MeshValidation,
Status: ordervalidate.ROSenderAddressNotAllowed,
})
continue
}
if order.ExchangeAddress != contractAddresses.Exchange {
results.Rejected = append(results.Rejected, &zeroex.RejectedOrderInfo{
results.Rejected = append(results.Rejected, &ordervalidate.RejectedOrderInfo{
OrderHash: orderHash,
SignedOrder: order,
Kind: zeroex.MeshValidation,
Status: zeroex.ROIncorrectNetwork,
Kind: ordervalidate.MeshValidation,
Status: ordervalidate.ROIncorrectNetwork,
})
continue
}
if err := validateOrderSize(order); err != nil {
if err == errMaxSize {
results.Rejected = append(results.Rejected, &zeroex.RejectedOrderInfo{
results.Rejected = append(results.Rejected, &ordervalidate.RejectedOrderInfo{
OrderHash: orderHash,
SignedOrder: order,
Kind: zeroex.MeshValidation,
Status: zeroex.ROMaxOrderSizeExceeded,
Kind: ordervalidate.MeshValidation,
Status: ordervalidate.ROMaxOrderSizeExceeded,
})
continue
} else {
log.WithField("error", err).Error("could not validate order size")
results.Rejected = append(results.Rejected, &zeroex.RejectedOrderInfo{
results.Rejected = append(results.Rejected, &ordervalidate.RejectedOrderInfo{
OrderHash: orderHash,
SignedOrder: order,
Kind: zeroex.MeshError,
Status: zeroex.ROInternalError,
Kind: ordervalidate.MeshError,
Status: ordervalidate.ROInternalError,
})
continue
}
Expand All @@ -169,16 +170,16 @@ func (app *App) validateOrders(orders []*zeroex.SignedOrder) (*zeroex.Validation
} else {
// If stored but flagged for removal, reject it
if dbOrder.IsRemoved {
results.Rejected = append(results.Rejected, &zeroex.RejectedOrderInfo{
results.Rejected = append(results.Rejected, &ordervalidate.RejectedOrderInfo{
OrderHash: orderHash,
SignedOrder: order,
Kind: zeroex.MeshValidation,
Status: zeroex.ROOrderAlreadyStoredAndUnfillable,
Kind: ordervalidate.MeshValidation,
Status: ordervalidate.ROOrderAlreadyStoredAndUnfillable,
})
continue
} else {
// If stored but not flagged for removal, accept it without re-validation
results.Accepted = append(results.Accepted, &zeroex.AcceptedOrderInfo{
results.Accepted = append(results.Accepted, &ordervalidate.AcceptedOrderInfo{
OrderHash: orderHash,
SignedOrder: order,
FillableTakerAssetAmount: dbOrder.FillableTakerAssetAmount,
Expand All @@ -198,7 +199,7 @@ func (app *App) validateOrders(orders []*zeroex.SignedOrder) (*zeroex.Validation
}

func validateMessageSize(message *p2p.Message) error {
if len(message.Data) > zeroex.MaxOrderSizeInBytes {
if len(message.Data) > ordervalidate.MaxOrderSizeInBytes {
return errMaxSize
}
return nil
Expand All @@ -209,7 +210,7 @@ func validateOrderSize(order *zeroex.SignedOrder) error {
if err != nil {
return err
}
if len(encoded) > zeroex.MaxOrderSizeInBytes {
if len(encoded) > ordervalidate.MaxOrderSizeInBytes {
return errMaxSize
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions expirationwatch/expiration_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sync"
"time"

"github.com/0xProject/0x-mesh/zeroex"
"github.com/0xProject/0x-mesh/zeroex/ordervalidate"
"github.com/albrow/stringset"
"github.com/ocdogan/rbt"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -137,7 +137,7 @@ func (w *Watcher) prune() []ExpiredItem {
}
expirationTimeSeconds := int64(*key.(*rbt.Int64Key))
expirationTime := time.Unix(expirationTimeSeconds, 0)
if !zeroex.IsExpired(expirationTime, w.expirationBuffer) {
if !ordervalidate.IsExpired(expirationTime, w.expirationBuffer) {
break
}
ids := value.(stringset.Set)
Expand Down
5 changes: 3 additions & 2 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/0xProject/0x-mesh/zeroex"
"github.com/0xProject/0x-mesh/zeroex/ordervalidate"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rpc"
peer "github.com/libp2p/go-libp2p-peer"
Expand All @@ -30,8 +31,8 @@ func NewClient(addr string) (*Client, error) {

// AddOrders adds orders to the 0x Mesh node and broadcasts them throughout the
// 0x Mesh network.
func (c *Client) AddOrders(orders []*zeroex.SignedOrder) (*zeroex.ValidationResults, error) {
var validationResults zeroex.ValidationResults
func (c *Client) AddOrders(orders []*zeroex.SignedOrder) (*ordervalidate.ValidationResults, error) {
var validationResults ordervalidate.ValidationResults
if err := c.rpcClient.Call(&validationResults, "mesh_addOrders", orders); err != nil {
return nil, err
}
Expand Down
11 changes: 6 additions & 5 deletions rpc/client_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/0xProject/0x-mesh/constants"
"github.com/0xProject/0x-mesh/ethereum"
"github.com/0xProject/0x-mesh/zeroex"
"github.com/0xProject/0x-mesh/zeroex/ordervalidate"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rpc"
peer "github.com/libp2p/go-libp2p-peer"
Expand All @@ -26,14 +27,14 @@ import (
// dummyRPCHandler is used for testing purposes. It allows declaring handlers
// for some requests or all of them, depending on testing needs.
type dummyRPCHandler struct {
addOrdersHandler func(signedOrdersRaw []*json.RawMessage) (*zeroex.ValidationResults, error)
addOrdersHandler func(signedOrdersRaw []*json.RawMessage) (*ordervalidate.ValidationResults, error)
getOrdersHandler func(page, perPage int, snapshotID string) (*GetOrdersResponse, error)
addPeerHandler func(peerInfo peerstore.PeerInfo) error
getStatsHandler func() (*GetStatsResponse, error)
subscribeToOrdersHandler func(ctx context.Context) (*rpc.Subscription, error)
}

func (d *dummyRPCHandler) AddOrders(signedOrdersRaw []*json.RawMessage) (*zeroex.ValidationResults, error) {
func (d *dummyRPCHandler) AddOrders(signedOrdersRaw []*json.RawMessage) (*ordervalidate.ValidationResults, error) {
if d.addOrdersHandler == nil {
return nil, errors.New("dummyRPCHandler: no handler set for AddOrder")
}
Expand Down Expand Up @@ -124,16 +125,16 @@ func TestAddOrdersSuccess(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(1)
rpcHandler := &dummyRPCHandler{
addOrdersHandler: func(signedOrdersRaw []*json.RawMessage) (*zeroex.ValidationResults, error) {
addOrdersHandler: func(signedOrdersRaw []*json.RawMessage) (*ordervalidate.ValidationResults, error) {
require.Len(t, signedOrdersRaw, 1)
validationResponse := &zeroex.ValidationResults{}
validationResponse := &ordervalidate.ValidationResults{}
for _, signedOrderRaw := range signedOrdersRaw {
signedOrder := &zeroex.SignedOrder{}
err := signedOrder.UnmarshalJSON([]byte(*signedOrderRaw))
require.NoError(t, err)
orderHash, err := signedOrder.ComputeOrderHash()
require.NoError(t, err)
validationResponse.Accepted = append(validationResponse.Accepted, &zeroex.AcceptedOrderInfo{
validationResponse.Accepted = append(validationResponse.Accepted, &ordervalidate.AcceptedOrderInfo{
OrderHash: orderHash,
SignedOrder: signedOrder,
FillableTakerAssetAmount: signedOrder.TakerAssetAmount,
Expand Down
6 changes: 3 additions & 3 deletions rpc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

"github.com/0xProject/0x-mesh/constants"
"github.com/0xProject/0x-mesh/zeroex"
"github.com/0xProject/0x-mesh/zeroex/ordervalidate"
"github.com/ethereum/go-ethereum/rpc"
ethRpc "github.com/ethereum/go-ethereum/rpc"
peer "github.com/libp2p/go-libp2p-peer"
Expand All @@ -28,7 +28,7 @@ type rpcService struct {
// RPCHandler is used to respond to incoming requests from the client.
type RPCHandler interface {
// AddOrders is called when the client sends an AddOrders request.
AddOrders(signedOrdersRaw []*json.RawMessage) (*zeroex.ValidationResults, error)
AddOrders(signedOrdersRaw []*json.RawMessage) (*ordervalidate.ValidationResults, error)
// GetOrders is called when the clients sends a GetOrders request
GetOrders(page, perPage int, snapshotID string) (*GetOrdersResponse, error)
// AddPeer is called when the client sends an AddPeer request.
Expand Down Expand Up @@ -118,7 +118,7 @@ func SetupHeartbeat(ctx context.Context) (*ethRpc.Subscription, error) {
}

// AddOrders calls rpcHandler.AddOrders and returns the validation results.
func (s *rpcService) AddOrders(signedOrdersRaw []*json.RawMessage) (*zeroex.ValidationResults, error) {
func (s *rpcService) AddOrders(signedOrdersRaw []*json.RawMessage) (*ordervalidate.ValidationResults, error) {
return s.rpcHandler.AddOrders(signedOrdersRaw)
}

Expand Down
2 changes: 1 addition & 1 deletion scenario/scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func SetupBalancesAndAllowances(t *testing.T, makerAddress, takerAddress common.
// Transfer ZRX to makerAddress
opts = &bind.TransactOpts{
From: zrxCoinbase,
Signer: GetTestSignerFn(zrxCoinbase),
Signer: getTestSignerFn(zrxCoinbase),
}
txn, err = zrx.Transfer(opts, makerAddress, zrxAmount)
require.NoError(t, err)
Expand Down
Loading

0 comments on commit bda3547

Please sign in to comment.