Skip to content

Commit

Permalink
Make fields optional when deserializing for compatibility (#616)
Browse files Browse the repository at this point in the history
* Make some block fields optional for Celo compatibility

* Make web3::types::trace_filtering::Call.input optional when deserializing

* Make types::trace_filtering::CallResult.output optional

* Remove comment referring to Celo

* Add `allow-missing-fields` feature

---------

Co-authored-by: David Lutterkort <lutter@watzmann.net>
  • Loading branch information
leoyvens and lutter authored Jun 2, 2023
1 parent 85abcf4 commit da2a0a3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,6 @@ ws-tls-async-std = ["async-native-tls", "async-native-tls/runtime-async-std", "w
ipc-tokio = ["tokio", "tokio-stream", "tokio-util"]
arbitrary_precision = ["serde_json/arbitrary_precision", "jsonrpc-core/arbitrary_precision"]
test = []
allow-missing-fields = []

[workspace]
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,6 @@ The library supports following features:
- `eip-1193` - Enable EIP-1193 support.
- `wasm` - Compile for WASM (make sure to disable default features).
- `arbitrary_precision` - Enable `arbitrary_precision` in `serde_json`.
- `allow-missing-fields` - Some response fields are mandatory in Ethereum but not present in
EVM-compatible chains such as Celo and Fantom. This feature enables compatibility by setting a
default value on those fields.
7 changes: 7 additions & 0 deletions src/types/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct BlockHeader {
pub parent_hash: H256,
/// Hash of the uncles
#[serde(rename = "sha3Uncles")]
#[cfg_attr(feature = "allow-missing-fields", serde(default))]
pub uncles_hash: H256,
/// Miner/author's address.
#[serde(rename = "miner", default, deserialize_with = "null_to_default")]
Expand All @@ -31,6 +32,7 @@ pub struct BlockHeader {
pub gas_used: U256,
/// Gas Limit
#[serde(rename = "gasLimit")]
#[cfg_attr(feature = "allow-missing-fields", serde(default))]
pub gas_limit: U256,
/// Base fee per unit of gas (if past London)
#[serde(rename = "baseFeePerGas", skip_serializing_if = "Option::is_none")]
Expand All @@ -44,6 +46,7 @@ pub struct BlockHeader {
/// Timestamp
pub timestamp: U256,
/// Difficulty
#[cfg_attr(feature = "allow-missing-fields", serde(default))]
pub difficulty: U256,
/// Mix Hash
#[serde(rename = "mixHash")]
Expand All @@ -63,6 +66,7 @@ pub struct Block<TX> {
pub parent_hash: H256,
/// Hash of the uncles
#[serde(rename = "sha3Uncles")]
#[cfg_attr(feature = "allow-missing-fields", serde(default))]
pub uncles_hash: H256,
/// Miner/author's address.
#[serde(rename = "miner", default, deserialize_with = "null_to_default")]
Expand All @@ -83,6 +87,7 @@ pub struct Block<TX> {
pub gas_used: U256,
/// Gas Limit
#[serde(rename = "gasLimit")]
#[cfg_attr(feature = "allow-missing-fields", serde(default))]
pub gas_limit: U256,
/// Base fee per unit of gas (if past London)
#[serde(rename = "baseFeePerGas", skip_serializing_if = "Option::is_none")]
Expand All @@ -96,6 +101,7 @@ pub struct Block<TX> {
/// Timestamp
pub timestamp: U256,
/// Difficulty
#[cfg_attr(feature = "allow-missing-fields", serde(default))]
pub difficulty: U256,
/// Total difficulty
#[serde(rename = "totalDifficulty")]
Expand All @@ -104,6 +110,7 @@ pub struct Block<TX> {
#[serde(default, rename = "sealFields")]
pub seal_fields: Vec<Bytes>,
/// Uncles' hashes
#[cfg_attr(feature = "allow-missing-fields", serde(default))]
pub uncles: Vec<H256>,
/// Transactions
pub transactions: Vec<TX>,
Expand Down
2 changes: 2 additions & 0 deletions src/types/trace_filtering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ pub struct CallResult {
#[serde(rename = "gasUsed")]
pub gas_used: U256,
/// Output bytes
#[cfg_attr(feature = "allow-missing-fields", serde(default))]
pub output: Bytes,
}

Expand Down Expand Up @@ -187,6 +188,7 @@ pub struct Call {
/// Gas
pub gas: U256,
/// Input data
#[cfg_attr(feature = "allow-missing-fields", serde(default))]
pub input: Bytes,
/// The type of the call.
#[serde(rename = "callType")]
Expand Down

0 comments on commit da2a0a3

Please sign in to comment.