Skip to content

Commit

Permalink
Remove proto uint64 for u64
Browse files Browse the repository at this point in the history
  • Loading branch information
brianp committed Oct 9, 2023
1 parent 05fc2e9 commit c8607cc
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ async fn get_tip_height(wallet: &WalletSqlite) -> Option<u64> {
.await
.ok()
.and_then(|t| t.metadata)
.and_then(|m| m.height_of_longest_chain),
.map(|m| m.height_of_longest_chain),
None => None,
}
}
10 changes: 5 additions & 5 deletions base_layer/core/src/base_node/chain_metadata_service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,14 @@ mod test {
fn create_sample_proto_chain_metadata() -> proto::ChainMetadata {
let diff: u128 = 1;
proto::ChainMetadata {
height_of_longest_chain: Some(1),
height_of_longest_chain: 1,
best_block: Some(vec![
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
28, 29, 30, 31,
]),
pruned_height: 0,
accumulated_difficulty: diff.to_be_bytes().to_vec(),
timestamp: Some(0),
timestamp: 0,
}
}

Expand All @@ -278,7 +278,7 @@ mod test {
let (mut service, liveness_mock_state, mut base_node_receiver, _) = setup();

let mut proto_chain_metadata = create_sample_proto_chain_metadata();
proto_chain_metadata.height_of_longest_chain = Some(123);
proto_chain_metadata.height_of_longest_chain = 123;
let chain_metadata = proto_chain_metadata.clone().try_into().unwrap();

task::spawn(async move {
Expand All @@ -297,7 +297,7 @@ mod test {
unpack_enum!(LivenessRequest::SetMetadataEntry(metadata_key, data) = last_call);
assert_eq!(metadata_key, MetadataKey::ChainMetadata);
let chain_metadata = proto::ChainMetadata::decode(data.as_slice()).unwrap();
assert_eq!(chain_metadata.height_of_longest_chain, Some(123));
assert_eq!(chain_metadata.height_of_longest_chain, 123);
}
#[tokio::test]
async fn handle_liveness_event_ok() {
Expand All @@ -320,7 +320,7 @@ mod test {
assert_eq!(*metadata.node_id(), node_id);
assert_eq!(
metadata.claimed_chain_metadata().height_of_longest_chain(),
proto_chain_metadata.height_of_longest_chain.unwrap()
proto_chain_metadata.height_of_longest_chain
);
}

Expand Down
4 changes: 2 additions & 2 deletions base_layer/core/src/base_node/proto/chain_metadata.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package tari.base_node;

message ChainMetadata {
// The current chain height, or the block number of the longest valid chain, or `None` if there is no chain
google.protobuf.UInt64Value height_of_longest_chain = 1;
uint64 height_of_longest_chain = 1;
// The block hash of the current tip of the longest valid chain, or `None` for an empty chain
google.protobuf.BytesValue best_block = 2;
// The current geometric mean of the pow of the chain tip, or `None` if there is no chain
Expand All @@ -20,5 +20,5 @@ message ChainMetadata {
// Archival nodes wil always have an `pruned_height` of zero.
uint64 pruned_height = 6;
// Timestamp of the last block in the chain, or `None` if there is no chain
google.protobuf.UInt64Value timestamp = 7;
uint64 timestamp = 7;
}
16 changes: 9 additions & 7 deletions base_layer/core/src/base_node/proto/chain_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ impl TryFrom<proto::ChainMetadata> for ChainMetadata {
let mut acc_diff = [0; ACC_DIFFICULTY_ARRAY_LEN];
acc_diff.copy_from_slice(&metadata.accumulated_difficulty[0..ACC_DIFFICULTY_ARRAY_LEN]);
let accumulated_difficulty = u128::from_be_bytes(acc_diff);
let height_of_longest_chain = metadata
.height_of_longest_chain
.ok_or_else(|| "Height of longest chain is missing".to_string())?;
let height_of_longest_chain = metadata.height_of_longest_chain;
if height_of_longest_chain == 0 {
return Err("Height of longest chain is missing".to_string());
}

let pruning_horizon = if metadata.pruned_height == 0 {
metadata.pruned_height
} else {
Expand All @@ -64,7 +66,7 @@ impl TryFrom<proto::ChainMetadata> for ChainMetadata {
pruning_horizon,
metadata.pruned_height,
accumulated_difficulty,
metadata.timestamp.unwrap_or_default(),
metadata.timestamp,
))
}
}
Expand All @@ -73,17 +75,17 @@ impl From<ChainMetadata> for proto::ChainMetadata {
fn from(metadata: ChainMetadata) -> Self {
let accumulated_difficulty = metadata.accumulated_difficulty().to_be_bytes().to_vec();
Self {
height_of_longest_chain: Some(metadata.height_of_longest_chain()),
height_of_longest_chain: metadata.height_of_longest_chain(),
best_block: Some(metadata.best_block().to_vec()),
pruned_height: metadata.pruned_height(),
accumulated_difficulty,
timestamp: Some(metadata.timestamp()),
timestamp: metadata.timestamp(),
}
}
}

impl proto::ChainMetadata {
pub fn height_of_longest_chain(&self) -> u64 {
self.height_of_longest_chain.unwrap_or(0)
self.height_of_longest_chain
}
}
6 changes: 3 additions & 3 deletions base_layer/core/src/base_node/proto/wallet_rpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ message TxQueryResponse {
uint64 confirmations = 3;
bool is_synced = 4;
uint64 height_of_longest_chain = 5;
google.protobuf.UInt64Value mined_timestamp = 6;
uint64 mined_timestamp = 6;
}

message TxQueryBatchResponse {
Expand All @@ -48,15 +48,15 @@ message TxQueryBatchResponse {
google.protobuf.BytesValue block_hash = 3;
uint64 confirmations = 4;
uint64 block_height = 5;
google.protobuf.UInt64Value mined_timestamp = 6;
uint64 mined_timestamp = 6;
}

message TxQueryBatchResponses {
repeated TxQueryBatchResponse responses = 1;
bool is_synced = 2;
google.protobuf.BytesValue tip_hash = 3;
uint64 height_of_longest_chain = 4;
google.protobuf.UInt64Value tip_mined_timestamp = 5;
uint64 tip_mined_timestamp = 5;
}

message FetchMatchingUtxos {
Expand Down
16 changes: 13 additions & 3 deletions base_layer/core/src/base_node/proto/wallet_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ impl TryFrom<proto::TxQueryResponse> for TxQueryResponse {
},
None => None,
};

let mined_timestamp = match proto_response.mined_timestamp {
0 => None,
t => Some(t),
};

Ok(Self {
location: TxLocation::try_from(
proto::TxLocation::from_i32(proto_response.location)
Expand All @@ -207,7 +213,7 @@ impl TryFrom<proto::TxQueryResponse> for TxQueryResponse {
confirmations: proto_response.confirmations,
is_synced: proto_response.is_synced,
height_of_longest_chain: proto_response.height_of_longest_chain,
mined_timestamp: proto_response.mined_timestamp,
mined_timestamp,
})
}
}
Expand All @@ -220,7 +226,7 @@ impl From<TxQueryResponse> for proto::TxQueryResponse {
confirmations: response.confirmations,
is_synced: response.is_synced,
height_of_longest_chain: response.height_of_longest_chain,
mined_timestamp: response.mined_timestamp,
mined_timestamp: response.mined_timestamp.unwrap_or_default(),
}
}
}
Expand All @@ -236,6 +242,10 @@ impl TryFrom<proto::TxQueryBatchResponse> for TxQueryBatchResponse {
},
None => None,
};
let mined_timestamp = match proto_response.mined_timestamp {
0 => None,
t => Some(t),
};
Ok(Self {
signature: Signature::try_from(
proto_response
Expand All @@ -249,7 +259,7 @@ impl TryFrom<proto::TxQueryBatchResponse> for TxQueryBatchResponse {
block_hash: hash,
block_height: proto_response.block_height,
confirmations: proto_response.confirmations,
mined_timestamp: proto_response.mined_timestamp,
mined_timestamp,
})
}
}
Expand Down
8 changes: 4 additions & 4 deletions base_layer/core/src/base_node/rpc/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl<B: BlockchainBackend + 'static> BaseNodeWalletRpcService<B> {
confirmations,
is_synced,
height_of_longest_chain: chain_metadata.height_of_longest_chain(),
mined_timestamp: Some(header.timestamp.as_u64()),
mined_timestamp: header.timestamp.as_u64(),
};
return Ok(response);
},
Expand All @@ -150,7 +150,7 @@ impl<B: BlockchainBackend + 'static> BaseNodeWalletRpcService<B> {
confirmations: 0,
is_synced,
height_of_longest_chain: chain_metadata.height_of_longest_chain(),
mined_timestamp: None,
mined_timestamp: 0,
},
TxStorageResponse::ReorgPool |
TxStorageResponse::NotStoredOrphan |
Expand All @@ -165,7 +165,7 @@ impl<B: BlockchainBackend + 'static> BaseNodeWalletRpcService<B> {
confirmations: 0,
is_synced,
height_of_longest_chain: chain_metadata.height_of_longest_chain(),
mined_timestamp: None,
mined_timestamp: 0,
},
};
Ok(mempool_response)
Expand Down Expand Up @@ -320,7 +320,7 @@ impl<B: BlockchainBackend + 'static> BaseNodeWalletService for BaseNodeWalletRpc
is_synced,
tip_hash: Some(metadata.best_block().to_vec()),
height_of_longest_chain: metadata.height_of_longest_chain(),
tip_mined_timestamp: Some(metadata.timestamp()),
tip_mined_timestamp: metadata.timestamp(),
}))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,15 +345,14 @@ where
.tip_hash
.ok_or_else(|| TransactionServiceError::ProtobufConversionError("Missing `tip_hash` field".to_string()))?
.try_into()?;

Ok((
mined,
unmined,
Some((
batch_response.height_of_longest_chain,
tip,
batch_response.tip_mined_timestamp.ok_or_else(|| {
TransactionServiceError::ProtobufConversionError("Missing `tip_hash` field".to_string())
})?,
batch_response.tip_mined_timestamp,
)),
))
}
Expand Down
10 changes: 5 additions & 5 deletions base_layer/wallet/tests/support/comms_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,15 @@ impl BaseNodeWalletRpcMockState {
tip_hash: Some(vec![]),
is_synced: true,
height_of_longest_chain: 0,
tip_mined_timestamp: Some(0),
tip_mined_timestamp: 0,
})),
tip_info_response: Arc::new(Mutex::new(TipInfoResponse {
metadata: Some(ChainMetadataProto {
height_of_longest_chain: Some(std::i64::MAX as u64),
height_of_longest_chain: std::i64::MAX as u64,
best_block: Some(Vec::new()),
accumulated_difficulty: Vec::new(),
pruned_height: 0,
timestamp: Some(0),
timestamp: 0,
}),
is_synced: true,
})),
Expand Down Expand Up @@ -931,11 +931,11 @@ mod test {
assert_eq!(calls.len(), 1);

let chain_metadata = ChainMetadata {
height_of_longest_chain: Some(444),
height_of_longest_chain: 444,
best_block: Some(Vec::new()),
accumulated_difficulty: Vec::new(),
pruned_height: 0,
timestamp: Some(0),
timestamp: 0,
};
service_state.set_tip_info_response(TipInfoResponse {
metadata: Some(chain_metadata),
Expand Down
Loading

0 comments on commit c8607cc

Please sign in to comment.