Skip to content

Commit

Permalink
Revert dex removal (#1801)
Browse files Browse the repository at this point in the history
* Revert dex removal

* Add v5.7.4

* Update integration test

* update go mod
  • Loading branch information
udpatil authored Aug 1, 2024
1 parent d57ab7d commit d502396
Show file tree
Hide file tree
Showing 322 changed files with 52,600 additions and 39 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/fuzz.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Fuzzing


on:
push:
branches:
- main
pull_request:
branches:
- main

defaults:
run:
shell: bash

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.21

- name: Fuzz Place Order Msg
run: go test github.com/sei-protocol/sei-chain/x/dex/keeper/msgserver -fuzz FuzzPlaceOrders -fuzztime 30s

- name: Fuzz Limit Order Matching
run: go test github.com/sei-protocol/sei-chain/x/dex/exchange -fuzz FuzzMatchLimitOrders -fuzztime 30s

- name: Fuzz Market Order Matching
run: go test github.com/sei-protocol/sei-chain/x/dex/exchange -fuzz FuzzMatchMarketOrders -fuzztime 30s

- name: Fuzz Limit Order Settlement
run: go test github.com/sei-protocol/sei-chain/x/dex/exchange -fuzz FuzzSettleLimitOrder -fuzztime 30s

- name: Fuzz Market Order Settlement
run: go test github.com/sei-protocol/sei-chain/x/dex/exchange -fuzz FuzzSettleMarketOrder -fuzztime 30s
1 change: 1 addition & 0 deletions aclmapping/bank/mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var ErrorInvalidMsgType = fmt.Errorf("invalid message received for bank module")
func GetBankDepedencyGenerator() aclkeeper.DependencyGeneratorMap {
dependencyGeneratorMap := make(aclkeeper.DependencyGeneratorMap)

// dex place orders
placeOrdersKey := acltypes.GenerateMessageKey(&banktypes.MsgSend{})
dependencyGeneratorMap[placeOrdersKey] = MsgSendDependencyGenerator

Expand Down
2 changes: 2 additions & 0 deletions aclmapping/dependency_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aclmapping
import (
aclkeeper "github.com/cosmos/cosmos-sdk/x/accesscontrol/keeper"
aclbankmapping "github.com/sei-protocol/sei-chain/aclmapping/bank"
acldexmapping "github.com/sei-protocol/sei-chain/aclmapping/dex"
aclevmmapping "github.com/sei-protocol/sei-chain/aclmapping/evm"
acloraclemapping "github.com/sei-protocol/sei-chain/aclmapping/oracle"
acltokenfactorymapping "github.com/sei-protocol/sei-chain/aclmapping/tokenfactory"
Expand All @@ -20,6 +21,7 @@ func (customDepGen CustomDependencyGenerator) GetCustomDependencyGenerators(evmK
dependencyGeneratorMap := make(aclkeeper.DependencyGeneratorMap)
wasmDependencyGenerators := aclwasmmapping.NewWasmDependencyGenerator()

dependencyGeneratorMap = dependencyGeneratorMap.Merge(acldexmapping.GetDexDependencyGenerators())
dependencyGeneratorMap = dependencyGeneratorMap.Merge(aclbankmapping.GetBankDepedencyGenerator())
dependencyGeneratorMap = dependencyGeneratorMap.Merge(acltokenfactorymapping.GetTokenFactoryDependencyGenerators())
dependencyGeneratorMap = dependencyGeneratorMap.Merge(wasmDependencyGenerators.GetWasmDependencyGenerators())
Expand Down
229 changes: 229 additions & 0 deletions aclmapping/dex/mappings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
package acldexmapping

import (
"encoding/hex"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkacltypes "github.com/cosmos/cosmos-sdk/types/accesscontrol"
aclkeeper "github.com/cosmos/cosmos-sdk/x/accesscontrol/keeper"
acltypes "github.com/cosmos/cosmos-sdk/x/accesscontrol/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
dexkeeper "github.com/sei-protocol/sei-chain/x/dex/keeper"
dextypes "github.com/sei-protocol/sei-chain/x/dex/types"
)

var ErrPlaceOrdersGenerator = fmt.Errorf("invalid message received for dex module")

func GetDexDependencyGenerators() aclkeeper.DependencyGeneratorMap {
dependencyGeneratorMap := make(aclkeeper.DependencyGeneratorMap)

// dex place orders
placeOrdersKey := acltypes.GenerateMessageKey(&dextypes.MsgPlaceOrders{})
cancelOrdersKey := acltypes.GenerateMessageKey(&dextypes.MsgCancelOrders{})
dependencyGeneratorMap[placeOrdersKey] = DexPlaceOrdersDependencyGenerator
dependencyGeneratorMap[cancelOrdersKey] = DexCancelOrdersDependencyGenerator

return dependencyGeneratorMap
}

func GetLongShortOrderBookOps(contractAddr string, priceDenom string, assetDenom string) []sdkacltypes.AccessOperation {
return []sdkacltypes.AccessOperation{
{
AccessType: sdkacltypes.AccessType_READ,
ResourceType: sdkacltypes.ResourceType_KV_DEX_CONTRACT_LONGBOOK,
IdentifierTemplate: hex.EncodeToString(dextypes.OrderBookPrefix(true, contractAddr, priceDenom, assetDenom)),
},

{
AccessType: sdkacltypes.AccessType_READ,
ResourceType: sdkacltypes.ResourceType_KV_DEX_CONTRACT_SHORTBOOK,
IdentifierTemplate: hex.EncodeToString(dextypes.OrderBookPrefix(false, contractAddr, priceDenom, assetDenom)),
},
}
}

func DexPlaceOrdersDependencyGenerator(keeper aclkeeper.Keeper, _ sdk.Context, msg sdk.Msg) ([]sdkacltypes.AccessOperation, error) {
// TODO: read + write downstream, write contracts to process
placeOrdersMsg, ok := msg.(*dextypes.MsgPlaceOrders)
if !ok {
return []sdkacltypes.AccessOperation{}, ErrPlaceOrdersGenerator
}

moduleAdr := keeper.AccountKeeper.GetModuleAddress(dextypes.ModuleName)

senderBankAddrIdentifier := hex.EncodeToString(banktypes.CreateAccountBalancesPrefixFromBech32(placeOrdersMsg.Creator))
contractAddr := placeOrdersMsg.ContractAddr

aclOps := []sdkacltypes.AccessOperation{
{
AccessType: sdkacltypes.AccessType_READ,
ResourceType: sdkacltypes.ResourceType_KV_DEX_NEXT_ORDER_ID,
IdentifierTemplate: hex.EncodeToString(dextypes.NextOrderIDPrefix(contractAddr)),
},
{
AccessType: sdkacltypes.AccessType_WRITE,
ResourceType: sdkacltypes.ResourceType_KV_DEX_NEXT_ORDER_ID,
IdentifierTemplate: hex.EncodeToString(dextypes.NextOrderIDPrefix(contractAddr)),
},
{
AccessType: sdkacltypes.AccessType_READ,
ResourceType: sdkacltypes.ResourceType_KV_DEX_REGISTERED_PAIR,
IdentifierTemplate: hex.EncodeToString(dextypes.RegisteredPairPrefix(contractAddr)),
},
{
AccessType: sdkacltypes.AccessType_READ,
ResourceType: sdkacltypes.ResourceType_KV_DEX_MEM_DEPOSIT,
IdentifierTemplate: hex.EncodeToString(append(
dextypes.MemDepositPrefix(contractAddr),
[]byte(placeOrdersMsg.Creator)...,
)),
},
{
AccessType: sdkacltypes.AccessType_WRITE,
ResourceType: sdkacltypes.ResourceType_KV_DEX_MEM_DEPOSIT,
IdentifierTemplate: hex.EncodeToString(append(
dextypes.MemDepositPrefix(contractAddr),
[]byte(placeOrdersMsg.Creator)...,
)),
},

{
AccessType: sdkacltypes.AccessType_READ,
ResourceType: sdkacltypes.ResourceType_KV_DEX_MEM_ORDER,
IdentifierTemplate: hex.EncodeToString(dextypes.MemOrderPrefix(contractAddr)),
},
{
AccessType: sdkacltypes.AccessType_WRITE,
ResourceType: sdkacltypes.ResourceType_KV_DEX_MEM_ORDER,
IdentifierTemplate: hex.EncodeToString(dextypes.MemOrderPrefix(contractAddr)),
},

{
AccessType: sdkacltypes.AccessType_READ,
ResourceType: sdkacltypes.ResourceType_KV_DEX_MEM_DOWNSTREAM_CONTRACTS,
IdentifierTemplate: hex.EncodeToString(dextypes.MemDownstreamContractsKey(contractAddr)),
},
{
AccessType: sdkacltypes.AccessType_WRITE,
ResourceType: sdkacltypes.ResourceType_KV_DEX_MEM_DOWNSTREAM_CONTRACTS,
IdentifierTemplate: hex.EncodeToString(dextypes.MemDownstreamContractsKey(contractAddr)),
},
{
AccessType: sdkacltypes.AccessType_WRITE,
ResourceType: sdkacltypes.ResourceType_KV_DEX_MEM_CONTRACTS_TO_PROCESS,
IdentifierTemplate: hex.EncodeToString(dextypes.MemContractsToProcessKey(contractAddr)),
},

// Checks balance of sender
{
AccessType: sdkacltypes.AccessType_READ,
ResourceType: sdkacltypes.ResourceType_KV_BANK_BALANCES,
IdentifierTemplate: senderBankAddrIdentifier,
},
// Reduce the amount from the sender's balance
{
AccessType: sdkacltypes.AccessType_WRITE,
ResourceType: sdkacltypes.ResourceType_KV_BANK_BALANCES,
IdentifierTemplate: senderBankAddrIdentifier,
},

// update the amounts for dex module balance
{
AccessType: sdkacltypes.AccessType_READ,
ResourceType: sdkacltypes.ResourceType_KV_AUTH_ADDRESS_STORE,
IdentifierTemplate: hex.EncodeToString(authtypes.AddressStoreKey(moduleAdr)),
},
{
AccessType: sdkacltypes.AccessType_READ,
ResourceType: sdkacltypes.ResourceType_KV_BANK_BALANCES,
IdentifierTemplate: hex.EncodeToString(banktypes.CreateAccountBalancesPrefix(moduleAdr)),
},
{
AccessType: sdkacltypes.AccessType_WRITE,
ResourceType: sdkacltypes.ResourceType_KV_BANK_BALANCES,
IdentifierTemplate: hex.EncodeToString(banktypes.CreateAccountBalancesPrefix(moduleAdr)),
},

// Gets Account Info for the sender
{
AccessType: sdkacltypes.AccessType_READ,
ResourceType: sdkacltypes.ResourceType_KV_AUTH_ADDRESS_STORE,
IdentifierTemplate: hex.EncodeToString(authtypes.CreateAddressStoreKeyFromBech32(placeOrdersMsg.Creator)),
},

{
AccessType: sdkacltypes.AccessType_READ,
ResourceType: sdkacltypes.ResourceType_KV_DEX_CONTRACT,
IdentifierTemplate: hex.EncodeToString([]byte(dexkeeper.ContractPrefixKey)),
},

{
AccessType: sdkacltypes.AccessType_READ,
ResourceType: sdkacltypes.ResourceType_KV_DEX_LONG_ORDER_COUNT,
IdentifierTemplate: hex.EncodeToString([]byte(dextypes.LongOrderCountKey)),
},

{
AccessType: sdkacltypes.AccessType_READ,
ResourceType: sdkacltypes.ResourceType_KV_DEX_SHORT_ORDER_COUNT,
IdentifierTemplate: hex.EncodeToString([]byte(dextypes.ShortOrderCountKey)),
},
}

// Last Operation should always be a commit
aclOps = append(aclOps, *acltypes.CommitAccessOp())
return aclOps, nil
}

func DexCancelOrdersDependencyGenerator(_ aclkeeper.Keeper, _ sdk.Context, msg sdk.Msg) ([]sdkacltypes.AccessOperation, error) {
cancelOrdersMsg, ok := msg.(*dextypes.MsgCancelOrders)
if !ok {
return []sdkacltypes.AccessOperation{}, ErrPlaceOrdersGenerator
}
contractAddr := cancelOrdersMsg.ContractAddr

aclOps := []sdkacltypes.AccessOperation{
{
AccessType: sdkacltypes.AccessType_READ,
ResourceType: sdkacltypes.ResourceType_KV_DEX_MEM_CANCEL,
IdentifierTemplate: hex.EncodeToString(dextypes.MemCancelPrefix(contractAddr)),
},
{
AccessType: sdkacltypes.AccessType_WRITE,
ResourceType: sdkacltypes.ResourceType_KV_DEX_MEM_CANCEL,
IdentifierTemplate: hex.EncodeToString(dextypes.MemCancelPrefix(contractAddr)),
},
{
AccessType: sdkacltypes.AccessType_READ,
ResourceType: sdkacltypes.ResourceType_KV_DEX_CONTRACT,
IdentifierTemplate: hex.EncodeToString([]byte(dexkeeper.ContractPrefixKey)),
},
{
AccessType: sdkacltypes.AccessType_READ,
ResourceType: sdkacltypes.ResourceType_KV_DEX_MEM_DOWNSTREAM_CONTRACTS,
IdentifierTemplate: hex.EncodeToString(dextypes.MemDownstreamContractsKey(contractAddr)),
},
{
AccessType: sdkacltypes.AccessType_WRITE,
ResourceType: sdkacltypes.ResourceType_KV_DEX_MEM_DOWNSTREAM_CONTRACTS,
IdentifierTemplate: hex.EncodeToString(dextypes.MemDownstreamContractsKey(contractAddr)),
},
{
AccessType: sdkacltypes.AccessType_WRITE,
ResourceType: sdkacltypes.ResourceType_KV_DEX_MEM_CONTRACTS_TO_PROCESS,
IdentifierTemplate: hex.EncodeToString(dextypes.MemContractsToProcessKey(contractAddr)),
},
}

for _, order := range cancelOrdersMsg.GetCancellations() {
priceDenom := order.GetPriceDenom()
assetDenom := order.GetAssetDenom()
aclOps = append(aclOps, GetLongShortOrderBookOps(contractAddr, priceDenom, assetDenom)...)
}

// Last Operation should always be a commit
aclOps = append(aclOps, *acltypes.CommitAccessOp())
return aclOps, nil
}
Loading

0 comments on commit d502396

Please sign in to comment.