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

Test: Offchain workers disabled by default for container chains #723

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
8b37ec9
feat: added offchain worker pallet that sends http requests the sim…
Oct 16, 2024
230f161
feat: removed unused imports and process result and added simple even…
Oct 21, 2024
133d677
test: added initial testing setup for offchain-logic testing
Oct 23, 2024
7e05220
fix: comment http function inside offchain worker pallet
Oct 23, 2024
c42d07d
fix: fixed unused block number offchain worker function
Oct 23, 2024
1987e22
style: fixed toml linting
Oct 23, 2024
7aaeec2
style: commented http-related imports for offchain worker
Oct 23, 2024
2520247
style: removed added empty lines in frontier container chain node tom…
Oct 28, 2024
e7ff792
refactor: cleaned offchain worker pallet dead code and renamed it to …
Oct 28, 2024
bbee086
feat: added root function that switches on and of the offchain worker
Oct 28, 2024
d6afff8
feat: added genesis build config that switches off the offchain worke…
Oct 28, 2024
527989c
feat: offchain worker is emitting events using raw unsigned extrinsic
Oct 29, 2024
62b2e30
fix: offchain worker test does not timeout
Oct 30, 2024
32b714d
fix: removed deprecated where clause for simple container chain runtime
Oct 30, 2024
a067c26
test: updated moonwall tests to include cases that check for offchain…
Oct 30, 2024
5be9b7a
fix: removed unused import for offchain worker tests
Oct 30, 2024
bc73c6d
fix: changed offchain worker testing enabled status variable to const…
Oct 30, 2024
b0ae711
Merge branch 'master' into aleks-container-chains-disabled-offchain-w…
chexware Oct 30, 2024
5c8f778
test: added check if offchain events are emitted after enabling and t…
Oct 30, 2024
1ef792a
Merge branch 'master' into aleks-container-chains-disabled-offchain-w…
chexware Oct 30, 2024
2497476
feat: added check if offchain testing is enbaled when validating unch…
Oct 31, 2024
e66e873
refactor: renamed switch function for offchain worker to set_offchain…
Oct 31, 2024
e6bcbff
feat: added dummy weights to offchain worker pallet
Oct 31, 2024
d263ed4
test: reduced number of collators for offchain worker tests
Oct 31, 2024
3faffe0
test: added utility function that check if event is emitted in the ne…
Nov 4, 2024
7834cad
test: offchain worker test uses build-spec-dancelight-single-containe…
Nov 5, 2024
53fac57
Merge branch 'master' into aleks-container-chains-disabled-offchain-w…
chexware Nov 5, 2024
d9ddc08
feat: added additional check if offchain worker is enabled inside the…
Nov 5, 2024
401e514
test: added offchain worker enabling-related log to checkLogsNotExist
Nov 5, 2024
e1645da
Merge branch 'master' into aleks-container-chains-disabled-offchain-w…
chexware Nov 6, 2024
e4bf099
ci: added sha parameter to zombienet tests ci flow
Nov 6, 2024
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
16 changes: 16 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pallet-data-preservers-runtime-api = { path = "pallets/data-preservers/runtime-a
pallet-inflation-rewards = { path = "pallets/inflation-rewards", default-features = false }
pallet-initializer = { path = "pallets/initializer", default-features = false }
pallet-invulnerables = { path = "pallets/invulnerables", default-features = false }
pallet-ocw-simple = { path = "pallets/ocw-simple", default-features = false }
pallet-pooled-staking = { path = "pallets/pooled-staking", default-features = false }
pallet-registrar = { path = "pallets/registrar", default-features = false }
pallet-registrar-runtime-api = { path = "pallets/registrar/runtime-api", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions container-chains/nodes/frontier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ fc-storage = { workspace = true }
fp-evm = { workspace = true }
fp-rpc = { workspace = true }
pallet-ethereum = { workspace = true }


chexware marked this conversation as resolved.
Show resolved Hide resolved
[build-dependencies]
substrate-build-script-utils = { workspace = true }

Expand Down
6 changes: 6 additions & 0 deletions container-chains/runtime-templates/simple/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ parachains-common = { workspace = true }
frame-benchmarking = { workspace = true, optional = true }
frame-system-benchmarking = { workspace = true, optional = true }
frame-try-runtime = { workspace = true, optional = true }

# Off-chain workers (testing)
pallet-ocw-simple = { workspace = true }

[build-dependencies]
substrate-wasm-builder = { workspace = true }

Expand Down Expand Up @@ -140,6 +144,7 @@ std = [
"pallet-message-queue/std",
"pallet-migrations/std",
"pallet-multisig/std",
"pallet-ocw-simple/std",
"pallet-proxy/std",
"pallet-root-testing/std",
"pallet-session/std",
Expand Down Expand Up @@ -247,6 +252,7 @@ try-runtime = [
"pallet-message-queue/try-runtime",
"pallet-migrations/try-runtime",
"pallet-multisig/try-runtime",
"pallet-ocw-simple/try-runtime",
"pallet-proxy/try-runtime",
"pallet-root-testing/try-runtime",
"pallet-session/try-runtime",
Expand Down
5 changes: 5 additions & 0 deletions container-chains/runtime-templates/simple/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,10 @@ impl pallet_multisig::Config for Runtime {
type WeightInfo = weights::pallet_multisig::SubstrateWeight<Runtime>;
}

impl pallet_ocw_simple::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
}

impl_tanssi_pallets_config!(Runtime);

// Create the runtime by composing the FRAME pallets that were previously configured.
Expand Down Expand Up @@ -701,6 +705,7 @@ construct_runtime!(
RootTesting: pallet_root_testing = 100,
AsyncBacking: pallet_async_backing::{Pallet, Storage} = 110,

OffchainWorker: pallet_ocw_simple::{Pallet,Call,Event<T>} = 120,
chexware marked this conversation as resolved.
Show resolved Hide resolved
}
);

Expand Down
46 changes: 46 additions & 0 deletions pallets/ocw-simple/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[package]
name = "pallet-ocw-simple"
authors = { workspace = true }
description = "Simple Off-chain worker pallet used for testing"
edition = "2021"
license = "GPL-3.0-only"
version = "0.1.0"

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

[lints]
workspace = true

[dependencies]
frame-support = { workspace = true }
frame-system = { workspace = true }
log = { workspace = true }
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }

[dev-dependencies]
sp-core = { workspace = true }


[features]
default = [ "std" ]
std = [
"frame-support/std",
"frame-system/std",
"log/std",
"parity-scale-codec/std",
"scale-info/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"sp-runtime/try-runtime",
]
117 changes: 117 additions & 0 deletions pallets/ocw-simple/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright (C) Moondance Labs Ltd.
// This file is part of Tanssi.

// Tanssi 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.

// Tanssi 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 Tanssi. If not, see <http://www.gnu.org/licenses/>

#![cfg_attr(not(feature = "std"), no_std)]
use frame_system::pallet_prelude::BlockNumberFor;
// Remove comment to enable http requests
// use sp_runtime::offchain::{http, Duration};

pub use pallet::*;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
#[pallet::pallet]
pub struct Pallet<T>(_);

#[pallet::config]
pub trait Config: frame_system::Config {
/// The overarching event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
/// Offchain worker entry point.
///
/// By implementing `fn offchain_worker` you declare a new offchain worker.
/// This function will be called when the node is fully synced and a new best block is
/// successfully imported.
/// Note that it's not guaranteed for offchain workers to run on EVERY block, there might
/// be cases where some blocks are skipped, or for some the worker runs twice (re-orgs),
/// so the code should be able to handle that.
fn offchain_worker(_block_number: BlockNumberFor<T>) {
log::info!("Entering off-chain worker.");
// The entry point of your code called by offchain worker
Self::emit_offchain_event();
// Remove comment if you want to emit event on simple http request
//let _ = Self::fetch_price(block_number);
}
}
#[pallet::call]
impl<T: Config> Pallet<T> {}

/// Events for the pallet.
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// Simple offchain event
SimpleOffchainEvent,
/// Event generated when new price is fetched
PriceFetched { block_number: BlockNumberFor<T> },
}
}

impl<T: Config> Pallet<T> {
/// Simple event emitter
fn emit_offchain_event() {
Self::deposit_event(Event::SimpleOffchainEvent);
}

// Remove comment to enable http requests
/*
/// Fetch current price and return the result in cents.
chexware marked this conversation as resolved.
Show resolved Hide resolved
fn fetch_price(block_number: BlockNumberFor<T>) -> Result<u32, http::Error> {
// We want to keep the offchain worker execution time reasonable, so we set a hard-coded
// deadline to 2s to complete the external call.
// You can also wait indefinitely for the response, however you may still get a timeout
// coming from the host machine.
let deadline = sp_io::offchain::timestamp().add(Duration::from_millis(2_000));
// Initiate an external HTTP GET request.
// This is using high-level wrappers from `sp_runtime`, for the low-level calls that
// you can find in `sp_io`. The API is trying to be similar to `request`, but
// since we are running in a custom WASM execution environment we can't simply
// import the library here.
let request =
http::Request::get("https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USD");
// We set the deadline for sending of the request, note that awaiting response can
// have a separate deadline. Next we send the request, before that it's also possible
// to alter request headers or stream body content in case of non-GET requests.
let pending = request
.deadline(deadline)
.send()
.map_err(|_| http::Error::IoError)?;

// The request is already being processed by the host, we are free to do anything
// else in the worker (we can send multiple concurrent requests too).
// At some point however we probably want to check the response though,
// so we can block current thread and wait for it to finish.
// Note that since the request is being driven by the host, we don't have to wait
// for the request to have it complete, we will just not read the response.
let response = pending
.try_wait(deadline)
.map_err(|_| http::Error::DeadlineReached)??;
// Let's check the status code before we proceed to reading the response.
if response.code != 200 {
log::warn!("Unexpected status code: {}", response.code);
return Err(http::Error::Unknown);
}

Self::deposit_event(Event::PriceFetched { block_number });
Ok(response.code.into())
}
*/
}
108 changes: 108 additions & 0 deletions test/configs/zombieDancelightSingleContainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"settings": {
"timeout": 1000,
"provider": "native"
},
"relaychain": {
"chain_spec_path": "specs/tanssi-relay.json",
"default_command": "../target/release/tanssi-relay",
"default_args": ["--no-hardware-benchmarks", "-lparachain=debug", "--database=paritydb", "--no-beefy"],
"genesis": {
"runtimeGenesis": {
"patch": {
"configuration": {
"config": {
"async_backing_params": {
"allowed_ancestry_len": 2,
"max_candidate_depth": 3
},
"scheduler_params": {
"scheduling_lookahead": 2,
"num_cores": 4
}
}
}
}
}
},
"nodes": [
{
"name": "alice",
"ws_port": "9947",
"validator": true
},
{
"name": "bob",
"validator": true
},
{
"name": "charlie",
"validator": true
},
{
"name": "dave",
"validator": true
}
]
},
"parachains": [
{
"id": 2000,
"chain_spec_path": "specs/single-container-template-container-2000.json",
"collators": [
{
"name": "FullNode-2000",
"validator": false,
"command": "../target/release/container-chain-simple-node",
"args": ["--no-hardware-benchmarks", "--database=paritydb", "--wasmtime-precompiled=wasm"],
"ws_port": 9949,
"p2p_port": 33049,
"prometheus_port": 33102
},
{
"name": "Collator-01",
"command": "../target/release/tanssi-node solo-chain",
"args": ["--no-hardware-benchmarks", "--database=paritydb", "--wasmtime-precompiled=wasm"],
"prometheus_port": 33102
},
{
"name": "Collator-02",
"command": "../target/release/tanssi-node solo-chain",
"args": ["--no-hardware-benchmarks", "--database=paritydb", "--wasmtime-precompiled=wasm"],
"prometheus_port": 33102
},
{
"name": "Collator-03",
"command": "../target/release/tanssi-node solo-chain",
"args": ["--no-hardware-benchmarks", "--database=paritydb", "--wasmtime-precompiled=wasm"],
"prometheus_port": 33102
},
{
"name": "Collator-04",
"command": "../target/release/tanssi-node solo-chain",
"args": ["--no-hardware-benchmarks", "--database=paritydb", "--wasmtime-precompiled=wasm"],
"prometheus_port": 33102
},
{
"name": "Collator-05",
"command": "../target/release/tanssi-node solo-chain",
"args": ["--no-hardware-benchmarks", "--database=paritydb", "--wasmtime-precompiled=wasm"],
"prometheus_port": 33102
},
{
"name": "Collator-06",
chexware marked this conversation as resolved.
Show resolved Hide resolved
"command": "../target/release/tanssi-node solo-chain",
"args": ["--no-hardware-benchmarks", "--database=paritydb", "--wasmtime-precompiled=wasm"],
"prometheus_port": 33102
}
]
}
],
"types": {
"Header": {
"number": "u64",
"parent_hash": "Hash",
"post_state": "Hash"
}
}
}
Loading
Loading