Skip to content

Commit

Permalink
Use autoincrementing index
Browse files Browse the repository at this point in the history
  • Loading branch information
gartnera committed Feb 19, 2025
1 parent 3bf2633 commit e932bca
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
58 changes: 43 additions & 15 deletions x/crosschain/keeper/cctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,43 @@ func (k Keeper) setNonceToCCTX(
}
}

const (
TSIndexKey = "cctx-ts-"
)
// GetCctxCounter retrieves the current counter value
func (k Keeper) GetCctxCounter(ctx sdk.Context) uint64 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.CounterValueKey))
storedCounter := store.Get([]byte(types.CounterValueKey))

return sdk.BigEndianToUint64(storedCounter)
}

// getNextCctxCounter retrieves and increments the counter for ordering
func (k Keeper) getNextCctxCounter(ctx sdk.Context) uint64 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.CounterValueKey))
storedCounter := k.GetCctxCounter(ctx)

nextCounter := storedCounter + 1

store.Set([]byte(types.CounterValueKey), sdk.Uint64ToBigEndian(nextCounter))
return nextCounter
}

// setCctxCounterIndex sets the CCTX in the counter index
//
// note that we use the raw bytes in the index rather than the hex encoded bytes
// like in the main store
func (k Keeper) setCctxCounterIndex(ctx sdk.Context, cctx types.CrossChainTx) {
counterIndexStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.CounterIndexKey))
nextCounter := k.getNextCctxCounter(ctx)

cctxIndex, err := cctx.GetCCTXIndexBytes()
if err != nil {
k.Logger(ctx).Error("get cctx index bytes", "err", err)
return
}

// must use big endian so most significant bytes are first for sortability
nextCounterBytes := sdk.Uint64ToBigEndian(nextCounter)
counterIndexStore.Set(nextCounterBytes, cctxIndex[:])
}

// SetCrossChainTx set a specific cctx in the store from its index
func (k Keeper) SetCrossChainTx(ctx sdk.Context, cctx types.CrossChainTx) {
Expand All @@ -107,20 +141,14 @@ func (k Keeper) SetCrossChainTx(ctx sdk.Context, cctx types.CrossChainTx) {
p := types.KeyPrefix(fmt.Sprintf("%s", types.CCTXKey))
store := prefix.NewStore(ctx.KVStore(k.storeKey), p)
b := k.cdc.MustMarshal(&cctx)
cctxIndexPrefix := types.KeyPrefix(cctx.Index)
store.Set(cctxIndexPrefix, b)
cctxIndex := types.KeyPrefix(cctx.Index)

tsIndexPrefix := types.KeyPrefix(TSIndexKey)
tsIndexStore := prefix.NewStore(ctx.KVStore(k.storeKey), tsIndexPrefix)
cctxIndex, err := cctx.GetCCTXIndexBytes()
if err != nil {
k.Logger(ctx).Error("unable to GetCCTXIndexBytes", "err", err)
return
isUpdate := store.Has(cctxIndex)
store.Set(cctxIndex, b)

if !isUpdate {
k.setCctxCounterIndex(ctx, cctx)
}
// must use big endian so most significant bytes are first for sortability
createdAtBytes := sdk.Uint64ToBigEndian(uint64(cctx.CctxStatus.CreatedTimestamp))
indexKey := append(createdAtBytes, cctxIndex[:]...)
tsIndexStore.Set(indexKey, cctxIndexPrefix)
}

// GetCrossChainTx returns a cctx from its index
Expand Down
9 changes: 6 additions & 3 deletions x/crosschain/keeper/grpc_query_cctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (k Keeper) CctxAll(c context.Context, req *types.QueryAllCctxRequest) (*typ

store := ctx.KVStore(k.storeKey)
cctxStore := prefix.NewStore(store, types.KeyPrefix(types.CCTXKey))
tsStore := prefix.NewStore(store, types.KeyPrefix(TSIndexKey))
counterStore := prefix.NewStore(store, types.KeyPrefix(types.CounterIndexKey))

if req.Pagination == nil {
req.Pagination = &query.PageRequest{}
Expand All @@ -56,9 +56,12 @@ func (k Keeper) CctxAll(c context.Context, req *types.QueryAllCctxRequest) (*typ
}

var sends []*types.CrossChainTx
pageRes, err := query.Paginate(tsStore, req.Pagination, func(_ []byte, value []byte) error {
pageRes, err := query.Paginate(counterStore, req.Pagination, func(_ []byte, value []byte) error {
var indexBytes [32]byte
copy(indexBytes[:], value[:32])
cctxIndex := types.GetCctxIndexFromBytes(indexBytes)
var cctx types.CrossChainTx
cctxBytes := cctxStore.Get(value)
cctxBytes := cctxStore.Get(types.KeyPrefix(cctxIndex))
if err := k.cdc.Unmarshal(cctxBytes, &cctx); err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions x/crosschain/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ const (
// NOTE: Send is the previous name of CCTX and is kept for backward compatibility
CCTXKey = "Send-value-"

// CounterValueKey is a static key for storing the cctx counter key for ordering
CounterValueKey = "ctr-value"

// CounterIndexKey is is the prefix to use for the counter index
CounterIndexKey = "ctr-idx-"

LastBlockHeightKey = "LastBlockHeight-value-"
FinalizedInboundsKey = "FinalizedInbounds-value-"

Expand Down

0 comments on commit e932bca

Please sign in to comment.