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 set oracles at ApplyUpgrade #586

Merged
merged 49 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
3893eae
feat: add extra commission rate flags to genoracle cmd
audtlr24 Dec 19, 2022
8330472
feat: add v2.1.0 upgrade handler (#552)
0xHansLee Dec 15, 2022
eda304c
ci: make actions work for main branch (#555)
Dec 16, 2022
fecab14
feat: add oracle upgrade proposal tx & cli
audtlr24 Dec 26, 2022
af4a238
feat: add test code
audtlr24 Dec 26, 2022
402eeb1
feat: add oracle upgrade proposal tx & cli
audtlr24 Dec 26, 2022
468a132
feat: add test code
audtlr24 Dec 26, 2022
31557d5
Merge remote-tracking branch 'origin/ft/569/oracle-upgrade-handler' i…
audtlr24 Dec 26, 2022
ec72841
feat: proto for oracle upgrade and its skeleton
Dec 26, 2022
8eec577
fix test
Dec 26, 2022
69bc58b
add event type of oracle upgrade
Dec 26, 2022
860223d
feat: add oracle upgrade to EndBlocker
audtlr24 Dec 26, 2022
909c3d8
add oracle upgrade key
Dec 27, 2022
07b94cc
fix
audtlr24 Dec 27, 2022
cb6b661
fix
audtlr24 Dec 27, 2022
1ee72d5
fix
audtlr24 Dec 27, 2022
34fee18
fix
audtlr24 Dec 27, 2022
660d959
Merge remote-tracking branch 'origin/ft/569/oracle-upgrade-handler' i…
audtlr24 Dec 27, 2022
12f0e02
feat: add upgrade oracle tx cli
audtlr24 Dec 27, 2022
c634f6f
Merge remote-tracking branch 'origin/ft/570/upgrade-endblocker' into …
audtlr24 Dec 27, 2022
6ebe8b6
feat: add upgrade oracle keeper
audtlr24 Dec 27, 2022
f290bf2
feat: add test code for oracle upgrade
audtlr24 Dec 27, 2022
61e8e4e
Merge remote-tracking branch 'origin/ft/570/upgrade-endblocker' into …
Dec 27, 2022
c66ff61
remove duplicated grpc query
Dec 27, 2022
f73a2ca
use the same request/response message for approval of oracle upgrade
Dec 28, 2022
44a3f47
fix
Dec 28, 2022
442479b
Revert "fix"
Dec 28, 2022
5da5d80
Revert "use the same request/response message for approval of oracle …
Dec 28, 2022
b7a94d9
Merge remote-tracking branch 'origin/ft/575/upgrade-proto' into ft/57…
audtlr24 Dec 28, 2022
9630dfb
fix
audtlr24 Dec 28, 2022
db97922
chore: rename ApproveOracleRegistration to reuse
Dec 28, 2022
0b4fae8
feat: implement approve oracle upgrade tx
Dec 29, 2022
d5866b7
add test
Dec 29, 2022
ea8f814
fix lint
Dec 29, 2022
c2d2532
fix
Dec 29, 2022
a3cd3e3
fix test
Dec 29, 2022
9b5944b
add msg server
Dec 29, 2022
115bb01
fix
Dec 29, 2022
d462a4c
fix
Dec 29, 2022
b54727f
feat: improve apply oracle using OracleUpgradeQueue
audtlr24 Dec 30, 2022
a23289e
Revert "feat: improve apply oracle using OracleUpgradeQueue"
audtlr24 Dec 30, 2022
4fe4352
remove duplicated validation
Dec 30, 2022
df49732
delete unnecessary test
Dec 30, 2022
1c69a6d
review applied
Dec 30, 2022
5b7a38d
feat: add set oracles at ApplyUpgrade
audtlr24 Jan 3, 2023
5cb50bf
feat: add test code for ApplyUpgrade
audtlr24 Jan 3, 2023
0e5ad56
fix
audtlr24 Jan 3, 2023
6839052
Merge remote-tracking branch 'origin/main' into ft/na/improve-apply-u…
audtlr24 Jan 4, 2023
877a9f7
fix
audtlr24 Jan 4, 2023
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
179 changes: 158 additions & 21 deletions x/oracle/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"testing"

"github.com/btcsuite/btcd/btcec"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/medibloc/panacea-core/v2/types/testsuite"
"github.com/medibloc/panacea-core/v2/x/oracle"
"github.com/medibloc/panacea-core/v2/x/oracle/types"
Expand All @@ -14,60 +17,194 @@ import (
type abciTestSuite struct {
testsuite.TestSuite

uniqueID string
oracleAccPrivKey cryptotypes.PrivKey
oracleAccPubKey cryptotypes.PubKey
oracleAccAddr sdk.AccAddress

oracle2AccPrivKey cryptotypes.PrivKey
oracle2AccPubKey cryptotypes.PubKey
oracle2AccAddr sdk.AccAddress

approverAccPrivKey cryptotypes.PrivKey
approverAccPubKey cryptotypes.PubKey
approverAccAddr sdk.AccAddress

oraclePrivKey *btcec.PrivateKey
oraclePubKey *btcec.PublicKey

nodePrivKey *btcec.PrivateKey
nodePubKey *btcec.PublicKey

nodePubKeyRemoteReport []byte

currentUniqueID string
upgradeUniqueID string
}

func TestAbciTestSuite(t *testing.T) {
suite.Run(t, new(abciTestSuite))
}

func (suite *abciTestSuite) BeforeTest(_, _ string) {
ctx := suite.Ctx
suite.uniqueID = "uniqueID"

oraclePrivKey, err := btcec.NewPrivateKey(btcec.S256())
suite.Require().NoError(err)
suite.oracleAccPrivKey = secp256k1.GenPrivKey()
suite.oracleAccPubKey = suite.oracleAccPrivKey.PubKey()
suite.oracleAccAddr = sdk.AccAddress(suite.oracleAccPubKey.Address())

suite.oracle2AccPrivKey = secp256k1.GenPrivKey()
suite.oracle2AccPubKey = suite.oracle2AccPrivKey.PubKey()
suite.oracle2AccAddr = sdk.AccAddress(suite.oracle2AccPubKey.Address())

suite.approverAccPrivKey = secp256k1.GenPrivKey()
suite.approverAccPubKey = suite.approverAccPrivKey.PubKey()
suite.approverAccAddr = sdk.AccAddress(suite.approverAccPubKey.Address())

suite.oraclePrivKey, _ = btcec.NewPrivateKey(btcec.S256())
suite.oraclePubKey = suite.oraclePrivKey.PubKey()

suite.nodePrivKey, _ = btcec.NewPrivateKey(btcec.S256())
suite.nodePubKey = suite.nodePrivKey.PubKey()

suite.OracleKeeper.SetParams(ctx, types.Params{
OraclePublicKey: base64.StdEncoding.EncodeToString(oraclePrivKey.PubKey().SerializeCompressed()),
OraclePubKeyRemoteReport: base64.StdEncoding.EncodeToString([]byte("oraclePubKeyRemoteReport")),
UniqueId: suite.uniqueID,
suite.nodePubKeyRemoteReport = []byte("nodePubKeyRemoteReport")

suite.currentUniqueID = "currentUniqueID"
suite.upgradeUniqueID = "upgradeUniqueID"

suite.OracleKeeper.SetParams(suite.Ctx, types.Params{
OraclePublicKey: base64.StdEncoding.EncodeToString(suite.oraclePubKey.SerializeCompressed()),
OraclePubKeyRemoteReport: "",
UniqueId: suite.currentUniqueID,
})
}

func (suite *abciTestSuite) TestOracleUpgradeSuccess() {
ctx := suite.Ctx
ctx = ctx.WithBlockHeight(1)

suite.Require().Equal(suite.uniqueID, suite.OracleKeeper.GetParams(ctx).UniqueId)

upgradeUniqueID := "upgradeUniqueID"
suite.Require().Equal(suite.currentUniqueID, suite.OracleKeeper.GetParams(ctx).UniqueId)

upgradeInfo := &types.OracleUpgradeInfo{
UniqueId: upgradeUniqueID,
UniqueId: suite.upgradeUniqueID,
Height: 10,
}

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

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

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

suite.Require().Equal(upgradeUniqueID, suite.OracleKeeper.GetParams(ctx).UniqueId)
oracle1 := &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),
}

oracle2 := &types.Oracle{
OracleAddress: suite.oracle2AccAddr.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, oracle1))
suite.Require().NoError(suite.OracleKeeper.SetOracle(ctx, oracle2))

// upgrade oracle1, oracle2
msgOracleUpgrade := &types.MsgUpgradeOracle{
UniqueId: suite.upgradeUniqueID,
OracleAddress: suite.oracleAccAddr.String(),
NodePubKey: suite.nodePubKey.SerializeCompressed(),
NodePubKeyRemoteReport: suite.nodePubKeyRemoteReport,
TrustedBlockHeight: int64(1),
TrustedBlockHash: []byte("trustedBlockHash"),
}
msgOracle2Upgrade := &types.MsgUpgradeOracle{
UniqueId: suite.upgradeUniqueID,
OracleAddress: suite.oracle2AccAddr.String(),
NodePubKey: suite.nodePubKey.SerializeCompressed(),
NodePubKeyRemoteReport: suite.nodePubKeyRemoteReport,
TrustedBlockHeight: int64(1),
TrustedBlockHash: []byte("trustedBlockHash"),
}

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

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

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

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

msgApproveOracleUpgrade := types.NewMsgApproveOracleUpgrade(approveOracleUpgrade, signature.Serialize())
suite.Require().NoError(suite.OracleKeeper.ApproveOracleUpgrade(ctx, msgApproveOracleUpgrade))

approveOracle2Upgrade := &types.ApprovalSharingOracleKey{
UniqueId: suite.currentUniqueID,
ApproverOracleAddress: suite.approverAccAddr.String(),
TargetOracleAddress: suite.oracle2AccAddr.String(),
EncryptedOraclePrivKey: encryptedOraclePrivKey,
}

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

msgApproveOracle2Upgrade := types.NewMsgApproveOracleUpgrade(approveOracle2Upgrade, signature.Serialize())
suite.Require().NoError(suite.OracleKeeper.ApproveOracleUpgrade(ctx, msgApproveOracle2Upgrade))

// ApplyUpgrade
ctx = ctx.WithBlockHeight(10)
oracle.EndBlocker(ctx, suite.OracleKeeper)

// check uniqueID change
suite.Require().Equal(suite.upgradeUniqueID, suite.OracleKeeper.GetParams(ctx).UniqueId)
getOracle, err := suite.OracleKeeper.GetOracle(ctx, suite.oracleAccAddr.String())
suite.Require().NoError(err)
suite.Require().Equal(suite.upgradeUniqueID, getOracle.UniqueId)
getOracle, err = suite.OracleKeeper.GetOracle(ctx, suite.oracle2AccAddr.String())
suite.Require().NoError(err)
suite.Require().Equal(suite.upgradeUniqueID, getOracle.UniqueId)
getOracle, err = suite.OracleKeeper.GetOracle(ctx, suite.approverAccAddr.String())
suite.Require().NoError(err)
suite.Require().Equal(suite.currentUniqueID, getOracle.UniqueId)
}

func (suite *abciTestSuite) TestOracleUpgradeFailedBeforeReachUpgradeHeight() {
ctx := suite.Ctx
ctx = ctx.WithBlockHeight(1)

suite.Require().Equal(suite.uniqueID, suite.OracleKeeper.GetParams(ctx).UniqueId)

upgradeUniqueID := "upgradeUniqueID"
suite.Require().Equal(suite.currentUniqueID, suite.OracleKeeper.GetParams(ctx).UniqueId)

upgradeInfo := &types.OracleUpgradeInfo{
UniqueId: upgradeUniqueID,
UniqueId: suite.upgradeUniqueID,
Height: 10,
}

Expand All @@ -77,5 +214,5 @@ func (suite *abciTestSuite) TestOracleUpgradeFailedBeforeReachUpgradeHeight() {

oracle.EndBlocker(ctx, suite.OracleKeeper)

suite.Require().Equal(suite.uniqueID, suite.OracleKeeper.GetParams(ctx).UniqueId)
suite.Require().Equal(suite.currentUniqueID, suite.OracleKeeper.GetParams(ctx).UniqueId)
}
35 changes: 26 additions & 9 deletions x/oracle/keeper/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,35 @@ func (k Keeper) GetOracleUpgradeInfo(ctx sdk.Context) (*types.OracleUpgradeInfo,
return &upgradeInfo, nil
}

func (k Keeper) RemoveOracleUpgradeInfo(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)

store.Delete(types.OracleUpgradeInfoKey)
}

func (k Keeper) ApplyUpgrade(ctx sdk.Context, info *types.OracleUpgradeInfo) error {

params := k.GetParams(ctx)
params.UniqueId = info.UniqueId
if err := params.Validate(); err != nil {
return err
}
k.SetParams(ctx, params)

//TODO: update `Oracles` that already upgraded
iterator := k.GetOracleUpgradeIterator(ctx, info.UniqueId)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
bz := iterator.Value()
oracleUpgrade := &types.OracleUpgrade{}
if err := k.cdc.UnmarshalLengthPrefixed(bz, oracleUpgrade); err != nil {
return err
}
if oracleUpgrade.EncryptedOraclePrivKey != nil {
oracle, err := k.GetOracle(ctx, oracleUpgrade.OracleAddress)
if err != nil {
return err
}
oracle.UniqueId = info.UniqueId
if err := k.SetOracle(ctx, oracle); err != nil {
return err
}
}
}

ctx.Logger().Info("Oracle upgrade was successful.", fmt.Sprintf("uniqueID: %s, height: %v", info.UniqueId, info.Height))
return nil
Expand Down Expand Up @@ -196,7 +210,10 @@ func (k Keeper) ApproveOracleUpgrade(ctx sdk.Context, msg *types.MsgApproveOracl
),
)

// TODO: add to queue(?) for update unique ID

return nil
}

func (k Keeper) GetOracleUpgradeIterator(ctx sdk.Context, uniqueID string) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIterator(store, types.GetOracleUpgradeByUniqueIDKey(uniqueID))
}
4 changes: 4 additions & 0 deletions x/oracle/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func GetOracleUpgradeKey(uniqueID string, address sdk.AccAddress) []byte {
return append(OracleUpgradeKey, CombineKeys([]byte(uniqueID), address)...)
}

func GetOracleUpgradeByUniqueIDKey(uniqueID string) []byte {
return append(OracleUpgradeKey, []byte(uniqueID)...)
}

func CombineKeys(keys ...[]byte) []byte {
return bytes.Join(keys, IndexSeparator)
}