From abf6b5bbc27766242a9f0b6ec2b3554e94b7ffde Mon Sep 17 00:00:00 2001 From: Xuejie Xiao Date: Fri, 21 Dec 2018 12:46:03 +0800 Subject: [PATCH 01/59] refactor: Remove obsolete secp256k1 script in CKB --- devtools/playground/random_transaction.rb | 109 -------------- nodes_template/default.json | 2 +- nodes_template/spec/dev.json | 1 - script/src/verify.rs | 2 +- .../spec/cells => script/testdata}/verify | Bin script/testdata/verify.c | 135 ++++++++++++++++++ src/cli/args.rs | 27 +--- src/cli/mod.rs | 2 +- src/cli/run_impl.rs | 86 +---------- src/main.rs | 3 +- 10 files changed, 146 insertions(+), 221 deletions(-) delete mode 100755 devtools/playground/random_transaction.rb rename {nodes_template/spec/cells => script/testdata}/verify (100%) create mode 100644 script/testdata/verify.c diff --git a/devtools/playground/random_transaction.rb b/devtools/playground/random_transaction.rb deleted file mode 100755 index 94abc7c579..0000000000 --- a/devtools/playground/random_transaction.rb +++ /dev/null @@ -1,109 +0,0 @@ -#!/usr/bin/env ruby -require 'json' -require 'net/http' -require 'uri' - -URL = ARGV[0] || "http://localhost:8114" -CKB_BIN = ARGV[1] || "./target/debug/ckb" -ACCOUNTS = [ - { - name: "miner", - type_hash: "321c1ca2887fb8eddaaa7e917399f71e63e03a1c83ff75ed12099a01115ea2ff", - private_key: "e79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3", - utxo: [] - }, - { - name: "alice", - type_hash: "67295822d1f852602a7d398edfe9ad42734cf41999c084ea8a401ff7cc994cfa", - private_key: "76e853efa8245389e33f6fe49dcbd359eb56be2f6c3594e12521d2a806d32156", - utxo: [] - }, - { - name: "bob", - type_hash: "45996d79e9403f99eb2d79c4f649d45be4b5ab8458e359f5421ff1ab4e88e9f4", - private_key: "9f7fd78dffeda83b77c5c2d7eeaccb05120457787defdbb46da6d2186bf28f13", - utxo: [] - } -] - -class Fixnum - def random_split(set = nil, repeats = false) - set ||= 1..self - set = [*set] - return if set.empty? || set.min > self || set.inject(0, :+) < self - tried_numbers = [] - while (not_tried = (set - tried_numbers).select {|n| n <= self }).any? - tried_numbers << number = not_tried.sample - return [number] if number == self - new_set = set.dup - new_set.delete_at(new_set.index(number)) unless repeats - randomized_rest = (self-number).random_split(new_set, repeats) - return [number] + randomized_rest if randomized_rest - end - end -end - -def pull_transactions - number = rpc('get_tip_header')[:raw][:number] - block_hash = rpc('get_block_hash', "[#{number}]") - block = rpc('get_block', "[\"#{block_hash}\"]") - block[:transactions].each do |tx| - tx[:transaction][:outputs].each_with_index do |output, i| - if match = ACCOUNTS.find{|account| output[:lock] == "0x#{account[:type_hash]}" } - match[:utxo] << {hash: tx[:hash], index: i, capacity: output[:capacity]} - end - end - end -end - -def send_transactions - account = ACCOUNTS.sample - if account[:utxo].size > 0 - utxo = account[:utxo].sample(account[:utxo].size / 2 + 1) - total = utxo.inject(0){|s, o| s + o[:capacity]} - inputs = utxo.map do |o| - account[:utxo].delete(o) - { - previous_output: { - hash: o[:hash], - index: o[:index] - }, - unlock: { - version: 0, - args: [account[:name].bytes.to_a], - signed_args: [] - } - } - end - outputs = total.random_split(100..10000).map do |capacity| - { - capacity: capacity, - data: [0], - lock: "0x#{ACCOUNTS.sample[:type_hash]}" - } - end - transaction = { - version: 0, - deps: [], - inputs: inputs, - outputs: outputs, - } - signed = `#{CKB_BIN} cli sign -p #{account[:private_key]} -u '#{transaction.to_json}'` - rpc('send_transaction', "[#{signed}]") - end -end - -def rpc(method, params = "null") - puts "rpc method: #{method}, params: #{params}" - response = Net::HTTP.post( - URI(URL), - "{\"id\": 1, \"jsonrpc\": \"2.0\", \"method\": \"#{method}\", \"params\": #{params}}", - "Content-Type" => "application/json") - JSON.parse(response.body, symbolize_names: true)[:result] -end - -10.times do - pull_transactions - send_transactions - sleep(5) -end diff --git a/nodes_template/default.json b/nodes_template/default.json index b1e6527d10..24fe527932 100644 --- a/nodes_template/default.json +++ b/nodes_template/default.json @@ -34,7 +34,7 @@ }, "miner": { "new_transactions_threshold": 8, - "type_hash": "0x321c1ca2887fb8eddaaa7e917399f71e63e03a1c83ff75ed12099a01115ea2ff", + "type_hash": "0x0da2fe99fe549e082d4ed483c2e968a89ea8d11aabf5d79e5cbf06522de6e674", "rpc_url": "http://127.0.0.1:8114/", "poll_interval": 5, "max_transactions": 10000, diff --git a/nodes_template/spec/dev.json b/nodes_template/spec/dev.json index 66253ac45b..aa848a97cc 100644 --- a/nodes_template/spec/dev.json +++ b/nodes_template/spec/dev.json @@ -35,7 +35,6 @@ "initial_block_reward": 50000 }, "system_cells": [ - {"path": "cells/verify"}, {"path": "cells/always_success"} ], "pow": { diff --git a/script/src/verify.rs b/script/src/verify.rs index 873bf5c367..a04404f151 100644 --- a/script/src/verify.rs +++ b/script/src/verify.rs @@ -204,7 +204,7 @@ mod tests { fn open_cell_verify() -> File { File::open( - Path::new(env!("CARGO_MANIFEST_DIR")).join("../nodes_template/spec/cells/verify"), + Path::new(env!("CARGO_MANIFEST_DIR")).join("../script/testdata/verify"), ) .unwrap() } diff --git a/nodes_template/spec/cells/verify b/script/testdata/verify similarity index 100% rename from nodes_template/spec/cells/verify rename to script/testdata/verify diff --git a/script/testdata/verify.c b/script/testdata/verify.c new file mode 100644 index 0000000000..176ef8dd66 --- /dev/null +++ b/script/testdata/verify.c @@ -0,0 +1,135 @@ +#include +#include "sha3.h" + +#define SHA3_BLOCK_SIZE 32 + +#define CUSTOM_ABORT 1 +#define CUSTOM_PRINT_ERR 1 + +#include "syscall.h" +void custom_abort() +{ + syscall_errno(93, 10, 0, 0, 0, 0, 0); +} + +int custom_print_err(const char * arg, ...) +{ + (void) arg; + return 0; +} + +#include +/* + * We are including secp256k1 implementation directly so gcc can strip + * unused functions. For some unknown reasons, if we link in libsecp256k1.a + * directly, the final binary will include all functions rather than those used. + */ +#include + +int char_to_int(char ch) +{ + if (ch >= '0' && ch <= '9') { + return ch - '0'; + } + if (ch >= 'a' && ch <= 'f') { + return ch - 'a' + 10; + } + return -1; +} + +int hex_to_bin(char* buf, size_t buf_len, const char* hex) +{ + int i = 0; + + for (; i < buf_len && hex[i * 2] != '\0' && hex[i * 2 + 1] != '\0'; i++) { + int a = char_to_int(hex[i * 2]); + int b = char_to_int(hex[i * 2 + 1]); + + if (a < 0 || b < 0) { + return -1; + } + + buf[i] = ((a & 0xF) << 4) | (b & 0xF); + } + + if (i == buf_len && hex[i * 2] != '\0') { + return -1; + } + return i; +} + +#define CHECK_LEN(x) if ((x) <= 0) { return x; } + +/* + * Arguments are listed in the following order: + * 0. Program name, ignored here, only preserved for compatibility reason + * 1. Pubkey in hex format, a maximum of 130 bytes will be processed + * 2. Signature in hex format, a maximum of 512 bytes will be processed + * 3. Current script hash in hex format, which is 128 bytes. While this program + * cannot verify the hash directly, this ensures the script is include in + * signature calculation + * 4. Other additional parameters that might be included. Notice only ASCII + * characters are included, so binary should be passed as binary format. + * + * This program will run double sha256 on all arguments excluding pubkey and + * signature(also for simplicity, we are running sha256 on ASCII chars directly, + * not deserialized raw bytes), then it will use sha256 result calculated as the + * message to verify the signature. It returns 0 if the signature works, and + * a non-zero value otherwise. + * + * Note all hex values passed in as arguments must have lower case letters for + * deterministic behavior. + */ +int main(int argc, char* argv[]) +{ + char buf[256]; + int len; + + if (argc < 4) { + return -1; + } + + secp256k1_context context; + int ret = secp256k1_context_initialize(&context, SECP256K1_CONTEXT_VERIFY); + if (ret == 0) { + return 4; + } + + len = hex_to_bin(buf, 65, argv[1]); + CHECK_LEN(len); + secp256k1_pubkey pubkey; + + ret = secp256k1_ec_pubkey_parse(&context, &pubkey, buf, len); + if (ret == 0) { + return 1; + } + + len = hex_to_bin(buf, 256, argv[2]); + CHECK_LEN(len); + secp256k1_ecdsa_signature signature; + secp256k1_ecdsa_signature_parse_der(&context, &signature, buf, len); + if (ret == 0) { + return 3; + } + + sha3_ctx_t sha3_ctx; + unsigned char hash[SHA3_BLOCK_SIZE]; + sha3_init(&sha3_ctx, SHA3_BLOCK_SIZE); + for (int i = 3; i < argc; i++) { + sha3_update(&sha3_ctx, argv[i], strlen(argv[i])); + } + sha3_final(hash, &sha3_ctx); + + sha3_init(&sha3_ctx, SHA3_BLOCK_SIZE); + sha3_update(&sha3_ctx, hash, SHA3_BLOCK_SIZE); + sha3_final(hash, &sha3_ctx); + + ret = secp256k1_ecdsa_verify(&context, &signature, hash, &pubkey); + if (ret == 1) { + ret = 0; + } else { + ret = 2; + } + + return ret; +} diff --git a/src/cli/args.rs b/src/cli/args.rs index de8abbf94c..8aecfe73c7 100644 --- a/src/cli/args.rs +++ b/src/cli/args.rs @@ -75,38 +75,13 @@ fn import() -> App<'static, 'static> { ) } -fn arg_private_key() -> Arg<'static, 'static> { - Arg::with_name("private-key") - .short("p") - .long("private-key") - .value_name("H256") - .help("Specify the private key") - .takes_value(true) - .required(true) -} - fn cli() -> App<'static, 'static> { SubCommand::with_name("cli") .about("Running ckb cli") .setting(AppSettings::ArgRequiredElseHelp) - .subcommand( - SubCommand::with_name("sign") - .about("Sign transaction using sha3-secp256k1 defined in system cell") - .arg(arg_private_key()) - .arg( - Arg::with_name("unsigned-transaction") - .short("u") - .long("unsigned-transaction") - .value_name("JSON") - .help("Specify the unsigned transaction json string") - .takes_value(true) - .required(true), - ), - ) .subcommand( SubCommand::with_name("type_hash") - .about("Generate script type hash using sha3-secp256k1 defined in system cell") - .arg(arg_private_key()), + .about("Generate lock script type hash using the first system cell, which by default is always_success"), ) .subcommand(SubCommand::with_name("keygen").about("Generate new key")) } diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 2733475bbf..1a5b24f14e 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -8,4 +8,4 @@ pub use self::args::get_matches; pub use self::export::export; pub use self::import::import; pub use self::miner::miner; -pub use self::run_impl::{keygen, run, sign, type_hash}; +pub use self::run_impl::{keygen, run, type_hash}; diff --git a/src/cli/run_impl.rs b/src/cli/run_impl.rs index 6ab1316cc1..5e5765190e 100644 --- a/src/cli/run_impl.rs +++ b/src/cli/run_impl.rs @@ -2,7 +2,6 @@ use crate::helper::wait_for_exit; use crate::Setup; use ckb_chain::chain::{ChainBuilder, ChainController}; use ckb_core::script::Script; -use ckb_core::transaction::{CellInput, OutPoint, Transaction, TransactionBuilder}; use ckb_db::diskdb::RocksDB; use ckb_miner::{Agent, AgentController}; use ckb_network::CKBProtocol; @@ -17,14 +16,11 @@ use ckb_shared::index::ChainIndex; use ckb_shared::shared::{ChainProvider, Shared, SharedBuilder}; use ckb_shared::store::ChainKVStore; use ckb_sync::{Relayer, Synchronizer, RELAY_PROTOCOL_ID, SYNC_PROTOCOL_ID}; -use clap::{value_t, ArgMatches}; -use crypto::secp::{Generator, Privkey}; -use faster_hex::{hex_string, hex_to}; -use hash::sha3_256; +use crypto::secp::Generator; +use faster_hex::hex_string; use log::info; use numext_fixed_hash::H256; use serde_json; -use std::io::Write; use std::sync::Arc; use std::thread; @@ -152,89 +148,19 @@ fn setup_rpc( }); } -pub fn sign(setup: &Setup, matches: &ArgMatches) { +pub fn type_hash(setup: &Setup) { let consensus = setup.chain_spec.to_consensus().unwrap(); let system_cell_tx = &consensus.genesis_block().commit_transactions()[0]; let system_cell_data_hash = system_cell_tx.outputs()[0].data_hash(); - let system_cell_tx_hash = system_cell_tx.hash(); - let system_cell_out_point = OutPoint::new(system_cell_tx_hash.clone(), 0); - - let privkey: Privkey = value_t!(matches.value_of("private-key"), H256) - .unwrap_or_else(|e| e.exit()) - .into(); - let pubkey = privkey.pubkey().unwrap(); - let json = - value_t!(matches.value_of("unsigned-transaction"), String).unwrap_or_else(|e| e.exit()); - let transaction: Transaction = serde_json::from_str(&json).unwrap(); - let mut inputs = Vec::new(); - for unsigned_input in transaction.inputs() { - let mut bytes = vec![]; - for argument in &unsigned_input.unlock.args { - bytes.write_all(argument).unwrap(); - } - let hash1 = sha3_256(&bytes); - let hash2 = sha3_256(hash1); - let signature = privkey.sign_recoverable(&hash2.into()).unwrap(); - let signature_der = signature.serialize_der(); - let mut hex_signature = vec![0; signature_der.len() * 2]; - hex_to(&signature_der, &mut hex_signature).expect("hex signature"); - - let mut new_args = vec![hex_signature]; - new_args.extend_from_slice(&unsigned_input.unlock.args); - - let pubkey_ser = pubkey.serialize(); - let mut hex_pubkey = vec![0; pubkey_ser.len() * 2]; - hex_to(&pubkey_ser, &mut hex_pubkey).expect("hex pubkey"); - let script = Script::new( - 0, - new_args, - Some(system_cell_data_hash.clone()), - None, - vec![hex_pubkey], - ); - let signed_input = CellInput::new(unsigned_input.previous_output.clone(), script); - inputs.push(signed_input); - } - // First, add verify system cell as a dep - // Then, sign each input - let result = TransactionBuilder::default() - .transaction(transaction) - .dep(system_cell_out_point) - .inputs_clear() - .inputs(inputs) - .build(); - println!("{}", serde_json::to_string(&result).unwrap()); -} - -pub fn type_hash(setup: &Setup, matches: &ArgMatches) { - let consensus = setup.chain_spec.to_consensus().unwrap(); - let system_cell_tx = &consensus.genesis_block().commit_transactions()[0]; - let system_cell_data_hash = system_cell_tx.outputs()[0].data_hash(); - - let privkey: Privkey = value_t!(matches.value_of("private-key"), H256) - .unwrap_or_else(|e| e.exit()) - .into(); - let pubkey = privkey.pubkey().unwrap(); - - let pubkey_ser = pubkey.serialize(); - let mut hex_pubkey = vec![0; pubkey_ser.len() * 2]; - hex_to(&pubkey_ser, &mut hex_pubkey).expect("hex pubkey"); - - let script = Script::new( - 0, - Vec::new(), - Some(system_cell_data_hash), - None, - vec![hex_pubkey], - ); + let script = Script::new(0, vec![], Some(system_cell_data_hash), None, vec![]); println!( - "{}", + "0x{}", hex_string(script.type_hash().as_bytes()).expect("hex string") ); } pub fn keygen() { let result: H256 = Generator::new().random_privkey().into(); - println!("{:?}", result) + println!("0x{}", hex_string(result.as_bytes()).expect("hex string")); } diff --git a/src/main.rs b/src/main.rs index 09f6c8f911..7102ee554b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,8 +28,7 @@ fn main() { match matches.subcommand() { ("cli", Some(cli_matches)) => match cli_matches.subcommand() { - ("sign", Some(sign_matches)) => cli::sign(&setup, sign_matches), - ("type_hash", Some(type_hash_matches)) => cli::type_hash(&setup, type_hash_matches), + ("type_hash", _) => cli::type_hash(&setup), ("keygen", _) => cli::keygen(), _ => unreachable!(), }, From be683a8807df9cddb814119d966a270ac66096f4 Mon Sep 17 00:00:00 2001 From: Xuejie Xiao Date: Fri, 21 Dec 2018 14:45:37 +0800 Subject: [PATCH 02/59] fix: rustfmt --- script/src/verify.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/script/src/verify.rs b/script/src/verify.rs index a04404f151..ff9fbb9a21 100644 --- a/script/src/verify.rs +++ b/script/src/verify.rs @@ -203,10 +203,7 @@ mod tests { use std::path::Path; fn open_cell_verify() -> File { - File::open( - Path::new(env!("CARGO_MANIFEST_DIR")).join("../script/testdata/verify"), - ) - .unwrap() + File::open(Path::new(env!("CARGO_MANIFEST_DIR")).join("../script/testdata/verify")).unwrap() } fn open_cell_always_success() -> File { File::open( From 7af86395c56eb309036fc1e72c504120b67dc427 Mon Sep 17 00:00:00 2001 From: Xuejie Xiao Date: Fri, 21 Dec 2018 14:50:07 +0800 Subject: [PATCH 03/59] refactor: Use fmt::LowerHex to simplify code --- Cargo.lock | 1 - Cargo.toml | 1 - src/cli/run_impl.rs | 8 ++------ 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cfd89228ea..31c0057076 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -320,7 +320,6 @@ dependencies = [ "crypto 0.3.0-pre", "ctrlc 3.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "dir 0.3.0-pre", - "faster-hex 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "hash 0.3.0-pre", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "logger 0.3.0-pre", diff --git a/Cargo.toml b/Cargo.toml index 9bb4d0f1ed..b8a2581263 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,7 +35,6 @@ serde_json = "1.0" crypto = { path = "util/crypto"} ckb-instrument = { path = "util/instrument", features = ["progress_bar"] } hash = { path = "util/hash"} -faster-hex = "0.1" build-info = { path = "util/build-info" } [features] diff --git a/src/cli/run_impl.rs b/src/cli/run_impl.rs index 5e5765190e..b6ca278d53 100644 --- a/src/cli/run_impl.rs +++ b/src/cli/run_impl.rs @@ -17,7 +17,6 @@ use ckb_shared::shared::{ChainProvider, Shared, SharedBuilder}; use ckb_shared::store::ChainKVStore; use ckb_sync::{Relayer, Synchronizer, RELAY_PROTOCOL_ID, SYNC_PROTOCOL_ID}; use crypto::secp::Generator; -use faster_hex::hex_string; use log::info; use numext_fixed_hash::H256; use serde_json; @@ -154,13 +153,10 @@ pub fn type_hash(setup: &Setup) { let system_cell_data_hash = system_cell_tx.outputs()[0].data_hash(); let script = Script::new(0, vec![], Some(system_cell_data_hash), None, vec![]); - println!( - "0x{}", - hex_string(script.type_hash().as_bytes()).expect("hex string") - ); + println!("{:#x}", script.type_hash()); } pub fn keygen() { let result: H256 = Generator::new().random_privkey().into(); - println!("0x{}", hex_string(result.as_bytes()).expect("hex string")); + println!("{:#x}", result); } From 4ecf813cc30dd085d0740ddd82a8e6059d732dc1 Mon Sep 17 00:00:00 2001 From: quake wang Date: Tue, 25 Dec 2018 13:23:25 +0800 Subject: [PATCH 04/59] feat: add rpc max_request_body_size config --- nodes_template/default.json | 6 ++++-- rpc/src/config.rs | 1 + rpc/src/server.rs | 1 + 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/nodes_template/default.json b/nodes_template/default.json index ec30d28433..41b71adedc 100644 --- a/nodes_template/default.json +++ b/nodes_template/default.json @@ -3,7 +3,8 @@ "rpc modules": [ "List of API modules", ["Net", "Pool", "Miner", "Chain"] - ] + ], + "rpc max_request_body_size": "Default is 10MiB = 10 * 1024 * 1024" }, "data_dir": "default", @@ -27,7 +28,8 @@ }, "rpc": { "listen_address": "0.0.0.0:8114", - "modules": ["Net", "Pool", "Miner", "Chain"] + "modules": ["Net", "Pool", "Miner", "Chain"], + "max_request_body_size": 10485760 }, "sync": { "verification_level": "Full", diff --git a/rpc/src/config.rs b/rpc/src/config.rs index a9503ddea4..a12886e714 100644 --- a/rpc/src/config.rs +++ b/rpc/src/config.rs @@ -10,6 +10,7 @@ pub struct Config { pub listen_address: String, pub threads: Option, pub modules: Vec, + pub max_request_body_size: usize, } impl Config { diff --git a/rpc/src/server.rs b/rpc/src/server.rs index fc881fd4d9..a7c5d4c17c 100644 --- a/rpc/src/server.rs +++ b/rpc/src/server.rs @@ -92,6 +92,7 @@ impl RpcServer { AccessControlAllowOrigin::Any, ])) .threads(self.config.threads.unwrap_or_else(num_cpus::get)) + .max_request_body_size(self.config.max_request_body_size) .start_http(&self.config.listen_address.parse().unwrap()) .unwrap(); From 11e39869031a3726065ae35fdd8f98825920ccc5 Mon Sep 17 00:00:00 2001 From: zhangyaning Date: Tue, 25 Dec 2018 14:29:39 +0800 Subject: [PATCH 05/59] doc: add missing blank space in comment (#124) --- db/src/diskdb.rs | 4 ++-- db/src/memorydb.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/db/src/diskdb.rs b/db/src/diskdb.rs index 3287d04e86..5950b68613 100644 --- a/db/src/diskdb.rs +++ b/db/src/diskdb.rs @@ -115,7 +115,7 @@ mod tests { assert_eq!(None, db.read(Some(1), &vec![0, 0]).unwrap()); assert_eq!(Some(vec![1, 1, 1]), db.read(Some(1), &vec![1, 1]).unwrap()); - //return err when col doesn't exist + // return err when col doesn't exist assert!(db.read(Some(2), &vec![0, 0]).is_err()); } @@ -168,7 +168,7 @@ mod tests { None, db.partial_read(Some(1), &vec![1, 1], &(3..0)).unwrap() ); - //return err when col doesn't exist + // return err when col doesn't exist assert!(db.partial_read(Some(2), &vec![0, 0], &(0..1)).is_err()); assert_eq!( diff --git a/db/src/memorydb.rs b/db/src/memorydb.rs index 72895425b4..9289044576 100644 --- a/db/src/memorydb.rs +++ b/db/src/memorydb.rs @@ -97,7 +97,7 @@ mod tests { assert_eq!(None, db.read(Some(1), &vec![0, 0]).unwrap()); assert_eq!(Some(vec![1, 1, 1]), db.read(Some(1), &vec![1, 1]).unwrap()); - //return err when col doesn't exist + // return err when col doesn't exist assert!(db.read(Some(2), &vec![0, 0]).is_err()); } @@ -142,7 +142,7 @@ mod tests { None, db.partial_read(Some(1), &vec![1, 1], &(3..0)).unwrap() ); - //return err when col doesn't exist + // return err when col doesn't exist assert!(db.partial_read(Some(2), &vec![0, 0], &(0..1)).is_err()); assert_eq!( From 18d1d8eeabcc2d02052eb11e92f9c79e3d13ca36 Mon Sep 17 00:00:00 2001 From: u2 Date: Tue, 25 Dec 2018 17:25:53 +0800 Subject: [PATCH 06/59] refactor: use derive clone --- verification/src/block_verifier.rs | 33 +++--------------------------- 1 file changed, 3 insertions(+), 30 deletions(-) diff --git a/verification/src/block_verifier.rs b/verification/src/block_verifier.rs index a7526a84a1..55293685cf 100644 --- a/verification/src/block_verifier.rs +++ b/verification/src/block_verifier.rs @@ -15,6 +15,7 @@ use rayon::iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIter use std::collections::HashSet; //TODO: cellbase, witness +#[derive(Clone)] pub struct BlockVerifier

{ // Verify if the committed transactions is empty empty: EmptyVerifier, @@ -32,20 +33,6 @@ pub struct BlockVerifier

{ transactions: TransactionsVerifier

, } -impl ::std::clone::Clone for BlockVerifier

{ - fn clone(&self) -> Self { - BlockVerifier { - empty: self.empty.clone(), - duplicate: self.duplicate.clone(), - cellbase: self.cellbase.clone(), - merkle_root: self.merkle_root.clone(), - uncles: self.uncles.clone(), - commit: self.commit.clone(), - transactions: self.transactions.clone(), - } - } -} - impl

BlockVerifier

where P: ChainProvider + CellProvider + Clone + 'static, @@ -247,18 +234,11 @@ impl<'a, CP: ChainProvider> HeaderResolver for HeaderResolverWrapper<'a, CP> { } // TODO redo uncle verifier, check uncle proposal duplicate +#[derive(Clone)] pub struct UnclesVerifier { provider: CP, } -impl ::std::clone::Clone for UnclesVerifier { - fn clone(&self) -> Self { - UnclesVerifier { - provider: self.provider.clone(), - } - } -} - impl UnclesVerifier { pub fn new(provider: CP) -> Self { UnclesVerifier { provider } @@ -411,18 +391,11 @@ impl UnclesVerifier { } } +#[derive(Clone)] pub struct TransactionsVerifier

{ provider: P, } -impl ::std::clone::Clone for TransactionsVerifier

{ - fn clone(&self) -> Self { - TransactionsVerifier { - provider: self.provider.clone(), - } - } -} - struct TransactionsVerifierWrapper<'a, P: CellProvider + 'a> { verifier: &'a TransactionsVerifier

, block: &'a Block, From a7cadea4d8f34913dcf102deca98b1fb10c82c87 Mon Sep 17 00:00:00 2001 From: ian Date: Wed, 26 Dec 2018 12:57:05 +0800 Subject: [PATCH 07/59] doc: add mailing list address to CONTRIBUTING --- CONTRIBUTING.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1d3cf0a951..ffcbeb0a10 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,7 +1,12 @@ # Contributing When contributing to this repository, please first discuss the change you wish to make via issue, -email, or any other method with the community before making a change. +email, or any other method with the community before making a change. The developer mailing list +([English] | [Chinese]) should be used to discuss complicated or controversial changes before working +on a patch set. + +[english]: https://groups.google.com/a/nervos.org/d/forum/ckb-dev +[chinese]: https://groups.google.com/a/nervos.org/d/forum/ckb-dev-zh Please note we have a code of conduct, please follow it in all your interactions with the project. From 8206b98a219d2c821c4d679641a64086d42bb7a9 Mon Sep 17 00:00:00 2001 From: u2 Date: Wed, 26 Dec 2018 13:51:33 +0800 Subject: [PATCH 08/59] refactor: use derive clone and some tiny improvements --- shared/src/shared.rs | 1 + sync/src/relayer/mod.rs | 12 +----------- sync/src/synchronizer/block_fetcher.rs | 4 ++-- sync/src/synchronizer/mod.rs | 3 ++- 4 files changed, 6 insertions(+), 14 deletions(-) diff --git a/shared/src/shared.rs b/shared/src/shared.rs index fc78a9d8e9..258011ffba 100644 --- a/shared/src/shared.rs +++ b/shared/src/shared.rs @@ -68,6 +68,7 @@ pub struct Shared { consensus: Consensus, } +// https://github.com/rust-lang/rust/issues/40754 impl ::std::clone::Clone for Shared { fn clone(&self) -> Self { Shared { diff --git a/sync/src/relayer/mod.rs b/sync/src/relayer/mod.rs index 2ff67720b1..a26fa2fdb7 100644 --- a/sync/src/relayer/mod.rs +++ b/sync/src/relayer/mod.rs @@ -34,6 +34,7 @@ use std::time::Duration; pub const TX_PROPOSAL_TOKEN: TimerToken = 0; +#[derive(Clone)] pub struct Relayer { chain: ChainController, shared: Shared, @@ -41,17 +42,6 @@ pub struct Relayer { state: Arc, } -impl ::std::clone::Clone for Relayer { - fn clone(&self) -> Self { - Relayer { - chain: self.chain.clone(), - shared: self.shared.clone(), - tx_pool: self.tx_pool.clone(), - state: Arc::clone(&self.state), - } - } -} - impl Relayer where CI: ChainIndex + 'static, diff --git a/sync/src/synchronizer/block_fetcher.rs b/sync/src/synchronizer/block_fetcher.rs index f39ca6fe16..bc39d60e30 100644 --- a/sync/src/synchronizer/block_fetcher.rs +++ b/sync/src/synchronizer/block_fetcher.rs @@ -24,12 +24,12 @@ impl BlockFetcher where CI: ChainIndex, { - pub fn new(synchronizer: &Synchronizer, peer: PeerIndex) -> Self { + pub fn new(synchronizer: Synchronizer, peer: PeerIndex) -> Self { let tip_header = synchronizer.shared.tip_header().read().clone(); BlockFetcher { tip_header, peer, - synchronizer: synchronizer.clone(), + synchronizer, } } pub fn initial_and_check_inflight(&self) -> bool { diff --git a/sync/src/synchronizer/mod.rs b/sync/src/synchronizer/mod.rs index e864959f1c..42a34a8d59 100644 --- a/sync/src/synchronizer/mod.rs +++ b/sync/src/synchronizer/mod.rs @@ -83,6 +83,7 @@ pub struct Synchronizer { pub outbound_peers_with_protect: Arc, } +// https://github.com/rust-lang/rust/issues/40754 impl ::std::clone::Clone for Synchronizer { fn clone(&self) -> Self { Synchronizer { @@ -477,7 +478,7 @@ impl Synchronizer { } pub fn get_blocks_to_fetch(&self, peer: PeerIndex) -> Option> { - BlockFetcher::new(&self, peer).fetch() + BlockFetcher::new(self.clone(), peer).fetch() } fn on_connected(&self, nc: &CKBProtocolContext, peer: PeerIndex) { From f348821a122e218c61684365b598486364193f26 Mon Sep 17 00:00:00 2001 From: zhangsoledad <787953403@qq.com> Date: Mon, 24 Dec 2018 10:19:07 +0800 Subject: [PATCH 09/59] refactor: rename OutofBound -> OutOfBound --- core/src/error.rs | 2 +- verification/src/error.rs | 2 +- verification/src/tests/transaction_verifier.rs | 2 +- verification/src/transaction_verifier.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/src/error.rs b/core/src/error.rs index ff5b7c10cc..c1e56af1a9 100644 --- a/core/src/error.rs +++ b/core/src/error.rs @@ -21,7 +21,7 @@ impl From for Error { #[derive(Debug, PartialEq, Eq)] pub enum TxError { - OutofBound, + OutOfBound, NotMatch, EmptyGroup, WrongFormat, diff --git a/verification/src/error.rs b/verification/src/error.rs index d2f3b67359..078ad2c107 100644 --- a/verification/src/error.rs +++ b/verification/src/error.rs @@ -114,7 +114,7 @@ pub enum DifficultyError { #[derive(Debug, PartialEq, Clone, Copy, Eq)] pub enum TransactionError { NullInput, - OutofBound, + OutOfBound, DuplicateInputs, Empty, InvalidCapacity, diff --git a/verification/src/tests/transaction_verifier.rs b/verification/src/tests/transaction_verifier.rs index 0a613f1b2f..8a5e8f70a5 100644 --- a/verification/src/tests/transaction_verifier.rs +++ b/verification/src/tests/transaction_verifier.rs @@ -45,7 +45,7 @@ pub fn test_capacity_outofbound() { }; let verifier = CapacityVerifier::new(&rtx); - assert_eq!(verifier.verify().err(), Some(TransactionError::OutofBound)); + assert_eq!(verifier.verify().err(), Some(TransactionError::OutOfBound)); } #[test] diff --git a/verification/src/transaction_verifier.rs b/verification/src/transaction_verifier.rs index b905649d44..b487166a70 100644 --- a/verification/src/transaction_verifier.rs +++ b/verification/src/transaction_verifier.rs @@ -192,7 +192,7 @@ impl<'a> CapacityVerifier<'a> { .iter() .any(|output| output.bytes_len() as Capacity > output.capacity) { - Err(TransactionError::OutofBound) + Err(TransactionError::OutOfBound) } else { Ok(()) } From 6e128c1114f5109919ac768e830a29994b133d49 Mon Sep 17 00:00:00 2001 From: zhangsoledad <787953403@qq.com> Date: Mon, 24 Dec 2018 11:44:17 +0800 Subject: [PATCH 10/59] refactor: rename CellOutput#contract to CellOutput#_type --- core/src/cell.rs | 2 +- core/src/error.rs | 8 -------- core/src/transaction.rs | 9 +++++---- protocol/src/builder.rs | 9 +++------ protocol/src/convert.rs | 2 +- protocol/src/protocol.fbs | 2 +- protocol/src/protocol_generated.rs | 16 ++++++++-------- rpc/doc.md | 6 +++--- script/src/syscalls/fetch_script_hash.rs | 8 ++++---- script/src/syscalls/load_cell_by_field.rs | 12 ++++++------ script/src/syscalls/mod.rs | 10 +++++----- script/src/verify.rs | 4 ++-- 12 files changed, 39 insertions(+), 49 deletions(-) diff --git a/core/src/cell.rs b/core/src/cell.rs index 96214710a1..da180d2249 100644 --- a/core/src/cell.rs +++ b/core/src/cell.rs @@ -217,7 +217,7 @@ mod tests { capacity: 2, data: vec![], lock: H256::default(), - contract: None, + type_: None, }; db.cells.insert(p1.clone(), Some(o.clone())); diff --git a/core/src/error.rs b/core/src/error.rs index c1e56af1a9..192fe039a3 100644 --- a/core/src/error.rs +++ b/core/src/error.rs @@ -18,11 +18,3 @@ impl From for Error { Error::InvalidSignature(e) } } - -#[derive(Debug, PartialEq, Eq)] -pub enum TxError { - OutOfBound, - NotMatch, - EmptyGroup, - WrongFormat, -} diff --git a/core/src/transaction.rs b/core/src/transaction.rs index ccaf942835..a643998caf 100644 --- a/core/src/transaction.rs +++ b/core/src/transaction.rs @@ -79,16 +79,17 @@ pub struct CellOutput { pub capacity: Capacity, pub data: Vec, pub lock: H256, - pub contract: Option