Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
04f2167
Start
bkchr Mar 13, 2023
08c0b32
More work!
bkchr Mar 13, 2023
d1d5d4c
Moar
bkchr Mar 14, 2023
55af869
More changes
bkchr Mar 15, 2023
4374d01
More fixes
bkchr Mar 16, 2023
cc3c030
More worrk
bkchr Mar 17, 2023
0cac727
More fixes
bkchr Mar 20, 2023
7dbaa81
More fixes to make it compile
bkchr Mar 20, 2023
5b2fc23
Adds `NoOffchainStorage`
bkchr Mar 21, 2023
9fed302
Pass the extensions
bkchr Mar 21, 2023
6730212
Small basti making small progress
bkchr Mar 22, 2023
63ada35
Merge remote-tracking branch 'origin/master' into bkchr-execution-str…
bkchr Apr 4, 2023
a3ff3e3
Fix merge errors and remove `ExecutionContext`
bkchr Apr 4, 2023
afbc691
Move registration of `ReadRuntimeVersionExt` to `ExecutionExtension`
bkchr Apr 4, 2023
947b02f
Merge remote-tracking branch 'origin/bkchr-move-read-runtime-version-…
bkchr Apr 4, 2023
906fb5a
Fix compilation
bkchr Apr 5, 2023
20f3c02
Register the global extensions inside runtime api instance
bkchr Apr 10, 2023
f6928f8
Merge remote-tracking branch 'origin/master' into bkchr-execution-str…
bkchr Apr 24, 2023
033a32b
Fixes
bkchr Apr 26, 2023
cb24657
Merge remote-tracking branch 'origin/master' into bkchr-execution-str…
bkchr Apr 28, 2023
97befb6
Fix `generate_initial_session_keys` by passing the keystore extension
bkchr May 1, 2023
c76f8fd
Fix the grandpa tests
bkchr May 2, 2023
13347fb
Fix more tests
bkchr May 2, 2023
0bd9113
Fix more tests
bkchr May 2, 2023
d457ac9
Don't set any heap pages if there isn't an override
bkchr May 3, 2023
4e4b5e4
Fix small fallout
bkchr May 3, 2023
7d48c14
FMT
bkchr May 3, 2023
d85f8ea
Merge remote-tracking branch 'origin/master' into bkchr-execution-str…
bkchr May 5, 2023
05223f1
Fix tests
bkchr May 7, 2023
6ca6737
More tests
bkchr May 8, 2023
440d0e5
Merge remote-tracking branch 'origin/master' into bkchr-execution-str…
bkchr May 8, 2023
5942c96
Offchain worker custom extensions
bkchr May 9, 2023
5b253f8
More fixes
bkchr May 9, 2023
3b7a8b4
Merge remote-tracking branch 'origin/master' into bkchr-execution-str…
bkchr May 17, 2023
16714b6
Make offchain tx pool creation reusable
bkchr May 23, 2023
e52f63d
Merge remote-tracking branch 'origin/master' into bkchr-execution-str…
bkchr May 25, 2023
190e036
Merge remote-tracking branch 'origin/bkchr-offchain-tx-pool-factory' …
bkchr May 25, 2023
902f1a4
Fixes
bkchr May 25, 2023
671f9b9
Fixes
bkchr May 25, 2023
59a6fa0
Set offchain transaction pool in BABE before using it in the runtime
bkchr May 29, 2023
9145976
Add the `offchain_tx_pool` to Grandpa as well
bkchr May 30, 2023
c29dcb3
Fix the nodes
bkchr May 30, 2023
23578b1
Print some error when using the old warnings
bkchr Jun 2, 2023
9208bf4
Merge remote-tracking branch 'origin/master' into bkchr-execution-str…
bkchr Jun 2, 2023
0e0325c
Fix merge issues
bkchr Jun 2, 2023
d12e6ba
Merge remote-tracking branch 'origin/master' into bkchr-execution-str…
bkchr Jun 5, 2023
65248e9
Fix compilation
bkchr Jun 5, 2023
67b1730
Rename `babe_link`
bkchr Jun 5, 2023
b1bb12d
Rename to `offchain_tx_pool_factory`
bkchr Jun 5, 2023
d4de293
Merge remote-tracking branch 'origin/master' into bkchr-execution-str…
bkchr Jun 14, 2023
85ccb89
Cleanup
bkchr Jun 14, 2023
5042fb0
FMT
bkchr Jun 14, 2023
99a5ca8
Fix benchmark name
bkchr Jun 15, 2023
32e69a2
Fix `try-runtime`
bkchr Jun 15, 2023
38191af
Remove `--execution` CLI args
bkchr Jun 15, 2023
8960448
Make clippy happy
bkchr Jun 16, 2023
42301cd
Forward bls functions
bkchr Jun 16, 2023
296ffd5
Merge branch 'bkchr-execution-strategies-remove' of github.com:parity…
bkchr Jun 16, 2023
e4bfccc
Fix docs
bkchr Jun 16, 2023
ae756aa
Merge remote-tracking branch 'origin/master' into bkchr-execution-str…
bkchr Jun 20, 2023
22c5603
Update UI tests
bkchr Jun 20, 2023
74ac48e
Update client/api/src/execution_extensions.rs
bkchr Jun 24, 2023
b07fde8
Apply suggestions from code review
bkchr Jun 24, 2023
8b3f2d9
Merge remote-tracking branch 'origin/master' into bkchr-execution-str…
bkchr Jun 24, 2023
ec9e872
Update client/cli/src/params/import_params.rs
bkchr Jun 24, 2023
e4781c4
Update client/api/src/execution_extensions.rs
bkchr Jun 24, 2023
4115970
Merge remote-tracking branch 'origin/master' into bkchr-execution-str…
bkchr Jun 28, 2023
c668c7c
Merge remote-tracking branch 'origin/master' into bkchr-execution-str…
bkchr Jun 29, 2023
9d61976
Merge branch 'bkchr-execution-strategies-remove' of github.com:parity…
bkchr Jun 30, 2023
3c71aa1
Pass the offchain storage to the MMR RPC
bkchr Jun 30, 2023
0c5c43d
Merge remote-tracking branch 'origin/master' into bkchr-execution-str…
bkchr Jul 5, 2023
7079d01
Update client/api/src/execution_extensions.rs
bkchr Jul 10, 2023
ad87780
Review comments
bkchr Jul 10, 2023
dc65806
Merge remote-tracking branch 'origin/master' into bkchr-execution-str…
bkchr Jul 10, 2023
792fbcc
Fixes
bkchr Jul 10, 2023
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
Prev Previous commit
Next Next commit
Pass the offchain storage to the MMR RPC
  • Loading branch information
bkchr committed Jun 30, 2023
commit 3c71aa160b8ee594aecf924c66dcadbb8b8c8cdc
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,10 @@ pub fn new_partial(
finality_provider: finality_proof_provider.clone(),
},
statement_store: rpc_statement_store.clone(),
backend: rpc_backend.clone(),
};

node_rpc::create_full(deps, rpc_backend.clone()).map_err(Into::into)
node_rpc::create_full(deps).map_err(Into::into)
};

(rpc_extensions_builder, shared_voter_state2)
Expand Down
35 changes: 22 additions & 13 deletions bin/node/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,23 @@ pub struct FullDeps<C, P, SC, B> {
pub grandpa: GrandpaDeps<B>,
/// Shared statement store reference.
pub statement_store: Arc<dyn sp_statement_store::StatementStore>,
/// The backend used by the node.
pub backend: Arc<B>,
}

/// Instantiate all Full RPC extensions.
pub fn create_full<C, P, SC, B>(
deps: FullDeps<C, P, SC, B>,
backend: Arc<B>,
FullDeps {
client,
pool,
select_chain,
chain_spec,
deny_unsafe,
babe,
grandpa,
statement_store,
backend,
}: FullDeps<C, P, SC, B>,
) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>
where
C: ProvideRuntimeApi<Block>
Expand Down Expand Up @@ -130,16 +141,6 @@ where
use substrate_state_trie_migration_rpc::{StateMigration, StateMigrationApiServer};

let mut io = RpcModule::new(());
let FullDeps {
client,
pool,
select_chain,
chain_spec,
deny_unsafe,
babe,
grandpa,
statement_store,
} = deps;

let BabeDeps { keystore, babe_worker_handle } = babe;
let GrandpaDeps {
Expand All @@ -159,7 +160,15 @@ where
// Making synchronous calls in light client freezes the browser currently,
// more context: https://github.com/paritytech/substrate/pull/3480
// These RPCs should use an asynchronous caller instead.
io.merge(Mmr::new(client.clone()).into_rpc())?;
io.merge(
Mmr::new(
client.clone(),
backend
.offchain_storage()
.ok_or_else(|| "Backend doesn't provide an offchain storage")?,
)
.into_rpc(),
)?;
io.merge(TransactionPayment::new(client.clone()).into_rpc())?;
io.merge(
Babe::new(client.clone(), babe_worker_handle.clone(), keystore, select_chain, deny_unsafe)
Expand Down
29 changes: 19 additions & 10 deletions client/merkle-mountain-range/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ use jsonrpsee::{
};
use serde::{Deserialize, Serialize};

use sp_api::{NumberFor, ProvideRuntimeApi};
use sp_api::{ApiExt, NumberFor, ProvideRuntimeApi};
use sp_blockchain::HeaderBackend;
use sp_core::Bytes;
use sp_core::{
offchain::{storage::OffchainDb, OffchainDbExt, OffchainStorage},
Bytes,
};
use sp_mmr_primitives::{Error as MmrError, Proof};
use sp_runtime::traits::Block as BlockT;

Expand Down Expand Up @@ -127,26 +130,28 @@ pub trait MmrApi<BlockHash, BlockNumber, MmrHash> {
}

/// MMR RPC methods.
pub struct Mmr<Client, Block> {
pub struct Mmr<Client, Block, S> {
client: Arc<Client>,
offchain_db: OffchainDb<S>,
_marker: PhantomData<Block>,
}

impl<C, B> Mmr<C, B> {
impl<C, B, S> Mmr<C, B, S> {
/// Create new `Mmr` with the given reference to the client.
pub fn new(client: Arc<C>) -> Self {
Self { client, _marker: Default::default() }
pub fn new(client: Arc<C>, offchain_storage: S) -> Self {
Self { client, _marker: Default::default(), offchain_db: OffchainDb::new(offchain_storage) }
}
}

#[async_trait]
impl<Client, Block, MmrHash> MmrApiServer<<Block as BlockT>::Hash, NumberFor<Block>, MmrHash>
for Mmr<Client, (Block, MmrHash)>
impl<Client, Block, MmrHash, S> MmrApiServer<<Block as BlockT>::Hash, NumberFor<Block>, MmrHash>
for Mmr<Client, (Block, MmrHash), S>
where
Block: BlockT,
Client: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
Client::Api: MmrRuntimeApi<Block, MmrHash, NumberFor<Block>>,
MmrHash: Codec + Send + Sync + 'static,
S: OffchainStorage + 'static,
{
fn mmr_root(&self, at: Option<<Block as BlockT>::Hash>) -> RpcResult<MmrHash> {
let block_hash = at.unwrap_or_else(||
Expand All @@ -166,11 +171,13 @@ where
best_known_block_number: Option<NumberFor<Block>>,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<LeavesProof<<Block as BlockT>::Hash>> {
let api = self.client.runtime_api();
let mut api = self.client.runtime_api();
let block_hash = at.unwrap_or_else(||
// If the block hash is not supplied assume the best block.
self.client.info().best_hash);

api.register_extension(OffchainDbExt::new(self.offchain_db.clone()));

let (leaves, proof) = api
.generate_proof(block_hash, block_numbers, best_known_block_number)
.map_err(runtime_error_into_rpc_error)?
Expand All @@ -180,14 +187,16 @@ where
}

fn verify_proof(&self, proof: LeavesProof<<Block as BlockT>::Hash>) -> RpcResult<bool> {
let api = self.client.runtime_api();
let mut api = self.client.runtime_api();

let leaves = Decode::decode(&mut &proof.leaves.0[..])
.map_err(|e| CallError::InvalidParams(anyhow::Error::new(e)))?;

let decoded_proof = Decode::decode(&mut &proof.proof.0[..])
.map_err(|e| CallError::InvalidParams(anyhow::Error::new(e)))?;

api.register_extension(OffchainDbExt::new(self.offchain_db.clone()));

api.verify_proof(proof.block_hash, leaves, decoded_proof)
.map_err(runtime_error_into_rpc_error)?
.map_err(mmr_error_into_rpc_error)?;
Expand Down
109 changes: 4 additions & 105 deletions client/offchain/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub use http::SharedClient;
use libp2p::{Multiaddr, PeerId};
use sp_core::{
offchain::{
self, HttpError, HttpRequestId, HttpRequestStatus, OffchainStorage, OpaqueMultiaddr,
OpaqueNetworkState, StorageKind, Timestamp,
self, HttpError, HttpRequestId, HttpRequestStatus, OpaqueMultiaddr, OpaqueNetworkState,
Timestamp,
},
OpaquePeerId,
};
Expand All @@ -36,107 +36,6 @@ mod http;

mod timestamp;

fn unavailable_yet<R: Default>(name: &str) -> R {
tracing::error!(
target: super::LOG_TARGET,
"The {:?} API is not available for offchain workers yet. Follow \
https://github.com/paritytech/substrate/issues/1458 for details",
name
);
Default::default()
}

const LOCAL_DB: &str = "LOCAL (fork-aware) DB";

/// Offchain DB reference.
#[derive(Debug, Clone)]
pub struct Db<Storage> {
/// Persistent storage database.
persistent: Storage,
}

impl<Storage> Db<Storage> {
/// Create new instance of Offchain DB.
pub fn new(persistent: Storage) -> Self {
Self { persistent }
}

/// Create new instance of Offchain DB, backed by the given backend.
pub fn from_backend<Backend, Block>(backend: &Backend) -> Option<Db<Backend::OffchainStorage>>
where
Backend: sc_client_api::Backend<Block>,
Block: sp_runtime::traits::Block,
{
sc_client_api::Backend::offchain_storage(backend).map(|db| Db::new(db))
}
}

impl<Storage: OffchainStorage> offchain::DbExternalities for Db<Storage> {
fn local_storage_set(&mut self, kind: StorageKind, key: &[u8], value: &[u8]) {
tracing::debug!(
target: "offchain-worker::storage",
?kind,
key = ?array_bytes::bytes2hex("", key),
value = ?array_bytes::bytes2hex("", value),
"Write",
);
match kind {
StorageKind::PERSISTENT => self.persistent.set(STORAGE_PREFIX, key, value),
StorageKind::LOCAL => unavailable_yet(LOCAL_DB),
}
}

fn local_storage_clear(&mut self, kind: StorageKind, key: &[u8]) {
tracing::debug!(
target: "offchain-worker::storage",
?kind,
key = ?array_bytes::bytes2hex("", key),
"Clear",
);
match kind {
StorageKind::PERSISTENT => self.persistent.remove(STORAGE_PREFIX, key),
StorageKind::LOCAL => unavailable_yet(LOCAL_DB),
}
}

fn local_storage_compare_and_set(
&mut self,
kind: StorageKind,
key: &[u8],
old_value: Option<&[u8]>,
new_value: &[u8],
) -> bool {
tracing::debug!(
target: "offchain-worker::storage",
?kind,
key = ?array_bytes::bytes2hex("", key),
new_value = ?array_bytes::bytes2hex("", new_value),
old_value = ?old_value.as_ref().map(|s| array_bytes::bytes2hex("", s)),
"CAS",
);
match kind {
StorageKind::PERSISTENT =>
self.persistent.compare_and_set(STORAGE_PREFIX, key, old_value, new_value),
StorageKind::LOCAL => unavailable_yet(LOCAL_DB),
}
}

fn local_storage_get(&mut self, kind: StorageKind, key: &[u8]) -> Option<Vec<u8>> {
let result = match kind {
StorageKind::PERSISTENT => self.persistent.get(STORAGE_PREFIX, key),
StorageKind::LOCAL => unavailable_yet(LOCAL_DB),
};
tracing::debug!(
target: "offchain-worker::storage",
?kind,
key = ?array_bytes::bytes2hex("", key),
result = ?result.as_ref().map(|s| array_bytes::bytes2hex("", s)),
"Read",
);
result
}
}

/// Asynchronous offchain API.
///
/// NOTE this is done to prevent recursive calls into the runtime
Expand Down Expand Up @@ -326,7 +225,7 @@ mod tests {
config::MultiaddrWithPeerId, types::ProtocolName, NetworkPeers, NetworkStateInfo,
ReputationChange,
};
use sp_core::offchain::{DbExternalities, Externalities};
use sp_core::offchain::{storage::OffchainDb, DbExternalities, Externalities, StorageKind};
use std::time::SystemTime;

pub(super) struct TestNetwork();
Expand Down Expand Up @@ -416,7 +315,7 @@ mod tests {
}

fn offchain_db() -> Db<LocalStorage> {
Db::new(LocalStorage::new_test())
OffchainDb::new(LocalStorage::new_test())
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions client/offchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use threadpool::ThreadPool;

mod api;

pub use api::Db as OffchainDb;
pub use sp_core::offchain::storage::OffchainDb;
pub use sp_offchain::{OffchainWorkerApi, STORAGE_PREFIX};

const LOG_TARGET: &str = "offchain-worker";
Expand Down Expand Up @@ -133,7 +133,7 @@ pub struct OffchainWorkers<RA, Block: traits::Block, Storage> {
shared_http_client: api::SharedClient,
enable_http_requests: bool,
keystore: Option<KeystorePtr>,
offchain_db: Option<api::Db<Storage>>,
offchain_db: Option<OffchainDb<Storage>>,
transaction_pool: Option<OffchainTransactionPoolFactory<Block>>,
network_provider: Arc<dyn NetworkProvider + Send + Sync>,
is_validator: bool,
Expand Down Expand Up @@ -163,7 +163,7 @@ impl<RA, Block: traits::Block, Storage> OffchainWorkers<RA, Block, Storage> {
shared_http_client: api::SharedClient::new(),
enable_http_requests,
keystore,
offchain_db: offchain_db.map(|d| api::Db::new(d)),
offchain_db: offchain_db.map(OffchainDb::new),
transaction_pool,
is_validator,
network_provider,
Expand Down
2 changes: 2 additions & 0 deletions primitives/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ sp-externalities = { version = "0.19.0", optional = true, path = "../externaliti
futures = { version = "0.3.21", optional = true }
dyn-clonable = { version = "0.9.0", optional = true }
thiserror = { version = "1.0.30", optional = true }
tracing = { version = "0.1.29", optional = true }
bitflags = "1.3"
paste = "1.0.7"

Expand Down Expand Up @@ -113,6 +114,7 @@ std = [
"futures/thread-pool",
"libsecp256k1/std",
"dyn-clonable",
"tracing",
]

# Serde support without relying on std features.
Expand Down
Loading