Skip to content

Commit

Permalink
Oracle lifetime to 1 block (#394)
Browse files Browse the repository at this point in the history
* oracle lifetime to 1 block

* fix: move hard coded param to params

---------

Co-authored-by: Cosmic Vagabond <121588426+cosmic-vagabond@users.noreply.github.com>
  • Loading branch information
jelysn and cosmic-vagabond authored Mar 4, 2024
1 parent 8c1bdb5 commit 9ec1bd2
Show file tree
Hide file tree
Showing 16 changed files with 596 additions and 61 deletions.
12 changes: 12 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41107,6 +41107,9 @@ paths:
timestamp:
type: string
format: uint64
block_height:
type: string
format: uint64
pagination:
type: object
properties:
Expand Down Expand Up @@ -85106,6 +85109,9 @@ definitions:
timestamp:
type: string
format: uint64
block_height:
type: string
format: uint64
elys.oracle.PriceFeeder:
type: object
properties:
Expand Down Expand Up @@ -85215,6 +85221,9 @@ definitions:
timestamp:
type: string
format: uint64
block_height:
type: string
format: uint64
pagination:
type: object
properties:
Expand Down Expand Up @@ -85296,6 +85305,9 @@ definitions:
timestamp:
type: string
format: uint64
block_height:
type: string
format: uint64
elys.oracle.QueryLastBandRequestIdResponse:
type: object
properties:
Expand Down
1 change: 1 addition & 0 deletions proto/elys/oracle/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ message Params {
string client_id = 9 [ (gogoproto.customname) = "ClientID" ];
string band_epoch = 10;
uint64 price_expiry_time = 11;
uint64 life_time_in_blocks = 12;
}
12 changes: 12 additions & 0 deletions proto/elys/oracle/price.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,16 @@ message Price {
string source = 3;
string provider = 4;
uint64 timestamp = 5;
uint64 block_height = 6;
}

message LegacyPrice {
string asset = 1;
string price = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string source = 3;
string provider = 4;
uint64 timestamp = 5;
}
15 changes: 8 additions & 7 deletions scripts/upgrade-assure/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,14 @@ type Oracle struct {
type OracleParams struct {
oracletypes.Params

OracleScriptID json.Number `json:"oracle_script_id"`
Multiplier json.Number `json:"multiplier"`
AskCount json.Number `json:"ask_count"`
MinCount json.Number `json:"min_count"`
PrepareGas json.Number `json:"prepare_gas"`
ExecuteGas json.Number `json:"execute_gas"`
PriceExpiryTime json.Number `json:"price_expiry_time"`
OracleScriptID json.Number `json:"oracle_script_id"`
Multiplier json.Number `json:"multiplier"`
AskCount json.Number `json:"ask_count"`
MinCount json.Number `json:"min_count"`
PrepareGas json.Number `json:"prepare_gas"`
ExecuteGas json.Number `json:"execute_gas"`
PriceExpiryTime json.Number `json:"price_expiry_time"`
LifeTimeInBlocks json.Number `json:"life_time_in_blocks"`
}

type Capability struct {
Expand Down
1 change: 1 addition & 0 deletions scripts/upgrade-assure/update-genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func updateGenesis(validatorBalance, homePath, genesisFilePath string) {

// update oracle price expiration
genesis.AppState.Oracle.Params.PriceExpiryTime = "604800"
genesis.AppState.Oracle.Params.LifeTimeInBlocks = "1000000"

outputFilePath := homePath + "/config/genesis.json"
if err := writeGenesisFile(outputFilePath, genesis); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions x/oracle/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ func (k Keeper) EndBlock(ctx sdk.Context) {
if price.Timestamp+params.PriceExpiryTime < uint64(ctx.BlockTime().Unix()) {
k.RemovePrice(ctx, price.Asset, price.Source, price.Timestamp)
}

if price.BlockHeight+params.LifeTimeInBlocks < uint64(ctx.BlockHeight()) {
k.RemovePrice(ctx, price.Asset, price.Source, price.Timestamp)
}
}
}
1 change: 1 addition & 0 deletions x/oracle/keeper/msg_server_feed_multiple_prices.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (k msgServer) FeedMultiplePrices(goCtx context.Context, msg *types.MsgFeedM
for _, price := range msg.Prices {
price.Provider = msg.Creator
price.Timestamp = uint64(ctx.BlockTime().Unix())
price.BlockHeight = uint64(ctx.BlockHeight())
k.SetPrice(ctx, price)
}

Expand Down
11 changes: 6 additions & 5 deletions x/oracle/keeper/msg_server_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ func (k msgServer) FeedPrice(goCtx context.Context, msg *types.MsgFeedPrice) (*t
}

price := types.Price{
Provider: msg.Provider,
Asset: msg.Asset,
Price: msg.Price,
Source: msg.Source,
Timestamp: uint64(ctx.BlockTime().Unix()),
Provider: msg.Provider,
Asset: msg.Asset,
Price: msg.Price,
Source: msg.Source,
Timestamp: uint64(ctx.BlockTime().Unix()),
BlockHeight: uint64(ctx.BlockHeight()),
}

k.SetPrice(ctx, price)
Expand Down
23 changes: 23 additions & 0 deletions x/oracle/keeper/price.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,29 @@ func (k Keeper) GetAllPrice(ctx sdk.Context) (list []types.Price) {
return
}

// MigrateAllLegacyPrices migrates all legacy prices
func (k Keeper) MigrateAllLegacyPrices(ctx sdk.Context) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PriceKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})

defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var val types.LegacyPrice
k.cdc.MustUnmarshal(iterator.Value(), &val)
k.SetPrice(ctx, types.Price{
Asset: val.Asset,
Price: val.Price,
Source: val.Source,
Provider: val.Provider,
Timestamp: val.Timestamp,
BlockHeight: uint64(ctx.BlockHeight()),
})
}

return
}

func (k Keeper) GetAssetPrice(ctx sdk.Context, asset string) (types.Price, bool) {
// try out elys source
val, found := k.GetLatestPriceFromAssetAndSource(ctx, asset, types.ELYS)
Expand Down
13 changes: 13 additions & 0 deletions x/oracle/migrations/new_migrator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package migrations

import (
"github.com/elys-network/elys/x/oracle/keeper"
)

type Migrator struct {
keeper keeper.Keeper
}

func NewMigrator(keeper keeper.Keeper) Migrator {
return Migrator{keeper: keeper}
}
14 changes: 14 additions & 0 deletions x/oracle/migrations/v2_migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package migrations

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/oracle/types"
)

func (m Migrator) V2Migration(ctx sdk.Context) error {
params := types.DefaultParams()
params.BandChannelSource = "channel-0"
m.keeper.SetParams(ctx, params)
m.keeper.MigrateAllLegacyPrices(ctx)
return nil
}
8 changes: 7 additions & 1 deletion x/oracle/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
porttypes "github.com/cosmos/ibc-go/v7/modules/core/05-port/types"
"github.com/elys-network/elys/x/oracle/client/cli"
"github.com/elys-network/elys/x/oracle/keeper"
"github.com/elys-network/elys/x/oracle/migrations"
"github.com/elys-network/elys/x/oracle/types"
)

Expand Down Expand Up @@ -117,6 +118,11 @@ func NewAppModule(
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
m := migrations.NewMigrator(am.keeper)
err := cfg.RegisterMigration(types.ModuleName, 1, m.V2Migration)
if err != nil {
panic(err)
}
}

// RegisterInvariants registers the invariants of the module. If an invariant deviates from its predicted value, the InvariantRegistry triggers appropriate logic (most often the chain will be halted)
Expand All @@ -140,7 +146,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion is a sequence number for state-breaking change of the module. It should be incremented on each consensus-breaking change introduced by the module. To avoid wrong/empty versions, the initial version should be set to 1
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (AppModule) ConsensusVersion() uint64 { return 2 }

// BeginBlock contains the logic that is automatically triggered at the beginning of each block
func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
Expand Down
11 changes: 6 additions & 5 deletions x/oracle/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ func (im IBCModule) handleOraclePacket(

for index, symbol := range request.Symbols {
im.keeper.SetPrice(ctx, types.Price{
Asset: symbol,
Price: sdk.NewDecWithPrec(int64(BandPriceResult.Rates[index]), int64(params.Multiplier)),
Source: types.BAND,
Provider: "automation",
Timestamp: uint64(ctx.BlockTime().Unix()),
Asset: symbol,
Price: sdk.NewDecWithPrec(int64(BandPriceResult.Rates[index]), int64(params.Multiplier)),
Source: types.BAND,
Provider: "automation",
Timestamp: uint64(ctx.BlockTime().Unix()),
BlockHeight: uint64(ctx.BlockHeight()),
})
}

Expand Down
19 changes: 18 additions & 1 deletion x/oracle/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
KeyExecuteGas = []byte("ExecuteGas")
KeyModuleAdmin = []byte("ModuleAdmin")
KeyPriceExpiryTime = []byte("PriceExpiryTime")
KeyLifeTimeInBlocks = []byte("LifeTimeInBlocks")
)

var _ paramtypes.ParamSet = (*Params)(nil)
Expand All @@ -42,6 +43,7 @@ func NewParams(
prepareGas uint64,
executeGas uint64,
priceExpiryTime uint64,
lifeTimeInBlocks uint64,
) Params {
return Params{
BandEpoch: bandEpoch,
Expand All @@ -55,6 +57,7 @@ func NewParams(
PrepareGas: prepareGas,
ExecuteGas: executeGas,
PriceExpiryTime: priceExpiryTime,
LifeTimeInBlocks: lifeTimeInBlocks,
}
}

Expand All @@ -71,6 +74,7 @@ func DefaultParams() Params {
600000,
600000,
86400, // 1 day old data
1, // 1 block old data
)
}

Expand All @@ -88,6 +92,7 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
paramtypes.NewParamSetPair(KeyPrepareGas, &p.PrepareGas, validateGas),
paramtypes.NewParamSetPair(KeyExecuteGas, &p.ExecuteGas, validateGas),
paramtypes.NewParamSetPair(KeyPriceExpiryTime, &p.PriceExpiryTime, validatePriceExpiryTime),
paramtypes.NewParamSetPair(KeyLifeTimeInBlocks, &p.LifeTimeInBlocks, validateLifeTimeInBlocks),
}
}

Expand Down Expand Up @@ -126,6 +131,9 @@ func (p Params) Validate() error {
if err := validatePriceExpiryTime(p.PriceExpiryTime); err != nil {
return err
}
if err := validateLifeTimeInBlocks(p.LifeTimeInBlocks); err != nil {
return err
}

return nil
}
Expand Down Expand Up @@ -229,7 +237,16 @@ func validateGas(i interface{}) error {
func validatePriceExpiryTime(i interface{}) error {
_, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid type for module admin: %T", i)
return fmt.Errorf("invalid type for price expiry time: %T", i)
}

return nil
}

func validateLifeTimeInBlocks(i interface{}) error {
_, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid type for life time in blocks: %T", i)
}

return nil
Expand Down
Loading

0 comments on commit 9ec1bd2

Please sign in to comment.