Skip to content
Merged
25 changes: 18 additions & 7 deletions common/primitives/src/msa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ impl From<DelegatorId> for MessageSourceId {
#[cfg_attr(feature = "std", derive(Deserialize, Serialize))]
#[derive(TypeInfo, RuntimeDebug, Clone, Decode, Encode, MaxEncodedLen, Eq)]
pub struct DelegationResponse<DelegationIdType, BlockNumber> {
/// SchemaId of schema for which permission is/was granted
/// Provider ID for which permission is/was granted
pub provider_id: ProviderId,
/// The list of schema permissions grants
/// The list of permissions grants
pub permissions: Vec<DelegationGrant<DelegationIdType, BlockNumber>>,
/// Block number at which permission for ALL grants was revoked
pub revoked_at: BlockNumber,
}

/// RPC response for getting schema permission grants
Expand All @@ -92,8 +94,10 @@ pub struct DelegationResponse<DelegationIdType, BlockNumber> {
pub struct DelegationGrant<DelegationIdType, BlockNumber> {
/// ID of the delegated entity for which permission is/was granted
pub granted_id: DelegationIdType,
/// Block number the permission was/will be revoked (0 = not revoked)
/// Block number when the permission was/will be EFFECTIVELY revoked, taking into consideration the overall provider delegation revocation.
pub revoked_at: BlockNumber,
/// Block number the permission was/will be explicitly revoked (i.e., not implicitly revoked by a top-level revocation) (0 = not revoked)
pub explicit_revoked_at: BlockNumber,
Comment on lines +97 to +100
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opted to add new explicit_revoked_at instead of effective_revoked_at, because revoked_at in this context has historically ALWAYS meant the effective revocation block.

}

// Custom serialization implementation to allow for a slow roll-out of renaming
Expand All @@ -111,6 +115,7 @@ impl<DelegationIdType: Serialize, BlockNumber: Serialize> Serialize
s.serialize_field("schema_id", &self.granted_id)?;
s.serialize_field("granted_id", &self.granted_id)?;
s.serialize_field("revoked_at", &self.revoked_at)?;
s.serialize_field("explicit_revoked_at", &self.explicit_revoked_at)?;
Comment thread
JoeCap08055 marked this conversation as resolved.
s.end()
}
}
Expand All @@ -126,9 +131,13 @@ where
}

impl<DelegationIdType, BlockNumber> DelegationGrant<DelegationIdType, BlockNumber> {
/// Create a new SchemaGrant struct
pub fn new(granted_id: DelegationIdType, revoked_at: BlockNumber) -> Self {
DelegationGrant { granted_id, revoked_at }
/// Create a new DelegationGrant struct
pub fn new(
granted_id: DelegationIdType,
revoked_at: BlockNumber,
explicit_revoked_at: BlockNumber,
) -> Self {
DelegationGrant { granted_id, revoked_at, explicit_revoked_at }
}
}

Expand All @@ -138,7 +147,9 @@ where
BlockNumber: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.granted_id == other.granted_id && self.revoked_at == other.revoked_at
self.granted_id == other.granted_id &&
self.revoked_at == other.revoked_at &&
self.explicit_revoked_at == other.explicit_revoked_at
}
}

Expand Down
91 changes: 59 additions & 32 deletions e2e/scenarios/grantDelegation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ describe('Delegation Scenario Tests', function () {
assert.deepEqual(grantDelegationEvent?.data.delegatorId, msaId, 'delegator IDs should match');
});

it('initial granted intents should be correct', async function () {
it('initial granted intents should be correct via RPC (deprecated)', async function () {
const intentGrants = await ExtrinsicHelper.apiPromise.rpc.msa.grantedSchemaIdsByMsaId(msaId, providerId);
assert.equal(intentGrants.isSome, true);
const intentIds = intentGrants
Expand All @@ -166,6 +166,21 @@ describe('Delegation Scenario Tests', function () {
assert.deepStrictEqual(intentIds, expectedIntentIds, 'granted intents should equal initial set');
});

it('initial granted intents should be correct via Runtime API', async function () {
const response = await ExtrinsicHelper.apiPromise.call.msaRuntimeApi.getDelegationForMsaAndProvider(
msaId,
providerId
);
assert.equal(response.isSome, true, 'response should be valid');
const delegation = response.unwrap();
assert.equal(delegation.revokedAt.toNumber(), 0, 'delegation should not be revoked');
const intentIds = delegation.permissions
.filter((grant) => grant.revokedAt.toBigInt() === 0n)
.map((grant) => grant.grantedId.toNumber());
const expectedIntentIds = [intentId.toNumber()];
assert.deepStrictEqual(intentIds, expectedIntentIds, 'granted intents should equal initial set');
});

it('should grant additional intent permissions', async function () {
const payload = await generateDelegationPayload({
authorizedMsaId: providerId,
Expand Down Expand Up @@ -193,39 +208,51 @@ describe('Delegation Scenario Tests', function () {
assert.deepStrictEqual(grantedIntentIds.sort(), expectedIntentIds.sort());
});

it('should get all delegations and grants', async function () {
const payload = await generateDelegationPayload({
authorizedMsaId: providerId,
intentIds: [intentId, intentId2],
describe('multiple provider grants for an MSA', function () {
before(async function () {
const payload = await generateDelegationPayload({
authorizedMsaId: providerId,
intentIds: [intentId, intentId2],
});
const addProviderData = ExtrinsicHelper.api.registry.createType('PalletMsaAddProvider', payload);
const grantDelegationOp = ExtrinsicHelper.grantDelegation(
thirdMsaKeys,
providerKeys,
signPayloadSr25519(thirdMsaKeys, addProviderData),
payload
);
const { target: grantDelegationEvent } = await grantDelegationOp.fundAndSend(fundingSource);
assert.notEqual(grantDelegationEvent, undefined, 'should have returned DelegationGranted event');

const payload2 = await generateDelegationPayload({
authorizedMsaId: otherProviderId,
intentIds: [intentId, intentId2],
});
const addProviderData2 = ExtrinsicHelper.api.registry.createType('PalletMsaAddProvider', payload2);
const grantDelegationOp2 = ExtrinsicHelper.grantDelegation(
thirdMsaKeys,
otherProviderKeys,
signPayloadSr25519(thirdMsaKeys, addProviderData2),
payload2
);
const { target: grantDelegationEvent2 } = await grantDelegationOp2.fundAndSend(fundingSource);
assert.notEqual(grantDelegationEvent2, undefined, 'should have returned DelegationGranted event');
});
const addProviderData = ExtrinsicHelper.api.registry.createType('PalletMsaAddProvider', payload);
const grantDelegationOp = ExtrinsicHelper.grantDelegation(
thirdMsaKeys,
providerKeys,
signPayloadSr25519(thirdMsaKeys, addProviderData),
payload
);
const { target: grantDelegationEvent } = await grantDelegationOp.fundAndSend(fundingSource);
assert.notEqual(grantDelegationEvent, undefined, 'should have returned DelegationGranted event');

const payload2 = await generateDelegationPayload({
authorizedMsaId: otherProviderId,
intentIds: [intentId, intentId2],
it('should get all delegations and grants via RPC (deprecated)', async function () {
const grants = await ExtrinsicHelper.apiPromise.rpc.msa.getAllGrantedDelegationsByMsaId(thirdMsaId);
assert.deepStrictEqual(grants.length, 2);
const expectedProviderIds = [providerId.toNumber(), otherProviderId.toNumber()];
assert(expectedProviderIds.indexOf(grants[0].provider_id.toNumber()) !== -1);
assert(expectedProviderIds.indexOf(grants[1].provider_id.toNumber()) !== -1);
});

it('should get all delegations and grants via Runtime API', async function () {
const grants = await ExtrinsicHelper.apiPromise.call.msaRuntimeApi.getAllGrantedDelegationsByMsaId(thirdMsaId);
assert.deepStrictEqual(grants.length, 2);
const expectedProviderIds = [providerId.toNumber(), otherProviderId.toNumber()];
assert(expectedProviderIds.indexOf(grants[0].providerId.toNumber()) !== -1);
assert(expectedProviderIds.indexOf(grants[1].providerId.toNumber()) !== -1);
});
const addProviderData2 = ExtrinsicHelper.api.registry.createType('PalletMsaAddProvider', payload2);
const grantDelegationOp2 = ExtrinsicHelper.grantDelegation(
thirdMsaKeys,
otherProviderKeys,
signPayloadSr25519(thirdMsaKeys, addProviderData2),
payload2
);
const { target: grantDelegationEvent2 } = await grantDelegationOp2.fundAndSend(fundingSource);
assert.notEqual(grantDelegationEvent2, undefined, 'should have returned DelegationGranted event');

const grants = await ExtrinsicHelper.apiPromise.rpc.msa.getAllGrantedDelegationsByMsaId(thirdMsaId);
assert.deepStrictEqual(grants.length, 2);
const expectedProviderIds = [providerId.toNumber(), otherProviderId.toNumber()];
assert(expectedProviderIds.indexOf(grants[0].provider_id.toNumber()) !== -1);
assert(expectedProviderIds.indexOf(grants[1].provider_id.toNumber()) !== -1);
});
});
2 changes: 1 addition & 1 deletion e2e/scenarios/grantDelegationRevoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('Delegation Scenario Tests: Revocation', function () {
revokedAtBlock = await getBlockNumber();
});

it('revoked delegation should be reflected in all previously-granted schema permissions', async function () {
it('revoked delegation should be reflected in all previously-granted permissions', async function () {
// Make a block first to make sure the state has rolled to the next block
const currentBlock = await getBlockNumber();
await ExtrinsicHelper.runToBlock(currentBlock + 1);
Expand Down
9 changes: 8 additions & 1 deletion js/api-augment/definitions/msa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,17 @@ export default {
SchemaGrantResponse: {
schema_id: 'IntentId',
revoked_at: 'BlockNumber',
explicit_revoked_at: 'BlockNumber',
Comment thread
JoeCap08055 marked this conversation as resolved.
},
DelegationGrant: {
granted_id: 'IntentId',
revoked_at: 'BlockNumber',
explicit_revoked_at: 'BlockNumber',
},
DelegationResponse: {
provider_id: 'ProviderId',
permissions: 'Vec<SchemaGrantResponse>',
permissions: 'Vec<DelegationGrant>',
revoked_at: 'BlockNumber',
},
// Runtime types
// Not sure why these have to be noted here, but they do
Expand Down
12 changes: 6 additions & 6 deletions pallets/messages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ See [Rust Docs](https://frequency-chain.github.io/frequency/pallet_messages/pall

| Name | Description | Query | Runtime Added |
|------------|-------------------------------------------------------------------------------------------------------------------------------------------------------|--------------|---------------|
| MessagesV3 | Suggested: Use custom runtime API instead of querying this storage directly.<br/>Storage for the messages by Block Number, IntentId, and MessageIndex | `messagesV3` | ? |
| MessagesV2 | Removed in Runtime ?? | `messagesV2` | 61 |
| MessagesV3 | Suggested: Use custom runtime API instead of querying this storage directly.<br/>Storage for the messages by Block Number, IntentId, and MessageIndex | `messagesV3` | 184 |
| MessagesV2 | Removed in Runtime 184 | `messagesV2` | 61 |
| Messages | Removed in Runtime 60 | `messages` | 1-60 |

See the [Rust Docs](https://frequency-chain.github.io/frequency/pallet_messages/pallet/storage_types/index.html) for additional state queries and details.
Expand All @@ -59,9 +59,9 @@ See the [Rust Docs](https://frequency-chain.github.io/frequency/pallet_messages/

Note: May be restricted based on node settings and configuration.

| Name | Description | Call | Node Version |
|------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------|--------------|
| Get Messages by Schema Id _(deprecated)_ | Fetch paginated messages for a specific Schema Id in the given block range for a given Schema Id<br/>Deprecated in `v1.17.?`. Use custom Runtime API `get_messages_by_intent_id` instead | [`getBySchemaId`](https://frequency-chain.github.io/frequency/pallet_messages_rpc/trait.MessagesApiServer.html#tymethod.get_messages_by_schema_id) | v1.0.0+ |
| Name | Description | Call | Node Version |
|------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------|--------------|
| Get Messages by Schema Id _(deprecated)_ | Fetch paginated messages for a specific Schema Id in the given block range for a given Schema Id<br/>Deprecated in `v2.0.0`. Use custom Runtime API `get_messages_by_intent_id` instead | [`getBySchemaId`](https://frequency-chain.github.io/frequency/pallet_messages_rpc/trait.MessagesApiServer.html#tymethod.get_messages_by_schema_id) | v1.0.0+ |

See [Rust Docs](https://frequency-chain.github.io/frequency/pallet_messages_rpc/trait.MessagesApiServer.html) for more details.

Expand All @@ -71,7 +71,7 @@ See [Rust Docs](https://frequency-chain.github.io/frequency/pallet_messages_rpc/
|-------------------------------------------------|---------------------------------------------------------------------------|-------------------------------|-------------------|---------------|
| Get Schema by Id _(deprecated)_ | Retrieves the schema for the given Schema Id | `getBySchemaId` | 1 | 1 |
| Get Messages by Schema and Block _(deprecated)_ | Retrieve the messages for a particular schema and block number | `getMessagesBySchemaAndBlock` | 1 | 1 |
| Get Messages by Intent and Block | Retrieve the messages for a particular intent and block range (paginated) | `getMessagesByIntentId` | 2 | ? |
| Get Messages by Intent and Block | Retrieve the messages for a particular intent and block range (paginated) | `getMessagesByIntentId` | 2 | 184 |

See [Rust Docs](https://frequency-chain.github.io/frequency/pallet_messages_runtime_api/trait.MessagesRuntimeApi.html) for
more details.
Loading