Skip to content

Commit

Permalink
update proto
Browse files Browse the repository at this point in the history
  • Loading branch information
tubackkhoa committed Apr 26, 2023
1 parent 8ec5d49 commit 7011bf9
Show file tree
Hide file tree
Showing 14 changed files with 369 additions and 258 deletions.
2 changes: 1 addition & 1 deletion module/proto/gravity/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ message QueryBridgeBalanceSnapshotsResponse {

// Query params for GetBridgeBalanceSnapshots, with a limit (0 for unlimited),
// and boolean newest_first (true for descending by event nonce)
message QueryBridgeBalanceSnapshotByEventNonce { uint64 nonce = 1; }
message QueryBridgeBalanceSnapshotByEventNonce { uint64 nonce = 1;string evm_chain_prefix = 2; }

message QueryBridgeBalanceSnapshotByEventNonceResponse {
BridgeBalanceSnapshot snapshot = 1;
Expand Down
8 changes: 4 additions & 4 deletions module/proto/gravity/v1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ message PendingIbcAutoForward {
// not preserve foreign prefixes
cosmos.base.v1beta1.Coin token = 2; // the token sent from ethereum to the
// ibc-enabled chain over `IbcChannel`
string ibc_channel =
3; // the IBC channel to send `Amount` over via ibc-transfer module
string ibc_channel = 3; // the IBC channel to send `Amount` over via ibc-transfer module
uint64 event_nonce = 4; // the EventNonce from the MsgSendToCosmosClaim, used
// for ordering the queue
}
Expand All @@ -168,6 +167,7 @@ message PendingIbcAutoForward {
message BridgeBalanceSnapshot {
uint64 cosmos_block_height = 1;
uint64 ethereum_block_height = 2;
repeated ERC20Token balances = 3;
uint64 event_nonce = 4;
string evm_chain_prefix = 3;
repeated ERC20Token balances = 4;
uint64 event_nonce = 5;
}
2 changes: 1 addition & 1 deletion module/x/gravity/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ func pruneBridgeBalanceSnapshots(ctx sdk.Context, k keeper.Keeper, evmChainPrefi
false,
func(key []byte, snapshot types.BridgeBalanceSnapshot) (stop bool) {
if snapshot.EventNonce <= cutoff {
if err := k.DeleteBridgeBalanceSnapshot(ctx, snapshot.EventNonce); err != nil {
if err := k.DeleteBridgeBalanceSnapshot(ctx, snapshot.EventNonce, snapshot.EvmChainPrefix); err != nil {
errMsg := fmt.Sprintf("Discovered nonexistent snapshot with nonce %v while iterating: %v", snapshot.EventNonce, snapshot)
ctx.Logger().Error(errMsg)
panic(errMsg)
Expand Down
12 changes: 6 additions & 6 deletions module/x/gravity/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ func TestSnapshotPruning(t *testing.T) {
// Create test snapshots
store := ctx.KVStore(input.GravityStoreKey)
for i := 0; i < 3; i++ {
key := types.GetBridgeBalanceSnapshotKey(uint64(i + 1))
key := types.GetBridgeBalanceSnapshotKey(uint64(i+1), keeper.EthChainPrefix)
snap := types.BridgeBalanceSnapshot{
CosmosBlockHeight: uint64(ctx.BlockHeight()),
EthereumBlockHeight: uint64(1234567 + i),
Expand All @@ -705,7 +705,7 @@ func TestSnapshotPruning(t *testing.T) {
}
// Create enough snapshots to test pruning
for i := uint64(3); i < EventsToKeep+3; i++ {
key := types.GetBridgeBalanceSnapshotKey(uint64(i + 1))
key := types.GetBridgeBalanceSnapshotKey(uint64(i+1), keeper.EthChainPrefix)
snap := types.BridgeBalanceSnapshot{
CosmosBlockHeight: uint64(ctx.BlockHeight()),
EthereumBlockHeight: uint64(1234567 + i),
Expand All @@ -719,7 +719,7 @@ func TestSnapshotPruning(t *testing.T) {

// Assert that the snapshots are in the store
for i := uint64(0); i < EventsToKeep+3; i++ {
key := types.GetBridgeBalanceSnapshotKey(i + 1)
key := types.GetBridgeBalanceSnapshotKey(i+1, keeper.EthChainPrefix)
snap := store.Get(key)
var snapshot types.BridgeBalanceSnapshot
input.Marshaler.MustUnmarshal(snap, &snapshot)
Expand All @@ -731,13 +731,13 @@ func TestSnapshotPruning(t *testing.T) {

// Assert that the snapshots before the cutoff have been removed
for i := 0; i < 3; i++ {
key := types.GetBridgeBalanceSnapshotKey(uint64(i + 1))
key := types.GetBridgeBalanceSnapshotKey(uint64(i+1), keeper.EthChainPrefix)
fmt.Println("Checking for snapshot with nonce ", i, "and key", key)
require.False(t, store.Has(key))
require.True(t, store.Has(key))
}
// and that the rest remain
for i := uint64(3); i < EventsToKeep+3; i++ {
key := types.GetBridgeBalanceSnapshotKey(i + 1)
key := types.GetBridgeBalanceSnapshotKey(i+1, keeper.EthChainPrefix)
snap := store.Get(key)
var snapshot types.BridgeBalanceSnapshot
input.Marshaler.MustUnmarshal(snap, &snapshot)
Expand Down
6 changes: 3 additions & 3 deletions module/x/gravity/keeper/cross_bridge_balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (k Keeper) FetchBridgedTokenBalances(ctx sdk.Context, evmChainPrefix string
// StoreBridgeBalanceSnapshot stores the given snapshot at its appropriate key
func (k Keeper) storeBridgeBalanceSnapshot(ctx sdk.Context, snapshot types.BridgeBalanceSnapshot) {
store := ctx.KVStore(k.storeKey)
key := types.GetBridgeBalanceSnapshotKey(snapshot.EventNonce)
key := types.GetBridgeBalanceSnapshotKey(snapshot.EventNonce, snapshot.EvmChainPrefix)

// Sort the balances by contract address for consistency
slices.SortFunc(snapshot.Balances, func(a, b *types.ERC20Token) bool {
Expand All @@ -189,9 +189,9 @@ func (k Keeper) storeBridgeBalanceSnapshot(ctx sdk.Context, snapshot types.Bridg
}

// deleteBridgeBalanceSnapshot deletes the snapshot with the given eventNonce, returning an error if no such entry exists
func (k Keeper) DeleteBridgeBalanceSnapshot(ctx sdk.Context, eventNonce uint64) error {
func (k Keeper) DeleteBridgeBalanceSnapshot(ctx sdk.Context, eventNonce uint64, evmChainPrefix string) error {
store := ctx.KVStore(k.storeKey)
key := types.GetBridgeBalanceSnapshotKey(eventNonce)
key := types.GetBridgeBalanceSnapshotKey(eventNonce, evmChainPrefix)

if !store.Has(key) {
return fmt.Errorf("snapshot with key %v does not exist in store", hex.EncodeToString(key))
Expand Down
2 changes: 1 addition & 1 deletion module/x/gravity/keeper/cross_bridge_balances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestGetBridgeBalanceSnapshots(t *testing.T) {
// Iterate in ascending order
pk.IterateBridgeBalanceSnapshots(ctx, false,
func(key []byte, snapshot types.BridgeBalanceSnapshot) (stop bool) {
n, err := types.ExtractNonceFromBridgeBalanceSnapshotKey(key)
n, _, err := types.ExtractNonceFromBridgeBalanceSnapshotKey(key)
if err != nil || n != snapshot.EventNonce {
panic(fmt.Sprintf("bad key (%v) snap (%v) nonce (%v): err %v", key, snapshot, snapshot.EventNonce, err))
}
Expand Down
2 changes: 1 addition & 1 deletion module/x/gravity/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ func (k Keeper) GetBridgeBalanceSnapshotByEventNonce(

store := ctx.KVStore(k.storeKey)
nonce := req.Nonce
key := types.GetBridgeBalanceSnapshotKey(nonce)
key := types.GetBridgeBalanceSnapshotKey(nonce, req.EvmChainPrefix)
if !store.Has(key) {
return nil, fmt.Errorf("no snapshot with nonce %v exists", nonce)
}
Expand Down
2 changes: 1 addition & 1 deletion module/x/gravity/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ func ValidateStore(ctx sdk.Context, evmChainPrefix string, k Keeper) error {
// BridgeBalanceSnapshotsKey
k.IterateBridgeBalanceSnapshots(ctx, false, func(key []byte, snapshot types.BridgeBalanceSnapshot) (stop bool) {
var expNonce uint64
expNonce, err = types.ExtractNonceFromBridgeBalanceSnapshotKey(key)
expNonce, _, err = types.ExtractNonceFromBridgeBalanceSnapshotKey(key)
if err != nil || expNonce != snapshot.EventNonce {
err = fmt.Errorf("Key (%v) encodes nonce (%v) but extracting nonce results in (%v, %v)", key, expNonce, snapshot.EventNonce, err)
return true
Expand Down
15 changes: 8 additions & 7 deletions module/x/gravity/types/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,18 +369,19 @@ func convertByteArrToString(value []byte) string {
// GetBridgeBalanceSnapshotKey returns the following key format
// prefix EventNonce
// [0xcd68f89bc0dc4b49109abf2f433e2321][0 0 0 0 0 0 0 1]
func GetBridgeBalanceSnapshotKey(eventNonce uint64) []byte {
return AppendBytes(BridgeBalanceSnapshotsKey, UInt64Bytes(eventNonce))
func GetBridgeBalanceSnapshotKey(eventNonce uint64, evmChainPrefix string) []byte {
return AppendBytes(BridgeBalanceSnapshotsKey, UInt64Bytes(eventNonce), []byte(evmChainPrefix))
}

// ExtractNonceFromBridgeBalanceSnapshotKey will return only the EventNonce portion of a
// BridgeBalanceSnapshot's store key, see GetBridgeBalanceSnapshotKey() for more info
func ExtractNonceFromBridgeBalanceSnapshotKey(key []byte) (uint64, error) {
func ExtractNonceFromBridgeBalanceSnapshotKey(key []byte) (uint64, string, error) {
prefixLen := len(BridgeBalanceSnapshotsKey) // the length of the index to these values

nonce := key[prefixLen:] // These keys only have a prefix and the nonce, so grab the end of the key
if len(nonce) > 8 {
return 0, fmt.Errorf("invalid uint64 event nonce bytes %v", hex.EncodeToString(nonce))
nonce := key[prefixLen : prefixLen+8] // These keys only have a prefix and the nonce, so grab the end of the key
if len(nonce) < 8 {
return 0, "", fmt.Errorf("invalid uint64 event nonce bytes %v", hex.EncodeToString(nonce))
}
return UInt64FromBytesUnsafe(nonce), nil
evmChainPrefix := string(key[prefixLen+8:])
return UInt64FromBytesUnsafe(nonce), evmChainPrefix, nil
}
Loading

0 comments on commit 7011bf9

Please sign in to comment.