Skip to content

Commit f030ef6

Browse files
committed
Add getBestBlockId RPC method
1 parent 752aa7b commit f030ef6

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,4 @@ pub use service::ClientService;
105105
pub use spec::Spec;
106106
pub use state::{Asset, AssetAddress, AssetScheme, AssetSchemeAddress, ShardStateInfo, TopStateInfo};
107107
pub use transaction::{Error as TransactionError, Transaction};
108-
pub use types::{BlockId, BlockNumber, ParcelId};
108+
pub use types::{BlockId, BlockNumber, BlockNumberAndHash, ParcelId};

core/src/types/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,16 @@ mod block_status;
1818
mod ids;
1919
mod verification_queue_info;
2020

21+
use ctypes::H256;
22+
2123
pub use self::block_status::BlockStatus;
2224
pub use self::ids::{BlockId, ParcelId, TransactionId};
2325
pub use self::verification_queue_info::VerificationQueueInfo;
2426

2527
pub type BlockNumber = u64;
28+
29+
#[derive(Debug, Serialize)]
30+
pub struct BlockNumberAndHash {
31+
pub number: BlockNumber,
32+
pub hash: H256,
33+
}

rpc/src/v1/impls/chain.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use std::sync::Arc;
1818

1919
use ccore::{
2020
Asset, AssetAddress, AssetScheme, AssetSchemeAddress, Balance, BlockChainClient, BlockId, BlockInfo, BlockNumber,
21-
ChainInfo, Client, Invoice, Miner, MinerService, Nonce, ParcelInvoice, RegularKey, SignedParcel, TopStateInfo,
21+
BlockNumberAndHash, ChainInfo, Client, Invoice, Miner, MinerService, Nonce, ParcelInvoice, RegularKey,
22+
SignedParcel, TopStateInfo,
2223
};
2324
use ctypes::{H160, H256, Public, U256};
2425
use rlp::UntrustedRlp;
@@ -110,6 +111,13 @@ impl Chain for ChainClient {
110111
Ok(self.client.chain_info().best_block_number)
111112
}
112113

114+
fn get_best_block_id(&self) -> Result<BlockNumberAndHash> {
115+
Ok(BlockNumberAndHash {
116+
number: self.client.chain_info().best_block_number,
117+
hash: self.client.chain_info().best_block_hash,
118+
})
119+
}
120+
113121
fn get_block_hash(&self, block_number: u64) -> Result<Option<H256>> {
114122
Ok(self.client.block_hash(BlockId::Number(block_number)))
115123
}

rpc/src/v1/traits/chain.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17-
use ccore::{Asset, AssetScheme, BlockNumber, Invoice, ParcelInvoice};
17+
use ccore::{Asset, AssetScheme, BlockNumber, BlockNumberAndHash, Invoice, ParcelInvoice};
1818
use ctypes::{H160, H256, Public, U256};
1919

2020
use jsonrpc_core::Result;
@@ -63,6 +63,10 @@ build_rpc_trait! {
6363
# [rpc(name = "chain_getBestBlockNumber")]
6464
fn get_best_block_number(&self) -> Result<BlockNumber>;
6565

66+
/// Gets the number and the hash of the best block.
67+
# [rpc(name = "chain_getBestBlockId")]
68+
fn get_best_block_id(&self) -> Result<BlockNumberAndHash>;
69+
6670
/// Gets the hash of the block with given number.
6771
# [rpc(name = "chain_getBlockHash")]
6872
fn get_block_hash(&self, u64) -> Result<Option<H256>>;

0 commit comments

Comments
 (0)