Skip to content

Commit

Permalink
feat: improve apply oracle using OracleUpgradeQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
audtlr24 committed Dec 30, 2022
1 parent d462a4c commit b54727f
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 2 deletions.
38 changes: 36 additions & 2 deletions x/oracle/keeper/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,22 @@ func (k Keeper) ApplyUpgrade(ctx sdk.Context, info *types.OracleUpgradeInfo) err
}
k.SetParams(ctx, params)

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

for ; iterator.Valid(); iterator.Next() {
uniqueID, accAddr := types.SplitOracleUpgradeQueueKey(iterator.Key())
upgradeOracleAddress := accAddr.String()
oracle, err := k.GetOracle(ctx, upgradeOracleAddress)
if err != nil {
return err
}
oracle.UniqueId = uniqueID
if err := k.SetOracle(ctx, oracle); err != nil {
return err
}
k.RemoveOracleUpgradeQueue(ctx, uniqueID, accAddr)
}

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

// TODO: add to queue(?) for update unique ID
// add to oracle upgrade queue
accAddr, err := sdk.AccAddressFromBech32(oracleUpgrade.OracleAddress)
if err != nil {
return sdkerrors.Wrapf(types.ErrApproveOracleUpgrade, err.Error())
}
k.AddOracleUpgradeQueue(ctx, oracleUpgrade.UniqueId, accAddr)

return nil
}

func (k Keeper) AddOracleUpgradeQueue(ctx sdk.Context, uniqueID string, addr sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetOracleUpgradeQueueKey(uniqueID, addr), addr)
}
func (k Keeper) GetOracleUpgradeQueueIterator(ctx sdk.Context, uniqueID string) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIterator(store, types.GetOracleUpgradesKey(uniqueID))
}

func (k Keeper) RemoveOracleUpgradeQueue(ctx sdk.Context, uniqueID string, addr sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetOracleUpgradeQueueKey(uniqueID, addr))
}
53 changes: 53 additions & 0 deletions x/oracle/keeper/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type oracleUpgradeTestSuite struct {
oracleAccPubKey cryptotypes.PubKey
oracleAccAddr sdk.AccAddress

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

approverAccPrivKey cryptotypes.PrivKey
approverAccPubKey cryptotypes.PubKey
approverAccAddr sdk.AccAddress
Expand All @@ -46,6 +50,10 @@ func (suite *oracleUpgradeTestSuite) BeforeTest(_, _ string) {
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())
Expand Down Expand Up @@ -100,8 +108,42 @@ func (suite *oracleUpgradeTestSuite) TestApplyUpgradeSuccess() {
Height: 1,
}

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))

suite.OracleKeeper.AddOracleUpgradeQueue(suite.Ctx, suite.upgradeUniqueID, suite.oracleAccAddr)
suite.OracleKeeper.AddOracleUpgradeQueue(suite.Ctx, suite.upgradeUniqueID, suite.oracle2AccAddr)

suite.Require().NoError(suite.OracleKeeper.ApplyUpgrade(ctx, upgradeInfo))
suite.Require().Equal(upgradeInfo.UniqueId, suite.OracleKeeper.GetParams(ctx).UniqueId)

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

getOracle2, err := suite.OracleKeeper.GetOracle(ctx, suite.oracle2AccAddr.String())
suite.Require().NoError(err)
suite.Require().Equal(suite.upgradeUniqueID, getOracle2.UniqueId)
}

func (suite *oracleUpgradeTestSuite) TestUpgradeOracleSuccess() {
Expand Down Expand Up @@ -309,6 +351,17 @@ func (suite *oracleUpgradeTestSuite) TestApproveOracleUpgradeSuccess() {
for _, v := range requiredEvents {
suite.Require().True(v)
}

// check OracleUpgradeQueue
iterator := suite.OracleKeeper.GetOracleUpgradeQueueIterator(ctx, suite.upgradeUniqueID)
iteratorSize := 0
for ; iterator.Valid(); iterator.Next() {
uniqueID, accAddr := types.SplitOracleUpgradeQueueKey(iterator.Key())
suite.Require().Equal(uniqueID, suite.upgradeUniqueID)
suite.Require().Equal(accAddr, suite.oracleAccAddr)
iteratorSize++
}
suite.Require().Equal(1, iteratorSize)
}

func (suite *oracleUpgradeTestSuite) TestApproveOracleUpgradeFailAlreadyApproved() {
Expand Down
13 changes: 13 additions & 0 deletions x/oracle/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var (
OracleRegistrationKey = []byte{0x02}
OracleUpgradeInfoKey = []byte{0x03}
OracleUpgradeKey = []byte{0x04}
OracleUpgradeQueueKey = []byte{0x05}

IndexSeparator = []byte{0xFF}
)
Expand All @@ -45,6 +46,18 @@ func GetOracleUpgradeKey(uniqueID string, address sdk.AccAddress) []byte {
return append(OracleUpgradeKey, CombineKeys([]byte(uniqueID), address)...)
}

func GetOracleUpgradeQueueKey(uniqueID string, addr sdk.AccAddress) []byte {
return append(OracleUpgradeQueueKey, CombineKeys([]byte(uniqueID), addr)...)
}

func GetOracleUpgradesKey(uniqueID string) []byte {
return append(OracleUpgradeQueueKey, []byte(uniqueID)...)
}

func SplitOracleUpgradeQueueKey(key []byte) (string, sdk.AccAddress) {
return string(key[1 : len(key)-21]), key[len(key)-20:]
}

func CombineKeys(keys ...[]byte) []byte {
return bytes.Join(keys, IndexSeparator)
}
21 changes: 21 additions & 0 deletions x/oracle/types/keys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package types_test

import (
"testing"

"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/medibloc/panacea-core/v2/x/oracle/types"
"github.com/stretchr/testify/require"
)

func TestSplitOracleUpgradeQueueKey(t *testing.T) {
uniqueID := "uniqueID"
addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())

key := types.GetOracleUpgradeQueueKey(uniqueID, addr)

splitUniqueID, splitAddr := types.SplitOracleUpgradeQueueKey(key)
require.Equal(t, uniqueID, splitUniqueID)
require.Equal(t, addr, splitAddr)
}

0 comments on commit b54727f

Please sign in to comment.