Skip to content

Commit

Permalink
feat(validator_node): add get_sidechain_block p2p rpc method (#3803)
Browse files Browse the repository at this point in the history
Description
---

- Add get_sidechain_blocks to validator node p2p rpc
- Use fixed 32-byte array for hashes on dan layer
- Implement FromStr and fallible integer conversions for TemplateId
- Remove asset_public_key from Instruction
- Add necessary chain db calls
- Add client call for get_sidechain_blocks
- Adds mocks and basic tests for get_sidechain_blocks validator node rpc

Motivation and Context
---
Allow client to fetch sidechain instruction state from vn

How Has This Been Tested?
---
Manually
  • Loading branch information
sdbondi committed Feb 8, 2022
1 parent a171513 commit 74df1d0
Show file tree
Hide file tree
Showing 72 changed files with 1,877 additions and 427 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Expand Up @@ -44,4 +44,7 @@ node_modules

# ignore output files from windows ISS
buildtools/Output/
/applications/tari_collectibles/src-tauri/data
/applications/tari_collectibles/src-tauri/data

# Asset files
assets/
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion applications/tari_app_grpc/proto/validator_node.proto
Expand Up @@ -99,4 +99,4 @@ message InvokeMethodRequest{
message InvokeMethodResponse {
string status = 1;
bytes result = 2;
}
}
15 changes: 8 additions & 7 deletions applications/tari_validator_node/Cargo.toml
Expand Up @@ -9,7 +9,7 @@ edition = "2018"

[dependencies]
tari_app_utilities = { path = "../tari_app_utilities" }
tari_app_grpc = {path = "../tari_app_grpc" }
tari_app_grpc = { path = "../tari_app_grpc" }
tari_common = { path = "../../common" }
tari_comms = { path = "../../comms" }
tari_comms_dht = { path = "../../comms/dht" }
Expand All @@ -20,10 +20,11 @@ tari_p2p = { path = "../../base_layer/p2p" }
tari_service_framework = { path = "../../base_layer/service_framework" }
tari_shutdown = { path = "../../infrastructure/shutdown" }
tari_storage = { path = "../../infrastructure/storage" }
tari_core = {path = "../../base_layer/core"}
tari_dan_core = {path = "../../dan_layer/core"}
tari_dan_storage_sqlite = {path = "../../dan_layer/storage_sqlite"}
tari_common_types = {path = "../../base_layer/common_types"}
tari_core = { path = "../../base_layer/core" }
tari_dan_core = { path = "../../dan_layer/core" }
tari_dan_storage_sqlite = { path = "../../dan_layer/storage_sqlite" }
tari_dan_common_types = { path = "../../dan_layer/common_types" }
tari_common_types = { path = "../../base_layer/common_types" }

anyhow = "1.0.53"
async-trait = "0.1.50"
Expand All @@ -37,7 +38,7 @@ prost = "0.9"
prost-types = "0.9"
serde = "1.0.126"
thiserror = "^1.0.20"
tokio = { version="1.10", features = ["macros", "time", "sync"]}
tokio = { version = "1.10", features = ["macros", "time", "sync"] }
tokio-stream = { version = "0.1.7", features = ["sync"] }
tonic = "0.6.2"

Expand All @@ -47,7 +48,7 @@ bytecodec = { version = "0.4.14", features = ["bincode_codec"] }
serde_json = "1.0.64"

[dev-dependencies]
tari_test_utils = "0.8.1"
tari_test_utils = { path = "../../infrastructure/test_utils" }

[build-dependencies]
tari_common = { path = "../../common", features = ["build"] }
3 changes: 2 additions & 1 deletion applications/tari_validator_node/build.rs
Expand Up @@ -22,7 +22,8 @@

fn main() -> Result<(), Box<dyn std::error::Error>> {
tari_common::build::ProtobufCompiler::new()
.proto_paths(&["proto/p2p"])
.proto_paths(&["proto/dan"])
.include_paths(&["proto/dan"])
.emit_rerun_if_changed_directives()
.compile()
.unwrap();
Expand Down
27 changes: 27 additions & 0 deletions applications/tari_validator_node/proto/dan/common.proto
@@ -0,0 +1,27 @@
syntax = "proto3";

package tari.dan.common;

message SideChainBlock {
Node node = 1;
InstructionSet instructions = 2;
}

message Node {
bytes hash = 1;
bytes parent = 2;
uint32 height = 3;
bool is_committed = 4;
}

message Instruction {
uint32 template_id = 1;
string method = 2;
bytes args = 3;
// bytes token_id = 5;
// bytes signature = 6;
}

message InstructionSet{
repeated Instruction instructions = 1;
}
@@ -1,6 +1,8 @@
syntax = "proto3";

package tari.p2p.dan;
package tari.dan.consensus;

import "common.proto";

enum HotStuffMessageType {
HOT_STUFF_MESSAGE_TYPE_UNKNOWN = 0;
Expand Down Expand Up @@ -41,22 +43,10 @@ message Signature{
}

message TariDanPayload {
InstructionSet instruction_set = 1;
tari.dan.common.InstructionSet instruction_set = 1;
CheckpointData checkpoint = 2;
}

message CheckpointData {
// todo: fill this in
}

message InstructionSet{
repeated Instruction instructions = 1;
}
message Instruction {
bytes asset_public_key = 1;
uint32 template_id = 2;
string method = 3;
bytes args = 4;
// bytes token_id = 5;
// bytes signature = 6;
}
Expand Up @@ -21,7 +21,9 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
syntax = "proto3";

package tari.p2p.validator_node;
package tari.dan.validator_node;

import "common.proto";

service ValidatorNode {
rpc GetTokenData(GetTokenDataRequest) returns (GetTokenDataResponse);
Expand Down Expand Up @@ -64,4 +66,15 @@ message InvokeMethodRequest{
message InvokeMethodResponse {
bytes result = 1;
Status status = 2;
}
}

message GetSidechainBlocksRequest {
bytes asset_public_key = 1;
bytes start_hash = 2;
bytes end_hash = 3;
}


message GetSidechainBlocksResponse {
tari.dan.common.SideChainBlock block = 1;
}
25 changes: 11 additions & 14 deletions applications/tari_validator_node/src/dan_node.rs
Expand Up @@ -115,7 +115,7 @@ impl DanNode {
}
}
info!(target: LOG_TARGET, "Adding asset {:?}", asset.public_key);
let node_identitiy = node_identity.as_ref().clone();
let node_identity = node_identity.as_ref().clone();
let mempool = mempool_service.clone();
let handles = handles.clone();
let subscription_factory = subscription_factory.clone();
Expand All @@ -124,19 +124,16 @@ impl DanNode {
let db_factory = db_factory.clone();
tasks.insert(
asset.public_key.clone(),
task::spawn(async move {
DanNode::start_asset_worker(
asset.clone(),
node_identitiy,
mempool,
handles,
subscription_factory,
shutdown,
dan_config,
db_factory,
)
.await
}),
task::spawn(DanNode::start_asset_worker(
asset.clone(),
node_identity,
mempool,
handles,
subscription_factory,
shutdown,
dan_config,
db_factory,
)),
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_validator_node/src/grpc/conversions.rs
Expand Up @@ -30,7 +30,7 @@ impl From<SidechainMetadata> for St {
Self(tari_rpc::SidechainMetadata {
asset_public_key: source.asset_public_key().as_bytes().to_vec(),
committed_height: source.committed_height().into(),
committed_hash: source.committed_hash().clone().into(),
committed_hash: source.committed_hash().as_bytes().to_vec(),
})
}
}
2 changes: 1 addition & 1 deletion applications/tari_validator_node/src/grpc/mod.rs
Expand Up @@ -19,6 +19,6 @@
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
pub(crate) mod conversions;
mod conversions;
pub mod services;
pub(crate) mod validator_node_grpc_server;
Expand Up @@ -19,12 +19,13 @@
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
use std::convert::TryInto;

use tari_app_grpc::tari_rpc as rpc;
use tari_common_types::types::PublicKey;
use tari_comms::NodeIdentity;
use tari_crypto::tari_utilities::ByteArray;
use tari_dan_core::{
models::TemplateId,
services::{AssetProcessor, AssetProxy, ServiceSpecification},
storage::DbFactory,
};
Expand Down Expand Up @@ -90,7 +91,10 @@ impl<TServiceSpecification: ServiceSpecification + 'static> rpc::validator_node_
.asset_proxy
.invoke_method(
&asset_public_key,
request.template_id.into(),
request
.template_id
.try_into()
.map_err(|_| Status::invalid_argument("invalid template_id"))?,
request.method.clone(),
request.args.clone(),
)
Expand Down Expand Up @@ -130,6 +134,10 @@ impl<TServiceSpecification: ServiceSpecification + 'static> rpc::validator_node_
let request = request.into_inner();
let asset_public_key = PublicKey::from_bytes(&request.asset_public_key)
.map_err(|err| Status::invalid_argument(format!("Asset public key was not a valid public key:{}", err)))?;
let template_id = request
.template_id
.try_into()
.map_err(|_| Status::invalid_argument("Invalid template_id"))?;
if let Some(state) = self
.db_factory
.get_state_db(&asset_public_key)
Expand All @@ -138,12 +146,7 @@ impl<TServiceSpecification: ServiceSpecification + 'static> rpc::validator_node_
let mut unit_of_work = state.new_unit_of_work();
let response_bytes = self
.asset_processor
.invoke_read_method(
TemplateId::from(request.template_id),
request.method,
&request.args,
&mut unit_of_work,
)
.invoke_read_method(template_id, request.method, &request.args, &mut unit_of_work)
.map_err(|e| Status::internal(format!("Could not invoke read method: {}", e)))?;
Ok(Response::new(rpc::InvokeReadMethodResponse {
result: response_bytes.unwrap_or_default(),
Expand All @@ -157,12 +160,7 @@ impl<TServiceSpecification: ServiceSpecification + 'static> rpc::validator_node_
// Forward to proxy
let response_bytes = self
.asset_proxy
.invoke_read_method(
&asset_public_key,
TemplateId::from(request.template_id),
request.method,
request.args,
)
.invoke_read_method(&asset_public_key, template_id, request.method, request.args)
.await
.map_err(|err| Status::internal(format!("Error calling proxied method:{}", err)))?;
// TODO: Populate authority
Expand Down

0 comments on commit 74df1d0

Please sign in to comment.