Skip to content

Commit 645cd7a

Browse files
committed
wip
1 parent 64a4e81 commit 645cd7a

File tree

10 files changed

+63
-114
lines changed

10 files changed

+63
-114
lines changed

crates/cartridge/src/vrf/bootstrap.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub fn derive_vrf_accounts(
9595
) -> Result<VrfDerivedAccounts> {
9696
// vrf-server expects a u64 secret, so derive one from the account key.
9797
let secret_key = vrf_secret_key_from_account_key(source_private_key);
98-
let public_key = generate_public_key(scalar_from_felt(Felt::from(secret_key)));
98+
let public_key = generate_public_key(scalar_from_felt(secret_key.into()));
9999
let vrf_public_key_x = felt_from_field(public_key.x)?;
100100
let vrf_public_key_y = felt_from_field(public_key.y)?;
101101

@@ -154,7 +154,6 @@ pub async fn bootstrap_vrf(config: &VrfBootstrapConfig) -> Result<VrfBootstrapRe
154154
chain_id_felt,
155155
ExecutionEncoding::New,
156156
);
157-
account.set_block_id(BlockId::Tag(BlockTag::PreConfirmed));
158157

159158
// Deploy VRF account if not already deployed
160159
if !is_deployed(&provider, vrf_account_address).await? {

crates/cartridge/src/vrf/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ mod client;
1010
pub mod bootstrap;
1111
pub mod sidecar;
1212

13-
pub use client::*;
14-
1513
pub use bootstrap::{
1614
bootstrap_vrf, derive_vrf_accounts, vrf_account_class_hash, vrf_consumer_class_hash,
1715
vrf_secret_key_from_account_key, VrfBootstrap, VrfBootstrapConfig, VrfBootstrapResult,
1816
VrfDerivedAccounts, BOOTSTRAP_TIMEOUT, VRF_ACCOUNT_SALT, VRF_CONSUMER_SALT,
1917
};
18+
pub use client::*;
2019
pub use sidecar::{
2120
resolve_executable, wait_for_http_ok, Error as VrfSidecarError, Result as VrfSidecarResult,
2221
VrfService, VrfServiceConfig, VrfServiceProcess, SIDECAR_TIMEOUT, VRF_SERVER_PORT,

crates/cartridge/src/vrf/sidecar.rs

Lines changed: 21 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,24 @@
22
//!
33
//! This module handles spawning and managing the VRF sidecar process.
44
5-
use std::env;
6-
use std::io;
75
use std::path::{Path, PathBuf};
86
use std::process::Stdio;
97
use std::time::{Duration, Instant};
8+
use std::{env, io};
109

1110
use katana_primitives::{ContractAddress, Felt};
1211
use tokio::process::{Child, Command};
1312
use tokio::time::sleep;
1413
use tracing::{debug, info, warn};
1514
use url::Url;
1615

17-
use super::bootstrap::{bootstrap_vrf, VrfBootstrapConfig, VrfBootstrapResult};
18-
1916
const LOG_TARGET: &str = "katana::cartridge::vrf::sidecar";
2017

2118
/// Fixed port used by vrf-server.
2219
pub const VRF_SERVER_PORT: u16 = 3000;
2320

21+
const DEFAULT_VRF_SERVICE_PATH: &str = "vrf-service";
22+
2423
/// Default timeout for waiting on sidecar readiness.
2524
pub const SIDECAR_TIMEOUT: Duration = Duration::from_secs(10);
2625

@@ -47,91 +46,51 @@ pub enum Error {
4746
Bootstrap(#[source] anyhow::Error),
4847
}
4948

50-
/// Result type alias for VRF sidecar operations.
5149
pub type Result<T, E = Error> = std::result::Result<T, E>;
5250

5351
// ============================================================================
54-
// Configuration Types
52+
// VRF Service
5553
// ============================================================================
5654

57-
/// Configuration for the VRF service.
5855
#[derive(Debug, Clone)]
5956
pub struct VrfServiceConfig {
60-
/// RPC URL of the katana node (for bootstrap).
61-
pub rpc_url: Url,
62-
/// Source account address (deploys contracts and funds VRF account).
63-
pub source_address: ContractAddress,
64-
/// Source account private key.
65-
pub source_private_key: Felt,
66-
/// Path to the vrf-server binary (None = lookup in PATH).
67-
pub program_path: Option<PathBuf>,
57+
pub vrf_account_address: ContractAddress,
58+
pub vrf_private_key: Felt,
59+
pub secret_key: u64,
6860
}
6961

70-
// ============================================================================
71-
// VRF Service
72-
// ============================================================================
73-
74-
/// VRF service that handles bootstrapping and spawning the sidecar process.
7562
#[derive(Debug, Clone)]
7663
pub struct VrfService {
7764
config: VrfServiceConfig,
78-
bootstrap_result: Option<VrfBootstrapResult>,
65+
path: PathBuf,
7966
}
8067

8168
impl VrfService {
82-
/// Create a new VRF service with the given configuration.
8369
pub fn new(config: VrfServiceConfig) -> Self {
84-
Self { config, bootstrap_result: None }
70+
Self { config, path: PathBuf::from(DEFAULT_VRF_SERVICE_PATH) }
8571
}
8672

87-
/// Set a pre-existing bootstrap result, skipping the bootstrap step.
73+
/// Sets the path to the vrf service program.
8874
///
89-
/// Use this when the VRF contracts have already been deployed.
90-
pub fn bootstrap_result(mut self, result: VrfBootstrapResult) -> Self {
91-
self.bootstrap_result = Some(result);
75+
/// If no path is set, the default executable name [`DEFAULT_VRF_SERVICE_PATH`] will be used.
76+
pub fn path<T: Into<PathBuf>>(mut self, path: T) -> Self {
77+
self.path = path.into();
9278
self
9379
}
9480

95-
/// Get the bootstrap result, if set.
96-
pub fn get_bootstrap_result(&self) -> Option<&VrfBootstrapResult> {
97-
self.bootstrap_result.as_ref()
98-
}
99-
100-
/// Bootstrap the VRF service by deploying necessary contracts.
101-
///
102-
/// This deploys the VRF account and consumer contracts via RPC,
103-
/// sets up the VRF public key, and optionally funds the account.
104-
pub async fn bootstrap(&mut self) -> Result<&VrfBootstrapResult> {
105-
let bootstrap_config = VrfBootstrapConfig {
106-
rpc_url: self.config.rpc_url.clone(),
107-
source_address: self.config.source_address,
108-
source_private_key: self.config.source_private_key,
109-
};
110-
111-
let result = bootstrap_vrf(&bootstrap_config).await.map_err(Error::Bootstrap)?;
112-
self.bootstrap_result = Some(result);
113-
114-
Ok(self.bootstrap_result.as_ref().expect("just set"))
115-
}
116-
117-
/// Start the VRF sidecar process.
118-
///
119-
/// This spawns the vrf-server binary and waits for it to become ready.
120-
/// Returns an error if bootstrap has not been performed.
12181
pub async fn start(self) -> Result<VrfServiceProcess> {
122-
let bootstrap_result = self.bootstrap_result.ok_or(Error::BootstrapResultNotSet)?.clone();
123-
124-
let bin = self.config.program_path.unwrap_or_else(|| "vrf-server".into());
125-
let bin = resolve_executable(Path::new(&bin))?;
82+
let bin = resolve_executable(&self.path)?;
12683

12784
let mut command = Command::new(bin);
12885
command
12986
.arg("--port")
13087
.arg(VRF_SERVER_PORT.to_string())
13188
.arg("--account-address")
89+
.arg(self.config.vrf_account_address.to_hex_string())
13290
.arg("--account-private-key")
91+
.arg(self.config.vrf_private_key.to_hex_string())
13392
.arg("--secret-key")
134-
.arg(bootstrap_result.secret_key.to_string())
93+
.arg(self.config.secret_key.to_string())
13594
.stdout(Stdio::inherit())
13695
.stderr(Stdio::inherit())
13796
.kill_on_drop(true);
@@ -141,33 +100,26 @@ impl VrfService {
141100
let url = format!("http://127.0.0.1:{VRF_SERVER_PORT}/info",);
142101
wait_for_http_ok(&url, "vrf info", SIDECAR_TIMEOUT).await?;
143102

144-
Ok(VrfServiceProcess { process, bootstrap_result })
103+
Ok(VrfServiceProcess { process, inner: self })
145104
}
146105
}
147106

148-
// ============================================================================
149-
// VRF Sidecar Process
150-
// ============================================================================
151-
152107
/// A running VRF sidecar process.
153108
#[derive(Debug)]
154109
pub struct VrfServiceProcess {
155110
process: Child,
156-
bootstrap_result: VrfBootstrapResult,
111+
inner: VrfService,
157112
}
158113

159114
impl VrfServiceProcess {
160-
/// Get a mutable reference to the underlying child process.
161115
pub fn process(&mut self) -> &mut Child {
162116
&mut self.process
163117
}
164118

165-
/// Get the bootstrap result containing VRF account information.
166-
pub fn bootstrap_result(&self) -> &VrfBootstrapResult {
167-
&self.bootstrap_result
119+
pub fn config(&self) -> &VrfServiceConfig {
120+
&self.inner.config
168121
}
169122

170-
/// Gracefully shutdown the sidecar process.
171123
pub async fn shutdown(&mut self) -> io::Result<()> {
172124
self.process.kill().await
173125
}

crates/cli/src/args.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ use url::Url;
3636

3737
use crate::file::NodeArgsConfig;
3838
use crate::options::*;
39-
#[cfg(feature = "cartridge")]
40-
use crate::sidecar::bootstrap_and_start_sidecars;
4139
#[cfg(feature = "vrf")]
4240
use crate::sidecar::{build_vrf_config, VrfSidecarInfo};
4341
use crate::utils::{self, parse_chain_config_dir, parse_seed};

crates/cli/src/sidecar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,8 @@ pub async fn start_sidecars(
286286
// .rpc(paymaster_cfg.rpc_url.clone())
287287
// .port(paymaster_cfg.port)
288288
// .api_key(paymaster_cfg.api_key.clone())
289-
// .relayer(paymaster_bootstrap.relayer_address, paymaster_bootstrap.relayer_private_key)
290-
// .gas_tank(
289+
// .relayer(paymaster_bootstrap.relayer_address,
290+
// paymaster_bootstrap.relayer_private_key) .gas_tank(
291291
// paymaster_bootstrap.gas_tank_address,
292292
// paymaster_bootstrap.gas_tank_private_key,
293293
// )

crates/node/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ vrf = [
6262
"katana-rpc-server/vrf",
6363
"dep:stark-vrf",
6464
"dep:ark-ff",
65+
"cartridge",
6566
]
6667
cartridge = [
6768
"katana-rpc-api/cartridge",
6869
"katana-rpc-server/cartridge",
6970
"paymaster",
70-
"vrf",
7171
]
7272
explorer = ["katana-rpc-server/explorer"]
7373
native = ["katana-executor/native"]

crates/node/src/config/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ pub struct Config {
6464
#[cfg(feature = "paymaster")]
6565
pub paymaster: Option<paymaster::PaymasterConfig>,
6666

67-
/// VRF options.
68-
#[cfg(feature = "vrf")]
69-
pub vrf: Option<paymaster::VrfConfig>,
70-
7167
/// TEE attestation options.
7268
#[cfg(feature = "tee")]
7369
pub tee: Option<tee::TeeConfig>,

crates/node/src/config/paymaster.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,6 @@ pub struct PaymasterConfig {
1212
pub cartridge_api: Option<CartridgeApiConfig>,
1313
}
1414

15-
/// Configuration for connecting to a VRF service.
16-
///
17-
/// The node treats the VRF as an external service - it simply connects to the
18-
/// provided URL. Whether the service is managed externally or as a sidecar process
19-
/// is a concern of the CLI layer, not the node.
20-
#[derive(Debug, Clone, PartialEq, Eq)]
21-
pub struct VrfConfig {
22-
/// The VRF service URL.
23-
pub url: Url,
24-
}
25-
2615
/// Configuration for connecting to a Cartridge paymaster service.
2716
///
2817
/// The node treats the paymaster as an external service - it simply connects to the
@@ -40,4 +29,21 @@ pub struct CartridgeApiConfig {
4029
pub controller_deployer_address: ContractAddress,
4130
/// The paymaster account private key. (used for deploying controller)
4231
pub controller_deployer_private_key: Felt,
32+
33+
#[cfg(feature = "vrf")]
34+
pub vrf: Option<VrfConfig>,
35+
}
36+
37+
/// Configuration for connecting to a VRF service.
38+
///
39+
/// The node treats the VRF as an external service - it simply connects to the
40+
/// provided URL. Whether the service is managed externally or as a sidecar process
41+
/// is not a concern of the [`Node`](crate::Node).
42+
#[cfg(feature = "vrf")]
43+
#[derive(Debug, Clone, PartialEq, Eq)]
44+
pub struct VrfConfig {
45+
/// The VRF service URL.
46+
pub url: Url,
47+
/// The address of the VRF contract.
48+
pub vrf_contract: ContractAddress,
4349
}

crates/node/src/lib.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -232,42 +232,41 @@ where
232232
);
233233

234234
#[cfg(feature = "vrf")]
235-
let vrf = if let Some(vrf) = &config.vrf {
235+
let vrf = if let Some(vrf) = &cartridge_api_cfg.vrf {
236236
use url::Url;
237237

238-
let derived = crate::sidecar::derive_vrf_accounts(vrf, &config, &backend)?;
239238
let rpc_url = Url::parse(&format!("http://{}", config.rpc.socket_addr()))
240239
.expect("valid rpc url");
241240

242241
Some(katana_rpc_server::cartridge::VrfServiceConfig {
243242
rpc_url,
244-
url: vrf.url.clone(),
245-
account_address: derived.vrf_account_address,
243+
service_url: vrf.url.clone(),
244+
vrf_contract: vrf.vrf_contract,
246245
})
247246
} else {
248247
None
249248
};
250249

251-
#[cfg(not(feature = "vrf"))]
252-
let vrf = None;
250+
let cartridge_api_config = CartridgeConfig {
251+
paymaster_url: cfg.url.clone(),
252+
paymaster_api_key: cfg.api_key.clone(),
253+
api_url: cartridge_api_cfg.cartridge_api_url.clone(),
254+
controller_deployer_address: cartridge_api_cfg.controller_deployer_address,
255+
controller_deployer_private_key: cartridge_api_cfg
256+
.controller_deployer_private_key,
257+
#[cfg(feature = "vrf")]
258+
vrf,
259+
};
253260

254-
let api = CartridgeApi::new(
261+
let cartrige_api = CartridgeApi::new(
255262
backend.clone(),
256263
block_producer.clone(),
257264
pool.clone(),
258265
task_spawner.clone(),
259-
CartridgeConfig {
260-
paymaster_url: cfg.url.clone(),
261-
paymaster_api_key: cfg.api_key.clone(),
262-
api_url: cartridge_api_cfg.cartridge_api_url.clone(),
263-
controller_deployer_address: cartridge_api_cfg.controller_deployer_address,
264-
controller_deployer_private_key: cartridge_api_cfg
265-
.controller_deployer_private_key,
266-
vrf,
267-
},
266+
cartridge_api_config,
268267
)?;
269268

270-
rpc_modules.merge(CartridgeApiServer::into_rpc(api))?;
269+
rpc_modules.merge(CartridgeApiServer::into_rpc(cartrige_api))?;
271270

272271
Some(CartridgePaymasterConfig {
273272
cartridge_api_url: cartridge_api_cfg.cartridge_api_url.clone(),

crates/rpc/rpc-server/src/cartridge/vrf.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use url::Url;
1111

1212
#[derive(Debug, Clone)]
1313
pub struct VrfServiceConfig {
14-
pub url: Url,
15-
pub account_address: ContractAddress,
1614
pub rpc_url: Url,
15+
pub service_url: Url,
16+
pub vrf_contract: ContractAddress,
1717
}
1818

1919
#[derive(Clone)]
@@ -26,8 +26,8 @@ pub struct VrfService {
2626
impl VrfService {
2727
pub fn new(config: VrfServiceConfig) -> Self {
2828
Self {
29-
client: VrfClient::new(config.url),
30-
account_address: config.account_address,
29+
client: VrfClient::new(config.service_url),
30+
account_address: config.vrf_contract,
3131
rpc_url: config.rpc_url,
3232
}
3333
}

0 commit comments

Comments
 (0)