Skip to content

Commit 45da7b8

Browse files
committed
fix: fix always success cell init
1 parent 5bb8e5d commit 45da7b8

6 files changed

Lines changed: 39 additions & 46 deletions

File tree

core/executor/src/system_contract/image_cell/mod.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
mod abi;
22
mod store;
33

4-
pub mod utils;
5-
64
pub use abi::image_cell_abi;
75
pub use store::{CellInfo, CellKey};
86

@@ -81,16 +79,3 @@ impl ImageCellContract {
8179
ALLOW_READ.load(Ordering::Relaxed)
8280
}
8381
}
84-
85-
impl ImageCellContract {
86-
/// This method is only use in init. It insert the always success script
87-
/// deployed cell.
88-
pub(super) fn save_cells(
89-
&self,
90-
root: H256,
91-
cells: Vec<image_cell_abi::CellInfo>,
92-
created_number: u64,
93-
) -> ProtocolResult<()> {
94-
ImageCellStore::new(root)?.save_cells(cells, created_number)
95-
}
96-
}

core/executor/src/system_contract/image_cell/utils.rs

Lines changed: 0 additions & 22 deletions
This file was deleted.

core/executor/src/system_contract/mod.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ use protocol::types::{Bytes, Hasher, SignedTransaction, TxResp, H160, H256};
3131
use protocol::{ckb_blake2b_256, traits::ExecutorAdapter};
3232

3333
use crate::adapter::RocksTrieDB;
34-
use crate::system_contract::image_cell::utils::always_success_script_deploy_cell;
3534
use crate::system_contract::utils::generate_mpt_root_changes;
3635

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

122121
if current_cell_root.is_zero() {
123-
// todo need refactoring
124-
ImageCellContract::default()
125-
.save_cells(H256::zero(), vec![always_success_script_deploy_cell()], 0)
126-
.unwrap();
127122
let changes = generate_mpt_root_changes(adapter, ImageCellContract::ADDRESS);
128123
adapter.apply(changes, vec![], false);
129124
}
130-
131125
(current_metadata_root, current_cell_root)
132126
}
133127

core/interoperation/src/utils.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ use ckb_types::core::cell::{CellMeta, CellProvider, CellStatus, ResolvedTransact
22
use ckb_types::core::{DepType, TransactionView};
33
use ckb_types::{packed, prelude::*};
44

5-
use protocol::{lazy::DUMMY_INPUT_OUT_POINT, types::CellWithData, ProtocolResult};
5+
use protocol::{
6+
lazy::{always_success_script_meta, DUMMY_INPUT_OUT_POINT},
7+
types::CellWithData,
8+
ProtocolResult,
9+
};
610

711
use crate::InteroperationError;
812

@@ -36,6 +40,8 @@ pub fn resolve_transaction<CL: CellProvider>(
3640
}
3741
}
3842

43+
let always_success_meta = always_success_script_meta();
44+
3945
for cell_dep in tx.cell_deps_iter() {
4046
if cell_dep.dep_type() == DepType::DepGroup.into() {
4147
let dep_group = resolve_cell(&cell_dep.out_point())?;
@@ -47,6 +53,8 @@ pub fn resolve_transaction<CL: CellProvider>(
4753
resolved_cell_deps.push(resolve_cell(&sub_out_point)?);
4854
}
4955
resolved_dep_groups.push(dep_group);
56+
} else if cell_dep.out_point() == always_success_meta.out_point {
57+
resolved_cell_deps.push(always_success_meta.clone())
5058
} else {
5159
resolved_cell_deps.push(resolve_cell(&cell_dep.out_point())?);
5260
}

core/network/src/peer_manager/peer_store/peer_store_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl PeerStore {
5353
.entry(extract_peer_id(&addr).expect("connected addr should have peer id"))
5454
{
5555
Entry::Occupied(mut entry) => {
56-
let mut peer = entry.get_mut();
56+
let peer = entry.get_mut();
5757
peer.connected_addr = addr;
5858
peer.last_connected_at_ms = now_ms;
5959
peer.session_type = session_type;

protocol/src/lazy.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
use arc_swap::ArcSwap;
22
use ckb_always_success_script::ALWAYS_SUCCESS;
3-
use ckb_types::{core::ScriptHashType, packed, prelude::*};
3+
use ckb_types::{bytes::Bytes, core::cell::CellMeta, core::ScriptHashType, packed, prelude::*};
44

5+
use crate::traits::BYTE_SHANNONS;
56
use crate::{ckb_blake2b_256, types::Hex};
67

8+
use std::sync::OnceLock;
9+
10+
static CELL: OnceLock<CellMeta> = OnceLock::new();
11+
712
lazy_static::lazy_static! {
813
pub static ref CHAIN_ID: ArcSwap<u64> = ArcSwap::from_pointee(Default::default());
914
pub static ref PROTOCOL_VERSION: ArcSwap<Hex> = ArcSwap::from_pointee(Default::default());
@@ -20,3 +25,26 @@ lazy_static::lazy_static! {
2025
.index(0u32.pack())
2126
.build();
2227
}
28+
29+
pub fn always_success_script_meta() -> &'static CellMeta {
30+
CELL.get_or_init(|| {
31+
let capacity = (32 + 8 + 1 + ALWAYS_SUCCESS.len()) as u64 * BYTE_SHANNONS;
32+
let deploy_cell_output = packed::CellOutputBuilder::default()
33+
.capacity(capacity.pack())
34+
.build();
35+
let deploy_cell_out_point = packed::OutPointBuilder::default()
36+
.tx_hash(ALWAYS_SUCCESS_DEPLOY_TX_HASH.pack())
37+
.index(0u32.pack())
38+
.build();
39+
CellMeta {
40+
cell_output: deploy_cell_output,
41+
out_point: deploy_cell_out_point,
42+
transaction_info: None,
43+
data_bytes: ALWAYS_SUCCESS.len() as u64,
44+
mem_cell_data: Some(Bytes::from(ALWAYS_SUCCESS.to_vec())),
45+
mem_cell_data_hash: Some(packed::Byte32::new_unchecked(Bytes::from(
46+
ckb_blake2b_256(ALWAYS_SUCCESS).to_vec(),
47+
))),
48+
}
49+
})
50+
}

0 commit comments

Comments
 (0)