Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ckb 2023 edition #3917

Merged
merged 31 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
79b1921
Add script version 2
mohanson Nov 8, 2022
4f53393
Re-generate rpc/README.md
mohanson Mar 28, 2023
7c8ceb7
Fix clippy
mohanson Mar 28, 2023
7747fdb
jsonrpc-types(tests): fix test_script_serialization
mohanson Mar 28, 2023
259dc88
utils(tests): fix core::tests::blockchain::test_script_hash_type
mohanson Mar 28, 2023
09fdf16
util(tests): fix extension::tests::check_data::check_data
mohanson Mar 28, 2023
73650db
feat: prepare configuration for 2023
zhangsoledad Mar 2, 2023
2494423
feat: hardfork 2023 network
driftluo Apr 10, 2023
cf4dd8c
feat: ckb2023 config init
zhangsoledad Apr 23, 2023
a41ba71
feat: vm selection for 2023
zhangsoledad Apr 24, 2023
60b6e2f
feat: remove block version reservation
zhangsoledad Apr 25, 2023
79a394a
Use new spawn API
mohanson Apr 25, 2023
76fe7e1
feat: delay txs during hardfork
zhangsoledad Apr 27, 2023
0e93a55
feat: impl load_extension syscall
zhangsoledad May 6, 2023
1f15b9f
feat: enable ckb2023 on dev by default
zhangsoledad May 9, 2023
f6466c3
sync: add RelayV3 to `fn protocol_name`
eval-exec May 11, 2023
678b112
docs: fix ScriptVersion comment
eval-exec May 11, 2023
41e91bb
docs: fix ckb-c-stdlib version in `script/testdata/README.md`
eval-exec May 11, 2023
247a7ca
feat: fuzz scripts verifier data2
zhangsoledad May 16, 2023
d193b34
Merge pull request #3984 from zhangsoledad/zhangsoledad/fuzz
zhangsoledad May 16, 2023
81b8010
Merge branch 'develop' into ckb2023
zhangsoledad May 16, 2023
ce5b8bd
ci: ckb_replay more blocks
eval-exec May 16, 2023
04e7507
Update ckb-vm to v0.24.0
mohanson May 18, 2023
d6571e6
Merge pull request #3988 from mohanson/update_ckb_vm
zhangsoledad May 18, 2023
147ee71
chore: remove unnecessary clone in spawn syscall
quake May 22, 2023
879208f
Merge pull request #3989 from quake/quake/refactor-spawn-syscall
zhangsoledad May 22, 2023
0592011
chore: clear comments
zhangsoledad May 17, 2023
0fbcc68
scripts(spawn): deduct cycls used to build the machine
mohanson May 23, 2023
249c402
Merge pull request #3990 from mohanson/spawn_cycles
zhangsoledad May 23, 2023
154bdfe
script(spawn): typos
mohanson May 23, 2023
c050140
Merge pull request #3991 from mohanson/typos
quake May 23, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 41 additions & 1 deletion chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use ckb_types::{
resolve_transaction, BlockCellProvider, HeaderChecker, OverlayCellProvider,
ResolvedTransaction,
},
hardfork::HardForks,
service::{Request, DEFAULT_CHANNEL_SIZE, SIGNAL_CHANNEL_SIZE},
BlockExt, BlockNumber, BlockView, Cycle, HeaderView,
},
Expand Down Expand Up @@ -173,6 +174,35 @@ impl ForkChanges {
blk.header().number()
})
}

pub fn during_hardfork(&self, hardfork_switch: &HardForks) -> bool {
let hardfork_during_detach =
self.check_if_hardfork_during_blocks(hardfork_switch, &self.detached_blocks);
let hardfork_during_attach =
self.check_if_hardfork_during_blocks(hardfork_switch, &self.attached_blocks);

hardfork_during_detach || hardfork_during_attach
}

fn check_if_hardfork_during_blocks(
&self,
hardfork: &HardForks,
blocks: &VecDeque<BlockView>,
) -> bool {
if blocks.is_empty() {
false
} else {
// This method assumes that the input blocks are sorted and unique.
let rfc_0148 = hardfork.ckb2023.rfc_0148();
let epoch_first = blocks.front().unwrap().epoch().number();
let epoch_next = blocks
.back()
.unwrap()
.epoch()
.minimum_epoch_number_after_n_blocks(1);
epoch_first < rfc_0148 && rfc_0148 <= epoch_next
}
}
}

pub(crate) struct GlobalIndex {
Expand Down Expand Up @@ -729,9 +759,19 @@ impl ChainService {
}

let txs_verify_cache = self.shared.txs_verify_cache();
let consensus = self.shared.cloned_consensus();

let consensus = self.shared.consensus();
let hardfork_switch = consensus.hardfork_switch();
let during_hardfork = fork.during_hardfork(hardfork_switch);
let async_handle = self.shared.tx_pool_controller().handle();

if during_hardfork {
async_handle.block_on(async {
txs_verify_cache.write().await.clear();
});
}

let consensus = self.shared.cloned_consensus();
let start_block_header = fork.attached_blocks()[0].header();
let mmr_size = leaf_index_to_mmr_size(start_block_header.number() - 1);
trace!("light-client: new chain root MMR with size = {}", mmr_size);
Expand Down
19 changes: 12 additions & 7 deletions chain/src/tests/load_code_with_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use ckb_types::prelude::*;
use ckb_types::{
bytes::Bytes,
core::{
capacity_bytes, hardfork::HardForkSwitch, BlockBuilder, Capacity, EpochNumberWithFraction,
ScriptHashType, TransactionBuilder, TransactionView,
capacity_bytes,
hardfork::{HardForks, CKB2021, CKB2023},
BlockBuilder, Capacity, EpochNumberWithFraction, ScriptHashType, TransactionBuilder,
TransactionView,
},
packed::{self, CellDep, CellInput, CellOutputBuilder, OutPoint, Script},
utilities::DIFF_TWO,
Expand Down Expand Up @@ -235,11 +237,14 @@ fn _test_load_code_with_snapshot_after_hardfork(script_type: ScriptHashType) {
.dao(dao)
.build();

let hardfork_switch = HardForkSwitch::new_mirana()
.as_builder()
.rfc_0032(0)
.build()
.unwrap();
let hardfork_switch = HardForks {
ckb2021: CKB2021::new_mirana()
.as_builder()
.rfc_0032(0)
.build()
.unwrap(),
ckb2023: CKB2023::new_mirana().as_builder().build().unwrap(),
};
let consensus = ConsensusBuilder::default()
.cellbase_maturity(EpochNumberWithFraction::new(0, 0, 1))
.genesis_block(genesis_block)
Expand Down
4 changes: 2 additions & 2 deletions docs/hashes.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ tx_hash = "0xf8de3bb47d055cdf460d93a2a6e1b05f7432f9777c8c474abf4eec1d4aee5d37"
index = 1

[ckb_staging]
spec_hash = "0x1e501d0048dbfee37707432fb5ec4c5e2b0404d887ebc882564c8761ef54e68a"
spec_hash = "0x7a0ed50e9723e6c41a5c8450225e9f0a639070bb81b87e395fd5f91eef6ce373"
genesis = "0xbc081e6b2e31149c1dc39007f161ed0a0b63d5d30b3b771acc6a3b622133fcc0"
cellbase = "0x7295631c414e50a8d9bb73d9845231ac212d10404045c96de7f149ec874ac6b7"

Expand Down Expand Up @@ -127,7 +127,7 @@ tx_hash = "0xd5780747735fd22c9ba7363bde8afe59061658caa836962867253b03cbda264e"
index = 1

[ckb_dev]
spec_hash = "0x307bfa518f3dd6ffe6d82317c5ce7eb738db4623402edcc06b5adbee2e3a6b29"
spec_hash = "0xdd75ca95ff59da78879506a46f509adf4e8d131f1826827be708408e60ebde37"
genesis = "0x823b2ff5785b12da8b1363cac9a5cbe566d8b715a4311441b119c39a0367488c"
cellbase = "0xa563884b3686078ec7e7677a5f86449b15cf2693f3c1241766c6996f206cc541"

Expand Down
19 changes: 19 additions & 0 deletions network/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ pub struct NetworkState {
/// fields: ProtocolId, Protocol Name, Supported Versions
pub(crate) protocols: RwLock<Vec<(ProtocolId, String, Vec<String>)>>,
pub(crate) required_flags: Flags,

pub(crate) ckb2023: AtomicBool,
}

impl NetworkState {
Expand Down Expand Up @@ -137,9 +139,16 @@ impl NetworkState {
active: AtomicBool::new(true),
protocols: RwLock::new(Vec::new()),
required_flags: Flags::SYNC | Flags::DISCOVERY | Flags::RELAY,
ckb2023: AtomicBool::new(false),
})
}

/// fork flag
pub fn ckb2023(self, init: bool) -> Self {
self.ckb2023.store(init, Ordering::SeqCst);
self
}

/// use to discovery get nodes message to announce what kind of node information need from the other peer
/// default with `Flags::SYNC | Flags::DISCOVERY | Flags::RELAY`
pub fn required_flags(mut self, flags: Flags) -> Self {
Expand Down Expand Up @@ -1175,6 +1184,16 @@ pub struct NetworkController {
}

impl NetworkController {
/// Set ckb2023 start
pub fn init_ckb2023(&self) {
self.network_state.ckb2023.store(true, Ordering::SeqCst);
}

/// Get ckb2023 flag
pub fn load_ckb2023(&self) -> bool {
self.network_state.ckb2023.load(Ordering::SeqCst)
}

/// Node listen address list
pub fn public_urls(&self, max_urls: usize) -> Vec<(String, u8)> {
self.network_state.public_urls(max_urls)
Expand Down
12 changes: 9 additions & 3 deletions network/src/protocols/feeler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use p2p::{
context::{ProtocolContext, ProtocolContextMutRef},
traits::ServiceProtocol,
};
use std::sync::Arc;
use std::sync::{atomic::Ordering, Arc};

/// Feeler
/// Currently do nothing, CKBProtocol auto refresh peer_store after connected.
Expand All @@ -24,9 +24,15 @@ impl Feeler {
impl ServiceProtocol for Feeler {
async fn init(&mut self, _context: &mut ProtocolContext) {}

async fn connected(&mut self, context: ProtocolContextMutRef<'_>, _version: &str) {
async fn connected(&mut self, context: ProtocolContextMutRef<'_>, version: &str) {
let session = context.session;
if context.session.ty.is_outbound() {
if self.network_state.ckb2023.load(Ordering::SeqCst) && version != "3" {
self.network_state
.peer_store
.lock()
.mut_addr_manager()
.remove(&session.address);
} else if context.session.ty.is_outbound() {
let flags = self.network_state.with_peer_registry(|reg| {
if let Some(p) = reg.get_peer(session.id) {
p.identify_info
Expand Down
54 changes: 48 additions & 6 deletions network/src/protocols/identify/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::{atomic::Ordering, Arc};
use std::time::{Duration, Instant};

use ckb_logger::{debug, error, trace, warn};
Expand Down Expand Up @@ -370,7 +370,18 @@ impl Callback for IdentifyCallback {
}

fn unregister(&self, context: &ProtocolContextMutRef) {
if context.session.ty.is_outbound() {
let protocol_version_match = self
.network_state
.with_peer_registry(|reg| {
reg.get_peer(context.session.id)
.map(|p| p.protocol_version(context.proto_id))
})
.flatten()
.map(|version| version != "3")
.unwrap_or_default();

if self.network_state.ckb2023.load(Ordering::SeqCst) && protocol_version_match {
} else if context.session.ty.is_outbound() {
// Due to the filtering strategy of the peer store, if the node is
// disconnected after a long connection is maintained for more than seven days,
// it is possible that the node will be accidentally evicted, so it is necessary
Expand Down Expand Up @@ -416,14 +427,40 @@ impl Callback for IdentifyCallback {

let required_flags = self.network_state.required_flags;

let protocol_version_match = self
.network_state
.with_peer_registry(|reg| {
reg.get_peer(context.session.id)
.map(|p| p.protocol_version(context.proto_id))
})
.flatten()
.map(|version| version != "3")
.unwrap_or_default();
let ckb2023 = self.network_state.ckb2023.load(Ordering::SeqCst);

let renew = if ckb2023 && protocol_version_match {
if context.session.ty.is_outbound() {
self.network_state
.peer_store
.lock()
.mut_addr_manager()
.remove(&context.session.address);
}
false
} else {
true
};

if context.session.ty.is_outbound() {
// why don't set inbound here?
// because inbound address can't feeler during staying connected
// and if set it to peer store, it will be broadcast to the entire network,
// but this is an unverified address
self.network_state.with_peer_store_mut(|peer_store| {
peer_store.add_outbound_addr(context.session.address.clone(), flags);
});
if renew {
self.network_state.with_peer_store_mut(|peer_store| {
peer_store.add_outbound_addr(context.session.address.clone(), flags);
});
}

if self
.network_state
Expand All @@ -441,7 +478,12 @@ impl Callback for IdentifyCallback {
.open_protocols(
context.session.id,
TargetProtocol::Filter(Box::new(move |id| {
id != &SupportProtocols::Feeler.protocol_id()
if ckb2023 {
id != &SupportProtocols::Feeler.protocol_id()
&& id != &SupportProtocols::RelayV2.protocol_id()
} else {
id != &SupportProtocols::Feeler.protocol_id()
}
})),
)
.await;
Expand Down
25 changes: 24 additions & 1 deletion network/src/protocols/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ pub type BoxedFutureTask = Pin<Box<dyn Future<Output = ()> + 'static + Send>>;
use crate::{
compress::{compress, decompress},
network::{async_disconnect_with_message, disconnect_with_message},
Behaviour, Error, NetworkState, Peer, ProtocolVersion,
Behaviour, Error, NetworkState, Peer, ProtocolVersion, SupportProtocols,
};

/// Abstract protocol context
#[async_trait]
pub trait CKBProtocolContext: Send {
/// Get ckb2023 flag
fn ckb2023(&self) -> bool;
/// Set notify to tentacle
// Interact with underlying p2p service
async fn set_notify(&self, interval: Duration, token: u64) -> Result<(), Error>;
Expand Down Expand Up @@ -283,6 +285,22 @@ impl ServiceProtocol for CKBHandler {
}

async fn connected(&mut self, context: ProtocolContextMutRef<'_>, version: &str) {
// This judgment will be removed in the first release after 2023 hardfork
if self
.network_state
.ckb2023
.load(std::sync::atomic::Ordering::SeqCst)
&& version != "3"
&& context.proto_id != SupportProtocols::RelayV2.protocol_id()
{
debug!(
"session {}, protocol {} with version {}, not 3, so disconnect it",
context.session.id, context.proto_id, version
);
let id = context.session.id;
let _ignore = context.disconnect(id);
return;
}
self.network_state.with_peer_registry_mut(|reg| {
if let Some(peer) = reg.get_peer_mut(context.session.id) {
peer.protocols.insert(self.proto_id, version.to_owned());
Expand Down Expand Up @@ -381,6 +399,11 @@ struct DefaultCKBProtocolContext {

#[async_trait]
impl CKBProtocolContext for DefaultCKBProtocolContext {
fn ckb2023(&self) -> bool {
self.network_state
.ckb2023
.load(std::sync::atomic::Ordering::SeqCst)
}
async fn set_notify(&self, interval: Duration, token: u64) -> Result<(), Error> {
self.async_p2p_control
.set_service_notify(self.proto_id, interval, token)
Expand Down
Loading