Skip to content

Commit 340b911

Browse files
rphmeierpepyakin
authored andcommitted
integrate into sovereign-sdk adapter
1 parent f5d4fe7 commit 340b911

File tree

6 files changed

+51
-29
lines changed

6 files changed

+51
-29
lines changed

sugondat-shim/src/adapters/sovereign.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ use jsonrpsee::types::ErrorObjectOwned;
22
use sugondat_shim_common_sovereign::{Block, SovereignRPCServer};
33
use tracing::info;
44

5-
use crate::sugondat_rpc;
5+
use crate::{key::Keypair, sugondat_rpc};
66

77
pub struct SovereignAdapter {
88
client: sugondat_rpc::Client,
9+
submit_key: Option<Keypair>,
910
}
1011

1112
impl SovereignAdapter {
12-
pub fn new(client: sugondat_rpc::Client) -> Self {
13-
Self { client }
13+
pub fn new(client: sugondat_rpc::Client, submit_key: Option<Keypair>) -> Self {
14+
Self { client, submit_key }
1415
}
1516
}
1617

@@ -50,7 +51,17 @@ impl SovereignRPCServer for SovereignAdapter {
5051
namespace: sugondat_nmt::Namespace,
5152
) -> Result<(), ErrorObjectOwned> {
5253
info!("submit_blob({}, {:?})", blob.len(), namespace);
53-
self.client.submit_blob(blob, namespace).await.unwrap();
54+
55+
let submit_key = match self.submit_key.as_ref() {
56+
Some(k) => k.clone(),
57+
None => return Err(ErrorObjectOwned::owned(
58+
jsonrpsee::types::error::INTERNAL_ERROR_CODE,
59+
"Internal Error: no key for signing blobs",
60+
None::<()>,
61+
)),
62+
};
63+
64+
self.client.submit_blob(blob, namespace, submit_key).await.unwrap();
5465
Ok(())
5566
}
5667
}

sugondat-shim/src/cli.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ pub struct Cli {
3737
pub command: Commands,
3838
}
3939

40-
/// Common parameters for key management subcommand.
40+
/// Common parameters for key management in a subcommand.
41+
// TODO: for adapters, this should not be required and for query submit it should
42+
// be. Unfortunately, clap doesn't support this easily so it is handled manually
43+
// within the command execution for submit.
4144
#[derive(clap::Args, Debug)]
42-
#[group(required = true, multiple = false)]
45+
#[group(multiple = false)]
4346
pub struct KeyManagementParams {
4447
/// Use the Alice development key to sign blob transactions.
4548
///
@@ -60,9 +63,6 @@ pub struct KeyManagementParams {
6063
/// Common parameters for the adapter subcommands.
6164
#[derive(clap::Args, Debug)]
6265
pub struct AdapterServerParams {
63-
#[clap(flatten)]
64-
pub key_management: KeyManagementParams,
65-
6666
/// The address on which the shim should listen for incoming connections from the rollup nodes.
6767
#[clap(short, long, default_value = "127.0.0.1", group = "listen")]
6868
pub address: String,
@@ -108,7 +108,7 @@ pub enum Commands {
108108
pub mod serve {
109109
//! CLI definition for the `serve` subcommand.
110110
111-
use super::{AdapterServerParams, SugondatRpcParams};
111+
use super::{AdapterServerParams, KeyManagementParams, SugondatRpcParams};
112112
use clap::Args;
113113

114114
#[derive(Debug, Args)]
@@ -118,6 +118,9 @@ pub mod serve {
118118

119119
#[clap(flatten)]
120120
pub adapter: AdapterServerParams,
121+
122+
#[clap(flatten)]
123+
pub key_management: KeyManagementParams,
121124
}
122125
}
123126

sugondat-shim/src/cmd/mod.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,12 @@ fn init_logging() -> anyhow::Result<()> {
3131
Ok(())
3232
}
3333

34-
fn fetch_key(params: crate::cli::KeyManagementParams) -> anyhow::Result<key::Keypair> {
34+
fn load_key(params: crate::cli::KeyManagementParams) -> anyhow::Result<Option<key::Keypair>> {
3535
if params.submit_dev_alice {
36-
return Ok(key::alice())
36+
Ok(Some(key::alice()))
37+
} else if let Some(path) = params.submit_private_key {
38+
Ok(Some(key::load(path)?))
39+
} else {
40+
Ok(None)
3741
}
38-
39-
if let Some(path) = params.submit_private_key {
40-
return key::load(path)
41-
}
42-
43-
// sanity: clap is supposed to prevent this
44-
return Err(anyhow::anyhow!("No blob submission key specified. See --help"))
4542
}

sugondat-shim/src/cmd/query/submit.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@ pub async fn run(params: Params) -> anyhow::Result<()> {
1212
} = params;
1313
let blob = read_blob(&blob_path)
1414
.with_context(|| format!("cannot read blob file path '{}'", blob_path))?;
15+
16+
let key = crate::cmd::load_key(key_management)
17+
.with_context(|| format!("cannot load submission signing key"))?
18+
.ok_or_else(|| anyhow::anyhow!("submission signing key required"))?;
19+
1520
let namespace = read_namespace(&namespace)?;
1621
let client = connect_rpc(rpc).await?;
1722
tracing::info!("submitting blob to namespace {}", namespace);
18-
let block_hash = client.submit_blob(blob, namespace).await?;
23+
let block_hash = client.submit_blob(blob, namespace, key).await?;
1924
tracing::info!("submitted blob to block hash 0x{}", hex::encode(block_hash));
2025
Ok(())
2126
}

sugondat-shim/src/cmd/serve.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{
22
adapters::sovereign::SovereignAdapter,
33
cli::{serve::Params, AdapterServerParams},
4+
key::Keypair,
45
sugondat_rpc::Client,
56
};
67

@@ -14,9 +15,10 @@ pub async fn run(params: Params) -> anyhow::Result<()> {
1415
params.adapter.address, params.adapter.port
1516
);
1617
let listen_on = (params.adapter.address.as_str(), params.adapter.port);
18+
let maybe_key = crate::cmd::load_key(params.key_management)?;
1719
let server = Server::builder().build(listen_on).await?;
1820
let client = connect_client(&params.rpc.node_url).await?;
19-
let handle = server.start(init_adapters(client, &params.adapter));
21+
let handle = server.start(init_adapters(client, &params.adapter, maybe_key));
2022
handle.stopped().await;
2123
Ok(())
2224
}
@@ -26,11 +28,15 @@ async fn connect_client(url: &str) -> anyhow::Result<Client> {
2628
Ok(client)
2729
}
2830

29-
fn init_adapters(client: Client, adapter: &AdapterServerParams) -> Methods {
31+
fn init_adapters(
32+
client: Client,
33+
adapter: &AdapterServerParams,
34+
maybe_key: Option<Keypair>,
35+
) -> Methods {
3036
let mut methods = Methods::new();
3137
if adapter.enable_sovereign() {
3238
debug!("enabling sovereign adapter");
33-
let adapter = SovereignAdapter::new(client.clone());
39+
let adapter = SovereignAdapter::new(client.clone(), maybe_key);
3440
methods.merge(adapter.into_rpc()).unwrap();
3541
}
3642
methods

sugondat-shim/src/sugondat_rpc.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use sugondat_nmt::Namespace;
44
use sugondat_subxt::{
55
sugondat::runtime_types::bounded_collections::bounded_vec::BoundedVec, Header,
66
};
7+
use crate::key::Keypair;
78

89
// NOTE: we specifically avoid prolifiration of subxt types around the codebase. To that end, we
910
// avoid returning H256 and instead return [u8; 32] directly.
@@ -79,25 +80,24 @@ impl Client {
7980
})
8081
}
8182

82-
/// Submit a blob with the given namespace. Returns a block hash in which the extrinsic was
83-
/// included.
83+
/// Submit a blob with the given namespace and signed with the given key.
84+
///
85+
/// Returns a block hash in which the extrinsic was included.
8486
pub async fn submit_blob(
8587
&self,
8688
blob: Vec<u8>,
8789
namespace: sugondat_nmt::Namespace,
90+
key: Keypair,
8891
) -> anyhow::Result<[u8; 32]> {
89-
use subxt_signer::sr25519::dev;
90-
9192
let namespace_id = namespace.namespace_id();
9293
let extrinsic = sugondat_subxt::sugondat::tx()
9394
.blob()
9495
.submit_blob(namespace_id, BoundedVec(blob));
9596

96-
let from = dev::alice();
9797
let signed = self
9898
.subxt
9999
.tx()
100-
.create_signed(&extrinsic, &from, Default::default())
100+
.create_signed(&extrinsic, &key, Default::default())
101101
.await
102102
.with_context(|| format!("failed to validate or sign extrinsic with dev key pair"))?;
103103

0 commit comments

Comments
 (0)