Skip to content

Commit

Permalink
feat: add Oracle update at ApproveOracleUpgrade (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
audtlr24 authored Jan 11, 2023
1 parent 0cde533 commit 75c28ad
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
13 changes: 13 additions & 0 deletions x/oracle/keeper/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,19 @@ func (k Keeper) ApproveOracleUpgrade(ctx sdk.Context, msg *types.MsgApproveOracl
return sdkerrors.Wrapf(types.ErrApproveOracleUpgrade, err.Error())
}

// approve the oracle-upgrade request immediately if the upgrade height has been already passed
// (if the oracle-upgrade request wasn't processed by the BeginBlocker at the upgrade height, because there was no approval at that time.)
if ctx.BlockHeight() >= upgradeInfo.Height {
oracle, err := k.GetOracle(ctx, oracleUpgrade.OracleAddress)
if err != nil {
return err
}
oracle.UniqueId = upgradeInfo.UniqueId
if err := k.SetOracle(ctx, oracle); err != nil {
return err
}
}

// emit event
ctx.EventManager().EmitEvent(
sdk.NewEvent(
Expand Down
81 changes: 81 additions & 0 deletions x/oracle/keeper/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,84 @@ func (suite *oracleUpgradeTestSuite) TestApproveOracleUpgradeFailInvalidUniqueID
err = suite.OracleKeeper.ApproveOracleUpgrade(ctx, msgApproveOracleUpgrade)
suite.Require().ErrorContains(err, types.ErrInvalidUniqueID.Error())
}

func (suite *oracleUpgradeTestSuite) TestApproveOracleUpgradeAfterUpgradeHeight() {
ctx := suite.Ctx

// set oracle upgrade info
upgradeInfo := &types.OracleUpgradeInfo{
UniqueId: suite.upgradeUniqueID,
Height: 10,
}

suite.Require().NoError(suite.OracleKeeper.SetOracleUpgradeInfo(ctx, upgradeInfo))

// set approver oracle
approverOracle := &types.Oracle{
OracleAddress: suite.approverAccAddr.String(),
UniqueId: suite.upgradeUniqueID,
Endpoint: "iam-approver.com",
UpdateTime: ctx.BlockTime(),
OracleCommissionRate: sdk.NewDecWithPrec(1, 1),
OracleCommissionMaxRate: sdk.NewDecWithPrec(2, 1),
OracleCommissionMaxChangeRate: sdk.NewDecWithPrec(1, 2),
}

suite.Require().NoError(suite.OracleKeeper.SetOracle(ctx, approverOracle))

oracle := &types.Oracle{
OracleAddress: suite.oracleAccAddr.String(),
UniqueId: suite.currentUniqueID,
Endpoint: "test.com",
UpdateTime: ctx.BlockTime(),
OracleCommissionRate: sdk.NewDecWithPrec(1, 1),
OracleCommissionMaxRate: sdk.NewDecWithPrec(2, 1),
OracleCommissionMaxChangeRate: sdk.NewDecWithPrec(1, 2),
}

suite.Require().NoError(suite.OracleKeeper.SetOracle(ctx, oracle))

// reaching upgrade height & apply upgrade
ctx = ctx.WithBlockHeight(10)
suite.Require().NoError(suite.OracleKeeper.ApplyUpgrade(ctx, upgradeInfo))

// request upgrade after reaching upgrade height
ctx = ctx.WithBlockHeight(11)
msgOracleUpgrade := &types.MsgUpgradeOracle{
UniqueId: suite.upgradeUniqueID,
OracleAddress: suite.oracleAccAddr.String(),
NodePubKey: suite.nodePubKey.SerializeCompressed(),
NodePubKeyRemoteReport: suite.nodePubKeyRemoteReport,
TrustedBlockHeight: int64(1),
TrustedBlockHash: []byte("trustedBlockHash"),
}

suite.Require().NoError(suite.OracleKeeper.UpgradeOracle(ctx, msgOracleUpgrade))

// approve oracle upgrade
encryptedOraclePrivKey, err := btcec.Encrypt(suite.nodePubKey, suite.oraclePrivKey.Serialize())
suite.Require().NoError(err)

approveOracleUpgrade := &types.ApprovalSharingOracleKey{
ApproverUniqueId: suite.upgradeUniqueID,
ApproverOracleAddress: suite.approverAccAddr.String(),
TargetUniqueId: suite.upgradeUniqueID,
TargetOracleAddress: suite.oracleAccAddr.String(),
EncryptedOraclePrivKey: encryptedOraclePrivKey,
}

approveOracleRegistrationBz, err := suite.Cdc.Marshaler.Marshal(approveOracleUpgrade)
suite.Require().NoError(err)
signature, err := suite.oraclePrivKey.Sign(approveOracleRegistrationBz)
suite.Require().NoError(err)

msgApproveOracleUpgrade := types.NewMsgApproveOracleUpgrade(approveOracleUpgrade, signature.Serialize())

err = suite.OracleKeeper.ApproveOracleUpgrade(ctx, msgApproveOracleUpgrade)
suite.Require().NoError(err)

getOracle, err := suite.OracleKeeper.GetOracle(ctx, suite.oracleAccAddr.String())
suite.Require().NoError(err)
suite.Require().Equal(suite.upgradeUniqueID, getOracle.UniqueId)

}

0 comments on commit 75c28ad

Please sign in to comment.