Skip to content

Commit

Permalink
test: add an e2e case for quoting after delegation
Browse files Browse the repository at this point in the history
  • Loading branch information
leonz789 committed Jan 19, 2025
1 parent 0b50903 commit 8125952
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 21 deletions.
1 change: 0 additions & 1 deletion precompiles/delegation/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func (p Precompile) Delegate(
if err != nil {
return nil, err
}

err = p.delegationKeeper.DelegateTo(ctx, delegationParams)
if err != nil {
return nil, err
Expand Down
66 changes: 64 additions & 2 deletions tests/e2e/oracle/create_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,67 @@ func (s *E2ETestSuite) TestCreatePrice() {
s.testCreatePriceNST()
s.testCreatePriceLST()
s.testSlashing()
s.testCreatePriceLSTAfterDelegationChangePower()
}

func (s *E2ETestSuite) testCreatePriceLSTAfterDelegationChangePower() {
s.moveToAndCheck(80)
priceTest1R1 := price2.updateTimestamp()
priceTimeDetID1R1 := priceTest1R1.getPriceTimeDetID("9")
priceSource1R1 := oracletypes.PriceSource{
SourceID: 1,
Prices: []*oracletypes.PriceTimeDetID{
&priceTimeDetID1R1,
},
}

// send create-price from validator-0
msg0 := oracletypes.NewMsgCreatePrice(creator0.String(), 1, []*oracletypes.PriceSource{&priceSource1R1}, 80, 1)
err := s.network.SendTxOracleCreateprice([]sdk.Msg{msg0}, "valconskey0", kr0)
s.Require().NoError(err)

// send create-price from validator-1
msg1 := oracletypes.NewMsgCreatePrice(creator1.String(), 1, []*oracletypes.PriceSource{&priceSource1R1}, 80, 1)
err = s.network.SendTxOracleCreateprice([]sdk.Msg{msg1}, "valconskey1", kr1)
s.Require().NoError(err)

s.moveToAndCheck(82)
res, err := s.network.QueryOracle().LatestPrice(ctxWithHeight(81), &oracletypes.QueryGetLatestPriceRequest{TokenId: 1})
s.NoError(err)
s.Require().Equal(res.Price.Price, price1.Price)

s.moveToAndCheck(85)
clientChainID := uint32(101)
lzNonce := uint64(0)
assetAddr, _ := hexutil.Decode(network.ETHAssetAddress)
stakerAddr := []byte(s.network.Validators[0].Address)
operatorAddr := []byte(s.network.Validators[0].Address.String())
opAmount := big.NewInt(90000000)
// deposit 32 NSTETH to staker from beaconchain_validatro_1
err = s.network.SendPrecompileTx(network.DELEGATION, "delegate", clientChainID, lzNonce, assetAddr, stakerAddr, operatorAddr, opAmount)
s.Require().NoError(err)

// wait for validator set update
s.moveToAndCheck(120)

// send create-price from validator-0
msg0 = oracletypes.NewMsgCreatePrice(creator0.String(), 1, []*oracletypes.PriceSource{&priceSource1R1}, 120, 1)
err = s.network.SendTxOracleCreateprice([]sdk.Msg{msg0}, "valconskey0", kr0)
s.Require().NoError(err)

// send create-price from validator-1
msg1 = oracletypes.NewMsgCreatePrice(creator1.String(), 1, []*oracletypes.PriceSource{&priceSource1R1}, 120, 1)
err = s.network.SendTxOracleCreateprice([]sdk.Msg{msg1}, "valconskey1", kr1)
s.Require().NoError(err)

s.moveToAndCheck(122)
// query final price. query state of 11 on height 12
res, err = s.network.QueryOracle().LatestPrice(ctxWithHeight(121), &oracletypes.QueryGetLatestPriceRequest{TokenId: 1})
s.Require().NoError(err)

ret := priceTest1R1.getPriceTimeRound(12)
ret.Timestamp = res.Price.Timestamp
s.Require().Equal(ret, res.Price)
}

/*
Expand Down Expand Up @@ -341,7 +402,8 @@ func (s *E2ETestSuite) testSlashing() {
s.Require().NoError(err)
s.Require().True(resOperator.Jailed)
// wait for validator3 to pass jail duration
time.Sleep(35 * time.Second)
// timeout commit is set to 2 seconds, 10 blocks about 20 seconds
s.moveToAndCheck(75)
msgUnjail := slashingtypes.NewMsgUnjail(s.network.Validators[3].ValAddress)
// unjail validator3
err = s.network.SendTx([]sdk.Msg{msgUnjail}, "node3", kr3)
Expand Down Expand Up @@ -374,7 +436,7 @@ func (s *E2ETestSuite) testRegisterTokenThroughPrecompile() {
}

func (s *E2ETestSuite) moveToAndCheck(height int64) {
_, err := s.network.WaitForStateHeightWithTimeout(height, 30*time.Second)
_, err := s.network.WaitForStateHeightWithTimeout(height, 120*time.Second)
s.Require().NoError(err)
}

Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/oracle/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ func generateNSTPriceTime(sc [][]int) priceTime {

var (
price1 = priceTime{
Price: "19",
Price: "1900000000",
Decimal: 8,
Timestamp: now,
}
price2 = priceTime{
Price: "29",
Price: "290000000",
Decimal: 8,
Timestamp: now,
}
Expand Down
9 changes: 5 additions & 4 deletions testutil/network/genesis_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ var (
},
},
Tokens: []assetstypes.StakingAssetInfo{
NewTestToken("ETH", "Ethereum native token", ETHAssetAddress, TestEVMChainID, 5000),
NewTestToken("NST ETH", "native restaking ETH", NativeAssetAddress, TestEVMChainID, 5000),
NewTestToken("ETH", "Ethereum native token", ETHAssetAddress, TestEVMChainID, 0, 5000),
NewTestToken("NST ETH", "native restaking ETH", NativeAssetAddress, TestEVMChainID, 0, 5000),
},
}

Expand Down Expand Up @@ -80,10 +80,10 @@ func init() {
// set slashing_miss window to 4
DefaultGenStateOracle.Params.Slashing.ReportedRoundsWindow = 4
// set jailduration of oracle report downtime to 30 seconds for test
DefaultGenStateOracle.Params.Slashing.OracleMissJailDuration = 30 * time.Second
DefaultGenStateOracle.Params.Slashing.OracleMissJailDuration = 15 * time.Second
}

func NewTestToken(name, metaInfo, address string, chainID uint64, amount int64) assetstypes.StakingAssetInfo {
func NewTestToken(name, metaInfo, address string, chainID uint64, decimal uint32, amount int64) assetstypes.StakingAssetInfo {
if name == "" {
panic("token name cannot be empty")
}
Expand All @@ -94,6 +94,7 @@ func NewTestToken(name, metaInfo, address string, chainID uint64, amount int64)
AssetBasicInfo: assetstypes.AssetInfo{
Name: name,
MetaInfo: metaInfo,
Decimals: decimal,
Address: address,
LayerZeroChainID: chainID,
},
Expand Down
13 changes: 7 additions & 6 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ func DefaultConfig() Config {
NumValidators: 4,
NativeDenom: "hua",
MinGasPrices: "10hua",
AccountTokens: sdk.TokensFromConsensusPower(1000, evmostypes.PowerReduction),
DepositedTokens: sdk.TokensFromConsensusPower(500, evmostypes.PowerReduction),
StakingTokens: sdk.TokensFromConsensusPower(200, evmostypes.PowerReduction),
PruningStrategy: pruningtypes.PruningOptionNothing,
CleanupDir: true,
SigningAlgo: string(hd.EthSecp256k1Type),
AccountTokens: sdk.NewInt(150000000),
DepositedTokens: sdk.NewInt(110000000),
StakingTokens: sdk.NewInt(20000000),

PruningStrategy: pruningtypes.PruningOptionNothing,
CleanupDir: true,
SigningAlgo: string(hd.EthSecp256k1Type),
// KeyringOptions: []keyring.Option{hd.EthSecp256k1Option()},
KeyringOptions: []keyring.Option{exocorecrypto.Ed25519Option()},
PrintMnemonic: false,
Expand Down
9 changes: 5 additions & 4 deletions testutil/network/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
cmttime "github.com/cometbft/cometbft/types/time"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/evmos/evmos/v16/server"
evmostypes "github.com/evmos/evmos/v16/types"
evmtypes "github.com/evmos/evmos/v16/x/evm/types"
feemarkettypes "github.com/evmos/evmos/v16/x/feemarket/types"
)
Expand Down Expand Up @@ -258,7 +257,9 @@ func initGenFiles(cfg Config, genAccounts []authtypes.GenesisAccount, genBalance
}

// generate empty genesis files for each validator and save
// debug-lz
gTime := cmttime.Now()
// we use a time 100 minutes before now, to trigger epoch change for each block in the early blocks(more than 100 blocks)
for i := 0; i < cfg.NumValidators; i++ {
if genDoc.InitialHeight == 0 {
genDoc.InitialHeight = 1
Expand Down Expand Up @@ -397,7 +398,7 @@ func NewGenStateOperator(operatorAccAddresses []sdk.AccAddress, consPubKeys []st
})
// OperatorUSDValues
// the price unit of assets is 1 not decimal 18
stakingValue := sdk.TokensToConsensusPower(stakingAmount, evmostypes.PowerReduction)
stakingValue := stakingAmount.Int64()
DefaultGenStateOperator.OperatorUSDValues = append(DefaultGenStateOperator.OperatorUSDValues, operatortypes.OperatorUSDValue{
Key: AVSAddress + "/" + operatorAccAddress.String(),
OptedUSDValue: operatortypes.OperatorOptedUSDValue{
Expand All @@ -414,7 +415,7 @@ func NewGenStateOperator(operatorAccAddresses []sdk.AccAddress, consPubKeys []st
AVSAddr: AVSAddress,
Value: operatortypes.DecValueField{
// the price unit of assets is 1 not decimal 18
Amount: sdkmath.LegacyNewDec(sdk.TokensToConsensusPower(totalStakingAmount, evmostypes.PowerReduction)),
Amount: sdkmath.LegacyNewDec(totalStakingAmount.Int64()),
},
})
}
Expand All @@ -425,7 +426,7 @@ func NewGenStateOperator(operatorAccAddresses []sdk.AccAddress, consPubKeys []st
// stakingAmount is the amount each operator have for every single asset defined in assets module, so for a single operator the total stakingAmount they have is stakingAmount*count(assets)
// assets genesis state is required as input argument to provide assets information. It should be called with NewGenStateAssets to update default assets genesis state for test
func NewGenStateDogfood(consPubKeys []string, stakingAmount sdkmath.Int, genStateAssets assetstypes.GenesisState) (dogfoodtypes.GenesisState, error) {
power := sdk.TokensToConsensusPower(stakingAmount.Mul(sdkmath.NewInt(int64(len(genStateAssets.Tokens)))), evmostypes.PowerReduction)
power := stakingAmount.Mul(sdkmath.NewInt(int64(len(genStateAssets.Tokens)))).Int64()
DefaultGenStateDogfood.Params.EpochIdentifier = "minute"
DefaultGenStateDogfood.Params.EpochsUntilUnbonded = 5
DefaultGenStateDogfood.Params.MinSelfDelegation = sdkmath.NewInt(100)
Expand Down
1 change: 0 additions & 1 deletion x/delegation/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func (k *Keeper) delegateTo(
if notGenesis && k.slashKeeper.IsOperatorFrozen(ctx, params.OperatorAddress) {
return delegationtype.ErrOperatorIsFrozen
}

stakerID, assetID := assetstype.GetStakerIDAndAssetID(params.ClientChainID, params.StakerAddress, params.AssetsAddress)
if assetID != assetstype.ExocoreAssetID {
// check if the staker asset has been deposited and the canWithdraw amount is bigger than the delegation amount
Expand Down
3 changes: 2 additions & 1 deletion x/oracle/keeper/feedermanagement/feedermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (f *FeederManager) EndBlock(ctx sdk.Context) {
f.ResetFlags()

f.updateCheckTx()

Check failure on line 92 in x/oracle/keeper/feedermanagement/feedermanager.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

File is not `gofumpt`-ed (gofumpt)
}

func (f *FeederManager) EndBlockInRecovery(ctx sdk.Context, params *oracletypes.Params) {
Expand Down Expand Up @@ -218,7 +219,7 @@ func (f *FeederManager) updateAndCommitCaches(ctx sdk.Context) (activeValidators
// commit caches: msgs is exists, params if updated, validatorPowers is updated
_, vUpdated, pUpdated := f.cs.Commit(ctx, false)
if vUpdated || pUpdated {
f.k.Logger(ctx).Info("update caches", "validatorUpdates", vUpdated, "paramsUpdated", pUpdated)
f.k.Logger(ctx).Info("update caches", "validatorUpdated", vUpdated, "paramsUpdated", pUpdated)
}
return activeValidators
}
Expand Down

0 comments on commit 8125952

Please sign in to comment.