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

feat: prepend the binary version to BlockAssemblerConfig message #2817

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions benches/benches/benchmarks/overall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ fn block_assembler_config() -> BlockAssemblerConfig {
hash_type: hash_type.into(),
args,
message: Default::default(),
use_binary_version_as_message_prefix: false,
binary_version: "BENCH".to_string(),
}
}

Expand Down
2 changes: 2 additions & 0 deletions benches/benches/benchmarks/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ fn block_assembler_config() -> BlockAssemblerConfig {
hash_type: hash_type.into(),
args,
message: Default::default(),
use_binary_version_as_message_prefix: false,
binary_version: "BENCH".to_string(),
}
}

Expand Down
28 changes: 27 additions & 1 deletion chain/src/tests/block_assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use ckb_types::{
TransactionBuilder, TransactionView,
},
h256,
packed::{Block, CellInput, CellOutput, CellOutputBuilder, OutPoint},
packed::{Block, CellInput, CellOutput, CellOutputBuilder, CellbaseWitness, OutPoint},
prelude::*,
H256,
};
Expand All @@ -36,6 +36,8 @@ fn start_chain(consensus: Option<Consensus>) -> (ChainController, Shared) {
args: Default::default(),
hash_type: ScriptHashType::Data,
message: Default::default(),
use_binary_version_as_message_prefix: true,
binary_version: "TEST".to_string(),
};
let (shared, mut pack) = builder
.block_assembler_config(Some(config))
Expand Down Expand Up @@ -163,6 +165,30 @@ fn test_block_template_timestamp() {
);
}

#[test]
fn test_block_template_message() {
let (_chain_controller, shared) = start_chain(None);
let tx_pool = shared.tx_pool_controller();

let block_template = tx_pool
.get_block_template(None, None, None)
.unwrap()
.unwrap();

let cellbase_witness = CellbaseWitness::from_slice(
block_template
.cellbase
.data
.witnesses
.get(0)
.unwrap()
.as_bytes(),
)
.expect("should be valid CellbaseWitness slice");

assert_eq!("TEST".as_bytes(), cellbase_witness.message().raw_data());
}

#[test]
fn test_prepare_uncles() {
let mut consensus = Consensus::default();
Expand Down
2 changes: 2 additions & 0 deletions chain/src/tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ pub(crate) fn start_chain(consensus: Option<Consensus>) -> (ChainController, Sha
args: Default::default(),
hash_type: ScriptHashType::Data,
message: Default::default(),
use_binary_version_as_message_prefix: false,
binary_version: "TEST".to_string(),
};

let (shared, mut pack) = builder
Expand Down
2 changes: 2 additions & 0 deletions resource/ckb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,5 @@ block_uncles_cache_size = 30
# # {{
# _ => {block_assembler}
# }}
# # CKB will prepend the binary version to message, to identify the block miner client. (default true, false to disable it)
# use_binary_version_as_message_prefix = true
2 changes: 1 addition & 1 deletion rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1582,7 +1582,7 @@ Response
],
"version": "0x0",
"witnesses": [
"0x590000000c00000055000000490000001000000030000000310000001892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df20114000000b2e61ff569acf041b3c2c17724e2379c581eeac300000000"
"0x650000000c00000055000000490000001000000030000000310000001892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df20114000000b2e61ff569acf041b3c2c17724e2379c581eeac30c00000054455354206d657373616765"
]
},
"hash": "0xbaf7e4db2fd002f19a597ca1a31dfe8cfe26ed8cebc91f52b75b16a7a5ec8bab"
Expand Down
2 changes: 1 addition & 1 deletion rpc/src/module/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub trait MinerRpc {
/// ],
/// "version": "0x0",
/// "witnesses": [
/// "0x590000000c00000055000000490000001000000030000000310000001892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df20114000000b2e61ff569acf041b3c2c17724e2379c581eeac300000000"
/// "0x650000000c00000055000000490000001000000030000000310000001892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df20114000000b2e61ff569acf041b3c2c17724e2379c581eeac30c00000054455354206d657373616765"
/// ]
/// },
/// "hash": "0xbaf7e4db2fd002f19a597ca1a31dfe8cfe26ed8cebc91f52b75b16a7a5ec8bab"
Expand Down
2 changes: 2 additions & 0 deletions rpc/src/module/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ impl IntegrationTestRpc for IntegrationTestRpcImpl {
hash_type: script.hash_type,
args: script.args,
message: block_assembler_message.unwrap_or_default(),
use_binary_version_as_message_prefix: false,
binary_version: "TEST".to_string(),
});
let block_template = tx_pool
.get_block_template_with_block_assembler_config(
Expand Down
4 changes: 3 additions & 1 deletion rpc/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ fn setup_rpc_test_suite(height: u64) -> RpcTestSuite {
code_hash: h256!("0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2"),
hash_type: ckb_jsonrpc_types::ScriptHashType::Type,
args: json_bytes("0xb2e61ff569acf041b3c2c17724e2379c581eeac3"),
message: Default::default(),
message: "message".pack().into(),
use_binary_version_as_message_prefix: true,
binary_version: "TEST".to_string(),
}))
.build()
.unwrap();
Expand Down
2 changes: 2 additions & 0 deletions test/src/specs/mining/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ impl Spec for BootstrapCellbase {
args: JsonBytes::from_bytes(Bytes::from(vec![2, 1])),
hash_type: ScriptHashType::Data.into(),
message: Default::default(),
use_binary_version_as_message_prefix: false,
binary_version: "TEST".to_string(),
});
}
}
2 changes: 2 additions & 0 deletions test/src/specs/tx_pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,7 @@ fn new_block_assembler_config(lock_arg: Bytes, hash_type: ScriptHashType) -> Blo
hash_type: hash_type.into(),
args: JsonBytes::from_bytes(lock_arg),
message: Default::default(),
use_binary_version_as_message_prefix: false,
binary_version: "TEST".to_string(),
}
}
2 changes: 2 additions & 0 deletions test/src/specs/tx_pool/send_multisig_secp_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,7 @@ fn new_block_assembler_config(lock_arg: Bytes, hash_type: ScriptHashType) -> Blo
hash_type: hash_type.into(),
args: JsonBytes::from_bytes(lock_arg),
message: Default::default(),
use_binary_version_as_message_prefix: false,
binary_version: "TEST".to_string(),
}
}
17 changes: 16 additions & 1 deletion tx-pool/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,24 @@ impl TxPoolService {
.code_hash(config.code_hash.pack())
.hash_type(hash_type.into())
.build();
let message = if config.use_binary_version_as_message_prefix {
if config.message.is_empty() {
config.binary_version.as_bytes().pack()
} else {
[
config.binary_version.as_bytes(),
b" ",
config.message.as_bytes(),
]
.concat()
.pack()
}
} else {
config.message.as_bytes().pack()
};
let cellbase_witness = CellbaseWitness::new_builder()
.lock(cellbase_lock)
.message(config.message.as_bytes().pack())
.message(message)
.build();

BlockAssembler::build_cellbase(snapshot, snapshot.tip_header(), cellbase_witness)
Expand Down
11 changes: 0 additions & 11 deletions util/app-config/src/configs/miner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use ckb_jsonrpc_types::{JsonBytes, ScriptHashType};
use ckb_types::H256;
use serde::{Deserialize, Serialize};

/// Miner config options.
Expand Down Expand Up @@ -36,15 +34,6 @@ pub enum WorkerConfig {
EaglesongSimple(EaglesongSimpleConfig),
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct BlockAssemblerConfig {
pub code_hash: H256,
pub args: Vec<JsonBytes>,
#[serde(default)]
pub message: JsonBytes,
pub hash_type: ScriptHashType,
}

/// Dummy worker config options.
///
/// Dummy worker can submit the new block at any time. This controls the pace that how much time
Expand Down
10 changes: 10 additions & 0 deletions util/app-config/src/configs/tx_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,14 @@ pub struct BlockAssemblerConfig {
pub message: JsonBytes,
/// The miner lock script hash type.
pub hash_type: ScriptHashType,
/// Use ckb binary version as message prefix to identify the block miner client (default true, false to disable it).
#[serde(default = "default_use_binary_version_as_message_prefix")]
pub use_binary_version_as_message_prefix: bool,
/// A field to store the block miner client version, non-configurable options.
#[serde(skip)]
pub binary_version: String,
}

const fn default_use_binary_version_as_message_prefix() -> bool {
true
}
5 changes: 4 additions & 1 deletion util/launcher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Launcher {
self.args.config.rpc.miner_enable(),
self.args.config.block_assembler.clone(),
) {
(true, Some(block_assembler)) => {
(true, Some(mut block_assembler)) => {
let check_lock_code_hash = |code_hash| -> Result<bool, ExitCode> {
let secp_cell_data =
Resource::bundled("specs/cells/secp256k1_blake160_sighash_all".to_string())
Expand Down Expand Up @@ -96,6 +96,9 @@ impl Launcher {
&& block_assembler.args.len() == SECP256K1_BLAKE160_SIGHASH_ALL_ARG_LEN
&& check_lock_code_hash(&block_assembler.code_hash.pack())?)
{
if block_assembler.use_binary_version_as_message_prefix {
block_assembler.binary_version = self.version.long();
}
Some(block_assembler)
} else {
info!(
Expand Down