Skip to content

Commit

Permalink
subnet: Update ChainVm trait (#49)
Browse files Browse the repository at this point in the history
* subnet: Update ChainVm trait

Updates the trait to incorporate the latest avalanchego interface changes.

Signed-off-by: Dan Sover <dan.sover@avalabs.org>

* subnet: Implement new trait methods on rpcvm server

Signed-off-by: Dan Sover <dan.sover@avalabs.org>

* fix: Update documentation

Signed-off-by: Dan Sover <dan.sover@avalabs.org>

* subnet: Add state_sync_enabled fn

Signed-off-by: Dan Sover <dan.sover@avalabs.org>

* fix: error handling

Signed-off-by: Dan Sover <dan.sover@avalabs.org>

* fix: Error handling

Signed-off-by: Dan Sover <dan.sover@avalabs.org>

* fix: Refactor error code function

Signed-off-by: Dan Sover <dan.sover@avalabs.org>

---------

Signed-off-by: Dan Sover <dan.sover@avalabs.org>
  • Loading branch information
exdx authored Sep 18, 2023
1 parent 3b10748 commit df3621b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 18 deletions.
6 changes: 3 additions & 3 deletions crates/avalanche-types/src/subnet/rpc/database/rpcdb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ lazy_static! {
};
}

pub fn error_to_error_code(msg: &str) -> io::Result<i32> {
pub fn error_to_error_code(msg: &str) -> i32 {
match ERROR_TO_ERROR_CODE.get(msg) {
None => Ok(0),
Some(code) => Ok(*code),
None => 0_i32,
Some(code) => *code,
}
}

Expand Down
20 changes: 10 additions & 10 deletions crates/avalanche-types/src/subnet/rpc/database/rpcdb/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl pb::rpcdb::database_server::Database for Server {
})),
Err(e) => Ok(Response::new(HasResponse {
has: false,
err: error_to_error_code(&e.to_string()).unwrap(),
err: error_to_error_code(&e.to_string()),
})),
}
}
Expand All @@ -76,7 +76,7 @@ impl pb::rpcdb::database_server::Database for Server {
})),
Err(e) => Ok(Response::new(GetResponse {
value: Bytes::from(""),
err: error_to_error_code(&e.to_string()).unwrap(),
err: error_to_error_code(&e.to_string()),
})),
}
}
Expand All @@ -90,7 +90,7 @@ impl pb::rpcdb::database_server::Database for Server {
err: pb::rpcdb::Error::Unspecified.into(),
})),
Err(e) => Ok(Response::new(PutResponse {
err: error_to_error_code(&e.to_string()).unwrap(),
err: error_to_error_code(&e.to_string()),
})),
}
}
Expand All @@ -107,7 +107,7 @@ impl pb::rpcdb::database_server::Database for Server {
err: pb::rpcdb::Error::Unspecified.into(),
})),
Err(e) => Ok(Response::new(DeleteResponse {
err: error_to_error_code(&e.to_string()).unwrap(),
err: error_to_error_code(&e.to_string()),
})),
}
}
Expand All @@ -130,7 +130,7 @@ impl pb::rpcdb::database_server::Database for Server {
err: pb::rpcdb::Error::Unspecified.into(),
})),
Err(e) => Ok(Response::new(CloseResponse {
err: error_to_error_code(&e.to_string()).unwrap(),
err: error_to_error_code(&e.to_string()),
})),
}
}
Expand Down Expand Up @@ -164,7 +164,7 @@ impl pb::rpcdb::database_server::Database for Server {
let resp = batch.put(&put.key, &put.value).await;
if let Err(e) = resp {
return Ok(Response::new(WriteBatchResponse {
err: error_to_error_code(&e.to_string()).unwrap(),
err: error_to_error_code(&e.to_string()),
}));
}
}
Expand All @@ -173,7 +173,7 @@ impl pb::rpcdb::database_server::Database for Server {
let resp = batch.delete(&del.key).await;
if let Err(e) = resp {
return Ok(Response::new(WriteBatchResponse {
err: error_to_error_code(&e.to_string()).unwrap(),
err: error_to_error_code(&e.to_string()),
}));
}
}
Expand All @@ -186,7 +186,7 @@ impl pb::rpcdb::database_server::Database for Server {
}
Err(e) => {
return Ok(Response::new(WriteBatchResponse {
err: error_to_error_code(&e.to_string()).unwrap(),
err: error_to_error_code(&e.to_string()),
}))
}
}
Expand Down Expand Up @@ -253,7 +253,7 @@ impl pb::rpcdb::database_server::Database for Server {
}
Err(e) => {
return Ok(Response::new(IteratorErrorResponse {
err: error_to_error_code(&e.to_string()).unwrap(),
err: error_to_error_code(&e.to_string()),
}))
}
}
Expand All @@ -279,7 +279,7 @@ impl pb::rpcdb::database_server::Database for Server {
}
Err(e) => {
return Ok(Response::new(IteratorReleaseResponse {
err: error_to_error_code(&e.to_string()).unwrap(),
err: error_to_error_code(&e.to_string()),
}))
}
}
Expand Down
12 changes: 12 additions & 0 deletions crates/avalanche-types/src/subnet/rpc/snowman/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ pub trait ChainVm: CommonVm + BatchedChainVm + Getter + Parser {
/// Returns the ID of the last accepted block.
/// If no blocks have been accepted, this should return the genesis block
async fn last_accepted(&self) -> Result<Id>;

/// Returns empty if the height index is available.
/// Returns ErrIndexIncomplete if the height index is not currently available.
/// TODO: Remove after v1.11.x activates.
async fn verify_height_index(&self) -> Result<()>;

/// Returns the ID of the block that was accepted with [height].
/// Returns ErrNotFound if the [height] index is unknown.
async fn get_block_id_at_height(&self, height: u64) -> Result<Id>;

/// Returns whether state sync is enabled.
async fn state_sync_enabled(&self) -> Result<bool>;
}

/// ref. <https://pkg.go.dev/github.com/ava-labs/avalanchego/snow/engine/snowman/block#Getter>
Expand Down
46 changes: 41 additions & 5 deletions crates/avalanche-types/src/subnet/rpc/vm/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ where
status: 0,
height: 0,
timestamp: Some(timestamp_from_time(&Utc.timestamp_opt(0, 0).unwrap())),
err: error_to_error_code(&e.to_string()).unwrap(),
err: error_to_error_code(&e.to_string()),
verify_with_context: false,
}))
}
Expand Down Expand Up @@ -922,7 +922,11 @@ where
) -> std::result::Result<Response<vm::StateSyncEnabledResponse>, tonic::Status> {
log::debug!("state_sync_enabled called");

Err(tonic::Status::unimplemented("state_sync_enabled"))
// TODO: Implement state sync request/response
Ok(Response::new(vm::StateSyncEnabledResponse {
enabled: false,
err: 0,
}))
}

async fn get_ongoing_sync_state_summary(
Expand Down Expand Up @@ -977,15 +981,47 @@ where
_req: Request<Empty>,
) -> std::result::Result<Response<vm::VerifyHeightIndexResponse>, tonic::Status> {
log::debug!("verify_height_index called");
Err(tonic::Status::unimplemented("verify_height_index"))

let inner_vm = self.vm.read().await;

match inner_vm.verify_height_index().await {
Ok(_) => return Ok(Response::new(vm::VerifyHeightIndexResponse { err: 0 })),
Err(e) => {
if error_to_error_code(&e.to_string()) != 0 {
return Ok(Response::new(vm::VerifyHeightIndexResponse {
err: error_to_error_code(&e.to_string()),
}));
}
return Err(tonic::Status::unknown(e.to_string()));
}
}
}

async fn get_block_id_at_height(
&self,
_req: Request<vm::GetBlockIdAtHeightRequest>,
req: Request<vm::GetBlockIdAtHeightRequest>,
) -> std::result::Result<Response<vm::GetBlockIdAtHeightResponse>, tonic::Status> {
log::debug!("get_block_id_at_height called");

Err(tonic::Status::unimplemented("get_block_id_at_height"))
let msg = req.into_inner();
let inner_vm = self.vm.read().await;

match inner_vm.get_block_id_at_height(msg.height).await {
Ok(height) => {
return Ok(Response::new(vm::GetBlockIdAtHeightResponse {
blk_id: height.to_vec().into(),
err: 0,
}))
}
Err(e) => {
if error_to_error_code(&e.to_string()) != 0 {
return Ok(Response::new(vm::GetBlockIdAtHeightResponse {
blk_id: vec![].into(),
err: error_to_error_code(&e.to_string()),
}));
}
return Err(tonic::Status::unknown(e.to_string()));
}
}
}
}

0 comments on commit df3621b

Please sign in to comment.