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

Store state in TinyCash rather than rely on Zebra State Service #20

Merged
merged 11 commits into from
Apr 10, 2024
509 changes: 34 additions & 475 deletions Cargo.lock

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,42 @@ ethereum-types = "0.14.1"
futures-util = "0.3.30"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
ethabi = "18.0.0"
ciborium = "0.2.2"
base64-url = "2.0.2"
alloy-json-abi = "0.6.4"
tonic = "0.11"
tonic = { version = "0.11", optional = true}

zebra-chain = { workspace = true }
zebra-state = { workspace = true }
zebra-consensus = { workspace = true }

tiny-cash = { path = "tiny-cash" }
cartezcash-lightwalletd = { path = "cartezcash-lightwalletd" }
cartezcash-lightwalletd = { path = "cartezcash-lightwalletd", optional = true}
tower-cartesi = { path = "tower-cartesi" }
base58check = "0.1.0"
zcash_address = "0.3.2"
zcash_keys = { version = "0.2.0", features = ["orchard"] }
zcash_primitives = "0.15.0"

[features]
default = []
lightwalletd = ["dep:cartezcash-lightwalletd", "dep:tonic"]

[workspace]
members = [ "cartezcash-lightwalletd", "tiny-cash", "tower-cartesi"]

[workspace.dependencies]

# patched zebra crates to support TinyCash network and expose things we need
zebra-consensus = { git = "https://github.com/willemolding/zebra", rev = "185e404bed938e3aa916aee1ae6999a70efceddb", default-features = false }
zebra-state = { git = "https://github.com/willemolding/zebra", rev = "185e404bed938e3aa916aee1ae6999a70efceddb", default-features = false }
zebra-test = { git = "https://github.com/willemolding/zebra", rev = "185e404bed938e3aa916aee1ae6999a70efceddb", default-features = false }
zebra-chain = { git = "https://github.com/willemolding/zebra", rev = "185e404bed938e3aa916aee1ae6999a70efceddb", default-features = false }
zebra-consensus = { git = "https://github.com/willemolding/zebra", rev = "04615946f3bae4e39925edae8ed1e150b7cf531a", default-features = false }
zebra-state = { git = "https://github.com/willemolding/zebra", rev = "04615946f3bae4e39925edae8ed1e150b7cf531a", default-features = false }
zebra-test = { git = "https://github.com/willemolding/zebra", rev = "04615946f3bae4e39925edae8ed1e150b7cf531a", default-features = false }
zebra-chain = { git = "https://github.com/willemolding/zebra", rev = "04615946f3bae4e39925edae8ed1e150b7cf531a", default-features = false }
zebra-script = { git = "https://github.com/willemolding/zebra", rev = "04615946f3bae4e39925edae8ed1e150b7cf531a", default-features = false }

# uncomment for local dev when modifying zebra
# zebra-consensus = { path = "../zebra/zebra-consensus", default-features = false }
# zebra-state = { path = "../zebra/zebra-state", default-features = false }
# zebra-test = { path = "../zebra/zebra-test", default-features = false }
# zebra-chain = { path = "../zebra/zebra-chain", default-features = false }
# zebra-script = { path = "../zebra/zebra-script", default-features = false }


[patch.crates-io]
Expand Down
52 changes: 26 additions & 26 deletions cartezcash-lightwalletd/src/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ where
&self,
request: tonic::Request<RawTransaction>,
) -> std::result::Result<tonic::Response<SendResponse>, tonic::Status> {
tracing::info!("send_transaction called. Fowarding to InputBox contract");
tracing::debug!("send_transaction called. Fowarding to InputBox contract");

let provider = Provider::<Http>::try_from("http://127.0.0.1:8545").unwrap();
let wallet: LocalWallet =
Expand Down Expand Up @@ -91,7 +91,7 @@ where
&self,
_request: tonic::Request<ChainSpec>,
) -> std::result::Result<tonic::Response<BlockId>, tonic::Status> {
tracing::info!("get_latest_block called");
tracing::debug!("get_latest_block called");

let res: zebra_state::ReadResponse = self
.state_read_service
Expand All @@ -104,13 +104,13 @@ where
.unwrap();

if let ReadResponse::Tip(Some((height, hash))) = res {
tracing::info!("returning tip: {:?}", res);
tracing::debug!("returning tip: {:?}", res);
Ok(tonic::Response::new(BlockId {
hash: hash.0.to_vec(),
height: height.0 as u64,
}))
} else {
tracing::info!("unexpected response");
tracing::debug!("unexpected response");
Err(tonic::Status::not_found(
"Could not find the latest block in the state store",
))
Expand All @@ -122,7 +122,7 @@ where
&self,
request: tonic::Request<BlockRange>,
) -> std::result::Result<tonic::Response<Self::GetBlockRangeStream>, tonic::Status> {
tracing::info!("get_block_range called with: {:?} ", request);
tracing::debug!("get_block_range called with: {:?} ", request);
let (tx, rx) = mpsc::channel(10);

// these sometimes come in reverse order...
Expand All @@ -141,7 +141,7 @@ where
let mut state_read_service = self.state_read_service.clone();

for height in range {
tracing::info!("fetching block at height: {}", height);
tracing::debug!("fetching block at height: {}", height);
let res: zebra_state::ReadResponse = state_read_service
.ready()
.await
Expand All @@ -155,7 +155,7 @@ where
let block = match res {
ReadResponse::Block(Some(block)) => block,
_ => {
tracing::info!("unexpected response");
tracing::debug!("unexpected response");
return Err(tonic::Status::not_found(
"Could not find the block in the state store",
));
Expand Down Expand Up @@ -220,7 +220,7 @@ where
&self,
request: tonic::Request<TxFilter>,
) -> std::result::Result<tonic::Response<RawTransaction>, tonic::Status> {
tracing::info!("get_transaction called");
tracing::debug!("get_transaction called");
let mut state_read_service = self.state_read_service.clone();

let request = zebra_state::ReadRequest::Transaction(
Expand All @@ -239,7 +239,7 @@ where
height: transaction.height.0 as u64,
}))
} else {
tracing::info!("unexpected response");
tracing::debug!("unexpected response");
Err(tonic::Status::not_found(
"Could not find the transaction in the state store",
))
Expand All @@ -251,7 +251,7 @@ where
&self,
request: tonic::Request<TransparentAddressBlockFilter>,
) -> std::result::Result<tonic::Response<Self::GetTaddressTxidsStream>, tonic::Status> {
tracing::info!("get_taddress_txids called with {:?}", request);
tracing::debug!("get_taddress_txids called with {:?}", request);

let request = request.into_inner();
let address = transparent::Address::from_str(&request.address).unwrap();
Expand All @@ -277,7 +277,7 @@ where

if let ReadResponse::AddressesTransactionIds(txns) = res {
let (tx, rx) = mpsc::channel(10);
tracing::info!("{:?} transactions found", txns.len());
tracing::debug!("{:?} transactions found", txns.len());
for (_location, tx_id) in txns.iter() {
tracing::debug!("got txid: {:?}", tx_id);

Expand Down Expand Up @@ -317,7 +317,7 @@ where
&self,
request: tonic::Request<BlockId>,
) -> std::result::Result<tonic::Response<TreeState>, tonic::Status> {
tracing::info!("get_tree_state called");
tracing::debug!("get_tree_state called");

let mut read_service = self.state_read_service.clone();
let height: Height = Height(request.into_inner().height.try_into().unwrap());
Expand Down Expand Up @@ -370,7 +370,7 @@ where
hash: hash.to_string(),
time: block.header.time.timestamp() as u32,
};
tracing::info!("returning tree state: {:?}", tree_state);
tracing::debug!("returning tree state: {:?}", tree_state);
Ok(tonic::Response::new(tree_state))
}

Expand All @@ -379,7 +379,7 @@ where
&self,
_request: tonic::Request<Empty>,
) -> std::result::Result<tonic::Response<LightdInfo>, tonic::Status> {
tracing::info!("get_lightd_info called");
tracing::debug!("get_lightd_info called");

let block_height = 0; // TODO: fetch this from the store

Expand Down Expand Up @@ -415,7 +415,7 @@ where
&self,
_request: tonic::Request<Empty>,
) -> std::result::Result<tonic::Response<Self::GetMempoolStreamStream>, tonic::Status> {
tracing::info!("get_mempool_stream called");
tracing::debug!("get_mempool_stream called");
// let (tx, rx) = mpsc::channel(4);
// TODO: Send the txiods into the tx end of the channel
Err(tonic::Status::unimplemented(
Expand All @@ -432,7 +432,7 @@ where
tonic::Response<crate::proto::compact_formats::CompactBlock>,
tonic::Status,
> {
tracing::info!("get_block called. Ignoring request");
tracing::debug!("get_block called. Ignoring request");
Err(tonic::Status::unimplemented(
"gRPC endpoint not supported for cartezcash",
))
Expand All @@ -446,7 +446,7 @@ where
tonic::Response<crate::proto::compact_formats::CompactBlock>,
tonic::Status,
> {
tracing::info!("get_block_nullifiers called. Ignoring request");
tracing::debug!("get_block_nullifiers called. Ignoring request");
Err(tonic::Status::unimplemented(
"gRPC endpoint not supported for cartezcash",
))
Expand All @@ -461,7 +461,7 @@ where
_request: tonic::Request<BlockRange>,
) -> std::result::Result<tonic::Response<Self::GetBlockRangeNullifiersStream>, tonic::Status>
{
tracing::info!("get_block_range_nullifiers called. Ignoring request");
tracing::debug!("get_block_range_nullifiers called. Ignoring request");
Err(tonic::Status::unimplemented(
"gRPC endpoint not supported for cartezcash",
))
Expand All @@ -471,7 +471,7 @@ where
&self,
_request: tonic::Request<AddressList>,
) -> std::result::Result<tonic::Response<Balance>, tonic::Status> {
tracing::info!("get_taddress_balance called. Ignoring request");
tracing::debug!("get_taddress_balance called. Ignoring request");
Err(tonic::Status::unimplemented(
"gRPC endpoint not supported for cartezcash",
))
Expand All @@ -481,7 +481,7 @@ where
&self,
_request: tonic::Request<tonic::Streaming<crate::proto::service::Address>>,
) -> std::result::Result<tonic::Response<Balance>, tonic::Status> {
tracing::info!("get_taddress_balance_stream called. Ignoring request");
tracing::debug!("get_taddress_balance_stream called. Ignoring request");
Err(tonic::Status::unimplemented(
"gRPC endpoint not supported for cartezcash",
))
Expand All @@ -503,7 +503,7 @@ where
&self,
_request: tonic::Request<Exclude>,
) -> std::result::Result<tonic::Response<Self::GetMempoolTxStream>, tonic::Status> {
tracing::info!("get_mempool_tx called. Ignoring request");
tracing::debug!("get_mempool_tx called. Ignoring request");
Err(tonic::Status::unimplemented(
"gRPC endpoint not supported for cartezcash",
))
Expand All @@ -513,7 +513,7 @@ where
&self,
_request: tonic::Request<Empty>,
) -> std::result::Result<tonic::Response<TreeState>, tonic::Status> {
tracing::info!("get_latest_tree_state called. Ignoring request");
tracing::debug!("get_latest_tree_state called. Ignoring request");
Err(tonic::Status::unimplemented(
"gRPC endpoint not supported for cartezcash",
))
Expand All @@ -527,7 +527,7 @@ where
&self,
_request: tonic::Request<GetSubtreeRootsArg>,
) -> std::result::Result<tonic::Response<Self::GetSubtreeRootsStream>, tonic::Status> {
tracing::info!("get_subtree_roots called. Ignoring request");
tracing::debug!("get_subtree_roots called. Ignoring request");
Err(tonic::Status::unimplemented(
"gRPC endpoint not supported for cartezcash",
))
Expand All @@ -536,7 +536,7 @@ where
&self,
_request: tonic::Request<GetAddressUtxosArg>,
) -> std::result::Result<tonic::Response<GetAddressUtxosReplyList>, tonic::Status> {
tracing::info!("get_address_utxos called. Ignoring request");
tracing::debug!("get_address_utxos called. Ignoring request");
Err(tonic::Status::unimplemented(
"gRPC endpoint not supported for cartezcash",
))
Expand All @@ -549,7 +549,7 @@ where
_request: tonic::Request<GetAddressUtxosArg>,
) -> std::result::Result<tonic::Response<Self::GetAddressUtxosStreamStream>, tonic::Status>
{
tracing::info!("get_address_utxos_stream called. Ignoring request");
tracing::debug!("get_address_utxos_stream called. Ignoring request");
Err(tonic::Status::unimplemented(
"gRPC endpoint not supported for cartezcash",
))
Expand All @@ -560,7 +560,7 @@ where
&self,
_request: tonic::Request<Duration>,
) -> std::result::Result<tonic::Response<PingResponse>, tonic::Status> {
tracing::info!("ping called. Ignoring request");
tracing::debug!("ping called. Ignoring request");
Err(tonic::Status::unimplemented(
"gRPC endpoint not supported for cartezcash",
))
Expand Down
5 changes: 1 addition & 4 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ run:
sunodo run --epoch-duration=10

run-local:
ROLLUP_HTTP_SERVER_URL=http://127.0.0.1:8080/host-runner GRPC_SERVER_URL="[::1]:50051" cargo run

run-proxy:
CARTESI_NODE_URL="0.0.0.0:8080" cargo run -p cartezcash-proxy
ROLLUP_HTTP_SERVER_URL=http://127.0.0.1:8080/host-runner GRPC_SERVER_URL="[::1]:50051" cargo run --features lightwalletd

sunodo-nobackend:
sunodo run --no-backend
Expand Down
53 changes: 25 additions & 28 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
use cartezcash_lightwalletd::{
proto::service::compact_tx_streamer_server::CompactTxStreamerServer,
service_impl::CompactTxStreamerImpl,
};
use service::{CarteZcashService, Request};
use zcash_keys::address::UnifiedAddress;
use zcash_primitives::consensus::MAIN_NETWORK;
Expand All @@ -17,13 +13,18 @@
use futures_util::future::FutureExt;

use zebra_chain::{block, parameters::Network};
use zebra_consensus::transaction as tx;

#[cfg(feature = "lightwalletd")]
use cartezcash_lightwalletd::{
proto::service::compact_tx_streamer_server::CompactTxStreamerServer,
service_impl::CompactTxStreamerImpl,
};

type StateService = Buffer<
BoxService<zebra_state::Request, zebra_state::Response, zebra_state::BoxError>,
zebra_state::Request,
>;
type StateReadService = Buffer<

Check warning on line 27 in src/main.rs

View workflow job for this annotation

GitHub Actions / check

type alias `StateReadService` is never used
BoxService<zebra_state::ReadRequest, zebra_state::ReadResponse, zebra_state::BoxError>,
zebra_state::ReadRequest,
>;
Expand All @@ -39,7 +40,6 @@
tracing::subscriber::set_global_default(subscriber)?;

let server_addr = env::var("ROLLUP_HTTP_SERVER_URL")?;
let grpc_addr = env::var("GRPC_SERVER_URL")?;

let network = Network::Mainnet;

Expand All @@ -55,25 +55,29 @@
// tiny_cash::initialize_halo2();
// tracing::info!("Initializing Halo2 verifier key complete");

let (state_service, state_read_service, _, _) = zebra_state::init(

Check warning on line 58 in src/main.rs

View workflow job for this annotation

GitHub Actions / check

unused variable: `state_read_service`
zebra_state::Config::ephemeral(),
network,
block::Height::MAX,
0,
);
let state_service = Buffer::new(state_service, 30);
let state_read_service = Buffer::new(state_read_service.boxed(), 30);

let mut cartezcash_app = CarteZcashApp::new(network, state_service).await;

let svc = CompactTxStreamerServer::new(CompactTxStreamerImpl { state_read_service });
let addr = grpc_addr.parse()?;
let grpc_server = tonic::transport::Server::builder()
.trace_fn(|_| tracing::info_span!("cartezcash-grpc"))
.add_service(svc)
.serve(addr);
tokio::spawn(grpc_server);
tracing::info!("wallet GRPC server listening on {}", addr);
let mut cartezcash_app = CarteZcashApp::new(state_service).await;

#[cfg(feature = "lightwalletd")]
{
let grpc_addr = env::var("GRPC_SERVER_URL")?;
let state_read_service = Buffer::new(state_read_service.boxed(), 30);
let svc = CompactTxStreamerServer::new(CompactTxStreamerImpl { state_read_service });
let addr = grpc_addr.parse()?;
let grpc_server = tonic::transport::Server::builder()
.trace_fn(|_| tracing::info_span!("cartezcash-grpc"))
.add_service(svc)
.serve(addr);
tokio::spawn(grpc_server);
tracing::info!("wallet GRPC server listening on {}", addr);
}

listen_http(&mut cartezcash_app, &server_addr)
.await
Expand All @@ -88,14 +92,10 @@
}

impl CarteZcashApp {
pub async fn new(network: Network, state_service: StateService) -> Self {
pub async fn new(state_service: StateService) -> Self {
// set up the services needed to run the rollup
let verifier_service = tx::Verifier::new(network, state_service.clone());
let mut tinycash = Buffer::new(
BoxService::new(tiny_cash::write::TinyCashWriteService::new(
state_service,
verifier_service,
)),
BoxService::new(tiny_cash::write::TinyCashWriteService::new(state_service)),
10,
);

Expand Down Expand Up @@ -147,11 +147,8 @@

async fn initialize_network<S>(tinycash: &mut S) -> Result<(), BoxError>
where
S: Service<
tiny_cash::write::Request,
Response = tiny_cash::write::Response,
Error = tiny_cash::write::BoxError,
> + Send
S: Service<tiny_cash::write::Request, Response = tiny_cash::write::Response, Error = BoxError>
+ Send
+ Clone
+ 'static,
S::Future: Send + 'static,
Expand Down
Loading
Loading