Skip to content

Commit

Permalink
Merge pull request #2302 from subspace/subspace-malicious-operator
Browse files Browse the repository at this point in the history
Introduce the intentional malicious operator
  • Loading branch information
NingLin-P authored Dec 13, 2023
2 parents f1ab683 + f4dea86 commit 591a50e
Show file tree
Hide file tree
Showing 27 changed files with 1,761 additions and 83 deletions.
60 changes: 60 additions & 0 deletions Cargo.lock

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

7 changes: 6 additions & 1 deletion crates/pallet-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ use sp_runtime::{RuntimeAppPublic, SaturatedConversion, Saturating};
use sp_std::boxed::Box;
use sp_std::collections::btree_map::BTreeMap;
use sp_std::vec::Vec;
pub use staking::OperatorConfig;
use subspace_core_primitives::U256;
use subspace_runtime_primitives::Balance;

Expand Down Expand Up @@ -358,10 +359,12 @@ mod pallet {

/// Indexes operator signing key against OperatorId.
#[pallet::storage]
#[pallet::getter(fn operator_signing_key)]
pub(super) type OperatorSigningKey<T: Config> =
StorageMap<_, Identity, OperatorPublicKey, OperatorId, OptionQuery>;

#[pallet::storage]
#[pallet::getter(fn domain_staking_summary)]
pub(super) type DomainStakingSummary<T: Config> =
StorageMap<_, Identity, DomainId, StakingSummary<OperatorId, BalanceOf<T>>, OptionQuery>;

Expand Down Expand Up @@ -1388,7 +1391,9 @@ mod pallet {
_ => {
log::warn!(
target: "runtime::domains",
"Bad bundle {:?}, error: {e:?}", opaque_bundle.domain_id(),
"Bad bundle {:?}, operator {}, error: {e:?}",
opaque_bundle.domain_id(),
opaque_bundle.operator_id(),
);
}
}
Expand Down
10 changes: 10 additions & 0 deletions crates/sp-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use sp_runtime::traits::{
use sp_runtime::{Digest, DigestItem, OpaqueExtrinsic, Percent};
use sp_runtime_interface::pass_by;
use sp_runtime_interface::pass_by::PassBy;
use sp_std::collections::btree_map::BTreeMap;
use sp_std::collections::btree_set::BTreeSet;
use sp_std::fmt::{Display, Formatter};
use sp_std::vec::Vec;
Expand Down Expand Up @@ -1007,6 +1008,15 @@ sp_api::decl_runtime_apis! {

/// Returns the execution receipt
fn execution_receipt(receipt_hash: HeaderHashFor<DomainHeader>) -> Option<ExecutionReceiptFor<DomainHeader, Block, Balance>>;

/// Returns the current epoch and the next epoch operators of the given domain
fn domain_operators(domain_id: DomainId) -> Option<(BTreeMap<OperatorId, Balance>, Vec<OperatorId>)>;

/// Get operator id by signing key
fn operator_id_by_signing_key(signing_key: OperatorPublicKey) -> Option<OperatorId>;

/// Get the consensus chain sudo account id, currently only used in the intentional malicious operator
fn sudo_account_id() -> subspace_runtime_primitives::AccountId;
}

pub trait BundleProducerElectionApi<Balance: Encode + Decode> {
Expand Down
80 changes: 80 additions & 0 deletions crates/subspace-malicious-operator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
[package]
name = "subspace-malicious-operator"
version = "0.1.0"
authors = ["Subspace Labs <https://subspace.network>"]
description = "A Subspace Network Blockchain node."
edition = "2021"
license = "GPL-3.0-or-later"
build = "build.rs"
homepage = "https://subspace.network"
repository = "https://github.com/subspace/subspace"
include = [
"/src",
"/build.rs",
"/Cargo.toml",
"/README.md"
]

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
cross-domain-message-gossip = { version = "0.1.0", path = "../../domains/client/cross-domain-message-gossip" }
domain-client-message-relayer = { version = "0.1.0", path = "../../domains/client/relayer" }
domain-client-operator = { version = "0.1.0", path = "../../domains/client/domain-operator" }
domain-eth-service = { version = "0.1.0", path = "../../domains/client/eth-service" }
domain-service = { version = "0.1.0", path = "../../domains/service" }
domain-runtime-primitives = { version = "0.1.0", path = "../../domains/primitives/runtime" }
evm-domain-runtime = { version = "0.1.0", path = "../../domains/runtime/evm" }
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
frame-system-rpc-runtime-api = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
futures = "0.3.29"
log = "0.4.20"
mimalloc = "0.1.39"
pallet-domains = { version = "0.1.0", default-features = false, path = "../pallet-domains" }
pallet-transaction-payment = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
parity-scale-codec = "3.6.5"
pallet-sudo = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
sc-chain-spec = { version = "4.0.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
sc-cli = { version = "0.10.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5", default-features = false }
sc-client-api = { version = "4.0.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
sc-consensus-slots = { version = "0.10.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
sc-consensus-subspace = { version = "0.1.0", path = "../sc-consensus-subspace" }
sc-network = { version = "0.10.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
sc-service = { version = "0.10.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5", default-features = false }
sc-storage-monitor = { version = "0.1.0", git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5", default-features = false }
sc-tracing = { version = "4.0.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
sc-transaction-pool-api = { version = "4.0.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
sc-network-sync = { version = "0.10.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
sc-utils = { version = "4.0.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
serde_json = "1.0.106"
sp-api = { version = "4.0.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
sp-blockchain = { version = "4.0.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
sp-block-builder = { git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5", default-features = false, version = "4.0.0-dev" }
sp-consensus-subspace = { version = "0.1.0", path = "../sp-consensus-subspace" }
sp-consensus-slots = { version = "0.10.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
sp-core = { version = "21.0.0", git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
sp-domains = { version = "0.1.0", path = "../sp-domains" }
sp-domain-digests = { version = "0.1.0", path = "../../domains/primitives/digests" }
sp-transaction-pool = { version = "4.0.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
sp-messenger = { version = "0.1.0", path = "../../domains/primitives/messenger" }
sp-runtime = { version = "24.0.0", git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
sp-keystore = { version = "0.27.0", git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
sp-keyring = { git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
subspace-core-primitives = { version = "0.1.0", path = "../subspace-core-primitives" }
subspace-networking = { version = "0.1.0", path = "../subspace-networking" }
subspace-proof-of-space = { version = "0.1.0", path = "../subspace-proof-of-space" }
subspace-runtime = { version = "0.1.0", path = "../subspace-runtime" }
subspace-runtime-primitives = { version = "0.1.0", path = "../subspace-runtime-primitives" }
subspace-node = { version = "0.1.0", path = "../subspace-node" }
subspace-service = { version = "0.1.0", path = "../subspace-service" }
thiserror = "1.0.48"
tokio = "1.34.0"
rand = "0.8.5"
tracing = "0.1.37"

[build-dependencies]
substrate-build-script-utils = { version = "3.0.0", git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }

[features]
default = []
33 changes: 33 additions & 0 deletions crates/subspace-malicious-operator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# The intentional malicious operator node

NOTE: ****this is only use for testing purpose****

The malicious operator node act as a regular [domain operator](../../domains/README.md) but it will intentionally and continuously produce malicious content to test if the network can handle it properly.

### How it works

Most parts of the malicious operator act exactly the same as the regular domain operator except its bundle producer. When it produce a bundle, the bundle will be tampered with malicious content with probability before submitting to the consensus chain.

Currently, it supports produce:
- Invalid bundle
- Fraudulent ER

When the operator submit malicious content to the consensus chain, the honest operator in the network will detect and submit fraud proof that target these content, and cause the malicious operator being slashed and baned from submitting bundle.

The malicious operator node will detect the slashing and register a new operator as the malicious operator, moreover, it will enforce the epoch transition to accelerate the onboard of the new malicious operator, and contiune producing malicious content.

### Build from source

```bash
cargo build -r subspace-malicious-operator
```

### Run

The malicious operator node take the same args as the regular domain operator, please refer to [Domain operator](../../domains/README.md).

A few notable differences:
- The malicious operator node will ignore the `--operator-id` arg if specified, instead it will register new operator internally and automatically and using their id to produce malicious content.
- The malicious operator node requires the consensus chain sudo key pair to run in the network.
- With `--chains local/dev`, Alice is the sudo account and its key pair is already exist in the node.
- With `--chain devnet`, the sudo key pair need to insert into the keystore with `subspace-node key insert --suri "<Secret phrase>" --key-type sub_ --scheme sr25519 --keystore-path <PATH>`.
23 changes: 23 additions & 0 deletions crates/subspace-malicious-operator/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (C) 2023 Subspace Labs, Inc.
// SPDX-License-Identifier: GPL-3.0-or-later

// This program 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.

// This program 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/>.

use substrate_build_script_utils::{generate_cargo_keys, rerun_if_git_head_changed};

fn main() {
generate_cargo_keys();

rerun_if_git_head_changed();
}
Loading

0 comments on commit 591a50e

Please sign in to comment.