Skip to content

Commit 6464fd4

Browse files
JesseAbramameba23
andauthored
Add outtie pallet (#1418)
* Add outtie pallet * compiles * add box * test * add event * add benchmarks * add to runtime * inline docs * fmt * changelog * Apply suggestions from code review Co-authored-by: peg <[email protected]> * fixes * metadata --------- Co-authored-by: peg <[email protected]>
1 parent e944bf4 commit 6464fd4

File tree

13 files changed

+537
-0
lines changed

13 files changed

+537
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ At the moment this project **does not** adhere to
1212
### Breaking
1313
- In [#1387](https://github.com/entropyxyz/entropy-core/pull/1387/) the substrate version was updated, this could cause changes to the chainspec file as well requires a strategy for command line argument ```--public-addr``` and for handling ```node-key-generation```
1414

15+
### Added
16+
- Add outtie pallet ([#1418](https://github.com/entropyxyz/entropy-core/pull/1418))
17+
1518
### Changed
1619
- Update substrate to polkadot stable2409 ([#1387](https://github.com/entropyxyz/entropy-core/pull/1387))
1720
- Remove deadlines in OCW ([#1411](https://github.com/entropyxyz/entropy-core/pull/1411))

Cargo.lock

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/client/entropy_metadata.scale

863 Bytes
Binary file not shown.

pallets/outtie/Cargo.toml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
[package]
2+
name ="pallet-outtie"
3+
version ='0.4.0-rc.1'
4+
authors =['Entropy Cryptography <[email protected]>']
5+
homepage ='https://entropy.xyz/'
6+
license ='AGPL-3.0-or-later'
7+
repository='https://github.com/entropyxyz/entropy-core'
8+
edition ='2021'
9+
publish =false
10+
11+
[dependencies]
12+
codec ={ package="parity-scale-codec", version="3.6.3", default-features=false, features=["derive"] }
13+
scale-info={ version="2.11", default-features=false, features=["derive"] }
14+
15+
frame-benchmarking={ version="38.0.0", default-features=false, optional=true }
16+
frame-support ={ version="38.0.0", default-features=false }
17+
frame-system ={ version="38.0.0", default-features=false }
18+
sp-runtime ={ version="39.0.1", default-features=false }
19+
sp-std ={ version="14.0.0", default-features=false }
20+
pallet-session ={ version="38.0.0", default-features=false }
21+
serde ={ version="1.0.219", default-features=false }
22+
23+
entropy-shared={ version="0.4.0-rc.1", path="../../crates/shared", features=[
24+
"wasm-no-std",
25+
], default-features=false }
26+
27+
[dev-dependencies]
28+
sp-core ={ version="34.0.0" }
29+
sp-io ={ version="38.0.0" }
30+
sp-staking={ version="36.0.0", default-features=false }
31+
32+
[features]
33+
default=["std"]
34+
runtime-benchmarks=[
35+
'frame-benchmarking',
36+
'frame-support/runtime-benchmarks',
37+
'frame-system/runtime-benchmarks',
38+
]
39+
std=[
40+
"frame-support/std",
41+
"frame-system/std",
42+
"pallet-session/std",
43+
"scale-info/std",
44+
"sp-runtime/std",
45+
"sp-std/std",
46+
'frame-benchmarking/std',
47+
]
48+
try-runtime=["frame-support/try-runtime"]

pallets/outtie/src/benchmarking.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (C) 2023 Entropy Cryptography Inc.
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU Affero General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU Affero General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Affero General Public License
14+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
15+
16+
use frame_benchmarking::v2::*;
17+
use frame_system::{EventRecord, RawOrigin};
18+
use sp_std::vec;
19+
20+
use super::*;
21+
#[allow(unused)]
22+
use crate::Pallet as Outtie;
23+
24+
const NULL_ARR: [u8; 32] = [0; 32];
25+
26+
fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
27+
let events = frame_system::Pallet::<T>::events();
28+
let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
29+
// compare to the last event record
30+
let EventRecord { event, .. } = &events[events.len() - 1];
31+
assert_eq!(event, &system_event);
32+
}
33+
34+
#[benchmarks]
35+
mod benchmarks {
36+
use super::*;
37+
#[benchmark]
38+
fn add_box() {
39+
let caller: T::AccountId = whitelisted_caller();
40+
let x25519_public_key = NULL_ARR;
41+
let endpoint = vec![];
42+
43+
let server_info = OuttieServerInfo { x25519_public_key, endpoint };
44+
#[extrinsic_call]
45+
_(RawOrigin::Signed(caller.clone()), server_info.clone());
46+
47+
assert_last_event::<T>(Event::<T>::BoxAdded { box_account: caller, server_info }.into());
48+
}
49+
impl_benchmark_test_suite!(Outtie, crate::mock::new_test_ext(), crate::mock::Test);
50+
}

pallets/outtie/src/lib.rs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright (C) 2023 Entropy Cryptography Inc.
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU Affero General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU Affero General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Affero General Public License
14+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
15+
16+
//! # Outtie Pallet
17+
#![cfg_attr(not(feature = "std"), no_std)]
18+
#![allow(clippy::unused_unit)]
19+
20+
use entropy_shared::X25519PublicKey;
21+
use frame_support::pallet_prelude::*;
22+
use frame_system::pallet_prelude::*;
23+
#[cfg(feature = "std")]
24+
use serde::{Deserialize, Serialize};
25+
use sp_runtime::DispatchResult;
26+
use sp_std::vec::Vec;
27+
#[cfg(test)]
28+
mod mock;
29+
30+
#[cfg(test)]
31+
mod tests;
32+
33+
#[cfg(feature = "runtime-benchmarks")]
34+
mod benchmarking;
35+
36+
pub mod weights;
37+
38+
pub use module::*;
39+
pub use weights::WeightInfo;
40+
41+
#[frame_support::pallet]
42+
pub mod module {
43+
use super::*;
44+
45+
#[pallet::config]
46+
pub trait Config: frame_system::Config + pallet_session::Config {
47+
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
48+
49+
/// The maximum length of an API box server's endpoint address, in bytes.
50+
type MaxEndpointLength: Get<u32>;
51+
52+
/// Weight information for the extrinsics in this module.
53+
type WeightInfo: WeightInfo;
54+
}
55+
56+
/// Information about a tdx server
57+
#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)]
58+
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
59+
pub struct OuttieServerInfo {
60+
pub x25519_public_key: X25519PublicKey,
61+
pub endpoint: Vec<u8>,
62+
// TODO #1421 pub provisioning_certification_key: VerifyingKey,
63+
}
64+
65+
/// API box signing account => Server Info
66+
#[pallet::storage]
67+
#[pallet::getter(fn get_api_boxes)]
68+
pub type ApiBoxes<T: Config> =
69+
StorageMap<_, Blake2_128Concat, T::AccountId, OuttieServerInfo, OptionQuery>;
70+
71+
#[pallet::error]
72+
pub enum Error<T> {
73+
/// Endpoint is too long
74+
EndpointTooLong,
75+
/// Box account already exists
76+
BoxAccountAlreadyExists,
77+
}
78+
79+
#[pallet::event]
80+
#[pallet::generate_deposit(fn deposit_event)]
81+
pub enum Event<T: Config> {
82+
BoxAdded { box_account: T::AccountId, server_info: OuttieServerInfo },
83+
}
84+
85+
#[pallet::pallet]
86+
#[pallet::without_storage_info]
87+
pub struct Pallet<T>(_);
88+
89+
#[pallet::call]
90+
impl<T: Config> Pallet<T> {
91+
#[pallet::call_index(1)]
92+
#[pallet::weight(<T as Config>::WeightInfo::add_box())]
93+
pub fn add_box(
94+
origin: OriginFor<T>,
95+
server_info: OuttieServerInfo,
96+
// TODO #1421 quote: Vec<u8>,
97+
) -> DispatchResult {
98+
let box_account = ensure_signed(origin.clone())?;
99+
100+
ensure!(
101+
server_info.endpoint.len() as u32 <= T::MaxEndpointLength::get(),
102+
Error::<T>::EndpointTooLong
103+
);
104+
105+
ensure!(
106+
!ApiBoxes::<T>::contains_key(&box_account),
107+
Error::<T>::BoxAccountAlreadyExists
108+
);
109+
110+
// TODO #1421 assertion
111+
112+
ApiBoxes::<T>::insert(&box_account, server_info.clone());
113+
114+
Self::deposit_event(Event::BoxAdded { box_account, server_info });
115+
116+
Ok(())
117+
}
118+
}
119+
}

pallets/outtie/src/mock.rs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Copyright (C) 2023 Entropy Cryptography Inc.
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU Affero General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU Affero General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Affero General Public License
14+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
15+
16+
//! Mocks for the outtie pallet.
17+
18+
#![cfg(test)]
19+
use frame_support::{
20+
construct_runtime, derive_impl, parameter_types,
21+
traits::{ConstU64, Everything, OneSessionHandler},
22+
};
23+
use sp_core::H256;
24+
use sp_runtime::{
25+
testing::UintAuthorityId,
26+
traits::{ConvertInto, IdentityLookup},
27+
BuildStorage,
28+
};
29+
30+
use super::*;
31+
32+
pub type AccountId = u128;
33+
34+
use crate as pallet_outtie;
35+
36+
#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
37+
impl frame_system::Config for Test {
38+
type AccountData = ();
39+
type AccountId = AccountId;
40+
type BaseCallFilter = Everything;
41+
type Block = Block;
42+
type BlockHashCount = ConstU64<250>;
43+
type BlockLength = ();
44+
type BlockWeights = ();
45+
type DbWeight = ();
46+
type Hash = H256;
47+
type Hashing = sp_runtime::traits::BlakeTwo256;
48+
type Lookup = IdentityLookup<Self::AccountId>;
49+
type MaxConsumers = frame_support::traits::ConstU32<16>;
50+
type Nonce = u64;
51+
type OnKilledAccount = ();
52+
type OnNewAccount = ();
53+
type OnSetCode = ();
54+
type PalletInfo = PalletInfo;
55+
type RuntimeCall = RuntimeCall;
56+
type RuntimeEvent = RuntimeEvent;
57+
type RuntimeOrigin = RuntimeOrigin;
58+
type SS58Prefix = ();
59+
type SystemWeightInfo = ();
60+
type Version = ();
61+
}
62+
63+
pub struct MockSessionManager;
64+
impl pallet_session::SessionManager<AccountId> for MockSessionManager {
65+
fn end_session(_: sp_staking::SessionIndex) {}
66+
fn start_session(_: sp_staking::SessionIndex) {}
67+
fn new_session(_: sp_staking::SessionIndex) -> Option<Vec<AccountId>> {
68+
None
69+
}
70+
}
71+
72+
pub struct OtherSessionHandler;
73+
impl OneSessionHandler<AccountId> for OtherSessionHandler {
74+
type Key = UintAuthorityId;
75+
76+
fn on_genesis_session<'a, I: 'a>(_: I)
77+
where
78+
I: Iterator<Item = (&'a AccountId, Self::Key)>,
79+
AccountId: 'a,
80+
{
81+
}
82+
83+
fn on_new_session<'a, I: 'a>(_: bool, _: I, _: I)
84+
where
85+
I: Iterator<Item = (&'a AccountId, Self::Key)>,
86+
AccountId: 'a,
87+
{
88+
}
89+
90+
fn on_disabled(_validator_index: u32) {}
91+
}
92+
93+
impl sp_runtime::BoundToRuntimeAppPublic for OtherSessionHandler {
94+
type Public = UintAuthorityId;
95+
}
96+
97+
impl pallet_session::Config for Test {
98+
type RuntimeEvent = RuntimeEvent;
99+
type ValidatorId = u128;
100+
type ValidatorIdOf = ConvertInto;
101+
type ShouldEndSession = pallet_session::PeriodicSessions<ConstU64<1>, ConstU64<0>>;
102+
type NextSessionRotation = pallet_session::PeriodicSessions<ConstU64<1>, ConstU64<0>>;
103+
type SessionManager = MockSessionManager;
104+
type SessionHandler = (OtherSessionHandler,);
105+
type Keys = UintAuthorityId;
106+
type WeightInfo = ();
107+
}
108+
109+
parameter_types! {
110+
pub const MaxEndpointLength: u32 = 3;
111+
}
112+
113+
impl Config for Test {
114+
type RuntimeEvent = RuntimeEvent;
115+
type MaxEndpointLength = MaxEndpointLength;
116+
type WeightInfo = ();
117+
}
118+
119+
type Block = frame_system::mocking::MockBlock<Test>;
120+
121+
construct_runtime!(
122+
pub enum Test
123+
{
124+
System: frame_system,
125+
Outtie: pallet_outtie,
126+
Session: pallet_session,
127+
128+
}
129+
);
130+
131+
// Build genesis storage according to the mock runtime.
132+
pub fn new_test_ext() -> sp_io::TestExternalities {
133+
let t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
134+
t.into()
135+
}

0 commit comments

Comments
 (0)