Skip to content

Commit

Permalink
[1658]: Add a last_order_id field to the genesis state.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpicyLemon committed Oct 11, 2023
1 parent 5b5e61e commit d45ba46
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 9 deletions.
1 change: 1 addition & 0 deletions docs/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1891,6 +1891,7 @@ GenesisState is the data that should be loaded into the exchange module during g
| `markets` | [Market](#provenance.exchange.v1.Market) | repeated | markets are all of the markets to create at genesis. |
| `orders` | [Order](#provenance.exchange.v1.Order) | repeated | orders are all the orders to create at genesis. |
| `last_market_id` | [uint32](#uint32) | | last_market_id is the value of the last auto-selected market id. |
| `last_order_id` | [uint64](#uint64) | | last_order_id is the value of the last order id created. |



Expand Down
3 changes: 3 additions & 0 deletions proto/provenance/exchange/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ message GenesisState {

// last_market_id is the value of the last auto-selected market id.
uint32 last_market_id = 4;

// last_order_id is the value of the last order id created.
uint64 last_order_id = 5;
}
10 changes: 10 additions & 0 deletions x/exchange/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (g GenesisState) Validate() error {
}
}

maxOrderID := uint64(0)
orderIDs := make(map[uint64]int)
for i, order := range g.Orders {
if order.OrderId != 0 {
Expand All @@ -58,6 +59,15 @@ func (g GenesisState) Validate() error {
if !knownMarket {
errs = append(errs, fmt.Errorf("invalid order[%d]: unknown market id %d", i, order.GetMarketID()))
}

if order.OrderId > maxOrderID {
maxOrderID = order.OrderId
}
}

if g.LastOrderId < maxOrderID {
errs = append(errs, fmt.Errorf("last order id %d is less than the largest id in the provided orders %d",
g.LastOrderId, maxOrderID))
}

// No validation to do on LastMarketId.
Expand Down
47 changes: 39 additions & 8 deletions x/exchange/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions x/exchange/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func TestGenesisState_Validate(t *testing.T) {
bidOrder(8, 3, "5wong", "50nibbler"),
bidOrder(9, 3, "5wong", "50nibbler"),
},
LastOrderId: 9,
},
expErr: nil,
},
Expand Down Expand Up @@ -177,6 +178,7 @@ func TestGenesisState_Validate(t *testing.T) {
bidOrder(2, 2, "28fry", "2bender"),
askOrder(3, 3, "28fry", "2bender"),
},
LastOrderId: 3,
},
expErr: []string{
`invalid order[0]: unknown market id 1`,
Expand All @@ -193,6 +195,7 @@ func TestGenesisState_Validate(t *testing.T) {
bidOrder(1, 5, "28fry", "2bender"),
askOrder(1, 6, "28fry", "2bender"),
},
LastOrderId: 1,
},
expErr: []string{
`invalid order[1]: duplicate order id 1 seen at [0]`,
Expand All @@ -213,18 +216,28 @@ func TestGenesisState_Validate(t *testing.T) {
bidOrder(2, 4, "28fry", "2bender"),
askOrder(3, 3, "28fry", "2bender"),
},
LastOrderId: 1,
},
expErr: []string{
"invalid params: default split 10001 cannot be greater than 10000",
`invalid market[1]: invalid create-bid flat fee option "-1zapp": negative coin amount: -1`,
`invalid order[1]: unknown market id 4`,
"last order id 1 is less than the largest id in the provided orders 3",
},
},
{
name: "last market id 1",
genState: GenesisState{LastMarketId: 1},
expErr: nil,
},
{
name: "last market id less than largest market id",
genState: GenesisState{
Markets: []Market{{MarketId: 3}, {MarketId: 1}},
LastMarketId: 1,
},
expErr: nil,
},
{
name: "last market id 256",
genState: GenesisState{LastMarketId: 256},
Expand All @@ -245,6 +258,48 @@ func TestGenesisState_Validate(t *testing.T) {
genState: GenesisState{LastMarketId: 4_294_967_295},
expErr: nil,
},
{
name: "last order id less than largest order id",
genState: GenesisState{
Markets: []Market{{MarketId: 1}},
Orders: []Order{
askOrder(1, 1, "28fry", "2bender"),
bidOrder(88, 1, "28fry", "2bender"),
bidOrder(2, 1, "28fry", "2bender"),
askOrder(3, 1, "28fry", "2bender"),
},
LastOrderId: 87,
},
expErr: []string{"last order id 87 is less than the largest id in the provided orders 88"},
},
{
name: "last order id equals largest order id",
genState: GenesisState{
Markets: []Market{{MarketId: 1}},
Orders: []Order{
askOrder(1, 1, "28fry", "2bender"),
bidOrder(88, 1, "28fry", "2bender"),
bidOrder(2, 1, "28fry", "2bender"),
askOrder(3, 1, "28fry", "2bender"),
},
LastOrderId: 88,
},
expErr: nil,
},
{
name: "last order id more than largest order id",
genState: GenesisState{
Markets: []Market{{MarketId: 1}},
Orders: []Order{
askOrder(1, 1, "28fry", "2bender"),
bidOrder(88, 1, "28fry", "2bender"),
bidOrder(2, 1, "28fry", "2bender"),
askOrder(3, 1, "28fry", "2bender"),
},
LastOrderId: 89,
},
expErr: nil,
},
}

for _, tc := range tests {
Expand Down
6 changes: 5 additions & 1 deletion x/exchange/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState *exchange.GenesisState) {
amounts[addr] = amounts[addr].Add(order.GetHoldAmount()...)

Check warning on line 38 in x/exchange/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/exchange/keeper/genesis.go#L33-L38

Added lines #L33 - L38 were not covered by tests
}

setLastOrderID(store, genState.LastOrderId)

// Make sure all the needed funds have holds on them. These should have been placed during initialization of the hold module.
for _, addr := range addrs {
for _, reqAmt := range amounts[addr] {
Expand All @@ -54,9 +56,11 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState *exchange.GenesisState) {

// ExportGenesis creates a genesis state from the current state store.
func (k Keeper) ExportGenesis(ctx sdk.Context) *exchange.GenesisState {
store := k.getStore(ctx)
genState := &exchange.GenesisState{
Params: k.GetParams(ctx),
LastMarketId: getLastAutoMarketID(k.getStore(ctx)),
LastMarketId: getLastAutoMarketID(store),
LastOrderId: getLastOrderID(store),
}

k.IterateMarkets(ctx, func(market *exchange.Market) bool {
Expand Down

0 comments on commit d45ba46

Please sign in to comment.