From df3621b5cd0b0e59c1984a082edf13691045a461 Mon Sep 17 00:00:00 2001 From: exdx Date: Mon, 18 Sep 2023 17:10:22 -0400 Subject: [PATCH] subnet: Update ChainVm trait (#49) * subnet: Update ChainVm trait Updates the trait to incorporate the latest avalanchego interface changes. Signed-off-by: Dan Sover * subnet: Implement new trait methods on rpcvm server Signed-off-by: Dan Sover * fix: Update documentation Signed-off-by: Dan Sover * subnet: Add state_sync_enabled fn Signed-off-by: Dan Sover * fix: error handling Signed-off-by: Dan Sover * fix: Error handling Signed-off-by: Dan Sover * fix: Refactor error code function Signed-off-by: Dan Sover --------- Signed-off-by: Dan Sover --- .../src/subnet/rpc/database/rpcdb/mod.rs | 6 +-- .../subnet/rpc/database/rpcdb/server/mod.rs | 20 ++++---- .../src/subnet/rpc/snowman/block.rs | 12 +++++ .../src/subnet/rpc/vm/server.rs | 46 +++++++++++++++++-- 4 files changed, 66 insertions(+), 18 deletions(-) diff --git a/crates/avalanche-types/src/subnet/rpc/database/rpcdb/mod.rs b/crates/avalanche-types/src/subnet/rpc/database/rpcdb/mod.rs index e17e948..a68e969 100644 --- a/crates/avalanche-types/src/subnet/rpc/database/rpcdb/mod.rs +++ b/crates/avalanche-types/src/subnet/rpc/database/rpcdb/mod.rs @@ -17,10 +17,10 @@ lazy_static! { }; } -pub fn error_to_error_code(msg: &str) -> io::Result { +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, } } diff --git a/crates/avalanche-types/src/subnet/rpc/database/rpcdb/server/mod.rs b/crates/avalanche-types/src/subnet/rpc/database/rpcdb/server/mod.rs index 949c29f..4222c21 100644 --- a/crates/avalanche-types/src/subnet/rpc/database/rpcdb/server/mod.rs +++ b/crates/avalanche-types/src/subnet/rpc/database/rpcdb/server/mod.rs @@ -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()), })), } } @@ -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()), })), } } @@ -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()), })), } } @@ -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()), })), } } @@ -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()), })), } } @@ -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()), })); } } @@ -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()), })); } } @@ -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()), })) } } @@ -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()), })) } } @@ -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()), })) } } diff --git a/crates/avalanche-types/src/subnet/rpc/snowman/block.rs b/crates/avalanche-types/src/subnet/rpc/snowman/block.rs index 45ca15c..7d3693b 100644 --- a/crates/avalanche-types/src/subnet/rpc/snowman/block.rs +++ b/crates/avalanche-types/src/subnet/rpc/snowman/block.rs @@ -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; + + /// 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; + + /// Returns whether state sync is enabled. + async fn state_sync_enabled(&self) -> Result; } /// ref. diff --git a/crates/avalanche-types/src/subnet/rpc/vm/server.rs b/crates/avalanche-types/src/subnet/rpc/vm/server.rs index 5858973..a579871 100644 --- a/crates/avalanche-types/src/subnet/rpc/vm/server.rs +++ b/crates/avalanche-types/src/subnet/rpc/vm/server.rs @@ -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, })) } @@ -922,7 +922,11 @@ where ) -> std::result::Result, 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( @@ -977,15 +981,47 @@ where _req: Request, ) -> std::result::Result, 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, + req: Request, ) -> std::result::Result, 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())); + } + } } }