Skip to content

Commit

Permalink
Merge pull request #2952 from Pana/opCoreBlockRpc
Browse files Browse the repository at this point in the history
Update core space RPC block.gasUsed behavior to express more info
  • Loading branch information
Pana authored Nov 19, 2024
2 parents 674b472 + 23a5863 commit 1c06aa7
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 12 deletions.
5 changes: 5 additions & 0 deletions changelogs/JSONRPC.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# JSON-RPC CHANGELOG

## vNext

1. The gasUsed field of Core Space block will return `null` if the block is not executed.
2. Core Space pubsub block header notification add two new fields: `baseFeePerGas` and `custom`.

## v2.4.1

1. eSpace add new RPC method `eth_getBlockReceipts`
Expand Down
2 changes: 1 addition & 1 deletion crates/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ rand_08 = {package = "rand", version = "0.8.0"}
kvdb-rocksdb = { workspace = true }
tempdir = "0.3"
rustc-hex = { workspace = true }
threadpool = "1.0"
threadpool = { workspace = true }
metrics = { workspace = true }
delegate = { workspace = true }
transient-hashmap = "0.4"
Expand Down
22 changes: 15 additions & 7 deletions crates/client/src/rpc/types/cfx/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ impl Block {
// get the block.gas_used
let tx_len = b.transactions.len();

let (gas_used, transactions) = if tx_len == 0 {
(Some(U256::from(0)), BlockTransactions::Hashes(vec![]))
} else {
let (gas_used, transactions) = {
let maybe_results = consensus_inner
.block_execution_results_by_hash(
&b.hash(),
Expand Down Expand Up @@ -427,11 +425,13 @@ pub struct Header {
pub block_number: Option<U256>,
/// Gas Limit
pub gas_limit: U256,
/// Base fee
#[serde(skip_serializing_if = "Option::is_none")]
pub base_fee_per_gas: Option<U256>,
/// Timestamp
pub timestamp: U256,
/// Difficulty
pub difficulty: U256,
// TODO: We should change python test script and remove this field
/// PoW Quality
pub pow_quality: Option<U256>,
/// Referee hashes
Expand All @@ -440,6 +440,8 @@ pub struct Header {
pub adaptive: bool,
/// Nonce of the block
pub nonce: U256,
/// Custom field
pub custom: Vec<Bytes>,
/// PoS reference.
pub pos_reference: Option<PosBlockId>,
}
Expand All @@ -458,6 +460,9 @@ impl Header {

let block_number = consensus.get_block_number(&hash)?.map(Into::into);

let base_fee_per_gas: Option<U256> =
h.base_price().map(|x| x[Space::Native]).into();

let referee_hashes =
h.referee_hashes().iter().map(|x| H256::from(*x)).collect();

Expand All @@ -474,16 +479,17 @@ impl Header {
epoch_number,
block_number,
gas_limit: h.gas_limit().into(),
base_fee_per_gas,
timestamp: h.timestamp().into(),
difficulty: h.difficulty().into(),
adaptive: h.adaptive(),
referee_hashes,
nonce: h.nonce().into(),
pow_quality: h.pow_hash.map(|pow_hash| {
pow::pow_hash_to_quality(&pow_hash, &h.nonce())
}), /* TODO(thegaram):
* include custom */
}),
pos_reference: *h.pos_reference(),
custom: h.custom().clone().into_iter().map(Into::into).collect(),
})
}
}
Expand Down Expand Up @@ -619,6 +625,8 @@ mod tests {
transactions_root: KECCAK_EMPTY_LIST_RLP.into(),
epoch_number: None,
block_number: None,
base_fee_per_gas: None,
custom: vec![],
gas_limit: U256::default(),
timestamp: 0.into(),
difficulty: U256::default(),
Expand All @@ -632,7 +640,7 @@ mod tests {

assert_eq!(
serialized_header,
r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000000","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","height":"0x0","miner":"CFX:TYPE.NULL:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA0SFBNJM2","deferredStateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","deferredReceiptsRoot":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","deferredLogsBloomHash":"0xd397b3b043d87fcd6fad1291ff0bfd16401c274896d8c63a923727f077b8e0b5","blame":"0x0","transactionsRoot":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","epochNumber":null,"blockNumber":null,"gasLimit":"0x0","timestamp":"0x0","difficulty":"0x0","powQuality":null,"refereeHashes":[],"adaptive":false,"nonce":"0x0","posReference":null}"#
r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000000","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","height":"0x0","miner":"CFX:TYPE.NULL:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA0SFBNJM2","deferredStateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","deferredReceiptsRoot":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","deferredLogsBloomHash":"0xd397b3b043d87fcd6fad1291ff0bfd16401c274896d8c63a923727f077b8e0b5","blame":"0x0","transactionsRoot":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","epochNumber":null,"blockNumber":null,"gasLimit":"0x0","timestamp":"0x0","difficulty":"0x0","powQuality":null,"refereeHashes":[],"adaptive":false,"nonce":"0x0","custom":[],"posReference":null}"#
);
}
}
4 changes: 2 additions & 2 deletions tests/full_node_tests/remove_old_eras_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ def run_test(self):
self.log.info(f"checking existing blocks...")

# TODO We can use `num_blocks` here if we can guarantee all blocks form a chain.
latest_epoch = self.rpc[ARCHIVE_NODE].epoch_number()
latest_epoch = self.rpc[ARCHIVE_NODE].epoch_number("latest_state")
# we expect the last few eras are not removed
for epoch in range(8 * ERA_EPOCH_COUNT, latest_epoch+1):
for epoch in range(8 * ERA_EPOCH_COUNT, latest_epoch):
archive_block = self.rpc[ARCHIVE_NODE].block_by_epoch(hex(epoch), include_txs=True)
assert(archive_block != None)

Expand Down
2 changes: 1 addition & 1 deletion tests/light/rpc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ def assert_blocks_equal(self, light_block, block):

# full nodes will use '0x0' for empty blocks and None
# for transactions not executed yet
block['gasUsed'] = '0x0' if block['gasUsed'] == '0x0' else None
block['gasUsed'] = None

for tx in block['transactions']:
if type(tx) is not dict: continue
Expand Down
8 changes: 7 additions & 1 deletion tests/tx_consistency_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def test(self, num_senders, num_receivers, num_txs):
self.check_with_rpc(client.epoch_number)
self.check_with_rpc(client.best_block_hash)
self.check_with_rpc(client.gas_price)
time.sleep(5)
self.check_with_rpc(client.chain, True)

# check receipt
Expand Down Expand Up @@ -198,7 +199,12 @@ def check_with_rpc(self, client_rpc, collection_result=False):
for name in dir(client):
if name == client_rpc.__name__:
value = getattr(client, name)()
assert_equal(value, expected_value)
if collection_result:
assert_equal(len(value), len(expected_value))
for idy in range(len(value) - 7):
assert_equal(value[idy], expected_value[idy])
else:
assert_equal(value, expected_value)

if collection_result:
self.log.info("check RPC: API = {}, Len = {}".format(client_rpc.__name__, len(expected_value)))
Expand Down

0 comments on commit 1c06aa7

Please sign in to comment.