Skip to content

Commit

Permalink
add autoincrement ids to genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
kstoykov committed Nov 9, 2021
1 parent d481c8a commit 924242c
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 136 deletions.
26 changes: 14 additions & 12 deletions module/proto/gravity/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,18 @@ message Params {

// GenesisState struct
message GenesisState {
Params params = 1;
uint64 last_observed_nonce = 2;
repeated Valset valsets = 3;
repeated MsgValsetConfirm valset_confirms = 4;
repeated OutgoingTxBatch batches = 5;
repeated MsgConfirmBatch batch_confirms = 6 [(gogoproto.nullable) = false];
repeated OutgoingLogicCall logic_calls = 7;
repeated MsgConfirmLogicCall logic_call_confirms = 8 [(gogoproto.nullable) = false];
repeated Attestation attestations = 9 [(gogoproto.nullable) = false];
repeated MsgSetOrchestratorAddress delegate_keys = 10;
repeated ERC20ToDenom erc20_to_denoms = 11;
repeated OutgoingTransferTx unbatched_transfers = 12;
Params params = 1;
uint64 last_observed_nonce = 2;
repeated Valset valsets = 3;
repeated MsgValsetConfirm valset_confirms = 4;
repeated OutgoingTxBatch batches = 5;
repeated MsgConfirmBatch batch_confirms = 6 [(gogoproto.nullable) = false];
repeated OutgoingLogicCall logic_calls = 7;
repeated MsgConfirmLogicCall logic_call_confirms = 8 [(gogoproto.nullable) = false];
repeated Attestation attestations = 9 [(gogoproto.nullable) = false];
repeated MsgSetOrchestratorAddress delegate_keys = 10;
repeated ERC20ToDenom erc20_to_denoms = 11;
repeated OutgoingTransferTx unbatched_transfers = 12;
uint64 last_tx_pool_id = 13;
uint64 last_outgoing_batch_id = 14;
}
80 changes: 40 additions & 40 deletions module/x/gravity/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import (

// InitGenesis starts a chain from a genesis state
func InitGenesis(ctx sdk.Context, k Keeper, data types.GenesisState) {
var lastTxPoolId uint64 = 0
var lastBatchNonce uint64 = 0

k.SetParams(ctx, *data.Params)
// reset valsets in state
for _, vs := range data.Valsets {
Expand All @@ -27,19 +24,12 @@ func InitGenesis(ctx sdk.Context, k Keeper, data types.GenesisState) {
for _, batch := range data.Batches {
// TODO: block height?
k.StoreBatchUnsafe(ctx, batch)
if batch.BatchNonce > lastBatchNonce {
lastBatchNonce = batch.BatchNonce
}
for _, tx := range batch.Transactions {
if err := k.setPoolEntry(ctx, tx); err != nil {
panic(err)
}
if tx.Id > lastTxPoolId {
lastTxPoolId = tx.Id
}
}
}
k.setIncrementID(ctx, types.KeyLastOutgoingBatchID, lastBatchNonce+1)

// reset batch confirmations in state
for _, conf := range data.BatchConfirms {
Expand All @@ -64,13 +54,8 @@ func InitGenesis(ctx sdk.Context, k Keeper, data types.GenesisState) {
panic(err)
}
k.appendToUnbatchedTXIndex(ctx, tx.Erc20Fee.Contract, *tx.Erc20Fee, tx.Id)
if tx.Id > lastTxPoolId {
lastTxPoolId = tx.Id
}
}

k.setIncrementID(ctx, types.KeyLastTXPoolID, lastTxPoolId+1)

// reset attestations in state
for _, att := range data.Attestations {
att := att
Expand Down Expand Up @@ -151,25 +136,38 @@ func InitGenesis(ctx sdk.Context, k Keeper, data types.GenesisState) {
}
}

if data.LastTxPoolId == 0 {
k.setIncrementID(ctx, types.KeyLastTXPoolID, 1)
} else {
k.setIncrementID(ctx, types.KeyLastTXPoolID, data.LastTxPoolId)
}

if data.LastOutgoingBatchId == 0 {
k.setIncrementID(ctx, types.KeyLastOutgoingBatchID, 1)
} else {
k.setIncrementID(ctx, types.KeyLastOutgoingBatchID, data.LastOutgoingBatchId)
}
}

// ExportGenesis exports all the state needed to restart the chain
// from the current state of the chain
func ExportGenesis(ctx sdk.Context, k Keeper) types.GenesisState {
var (
p = k.GetParams(ctx)
calls = k.GetOutgoingLogicCalls(ctx)
batches = k.GetOutgoingTxBatches(ctx)
valsets = k.GetValsets(ctx)
attmap = k.GetAttestationMapping(ctx)
vsconfs = []*types.MsgValsetConfirm{}
batchconfs = []types.MsgConfirmBatch{}
callconfs = []types.MsgConfirmLogicCall{}
attestations = []types.Attestation{}
delegates = k.GetDelegateKeys(ctx)
lastobserved = k.GetLastObservedEventNonce(ctx)
erc20ToDenoms = []*types.ERC20ToDenom{}
unbatchedTransfers = k.GetPoolTransactions(ctx)
p = k.GetParams(ctx)
calls = k.GetOutgoingLogicCalls(ctx)
batches = k.GetOutgoingTxBatches(ctx)
valsets = k.GetValsets(ctx)
attmap = k.GetAttestationMapping(ctx)
vsconfs = []*types.MsgValsetConfirm{}
batchconfs = []types.MsgConfirmBatch{}
callconfs = []types.MsgConfirmLogicCall{}
attestations = []types.Attestation{}
delegates = k.GetDelegateKeys(ctx)
lastobserved = k.GetLastObservedEventNonce(ctx)
erc20ToDenoms = []*types.ERC20ToDenom{}
unbatchedTransfers = k.GetPoolTransactions(ctx)
lastTxPoolId = k.GetIncrementID(ctx, types.KeyLastTXPoolID)
lastOutgoingBatchID = k.GetIncrementID(ctx, types.KeyLastOutgoingBatchID)
)

// export valset confirmations from state
Expand Down Expand Up @@ -205,17 +203,19 @@ func ExportGenesis(ctx sdk.Context, k Keeper) types.GenesisState {
})

return types.GenesisState{
Params: &p,
LastObservedNonce: lastobserved,
Valsets: valsets,
ValsetConfirms: vsconfs,
Batches: batches,
BatchConfirms: batchconfs,
LogicCalls: calls,
LogicCallConfirms: callconfs,
Attestations: attestations,
DelegateKeys: delegates,
Erc20ToDenoms: erc20ToDenoms,
UnbatchedTransfers: unbatchedTransfers,
Params: &p,
LastObservedNonce: lastobserved,
Valsets: valsets,
ValsetConfirms: vsconfs,
Batches: batches,
BatchConfirms: batchconfs,
LogicCalls: calls,
LogicCallConfirms: callconfs,
Attestations: attestations,
DelegateKeys: delegates,
Erc20ToDenoms: erc20ToDenoms,
UnbatchedTransfers: unbatchedTransfers,
LastTxPoolId: lastTxPoolId,
LastOutgoingBatchId: lastOutgoingBatchID,
}
}
29 changes: 21 additions & 8 deletions module/x/gravity/keeper/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,14 +371,17 @@ func (k Keeper) createBatchFees(ctx sdk.Context, maxElements uint) map[string]*t
}

func (k Keeper) autoIncrementID(ctx sdk.Context, idKey []byte) uint64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get(idKey)
var id uint64 = 1
if bz != nil {
id = binary.BigEndian.Uint64(bz)
}
bz = sdk.Uint64ToBigEndian(id + 1)
store.Set(idKey, bz)
// store := ctx.KVStore(k.storeKey)
// bz := store.Get(idKey)
// var id uint64 = 1
// if bz != nil {
// id = binary.BigEndian.Uint64(bz)
// }
// bz = sdk.Uint64ToBigEndian(id + 1)
// store.Set(idKey, bz)
// return id
var id uint64 = k.GetIncrementID(ctx, idKey)
k.setIncrementID(ctx, idKey, id+1)
return id
}

Expand All @@ -388,3 +391,13 @@ func (k Keeper) setIncrementID(ctx sdk.Context, idKey []byte, id uint64) uint64
store.Set(idKey, bz)
return id
}

func (k Keeper) GetIncrementID(ctx sdk.Context, idKey []byte) uint64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get(idKey)
var id uint64 = 1
if bz != nil {
id = binary.BigEndian.Uint64(bz)
}
return id
}
Loading

0 comments on commit 924242c

Please sign in to comment.