Skip to content

Commit

Permalink
Order contract call signing
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrit committed Aug 4, 2023
1 parent c2d78eb commit 5ccfc53
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
36 changes: 27 additions & 9 deletions module/x/gravity/keeper/contract_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"bytes"
"encoding/hex"
"sort"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand All @@ -11,30 +12,31 @@ import (

func (k Keeper) GetUnsignedContractCallTxs(ctx sdk.Context, val sdk.ValAddress) []*types.ContractCallTx {
var unconfirmed []*types.ContractCallTx
k.IterateOutgoingTxsByType(ctx, types.ContractCallTxPrefixByte, func(_ []byte, otx types.OutgoingTx) bool {
sig := k.getEthereumSignature(ctx, otx.GetStoreIndex(), val)
k.IterateCompletedOutgoingTxsByType(ctx, types.ContractCallTxPrefixByte, func(_ []byte, cotx types.OutgoingTx) bool {
sig := k.getEthereumSignature(ctx, cotx.GetStoreIndex(), val)
if len(sig) == 0 {
call, ok := otx.(*types.ContractCallTx)
call, ok := cotx.(*types.ContractCallTx)
if !ok {
panic(sdkerrors.Wrapf(types.ErrInvalid, "couldn't cast to contract call for %s", otx))
panic(sdkerrors.Wrapf(types.ErrInvalid, "couldn't cast to contract call for completed tx %s", cotx))
}
unconfirmed = append(unconfirmed, call)
}
return false
})
k.IterateCompletedOutgoingTxsByType(ctx, types.ContractCallTxPrefixByte, func(_ []byte, cotx types.OutgoingTx) bool {
sig := k.getEthereumSignature(ctx, cotx.GetStoreIndex(), val)

k.IterateOutgoingTxsByType(ctx, types.ContractCallTxPrefixByte, func(_ []byte, otx types.OutgoingTx) bool {
sig := k.getEthereumSignature(ctx, otx.GetStoreIndex(), val)
if len(sig) == 0 {
call, ok := cotx.(*types.ContractCallTx)
call, ok := otx.(*types.ContractCallTx)
if !ok {
panic(sdkerrors.Wrapf(types.ErrInvalid, "couldn't cast to contract call for completed tx %s", cotx))
panic(sdkerrors.Wrapf(types.ErrInvalid, "couldn't cast to contract call for %s", otx))
}
unconfirmed = append(unconfirmed, call)
}
return false
})

return unconfirmed
return orderContractCallsByAddressAndNonceAscending(unconfirmed)
}

func (k Keeper) contractCallExecuted(ctx sdk.Context, invalidationScope []byte, invalidationNonce uint64) {
Expand Down Expand Up @@ -64,3 +66,19 @@ func (k Keeper) contractCallExecuted(ctx sdk.Context, invalidationScope []byte,

k.CompleteOutgoingTx(ctx, completedCallTx)
}

// orderContractCallsByAddressAndNonceAscending sorts a slice of contract calls by address and nonce in ascending order
func orderContractCallsByAddressAndNonceAscending(calls []*types.ContractCallTx) []*types.ContractCallTx {
sort.SliceStable(calls, func(i, j int) bool {
// Compare the addresses first
addrComparison := bytes.Compare(calls[i].InvalidationScope, calls[j].InvalidationScope)
if addrComparison != 0 {
return addrComparison < 0
}

// If the addresses are equal, compare the nonces
return calls[i].InvalidationNonce < calls[j].InvalidationNonce
})

return calls
}
13 changes: 12 additions & 1 deletion orchestrator/cosmos_gravity/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::cmp::Ordering;

use deep_space::address::Address;
use ethers::types::Address as EthAddress;
use gravity_proto::gravity::query_client::QueryClient as GravityQueryClient;
Expand Down Expand Up @@ -196,7 +198,16 @@ pub async fn get_oldest_unsigned_logic_call(
address: address.to_string(),
})
.await?;
let calls = request.into_inner().calls;
let mut calls = request.into_inner().calls;
// sort by scope then nonce ascending
calls.sort_by(|a, b| {
let scope_cmp = a.invalidation_scope.cmp(&b.invalidation_scope);
if scope_cmp == Ordering::Equal {
a.invalidation_nonce.cmp(&b.invalidation_nonce)
} else {
scope_cmp
}
});
let mut out = Vec::new();
for call in calls {
out.push(LogicCall::from_proto(call)?)
Expand Down

0 comments on commit 5ccfc53

Please sign in to comment.