Skip to content

Commit

Permalink
tidy up tracing outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
willemolding committed Apr 10, 2024
1 parent 717ca08 commit fbbaa52
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 40 deletions.
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
8 changes: 2 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use futures_util::future::FutureExt;

use zebra_chain::{block, parameters::Network};


#[cfg(feature = "lightwalletd")]
use cartezcash_lightwalletd::{
proto::service::compact_tx_streamer_server::CompactTxStreamerServer,
Expand Down Expand Up @@ -148,11 +147,8 @@ impl Service<RollAppRequest> for CarteZcashApp {

async fn initialize_network<S>(tinycash: &mut S) -> Result<(), BoxError>
where
S: Service<
tiny_cash::write::Request,
Response = tiny_cash::write::Response,
Error = BoxError,
> + Send
S: Service<tiny_cash::write::Request, Response = tiny_cash::write::Response, Error = BoxError>
+ Send
+ Clone
+ 'static,
S::Future: Send + 'static,
Expand Down
37 changes: 29 additions & 8 deletions tiny-cash/src/write.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use chrono::{DateTime, Utc};
use futures_util::future::FutureExt;
use zebra_state::DuplicateNullifierError;
use std::{
collections::{HashMap, HashSet, VecDeque},
future::Future,
Expand All @@ -10,6 +9,8 @@ use std::{
};
use tower::{BoxError, Service, ServiceExt};

use incrementalmerkletree::frontier::Frontier;
use zebra_chain::orchard::tree::NoteCommitmentTree;
use zebra_chain::transparent;
use zebra_chain::{
amount::{Amount, NonNegative},
Expand All @@ -28,7 +29,6 @@ use zebra_chain::{
use zebra_consensus::script;
use zebra_consensus::transaction as tx;
use zebra_consensus::transaction::Verifier as TxVerifier;
use incrementalmerkletree::frontier::Frontier;

use crate::extract_burn_info;

Expand All @@ -48,14 +48,15 @@ pub struct TinyCashWriteService<S> {
utxos_set: HashMap<OutPoint, OrderedUtxo>,

// The frontier of the commitment tree. This is the Merkle path of the last added commitment
// The frontier is all that is needed to update the root and produce a new frontier when new commitments are added
// Internally this just stores the frontier which is the Merkle path of the most recently added note
// This is all that is needed to update the root and produce a new frontier when new commitments are added
// So for a fixed depth this is constant size!
// commitment_tree_frontier: OrchardFrontier,
commitment_tree_frontier: NoteCommitmentTree,

// A FILO queue of orchard commitment tree roots.
// Only a fixed window are kept so we can have constant state.
// This means older witnesses become invalid after a period of time
// commitment_tree_roots: VecDeque<zebra_chain::orchard::tree::Root>,
historical_tree_roots: VecDeque<zebra_chain::orchard::tree::Root>,

// A set of all nullifiers that have been seen. This prevents double spends.
// sadly this is not fixed size but it could be replaced with a Sparse Merkle Tree
Expand All @@ -70,6 +71,8 @@ impl<S> TinyCashWriteService<S> {

tip_height: None,
tip_hash: None,
commitment_tree_frontier: NoteCommitmentTree::default(),
historical_tree_roots: VecDeque::new(),
utxos_set: HashMap::new(),
nullifier_set: HashSet::new(),
}
Expand Down Expand Up @@ -171,14 +174,32 @@ where
self.utxos_set
.extend(new_outputs.iter().map(|(k, v)| (k.clone(), v.clone())));


// check this block doesn't reuse a known nullifier then add the block nullifiers to the state
for nullifier in block.clone().orchard_nullifiers() {
if self.nullifier_set.contains(nullifier) {
panic!("duplicate nullifier detected");
}
}
self.nullifier_set.extend(block.orchard_nullifiers().cloned());
self.nullifier_set
.extend(block.orchard_nullifiers().cloned());

// update the commitment tree frontier and add the new root to the queue
for commitment in block.orchard_note_commitments() {
self.commitment_tree_frontier
.append(*commitment)
.expect("failed to append to tree");
}
self.historical_tree_roots
.push_back(self.commitment_tree_frontier.root());

// ensure the anchors/tree-roots referenced by the transaction are in the state
if let Some(tx) = tx_to_verify.as_ref() {
if let Some(data) = tx.orchard_shielded_data() {
if !self.historical_tree_roots.contains(&data.shared_anchor) {
panic!("Tree root not found in state. Witness may be invalid or too old");
}
}
}

async move {
if height > Height(0) {
Expand Down Expand Up @@ -299,7 +320,7 @@ where
&orchard_shielded_data,
&shielded_sighash,
)?)
},
}
_ => panic!("Only V5 transactions are supported"),
};
async_checks.check().await
Expand Down

0 comments on commit fbbaa52

Please sign in to comment.