Skip to content

Commit

Permalink
#523 PID scoped Accounts
Browse files Browse the repository at this point in the history
Enable submitting PID ranges while adding account

Signed-off-by: Abdulbois <[email protected]>
Signed-off-by: Abdulbois <[email protected]>
  • Loading branch information
Abdulbois committed Dec 22, 2023
1 parent 8c18b90 commit dd93905
Show file tree
Hide file tree
Showing 25 changed files with 684 additions and 133 deletions.
9 changes: 5 additions & 4 deletions cmd/dcld/cmd/genaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"encoding/json"
"fmt"
"github.com/zigbee-alliance/distributed-compliance-ledger/x/common/types"
"strings"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -110,7 +111,7 @@ the address will be looked up in the local Keybase.
}
}

var pidRanges []*dclauthtypes.Int32Range
var pidRanges []*types.Int32Range
if pidStrRanges := viper.GetString(FlagPIDs); len(pidStrRanges) > 0 {
var lastMax int32 = 0
for _, pidStrRange := range strings.Split(pidStrRanges, ",") {
Expand All @@ -130,10 +131,10 @@ the address will be looked up in the local Keybase.
if min > max || max == 0 || min == 0 {
return fmt.Errorf("invalid PID Range is provided: min=%d, max=%d", min, max)
}
if lastMax >= max {
return fmt.Errorf("invalid PID Range is provided: product ID %d is duplicated", max)
if max <= lastMax || min <= lastMax {
return fmt.Errorf("invalid PID Range is provided: {%d-%d}, ranges are overlapped, range items must be provided in increased order", min, max)
}
pid := dclauthtypes.Int32Range{Min: min, Max: max}
pid := types.Int32Range{Min: min, Max: max}
pidRanges = append(pidRanges, &pid)
lastMax = max
}
Expand Down
23 changes: 14 additions & 9 deletions integration_tests/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/zigbee-alliance/distributed-compliance-ledger/x/common/types"
)

func strToPubKey(pkStr string, cdc codec.Codec) cryptotypes.PubKey {
Expand Down Expand Up @@ -130,15 +131,19 @@ var (
UpgradePlanInfo = "Some upgrade info"

//
Address1, _ = sdk.AccAddressFromBech32("cosmos1s5xf3aanx7w84hgplk9z3l90qfpantg6nsmhpf")
Address2, _ = sdk.AccAddressFromBech32("cosmos1nl4uaesk9gtu7su3n89lne6xpa6lq8gljn79rq")
Address3, _ = sdk.AccAddressFromBech32("cosmos12r9vsus5js32pvnayt33zhcd4y9wcqcly45gr9")
Address4, _ = sdk.AccAddressFromBech32("cosmos1vvwldfef3yuggm7ge9p34d6dvpz5s74nus6n7g")
VendorID1 int32 = 1000
VendorID2 int32 = 2000
VendorID3 int32 = 3000
VendorID4 int32 = 4000
PubKey1 = strToPubKey(
Address1, _ = sdk.AccAddressFromBech32("cosmos1s5xf3aanx7w84hgplk9z3l90qfpantg6nsmhpf")
Address2, _ = sdk.AccAddressFromBech32("cosmos1nl4uaesk9gtu7su3n89lne6xpa6lq8gljn79rq")
Address3, _ = sdk.AccAddressFromBech32("cosmos12r9vsus5js32pvnayt33zhcd4y9wcqcly45gr9")
Address4, _ = sdk.AccAddressFromBech32("cosmos1vvwldfef3yuggm7ge9p34d6dvpz5s74nus6n7g")
VendorID1 int32 = 1000
VendorID2 int32 = 2000
VendorID3 int32 = 3000
VendorID4 int32 = 4000
ProductIDsEmpty []*types.Int32Range
ProductIDsFull = append([]*types.Int32Range{}, &types.Int32Range{Min: 1, Max: 65535})
ProductIDs100 = append([]*types.Int32Range{}, &types.Int32Range{Min: 1, Max: 100})
ProductIDs200 = append([]*types.Int32Range{}, &types.Int32Range{Min: 1, Max: 100}, &types.Int32Range{Min: 101, Max: 200})
PubKey1 = strToPubKey(
`{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"Aw1XXHQ8i6JVNKsFQ9eQArJVt2GXEO0EBFsQL6XJ5BxY"}`,
defEncConfig.Marshaler,
)
Expand Down
16 changes: 16 additions & 0 deletions integration_tests/grpc_rest/compliance/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ func CDCertificateIDUpdateChangesOnlyOneComplianceInfo(suite *utils.TestSuite) {
vendorName,
dclauthtypes.AccountRoles{dclauthtypes.Vendor},
vid,
testconstants.ProductIDsEmpty,
aliceName,
aliceAccount,
jackName,
Expand All @@ -538,6 +539,7 @@ func CDCertificateIDUpdateChangesOnlyOneComplianceInfo(suite *utils.TestSuite) {
certCenter,
dclauthtypes.AccountRoles{dclauthtypes.CertificationCenter},
1,
testconstants.ProductIDsEmpty,
aliceName,
aliceAccount,
jackName,
Expand Down Expand Up @@ -646,6 +648,7 @@ func DeleteComplianceInfoForAllCertStatuses(suite *utils.TestSuite) {
vendorName,
dclauthtypes.AccountRoles{dclauthtypes.Vendor},
vid,
testconstants.ProductIDsEmpty,
aliceName,
aliceAccount,
jackName,
Expand All @@ -661,6 +664,7 @@ func DeleteComplianceInfoForAllCertStatuses(suite *utils.TestSuite) {
certCenter,
dclauthtypes.AccountRoles{dclauthtypes.CertificationCenter},
1,
testconstants.ProductIDsEmpty,
aliceName,
aliceAccount,
jackName,
Expand Down Expand Up @@ -835,6 +839,7 @@ func DemoTrackCompliance(suite *utils.TestSuite) {
vendorName,
dclauthtypes.AccountRoles{dclauthtypes.Vendor},
vid,
testconstants.ProductIDsEmpty,
aliceName,
aliceAccount,
jackName,
Expand All @@ -850,6 +855,7 @@ func DemoTrackCompliance(suite *utils.TestSuite) {
certCenter,
dclauthtypes.AccountRoles{dclauthtypes.CertificationCenter},
1,
testconstants.ProductIDsEmpty,
aliceName,
aliceAccount,
jackName,
Expand Down Expand Up @@ -1162,6 +1168,7 @@ func DemoTrackRevocation(suite *utils.TestSuite) {
vendorName,
dclauthtypes.AccountRoles{dclauthtypes.Vendor},
vid,
testconstants.ProductIDsEmpty,
aliceName,
aliceAccount,
jackName,
Expand All @@ -1177,6 +1184,7 @@ func DemoTrackRevocation(suite *utils.TestSuite) {
certCenter,
dclauthtypes.AccountRoles{dclauthtypes.CertificationCenter},
1,
testconstants.ProductIDsEmpty,
aliceName,
aliceAccount,
jackName,
Expand Down Expand Up @@ -1339,6 +1347,7 @@ func DemoTrackProvision(suite *utils.TestSuite) {
vendorName,
dclauthtypes.AccountRoles{dclauthtypes.Vendor},
vid,
testconstants.ProductIDsEmpty,
aliceName,
aliceAccount,
jackName,
Expand All @@ -1354,6 +1363,7 @@ func DemoTrackProvision(suite *utils.TestSuite) {
certCenter,
dclauthtypes.AccountRoles{dclauthtypes.CertificationCenter},
1,
testconstants.ProductIDsEmpty,
aliceName,
aliceAccount,
jackName,
Expand Down Expand Up @@ -1682,6 +1692,7 @@ func DemoTrackComplianceWithHexVidAndPid(suite *utils.TestSuite) {
vendorName,
dclauthtypes.AccountRoles{dclauthtypes.Vendor},
vid,
testconstants.ProductIDsEmpty,
aliceName,
aliceAccount,
jackName,
Expand All @@ -1698,6 +1709,7 @@ func DemoTrackComplianceWithHexVidAndPid(suite *utils.TestSuite) {
certCenter,
dclauthtypes.AccountRoles{dclauthtypes.CertificationCenter},
1,
testconstants.ProductIDsEmpty,
aliceName,
aliceAccount,
jackName,
Expand Down Expand Up @@ -1791,6 +1803,7 @@ func DemoTrackRevocationWithHexVidAndPid(suite *utils.TestSuite) {
vendorName,
dclauthtypes.AccountRoles{dclauthtypes.Vendor},
vid,
testconstants.ProductIDsEmpty,
aliceName,
aliceAccount,
jackName,
Expand All @@ -1807,6 +1820,7 @@ func DemoTrackRevocationWithHexVidAndPid(suite *utils.TestSuite) {
certCenter,
dclauthtypes.AccountRoles{dclauthtypes.CertificationCenter},
1,
testconstants.ProductIDsEmpty,
aliceName,
aliceAccount,
jackName,
Expand Down Expand Up @@ -1924,6 +1938,7 @@ func DemoTrackProvisionByHexVidAndPid(suite *utils.TestSuite) {
vendorName,
dclauthtypes.AccountRoles{dclauthtypes.Vendor},
vid,
testconstants.ProductIDsEmpty,
aliceName,
aliceAccount,
jackName,
Expand All @@ -1939,6 +1954,7 @@ func DemoTrackProvisionByHexVidAndPid(suite *utils.TestSuite) {
certCenter,
dclauthtypes.AccountRoles{dclauthtypes.CertificationCenter},
1,
testconstants.ProductIDsEmpty,
aliceName,
aliceAccount,
jackName,
Expand Down
36 changes: 26 additions & 10 deletions integration_tests/grpc_rest/dclauth/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/stretchr/testify/require"
testconstants "github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/constants"
"github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/utils"
"github.com/zigbee-alliance/distributed-compliance-ledger/x/common/types"
dclauthtypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/dclauth/types"
modeltypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/model/types"
)
Expand Down Expand Up @@ -333,12 +334,13 @@ func ProposeAddAccount(
accKey cryptotypes.PubKey,
roles dclauthtypes.AccountRoles,
vendorID int32,
productIDs []*types.Int32Range,
signerName string,
signerAccount *dclauthtypes.Account,
info string,
) (*sdk.TxResponse, error) {
msg, err := dclauthtypes.NewMsgProposeAddAccount(
suite.GetAddress(signerName), accAddr, accKey, roles, vendorID, info)
suite.GetAddress(signerName), accAddr, accKey, roles, vendorID, productIDs, info)
require.NoError(suite.T, err)

return suite.BuildAndBroadcastTx([]sdk.Msg{msg}, signerName, signerAccount)
Expand Down Expand Up @@ -410,6 +412,7 @@ func CreateAccount(
accountName string,
roles dclauthtypes.AccountRoles,
vendorID int32,
productIDs []*types.Int32Range,
proposerName string,
proposerAccount *dclauthtypes.Account,
approverName string,
Expand All @@ -424,6 +427,7 @@ func CreateAccount(
accountInfo.GetPubKey(),
roles,
vendorID,
productIDs,
proposerName,
proposerAccount,
info,
Expand All @@ -450,6 +454,7 @@ func CreateVendorAccount(
accountName string,
roles dclauthtypes.AccountRoles,
vendorID int32,
productIDs []*types.Int32Range,
proposerName string,
proposerAccount *dclauthtypes.Account,
approverName string,
Expand All @@ -464,6 +469,7 @@ func CreateVendorAccount(
accountInfo.GetPubKey(),
roles,
vendorID,
productIDs,
proposerName,
proposerAccount,
info,
Expand Down Expand Up @@ -565,7 +571,7 @@ func AuthDemo(suite *utils.TestSuite) {
_, err = ProposeAddAccount(
suite,
testAccAddr, testAccPubKey,
dclauthtypes.AccountRoles{dclauthtypes.NodeAdmin}, 0,
dclauthtypes.AccountRoles{dclauthtypes.NodeAdmin}, 0, testconstants.ProductIDsEmpty,
jackName, jackAccount,
testconstants.Info,
)
Expand Down Expand Up @@ -718,7 +724,7 @@ func AuthDemo(suite *utils.TestSuite) {
_, err = ProposeAddAccount(
suite,
testAccAddr, testAccPubKey,
dclauthtypes.AccountRoles{dclauthtypes.NodeAdmin}, 0,
dclauthtypes.AccountRoles{dclauthtypes.NodeAdmin}, 0, testconstants.ProductIDsEmpty,
jackName, jackAccount,
testconstants.Info,
)
Expand Down Expand Up @@ -823,7 +829,7 @@ func AuthDemo(suite *utils.TestSuite) {
_, err = ProposeAddAccount(
suite,
testAccAddr, testAccPubKey,
dclauthtypes.AccountRoles{dclauthtypes.NodeAdmin}, 0,
dclauthtypes.AccountRoles{dclauthtypes.NodeAdmin}, 0, testconstants.ProductIDsEmpty,
jackName, jackAccount,
testconstants.Info,
)
Expand Down Expand Up @@ -964,7 +970,8 @@ func AuthDemo(suite *utils.TestSuite) {
_, err = ProposeAddAccount(
suite,
testAccAddr, testAccPubKey,
dclauthtypes.AccountRoles{dclauthtypes.Vendor, dclauthtypes.NodeAdmin}, testconstants.Vid,
dclauthtypes.AccountRoles{dclauthtypes.Vendor, dclauthtypes.NodeAdmin},
testconstants.Vid, testconstants.ProductIDsEmpty,
jackName, jackAccount,
testconstants.Info,
)
Expand All @@ -987,6 +994,7 @@ func AuthDemo(suite *utils.TestSuite) {
require.NoError(suite.T, err)
require.Equal(suite.T, testAccAddr, testProposedVendorAccount.GetAddress())
require.Equal(suite.T, []dclauthtypes.AccountRole{dclauthtypes.Vendor, dclauthtypes.NodeAdmin}, testProposedVendorAccount.GetRoles())
require.Equal(suite.T, []*types.Int32Range{}, testProposedVendorAccount.GetProductIDs())

// Alice approves new account
_, err = ApproveAddAccount(suite, testAccAddr, aliceName, aliceAccount, testconstants.Info)
Expand Down Expand Up @@ -1037,7 +1045,8 @@ func AuthDemo(suite *utils.TestSuite) {
_, err = ProposeAddAccount(
suite,
testAccAddr, testAccPubKey,
dclauthtypes.AccountRoles{dclauthtypes.Vendor}, testconstants.Vid,
dclauthtypes.AccountRoles{dclauthtypes.Vendor},
testconstants.Vid, testconstants.ProductIDsEmpty,
jackName, jackAccount,
testconstants.Info,
)
Expand All @@ -1052,6 +1061,7 @@ func AuthDemo(suite *utils.TestSuite) {
require.NoError(suite.T, err)
require.Equal(suite.T, testAccAddr, testVendorAccount.GetAddress())
require.Equal(suite.T, []dclauthtypes.AccountRole{dclauthtypes.Vendor}, testVendorAccount.GetRoles())
require.Equal(suite.T, []*types.Int32Range{}, testVendorAccount.GetProductIDs())

// Query all proposed accounts
receivedProposedAccounts, _ = GetProposedAccounts(suite)
Expand All @@ -1072,7 +1082,8 @@ func AuthDemo(suite *utils.TestSuite) {
_, err = ProposeAddAccount(
suite,
testAccAddr, testAccPubKey,
dclauthtypes.AccountRoles{dclauthtypes.Trustee}, 0,
dclauthtypes.AccountRoles{dclauthtypes.Trustee},
0, testconstants.ProductIDsEmpty,
jackName, jackAccount,
testconstants.Info,
)
Expand All @@ -1095,7 +1106,8 @@ func AuthDemo(suite *utils.TestSuite) {
_, err = ProposeAddAccount(
suite,
testAccAddr, testAccPubKey,
dclauthtypes.AccountRoles{dclauthtypes.Trustee}, 0,
dclauthtypes.AccountRoles{dclauthtypes.Trustee},
0, testconstants.ProductIDsEmpty,
jackName, jackAccount,
testconstants.Info,
)
Expand Down Expand Up @@ -1146,7 +1158,8 @@ func AuthDemo(suite *utils.TestSuite) {
_, err = ProposeAddAccount(
suite,
testAccAddr, testAccPubKey,
dclauthtypes.AccountRoles{dclauthtypes.Vendor}, testconstants.Vid,
dclauthtypes.AccountRoles{dclauthtypes.Vendor},
testconstants.Vid, testconstants.ProductIDsFull,
jackName, jackAccount,
testconstants.Info,
)
Expand Down Expand Up @@ -1177,7 +1190,8 @@ func AuthDemo(suite *utils.TestSuite) {
_, err = ProposeAddAccount(
suite,
testAccAddr, testAccPubKey,
dclauthtypes.AccountRoles{dclauthtypes.Vendor}, testconstants.Vid,
dclauthtypes.AccountRoles{dclauthtypes.Vendor},
testconstants.Vid, testconstants.ProductIDsFull,
jackName, jackAccount,
testconstants.Info,
)
Expand All @@ -1200,6 +1214,7 @@ func AuthDemo(suite *utils.TestSuite) {
require.NoError(suite.T, err)
require.Equal(suite.T, testAccAddr, testProposedVendorAccount.GetAddress())
require.Equal(suite.T, []dclauthtypes.AccountRole{dclauthtypes.Vendor}, testProposedVendorAccount.GetRoles())
require.Equal(suite.T, []*types.Int32Range{{Min: 1, Max: 65535}}, testVendorAccount.GetProductIDs())

// Alice approves new account
_, err = ApproveAddAccount(suite, testAccAddr, aliceName, aliceAccount, testconstants.Info)
Expand All @@ -1218,4 +1233,5 @@ func AuthDemo(suite *utils.TestSuite) {
require.NoError(suite.T, err)
require.Equal(suite.T, testAccAddr, testVendorAccount.GetAddress())
require.Equal(suite.T, []dclauthtypes.AccountRole{dclauthtypes.Vendor}, testVendorAccount.GetRoles())
require.Equal(suite.T, []*types.Int32Range{{Min: 1, Max: 65535}}, testVendorAccount.GetProductIDs())
}
2 changes: 2 additions & 0 deletions integration_tests/grpc_rest/dclupgrade/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ func ProposeUpgradeByNonTrustee(suite *utils.TestSuite) {
dclauthtypes.NodeAdmin,
},
int32(tmrand.Uint16()+1),
testconstants.ProductIDsEmpty,
aliceName,
aliceAccount,
bobName,
Expand Down Expand Up @@ -490,6 +491,7 @@ func ApproveUpgradeByNonTrustee(suite *utils.TestSuite) {
dclauthtypes.NodeAdmin,
},
int32(tmrand.Uint16()+1),
testconstants.ProductIDsEmpty,
aliceName,
aliceAccount,
bobName,
Expand Down
5 changes: 5 additions & 0 deletions integration_tests/grpc_rest/model/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func TestAddModelByNonVendorGRPC(t *testing.T) {
model.AddModelByNonVendor(&suite)
}

func TestAddModelByVendorWithNonAssociatedProductIdsGRPC(t *testing.T) {
suite := utils.SetupTest(t, testconstants.ChainID, false)
model.AddModelByVendorWithNonAssociatedProductIds(&suite)
}

func TestAddModelByDifferentVendorGRPC(t *testing.T) {
suite := utils.SetupTest(t, testconstants.ChainID, false)
model.AddModelByDifferentVendor(&suite)
Expand Down
Loading

0 comments on commit dd93905

Please sign in to comment.