Skip to content

Commit

Permalink
fix: no default values when unexpected errors occurred
Browse files Browse the repository at this point in the history
  • Loading branch information
yangby-cryptape committed Aug 3, 2023
1 parent 8f3eded commit d4bf768
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
16 changes: 12 additions & 4 deletions core/consensus/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ impl<Adapter: ConsensusAdapter + 'static> Engine<Proposal> for ConsensusEngine<A
let txs_root = if !txs.hashes.is_empty() {
TrieMerkle::from_iter(txs.hashes.iter().enumerate())
.root_hash()
.unwrap_or_default()
.unwrap_or_else(|err| {
panic!("failed to calculate trie root hash for transactions since {err}")
})
} else {
RLP_NULL
};
Expand Down Expand Up @@ -540,9 +542,15 @@ impl<Adapter: ConsensusAdapter + 'static> ConsensusEngine<Adapter> {
proposal: &Proposal,
signed_txs: &[SignedTransaction],
) -> ProtocolResult<()> {
let txs_root = TrieMerkle::from_iter(proposal.tx_hashes.iter().enumerate())
.root_hash()
.unwrap_or(RLP_NULL);
let txs_root = if proposal.tx_hashes.is_empty() {
RLP_NULL
} else {
TrieMerkle::from_iter(proposal.tx_hashes.iter().enumerate())
.root_hash()
.unwrap_or_else(|err| {
panic!("failed to calculate trie root hash for proposals since {err}")
})
};

let stxs_hash = Hasher::digest(rlp::encode_list(signed_txs));

Expand Down
2 changes: 1 addition & 1 deletion core/executor/src/adapter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ where
fn exists(&self, address: H160) -> bool {
self.trie
.contains(&Bytes::from(address.as_bytes().to_vec()))
.unwrap_or_default()
.unwrap_or(false)
}

fn basic(&self, address: H160) -> Basic {
Expand Down
33 changes: 21 additions & 12 deletions core/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,18 @@ impl Executor for AxonExecutor {

// self.update_system_contract_roots_for_external_module();

// TODO When fixes receipt_root, set to RLP_NULL when empty
let receipt_root = TrieMerkle::from_iter(hashes.iter().enumerate())
.root_hash()
.unwrap_or_else(|err| {
panic!("failed to calculate trie root hash for receipts since {err}")
});

ExecResp {
state_root: new_state_root,
receipt_root: TrieMerkle::from_iter(hashes.iter().enumerate())
.root_hash()
.unwrap_or_default(),
gas_used: gas,
tx_resp: res,
state_root: new_state_root,
receipt_root,
gas_used: gas,
tx_resp: res,
}
}

Expand Down Expand Up @@ -372,13 +377,17 @@ impl AxonExecutor {
// commit changes by all txs included in this block only once
let new_state_root = adapter.commit();

let receipt_root = TrieMerkle::from_iter(hashes.iter().enumerate())
.root_hash()
.unwrap_or_else(|err| {
panic!("failed to calculate trie root hash for receipts since {err}")
});

ExecResp {
state_root: new_state_root,
receipt_root: TrieMerkle::from_iter(hashes.iter().enumerate())
.root_hash()
.unwrap_or_default(),
gas_used: gas,
tx_resp: res,
state_root: new_state_root,
receipt_root,
gas_used: gas,
tx_resp: res,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/executor/src/precompiles/get_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl PrecompileContract for GetCell {
cell_data: c.cell_data.into(),
is_consumed: c.consumed_number.is_some(),
created_number: c.created_number,
consumed_number: c.consumed_number.unwrap_or_default(),
consumed_number: c.consumed_number.unwrap_or(0),
})
.unwrap_or_default();

Expand Down
6 changes: 1 addition & 5 deletions protocol/src/types/interoperation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,7 @@ impl From<&CellWithData> for CellMeta {

impl CellWithData {
pub fn capacity(&self) -> u64 {
let capacity = self
.type_script
.as_ref()
.map(|s| s.len())
.unwrap_or_default()
let capacity = self.type_script.as_ref().map(|s| s.len()).unwrap_or(0)
+ self.lock_script.len()
+ self.data.len()
+ CAPACITY_BYTES_LEN;
Expand Down

0 comments on commit d4bf768

Please sign in to comment.