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: fix get_shard_key #4744

Merged
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
1 change: 1 addition & 0 deletions applications/tari_app_grpc/proto/base_node.proto
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ message GetShardKeyRequest {

message GetShardKeyResponse {
bytes shard_key = 1;
bool found = 2;
}

message GetTemplateRegistrationsRequest {
Expand Down
15 changes: 12 additions & 3 deletions applications/tari_base_node/src/grpc/base_node_grpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1461,13 +1461,22 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
let mut handler = self.node_service.clone();
let public_key = PublicKey::from_bytes(&request.public_key)
.map_err(|e| obscure_error_if_true(report_error_flag, Status::invalid_argument(e.to_string())))?;

let shard_key = handler.get_shard_key(request.height, public_key).await.map_err(|e| {
error!(target: LOG_TARGET, "Error {}", e);
obscure_error_if_true(report_error_flag, Status::internal(e.to_string()))
})?;
Ok(Response::new(tari_rpc::GetShardKeyResponse {
shard_key: shard_key.to_vec(),
}))
if let Some(shard_key) = shard_key {
Ok(Response::new(tari_rpc::GetShardKeyResponse {
shard_key: shard_key.to_vec(),
found: true,
}))
} else {
Ok(Response::new(tari_rpc::GetShardKeyResponse {
shard_key: vec![],
found: false,
}))
}
}

async fn get_active_validator_nodes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub enum NodeCommsResponse {
},
FetchValidatorNodesKeysResponse(Vec<(PublicKey, [u8; 32])>),
FetchCommitteeResponse(Vec<ActiveValidatorNode>),
GetShardKeyResponse([u8; 32]),
GetShardKeyResponse(Option<[u8; 32]>),
FetchTemplateRegistrationsResponse(Vec<CodeTemplateRegistration>),
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,11 @@ impl LocalNodeCommsInterface {
}
}

pub async fn get_shard_key(&mut self, height: u64, public_key: PublicKey) -> Result<[u8; 32], CommsInterfaceError> {
pub async fn get_shard_key(
&mut self,
height: u64,
public_key: PublicKey,
) -> Result<Option<[u8; 32]>, CommsInterfaceError> {
match self
.request_sender
.call(NodeCommsRequest::GetShardKey { height, public_key })
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/chain_storage/async_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ impl<B: BlockchainBackend + 'static> AsyncBlockchainDb<B> {

make_async_fn!(fetch_committee(height: u64, shard: [u8;32]) -> Vec<ActiveValidatorNode>, "fetch_committee");

make_async_fn!(get_shard_key(height:u64, public_key: PublicKey) -> [u8;32], "get_shard_key");
make_async_fn!(get_shard_key(height:u64, public_key: PublicKey) -> Option<[u8;32]>, "get_shard_key");

make_async_fn!(fetch_template_registrations(from_height: u64) -> Vec<TemplateRegistration>, "fetch_template_registrations");
}
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/chain_storage/blockchain_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,6 @@ pub trait BlockchainBackend: Send + Sync {

fn fetch_active_validator_nodes(&self, height: u64) -> Result<Vec<(PublicKey, [u8; 32])>, ChainStorageError>;
fn fetch_committee(&self, height: u64, shard: [u8; 32]) -> Result<Vec<ActiveValidatorNode>, ChainStorageError>;
fn get_shard_key(&self, height: u64, public_key: PublicKey) -> Result<[u8; 32], ChainStorageError>;
fn get_shard_key(&self, height: u64, public_key: PublicKey) -> Result<Option<[u8; 32]>, ChainStorageError>;
fn fetch_template_registrations(&self, from_height: u64) -> Result<Vec<TemplateRegistration>, ChainStorageError>;
}
2 changes: 1 addition & 1 deletion base_layer/core/src/chain_storage/blockchain_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ where B: BlockchainBackend
db.fetch_mmr_size(tree)
}

pub fn get_shard_key(&self, height: u64, public_key: PublicKey) -> Result<[u8; 32], ChainStorageError> {
pub fn get_shard_key(&self, height: u64, public_key: PublicKey) -> Result<Option<[u8; 32]>, ChainStorageError> {
let db = self.db_read_access()?;
db.get_shard_key(height, public_key)
}
Expand Down
23 changes: 11 additions & 12 deletions base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2562,19 +2562,18 @@ impl BlockchainBackend for LMDBDatabase {
Ok(result)
}

fn get_shard_key(&self, height: u64, public_key: PublicKey) -> Result<[u8; 32], ChainStorageError> {
fn get_shard_key(&self, height: u64, public_key: PublicKey) -> Result<Option<[u8; 32]>, ChainStorageError> {
let txn = self.read_transaction()?;
let validator_nodes: Vec<ActiveValidatorNode> =
lmdb_get_multiple(&txn, &self.validator_nodes, public_key.as_bytes())?;
validator_nodes
.iter()
.find(|a| a.from_height <= height && height <= a.to_height)
.map(|a| a.shard_key)
.ok_or(ChainStorageError::ValueNotFound {
entity: "ShardKey",
field: "public_key",
value: public_key.to_hex(),
})
let mut validator_nodes: Vec<ActiveValidatorNode> =
lmdb_fetch_matching_after(&txn, &self.validator_nodes, public_key.as_bytes())?;
validator_nodes = validator_nodes
.into_iter()
.filter(|a| a.from_height <= height && height <= a.to_height)
.collect();
// get the last one
validator_nodes.sort_by(|a, b| a.from_height.cmp(&b.from_height));

Ok(validator_nodes.into_iter().map(|a| a.shard_key).last())
}

fn fetch_template_registrations(&self, from_height: u64) -> Result<Vec<TemplateRegistration>, ChainStorageError> {
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/test_helpers/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ impl BlockchainBackend for TempDatabase {
self.db.as_ref().unwrap().fetch_committee(height, shard)
}

fn get_shard_key(&self, height: u64, public_key: PublicKey) -> Result<[u8; 32], ChainStorageError> {
fn get_shard_key(&self, height: u64, public_key: PublicKey) -> Result<Option<[u8; 32]>, ChainStorageError> {
self.db.as_ref().unwrap().get_shard_key(height, public_key)
}

Expand Down
2 changes: 1 addition & 1 deletion clients/nodejs/base_node_grpc_client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const path = require("path");
const packageDefinition = protoLoader.loadSync(
path.resolve(
__dirname,
"../../../applications/tari_app_grpc/proto/base_node.proto"
"../../../../applications/tari_app_grpc/proto/base_node.proto"
),
{
keepCase: true,
Expand Down
2 changes: 1 addition & 1 deletion clients/nodejs/wallet_grpc_client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const protoLoader = require("@grpc/proto-loader");
const { promisifyAll } = require("grpc-promise");

const packageDefinition = protoLoader.loadSync(
`${__dirname}/../../applications/tari_app_grpc/proto/wallet.proto`,
`${__dirname}/../../../applications/tari_app_grpc/proto/wallet.proto`,
{
keepCase: true,
longs: String,
Expand Down