Skip to content

Commit

Permalink
chore: rename ApproveOracleRegistration to reuse (#580)
Browse files Browse the repository at this point in the history
Co-authored-by: audtlr24 <audtlr24@gmail.com>
Co-authored-by: Youngjoon Lee <taxihighway@gmail.com>
Co-authored-by: Youngjoon Lee <yjlee@medibloc.org>
  • Loading branch information
4 people committed Jan 4, 2023
1 parent c4dc6a6 commit 76593e2
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 256 deletions.
12 changes: 5 additions & 7 deletions proto/panacea/oracle/v2/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ message MsgRegisterOracleResponse {

// MsgApproveOracleRegistration defines the Msg/ApproveOracleRegistration
message MsgApproveOracleRegistration {
ApproveOracleRegistration approve_oracle_registration = 1;
ApprovalSharingOracleKey approval_sharing_oracle_key = 1;
bytes signature = 2;
}

// ApproveOracleRegistration defines for oracle registration approval
message ApproveOracleRegistration {
// ApprovalSharingOracleKey defines approval for sharing oracle key encrypted with target oracle's node public key
message ApprovalSharingOracleKey {
string unique_id = 1;
string approver_oracle_address = 2;
string target_oracle_address = 3;
Expand Down Expand Up @@ -90,10 +90,8 @@ message MsgUpgradeOracleResponse {}

// MsgApproveOracleUpgrade defines the Msg/ApproveOracleUpgrade
message MsgApproveOracleUpgrade {
string unique_id = 1;
string approver_oracle_address = 2;
string target_oracle_address = 3;
bytes encrypted_oracle_priv_key = 4;
ApprovalSharingOracleKey approval_sharing_oracle_key = 1;
bytes signature = 2;
}

// MsgApproveOracleUpgradeResponse defines the response of Msg/ApproveOracleUpgrade
Expand Down
20 changes: 10 additions & 10 deletions x/oracle/keeper/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ func (k Keeper) ApproveOracleRegistration(ctx sdk.Context, msg *types.MsgApprove
return sdkerrors.Wrapf(types.ErrApproveOracleRegistration, err.Error())
}

oracleRegistration, err := k.GetOracleRegistration(ctx, msg.ApproveOracleRegistration.UniqueId, msg.ApproveOracleRegistration.TargetOracleAddress)
oracleRegistration, err := k.GetOracleRegistration(ctx, msg.ApprovalSharingOracleKey.UniqueId, msg.ApprovalSharingOracleKey.TargetOracleAddress)
if err != nil {
return sdkerrors.Wrapf(types.ErrApproveOracleRegistration, err.Error())
}

oracleRegistration.EncryptedOraclePrivKey = msg.ApproveOracleRegistration.EncryptedOraclePrivKey
oracleRegistration.EncryptedOraclePrivKey = msg.ApprovalSharingOracleKey.EncryptedOraclePrivKey

// add an encrypted oracle private key to oracleRegistration
if err := k.SetOracleRegistration(ctx, oracleRegistration); err != nil {
return sdkerrors.Wrapf(types.ErrApproveOracleRegistration, err.Error())
}

newOracle := types.NewOracle(
msg.ApproveOracleRegistration.TargetOracleAddress,
msg.ApproveOracleRegistration.UniqueId,
msg.ApprovalSharingOracleKey.TargetOracleAddress,
msg.ApprovalSharingOracleKey.UniqueId,
oracleRegistration.Endpoint,
oracleRegistration.OracleCommissionRate,
oracleRegistration.OracleCommissionMaxRate,
Expand All @@ -79,8 +79,8 @@ func (k Keeper) ApproveOracleRegistration(ctx sdk.Context, msg *types.MsgApprove
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeApproveOracleRegistration,
sdk.NewAttribute(types.AttributeKeyOracleAddress, msg.ApproveOracleRegistration.TargetOracleAddress),
sdk.NewAttribute(types.AttributeKeyUniqueID, msg.ApproveOracleRegistration.UniqueId),
sdk.NewAttribute(types.AttributeKeyOracleAddress, msg.ApprovalSharingOracleKey.TargetOracleAddress),
sdk.NewAttribute(types.AttributeKeyUniqueID, msg.ApprovalSharingOracleKey.UniqueId),
),
)

Expand All @@ -92,19 +92,19 @@ func (k Keeper) ApproveOracleRegistration(ctx sdk.Context, msg *types.MsgApprove
func (k Keeper) validateApproveOracleRegistration(ctx sdk.Context, msg *types.MsgApproveOracleRegistration) error {

params := k.GetParams(ctx)
targetOracleAddress := msg.ApproveOracleRegistration.TargetOracleAddress
targetOracleAddress := msg.ApprovalSharingOracleKey.TargetOracleAddress

// check unique id
if msg.ApproveOracleRegistration.UniqueId != params.UniqueId {
if msg.ApprovalSharingOracleKey.UniqueId != params.UniqueId {
return types.ErrInvalidUniqueID
}

// verify signature
if err := k.VerifyOracleSignature(ctx, msg.ApproveOracleRegistration, msg.Signature); err != nil {
if err := k.VerifyOracleSignature(ctx, msg.ApprovalSharingOracleKey, msg.Signature); err != nil {
return err
}

if msg.ApproveOracleRegistration.EncryptedOraclePrivKey == nil {
if msg.ApprovalSharingOracleKey.EncryptedOraclePrivKey == nil {
return fmt.Errorf("encrypted oracle private key is nil")
}

Expand Down
14 changes: 7 additions & 7 deletions x/oracle/keeper/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,19 +264,19 @@ func (suite *oracleTestSuite) TestApproveOracleRegistrationSuccess() {
encryptedOraclePrivKey, err := btcec.Encrypt(suite.nodePubKey, suite.oraclePrivKey.Serialize())
suite.Require().NoError(err)

approveOracleRegistration := &types.ApproveOracleRegistration{
approvalSharingOracleKey := &types.ApprovalSharingOracleKey{
UniqueId: suite.uniqueID,
ApproverOracleAddress: suite.oracleAccAddr.String(),
TargetOracleAddress: suite.oracleAccAddr.String(),
EncryptedOraclePrivKey: encryptedOraclePrivKey,
}

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

msgApproveOracleRegistration := types.NewMsgApproveOracleRegistration(approveOracleRegistration, signature.Serialize())
msgApproveOracleRegistration := types.NewMsgApproveOracleRegistration(approvalSharingOracleKey, signature.Serialize())

err = suite.OracleKeeper.ApproveOracleRegistration(ctx, msgApproveOracleRegistration)
suite.Require().NoError(err)
Expand All @@ -301,7 +301,7 @@ func (suite *oracleTestSuite) TestApproveOracleRegistrationSuccess() {

getOracleRegistration, err := suite.OracleKeeper.GetOracleRegistration(suite.Ctx, suite.uniqueID, suite.oracleAccAddr.String())
suite.Require().NoError(err)
suite.Require().Equal(approveOracleRegistration.EncryptedOraclePrivKey, getOracleRegistration.EncryptedOraclePrivKey)
suite.Require().Equal(approvalSharingOracleKey.EncryptedOraclePrivKey, getOracleRegistration.EncryptedOraclePrivKey)
}

func (suite *oracleTestSuite) TestApproveOracleRegistrationFailedInvalidUniqueID() {
Expand All @@ -326,7 +326,7 @@ func (suite *oracleTestSuite) TestApproveOracleRegistrationFailedInvalidUniqueID
encryptedOraclePrivKey, err := btcec.Encrypt(suite.nodePubKey, suite.oraclePrivKey.Serialize())
suite.Require().NoError(err)

approveOracleRegistration := &types.ApproveOracleRegistration{
approveOracleRegistration := &types.ApprovalSharingOracleKey{
UniqueId: "uniqueID2",
ApproverOracleAddress: suite.oracleAccAddr.String(),
TargetOracleAddress: suite.oracleAccAddr.String(),
Expand Down Expand Up @@ -365,7 +365,7 @@ func (suite *oracleTestSuite) TestApproveOracleRegistrationFailedInvalidSignatur
encryptedOraclePrivKey, err := btcec.Encrypt(suite.nodePubKey, suite.oraclePrivKey.Serialize())
suite.Require().NoError(err)

approveOracleRegistration := &types.ApproveOracleRegistration{
approveOracleRegistration := &types.ApprovalSharingOracleKey{
UniqueId: "uniqueID",
ApproverOracleAddress: suite.oracleAccAddr.String(),
TargetOracleAddress: suite.oracleAccAddr.String(),
Expand Down Expand Up @@ -412,7 +412,7 @@ func (suite *oracleTestSuite) TestApproveOracleRegistrationFailedAlreadyExistOra
encryptedOraclePrivKey, err := btcec.Encrypt(suite.nodePubKey, suite.oraclePrivKey.Serialize())
suite.Require().NoError(err)

approveOracleRegistration := &types.ApproveOracleRegistration{
approveOracleRegistration := &types.ApprovalSharingOracleKey{
UniqueId: "uniqueID",
ApproverOracleAddress: suite.oracleAccAddr.String(),
TargetOracleAddress: suite.oracleAccAddr.String(),
Expand Down
14 changes: 7 additions & 7 deletions x/oracle/types/message_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ func (m *MsgRegisterOracle) GetSigners() []sdk.AccAddress {

var _ sdk.Msg = &MsgApproveOracleRegistration{}

func NewMsgApproveOracleRegistration(approve *ApproveOracleRegistration, signature []byte) *MsgApproveOracleRegistration {
func NewMsgApproveOracleRegistration(approve *ApprovalSharingOracleKey, signature []byte) *MsgApproveOracleRegistration {
return &MsgApproveOracleRegistration{
ApproveOracleRegistration: approve,
Signature: signature,
ApprovalSharingOracleKey: approve,
Signature: signature,
}
}

Expand All @@ -102,7 +102,7 @@ func (m *MsgApproveOracleRegistration) Type() string {
}

func (m *MsgApproveOracleRegistration) ValidateBasic() error {
if err := m.ApproveOracleRegistration.ValidateBasic(); err != nil {
if err := m.ApprovalSharingOracleKey.ValidateBasic(); err != nil {
return err
}

Expand All @@ -119,14 +119,14 @@ func (m *MsgApproveOracleRegistration) GetSignBytes() []byte {
}

func (m *MsgApproveOracleRegistration) GetSigners() []sdk.AccAddress {
oracleAddress, err := sdk.AccAddressFromBech32(m.ApproveOracleRegistration.ApproverOracleAddress)
oracleAddress, err := sdk.AccAddressFromBech32(m.ApprovalSharingOracleKey.ApproverOracleAddress)
if err != nil {
panic(err)
}
return []sdk.AccAddress{oracleAddress}
}

func (m *ApproveOracleRegistration) ValidateBasic() error {
func (m *ApprovalSharingOracleKey) ValidateBasic() error {
if len(m.UniqueId) == 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "uniqueID is empty")
}
Expand Down Expand Up @@ -268,7 +268,7 @@ func (m *MsgApproveOracleUpgrade) GetSignBytes() []byte {
}

func (m *MsgApproveOracleUpgrade) GetSigners() []sdk.AccAddress {
oracleAddress, err := sdk.AccAddressFromBech32(m.ApproverOracleAddress)
oracleAddress, err := sdk.AccAddressFromBech32(m.ApprovalSharingOracleKey.ApproverOracleAddress)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion x/oracle/types/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func NewUpgradeOracle(msg *MsgUpgradeOracle) *OracleUpgrade {
TrustedBlockHash: msg.TrustedBlockHash,
}
}

func (m *OracleUpgrade) ValidateBasic() error {
if len(m.UniqueId) == 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "uniqueID is empty")
Expand All @@ -174,6 +175,5 @@ func (m *OracleUpgrade) ValidateBasic() error {
if m.TrustedBlockHash == nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "trusted block hash should not be nil")
}

return nil
}
Loading

0 comments on commit 76593e2

Please sign in to comment.