Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(crosschain): store priorityFee (GasTipCap) for EVM chains #2483

Merged
merged 23 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions proto/zetachain/zetacore/crosschain/gas_price.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ message GasPrice {
int64 chain_id = 3;
repeated string signers = 4;
repeated uint64 block_nums = 5;

repeated uint64 prices = 6;

// index of the median gas price in the prices array
uint64 median_index = 7;

// priority fees for EIP-1559
repeated uint64 priority_fees = 8;
swift1337 marked this conversation as resolved.
Show resolved Hide resolved
}
7 changes: 6 additions & 1 deletion proto/zetachain/zetacore/crosschain/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,15 @@ message MsgRemoveOutboundTrackerResponse {}

message MsgVoteGasPrice {
string creator = 1;

int64 chain_id = 2;

uint64 price = 3;
uint64 priority_fee = 6;

uint64 block_number = 4;
string supply = 5;

reserved 5; // deprecated `string supply`
}

message MsgVoteGasPriceResponse {}
Expand Down
21 changes: 14 additions & 7 deletions x/crosschain/client/cli/cli_gas_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"strconv"

"cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
Expand Down Expand Up @@ -77,34 +78,40 @@ func CmdShowGasPrice() *cobra.Command {

func CmdVoteGasPrice() *cobra.Command {
cmd := &cobra.Command{
Use: "vote-gas-price [chain] [price] [supply] [blockNumber]",
Use: "vote-gas-price [chain] [price] [priorityFee] [blockNumber]",
Short: "Broadcast message to vote gas price",
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) error {
argsChain, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
return err
return errors.Wrapf(err, "invalid chain id %q", args[0])
}

argsPrice, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return err
return errors.Wrapf(err, "invalid price %q", args[1])
}

argsPriorityFee, err := strconv.ParseUint(args[2], 10, 64)
if err != nil {
return errors.Wrapf(err, "invalid priorityFee %q", args[2])
}
argsSupply := args[2]

argsBlockNumber, err := strconv.ParseUint(args[3], 10, 64)
if err != nil {
return err
return errors.Wrapf(err, "invalid blockNumber %q", args[3])
}

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
return errors.Wrap(err, "failed to get client context")
}

msg := types.NewMsgVoteGasPrice(
clientCtx.GetFromAddress().String(),
argsChain,
argsPrice,
argsSupply,
argsPriorityFee,
argsBlockNumber,
)

Expand Down
16 changes: 10 additions & 6 deletions x/crosschain/keeper/gas_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,33 @@ import (
// SetGasPrice set a specific gasPrice in the store from its index
func (k Keeper) SetGasPrice(ctx sdk.Context, gasPrice types.GasPrice) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.GasPriceKey))
b := k.cdc.MustMarshal(&gasPrice)
gasPrice.Index = strconv.FormatInt(gasPrice.ChainId, 10)
b := k.cdc.MustMarshal(&gasPrice)
store.Set(types.KeyPrefix(gasPrice.Index), b)
}

// GetGasPrice returns a gasPrice from its index
func (k Keeper) GetGasPrice(ctx sdk.Context, chainID int64) (val types.GasPrice, found bool) {
// GetGasPrice returns a gasPrice from its index or false if it doesn't exist.
func (k Keeper) GetGasPrice(ctx sdk.Context, chainID int64) (types.GasPrice, bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.GasPriceKey))

b := store.Get(types.KeyPrefix(strconv.FormatInt(chainID, 10)))
if b == nil {
return val, false
return types.GasPrice{}, false
}

var val types.GasPrice
k.cdc.MustUnmarshal(b, &val)

return val, true
}

func (k Keeper) GetMedianGasPriceInUint(ctx sdk.Context, chainID int64) (sdk.Uint, bool) {
func (k Keeper) GetMedianGasPriceInUint(ctx sdk.Context, chainID int64) (math.Uint, bool) {
gasPrice, isFound := k.GetGasPrice(ctx, chainID)
if !isFound {
return math.ZeroUint(), isFound
}
mi := gasPrice.MedianIndex
return sdk.NewUint(gasPrice.Prices[mi]), true
return math.NewUint(gasPrice.Prices[mi]), true
}

// RemoveGasPrice removes a gasPrice from the store
Expand Down
98 changes: 59 additions & 39 deletions x/crosschain/keeper/msg_server_vote_gas_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"context"
"math/big"
"sort"
"strconv"

cosmoserrors "cosmossdk.io/errors"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pkg/errors"

"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/x/crosschain/types"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
)
Expand All @@ -20,61 +21,80 @@ import (
//
// Only observer validators are authorized to broadcast this message.
func (k msgServer) VoteGasPrice(
goCtx context.Context,
cc context.Context,
msg *types.MsgVoteGasPrice,
) (*types.MsgVoteGasPriceResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
ctx := sdk.UnwrapSDKContext(cc)

chain, found := k.zetaObserverKeeper.GetSupportedChainFromChainID(ctx, msg.ChainId)
if !found {
return nil, cosmoserrors.Wrapf(types.ErrUnsupportedChain, "ChainID: %d ", msg.ChainId)
return nil, cosmoserrors.Wrapf(types.ErrUnsupportedChain, "chain id %d", msg.ChainId)
}

if ok := k.zetaObserverKeeper.IsNonTombstonedObserver(ctx, msg.Creator); !ok {
return nil, observertypes.ErrNotObserver
}

gasPrice, isFound := k.GetGasPrice(ctx, chain.ChainId)
if !isFound {
gasPrice = types.GasPrice{
Creator: msg.Creator,
Index: strconv.FormatInt(chain.ChainId, 10), // TODO : Not needed index set at keeper
ChainId: chain.ChainId,
Prices: []uint64{msg.Price},
BlockNums: []uint64{msg.BlockNumber},
Signers: []string{msg.Creator},
MedianIndex: 0,
}
} else {
signers := gasPrice.Signers
exist := false
for i, s := range signers {
if s == msg.Creator { // update existing entry
gasPrice.BlockNums[i] = msg.BlockNumber
gasPrice.Prices[i] = msg.Price
exist = true
break
}
}
if !exist {
gasPrice.Signers = append(gasPrice.Signers, msg.Creator)
gasPrice.BlockNums = append(gasPrice.BlockNums, msg.BlockNumber)
gasPrice.Prices = append(gasPrice.Prices, msg.Price)
return k.voteGasPrice(ctx, chain, types.GasPrice{
Creator: msg.Creator,
ChainId: chain.ChainId,
Prices: []uint64{msg.Price},
PriorityFees: []uint64{msg.PriorityFee},
BlockNums: []uint64{msg.BlockNumber},
Signers: []string{msg.Creator},
})
}

// Now we either want to update the gas price or add a new entry
var exists bool
for i, s := range gasPrice.Signers {
if s != msg.Creator {
continue
}
// recompute the median gas price
mi := medianOfArray(gasPrice.Prices)
// #nosec G115 always positive
gasPrice.MedianIndex = uint64(mi)

// update existing entry
gasPrice.BlockNums[i] = msg.BlockNumber
gasPrice.Prices[i] = msg.Price
gasPrice.PriorityFees[i] = msg.PriorityFee
exists = true
break
}

if !exists {
gasPrice.Signers = append(gasPrice.Signers, msg.Creator)
gasPrice.BlockNums = append(gasPrice.BlockNums, msg.BlockNumber)
gasPrice.Prices = append(gasPrice.Prices, msg.Price)
gasPrice.PriorityFees = append(gasPrice.PriorityFees, msg.PriorityFee)
}
k.SetGasPrice(ctx, gasPrice)
chainIDBigINT := big.NewInt(chain.ChainId)

gasUsed, err := k.fungibleKeeper.SetGasPrice(
ctx,
chainIDBigINT,
math.NewUint(gasPrice.Prices[gasPrice.MedianIndex]).BigInt(),
// recompute the median gas price
mi := medianOfArray(gasPrice.Prices)

// #nosec G115 always positive
gasPrice.MedianIndex = uint64(mi)

return k.voteGasPrice(ctx, chain, gasPrice)
}

func (k msgServer) voteGasPrice(
ctx sdk.Context,
chain chains.Chain,
entity types.GasPrice,
) (*types.MsgVoteGasPriceResponse, error) {
var (
chainID = big.NewInt(chain.ChainId)
gasPrice = math.NewUint(entity.Prices[entity.MedianIndex]).BigInt()
)

// set gas price in this module
k.SetGasPrice(ctx, entity)

// set gas price in fungible keeper (also calls EVM)
gasUsed, err := k.fungibleKeeper.SetGasPrice(ctx, chainID, gasPrice)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "unable to set gas price in fungible keeper")
}

// reset the gas count
Expand Down
Loading
Loading