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: add Oracle update at ApproveOracleUpgrade #608

Merged
merged 3 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
12 changes: 12 additions & 0 deletions x/oracle/keeper/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,18 @@ func (k Keeper) ApproveOracleUpgrade(ctx sdk.Context, msg *types.MsgApproveOracl
return sdkerrors.Wrapf(types.ErrApproveOracleUpgrade, err.Error())
}

// set oracle for request after upgrade height
if ctx.BlockHeight() >= upgradeInfo.Height {
youngjoon-lee marked this conversation as resolved.
Show resolved Hide resolved
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)

}