Skip to content

Commit 3d0cf7b

Browse files
authored
Merge pull request #2441 from opentensor/mev-shield-rework
Rework shielded transactions
2 parents 4634e77 + ca97f24 commit 3d0cf7b

46 files changed

Lines changed: 2489 additions & 3401 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/e2e.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
name: E2E Tests
22

3+
concurrency:
4+
group: e2e-${{ github.ref }}
5+
cancel-in-progress: true
6+
37
on:
4-
workflow_dispatch:
8+
pull_request:
9+
10+
workflow_dispatch:
11+
inputs:
12+
verbose:
13+
description: "Output more information when triggered manually"
14+
required: false
15+
default: ""

Cargo.lock

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

Cargo.toml

Lines changed: 144 additions & 334 deletions
Large diffs are not rendered by default.

common/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ use sp_runtime::{
1515
use subtensor_macros::freeze_struct;
1616

1717
pub use currency::*;
18+
pub use transaction_error::*;
1819

1920
mod currency;
21+
mod transaction_error;
2022

2123
/// Balance of an account.
2224
pub type Balance = u64;

common/src/transaction_error.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use sp_runtime::transaction_validity::{InvalidTransaction, TransactionValidityError};
2+
3+
#[derive(Debug, PartialEq)]
4+
pub enum CustomTransactionError {
5+
ColdkeyInSwapSchedule,
6+
StakeAmountTooLow,
7+
BalanceTooLow,
8+
SubnetNotExists,
9+
HotkeyAccountDoesntExist,
10+
NotEnoughStakeToWithdraw,
11+
RateLimitExceeded,
12+
InsufficientLiquidity,
13+
SlippageTooHigh,
14+
TransferDisallowed,
15+
HotKeyNotRegisteredInNetwork,
16+
InvalidIpAddress,
17+
ServingRateLimitExceeded,
18+
InvalidPort,
19+
BadRequest,
20+
ZeroMaxAmount,
21+
InvalidRevealRound,
22+
CommitNotFound,
23+
CommitBlockNotInRevealRange,
24+
InputLengthsUnequal,
25+
UidNotFound,
26+
EvmKeyAssociateRateLimitExceeded,
27+
ColdkeySwapDisputed,
28+
InvalidRealAccount,
29+
FailedShieldedTxParsing,
30+
InvalidShieldedTxPubKeyHash,
31+
}
32+
33+
impl From<CustomTransactionError> for u8 {
34+
fn from(variant: CustomTransactionError) -> u8 {
35+
match variant {
36+
CustomTransactionError::ColdkeyInSwapSchedule => 0,
37+
CustomTransactionError::StakeAmountTooLow => 1,
38+
CustomTransactionError::BalanceTooLow => 2,
39+
CustomTransactionError::SubnetNotExists => 3,
40+
CustomTransactionError::HotkeyAccountDoesntExist => 4,
41+
CustomTransactionError::NotEnoughStakeToWithdraw => 5,
42+
CustomTransactionError::RateLimitExceeded => 6,
43+
CustomTransactionError::InsufficientLiquidity => 7,
44+
CustomTransactionError::SlippageTooHigh => 8,
45+
CustomTransactionError::TransferDisallowed => 9,
46+
CustomTransactionError::HotKeyNotRegisteredInNetwork => 10,
47+
CustomTransactionError::InvalidIpAddress => 11,
48+
CustomTransactionError::ServingRateLimitExceeded => 12,
49+
CustomTransactionError::InvalidPort => 13,
50+
CustomTransactionError::BadRequest => 255,
51+
CustomTransactionError::ZeroMaxAmount => 14,
52+
CustomTransactionError::InvalidRevealRound => 15,
53+
CustomTransactionError::CommitNotFound => 16,
54+
CustomTransactionError::CommitBlockNotInRevealRange => 17,
55+
CustomTransactionError::InputLengthsUnequal => 18,
56+
CustomTransactionError::UidNotFound => 19,
57+
CustomTransactionError::EvmKeyAssociateRateLimitExceeded => 20,
58+
CustomTransactionError::ColdkeySwapDisputed => 21,
59+
CustomTransactionError::InvalidRealAccount => 22,
60+
CustomTransactionError::FailedShieldedTxParsing => 23,
61+
CustomTransactionError::InvalidShieldedTxPubKeyHash => 24,
62+
}
63+
}
64+
}
65+
66+
impl From<CustomTransactionError> for TransactionValidityError {
67+
fn from(variant: CustomTransactionError) -> Self {
68+
TransactionValidityError::Invalid(InvalidTransaction::Custom(variant.into()))
69+
}
70+
}

node/Cargo.toml

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ clap = { workspace = true, features = ["derive"] }
2626
futures = { workspace = true, features = ["thread-pool"] }
2727
serde = { workspace = true, features = ["derive"] }
2828
hex.workspace = true
29+
tokio = { workspace = true, features = ["time"] }
2930

3031
# Storage import
3132
memmap2.workspace = true
@@ -77,6 +78,7 @@ sp-keystore.workspace = true
7778
polkadot-sdk = { workspace = true, features = [
7879
"cumulus-primitives-proof-size-hostfunction",
7980
] }
81+
stp-io.workspace = true
8082

8183
# These dependencies are used for the subtensor's RPCs
8284
jsonrpsee = { workspace = true, features = ["server"] }
@@ -94,6 +96,7 @@ pallet-transaction-payment-rpc-runtime-api.workspace = true
9496
# These dependencies are used for runtime benchmarking
9597
frame-benchmarking.workspace = true
9698
frame-benchmarking-cli.workspace = true
99+
pallet-subtensor.workspace = true
97100

98101
# Needed for Frontier
99102
fc-mapping-sync.workspace = true
@@ -116,18 +119,8 @@ num-traits = { workspace = true, features = ["std"] }
116119

117120
# Mev Shield
118121
pallet-shield.workspace = true
119-
tokio = { version = "1.38", features = ["time"] }
120-
x25519-dalek = "2"
121-
hkdf = "0.12"
122-
chacha20poly1305 = { version = "0.10", features = ["std"] }
123-
codec.workspace = true
124-
rand.workspace = true
125-
sha2.workspace = true
126-
anyhow.workspace = true
127-
pallet-subtensor.workspace = true
128-
ml-kem.workspace = true
129-
rand_core = "0.9.3"
130-
blake2 = "0.10.6"
122+
stp-shield.workspace = true
123+
stc-shield.workspace = true
131124

132125
# Local Dependencies
133126
node-subtensor-runtime = { workspace = true, features = ["std"] }
@@ -187,8 +180,8 @@ try-runtime = [
187180
"pallet-commitments/try-runtime",
188181
"pallet-drand/try-runtime",
189182
"polkadot-sdk/try-runtime",
190-
"pallet-shield/try-runtime",
191183
"pallet-subtensor/try-runtime",
184+
"pallet-shield/try-runtime",
192185
]
193186

194187
metadata-hash = ["node-subtensor-runtime/metadata-hash"]

node/src/benchmarking.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ pub fn create_benchmark_extrinsic(
124124
.checked_next_power_of_two()
125125
.map(|c| c / 2)
126126
.unwrap_or(2) as u64;
127-
let extra: runtime::TransactionExtensions =
127+
let extra: runtime::TxExtension = (
128128
(
129129
frame_system::CheckNonZeroSender::<runtime::Runtime>::new(),
130130
frame_system::CheckSpecVersion::<runtime::Runtime>::new(),
@@ -136,32 +136,33 @@ pub fn create_benchmark_extrinsic(
136136
)),
137137
check_nonce::CheckNonce::<runtime::Runtime>::from(nonce),
138138
frame_system::CheckWeight::<runtime::Runtime>::new(),
139-
transaction_payment_wrapper::ChargeTransactionPaymentWrapper::new(
140-
pallet_transaction_payment::ChargeTransactionPayment::<runtime::Runtime>::from(0),
141-
),
139+
),
140+
(
141+
transaction_payment_wrapper::ChargeTransactionPaymentWrapper::new(0),
142142
sudo_wrapper::SudoTransactionExtension::<runtime::Runtime>::new(),
143+
pallet_shield::CheckShieldedTxValidity::<runtime::Runtime>::new(),
143144
pallet_subtensor::transaction_extension::SubtensorTransactionExtension::<
144145
runtime::Runtime,
145146
>::new(),
146147
pallet_drand::drand_priority::DrandPriority::<runtime::Runtime>::new(),
147-
frame_metadata_hash_extension::CheckMetadataHash::<runtime::Runtime>::new(true),
148-
);
148+
),
149+
frame_metadata_hash_extension::CheckMetadataHash::<runtime::Runtime>::new(true),
150+
);
149151

150152
let raw_payload = runtime::SignedPayload::from_raw(
151153
call.clone(),
152154
extra.clone(),
153155
(
154-
(),
155-
runtime::VERSION.spec_version,
156-
runtime::VERSION.transaction_version,
157-
genesis_hash,
158-
best_hash,
159-
(),
160-
(),
161-
(),
162-
(),
163-
(),
164-
(),
156+
(
157+
(),
158+
runtime::VERSION.spec_version,
159+
runtime::VERSION.transaction_version,
160+
genesis_hash,
161+
best_hash,
162+
(),
163+
(),
164+
),
165+
((), (), (), (), ()),
165166
None,
166167
),
167168
);

node/src/client.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ pub type HostFunctions = (
1616
frame_benchmarking::benchmarking::HostFunctions,
1717
sp_crypto_ec_utils::bls12_381::host_calls::HostFunctions,
1818
ProofSize,
19+
stp_io::SubtensorHostFunctions,
1920
);
2021
pub type RuntimeExecutor = WasmExecutor<HostFunctions>;

node/src/consensus/aura_consensus.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ use sp_inherents::CreateInherentDataProviders;
2828
use sp_keystore::KeystorePtr;
2929
use sp_runtime::traits::Block as BlockT;
3030
use sp_runtime::traits::NumberFor;
31+
use stc_shield::InherentDataProvider as ShieldInherentDataProvider;
3132
use std::{error::Error, sync::Arc};
33+
use stp_shield::ShieldKeystorePtr;
3234

3335
pub struct AuraConsensus;
3436

3537
impl ConsensusMechanism for AuraConsensus {
3638
type InherentDataProviders = (
3739
sp_consensus_aura::inherents::InherentDataProvider,
3840
sp_timestamp::InherentDataProvider,
41+
stc_shield::InherentDataProvider,
3942
);
4043

4144
fn start_authoring<C, SC, I, PF, SO, L, CIDP, BS, Error>(
@@ -101,18 +104,21 @@ impl ConsensusMechanism for AuraConsensus {
101104

102105
fn create_inherent_data_providers(
103106
slot_duration: SlotDuration,
107+
shield_keystore: ShieldKeystorePtr,
104108
) -> Result<Self::InherentDataProviders, Box<dyn Error + Send + Sync>> {
105109
let timestamp = sp_timestamp::InherentDataProvider::from_system_time();
106110
let slot =
107111
sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(
108112
*timestamp,
109113
slot_duration,
110114
);
111-
Ok((slot, timestamp))
115+
let shield = ShieldInherentDataProvider::new(shield_keystore);
116+
Ok((slot, timestamp, shield))
112117
}
113118

114119
fn pending_create_inherent_data_providers(
115120
slot_duration: SlotDuration,
121+
shield_keystore: ShieldKeystorePtr,
116122
) -> Result<Self::InherentDataProviders, Box<dyn Error + Send + Sync>> {
117123
let current = sp_timestamp::InherentDataProvider::from_system_time();
118124
let next_slot = current
@@ -125,7 +131,8 @@ impl ConsensusMechanism for AuraConsensus {
125131
*timestamp,
126132
slot_duration,
127133
);
128-
Ok((slot, timestamp))
134+
let shield = stc_shield::InherentDataProvider::new(shield_keystore);
135+
Ok((slot, timestamp, shield))
129136
}
130137

131138
fn new() -> Self {

node/src/consensus/babe_consensus.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ use sp_consensus_slots::SlotDuration;
3131
use sp_inherents::CreateInherentDataProviders;
3232
use sp_keystore::KeystorePtr;
3333
use sp_runtime::traits::NumberFor;
34+
use stc_shield::InherentDataProvider as ShieldInherentDataProvider;
3435
use std::{error::Error, sync::Arc};
36+
use stp_shield::ShieldKeystorePtr;
3537

3638
pub struct BabeConsensus {
3739
babe_link: Option<BabeLink<Block>>,
@@ -42,6 +44,7 @@ impl ConsensusMechanism for BabeConsensus {
4244
type InherentDataProviders = (
4345
sp_consensus_babe::inherents::InherentDataProvider,
4446
sp_timestamp::InherentDataProvider,
47+
stc_shield::InherentDataProvider,
4548
);
4649

4750
#[allow(clippy::expect_used)]
@@ -111,18 +114,21 @@ impl ConsensusMechanism for BabeConsensus {
111114

112115
fn create_inherent_data_providers(
113116
slot_duration: SlotDuration,
117+
shield_keystore: ShieldKeystorePtr,
114118
) -> Result<Self::InherentDataProviders, Box<dyn Error + Send + Sync>> {
115119
let timestamp = sp_timestamp::InherentDataProvider::from_system_time();
116120
let slot =
117121
sp_consensus_babe::inherents::InherentDataProvider::from_timestamp_and_slot_duration(
118122
*timestamp,
119123
slot_duration,
120124
);
121-
Ok((slot, timestamp))
125+
let shield = stc_shield::InherentDataProvider::new(shield_keystore);
126+
Ok((slot, timestamp, shield))
122127
}
123128

124129
fn pending_create_inherent_data_providers(
125130
slot_duration: SlotDuration,
131+
shield_keystore: ShieldKeystorePtr,
126132
) -> Result<Self::InherentDataProviders, Box<dyn Error + Send + Sync>> {
127133
let current = sp_timestamp::InherentDataProvider::from_system_time();
128134
let next_slot = current
@@ -135,7 +141,8 @@ impl ConsensusMechanism for BabeConsensus {
135141
*timestamp,
136142
slot_duration,
137143
);
138-
Ok((slot, timestamp))
144+
let shield = ShieldInherentDataProvider::new(shield_keystore);
145+
Ok((slot, timestamp, shield))
139146
}
140147

141148
fn new() -> Self {

0 commit comments

Comments
 (0)