Skip to content

Commit

Permalink
fix: fix always success cell init
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed Aug 10, 2023
1 parent 5bb8e5d commit 45da7b8
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 46 deletions.
15 changes: 0 additions & 15 deletions core/executor/src/system_contract/image_cell/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
mod abi;
mod store;

pub mod utils;

pub use abi::image_cell_abi;
pub use store::{CellInfo, CellKey};

Expand Down Expand Up @@ -81,16 +79,3 @@ impl ImageCellContract {
ALLOW_READ.load(Ordering::Relaxed)
}
}

impl ImageCellContract {
/// This method is only use in init. It insert the always success script
/// deployed cell.
pub(super) fn save_cells(
&self,
root: H256,
cells: Vec<image_cell_abi::CellInfo>,
created_number: u64,
) -> ProtocolResult<()> {
ImageCellStore::new(root)?.save_cells(cells, created_number)
}
}
22 changes: 0 additions & 22 deletions core/executor/src/system_contract/image_cell/utils.rs

This file was deleted.

6 changes: 0 additions & 6 deletions core/executor/src/system_contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use protocol::types::{Bytes, Hasher, SignedTransaction, TxResp, H160, H256};
use protocol::{ckb_blake2b_256, traits::ExecutorAdapter};

use crate::adapter::RocksTrieDB;
use crate::system_contract::image_cell::utils::always_success_script_deploy_cell;
use crate::system_contract::utils::generate_mpt_root_changes;

pub const fn system_contract_address(addr: u8) -> H160 {
Expand Down Expand Up @@ -120,14 +119,9 @@ pub fn init<P: AsRef<Path>, Adapter: ExecutorAdapter>(
let current_cell_root = adapter.storage(CkbLightClientContract::ADDRESS, *HEADER_CELL_ROOT_KEY);

if current_cell_root.is_zero() {
// todo need refactoring
ImageCellContract::default()
.save_cells(H256::zero(), vec![always_success_script_deploy_cell()], 0)
.unwrap();
let changes = generate_mpt_root_changes(adapter, ImageCellContract::ADDRESS);
adapter.apply(changes, vec![], false);
}

(current_metadata_root, current_cell_root)
}

Expand Down
10 changes: 9 additions & 1 deletion core/interoperation/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ use ckb_types::core::cell::{CellMeta, CellProvider, CellStatus, ResolvedTransact
use ckb_types::core::{DepType, TransactionView};
use ckb_types::{packed, prelude::*};

use protocol::{lazy::DUMMY_INPUT_OUT_POINT, types::CellWithData, ProtocolResult};
use protocol::{
lazy::{always_success_script_meta, DUMMY_INPUT_OUT_POINT},
types::CellWithData,
ProtocolResult,
};

use crate::InteroperationError;

Expand Down Expand Up @@ -36,6 +40,8 @@ pub fn resolve_transaction<CL: CellProvider>(
}
}

let always_success_meta = always_success_script_meta();

for cell_dep in tx.cell_deps_iter() {
if cell_dep.dep_type() == DepType::DepGroup.into() {
let dep_group = resolve_cell(&cell_dep.out_point())?;
Expand All @@ -47,6 +53,8 @@ pub fn resolve_transaction<CL: CellProvider>(
resolved_cell_deps.push(resolve_cell(&sub_out_point)?);
}
resolved_dep_groups.push(dep_group);
} else if cell_dep.out_point() == always_success_meta.out_point {
resolved_cell_deps.push(always_success_meta.clone())
} else {
resolved_cell_deps.push(resolve_cell(&cell_dep.out_point())?);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl PeerStore {
.entry(extract_peer_id(&addr).expect("connected addr should have peer id"))
{
Entry::Occupied(mut entry) => {
let mut peer = entry.get_mut();
let peer = entry.get_mut();
peer.connected_addr = addr;
peer.last_connected_at_ms = now_ms;
peer.session_type = session_type;
Expand Down
30 changes: 29 additions & 1 deletion protocol/src/lazy.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use arc_swap::ArcSwap;
use ckb_always_success_script::ALWAYS_SUCCESS;
use ckb_types::{core::ScriptHashType, packed, prelude::*};
use ckb_types::{bytes::Bytes, core::cell::CellMeta, core::ScriptHashType, packed, prelude::*};

use crate::traits::BYTE_SHANNONS;
use crate::{ckb_blake2b_256, types::Hex};

use std::sync::OnceLock;

static CELL: OnceLock<CellMeta> = OnceLock::new();

lazy_static::lazy_static! {
pub static ref CHAIN_ID: ArcSwap<u64> = ArcSwap::from_pointee(Default::default());
pub static ref PROTOCOL_VERSION: ArcSwap<Hex> = ArcSwap::from_pointee(Default::default());
Expand All @@ -20,3 +25,26 @@ lazy_static::lazy_static! {
.index(0u32.pack())
.build();
}

pub fn always_success_script_meta() -> &'static CellMeta {
CELL.get_or_init(|| {
let capacity = (32 + 8 + 1 + ALWAYS_SUCCESS.len()) as u64 * BYTE_SHANNONS;
let deploy_cell_output = packed::CellOutputBuilder::default()
.capacity(capacity.pack())
.build();
let deploy_cell_out_point = packed::OutPointBuilder::default()
.tx_hash(ALWAYS_SUCCESS_DEPLOY_TX_HASH.pack())
.index(0u32.pack())
.build();
CellMeta {
cell_output: deploy_cell_output,
out_point: deploy_cell_out_point,
transaction_info: None,
data_bytes: ALWAYS_SUCCESS.len() as u64,
mem_cell_data: Some(Bytes::from(ALWAYS_SUCCESS.to_vec())),
mem_cell_data_hash: Some(packed::Byte32::new_unchecked(Bytes::from(
ckb_blake2b_256(ALWAYS_SUCCESS).to_vec(),
))),
}
})
}

0 comments on commit 45da7b8

Please sign in to comment.