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 a genesis function of Consent #593

Merged
merged 2 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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/datadeal/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
panic(err)
}

for _, consent := range genState.Consents {
if err := k.SetConsent(ctx, &consent); err != nil {
panic(err)
}
}

}

// ExportGenesis returns the capability module's exported genesis.
Expand All @@ -37,5 +43,11 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
}
genesis.NextDealNumber = nextDealNum

consents, err := k.GetAllConsents(ctx)
if err != nil {
panic(err)
}
genesis.Consents = consents

return genesis
}
153 changes: 151 additions & 2 deletions x/datadeal/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
package datadeal_test

import (
"encoding/base64"
"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/assets"
"github.com/medibloc/panacea-core/v2/x/datadeal"
"github.com/medibloc/panacea-core/v2/x/datadeal/testutil"
"github.com/medibloc/panacea-core/v2/x/datadeal/types"
oracletypes "github.com/medibloc/panacea-core/v2/x/oracle/types"
"github.com/stretchr/testify/suite"
)

type genesisTestSuite struct {
testutil.DataDealBaseTestSuite

consumerAccAddr sdk.AccAddress
defaultFunds sdk.Coins
providerAccAddr sdk.AccAddress

oracleAccPrivKey cryptotypes.PrivKey
oracleAccPubKey cryptotypes.PubKey

oraclePrivKey *btcec.PrivateKey
oraclePubKey *btcec.PublicKey
oracleAccAddr sdk.AccAddress

defaultFunds sdk.Coins
}

func TestGenesisTestSuite(t *testing.T) {
Expand All @@ -25,16 +38,87 @@ func TestGenesisTestSuite(t *testing.T) {

func (suite *genesisTestSuite) BeforeTest(_, _ string) {
suite.consumerAccAddr = sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
suite.providerAccAddr = sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())

suite.oracleAccPrivKey = secp256k1.GenPrivKey()
suite.oracleAccPubKey = suite.oracleAccPrivKey.PubKey()
suite.oracleAccAddr = sdk.AccAddress(suite.oracleAccPubKey.Address())

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

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

oracle := &oracletypes.Oracle{
OracleAddress: suite.oracleAccAddr.String(),
UniqueId: "uniqueID",
Endpoint: "https://my-validator.org",
OracleCommissionRate: sdk.NewDecWithPrec(1, 1),
}
err := suite.OracleKeeper.SetOracle(suite.Ctx, oracle)
suite.Require().NoError(err)

suite.defaultFunds = sdk.NewCoins(sdk.NewCoin(assets.MicroMedDenom, sdk.NewInt(10000000000)))
}

func (suite *genesisTestSuite) TestInitGenesis() {
deal1 := suite.MakeTestDeal(1, suite.consumerAccAddr, 100)
deal2 := suite.MakeTestDeal(2, suite.consumerAccAddr, 100)

unsignedCert1 := &types.UnsignedCertificate{
Cid: "cid",
UniqueId: "uniqueID",
OracleAddress: suite.oracleAccAddr.String(),
DealId: 1,
ProviderAddress: suite.providerAccAddr.String(),
DataHash: "dataHash",
}

unsignedCertBz1, err := unsignedCert1.Marshal()
suite.Require().NoError(err)

sign1, err := suite.oraclePrivKey.Sign(unsignedCertBz1)

suite.Require().NoError(err)

consent1 := &types.Consent{
Certificate: &types.Certificate{
UnsignedCertificate: unsignedCert1,
Signature: sign1.Serialize(),
},
}

unsignedCert2 := &types.UnsignedCertificate{
Cid: "cid",
UniqueId: "uniqueID",
OracleAddress: suite.oracleAccAddr.String(),
DealId: 2,
ProviderAddress: suite.providerAccAddr.String(),
DataHash: "dataHash",
}

unsignedCertBz2, err := unsignedCert1.Marshal()
suite.Require().NoError(err)

sign2, err := suite.oraclePrivKey.Sign(unsignedCertBz2)

suite.Require().NoError(err)

consent2 := &types.Consent{
Certificate: &types.Certificate{
UnsignedCertificate: unsignedCert2,
Signature: sign2.Serialize(),
},
}

genesis := types.GenesisState{
Deals: []types.Deal{*deal1, *deal2},
NextDealNumber: 1,
Consents: []types.Consent{*consent1, *consent2},
}

datadeal.InitGenesis(suite.Ctx, suite.DataDealKeeper, genesis)
Expand All @@ -46,15 +130,47 @@ func (suite *genesisTestSuite) TestInitGenesis() {
getDeal2, err := suite.DataDealKeeper.GetDeal(suite.Ctx, 2)
suite.Require().NoError(err)
suite.Require().Equal(genesis.Deals[1], *getDeal2)

getConsent1, err := suite.DataDealKeeper.GetConsent(suite.Ctx, 1, "dataHash")
suite.Require().NoError(err)
suite.Require().Equal(genesis.Consents[0], *getConsent1)

getConsent2, err := suite.DataDealKeeper.GetConsent(suite.Ctx, 2, "dataHash")
suite.Require().NoError(err)
suite.Require().Equal(genesis.Consents[1], *getConsent2)
}

func (suite *genesisTestSuite) TestExportGenesis() {
deal1 := suite.MakeTestDeal(1, suite.consumerAccAddr, 100)
deal2 := suite.MakeTestDeal(2, suite.consumerAccAddr, 100)

unsignedCert1 := &types.UnsignedCertificate{
Cid: "cid",
UniqueId: "uniqueID",
OracleAddress: suite.oracleAccAddr.String(),
DealId: 1,
ProviderAddress: suite.providerAccAddr.String(),
DataHash: "dataHash",
}

unsignedCertBz1, err := unsignedCert1.Marshal()
suite.Require().NoError(err)

sign1, err := suite.oraclePrivKey.Sign(unsignedCertBz1)

suite.Require().NoError(err)

consent1 := &types.Consent{
Certificate: &types.Certificate{
UnsignedCertificate: unsignedCert1,
Signature: sign1.Serialize(),
},
}

genesis := types.GenesisState{
Deals: []types.Deal{*deal1},
NextDealNumber: 2,
Consents: []types.Consent{*consent1},
}

msgCreateDeal := &types.MsgCreateDeal{
Expand All @@ -64,14 +180,44 @@ func (suite *genesisTestSuite) TestExportGenesis() {
ConsumerAddress: deal2.ConsumerAddress,
}

unsignedCert2 := &types.UnsignedCertificate{
Cid: "cid",
UniqueId: "uniqueID",
OracleAddress: suite.oracleAccAddr.String(),
DealId: 2,
ProviderAddress: suite.providerAccAddr.String(),
DataHash: "dataHash",
}

unsignedCertBz2, err := unsignedCert1.Marshal()
suite.Require().NoError(err)

sign2, err := suite.oraclePrivKey.Sign(unsignedCertBz2)

suite.Require().NoError(err)

consent2 := &types.Consent{
Certificate: &types.Certificate{
UnsignedCertificate: unsignedCert2,
Signature: sign2.Serialize(),
},
}

msgSubmitConsent := &types.MsgSubmitConsent{
Consent: consent2,
}

datadeal.InitGenesis(suite.Ctx, suite.DataDealKeeper, genesis)

err := suite.FundAccount(suite.Ctx, suite.consumerAccAddr, suite.defaultFunds)
err = suite.FundAccount(suite.Ctx, suite.consumerAccAddr, suite.defaultFunds)
suite.Require().NoError(err)

_, err = suite.DataDealKeeper.CreateDeal(suite.Ctx, msgCreateDeal)
suite.Require().NoError(err)

err = suite.DataDealKeeper.SubmitConsent(suite.Ctx, msgSubmitConsent.Consent)
suite.Require().NoError(err)

genesisStatus := datadeal.ExportGenesis(suite.Ctx, suite.DataDealKeeper)

suite.Require().Equal(deal1.Id, genesisStatus.Deals[0].Id)
Expand All @@ -85,4 +231,7 @@ func (suite *genesisTestSuite) TestExportGenesis() {
suite.Require().Equal(deal1.Budget, genesisStatus.Deals[0].Budget)
suite.Require().Equal(deal2.Budget, genesisStatus.Deals[1].Budget)
suite.Require().Equal(uint64(3), genesisStatus.NextDealNumber)

suite.Require().Equal(*consent1, genesisStatus.Consents[0])
suite.Require().Equal(*consent2, genesisStatus.Consents[1])
}
21 changes: 21 additions & 0 deletions x/datadeal/keeper/consent.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,24 @@ func (k Keeper) GetConsent(ctx sdk.Context, dealID uint64, dataHash string) (*ty

return consent, nil
}

func (k Keeper) GetAllConsents(ctx sdk.Context) ([]types.Consent, error) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.ConsentKey)
defer iterator.Close()

consents := make([]types.Consent, 0)

for ; iterator.Valid(); iterator.Next() {
bz := iterator.Value()
var consent types.Consent

if err := k.cdc.UnmarshalLengthPrefixed(bz, &consent); err != nil {
return nil, sdkerrors.Wrapf(types.ErrGetConsent, err.Error())
}

consents = append(consents, consent)
}

return consents, nil
}
28 changes: 14 additions & 14 deletions x/datadeal/keeper/consent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/stretchr/testify/suite"
)

type certificateTestSuite struct {
type consentTestSuite struct {
testsuite.TestSuite

uniqueID string
Expand All @@ -41,10 +41,10 @@ type certificateTestSuite struct {
}

func TestCertificateTestSuite(t *testing.T) {
suite.Run(t, new(certificateTestSuite))
suite.Run(t, new(consentTestSuite))
}

func (suite *certificateTestSuite) BeforeTest(_, _ string) {
func (suite *consentTestSuite) BeforeTest(_, _ string) {
suite.consumerAccAddr = sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
suite.defaultFunds = sdk.NewCoins(sdk.NewCoin(assets.MicroMedDenom, sdk.NewInt(10000000000)))

Expand Down Expand Up @@ -74,7 +74,7 @@ func (suite *certificateTestSuite) BeforeTest(_, _ string) {
suite.Require().NoError(err)
}

func (suite *certificateTestSuite) createSampleDeal(budgetAmount, maxNumData uint64) uint64 {
func (suite *consentTestSuite) createSampleDeal(budgetAmount, maxNumData uint64) uint64 {
err := suite.FundAccount(suite.Ctx, suite.consumerAccAddr, suite.defaultFunds)
suite.Require().NoError(err)

Expand All @@ -93,7 +93,7 @@ func (suite *certificateTestSuite) createSampleDeal(budgetAmount, maxNumData uin
return dealID
}

func (suite *certificateTestSuite) storeSampleOracle(address, uniqueID string, commissionRate sdk.Dec) *oracletypes.Oracle {
func (suite *consentTestSuite) storeSampleOracle(address, uniqueID string, commissionRate sdk.Dec) *oracletypes.Oracle {
oracle := &oracletypes.Oracle{
OracleAddress: address,
UniqueId: uniqueID,
Expand All @@ -106,7 +106,7 @@ func (suite *certificateTestSuite) storeSampleOracle(address, uniqueID string, c
return oracle
}

func (suite *certificateTestSuite) TestSubmitConsentSuccess() {
func (suite *consentTestSuite) TestSubmitConsentSuccess() {
budgetAmount := uint64(10000)
dealID := suite.createSampleDeal(budgetAmount, 10)
deal, err := suite.DataDealKeeper.GetDeal(suite.Ctx, dealID)
Expand Down Expand Up @@ -169,7 +169,7 @@ func (suite *certificateTestSuite) TestSubmitConsentSuccess() {
suite.Require().Equal(types.DEAL_STATUS_ACTIVE, deal.Status)
}

func (suite *certificateTestSuite) TestSubmitConsentChangeStatusComplete() {
func (suite *consentTestSuite) TestSubmitConsentChangeStatusComplete() {
budgetAmount := uint64(10000)
dealID := suite.createSampleDeal(budgetAmount, 1)
deal, err := suite.DataDealKeeper.GetDeal(suite.Ctx, dealID)
Expand Down Expand Up @@ -231,7 +231,7 @@ func (suite *certificateTestSuite) TestSubmitConsentChangeStatusComplete() {
suite.Require().Equal(types.DEAL_STATUS_COMPLETED, deal.Status)
}

func (suite *certificateTestSuite) TestSubmitConsentNotRegisteredOracle() {
func (suite *consentTestSuite) TestSubmitConsentNotRegisteredOracle() {
budgetAmount := uint64(10000)
dealID := suite.createSampleDeal(budgetAmount, 1)

Expand Down Expand Up @@ -262,7 +262,7 @@ func (suite *certificateTestSuite) TestSubmitConsentNotRegisteredOracle() {
suite.Require().ErrorContains(err, fmt.Sprintf("failed to oracle validation. address(%s)", suite.providerAccAddr.String()))
}

func (suite *certificateTestSuite) TestSubmitConsentNotSameUniqueIDOfOracle() {
func (suite *consentTestSuite) TestSubmitConsentNotSameUniqueIDOfOracle() {
budgetAmount := uint64(10000)
dealID := suite.createSampleDeal(budgetAmount, 1)

Expand Down Expand Up @@ -296,7 +296,7 @@ func (suite *certificateTestSuite) TestSubmitConsentNotSameUniqueIDOfOracle() {
suite.Require().ErrorContains(err, "is not active an oracle.")
}

func (suite *certificateTestSuite) TestSubmitConsentInvalidSignature() {
func (suite *consentTestSuite) TestSubmitConsentInvalidSignature() {
budgetAmount := uint64(10000)
dealID := suite.createSampleDeal(budgetAmount, 1)

Expand Down Expand Up @@ -330,7 +330,7 @@ func (suite *certificateTestSuite) TestSubmitConsentInvalidSignature() {
suite.Require().ErrorContains(err, "failed to signature validation")
}

func (suite *certificateTestSuite) TestSubmitConsentNotExistDeal() {
func (suite *consentTestSuite) TestSubmitConsentNotExistDeal() {
oracleCommissionRate := sdk.NewDecWithPrec(1, 1) // 10%
suite.storeSampleOracle(suite.oracleAccAddr.String(), suite.uniqueID, oracleCommissionRate)

Expand Down Expand Up @@ -361,7 +361,7 @@ func (suite *certificateTestSuite) TestSubmitConsentNotExistDeal() {
suite.Require().ErrorContains(err, "failed to get deal.")
}

func (suite *certificateTestSuite) TestSubmitConsentAlreadyDealStatusComplete() {
func (suite *consentTestSuite) TestSubmitConsentAlreadyDealStatusComplete() {
suite.TestSubmitConsentChangeStatusComplete()

unsignedCert := &types.UnsignedCertificate{
Expand Down Expand Up @@ -392,7 +392,7 @@ func (suite *certificateTestSuite) TestSubmitConsentAlreadyDealStatusComplete()
suite.Require().ErrorContains(err, "deal status is not ACTIVE")
}

func (suite *certificateTestSuite) TestSubmitConsentExistSameCertificate() {
func (suite *consentTestSuite) TestSubmitConsentExistSameCertificate() {
suite.TestSubmitConsentSuccess()

unsignedCert := &types.UnsignedCertificate{
Expand Down Expand Up @@ -422,7 +422,7 @@ func (suite *certificateTestSuite) TestSubmitConsentExistSameCertificate() {
suite.Require().ErrorContains(err, "already provided consent: error while submit consent")
}

func (suite *certificateTestSuite) TestSubmitConsentNotSameUniqueIDOfCertificate() {
func (suite *consentTestSuite) TestSubmitConsentNotSameUniqueIDOfCertificate() {
budgetAmount := uint64(10000)
dealID := suite.createSampleDeal(budgetAmount, 1)

Expand Down
Loading