Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
520 changes: 339 additions & 181 deletions Cargo.lock

Large diffs are not rendered by default.

32 changes: 19 additions & 13 deletions kinode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,25 @@ simulation-mode = []

[dependencies]
aes-gcm = "0.10.3"
alloy-contract = { git = "https://github.com/alloy-rs/alloy", rev = "6f8ebb4" }
alloy-consensus = { git = "https://github.com/alloy-rs/alloy", rev = "6f8ebb4" }
alloy-pubsub = { git = "https://github.com/alloy-rs/alloy", rev = "6f8ebb4" }
alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy", rev = "6f8ebb4" }
alloy-json-rpc = { git = "https://github.com/alloy-rs/alloy", rev = "6f8ebb4" }
alloy-rpc-client = { git = "https://github.com/alloy-rs/alloy", rev = "6f8ebb4", features = ["ws"]}
alloy-transport-ws = { git = "https://github.com/alloy-rs/alloy", rev = "6f8ebb4" }
alloy-providers = { git = "https://github.com/alloy-rs/alloy", rev = "6f8ebb4" }
alloy-network = { git = "https://github.com/alloy-rs/alloy", rev = "6f8ebb4" }
alloy-primitives = "0.6.2"
alloy-sol-macro = "0.6.2"
alloy-sol-types = "0.6.2"
alloy-signer = { git = "https://github.com/alloy-rs/alloy", rev = "6f8ebb4" }
alloy = { git = "https://github.com/alloy-rs/alloy", rev = "05f8162", features = [
"consensus",
"contract",
"json-rpc",
"network",
"provider-ws",
"providers",
"pubsub",
"rpc-client-ws",
"rpc-client",
"rpc-types-eth",
"rpc-types",
"signer-wallet",
"signers",
] }

alloy-primitives = "0.7.5"
alloy-sol-macro = "0.7.5"
alloy-sol-types = "0.7.5"
anyhow = "1.0.71"
async-trait = "0.1.71"
base64 = "0.22.0"
Expand Down
31 changes: 14 additions & 17 deletions kinode/src/eth/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use alloy_json_rpc::RpcError;
use alloy_providers::provider::Provider;
use alloy_pubsub::PubSubFrontend;
use alloy_rpc_client::ClientBuilder;
use alloy_transport_ws::WsConnect;
use alloy::providers::{Provider, ProviderBuilder, RootProvider};
use alloy::pubsub::PubSubFrontend;
use alloy::rpc::client::WsConnect;
use alloy::rpc::json_rpc::RpcError;
use anyhow::Result;
use dashmap::DashMap;
use lib::types::core::*;
Expand Down Expand Up @@ -42,7 +41,7 @@ struct ActiveProviders {
struct UrlProvider {
pub trusted: bool,
pub url: String,
pub pubsub: Option<Provider<PubSubFrontend>>,
pub pubsub: Option<RootProvider<PubSubFrontend>>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -612,17 +611,13 @@ async fn fulfill_request(
}
}
};
match pubsub.inner().prepare(method, params.clone()).await {
match pubsub.raw_request(method.into(), params.clone()).await {
Ok(value) => {
let successful_provider = aps.urls.remove(index);
aps.urls.insert(0, successful_provider);
return EthResponse::Response { value };
}
Err(rpc_error) => {
// if rpc_error is of type ErrResponse, return to user!
if let RpcError::ErrorResp(err) = rpc_error {
return EthResponse::Err(EthError::RpcError(err));
}
verbose_print(
print_tx,
&format!(
Expand All @@ -631,6 +626,10 @@ async fn fulfill_request(
),
)
.await;
// if rpc_error is of type ErrResponse, return to user!
if let RpcError::ErrorResp(err) = rpc_error {
return EthResponse::Err(EthError::RpcError(err));
}
// this provider failed and needs to be reset
url_provider.pubsub = None;
}
Expand Down Expand Up @@ -858,16 +857,14 @@ async fn handle_eth_config_action(
async fn activate_url_provider(provider: &mut UrlProvider) -> Result<()> {
match Url::parse(&provider.url)?.scheme() {
"ws" | "wss" => {
let connector = WsConnect {
url: provider.url.to_string(),
auth: None,
};
let ws = WsConnect::new(provider.url.to_string());

let client = tokio::time::timeout(
std::time::Duration::from_secs(10),
ClientBuilder::default().ws(connector),
ProviderBuilder::new().on_ws(ws),
)
.await??;
provider.pubsub = Some(Provider::new_with_client(client));
provider.pubsub = Some(client);
Ok(())
}
_ => Err(anyhow::anyhow!(
Expand Down
11 changes: 5 additions & 6 deletions kinode/src/eth/subscription.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::eth::*;
use alloy_pubsub::RawSubscription;
use alloy_rpc_types::pubsub::SubscriptionResult;
use alloy::pubsub::RawSubscription;
use alloy::rpc::types::eth::pubsub::SubscriptionResult;

/// cleans itself up when the subscription is closed or fails.
pub async fn create_new_subscription(
Expand Down Expand Up @@ -212,12 +212,11 @@ async fn build_subscription(
let kind = serde_json::to_value(&kind).unwrap();
let params = serde_json::to_value(&params).unwrap();
match pubsub
.inner()
.prepare("eth_subscribe", [kind, params])
.subscribe::<[serde_json::Value; 2], SubscriptionResult>([kind, params])
.await
{
Ok(id) => {
let rx = pubsub.inner().get_raw_subscription(id).await;
Ok(sub) => {
let rx = sub.into_raw();
let successful_provider = aps.urls.remove(index);
aps.urls.insert(0, successful_provider);
return Ok(Ok(rx));
Expand Down
1 change: 0 additions & 1 deletion kinode/src/fakenet/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use alloy_sol_macro::sol;
use sha3::{Digest, Keccak256};

sol! {
#[sol(rpc)]
contract RegisterHelpers {
function register(
bytes calldata _name,
Expand Down
87 changes: 42 additions & 45 deletions kinode/src/fakenet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use alloy_consensus::TxLegacy;
use alloy_network::{Transaction, TxKind};
use alloy::network::{eip2718::Encodable2718, EthereumSigner, TransactionBuilder};
use alloy::providers::{Provider, ProviderBuilder, RootProvider};
use alloy::pubsub::PubSubFrontend;
use alloy::rpc::client::WsConnect;
use alloy::rpc::types::eth::{TransactionInput, TransactionRequest};
use alloy::signers::wallet::LocalWallet;
use alloy_primitives::{Address, Bytes, FixedBytes, B256, U256};
use alloy_providers::provider::{Provider, TempProvider};
use alloy_rpc_client::ClientBuilder;
use alloy_rpc_types::request::{TransactionInput, TransactionRequest};
use alloy_signer::{LocalWallet, Signer, SignerSync};
use alloy_sol_types::{SolCall, SolValue};
use alloy_transport_ws::WsConnect;
use lib::core::{Identity, NodeRouting};
use std::str::FromStr;

Expand All @@ -31,17 +30,17 @@ pub async fn register_local(
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
)?;

let wallet_address = wallet.address();

let signer: EthereumSigner = wallet.into();

let dotdev = Address::from_str(FAKE_DOTDEV)?;
let kns = Address::from_str(KNS_ADDRESS)?;

let endpoint = format!("ws://localhost:{}", fakechain_port);
let ws = WsConnect {
url: endpoint,
auth: None,
};
let ws = WsConnect::new(endpoint);

let client = ClientBuilder::default().ws(ws).await?;
let provider = Provider::new_with_client(client);
let provider: RootProvider<PubSubFrontend> = ProviderBuilder::default().on_ws(ws).await?;

let fqdn = dns_encode_fqdn(name);
let namehash = encode_namehash(name);
Expand Down Expand Up @@ -73,10 +72,10 @@ pub async fn register_local(
.abi_encode();

let exists_tx = TransactionRequest::default()
.to(Some(dotdev))
.to(dotdev)
.input(TransactionInput::new(exists_call.into()));

let exists = provider.call(exists_tx, None).await;
let exists = provider.call(&exists_tx).await;

let (call_input, to) = match exists {
Err(_e) => {
Expand Down Expand Up @@ -106,33 +105,40 @@ pub async fn register_local(
};

let multicall = multicallCall {
data: vec![set_ip.abi_encode(), set_key.abi_encode()],
data: vec![
Bytes::from(set_ip.abi_encode()),
Bytes::from(set_key.abi_encode()),
],
}
.abi_encode();

(multicall, kns)
}
};
let nonce = provider
.get_transaction_count(wallet.address(), None)
.await?;

let mut tx = TxLegacy {
to: TxKind::Call(to),
nonce: nonce.to::<u64>(),
input: call_input.into(),
chain_id: Some(31337),
gas_limit: 3000000,
gas_price: 100000000000,
let nonce = provider.get_transaction_count(wallet_address).await?;

let input = TransactionInput {
input: Some(call_input.into()),
..Default::default()
};

let sig = wallet.sign_transaction_sync(&mut tx)?;
let signed_tx = tx.into_signed(sig);
let mut buf = vec![];
signed_tx.encode_signed(&mut buf);
let tx = TransactionRequest::default()
.to(to)
.input(input)
.nonce(nonce)
.with_chain_id(31337)
.with_gas_limit(500_000)
.with_max_priority_fee_per_gas(1_000_000_000)
.with_max_fee_per_gas(20_000_000_000);

// Build the transaction using the `EthereumSigner` with the provided signer.
let tx_envelope = tx.build(&signer).await?;

let _tx_hash = provider.send_raw_transaction(buf.into()).await?;
// Encode the transaction using EIP-2718 encoding.
let tx_encoded = tx_envelope.encoded_2718();

// Send the raw transaction and retrieve the transaction receipt.
let _tx_hash = provider.send_raw_transaction(&tx_encoded).await?;

Ok(())
}
Expand All @@ -146,25 +152,16 @@ pub async fn assign_ws_local_helper(
) -> Result<(), anyhow::Error> {
let kns = Address::from_str(KNS_ADDRESS)?;
let endpoint = format!("ws://localhost:{}", fakechain_port);
let ws = WsConnect::new(endpoint);

let ws = WsConnect {
url: endpoint,
auth: None,
};

let client = ClientBuilder::default().ws(ws).await?;
let provider = Provider::new_with_client(client);
let provider: RootProvider<PubSubFrontend> = ProviderBuilder::default().on_ws(ws).await?;

let namehash = FixedBytes::<32>::from_slice(&keygen::namehash(&our.name));
let ip_call = ipCall { _0: namehash }.abi_encode();
let tx_input = TransactionInput::new(Bytes::from(ip_call));
let tx = TransactionRequest {
to: Some(kns),
input: tx_input,
..Default::default()
};
let tx = TransactionRequest::default().to(kns).input(tx_input);

let Ok(ip_data) = provider.call(tx, None).await else {
let Ok(ip_data) = provider.call(&tx).await else {
return Err(anyhow::anyhow!("Failed to fetch node IP data from PKI"));
};

Expand Down
Loading