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

fix: u64->i64->u64 conversion; chain split height as u64 #3442

Merged
merged 8 commits into from
Oct 11, 2021
33 changes: 24 additions & 9 deletions applications/tari_base_node/src/grpc/base_node_grpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,19 +324,34 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
let num_headers = cmp::min(num_headers, LIST_HEADERS_MAX_NUM_HEADERS);
let (mut tx, rx) = mpsc::channel(LIST_HEADERS_PAGE_SIZE);

let headers: Vec<u64> = if request.from_height != 0 {
let from_height = cmp::min(request.from_height, tip);

let headers: Vec<u64> = if from_height != 0 {
match sorting {
Sorting::Desc => ((cmp::max(0, request.from_height as i64 - num_headers as i64 + 1) as u64)..=
request.from_height)
.rev()
.collect(),
Sorting::Asc => (request.from_height..(request.from_height + num_headers)).collect(),
Sorting::Desc => {
let from = match from_height.overflowing_sub(num_headers) {
(_, true) => 0,
(res, false) => res + 1,
};
(from..=from_height).rev().collect()
},
Sorting::Asc => {
let to = match from_height.overflowing_add(num_headers) {
(_, true) => u64::MAX,
(res, false) => res,
};
(from_height..to).collect()
},
}
} else {
match sorting {
Sorting::Desc => ((cmp::max(0, tip as i64 - num_headers as i64 + 1) as u64)..=tip)
.rev()
.collect(),
Sorting::Desc => {
let from = match tip.overflowing_sub(num_headers) {
(_, true) => 0,
(res, false) => res + 1,
};
(from..=tip).rev().collect()
},
Sorting::Asc => (0..num_headers).collect(),
}
};
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/base_node/proto/rpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ message FindChainSplitResponse {
// An ordered list of headers starting from next header after the matching hash, up until `FindChainSplitRequest::count`
repeated tari.core.BlockHeader headers = 1;
// The index of the hash that matched from `FindChainSplitRequest::block_hashes`. This value could also be used to know how far back a split occurs.
uint32 fork_hash_index = 2;
uint64 fork_hash_index = 2;
/// The current header height of this node
uint64 tip_height = 3;
}
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/base_node/sync/header_sync/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub enum BlockHeaderSyncError {
#[error("Sync failed for all peers")]
SyncFailedAllPeers,
#[error("Peer sent a found hash index that was out of range (Expected less than {0}, Found: {1})")]
FoundHashIndexOutOfRange(u32, u32),
FoundHashIndexOutOfRange(u64, u64),
#[error("Failed to ban peer: {0}")]
FailedToBan(ConnectivityError),
#[error("Connectivity Error: {0}")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,15 +374,15 @@ impl<'a, B: BlockchainBackend + 'static> HeaderSynchronizer<'a, B> {
peer
);

if fork_hash_index >= block_hashes.len() as u32 {
if fork_hash_index >= block_hashes.len() as u64 {
let _ = self
.ban_peer_long(peer.clone(), BanReason::SplitHashGreaterThanHashes {
fork_hash_index,
num_block_hashes: block_hashes.len(),
})
.await;
return Err(BlockHeaderSyncError::FoundHashIndexOutOfRange(
block_hashes.len() as u32,
block_hashes.len() as u64,
fork_hash_index,
));
}
Expand Down Expand Up @@ -658,7 +658,7 @@ enum BanReason {
({num_block_hashes})"
)]
SplitHashGreaterThanHashes {
fork_hash_index: u32,
fork_hash_index: u64,
num_block_hashes: usize,
},
#[error("Peer sent invalid header: {0}")]
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/base_node/sync/rpc/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ impl<B: BlockchainBackend + 'static> BaseNodeSyncService for BaseNodeSyncRpcServ
.map_err(RpcStatus::log_internal_error(LOG_TARGET))?;

Ok(Response::new(FindChainSplitResponse {
fork_hash_index: idx as u32,
fork_hash_index: idx as u64,
headers: headers.into_iter().map(Into::into).collect(),
tip_height: metadata.height_of_longest_chain(),
}))
Expand Down