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: generic access control #316

Merged
merged 73 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
410eae3
wip: remove delegation, abstract ac
weichweich Jan 12, 2022
f3bd66a
wip: generic ac in attestations
weichweich Jan 13, 2022
3ac6c2f
wip: allow for parameter in ac
weichweich Jan 18, 2022
8a78bf8
remove runtime dependency from pallet
weichweich Jan 18, 2022
83660d1
add weight
weichweich Jan 19, 2022
db479e8
remove runtime dependency from delegations
weichweich Jan 19, 2022
39e801f
allow for different types than in Config
weichweich Jan 20, 2022
efb6659
impl AttestationAccessControl for delegation
weichweich Jan 20, 2022
c4e2b1f
impl AttestationAccessControl in runtime
weichweich Jan 20, 2022
5405fb1
Merge remote-tracking branch 'origin/develop' into aw-delegation-rework
weichweich Jan 20, 2022
6c8a255
Merge branch 'develop' into aw-delegation-rework
weichweich Jan 20, 2022
12f9f68
don't pass the attestation
weichweich Jan 24, 2022
fd11f3c
Merge remote-tracking branch 'origin/develop' into aw-delegation-rework
weichweich Jan 24, 2022
be8a359
add weight calculation
weichweich Jan 24, 2022
27f1f63
tests: attestations
weichweich Jan 24, 2022
9cb1b90
fix: benchmarks
weichweich Jan 25, 2022
38f4b20
bench: delegation
weichweich Jan 25, 2022
94c6bde
fmt
weichweich Jan 25, 2022
988b43b
cargo run --quiet --release -p kilt-parachain --features=runtime-benc…
Jan 25, 2022
6946c2f
Revert "cargo run --quiet --release -p kilt-parachain --features=runt…
weichweich Jan 25, 2022
7fb5ae4
bench: remove unused parameter
weichweich Jan 25, 2022
743cfb8
cargo run --quiet --release -p kilt-parachain --features=runtime-benc…
Jan 25, 2022
2ab2a2c
cargo run --quiet --release -p kilt-parachain --features=runtime-benc…
Jan 25, 2022
a5cf8c9
cargo run --quiet --release -p kilt-parachain --features=runtime-benc…
Jan 25, 2022
b97935c
🧹🐢
weichweich Jan 25, 2022
7451672
🧹🐢
weichweich Jan 25, 2022
8533e02
refactor: move delegationAc to mod
weichweich Jan 27, 2022
f963bfc
fix: saturating add
weichweich Jan 27, 2022
466df1d
refactor: move AttestationAccessControl to mod
weichweich Jan 27, 2022
a48fa14
style: NL for the devil
weichweich Jan 27, 2022
89800d9
style: rename to `attestation_auth_id`
weichweich Jan 27, 2022
7eec4c5
doc: better words
weichweich Jan 27, 2022
0edb891
add ctype and claimhash
weichweich Jan 27, 2022
0d69014
refactor: tuple struct to struct, split weight
weichweich Jan 31, 2022
3f03b7b
test: can_attest
weichweich Jan 31, 2022
b85f1c2
test: test_can_revoke_same_node
weichweich Feb 1, 2022
c3193b5
Merge remote-tracking branch 'origin/develop' into aw-delegation-rework
weichweich Feb 11, 2022
ef81ac4
test: add missing tests
weichweich Feb 11, 2022
8a1d0a0
Merge branch 'develop' into aw-delegation-rework
weichweich Feb 15, 2022
18595ff
fmt, rename, cleanup
weichweich Feb 15, 2022
4e4272d
external attestations
weichweich Feb 15, 2022
8685699
fix benchmark
weichweich Feb 15, 2022
bfdaf2c
clippy
weichweich Feb 15, 2022
897e189
cargo run --quiet --release -p kilt-parachain --features=runtime-benc…
Feb 16, 2022
ed465dc
Merge remote-tracking branch 'origin/develop' into aw-delegation-rework
weichweich Feb 17, 2022
70c0fe8
cargo run --quiet --release -p kilt-parachain --features=runtime-benc…
Feb 17, 2022
e85e85b
cargo run --quiet --release -p kilt-parachain --features=runtime-benc…
Feb 17, 2022
1016572
remove unused and restrict visibility
weichweich Feb 21, 2022
351e9e4
remove default implementation
weichweich Feb 21, 2022
4a49987
Update pallets/attestation/src/tests.rs
weichweich Feb 21, 2022
c99455b
Update pallets/delegation/src/access_control.rs
weichweich Feb 21, 2022
c07b421
Update pallets/delegation/src/access_control.rs
weichweich Feb 21, 2022
90764e1
Update pallets/delegation/src/access_control.rs
weichweich Feb 21, 2022
edb4b94
Update pallets/delegation/src/access_control.rs
weichweich Feb 21, 2022
9b16948
Update pallets/attestation/src/tests.rs
weichweich Feb 21, 2022
e1d37ce
doc: add comment
weichweich Feb 21, 2022
c5fec0c
sender -> subject
weichweich Feb 21, 2022
e675ecc
standalone AC
weichweich Feb 21, 2022
2a44afb
Merge remote-tracking branch 'origin/develop' into aw-delegation-rework
weichweich Feb 22, 2022
f4ba653
MaxEncodedLen
weichweich Feb 22, 2022
b85d393
cargo run --quiet --release -p kilt-parachain --features=runtime-benc…
Feb 22, 2022
4f55174
cargo run --quiet --release -p kilt-parachain --features=runtime-benc…
Feb 22, 2022
4290c3a
cargo run --quiet --release -p kilt-parachain --features=runtime-benc…
Feb 22, 2022
2730352
Merge branch 'develop' into aw-delegation-rework
weichweich Feb 23, 2022
1acaae9
Merge remote-tracking branch 'origin/develop' into aw-delegation-rework
weichweich Mar 1, 2022
daf5803
add test to ensure no migration is needed
weichweich Mar 1, 2022
e0be5f4
fix benchmarks and attestation size.
weichweich Mar 1, 2022
8c3f594
storage is never used, no migration needed
weichweich Mar 1, 2022
349119e
Merge branch 'develop' into aw-delegation-rework
weichweich Mar 9, 2022
37c5771
Merge branch 'develop' into aw-delegation-rework
weichweich Mar 16, 2022
665cfda
Merge branch 'develop' into aw-delegation-rework
weichweich Mar 16, 2022
5c5cf6c
Merge branch 'develop' into aw-delegation-rework
weichweich Mar 16, 2022
fffd0e1
🧹🐢
weichweich Mar 17, 2022
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
6 changes: 2 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 2 additions & 8 deletions pallets/attestation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ substrate-wasm-builder-runner = {version = "3.0.0"}

[dev-dependencies]
ctype = {features = ["mock"], path = "../ctype"}
delegation = {features = ["mock"], path = "../delegation"}
runtime-common = {default-features = false, path = "../../runtimes/common"}
kilt-support = {features = ["mock"], path = "../../support"}

pallet-balances = {branch = "polkadot-v0.9.17", default-features = false, git = "https://github.com/paritytech/substrate"}
serde = {version = "1.0.132"}
Expand All @@ -31,9 +30,7 @@ serde = {optional = true, version = "1.0.132"}

# Internal dependencies
ctype = {default-features = false, path = "../ctype"}
delegation = {default-features = false, path = "../delegation"}
kilt-support = {default-features = false, path = "../../support"}
runtime-common = {default-features = false, optional = true, path = "../../runtimes/common"}

#External dependencies
frame-benchmarking = {branch = "polkadot-v0.9.17", default-features = false, git = "https://github.com/paritytech/substrate", optional = true}
Expand All @@ -49,27 +46,24 @@ sp-std = {branch = "polkadot-v0.9.17", default-features = false, git = "https://
[features]
default = ["std"]
mock = [
"runtime-common",
"pallet-balances",
"serde",
"sp-core",
"sp-io",
"sp-keystore",
]
runtime-benchmarks = [
"delegation/runtime-benchmarks",
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"kilt-support/runtime-benchmarks",
"sp-core",
]
std = [
"codec/std",
"ctype/std",
"delegation/std",
"frame-support/std",
"frame-system/std",
"runtime-common/std",
"kilt-support/std",
"log/std",
"pallet-balances/std",
Expand Down
105 changes: 105 additions & 0 deletions pallets/attestation/src/access_control.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// KILT Blockchain – https://botlabs.org
// Copyright (C) 2019-2022 BOTLabs GmbH

// The KILT Blockchain is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The KILT Blockchain is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

// If you feel like getting in touch with us, you can do so at info@botlabs.org

use frame_support::dispatch::Weight;
use sp_runtime::DispatchError;

/// Allow for more complex schemes on who can attest, revoke and remove.
pub trait AttestationAccessControl<AttesterId, AuthorizationId, Ctype, ClaimHash> {
/// Decides whether the account is allowed to attest with the given
/// information provided by the sender (&self).
fn can_attest(&self, who: &AttesterId, ctype: &Ctype, claim: &ClaimHash) -> Result<Weight, DispatchError>;

/// Decides whether the account is allowed to revoke the attestation with
/// the `authorization_id` and the access information provided by the sender
/// (&self).
fn can_revoke(
&self,
who: &AttesterId,
ctype: &Ctype,
claim: &ClaimHash,
authorization_id: &AuthorizationId,
) -> Result<Weight, DispatchError>;

/// Decides whether the account is allowed to remove the attestation with
/// the `authorization_id` and the access information provided by the sender
/// (&self).
fn can_remove(
&self,
who: &AttesterId,
ctype: &Ctype,
claim: &ClaimHash,
authorization_id: &AuthorizationId,
) -> Result<Weight, DispatchError>;

/// The authorization ID that the sender provided. This will be used for new
/// attestations.
///
/// NOTE: This method must not read storage or do any heavy computation
/// since it's not covered by the weight returned by `self.weight()`.
fn authorization_id(&self) -> AuthorizationId;

/// The worst-case weight of `can_attest`.
fn can_attest_weight(&self) -> Weight;

/// The worst-case weight of `can_revoke`.
fn can_revoke_weight(&self) -> Weight;

/// The worst-case weight of `can_remove`.
fn can_remove_weight(&self) -> Weight;
}

impl<AttesterId, AuthorizationId, Ctype, ClaimHash>
AttestationAccessControl<AttesterId, AuthorizationId, Ctype, ClaimHash> for ()
where
AuthorizationId: Default,
{
fn can_attest(&self, _who: &AttesterId, _ctype: &Ctype, _claim: &ClaimHash) -> Result<Weight, DispatchError> {
Err(DispatchError::Other("Unimplemented"))
}
fn can_revoke(
&self,
_who: &AttesterId,
_ctype: &Ctype,
_claim: &ClaimHash,
_authorization_id: &AuthorizationId,
) -> Result<Weight, DispatchError> {
Err(DispatchError::Other("Unimplemented"))
}
fn can_remove(
&self,
_who: &AttesterId,
_ctype: &Ctype,
_claim: &ClaimHash,
_authorization_id: &AuthorizationId,
) -> Result<Weight, DispatchError> {
Err(DispatchError::Other("Unimplemented"))
}
fn authorization_id(&self) -> AuthorizationId {
Default::default()
}
fn can_attest_weight(&self) -> Weight {
0
}
fn can_revoke_weight(&self) -> Weight {
0
}
fn can_remove_weight(&self) -> Weight {
0
}
}
60 changes: 57 additions & 3 deletions pallets/attestation/src/attestations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@

use codec::{Decode, Encode, MaxEncodedLen};
use ctype::CtypeHashOf;
use delegation::DelegationNodeIdOf;
use kilt_support::deposit::Deposit;
use scale_info::TypeInfo;

use crate::{AccountIdOf, AttesterOf, BalanceOf, Config};
use crate::{AccountIdOf, AttesterOf, AuthorizationIdOf, BalanceOf, Config};

/// An on-chain attestation written by an attester.
#[derive(Clone, Debug, Encode, Decode, PartialEq, TypeInfo, MaxEncodedLen)]
Expand All @@ -35,10 +34,65 @@ pub struct AttestationDetails<T: Config> {
pub attester: AttesterOf<T>,
/// \[OPTIONAL\] The ID of the delegation node used to authorize the
/// attester.
pub delegation_id: Option<DelegationNodeIdOf<T>>,
pub authorization_id: Option<AuthorizationIdOf<T>>,
/// The flag indicating whether the attestation has been revoked or not.
pub revoked: bool,
/// The deposit that was taken to incentivise fair use of the on chain
/// storage.
pub deposit: Deposit<AccountIdOf<T>, BalanceOf<T>>,
}

#[cfg(test)]
mod tests {
use super::*;
use crate::mock::*;

/// Old Attestation
#[derive(Clone, Debug, Encode, Decode, PartialEq, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
#[codec(mel_bound())]
pub struct OldAttestationDetails<T: Config> {
/// The hash of the CType used for this attestation.
pub ctype_hash: CtypeHashOf<T>,
/// The ID of the attester.
pub attester: AttesterOf<T>,
/// \[OPTIONAL\] The ID of the delegation node used to authorize the
/// attester.
pub delegation_id: Option<[u8; 32]>,
/// The flag indicating whether the attestation has been revoked or not.
pub revoked: bool,
/// The deposit that was taken to incentivise fair use of the on chain
/// storage.
pub deposit: Deposit<AccountIdOf<T>, BalanceOf<T>>,
}

#[test]
fn test_no_need_to_migrate_if_none() {
let old = OldAttestationDetails::<Test> {
ctype_hash: claim_hash_from_seed(CLAIM_HASH_SEED_01),
attester: sr25519_did_from_seed(&ALICE_SEED),
delegation_id: None,
revoked: true,
deposit: Deposit {
owner: ACCOUNT_00,
amount: ATTESTATION_DEPOSIT,
},
};
let encoded = old.encode();

let new = AttestationDetails::<Test>::decode(&mut &encoded[..]);
assert_eq!(
new,
Ok(AttestationDetails::<Test> {
ctype_hash: claim_hash_from_seed(CLAIM_HASH_SEED_01),
attester: sr25519_did_from_seed(&ALICE_SEED),
authorization_id: None,
revoked: true,
deposit: Deposit {
owner: ACCOUNT_00,
amount: ATTESTATION_DEPOSIT,
},
})
);
}
}
Loading