From 706e22d6c446e4892bdd1a1377badc970d9c0451 Mon Sep 17 00:00:00 2001 From: jjy Date: Mon, 25 Feb 2019 16:31:52 +0800 Subject: [PATCH 01/99] refactor: ckb-network moved to new p2p library --- Cargo.lock | 1170 +++++-------------- network/Cargo.toml | 4 +- network/src/ckb_protocol.rs | 326 ++---- network/src/ckb_protocol_handler.rs | 22 +- network/src/ckb_service.rs | 255 ++-- network/src/errors.rs | 64 +- network/src/lib.rs | 43 +- network/src/network.rs | 641 ++++------ network/src/network_config.rs | 46 +- network/src/network_group.rs | 6 +- network/src/network_service.rs | 33 +- network/src/peer_store.rs | 4 +- network/src/peer_store/db.rs | 18 +- network/src/peer_store/sqlite_peer_store.rs | 10 +- network/src/peers_registry.rs | 189 ++- network/src/protocol.rs | 2 +- network/src/tests/peers_registry.rs | 37 +- network/src/tests/sqlite_peer_store.rs | 43 +- network/src/timer_service.rs | 157 +-- 19 files changed, 963 insertions(+), 2107 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 643846de8a..d7d8cb70eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,22 +1,40 @@ [[package]] -name = "aho-corasick" -version = "0.6.9" +name = "aes-ctr" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "memchr 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "aes-soft 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "aesni 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "stream-cipher 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] -name = "aio-limited" -version = "0.1.0" -source = "git+https://github.com/paritytech/aio-limited.git#f01b01501c87c93d3005f9120cc35d0e576fa7a3" +name = "aes-soft" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "parking_lot 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-executor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-timer 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", + "block-cipher-trait 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "opaque-debug 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "aesni" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-cipher-trait 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", + "opaque-debug 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "stream-cipher 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "aho-corasick" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -45,14 +63,6 @@ dependencies = [ "nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "asn1_der" -version = "0.5.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "etrace 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "atty" version = "0.2.11" @@ -90,24 +100,6 @@ dependencies = [ "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "base64" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "safemem 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "base64" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "safemem 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "base64" version = "0.9.3" @@ -117,15 +109,6 @@ dependencies = [ "safemem 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "bigint" -version = "4.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "crunchy 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "bincode" version = "1.1.2" @@ -172,13 +155,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "bitflags" -version = "0.9.1" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] -name = "bitflags" -version = "1.0.4" +name = "blake2" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "crypto-mac 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "digest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "opaque-debug 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "blake2b-rs" @@ -190,11 +179,29 @@ dependencies = [ [[package]] name = "block-buffer" -version = "0.3.3" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "arrayref 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", - "byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "block-padding 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "block-cipher-trait" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "block-padding" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -216,7 +223,7 @@ version = "0.7.0-pre" [[package]] name = "byte-tools" -version = "0.2.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -264,15 +271,6 @@ name = "cfg-if" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "chashmap" -version = "2.2.1" -source = "git+https://github.com/redox-os/tfs#b3e7cae1712470f4e1d7992930904c5e25621e07" -dependencies = [ - "owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", - "parking_lot 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "chrono" version = "0.4.6" @@ -480,13 +478,13 @@ dependencies = [ "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "libp2p 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "multihash 0.8.1-pre (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", + "p2p 0.1.0 (git+https://github.com/nervosnetwork/p2p)", "r2d2 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2_sqlite 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "rusqlite 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)", + "secio 0.1.0 (git+https://github.com/nervosnetwork/p2p)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "snap 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -943,6 +941,15 @@ dependencies = [ "secp256k1 0.9.1 (git+https://github.com/nervosnetwork/rust-secp256k1)", ] +[[package]] +name = "crypto-mac" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", + "subtle 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "csv" version = "1.0.5" @@ -960,6 +967,15 @@ dependencies = [ "memchr 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ctr" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-cipher-trait 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", + "stream-cipher 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "ctrlc" version = "3.1.1" @@ -970,24 +986,16 @@ dependencies = [ ] [[package]] -name = "datastore" -version = "0.1.0" -source = "git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch#6a61b9705cc8340fb9a40ff0df41f03042c80a5a" -dependencies = [ - "base64 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", - "chashmap 2.2.1 (git+https://github.com/redox-os/tfs)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", - "tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)", -] +name = "data-encoding" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "digest" -version = "0.7.6" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1024,22 +1032,6 @@ dependencies = [ "backtrace 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "eth-secp256k1" -version = "0.5.7" -source = "git+https://github.com/paritytech/rust-secp256k1#ccc06e7480148b723eb44ac56cf4d20eec380b6f" -dependencies = [ - "arrayvec 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", - "cc 1.0.29 (registry+https://github.com/rust-lang/crates.io-index)", - "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "etrace" -version = "1.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "failure" version = "0.1.5" @@ -1096,19 +1088,6 @@ name = "fnv" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "foreign-types" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "foreign-types-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "foreign-types-shared" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "fs_extra" version = "1.1.0" @@ -1147,14 +1126,9 @@ dependencies = [ "num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "gcc" -version = "0.3.55" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "generic-array" -version = "0.9.0" +version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1230,6 +1204,15 @@ dependencies = [ "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "hmac" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "crypto-mac 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "digest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "http" version = "0.1.15" @@ -1253,24 +1236,6 @@ dependencies = [ "quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "hyper" -version = "0.10.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", - "httparse 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)", - "language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", - "mime 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", - "num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", - "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", - "traitobject 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "typeable 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "unicase 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", - "url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "hyper" version = "0.11.27" @@ -1324,16 +1289,6 @@ dependencies = [ "want 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "idna" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-normalization 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "indexmap" version = "1.0.2" @@ -1351,11 +1306,6 @@ dependencies = [ "regex 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "integer-encoding" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "iovec" version = "0.1.2" @@ -1530,374 +1480,78 @@ dependencies = [ ] [[package]] -name = "libp2p" -version = "0.1.0" -source = "git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch#6a61b9705cc8340fb9a40ff0df41f03042c80a5a" +name = "librocksdb-sys" +version = "5.17.2" +source = "git+https://github.com/rust-rocksdb/rust-rocksdb?rev=1038c71df85a58b7f48894ca4cbf7c9228cb1cdf#1038c71df85a58b7f48894ca4cbf7c9228cb1cdf" dependencies = [ - "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "libp2p-core 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "libp2p-dns 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "libp2p-floodsub 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "libp2p-identify 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "libp2p-kad 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "libp2p-mplex 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "libp2p-peerstore 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "libp2p-ping 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "libp2p-ratelimit 0.1.1 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "libp2p-relay 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "libp2p-secio 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "libp2p-tcp-transport 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "libp2p-transport-timeout 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "libp2p-uds 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "libp2p-websocket 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "libp2p-yamux 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "multiaddr 0.3.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "stdweb 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-current-thread 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", + "bindgen 0.47.1 (registry+https://github.com/rust-lang/crates.io-index)", + "cc 1.0.29 (registry+https://github.com/rust-lang/crates.io-index)", + "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] -name = "libp2p-core" -version = "0.1.0" -source = "git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch#6a61b9705cc8340fb9a40ff0df41f03042c80a5a" +name = "libsqlite3-sys" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "bs58 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "multiaddr 0.3.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "multihash 0.8.1-pre (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "multistream-select 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", - "protobuf 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "rw-stream-sink 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "smallvec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", + "cc 1.0.29 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "vcpkg 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] -name = "libp2p-dns" -version = "0.1.0" -source = "git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch#6a61b9705cc8340fb9a40ff0df41f03042c80a5a" +name = "linked-hash-map" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "libp2p-core 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "multiaddr 0.3.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "tokio-dns-unofficial 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_test 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] -name = "libp2p-floodsub" -version = "0.1.0" -source = "git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch#6a61b9705cc8340fb9a40ff0df41f03042c80a5a" +name = "linked-hash-map" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "linked-hash-map" +version = "0.5.1" +source = "git+https://github.com/nervosnetwork/linked-hash-map?rev=df27f21#df27f2194df66d3c652b0fb661da707cc167c9d1" + +[[package]] +name = "linked-hash-map" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "lock_api" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "bs58 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "libp2p-core 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "multiaddr 0.3.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", - "protobuf 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "smallvec 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "unsigned-varint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "owning_ref 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] -name = "libp2p-identify" -version = "0.1.0" -source = "git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch#6a61b9705cc8340fb9a40ff0df41f03042c80a5a" +name = "log" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "libp2p-core 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "libp2p-peerstore 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "multiaddr 0.3.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", - "protobuf 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "unsigned-varint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] -name = "libp2p-kad" -version = "0.1.0" -source = "git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch#6a61b9705cc8340fb9a40ff0df41f03042c80a5a" +name = "log" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "arrayvec 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", - "bigint 4.4.1 (registry+https://github.com/rust-lang/crates.io-index)", - "bs58 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "datastore 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "libp2p-core 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "libp2p-identify 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "libp2p-ping 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "multiaddr 0.3.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", - "protobuf 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "smallvec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-timer 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", - "unsigned-varint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] -name = "libp2p-mplex" -version = "0.1.0" -source = "git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch#6a61b9705cc8340fb9a40ff0df41f03042c80a5a" -dependencies = [ - "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "libp2p-core 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "unsigned-varint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "libp2p-peerstore" -version = "0.1.0" -source = "git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch#6a61b9705cc8340fb9a40ff0df41f03042c80a5a" -dependencies = [ - "bs58 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "datastore 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "libp2p-core 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "multiaddr 0.3.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "libp2p-ping" -version = "0.1.0" -source = "git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch#6a61b9705cc8340fb9a40ff0df41f03042c80a5a" -dependencies = [ - "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "libp2p-core 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "multiaddr 0.3.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "multistream-select 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "libp2p-ratelimit" -version = "0.1.1" -source = "git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch#6a61b9705cc8340fb9a40ff0df41f03042c80a5a" -dependencies = [ - "aio-limited 0.1.0 (git+https://github.com/paritytech/aio-limited.git)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "libp2p-core 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-executor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "libp2p-relay" -version = "0.1.0" -source = "git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch#6a61b9705cc8340fb9a40ff0df41f03042c80a5a" -dependencies = [ - "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "libp2p-core 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "libp2p-peerstore 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "multiaddr 0.3.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "protobuf 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "unsigned-varint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "libp2p-secio" -version = "0.1.0" -source = "git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch#6a61b9705cc8340fb9a40ff0df41f03042c80a5a" -dependencies = [ - "asn1_der 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", - "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "eth-secp256k1 0.5.7 (git+https://github.com/paritytech/rust-secp256k1)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "libp2p-core 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "protobuf 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.3.23 (registry+https://github.com/rust-lang/crates.io-index)", - "ring 0.13.5 (registry+https://github.com/rust-lang/crates.io-index)", - "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", - "rw-stream-sink 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "untrusted 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "libp2p-tcp-transport" -version = "0.1.0" -source = "git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch#6a61b9705cc8340fb9a40ff0df41f03042c80a5a" -dependencies = [ - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "libp2p-core 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "multiaddr 0.3.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "tk-listen 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-tcp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "libp2p-transport-timeout" -version = "0.1.0" -source = "git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch#6a61b9705cc8340fb9a40ff0df41f03042c80a5a" -dependencies = [ - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "libp2p-core 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-timer 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "libp2p-uds" -version = "0.1.0" -source = "git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch#6a61b9705cc8340fb9a40ff0df41f03042c80a5a" -dependencies = [ - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "libp2p-core 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "multiaddr 0.3.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "tokio-uds 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "libp2p-websocket" -version = "0.1.0" -source = "git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch#6a61b9705cc8340fb9a40ff0df41f03042c80a5a" -dependencies = [ - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "libp2p-core 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "multiaddr 0.3.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "rw-stream-sink 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "stdweb 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "websocket 0.20.3 (git+https://github.com/tomaka/rust-websocket?branch=send)", -] - -[[package]] -name = "libp2p-yamux" -version = "0.1.0" -source = "git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch#6a61b9705cc8340fb9a40ff0df41f03042c80a5a" -dependencies = [ - "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "libp2p-core 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "yamux 0.1.6 (git+https://github.com/paritytech/yamux)", -] - -[[package]] -name = "librocksdb-sys" -version = "5.17.2" -source = "git+https://github.com/rust-rocksdb/rust-rocksdb?rev=1038c71df85a58b7f48894ca4cbf7c9228cb1cdf#1038c71df85a58b7f48894ca4cbf7c9228cb1cdf" -dependencies = [ - "bindgen 0.47.1 (registry+https://github.com/rust-lang/crates.io-index)", - "cc 1.0.29 (registry+https://github.com/rust-lang/crates.io-index)", - "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "libsqlite3-sys" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "cc 1.0.29 (registry+https://github.com/rust-lang/crates.io-index)", - "pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", - "vcpkg 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "linked-hash-map" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "serde 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_test 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "linked-hash-map" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[[package]] -name = "linked-hash-map" -version = "0.5.1" -source = "git+https://github.com/nervosnetwork/linked-hash-map?rev=df27f21#df27f2194df66d3c652b0fb661da707cc167c9d1" - -[[package]] -name = "linked-hash-map" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[[package]] -name = "lock_api" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "owning_ref 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "log" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "log" -version = "0.4.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "logger" -version = "0.7.0-pre" +name = "logger" +version = "0.7.0-pre" dependencies = [ "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1928,11 +1582,6 @@ dependencies = [ "linked-hash-map 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "matches" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "memchr" version = "2.1.3" @@ -1952,14 +1601,6 @@ name = "merkle-cbt" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "mime" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "mime" version = "0.3.13" @@ -2007,55 +1648,6 @@ dependencies = [ "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "multiaddr" -version = "0.3.0" -source = "git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch#6a61b9705cc8340fb9a40ff0df41f03042c80a5a" -dependencies = [ - "bs58 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "byteorder 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", - "integer-encoding 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", - "multihash 0.8.1-pre (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)", - "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "multihash" -version = "0.8.1-pre" -source = "git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch#6a61b9705cc8340fb9a40ff0df41f03042c80a5a" -dependencies = [ - "sha1 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", - "sha2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", - "tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "multistream-select" -version = "0.1.0" -source = "git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch#6a61b9705cc8340fb9a40ff0df41f03042c80a5a" -dependencies = [ - "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "smallvec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "unsigned-varint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "native-tls" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", - "openssl 0.9.24 (registry+https://github.com/rust-lang/crates.io-index)", - "schannel 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)", - "security-framework 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", - "security-framework-sys 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", - "tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "net2" version = "0.2.33" @@ -2083,11 +1675,6 @@ name = "nodrop" version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "nohash-hasher" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "nom" version = "4.2.0" @@ -2188,35 +1775,9 @@ dependencies = [ ] [[package]] -name = "openssl" -version = "0.9.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", - "foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", - "openssl-sys 0.9.40 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "openssl-sys" -version = "0.9.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "cc 1.0.29 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", - "pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", - "vcpkg 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "owning_ref" -version = "0.3.3" +name = "opaque-debug" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", -] [[package]] name = "owning_ref" @@ -2227,30 +1788,45 @@ dependencies = [ ] [[package]] -name = "parking_lot" -version = "0.4.8" -source = "registry+https://github.com/rust-lang/crates.io-index" +name = "p2p" +version = "0.1.0" +source = "git+https://github.com/nervosnetwork/p2p#75a3b0b40663358b7eb81356f107f7bcf60c288b" dependencies = [ - "owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", - "parking_lot_core 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", + "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", + "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "parity-multiaddr 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "secio 0.1.0 (git+https://github.com/nervosnetwork/p2p)", + "tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-yamux 0.1.3 (git+https://github.com/nervosnetwork/p2p)", ] [[package]] -name = "parking_lot" -version = "0.5.5" +name = "parity-multiaddr" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", - "parking_lot_core 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", + "arrayref 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "bs58 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "data-encoding 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "parity-multihash 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", + "unsigned-varint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] -name = "parking_lot" -version = "0.6.4" +name = "parity-multihash" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "lock_api 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", - "parking_lot_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "blake2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", + "sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "unsigned-varint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2262,29 +1838,6 @@ dependencies = [ "parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "parking_lot_core" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "smallvec 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "parking_lot_core" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", - "smallvec 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "parking_lot_core" version = "0.4.0" @@ -2344,16 +1897,6 @@ dependencies = [ "tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "protobuf" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[[package]] -name = "quick-error" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "quick-error" version = "1.2.2" @@ -2604,13 +2147,15 @@ dependencies = [ [[package]] name = "ring" -version = "0.13.5" +version = "0.14.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cc 1.0.29 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "spin 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "untrusted 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2633,18 +2178,6 @@ dependencies = [ "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "rust-crypto" -version = "0.2.36" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "gcc 0.3.55 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.3.23 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", - "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "rust-ini" version = "0.13.0" @@ -2655,11 +2188,6 @@ name = "rustc-demangle" version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "rustc-serialize" -version = "0.3.24" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "rustc_version" version = "0.2.3" @@ -2679,26 +2207,11 @@ dependencies = [ "wait-timeout 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "rw-stream-sink" -version = "0.1.0" -source = "git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch#6a61b9705cc8340fb9a40ff0df41f03042c80a5a" -dependencies = [ - "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "ryu" version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "safemem" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "safemem" version = "0.3.0" @@ -2712,15 +2225,6 @@ dependencies = [ "winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "schannel" -version = "0.1.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "scheduled-thread-pool" version = "0.2.0" @@ -2758,6 +2262,29 @@ dependencies = [ "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "secio" +version = "0.1.0" +source = "git+https://github.com/nervosnetwork/p2p#75a3b0b40663358b7eb81356f107f7bcf60c288b" +dependencies = [ + "aes-ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bs58 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", + "ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", + "hmac 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", + "ring 0.14.6 (registry+https://github.com/rust-lang/crates.io-index)", + "secp256k1 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", + "twofish 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unsigned-varint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "untrusted 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "secp256k1" version = "0.9.1" @@ -2769,22 +2296,11 @@ dependencies = [ ] [[package]] -name = "security-framework" -version = "0.1.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "core-foundation 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", - "core-foundation-sys 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", - "security-framework-sys 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "security-framework-sys" -version = "0.1.16" +name = "secp256k1" +version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "core-foundation-sys 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "cc 1.0.29 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2861,23 +2377,18 @@ dependencies = [ [[package]] name = "sha1" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[[package]] -name = "sha1" -version = "0.5.0" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "sha2" -version = "0.7.1" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "block-buffer 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", - "byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "digest 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", + "block-buffer 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "digest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "opaque-debug 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2900,11 +2411,6 @@ name = "smallvec" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "smallvec" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "smallvec" version = "0.6.8" @@ -2932,11 +2438,6 @@ name = "stable_deref_trait" version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "stdweb" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "stop-handler" version = "0.7.0-pre" @@ -2947,6 +2448,14 @@ dependencies = [ "parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "stream-cipher" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "string" version = "0.1.3" @@ -2957,6 +2466,11 @@ name = "strsim" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "subtle" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "syn" version = "0.15.26" @@ -2983,15 +2497,6 @@ name = "take" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "tempdir" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "tempfile" version = "3.0.7" @@ -3074,17 +2579,6 @@ dependencies = [ "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "tk-listen" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "tokio" version = "0.1.16" @@ -3145,17 +2639,6 @@ dependencies = [ "tokio-executor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "tokio-dns-unofficial" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "futures-cpupool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "tokio-executor" version = "0.1.6" @@ -3277,17 +2760,6 @@ dependencies = [ "tokio-executor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "tokio-tls" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "native-tls 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "tokio-udp" version = "0.1.3" @@ -3319,6 +2791,19 @@ dependencies = [ "tokio-reactor 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "tokio-yamux" +version = "0.1.3" +source = "git+https://github.com/nervosnetwork/p2p#75a3b0b40663358b7eb81356f107f7bcf60c288b" +dependencies = [ + "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", + "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "toml" version = "0.4.10" @@ -3327,11 +2812,6 @@ dependencies = [ "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "traitobject" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "try-lock" version = "0.1.0" @@ -3343,9 +2823,14 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] -name = "typeable" -version = "0.1.2" +name = "twofish" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-cipher-trait 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "opaque-debug 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "typenum" @@ -3357,14 +2842,6 @@ name = "ucd-util" version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "unicase" -version = "1.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "unicase" version = "2.2.0" @@ -3373,22 +2850,6 @@ dependencies = [ "version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "unicode-bidi" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "unicode-normalization" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "smallvec 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "unicode-width" version = "0.1.5" @@ -3420,26 +2881,12 @@ dependencies = [ name = "unsigned-varint" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", -] [[package]] name = "untrusted" version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "url" -version = "1.7.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", - "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", - "percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "utf8-ranges" version = "1.0.2" @@ -3503,27 +2950,6 @@ dependencies = [ "try-lock 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "websocket" -version = "0.20.3" -source = "git+https://github.com/tomaka/rust-websocket?branch=send#28ea5eb82b573bf3ace2fc75c36d791bcedf08b1" -dependencies = [ - "base64 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", - "bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", - "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "hyper 0.10.15 (registry+https://github.com/rust-lang/crates.io-index)", - "native-tls 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.3.23 (registry+https://github.com/rust-lang/crates.io-index)", - "sha1 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-tls 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", - "unicase 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", - "url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "which" version = "2.0.1" @@ -3596,49 +3022,30 @@ dependencies = [ "linked-hash-map 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "yamux" -version = "0.1.6" -source = "git+https://github.com/paritytech/yamux#957bccae409e827db4be05abb67d1d6246d5f5c8" -dependencies = [ - "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "nohash-hasher 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", - "quick-error 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", -] - [metadata] +"checksum aes-ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d2e5b0458ea3beae0d1d8c0f3946564f8e10f90646cf78c06b4351052058d1ee" +"checksum aes-soft 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cfd7e7ae3f9a1fb5c03b389fc6bb9a51400d0c13053f0dca698c832bfd893a0d" +"checksum aesni 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2f70a6b5f971e473091ab7cfb5ffac6cde81666c4556751d8d5620ead8abf100" "checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e" -"checksum aio-limited 0.1.0 (git+https://github.com/paritytech/aio-limited.git)" = "" "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" "checksum antidote 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "34fde25430d87a9388dadbe6e34d7f72a462c8b43ac8d309b42b0a8505d7e2a5" "checksum arrayref 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0d382e583f07208808f6b1249e60848879ba3543f57c32277bf52d69c2f0f0ee" "checksum arrayvec 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "92c7fb76bc8826a8b33b4ee5bb07a247a81e76764ab4d55e8f73e3a4d8808c71" -"checksum asn1_der 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)" = "766afdc5c6d7c15de1abe4c9c15e360be3aa972c363ba5b606be3c4271235886" "checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652" "checksum autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a6d640bee2da49f60a4068a7fae53acde8982514ab7bae8b8cea9e88cbcfd799" "checksum backtrace 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)" = "b5b493b66e03090ebc4343eb02f94ff944e0cbc9ac6571491d170ba026741eb5" "checksum backtrace-sys 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)" = "797c830ac25ccc92a7f8a7b9862bde440715531514594a6154e3d4a54dd769b6" -"checksum base64 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "96434f987501f0ed4eb336a411e0631ecd1afa11574fe148587adc4ff96143c9" -"checksum base64 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5032d51da2741729bfdaeb2664d9b8c6d9fd1e2b90715c660b6def36628499c2" "checksum base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "489d6c0ed21b11d038c31b6ceccca973e65d73ba3bd8ecb9a2babf5546164643" -"checksum bigint 4.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ebecac13b3c745150d7b6c3ea7572d372f09d627c2077e893bf26c5c7f70d282" "checksum bincode 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3efe0b4c8eaeed8600549c29f538a6a11bf422858d0ed435b1d70ec4ab101190" "checksum bindgen 0.47.1 (registry+https://github.com/rust-lang/crates.io-index)" = "91f5aa1a54c5997396b59cfac31af9c1cbcaa988958c4ce84364257b2cadfad0" "checksum bit-set 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6f1efcc46c18245a69c38fcc5cc650f16d3a59d034f3106e9ed63748f695730a" "checksum bit-vec 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4440d5cb623bb7390ae27fec0bb6c61111969860f8e3ae198bfa0663645e67cf" -"checksum bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4efd02e230a02e18f92fc2735f44597385ed02ad8f831e7c1c1156ee5e1ab3a5" "checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" "checksum blake2b-rs 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "895ae726056ce2ab1b19854be889ca43a4a2c0b02c355bee90aa9e8ee6d0379b" "checksum block-buffer 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a076c298b9ecdb530ed9d967e74a6027d6a7478924520acddcddc24c1c8ab3ab" "checksum bloom-filters 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b233192e97c6e528c071bd83180c5d4f35846b9aff4670ea8e05e3ba0daa5e30" "checksum bs58 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0de79cfb98e7aa9988188784d8664b4b5dad6eaaa0863b91d9a4ed871d4f7a42" -"checksum byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "560c32574a12a89ecd91f5e742165893f86e3ab98d21f8ea548658eb9eef5f40" +"checksum byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" "checksum byteorder 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "96c8b41881888cc08af32d47ac4edd52bc7fa27fef774be47a92443756451304" "checksum byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a019b10a2a7cdeb292db131fc8113e57ea2a908f6e7894b0c3c671893b65dbeb" "checksum bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c" @@ -3646,7 +3053,6 @@ dependencies = [ "checksum cc 1.0.29 (registry+https://github.com/rust-lang/crates.io-index)" = "4390a3b5f4f6bce9c1d0c00128379df433e53777fdd30e92f16a529332baec4e" "checksum cexpr 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "644d693ecfa91955ed32dcc7eda4914e1be97a641fb6f0645a37348e20b230da" "checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" -"checksum chashmap 2.2.1 (git+https://github.com/redox-os/tfs)" = "" "checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878" "checksum ckb-vm 0.1.0 (git+https://github.com/nervosnetwork/ckb-vm?rev=dd90ed1)" = "" "checksum clang-sys 0.26.4 (registry+https://github.com/rust-lang/crates.io-index)" = "6ef0c1bcf2e99c649104bd7a7012d8f8802684400e03db0ec0af48583c6fa0e4" @@ -3669,17 +3075,17 @@ dependencies = [ "checksum crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f8306fcef4a7b563b76b7dd949ca48f52bc1141aa067d2ea09565f3e2652aa5c" "checksum crunchy 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "a2f4a431c5c9f662e1200b7c7f02c34e91361150e382089a8f2dec3ba680cbda" "checksum crunchy 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c240f247c278fa08a6d4820a6a222bfc6e0d999e51ba67be94f44c905b2161f2" +"checksum crypto-mac 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4434400df11d95d556bac068ddfedd482915eb18fe8bea89bc80b6e4b1c179e5" "checksum csv 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "9fd1c44c58078cfbeaf11fbb3eac9ae5534c23004ed770cc4bfb48e658ae4f04" "checksum csv-core 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fa5cdef62f37e6ffe7d1f07a381bc0db32b7a3ff1cac0de56cb0d81e71f53d65" +"checksum ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "044f882973b245404e90c90e7e42e8ee8d7a64edfd7adf83d684fb97e8e2c1b6" "checksum ctrlc 3.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "630391922b1b893692c6334369ff528dcc3a9d8061ccf4c803aa8f83cb13db5e" -"checksum datastore 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)" = "" -"checksum digest 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "03b072242a8cbaf9c145665af9d250c59af3b958f83ed6824e13533cf76d5b90" +"checksum data-encoding 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f4f47ca1860a761136924ddd2422ba77b2ea54fe8cc75b9040804a0d9d32ad97" +"checksum digest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "05f47366984d3ad862010e22c7ce81a7dbcaebbdfb37241a620f8b6596ee135c" "checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0" "checksum encode_unicode 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "90b2c9496c001e8cb61827acdefad780795c42264c137744cae6f7d9e3450abd" "checksum env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b61fa891024a945da30a9581546e8cfaf5602c7b3f4c137a2805cf388f92075a" "checksum error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "07e791d3be96241c77c43846b665ef1384606da2cd2a48730abe606a12906e02" -"checksum eth-secp256k1 0.5.7 (git+https://github.com/paritytech/rust-secp256k1)" = "" -"checksum etrace 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f17311e68ea07046ee809b8513f6c259518bc10173681d98c21f8c3926f56f40" "checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2" "checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1" "checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" @@ -3688,32 +3094,27 @@ dependencies = [ "checksum faster-hex 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a5b8cccaafb5aae8c282692e5590f341925edea6c696e8715ff0d973320b2646" "checksum flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ea0c34f669be9911826facafe996adfda978aeee67285a13556869e2d8b8331f" "checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3" -"checksum foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" -"checksum foreign-types-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" "checksum fs_extra 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5f2a4a2034423744d2cc7ca2068453168dcdb82c438419e639a26bd87839c674" "checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" "checksum futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)" = "49e7653e374fe0d0c12de4250f0bdb60680b8c80eed558c5c7538eec9c89e21b" "checksum futures-cpupool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "ab90cde24b3319636588d0c35fe03b1333857621051837ed769faefb4c2162e4" -"checksum gcc 0.3.55 (registry+https://github.com/rust-lang/crates.io-index)" = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2" -"checksum generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ef25c5683767570c2bbd7deba372926a55eaae9982d7726ee2a1050239d45b9d" +"checksum generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3c0f28c2f5bfb5960175af447a2da7c18900693738343dc896ffbcabd9839592" "checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" "checksum globset 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4743617a7464bbda3c8aec8558ff2f9429047e025771037df561d383337ff865" "checksum goblin 0.0.19 (registry+https://github.com/rust-lang/crates.io-index)" = "c65cd533b33e3d04c6e393225fa8919ddfcf5862ca8919c7f9a167c312ef41c2" "checksum h2 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "ddb2b25a33e231484694267af28fec74ac63b5ccf51ee2065a5e313b834d836e" "checksum hashbrown 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3bae29b6653b3412c2e71e9d486db9f9df5d701941d86683005efb9f2d28e3da" "checksum heapsize 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1679e6ea370dee694f91f1dc469bf94cf8f52051d147aec3e1f9497c6fc22461" +"checksum hmac 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f127a908633569f208325f86f71255d3363c79721d7f9fe31cd5569908819771" "checksum http 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "1a10e5b573b9a0146545010f50772b9e8b1dd0a256564cc4307694c68832a2f5" "checksum httparse 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e8734b0cfd3bc3e101ec59100e101c2eecd19282202e87808b3037b442777a83" "checksum humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ca7e5f2e110db35f93b837c81797f3714500b81d517bf20c431b16d3ca4f114" -"checksum hyper 0.10.15 (registry+https://github.com/rust-lang/crates.io-index)" = "df0caae6b71d266b91b4a83111a61d2b94ed2e2bea024c532b933dcff867e58c" "checksum hyper 0.11.27 (registry+https://github.com/rust-lang/crates.io-index)" = "34a590ca09d341e94cddf8e5af0bbccde205d5fbc2fa3c09dd67c7f85cea59d7" "checksum hyper 0.12.25 (registry+https://github.com/rust-lang/crates.io-index)" = "7d5b6658b016965ae301fa995306db965c93677880ea70765a84235a96eae896" -"checksum idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e" "checksum indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7e81a7c05f79578dbc15793d8b619db9ba32b4577003ef3af1a91c416798c58d" "checksum indicatif 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2c60da1c9abea75996b70a931bba6c750730399005b61ccd853cee50ef3d0d0c" -"checksum integer-encoding 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "26746cbc2e680af687e88d717f20ff90079bd10fc984ad57d277cd0e37309fa5" "checksum iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dbe6e417e7d0975db6512b90796e8ce223145ac4e33c377e4a42882a0e88bb08" "checksum itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5b8467d9c1cebe26feb08c640139247fac215782d35371ade9a2136ed6085358" "checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b" @@ -3731,23 +3132,6 @@ dependencies = [ "checksum lazycell 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f" "checksum libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)" = "e962c7641008ac010fa60a7dfdc1712449f29c44ef2d4702394aea943ee75047" "checksum libloading 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9c3ad660d7cb8c5822cd83d10897b0f1f1526792737a179e73896152f85b88c2" -"checksum libp2p 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)" = "" -"checksum libp2p-core 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)" = "" -"checksum libp2p-dns 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)" = "" -"checksum libp2p-floodsub 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)" = "" -"checksum libp2p-identify 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)" = "" -"checksum libp2p-kad 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)" = "" -"checksum libp2p-mplex 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)" = "" -"checksum libp2p-peerstore 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)" = "" -"checksum libp2p-ping 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)" = "" -"checksum libp2p-ratelimit 0.1.1 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)" = "" -"checksum libp2p-relay 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)" = "" -"checksum libp2p-secio 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)" = "" -"checksum libp2p-tcp-transport 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)" = "" -"checksum libp2p-transport-timeout 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)" = "" -"checksum libp2p-uds 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)" = "" -"checksum libp2p-websocket 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)" = "" -"checksum libp2p-yamux 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)" = "" "checksum librocksdb-sys 5.17.2 (git+https://github.com/rust-rocksdb/rust-rocksdb?rev=1038c71df85a58b7f48894ca4cbf7c9228cb1cdf)" = "" "checksum libsqlite3-sys 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3567bc1a0c84e2c0d71eeb4a1f08451babf7843babd733158777d9c686dad9f3" "checksum linked-hash-map 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6d262045c5b87c0861b3f004610afd0e2c851e2908d08b6c870cbb9d5f494ecd" @@ -3759,7 +3143,6 @@ dependencies = [ "checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6" "checksum lru-cache 0.1.0 (git+https://github.com/nervosnetwork/lru-cache)" = "" "checksum lru-cache 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4d06ff7ff06f729ce5f4e227876cb88d10bc59cd4ae1e09fbb2bde15c850dc21" -"checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" "checksum memchr 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e1dd4eaac298c32ce07eb6ed9242eda7d82955b9170b7d6db59b2e02cc63fcb8" "checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3" "checksum merkle-cbt 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d589b5a7ca642540e7ccfbca3bcd0aa18693eb9287e2a6b17c79b1d062d52863" @@ -3768,14 +3151,9 @@ dependencies = [ "checksum mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)" = "71646331f2619b1026cc302f87a2b8b648d5c6dd6937846a16cc8ce0f347f432" "checksum mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "966257a94e196b11bb43aca423754d87429960a768de9414f3691d6957abf125" "checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" -"checksum multiaddr 0.3.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)" = "" -"checksum multihash 0.8.1-pre (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)" = "" -"checksum multistream-select 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)" = "" -"checksum native-tls 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f74dbadc8b43df7864539cedb7bc91345e532fdd913cfdc23ad94f4d2d40fbc0" "checksum net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "42550d9fb7b6684a6d404d9fa7250c2eb2646df731d1c06afc06dcee9e1bcf88" "checksum nix 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d37e713a259ff641624b6cb20e3b12b2952313ba36b6823c0f16e6cfd9e5de17" "checksum nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9667ddcc6cc8a43afc9b7917599d7216aa09c463919ea32c59ed6cac8bc945" -"checksum nohash-hasher 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0d138afcce92d219ccb6eb53d9b1e8a96ac0d633cfd3c53cd9856d96d1741bb8" "checksum nom 4.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b30adc557058ce00c9d0d7cb3c6e0b5bc6f36e2e2eabe74b0ba726d194abd588" "checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea" "checksum num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31" @@ -3785,16 +3163,12 @@ dependencies = [ "checksum numext-constructor 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0e938dadad7c0412ac4e2277846ba3b7241fae69c66e9b0389ea0fafa4be8e23" "checksum numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b58badd890ecf25ccef71682442c89ec2139772c5174782a0e9301a045648a3e" "checksum numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5dc24e4bed29ec6706d11ebe8ee5a77eb99de62b60e3af22ec704aae3f548c0d" -"checksum openssl 0.9.24 (registry+https://github.com/rust-lang/crates.io-index)" = "a3605c298474a3aa69de92d21139fb5e2a81688d308262359d85cdd0d12a7985" -"checksum openssl-sys 0.9.40 (registry+https://github.com/rust-lang/crates.io-index)" = "1bb974e77de925ef426b6bc82fce15fd45bdcbeb5728bffcfc7cdeeb7ce1c2d6" -"checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37" +"checksum opaque-debug 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "93f5bb2e8e8dec81642920ccff6b61f1eb94fa3020c5a325c9851ff604152409" "checksum owning_ref 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "49a4b8ea2179e6a2e27411d3bca09ca6dd630821cf6894c6c7c8467a8ee7ef13" -"checksum parking_lot 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "149d8f5b97f3c1133e3cfcd8886449959e856b557ff281e292b733d7c69e005e" -"checksum parking_lot 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d4d05f1349491390b1730afba60bb20d55761bef489a954546b58b4b34e1e2ac" -"checksum parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f0802bff09003b291ba756dc7e79313e51cc31667e94afbe847def490424cde5" +"checksum p2p 0.1.0 (git+https://github.com/nervosnetwork/p2p)" = "" +"checksum parity-multiaddr 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9a8e5d637787fe097ec1bfca2aa3eb687396518003df991c6c7216d86682d5ff" +"checksum parity-multihash 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3e8eab0287ccde7821e337a124dc5a4f1d6e4c25d10cc91e3f9361615dd95076" "checksum parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ab41b4aed082705d1056416ae4468b6ea99d52599ecf3169b00088d43113e337" -"checksum parking_lot_core 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "4db1a8ccf734a7bce794cc19b3df06ed87ab2f3907036b693c68f56b4d4537fa" -"checksum parking_lot_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad7f7e6ebdc79edff6fdcb87a55b620174f7a989e3eb31b65231f4af57f00b8c" "checksum parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94c8c7923936b28d546dfd14d4472eaf34c99b14e1c973a32b3e6d4eb04298c9" "checksum peeking_take_while 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" "checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" @@ -3802,8 +3176,6 @@ dependencies = [ "checksum plain 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" "checksum proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)" = "4d317f9caece796be1980837fd5cb3dfec5613ebdb04ad0956deea83ce168915" "checksum proptest 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea66c78d75f2c6e9f304269eaef90899798daecc69f1a625d5a3dd793ff3522" -"checksum protobuf 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d82d117bc7565ce6be0150159251c9b1eeec7b129f5a2aa86e10acb5970de1cb" -"checksum quick-error 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5fb6ccf8db7bbcb9c2eae558db5ab4f3da1c2a87e4e597ed394726bc8ea6ca1d" "checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0" "checksum quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "cdd8e04bd9c52e0342b406469d494fcb033be4bdbe5c606016defbb1681411e1" "checksum r2d2 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)" = "5d746fc8a0dab19ccea7ff73ad535854e90ddb3b4b8cdce953dd5cd0b2e7bd22" @@ -3831,29 +3203,24 @@ dependencies = [ "checksum regex-syntax 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8c2f35eedad5295fdf00a63d7d4b238135723f92b434ec06774dad15c7ab0861" "checksum relay 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1576e382688d7e9deecea24417e350d3062d97e32e45d70b1cde65994ff1489a" "checksum remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3488ba1b9a2084d38645c4c08276a1752dcbf2c7130d74f1569681ad5d2799c5" -"checksum ring 0.13.5 (registry+https://github.com/rust-lang/crates.io-index)" = "2c4db68a2e35f3497146b7e4563df7d4773a2433230c5e4b448328e31740458a" +"checksum ring 0.14.6 (registry+https://github.com/rust-lang/crates.io-index)" = "426bc186e3e95cac1e4a4be125a4aca7e84c2d616ffc02244eef36e2a60a093c" "checksum rocksdb 0.11.0 (git+https://github.com/rust-rocksdb/rust-rocksdb?rev=1038c71df85a58b7f48894ca4cbf7c9228cb1cdf)" = "" "checksum rusqlite 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6381ddfe91dbb659b4b132168da15985bc84162378cf4fcdc4eb99c857d063e2" -"checksum rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a" "checksum rust-ini 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3e52c148ef37f8c375d49d5a73aa70713125b7f19095948a923f80afdeb22ec2" "checksum rustc-demangle 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "adacaae16d02b6ec37fdc7acfcddf365978de76d1983d3ee22afc260e1ca9619" -"checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" "checksum rusty-fork 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9591f190d2852720b679c21f66ad929f9f1d7bb09d1193c26167586029d8489c" -"checksum rw-stream-sink 0.1.0 (git+https://github.com/nervosnetwork/rust-libp2p?branch=ring_patch)" = "" "checksum ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9e9b8cde282a9fe6a42dd4681319bfb63f121b8a8ee9439c6f4107e58a46f7" -"checksum safemem 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e27a8b19b835f7aea908818e871f5cc3a5a186550c30773be987e155e8163d8f" "checksum safemem 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8dca453248a96cb0749e36ccdfe2b0b4e54a61bfef89fb97ec621eb8e0a93dd9" "checksum same-file 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8f20c4be53a8a1ff4c1f1b2bd14570d2f634628709752f0702ecdd2b3f9a5267" -"checksum schannel 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "0e1a231dc10abf6749cfa5d7767f25888d484201accbd919b66ab5413c502d56" "checksum scheduled-thread-pool 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1a2ff3fc5223829be817806c6441279c676e454cc7da608faf03b0ccc09d3889" "checksum scoped-tls 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "332ffa32bf586782a3efaeb58f127980944bbc8c4d6913a86107ac2a5ab24b28" "checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" "checksum scroll 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2f84d114ef17fd144153d608fba7c446b0145d038985e7a8cc5d08bb0ce20383" "checksum scroll_derive 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8f1aa96c45e7f5a91cb7fabe7b279f02fea7126239fc40b732316e8b6a2d0fcb" +"checksum secio 0.1.0 (git+https://github.com/nervosnetwork/p2p)" = "" +"checksum secp256k1 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4070f3906e65249228094cf97b04a90799fba04468190bbbcfa812309cf86e32" "checksum secp256k1 0.9.1 (git+https://github.com/nervosnetwork/rust-secp256k1)" = "" -"checksum security-framework 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "dfa44ee9c54ce5eecc9de7d5acbad112ee58755239381f687e564004ba4a2332" -"checksum security-framework-sys 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "5421621e836278a0b139268f36eee0dc7e389b784dc3f79d8f11aabadf41bead" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" "checksum serde 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)" = "9dad3f759919b92c3068c696c15c3d17238234498bbdcc80f2c469606f948ac8" @@ -3863,25 +3230,23 @@ dependencies = [ "checksum serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)" = "bb6eabf4b5914e88e24eea240bb7c9f9a2cbc1bbbe8d961d381975ec3c6b806c" "checksum serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)" = "5a23aa71d4a4d43fdbfaac00eff68ba8a06a51759a89ac3304323e800c4dd40d" "checksum serde_test 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)" = "110b3dbdf8607ec493c22d5d947753282f3bae73c0f56d322af1e8c78e4c23d5" -"checksum sha1 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cc30b1e1e8c40c121ca33b86c23308a090d19974ef001b4bf6e61fd1a0fb095c" -"checksum sha1 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "171698ce4ec7cbb93babeb3190021b4d72e96ccb98e33d277ae4ea959d6f2d9e" -"checksum sha2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9eb6be24e4c23a84d7184280d2722f7f2731fcdd4a9d886efbfe4413e4847ea0" +"checksum sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" +"checksum sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7b4d8bfd0e469f417657573d8451fb33d16cfe0989359b93baf3a1ffc639543d" "checksum siphasher 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9913c75df657d84a03fa689c016b0bb2863ff0b497b26a8d6e9703f8d5df03a8" "checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23" "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" "checksum smallvec 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4c8cbcd6df1e117c2210e13ab5109635ad68a929fcbb8964dc965b76cb5ee013" -"checksum smallvec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1347484b6f8bc4b32a9323d9800b6d934376391002ad9c528cc659fe8afc08ee" "checksum smallvec 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)" = "88aea073965ab29f6edb5493faf96ad662fb18aa9eeb186a3b7057951605ed15" "checksum snap 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "95d697d63d44ad8b78b8d235bf85b34022a78af292c8918527c5f0cffdde7f43" "checksum spin 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "44363f6f51401c34e7be73db0db371c04705d35efbe9f7d6082e03a921a32c55" "checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8" -"checksum stdweb 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ef5430c8e36b713e13b48a9f709cc21e046723fe44ce34587b73a830203b533e" +"checksum stream-cipher 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8861bc80f649f5b4c9bd38b696ae9af74499d479dbfb327f0607de6b326a36bc" "checksum string 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "b639411d0b9c738748b5397d5ceba08e648f4f1992231aa859af1a017f31f60b" "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" +"checksum subtle 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2d67a5a62ba6e01cb2192ff309324cb4875d0c451d55fe2319433abe7a05a8ee" "checksum syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)" = "f92e629aa1d9c827b2bb8297046c1ccffc57c99b947a680d3ccff1f136a3bee9" "checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015" "checksum take 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b157868d8ac1f56b64604539990685fa7611d8fa9e5476cf0c02cf34d32917c5" -"checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" "checksum tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "b86c784c88d98c801132806dadd3819ed29d8600836c4088e855cdf3e178ed8a" "checksum termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4096add70612622289f2fdcdbd5086dc81c1e2675e6ae58d6c4f62a16c6d7f2f" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" @@ -3891,12 +3256,10 @@ dependencies = [ "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" "checksum tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e9175261fbdb60781fcd388a4d6cc7e14764a2b629a7ad94abb439aed223a44f" "checksum tinytemplate 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7655088894274afb52b807bd3c87072daa1fedd155068b8705cabfd628956115" -"checksum tk-listen 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5462b0f968c0457efe38fcd2df7e487096b992419e4f5337b06775a614bbda4b" "checksum tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "fcaabb3cec70485d0df6e9454fe514393ad1c4070dee8915f11041e95630b230" "checksum tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5c501eceaf96f0e1793cf26beb63da3d11c738c4a943fdf3746d81d64684c39f" "checksum tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)" = "aeeffbbb94209023feaef3c196a41cbcdafa06b4a6f893f68779bb5e53796f71" "checksum tokio-current-thread 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "c756b04680eea21902a46fca4e9f410a2332c04995af590e07ff262e2193a9a3" -"checksum tokio-dns-unofficial 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bb9bf62ca2c53bf2f2faec3e48a98b6d8c9577c27011cb0203a4beacdc8ab328" "checksum tokio-executor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "30c6dbf2d1ad1de300b393910e8a3aa272b724a400b6531da03eed99e329fbf0" "checksum tokio-fs 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "3fe6dc22b08d6993916647d108a1a7d15b9cd29c4f4496c62b92c45b5041b7af" "checksum tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "5090db468dad16e1a7a54c8c67280c5e4b544f3d3e018f0b913b400261f85926" @@ -3907,27 +3270,22 @@ dependencies = [ "checksum tokio-tcp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1d14b10654be682ac43efee27401d792507e30fd8d26389e1da3b185de2e4119" "checksum tokio-threadpool 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "742e511f6ce2298aeb86fc9ea0d8df81c2388c6ebae3dc8a7316e8c9df0df801" "checksum tokio-timer 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "2910970404ba6fa78c5539126a9ae2045d62e3713041e447f695f41405a120c6" -"checksum tokio-tls 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "772f4b04e560117fe3b0a53e490c16ddc8ba6ec437015d91fa385564996ed913" "checksum tokio-udp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "66268575b80f4a4a710ef83d087fdfeeabdce9b74c797535fbac18a2cb906e92" "checksum tokio-uds 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "037ffc3ba0e12a0ab4aca92e5234e0dedeb48fddf6ccd260f1f150a36a9f2445" +"checksum tokio-yamux 0.1.3 (git+https://github.com/nervosnetwork/p2p)" = "" "checksum toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "758664fc71a3a69038656bee8b6be6477d2a6c315a6b81f7081f591bffa4111f" -"checksum traitobject 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "efd1f82c56340fdf16f2a953d7bda4f8fdffba13d93b00844c25572110b26079" "checksum try-lock 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee2aa4715743892880f70885373966c83d73ef1b0838a664ef0c76fffd35e7c2" "checksum try-lock 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" -"checksum typeable 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1410f6f91f21d1612654e7cc69193b0334f909dcf2c790c4826254fbb86f8887" +"checksum twofish 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712d261e83e727c8e2dbb75dacac67c36e35db36a958ee504f2164fc052434e1" "checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169" "checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86" -"checksum unicase 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7f4765f83163b74f957c797ad9253caf97f103fb064d3999aea9568d09fc8a33" "checksum unicase 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9d3218ea14b4edcaccfa0df0a64a3792a2c32cc706f1b336e48867f9d3147f90" -"checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" -"checksum unicode-normalization 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "141339a08b982d942be2ca06ff8b076563cbe223d1befd5450716790d44e2426" "checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" "checksum unsigned-varint 0.2.2 (git+https://github.com/paritytech/unsigned-varint)" = "" "checksum unsigned-varint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2c64cdf40b4a9645534a943668681bcb219faf51874d4b65d2e0abda1b10a2ab" "checksum untrusted 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "55cd1f4b4e96b46aeb8d4855db4a7a9bd96eeeb5c6a1ab54593328761642ce2f" -"checksum url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a" "checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737" "checksum vcpkg 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "def296d3eb3b12371b2c7d0e83bfe1403e4db2d7a0bba324a12b21c4ee13143d" "checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" @@ -3937,7 +3295,6 @@ dependencies = [ "checksum walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9d9d7ed3431229a144296213105a390676cc49c9b6a72bd19f3176c98e129fa1" "checksum want 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a05d9d966753fa4b5c8db73fcab5eed4549cfe0e1e4e66911e5564a0085c35d1" "checksum want 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "797464475f30ddb8830cc529aaaae648d581f99e2036a928877dfde027ddf6b3" -"checksum websocket 0.20.3 (git+https://github.com/tomaka/rust-websocket?branch=send)" = "" "checksum which 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b57acb10231b9493c8472b20cb57317d0679a49e0bdbee44b3b803a6473af164" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" @@ -3948,4 +3305,3 @@ dependencies = [ "checksum wincolor 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "561ed901ae465d6185fa7864d63fbd5720d0ef718366c9a4dc83cf6170d7e9ba" "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" "checksum yaml-rust 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "95acf0db5515d07da9965ec0e0ba6cc2d825e2caeb7303b66ca441729801254e" -"checksum yamux 0.1.6 (git+https://github.com/paritytech/yamux)" = "" diff --git a/network/Cargo.toml b/network/Cargo.toml index 18f970b823..acd68e2bc9 100644 --- a/network/Cargo.toml +++ b/network/Cargo.toml @@ -20,11 +20,11 @@ bytes = "0.4.12" tokio = "0.1.16" futures = { version = "0.1.19", features = ["use_std"] } snap = "0.2" -libp2p = { git = "https://github.com/nervosnetwork/rust-libp2p", branch = "ring_patch", default-features = false, features = ["libp2p-secio", "libp2p-secio-secp256k1"] } +p2p = { git = "https://github.com/nervosnetwork/p2p", branch = "master" } +secio = { git = "https://github.com/nervosnetwork/p2p", branch = "master" } faketime = "0.2.0" rusqlite = {version = "0.16.0", features = ["bundled"]} lazy_static = "1.3.0" -multihash = { git = "https://github.com/nervosnetwork/rust-libp2p", branch = "ring_patch" } r2d2 = "0.8.3" r2d2_sqlite = "0.8.0" diff --git a/network/src/ckb_protocol.rs b/network/src/ckb_protocol.rs index 7567f18470..3eee1bc694 100644 --- a/network/src/ckb_protocol.rs +++ b/network/src/ckb_protocol.rs @@ -1,43 +1,45 @@ -#![allow(clippy::needless_pass_by_value)] - -use crate::{Error, ProtocolId}; +use crate::errors::{Error, ProtocolError}; use bytes::BufMut; use bytes::{Buf, IntoBuf}; use bytes::{Bytes, BytesMut}; use futures::sync::mpsc; use futures::{future, stream, Future, Sink, Stream}; -use libp2p::core::{ConnectionUpgrade, Endpoint, Multiaddr}; use log::{debug, error, trace}; -use snap; -use std::io; -use std::io::{Error as IoError, ErrorKind as IoErrorKind}; +use p2p::{ + context::{ServiceContext, SessionContext}, + multiaddr::Multiaddr, + traits::{ProtocolMeta, ServiceProtocol}, + PeerId, ProtocolId, SessionId, SessionType, +}; +use std::io::{self, Error as IoError, ErrorKind as IoErrorKind}; use std::string::ToString; use std::vec::IntoIter as VecIntoIter; use tokio::codec::Decoder; +use tokio::codec::LengthDelimitedCodec; use tokio::io::{AsyncRead, AsyncWrite}; -use unsigned_varint::codec::UviBytes; +use tokio::sync::mpsc::Sender; -pub type ProtocolVersion = u8; +pub type Version = u8; #[derive(Clone)] -pub struct CKBProtocol { +pub struct CKBProtocol { id: ProtocolId, // for example: b"/ckb/" base_name: Bytes, // supported version, used to check protocol version - supported_versions: Vec, - protocol_handler: T, + supported_versions: Vec, + event_sender: Sender, } -impl CKBProtocol { +impl CKBProtocol { pub fn new( base_name: String, - protocol_handler: T, id: ProtocolId, - versions: &[ProtocolVersion], + versions: &[Version], + event_sender: Sender, ) -> Self { let mut base_name_bytes = Bytes::from(format!("/{}/", base_name)); - base_name_bytes.extend_from_slice(&id); + base_name_bytes.extend_from_slice(format!("{}", id).as_bytes()); base_name_bytes.extend_from_slice(b"/"); CKBProtocol { base_name: base_name_bytes, @@ -47,12 +49,9 @@ impl CKBProtocol { versions.sort_by(|a, b| b.cmp(a)); versions.to_vec() }, - protocol_handler, + event_sender, } } - pub fn protocol_handler(&self) -> &T { - &self.protocol_handler - } pub fn id(&self) -> ProtocolId { self.id } @@ -61,239 +60,88 @@ impl CKBProtocol { } } -pub struct CKBProtocolOutput { - pub protocol_handler: T, - pub protocol_id: ProtocolId, - pub endpoint: Endpoint, - pub protocol_version: ProtocolVersion, - // channel to send outgoing messages - pub outgoing_msg_channel: mpsc::UnboundedSender, - // stream used to receive incoming messages - pub incoming_stream: Box + Send>, -} - -impl ConnectionUpgrade for CKBProtocol -where - C: AsyncRead + AsyncWrite + Send + 'static, - Maf: Future + Send + 'static, -{ - type NamesIter = VecIntoIter<(Bytes, Self::UpgradeIdentifier)>; - type UpgradeIdentifier = u8; - type Output = CKBProtocolOutput; - type MultiaddrFuture = Maf; - type Future = future::FutureResult<(Self::Output, Self::MultiaddrFuture), IoError>; - - fn protocol_names(&self) -> Self::NamesIter { - self.supported_versions - .iter() - .map(|version| { - let num = version.to_string(); - let mut name = self.base_name.clone(); - name.extend_from_slice(num.as_bytes()); - (name, *version) - }) - .collect::>() - .into_iter() +impl ProtocolMeta for CKBProtocol { + fn id(&self) -> ProtocolId { + self.id } - fn upgrade( - self, - socket: C, - protocol_version: Self::UpgradeIdentifier, - endpoint: Endpoint, - remote_addr: Maf, - ) -> Self::Future { - // This channel is used to send outgoing packets to the custom_data - // for this open substream. - let (incoming_stream, outgoing_msg_channel) = - match self.build_handling_stream_from_socket(socket, protocol_version) { - Ok(result) => result, - Err(err) => { - return { - error!(target: "network", "failed to upgrade ckb_protocol"); - future::err(IoError::new( - IoErrorKind::Other, - format!("faild to upgrade ckb_protocol, error: {}", err), - )) - }; - } - }; - - let out = CKBProtocolOutput { - protocol_handler: self.protocol_handler, - protocol_id: self.id, - endpoint, - protocol_version, - outgoing_msg_channel, - incoming_stream, - }; - trace!(target: "network", "success to upgrade ckb_protocol"); - - future::ok((out, remote_addr)) + fn codec(&self) -> LengthDelimitedCodec { + LengthDelimitedCodec::new() } -} - -impl CKBProtocol { - #[allow(clippy::type_complexity)] - fn build_handling_stream_from_socket( - &self, - socket: C, - _protocol_version: u8, - ) -> Result< - ( - Box + Send>, - mpsc::UnboundedSender, - ), - Error, - > - where - C: AsyncWrite + AsyncRead + Send + 'static, - { - let (msg_tx, msg_rx) = mpsc::unbounded(); - - // Build the sink for outgoing network bytes, and the stream for - // incoming instructions. `stream` implements `Stream`. - enum Message { - Recv(BytesMut), - SendData(Bytes), - Finished, - } - - let (sink, stream) = { - let framed = Decoder::framed(UviBytes::default(), socket); - let msg_rx = msg_rx.map(Message::SendData).map_err(|_err| { - IoError::new(IoErrorKind::Other, "error when read request from channel") - }); - let (sink, stream) = framed.split(); - let stream = stream - .map(Message::Recv) - .chain(stream::once(Ok(Message::Finished))); - (sink, msg_rx.select(stream)) - }; - let incoming = Box::new( - stream::unfold((sink, stream, false), move |(sink, stream, finished)| { - if finished { - return None; - } - - Some(stream.into_future().map_err(|(err, _)| err).and_then( - move |(message, stream)| match message { - Some(Message::Recv(compressed_data)) => { - if compressed_data.is_empty() { - debug!("receive a empty message, ignoring"); - let f = future::ok((None, (sink, stream, false))); - return future::Either::A(f); - } - // decompress data - let mut decompresser = - snap::Reader::new(compressed_data.freeze().into_buf().reader()); - let mut data = vec![].writer(); - match io::copy(&mut decompresser, &mut data) { - Ok(_) => { - let out = Some(data.into_inner().into()); - let f = future::ok((out, (sink, stream, false))); - future::Either::A(f) - } - Err(e) => future::Either::A(future::err(e)), - } - } - - Some(Message::SendData(data)) => { - let compressed_data = vec![].writer(); - let mut compresser = snap::Writer::new(compressed_data); - let data_buf = data.into_buf(); - match io::copy(&mut data_buf.reader(), &mut compresser) { - Ok(_) => match compresser.into_inner() { - Ok(compressed_data) => { - let compressed_data: Bytes = - compressed_data.into_inner().into(); - let fut = sink - .send(compressed_data) - .map(move |sink| (None, (sink, stream, false))); - future::Either::B(fut) - } - Err(e) => future::Either::A(future::err(IoError::new( - IoErrorKind::Other, - format!("compressed data error {}", e.to_string()), - ))), - }, - Err(e) => future::Either::A(future::err(IoError::new( - IoErrorKind::Other, - format!("error when receive data: {}", e), - ))), - } - } - - Some(Message::Finished) | None => { - let f = future::ok((None, (sink, stream, true))); - future::Either::A(f) - } - }, - )) - }) - .filter_map(|v| v), // filter will remove non Recv events - ) as Box + Send>; - Ok((incoming, msg_tx)) + fn service_handle(&self) -> Option> { + let handler = Box::new(CKBHandler { + id: self.id, + event_sender: self.event_sender.clone(), + }); + Some(handler) } -} -#[derive(Clone)] -pub struct CKBProtocols(pub Vec>); - -impl CKBProtocols { - pub fn find_protocol(&self, protocol_id: ProtocolId) -> Option<&CKBProtocol> { - self.0.iter().find(|protocol| protocol.id == protocol_id) + fn support_versions(&self) -> Vec { + self.supported_versions + .iter() + .map(|v| format!("{}", v)) + .collect() } } -impl Default for CKBProtocols { - fn default() -> Self { - CKBProtocols(Vec::new()) - } +pub enum Event { + Connected(PeerId, Multiaddr, ProtocolId, SessionType, Version), + ConnectedError(Multiaddr), + Disconnected(PeerId, ProtocolId), + Received(PeerId, ProtocolId, Vec), + Notify(ProtocolId, u64), } -impl ConnectionUpgrade for CKBProtocols -where - C: AsyncRead + AsyncWrite + Send + 'static, - Maf: Future + Send + 'static, -{ - type NamesIter = VecIntoIter<(Bytes, Self::UpgradeIdentifier)>; - type UpgradeIdentifier = ( - usize, - as ConnectionUpgrade>::UpgradeIdentifier, - ); +struct CKBHandler { + id: ProtocolId, + event_sender: Sender, +} - type Output = as ConnectionUpgrade>::Output; - type MultiaddrFuture = as ConnectionUpgrade>::MultiaddrFuture; - type Future = as ConnectionUpgrade>::Future; +impl ServiceProtocol for CKBHandler { + fn init(&mut self, _control: &mut ServiceContext) {} + fn connected( + &mut self, + _control: &mut ServiceContext, + session: &SessionContext, + version: &str, + ) { + let event = match session.remote_pubkey { + Some(ref pubkey) => { + let peer_id = pubkey.peer_id(); + Event::Connected( + peer_id, + session.address.clone(), + self.id, + session.ty, + version.parse::().expect("version"), + ) + } + None => Event::ConnectedError(session.address.clone()), + }; + self.event_sender.try_send(event); + } - fn protocol_names(&self) -> Self::NamesIter { - self.0 - .iter() - .enumerate() - .flat_map(|(n, proto)| { - ConnectionUpgrade::::protocol_names(proto) - .map(move |(name, id)| (name, (n, id))) - }) - .collect::>() - .into_iter() + fn disconnected(&mut self, _control: &mut ServiceContext, session: &SessionContext) { + let peer_id = session + .remote_pubkey + .as_ref() + .map(|pubkey| pubkey.peer_id()) + .expect("pubkey"); + self.event_sender + .try_send(Event::Disconnected(peer_id, self.id)); } - fn upgrade( - self, - socket: C, - upgrade_identifier: Self::UpgradeIdentifier, - endpoint: Endpoint, - remote_addr: Maf, - ) -> Self::Future { - let (protocol_index, inner_proto_id) = upgrade_identifier; - match self.0.into_iter().nth(protocol_index) { - Some(protocol) => protocol.upgrade(socket, inner_proto_id, endpoint, remote_addr), - None => future::err(IoError::new( - IoErrorKind::Other, - "cant't find ckb_protocol by index".to_string(), - )), - } + fn received(&mut self, control: &mut ServiceContext, session: &SessionContext, data: Vec) { + let peer_id = session + .remote_pubkey + .as_ref() + .map(|pubkey| pubkey.peer_id()) + .expect("pubkey"); + self.event_sender + .try_send(Event::Received(peer_id, self.id, data)); + } + fn notify(&mut self, control: &mut ServiceContext, token: u64) { + self.event_sender.try_send(Event::Notify(self.id, token)); } } diff --git a/network/src/ckb_protocol_handler.rs b/network/src/ckb_protocol_handler.rs index 5ccec469a0..47f9707fcc 100644 --- a/network/src/ckb_protocol_handler.rs +++ b/network/src/ckb_protocol_handler.rs @@ -1,6 +1,5 @@ -use crate::errors::{Error, ErrorKind}; -use crate::{Network, SessionInfo, Timer}; -use crate::{PeerIndex, ProtocolId, TimerToken}; +use crate::errors::{Error, PeerError, ProtocolError}; +use crate::{Network, PeerIndex, ProtocolId, SessionInfo, Timer, TimerRegistry, TimerToken}; use ckb_util::Mutex; use log::debug; use log::info; @@ -45,7 +44,7 @@ pub trait CKBProtocolContext: Send { pub(crate) struct DefaultCKBProtocolContext { pub protocol_id: ProtocolId, pub network: Arc, - pub timer_registry: Arc>>>, + pub timer_registry: TimerRegistry, } impl DefaultCKBProtocolContext { @@ -56,7 +55,7 @@ impl DefaultCKBProtocolContext { pub fn with_timer_registry( network: Arc, protocol_id: ProtocolId, - timer_registry: Arc>>>, + timer_registry: TimerRegistry, ) -> Self { DefaultCKBProtocolContext { network, @@ -79,7 +78,7 @@ impl CKBProtocolContext for DefaultCKBProtocolContext { if let Some(peer_id) = self.network.get_peer_id(peer_index) { self.network.send(&peer_id, protocol_id, data.into()) } else { - Err(ErrorKind::PeerNotFound.into()) + Err(Error::Peer(PeerError::IndexNotFound(peer_index))) } } // report peer behaviour @@ -102,18 +101,15 @@ impl CKBProtocolContext for DefaultCKBProtocolContext { } } fn register_timer(&self, token: TimerToken, duration: Duration) -> Result<(), Error> { - let handler = self + let (_, handler) = self .network - .ckb_protocols .find_protocol(self.protocol_id) - .ok_or(ErrorKind::BadProtocol)? - .protocol_handler() - .to_owned(); + .ok_or(ProtocolError::NotFound(self.protocol_id))?; match *self.timer_registry.lock() { Some(ref mut timer_registry) => { timer_registry.push((handler, self.protocol_id, token, duration)) } - None => return Err(ErrorKind::TimerRegisterNotAvailable.into()), + None => return Err(ProtocolError::DisallowRegisterTimer.into()), } Ok(()) } @@ -151,7 +147,7 @@ impl CKBProtocolContext for DefaultCKBProtocolContext { pub trait CKBProtocolHandler: Sync + Send { fn initialize(&self, _nc: Box); - fn received(&self, _nc: Box, _peer: PeerIndex, _data: &[u8]); + fn received(&self, _nc: Box, _peer: PeerIndex, _data: Vec); fn connected(&self, _nc: Box, _peer: PeerIndex); fn disconnected(&self, _nc: Box, _peer: PeerIndex); fn timer_triggered(&self, _nc: Box, _timer: TimerToken) {} diff --git a/network/src/ckb_service.rs b/network/src/ckb_service.rs index b0ae15e715..a6aa0aa25b 100644 --- a/network/src/ckb_service.rs +++ b/network/src/ckb_service.rs @@ -1,178 +1,133 @@ -#![allow(clippy::needless_pass_by_value)] - -use crate::ckb_protocol::CKBProtocolOutput; +use crate::ckb_protocol::CKBProtocol; +use crate::ckb_protocol::Event; use crate::ckb_protocol_handler::DefaultCKBProtocolContext; use crate::peer_store::{Behaviour, Status}; -use crate::protocol::Protocol; -use crate::protocol_service::ProtocolService; use crate::CKBProtocolHandler; use crate::Network; use crate::PeerId; use faketime::unix_time_as_millis; -use futures::future::{self, Future}; -use futures::Stream; -use libp2p::core::{Endpoint, Multiaddr, UniqueConnecState}; -use log::{error, info}; +use futures::{ + future::{self, Future}, + sync::mpsc::Receiver, + Async, Stream, +}; +use log::{debug, error, info, trace, warn}; +use p2p::{context::ServiceControl, multiaddr::Multiaddr, ProtocolId, SessionType}; use std::boxed::Box; use std::io::{Error as IoError, ErrorKind as IoErrorKind}; use std::sync::Arc; use tokio; -pub struct CKBService; +pub struct CKBService { + pub event_receiver: Receiver, + pub network: Arc, +} impl CKBService { - fn handle_protocol_connection( - network: Arc, - peer_id: PeerId, - protocol_output: CKBProtocolOutput>, - addr: Multiaddr, - ) -> Box + Send> { - let protocol_id = protocol_output.protocol_id; - let protocol_handler = protocol_output.protocol_handler; - let protocol_version = protocol_output.protocol_version; - let endpoint = protocol_output.endpoint; - // get peer protocol_connection - let protocol_connec = { - let result = match endpoint { - Endpoint::Dialer => { - network.try_outbound_ckb_protocol_connec(&peer_id, protocol_id, addr) - } - Endpoint::Listener => { - network.try_inbound_ckb_protocol_connec(&peer_id, protocol_id, addr) - } - }; - if let Err(err) = result { - return Box::new(future::err(IoError::new( - IoErrorKind::Other, - format!("handle ckb_protocol connection error: {}", err), - ))) as Box + Send>; - } - result.unwrap() - }; - if protocol_connec.state() == UniqueConnecState::Full { - error!( - target: "network", - "we already connected peer {:?} with {:?}, stop handling", - peer_id, protocol_id - ); - return Box::new(future::ok(())) as Box<_>; - } + fn find_handler(&self, protocol_id: ProtocolId) -> Option> { + self.network + .find_protocol(protocol_id) + .map(|(_, handler)| handler) + } +} - let peer_index = match network.get_peer_index(&peer_id) { - Some(peer_index) => peer_index, - None => { - return Box::new(future::err(IoError::new( - IoErrorKind::Other, - format!("can't find peer {:?}", peer_id), - ))); - } - }; +impl Stream for CKBService { + type Item = (); + type Error = (); + fn poll(&mut self) -> Result>, Self::Error> { + use crate::ckb_protocol::Event::*; - let protocol_future = { - let handling_future = protocol_output.incoming_stream.for_each({ - let network = Arc::clone(&network); - let protocol_handler = Arc::clone(&protocol_handler); - let peer_id = peer_id.clone(); - move |data| { - network.modify_peer(&peer_id, |peer| { - peer.last_message_time = Some(unix_time_as_millis()) - }); - let protocol_handler = Arc::clone(&protocol_handler); - let network = Arc::clone(&network); - let handle_received = future::lazy(move || { - protocol_handler.received( - Box::new(DefaultCKBProtocolContext::new(network, protocol_id)), - peer_index, - &data, - ); - Ok(()) - }); - tokio::spawn(handle_received); - Ok(()) - } - }); - protocol_connec - .tie_or_stop( - (protocol_output.outgoing_msg_channel, protocol_version), - handling_future, - ) - .then({ - let network = Arc::clone(&network); - let peer_id = peer_id.clone(); - let protocol_handler = Arc::clone(&protocol_handler); - let protocol_id = protocol_id; - move |val| { - info!( - target: "network", - "Disconnect! peer {:?} protocol_id {:?} reason {:?}", - peer_id, protocol_id, val - ); + let network = Arc::clone(&self.network); + match try_ready!(self.event_receiver.poll()) { + Some(Connected(peer_id, addr, protocol_id, session_type, version)) => { + let connect_result = match session_type { + SessionType::Client => { + network.new_outbound_connection(peer_id.clone(), addr.clone()) + } + SessionType::Server => { + network.new_inbound_connection(peer_id.clone(), addr.clone()) + } + }; + + match connect_result { + Ok(peer_index) => { + // update status in peer_store { let mut peer_store = network.peer_store().write(); - peer_store.report(&peer_id, Behaviour::UnexpectedDisconnect); - peer_store.update_status(&peer_id, Status::Disconnected); + peer_store.report(&peer_id, Behaviour::Connect); + peer_store.update_status(&peer_id, Status::Connected); + peer_store.add_discovered_address(&peer_id, addr); } - protocol_handler.disconnected( + // call handler + match self.find_handler(protocol_id) { + Some(handler) => handler.connected( + Box::new(DefaultCKBProtocolContext::new( + Arc::clone(&network), + protocol_id, + )), + peer_index, + ), + None => { + error!(target: "network", "can't find protocol handler for {}", protocol_id) + } + } + } + Err(err) => { + info!(target: "network", "reject connection from {} {}, because {}", peer_id.to_base58(), addr, err) + } + } + } + Some(ConnectedError(addr)) => { + error!(target: "network", "ckb protocol connected error, addr: {}", addr); + } + Some(Disconnected(peer_id, protocol_id)) => { + // update disconnect in peer_store + { + let mut peer_store = network.peer_store().write(); + peer_store.report(&peer_id, Behaviour::UnexpectedDisconnect); + peer_store.update_status(&peer_id, Status::Disconnected); + } + if let Some(peer_index) = network.get_peer_index(&peer_id) { + // call handler + match self.find_handler(protocol_id) { + Some(handler) => handler.disconnected( Box::new(DefaultCKBProtocolContext::new( Arc::clone(&network), protocol_id, )), peer_index, - ); - network.drop_peer(&peer_id); - val + ), + None => { + error!(target: "network", "can't find protocol handler for {}", protocol_id) + } } - }) - }; - - info!( - target: "network", - "Connected to peer {:?} with protocol_id {:?} version {}", - peer_id, protocol_id, protocol_version - ); - { - let mut peer_store = network.peer_store().write(); - peer_store.report(&peer_id, Behaviour::Connect); - peer_store.update_status(&peer_id, Status::Connected); - } - { - let handle_connected = future::lazy(move || { - protocol_handler.connected( - Box::new(DefaultCKBProtocolContext::new( - Arc::clone(&network), - protocol_id, - )), - peer_index, - ); - Ok(()) - }); - tokio::spawn(handle_connected); - } - Box::new(protocol_future) as Box<_> - } -} - -impl ProtocolService for CKBService { - type Output = CKBProtocolOutput>; - fn convert_to_protocol( - peer_id: Arc, - addr: &Multiaddr, - output: Self::Output, - ) -> Protocol { - Protocol::CKBProtocol(output, PeerId::clone(&peer_id), addr.to_owned()) - } - fn handle( - &self, - network: Arc, - protocol: Protocol, - ) -> Box + Send> { - match protocol { - Protocol::CKBProtocol(output, peer_id, addr) => { - let handling_future = - Self::handle_protocol_connection(network, peer_id, output, addr); - Box::new(handling_future) as Box + Send> + } + // disconnect + network.drop_peer(&peer_id); + } + Some(Received(peer_id, protocol_id, data)) => { + network.modify_peer(&peer_id, |peer| { + peer.last_message_time = Some(unix_time_as_millis()) + }); + let peer_index = network.get_peer_index(&peer_id).expect("peer_index"); + match self.find_handler(protocol_id) { + Some(handler) => handler.received( + Box::new(DefaultCKBProtocolContext::new(network, protocol_id)), + peer_index, + data, + ), + None => { + error!(target: "network", "can't find protocol handler for {}", protocol_id) + } + } + } + Some(Notify(protocol_id, token)) => { + debug!(target: "network", "receive ckb timer notify, protocol_id: {} token: {}", protocol_id, token); + } + None => { + error!(target: "network", "ckb service should not stop"); } - _ => Box::new(future::ok(())) as Box + Send>, } + Ok(Async::Ready(Some(()))) } } diff --git a/network/src/errors.rs b/network/src/errors.rs index 01b42d40f5..e6d10af34d 100644 --- a/network/src/errors.rs +++ b/network/src/errors.rs @@ -1,41 +1,63 @@ +use crate::{PeerIndex, ProtocolId}; +use p2p::PeerId; use std::error; use std::fmt; use std::fmt::Display; -use std::io::{Error as IoError, ErrorKind as IoErrorKind}; +use std::io::Error as IoError; #[derive(Debug)] -pub struct Error { - pub error_kind: ErrorKind, +pub enum Error { + Peer(PeerError), + Config(ConfigError), + Protocol(ProtocolError), + Io(IoError), + Shutdown, } #[derive(Debug)] -pub enum ErrorKind { - PeerNotFound, - InvalidNewPeer(String), - ParseAddress, - BadProtocol, - TimerRegisterNotAvailable, - Io(IoError), - Other(String), +pub enum ConfigError { + BadAddress, + InvalidKey, +} + +#[derive(Debug)] +pub enum PeerError { + IndexNotFound(PeerIndex), + ProtocolNotFound(PeerId, ProtocolId), + NotFound(PeerId), + NonReserved(PeerId), + Banned(PeerId), + ReachMaxInboundLimit(PeerId), + ReachMaxOutboundLimit(PeerId), } -impl From for Error { - fn from(e: ErrorKind) -> Error { - Error { error_kind: e } +#[derive(Debug)] +pub enum ProtocolError { + NotFound(ProtocolId), + DisallowRegisterTimer, +} + +impl From for Error { + fn from(err: PeerError) -> Error { + Error::Peer(err) } } impl From for Error { fn from(err: IoError) -> Error { - Error { - error_kind: ErrorKind::Io(err), - } + Error::Io(err) + } +} + +impl From for Error { + fn from(err: ConfigError) -> Error { + Error::Config(err) } } -impl Into for Error { - fn into(self: Error) -> IoError { - IoError::new(IoErrorKind::Other, self) +impl From for Error { + fn from(err: ProtocolError) -> Error { + Error::Protocol(err) } } @@ -47,7 +69,7 @@ impl Display for Error { impl error::Error for Error { fn description(&self) -> &str { - "general error in libp2p" + "ckb network error" } fn cause(&self) -> Option<&error::Error> { diff --git a/network/src/lib.rs b/network/src/lib.rs index df737ccf0b..32bf7d0adb 100644 --- a/network/src/lib.rs +++ b/network/src/lib.rs @@ -1,53 +1,40 @@ -#![type_length_limit = "2097152"] - +#[macro_use] +extern crate futures; mod ckb_protocol; mod ckb_protocol_handler; mod ckb_service; -mod errors; -mod identify_service; +pub mod errors; mod network; mod network_config; mod network_group; mod network_service; -mod outbound_peer_service; +//mod outbound_peer_service; pub mod peer_store; mod peers_registry; -mod ping_service; -mod protocol; -mod protocol_service; +//mod ping_service; #[cfg(test)] mod tests; mod timer_service; -mod transport; -pub use crate::ckb_protocol::{CKBProtocol, CKBProtocols}; +pub use crate::ckb_protocol::CKBProtocol; pub use crate::ckb_protocol_handler::{CKBProtocolContext, CKBProtocolHandler, Severity}; -pub use crate::errors::{Error, ErrorKind}; pub use crate::network::{Network, PeerInfo, SessionInfo}; pub use crate::network_config::NetworkConfig; pub use crate::network_service::NetworkService; -pub use libp2p::{ - core::Endpoint, multiaddr::AddrComponent, multiaddr::ToMultiaddr, Multiaddr, PeerId, -}; - -pub type TimerToken = usize; -pub type ProtocolId = [u8; 3]; - -use multihash::{encode, Hash}; -use rand::Rng; +pub use crate::timer_service::{Timer, TimerRegistry, TimerToken}; +pub use p2p::{multiaddr, PeerId, ProtocolId, SessionType}; use serde_derive::Deserialize; use std::sync::Arc; use std::time::Duration; const DEFAULT_OUTGOING_PEERS_RATIO: u32 = 3; -pub(crate) type Timer = (Arc, ProtocolId, TimerToken, Duration); // used in CKBProtocolContext pub type PeerIndex = usize; #[derive(Clone, Debug, PartialEq, Deserialize)] pub struct Config { - pub listen_addresses: Vec, + pub listen_addresses: Vec, pub secret_file: Option, pub nodes_file: Option, /// List of initial node addresses @@ -105,12 +92,8 @@ impl From for NetworkConfig { } } -pub fn random_peer_id() -> Result { - let mut seed: [u8; 32] = [0; 32]; - rand::thread_rng().fill(&mut seed); - let random_key = encode(Hash::SHA2256, &seed) - .expect("sha2256 encode") - .into_bytes(); - let peer_id = PeerId::from_bytes(random_key).expect("convert key to peer_id"); - Ok(peer_id) +pub fn random_peer_id() -> PeerId { + use p2p::SecioKeyPair; + let pubkey = SecioKeyPair::secp256k1_generated().to_public_key(); + PeerId::from_public_key(&pubkey) } diff --git a/network/src/network.rs b/network/src/network.rs index ee84293d09..c8184edc47 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -1,42 +1,47 @@ -#![allow(clippy::needless_pass_by_value)] - -use crate::ckb_protocol::{CKBProtocol, CKBProtocols}; +use crate::ckb_protocol::{CKBProtocol, Event as CKBEvent}; use crate::ckb_protocol_handler::CKBProtocolHandler; use crate::ckb_protocol_handler::DefaultCKBProtocolContext; use crate::ckb_service::CKBService; -use crate::identify_service::IdentifyService; -use crate::outbound_peer_service::OutboundPeerService; +//use crate::outbound_peer_service::OutboundPeerService; +use crate::errors::{ConfigError, Error, PeerError}; use crate::peer_store::{Behaviour, PeerStore, SqlitePeerStore}; -use crate::peers_registry::{ConnectionStatus, PeerConnection, PeerIdentifyInfo, PeersRegistry}; -use crate::ping_service::PingService; -use crate::protocol::Protocol; -use crate::protocol_service::ProtocolService; -use crate::timer_service::TimerService; -use crate::transport::{new_transport, TransportOutput}; +use crate::peers_registry::{ConnectionStatus, Peer, PeerIdentifyInfo, PeersRegistry}; +//use crate::ping_service::PingService; +use crate::timer_service::{TimerRegistry, TimerService}; use crate::NetworkConfig; -use crate::{Error, ErrorKind, PeerIndex, ProtocolId}; +use crate::{PeerIndex, ProtocolId}; use bytes::Bytes; use ckb_util::{Mutex, RwLock}; use fnv::FnvHashMap; use futures::future::{self, select_all, Future}; use futures::sync::mpsc::UnboundedSender; +use futures::sync::mpsc::{Receiver, Sender}; use futures::sync::oneshot; use futures::Stream; -use libp2p::core::{upgrade, MuxedTransport, PeerId}; -use libp2p::core::{Endpoint, Multiaddr, UniqueConnec}; -use libp2p::core::{PublicKey, SwarmController}; -use libp2p::{self, identify, ping, secio, Transport, TransportTimeout}; use log::{debug, info, trace, warn}; +use p2p::{ + builder::ServiceBuilder, + context::{ServiceContext, ServiceControl}, + multiaddr::Multiaddr, + service::{Service, ServiceError, ServiceEvent}, + traits::ServiceHandle, + PeerId, PublicKey, SessionType, +}; +use secio; use std::boxed::Box; +use std::cmp::max; use std::io::{Error as IoError, ErrorKind as IoErrorKind}; use std::sync::Arc; use std::time::Duration; use std::usize; +use tokio::codec::LengthDelimitedCodec; use tokio::io::{AsyncRead, AsyncWrite}; const DIAL_BOOTNODE_TIMEOUT: u64 = 20; const PEER_ADDRS_COUNT: u32 = 5; +pub type CKBProtocols = Vec<(CKBProtocol, Arc)>; + #[derive(Debug, Clone)] pub struct SessionInfo { pub peer: PeerInfo, @@ -46,7 +51,7 @@ pub struct SessionInfo { #[derive(Debug, Clone)] pub struct PeerInfo { pub peer_id: PeerId, - pub endpoint_role: Endpoint, + pub session_type: SessionType, pub last_ping_time: Option, pub connected_addr: Multiaddr, pub identify_info: Option, @@ -55,7 +60,7 @@ pub struct PeerInfo { impl PeerInfo { #[inline] pub fn is_outbound(&self) -> bool { - self.endpoint_role == Endpoint::Dialer + self.session_type == SessionType::Client } #[inline] @@ -64,17 +69,30 @@ impl PeerInfo { } } +type P2PService = Service; + pub struct Network { peers_registry: RwLock, peer_store: Arc>, listened_addresses: RwLock>, pub(crate) original_listened_addresses: RwLock>, - pub(crate) ckb_protocols: CKBProtocols>, + pub(crate) ckb_protocols: CKBProtocols, local_private_key: secio::SecioKeyPair, local_peer_id: PeerId, + p2p_control: RwLock, } impl Network { + pub fn find_protocol( + &self, + id: ProtocolId, + ) -> Option<(&CKBProtocol, Arc)> { + self.ckb_protocols + .iter() + .find(|(protocol, _)| protocol.id() == id) + .map(|(protocol, handler)| (protocol, Arc::clone(handler))) + } + pub fn report(&self, peer_id: &PeerId, behaviour: Behaviour) { self.peer_store.write().report(peer_id, behaviour); } @@ -104,9 +122,7 @@ impl Network { pub(crate) fn get_peer_index(&self, peer_id: &PeerId) -> Option { let peers_registry = self.peers_registry.read(); - peers_registry - .get(&peer_id) - .and_then(|peer| peer.peer_index) + peers_registry.get(&peer_id).map(|peer| peer.peer_index) } pub(crate) fn get_peer_id(&self, peer_index: PeerIndex) -> Option { @@ -123,7 +139,7 @@ impl Network { pub(crate) fn modify_peer(&self, peer_id: &PeerId, mut f: F) -> bool where - F: FnMut(&mut PeerConnection) -> (), + F: FnMut(&mut Peer) -> (), { let mut peers_registry = self.peers_registry.write(); match peers_registry.get_mut(peer_id) { @@ -157,13 +173,6 @@ impl Network { } } - pub(crate) fn get_peer_pinger(&self, peer_id: &PeerId) -> Option> { - let peers_registry = self.peers_registry.read(); - peers_registry - .get(peer_id) - .map(|peer| peer.pinger_loader.clone()) - } - pub(crate) fn get_peer_addresses(&self, peer_id: &PeerId) -> Vec { let peer_store = self.peer_store.read(); let addrs = peer_store.peer_addrs(&peer_id, PEER_ADDRS_COUNT); @@ -240,265 +249,96 @@ impl Network { protocol_id: ProtocolId, data: Bytes, ) -> Result<(), Error> { - if let Some(peer) = self.peers_registry.read().get(peer_id) { - if let Some(sender) = peer - .ckb_protocols - .iter() - .find(|(id, _)| id == &protocol_id) - .and_then(|(_, protocol_connec)| protocol_connec.poll()) - .map(|(sender, _)| sender) - { - sender.unbounded_send(data).map_err(|err| { - Error::from(ErrorKind::Other(format!("send to error: {:?}", err))) - })?; - Ok(()) - } else { - Err(ErrorKind::Other(format!( - "can't find protocol: {:?} for peer {:?}", - protocol_id, peer_id - )) - .into()) - } - } else { - Err(ErrorKind::PeerNotFound.into()) - } + self.peers_registry + .read() + .get(peer_id) + .map( + |peer| match peer.sessions.iter().find(|s| s.protocol_id == protocol_id) { + Some(session) => { + self.p2p_control.write().send_message( + Some(vec![session.id]), + session.protocol_id, + data.to_vec(), + ); + //sender.unbounded_send(data).map_err(|err| { + // Error::from(ErrorKind::Other(format!("send to error: {:?}", err))) + //})?; + Ok(()) + } + None => { + Err(PeerError::ProtocolNotFound(peer_id.to_owned(), protocol_id).into()) + } + }, + ) + .unwrap_or_else(|| Err(PeerError::NotFound(peer_id.to_owned()).into())) } - fn ckb_protocol_connec( - &self, - peer: &mut PeerConnection, - protocol_id: ProtocolId, - ) -> UniqueConnec<(UnboundedSender, u8)> { - peer.ckb_protocols - .iter() - .find(|&(id, _)| id == &protocol_id) - .map(|(_, ref protocol_connec)| protocol_connec.clone()) - .unwrap_or_else(|| { - let protocol_connec = UniqueConnec::empty(); - peer.ckb_protocols - .push((protocol_id, protocol_connec.clone())); - protocol_connec - }) - } - pub(crate) fn try_outbound_ckb_protocol_connec( + pub(crate) fn new_outbound_connection( &self, - peer_id: &PeerId, - protocol_id: ProtocolId, + peer_id: PeerId, connected_addr: Multiaddr, - ) -> Result, u8)>, Error> { - let mut peers_registry = self.peers_registry.write(); - // get peer protocol_connection - match peers_registry.try_outbound_peer(peer_id.clone(), connected_addr.clone()) { - Ok(_) => { - let _ = self - .peer_store() - .write() - .add_discovered_address(peer_id, connected_addr); - let peer = peers_registry.get_mut(&peer_id).unwrap(); - Ok(self.ckb_protocol_connec(peer, protocol_id)) - } - Err(err) => Err(err), - } + ) -> Result { + self.peers_registry + .write() + .try_outbound_peer(peer_id, connected_addr) } - pub(crate) fn try_inbound_ckb_protocol_connec( + pub(crate) fn new_inbound_connection( &self, - peer_id: &PeerId, - protocol_id: ProtocolId, + peer_id: PeerId, connected_addr: Multiaddr, - ) -> Result, u8)>, Error> { - let mut peers_registry = self.peers_registry.write(); - // get peer protocol_connection - match peers_registry.accept_inbound_peer(peer_id.clone(), connected_addr.clone()) { - Ok(_) => { - let _ = self - .peer_store() - .write() - .add_discovered_address(peer_id, connected_addr); - let peer = peers_registry.get_mut(&peer_id).unwrap(); - Ok(self.ckb_protocol_connec(peer, protocol_id)) - } - Err(err) => Err(err), - } + ) -> Result { + self.peers_registry + .write() + .accept_inbound_peer(peer_id, connected_addr) } pub fn peer_protocol_version(&self, peer_id: &PeerId, protocol_id: ProtocolId) -> Option { let peers_registry = self.peers_registry.read(); - match peers_registry.get(peer_id) { - Some(peer) => match peer.ckb_protocols.iter().find(|(id, _)| id == &protocol_id) { - Some((_, protocol_connec)) => protocol_connec.poll().map(|(_, version)| version), - None => None, - }, - None => None, - } + peers_registry.get(peer_id).and_then(|peer| { + peer.sessions + .iter() + .find(|s| s.protocol_id == protocol_id) + .map(|s| s.protocol_version) + }) } pub fn session_info(&self, peer_id: &PeerId, protocol_id: ProtocolId) -> Option { let peers_registry = self.peers_registry.read(); - match peers_registry.get(peer_id) { - Some(peer) => { - let protocol_version = - match peer.ckb_protocols.iter().find(|(id, _)| id == &protocol_id) { - Some((_, protocol_connec)) => { - protocol_connec.poll().map(|(_, version)| version) - } - None => None, - }; - let session = SessionInfo { - peer: PeerInfo { - peer_id: peer_id.to_owned(), - endpoint_role: peer.endpoint_role, - last_ping_time: peer.last_ping_time, - connected_addr: peer.connected_addr.clone(), - identify_info: peer.identify_info.clone(), - }, - protocol_version, - }; - Some(session) + peers_registry.get(peer_id).map(|peer| { + let protocol_version = peer + .sessions + .iter() + .find(|s| s.protocol_id == protocol_id) + .map(|s| s.protocol_version); + SessionInfo { + peer: PeerInfo { + peer_id: peer_id.to_owned(), + session_type: peer.session_type, + last_ping_time: peer.last_ping_time, + connected_addr: peer.connected_addr.clone(), + identify_info: peer.identify_info.clone(), + }, + protocol_version, } - None => None, - } + }) } - pub fn dial_to_peer( - &self, - transport: Tran, - addr: &Multiaddr, - expected_peer_id: &PeerId, - swarm_controller: &SwarmController + Send>>, - timeout: Duration, - ) where - Tran: MuxedTransport> + Send + Clone + 'static, - Tran::MultiaddrFuture: Send + 'static, - Tran::Dial: Send, - Tran::Listener: Send, - Tran::ListenerUpgrade: Send, - Tran::Incoming: Send, - Tran::IncomingUpgrade: Send, - To: AsyncRead + AsyncWrite + Send + 'static, - St: MuxedTransport> + Send + Clone + 'static, - St::Dial: Send, - St::MultiaddrFuture: Send, - St::Listener: Send, - St::ListenerUpgrade: Send, - St::Incoming: Send, - St::IncomingUpgrade: Send, - C: Send + 'static, - { + pub fn dial(&self, expected_peer_id: &PeerId, addr: Multiaddr) { if expected_peer_id == self.local_peer_id() { debug!(target: "network", "ignore dial to self"); return; } debug!(target: "network", "dial to peer {:?} address {:?}", expected_peer_id, addr); - for protocol in &self.ckb_protocols.0 { - self.dial_to_peer_protocol( - transport.clone(), - addr, - protocol.to_owned(), - expected_peer_id, - swarm_controller, - timeout, - ) - } - } - - fn dial_to_peer_protocol( - &self, - transport: Tran, - addr: &Multiaddr, - protocol: CKBProtocol>, - expected_peer_id: &PeerId, - swarm_controller: &SwarmController + Send>>, - timeout: Duration, - ) where - Tran: MuxedTransport> + Send + Clone + 'static, - Tran::MultiaddrFuture: Send + 'static, - Tran::Dial: Send, - Tran::Listener: Send, - Tran::ListenerUpgrade: Send, - Tran::Incoming: Send, - Tran::IncomingUpgrade: Send, - To: AsyncRead + AsyncWrite + Send + 'static, - St: MuxedTransport> + Send + Clone + 'static, - St::Dial: Send, - St::MultiaddrFuture: Send, - St::Listener: Send, - St::ListenerUpgrade: Send, - St::Incoming: Send, - St::IncomingUpgrade: Send, - C: Send + 'static, - { - trace!( - target: "network", - "prepare open protocol {:?} to {:?}", - protocol.base_name(), - addr - ); - - let protocol_id = protocol.id(); - let transport = transport.clone().and_then({ - let addr = addr.clone(); - move |out, endpoint, client_addr| { - let peer_id = out.peer_id; - upgrade::apply(out.socket, protocol, endpoint, client_addr).map( - move |(output, client_addr)| { - ( - ( - peer_id.clone(), - Protocol::CKBProtocol(output, peer_id, addr), - ), - client_addr, - ) - }, - ) - } - }); - - let transport = TransportTimeout::new(transport, timeout); - let unique_connec = match self.try_outbound_ckb_protocol_connec( - expected_peer_id, - protocol_id, - addr.to_owned(), - ) { - Ok(unique_connec) => unique_connec, - Err(_) => return, - }; - - let transport = transport.and_then({ - let expected_peer_id = expected_peer_id.clone(); - move |(peer_id, protocol), _, client_addr| { - if peer_id == expected_peer_id { - debug!(target: "network", "success connect to {:?}", peer_id); - future::ok((protocol, client_addr)) - } else { - debug!(target: "network", "connected peer id mismatch {:?}, disconnect!", peer_id); - //Because multiaddrs is responsed by a third-part node, the mismatched - //peer itself should not seems as a misbehaviour peer. - //So we do not report this behaviour - future::err(IoError::new( - IoErrorKind::ConnectionRefused, - "Peer id mismatch", - )) - } - } - }); - - trace!( - target: "network", - "Opening connection to {:?} addr {} with protocol {:?}", - expected_peer_id, - addr, - protocol_id - ); - let _ = unique_connec.dial(swarm_controller, addr, transport); + self.p2p_control.write().dial(addr); } pub(crate) fn inner_build( config: &NetworkConfig, - ckb_protocols: Vec>>, - ) -> Result, Error> { + ckb_protocols: CKBProtocols, + ) -> Result<(Arc, P2PService, TimerRegistry), Error> { let local_private_key = match config.fetch_private_key() { Some(private_key) => private_key?, - None => return Err(ErrorKind::Other("secret_key not set".to_owned()).into()), + None => return Err(ConfigError::InvalidKey.into()), }; // set max score to public addresses let listened_addresses: FnvHashMap = config @@ -526,140 +366,36 @@ impl Network { config.reserved_only, reserved_peers, ); + let mut p2p_service = ServiceBuilder::default().forever(true); + // register protocols + for (ckb_protocol, _) in &ckb_protocols { + p2p_service = p2p_service.insert_protocol(ckb_protocol.clone()); + } + let mut p2p_service = p2p_service.build(EventHandler {}); + let p2p_control = p2p_service.control().clone(); let network: Arc = Arc::new(Network { peers_registry: RwLock::new(peers_registry), peer_store: Arc::clone(&peer_store), listened_addresses: RwLock::new(listened_addresses), original_listened_addresses: RwLock::new(Vec::new()), - ckb_protocols: CKBProtocols(ckb_protocols), + ckb_protocols, local_private_key: local_private_key.clone(), - local_peer_id: local_private_key.to_peer_id(), + local_peer_id: local_private_key.to_public_key().peer_id(), + p2p_control: RwLock::new(p2p_control.clone()), }); - Ok(network) - } - pub(crate) fn build_network_future( - network: Arc, - config: &NetworkConfig, - close_rx: oneshot::Receiver<()>, - ) -> Result + Send>, Error> { - let local_private_key = network.local_private_key().to_owned(); - let local_peer_id: PeerId = local_private_key.to_peer_id(); - let basic_transport_timeout = config.transport_timeout; - let client_version = config.client_version.clone(); - let protocol_version = config.protocol_version.clone(); - let max_outbound = config.max_outbound_peers as usize; - let basic_transport = { - let basic_transport = new_transport(local_private_key, basic_transport_timeout) - .map_err_dial({ - move |err, addr| { - trace!(target: "network", "Failed to connect to peer {}, error: {:?}", addr, err); - err - } - }); - Transport::and_then(basic_transport, { - // Register new peers information - let local_peer_id = local_peer_id.clone(); - move |(peer_id, stream), _endpoint, remote_addr_fut| { - remote_addr_fut.and_then(move |remote_addr| { - debug!(target: "network", "connection from {:?} peer_id: {:?}", remote_addr, peer_id); - if peer_id == local_peer_id { - debug!(target: "network", "connect to self, disconnect"); - return Err(IoErrorKind::ConnectionRefused.into()); - } - let out = TransportOutput { - socket: stream, - peer_id, - original_addr: remote_addr.clone(), - }; - Ok((out, future::ok(remote_addr))) - }) - } - }) - }; - - // initialize ckb_protocols let timer_registry = Arc::new(Mutex::new(Some(Vec::new()))); - for protocol in &network.ckb_protocols.0 { - protocol.protocol_handler().initialize(Box::new( - DefaultCKBProtocolContext::with_timer_registry( - Arc::clone(&network), - protocol.id(), - Arc::clone(&timer_registry), - ), - )); - } - let ping_service = Arc::new(PingService::new(config.ping_interval, config.ping_timeout)); - let identify_service = Arc::new(IdentifyService { - client_version, - protocol_version, - identify_timeout: config.identify_timeout, - identify_interval: config.identify_interval, - }); - - let ckb_protocol_service = Arc::new(CKBService {}); - let timer_service = Arc::new(TimerService { - timer_registry: Arc::clone(&timer_registry), - }); - let outbound_peer_service = Arc::new(OutboundPeerService { - try_connect_interval: config.try_outbound_connect_interval, - timeout: config.try_outbound_connect_timeout, - }); // Transport used to handling received connections - let handling_transport = { - let transport = basic_transport.clone(); - transport.and_then({ - let network = Arc::clone(&network); - move |out, endpoint, fut| { - let peer_id = Arc::new(out.peer_id); - let original_addr = out.original_addr; - // upgrades and apply protocols - let ping_upgrade = upgrade::map_with_addr(libp2p::ping::Ping, { - let peer_id = Arc::clone(&peer_id); - move |out, addr| PingService::convert_to_protocol(peer_id, addr, out) - }); - let identify_upgrade = - upgrade::map_with_addr(identify::IdentifyProtocolConfig, { - let peer_id = Arc::clone(&peer_id); - let original_addr = original_addr.clone(); - move |out, _addr| { - IdentifyService::convert_to_protocol(peer_id, &original_addr, out) - } - }); - let ckb_protocols_upgrade = - upgrade::map_with_addr(network.ckb_protocols.clone(), { - let peer_id = Arc::clone(&peer_id); - move |out, addr| CKBService::convert_to_protocol(peer_id, addr, out) - }); - let all_upgrade = upgrade::or( - ckb_protocols_upgrade, - upgrade::or(identify_upgrade, ping_upgrade), - ); - upgrade::apply(out.socket, all_upgrade, endpoint, fut) - } - }) - }; - let (swarm_controller, swarm_events) = libp2p::core::swarm(handling_transport, { - let ping_service = Arc::clone(&ping_service); - let identify_service = Arc::clone(&identify_service); - let ckb_protocol_service = Arc::clone(&ckb_protocol_service); - let network = Arc::clone(&network); - move |protocol, _addr| match protocol { - Protocol::Ping(..) | Protocol::Pong(..) => ping_service - .handle(Arc::clone(&network), protocol) - as Box + Send>, - Protocol::IdentifyRequest(..) | Protocol::IdentifyResponse(..) => { - identify_service.handle(Arc::clone(&network), protocol) - } - Protocol::CKBProtocol(..) => { - ckb_protocol_service.handle(Arc::clone(&network), protocol) - } - } - }); - - // listen_on local addresses + for (protocol, handler) in &network.ckb_protocols { + handler.initialize(Box::new(DefaultCKBProtocolContext::with_timer_registry( + Arc::clone(&network), + protocol.id(), + Arc::clone(&timer_registry), + ))); + } + // listen local addresses for addr in &config.listen_addresses { - match swarm_controller.listen_on(addr.clone()) { + match p2p_service.listen(&addr) { Ok(listen_address) => { info!( target: "network", @@ -678,7 +414,8 @@ impl Network { addr.clone(), err ); - return Err(ErrorKind::Other(format!("listen address error: {:?}", err)).into()); + //return Err(ErrorKind::Other(format!("listen address error: {:?}", err)).into()); + return Err(Error::Io(err)); } }; } @@ -689,63 +426,92 @@ impl Network { let dial_timeout = Duration::from_secs(DIAL_BOOTNODE_TIMEOUT); // dial reserved_nodes for (peer_id, addr) in config.reserved_peers()? { - network.dial_to_peer( - basic_transport.clone(), - &addr, - &peer_id, - &swarm_controller, - dial_timeout, - ); + network.dial(&peer_id, addr); } let bootnodes = network .peer_store() .read() - .bootnodes((max_outbound / 2) as u32) + .bootnodes(max((config.max_outbound_peers / 2) as u32, 1)) .clone(); // dial half bootnodes for (peer_id, addr) in bootnodes { debug!(target: "network", "dial bootnode {:?} {:?}", peer_id, addr); - network.dial_to_peer( - basic_transport.clone(), - &addr, - &peer_id, - &swarm_controller, - dial_timeout, - ); + network.dial(&peer_id, addr); } } + Ok((network, p2p_service, timer_registry)) + } + + pub(crate) fn build_network_future( + network: Arc, + config: &NetworkConfig, + close_rx: oneshot::Receiver<()>, + p2p_service: P2PService, + timer_registry: TimerRegistry, + ckb_event_receiver: Receiver, + ) -> Result + Send>, Error> { + let local_private_key = network.local_private_key().to_owned(); + let local_peer_id: PeerId = local_private_key.to_peer_id(); + let basic_transport_timeout = config.transport_timeout; + let client_version = config.client_version.clone(); + let protocol_version = config.protocol_version.clone(); + let max_outbound = config.max_outbound_peers as usize; + + // initialize ckb_protocols + //let ping_service = Arc::new(PingService::new(config.ping_interval, config.ping_timeout)); + //let identify_service = Arc::new(IdentifyService { + // client_version, + // protocol_version, + // identify_timeout: config.identify_timeout, + // identify_interval: config.identify_interval, + //}); + + let ckb_service = CKBService { + event_receiver: ckb_event_receiver, + network: Arc::clone(&network), + }; + let timer_service = TimerService::new(timer_registry, Arc::clone(&network)); + //let outbound_peer_service = Arc::new(OutboundPeerService { + // try_connect_interval: config.try_outbound_connect_interval, + // timeout: config.try_outbound_connect_timeout, + //}); // prepare services futures - let futures: Vec + Send>> = vec![ - Box::new(swarm_events.for_each(|_| Ok(()))), + let futures: Vec + Send>> = vec![ + Box::new( + p2p_service + .for_each(|_| Ok(())) + .map_err(|err| Error::Shutdown), + ), + Box::new( + ckb_service + .for_each(|_| Ok(())) + .map_err(|err| Error::Shutdown), + ), // Box::new( // discovery_query_service // .into_future() // .map(|_| ()) // .map_err(|(err, _)| err), // ) as Box + Send>, - ping_service.start_protocol( - Arc::clone(&network), - swarm_controller.clone(), - basic_transport.clone(), - ), - identify_service.start_protocol( - Arc::clone(&network), - swarm_controller.clone(), - basic_transport.clone(), - ), - timer_service.start_protocol( - Arc::clone(&network), - swarm_controller.clone(), - basic_transport.clone(), - ), - outbound_peer_service.start_protocol( - Arc::clone(&network), - swarm_controller.clone(), - basic_transport.clone(), - ), - Box::new(close_rx.map_err(|err| IoError::new(IoErrorKind::Other, err))), + //ping_service.start_protocol( + // Arc::clone(&network), + // swarm_controller.clone(), + // basic_transport.clone(), + //), + //identify_service.start_protocol( + // Arc::clone(&network), + // swarm_controller.clone(), + // basic_transport.clone(), + //), + Box::new(timer_service.timer_futures.for_each(|_| Ok(()))), + //outbound_peer_service.start_protocol( + // Arc::clone(&network), + // swarm_controller.clone(), + // basic_transport.clone(), + //), + Box::new(close_rx.map_err(|err| Error::Shutdown)), ]; let service_futures = select_all(futures) .and_then({ @@ -762,25 +528,44 @@ impl Network { err }); let service_futures = - Box::new(service_futures) as Box + Send>; + Box::new(service_futures) as Box + Send>; Ok(service_futures) } - #[allow(clippy::type_complexity)] pub fn build( config: &NetworkConfig, - ckb_protocols: Vec>>, + ckb_protocols: CKBProtocols, + ckb_event_receiver: Receiver, ) -> Result< ( Arc, oneshot::Sender<()>, - Box + Send>, + Box + Send>, ), Error, > { - let network = Self::inner_build(config, ckb_protocols)?; + let (network, p2p_service, timer_registry) = Self::inner_build(config, ckb_protocols)?; let (close_tx, close_rx) = oneshot::channel(); - let network_future = Self::build_network_future(Arc::clone(&network), &config, close_rx)?; + let network_future = Self::build_network_future( + Arc::clone(&network), + &config, + close_rx, + p2p_service, + timer_registry, + ckb_event_receiver, + )?; Ok((network, close_tx, network_future)) } } + +pub struct EventHandler {} + +impl ServiceHandle for EventHandler { + fn handle_error(&mut self, _env: &mut ServiceContext, error: ServiceError) { + trace!(target: "network", "p2p service error: {:?}", error); + } + + fn handle_event(&mut self, _env: &mut ServiceContext, event: ServiceEvent) { + debug!(target: "network", "p2p service event: {:?}", event); + } +} diff --git a/network/src/network_config.rs b/network/src/network_config.rs index 283d379b53..e2b348145a 100644 --- a/network/src/network_config.rs +++ b/network/src/network_config.rs @@ -1,12 +1,11 @@ +use crate::errors::{ConfigError, Error}; use crate::PeerId; -use crate::{Error, ErrorKind}; use bytes::Bytes; -use libp2p::core::{AddrComponent, Multiaddr}; -use libp2p::multiaddr::ToMultiaddr; -use libp2p::secio; use log::info; +use p2p::multiaddr::{self, Multiaddr, Protocol, ToMultiaddr}; use rand; use rand::Rng; +use secio; use std::fs; use std::io::Read; use std::io::Write; @@ -63,17 +62,12 @@ impl NetworkConfig { Self::default() } - pub fn generate_random_key(&mut self) -> Result { + pub fn generate_random_key(&mut self) -> Result { info!(target: "network", "Generate random key"); let mut key: [u8; 32] = [0; 32]; rand::thread_rng().fill(&mut key); self.secret_key = Some(Bytes::from(key.to_vec())); - secio::SecioKeyPair::secp256k1_raw_key(&key).map_err(|err| { - IoError::new( - IoErrorKind::InvalidData, - format!("generate random key error: {:?}", err), - ) - }) + secio::SecioKeyPair::secp256k1_raw_key(&key).map_err(|err| ConfigError::InvalidKey.into()) } pub fn write_secret_key_to_file(&mut self) -> Result<(), IoError> { @@ -90,15 +84,11 @@ impl NetworkConfig { Ok(()) } - pub fn fetch_private_key(&self) -> Option> { + pub fn fetch_private_key(&self) -> Option> { if let Some(secret) = self.read_secret_key() { Some( - secio::SecioKeyPair::secp256k1_raw_key(&secret).map_err(|err| { - IoError::new( - IoErrorKind::InvalidData, - format!("fetch private_key error: {:?}", err), - ) - }), + secio::SecioKeyPair::secp256k1_raw_key(&secret) + .map_err(|err| ConfigError::InvalidKey.into()), ) } else { None @@ -110,12 +100,12 @@ impl NetworkConfig { for addr_str in &self.reserved_peers { let mut addr = addr_str .to_multiaddr() - .map_err(|_| ErrorKind::ParseAddress)?; + .map_err(|_| ConfigError::BadAddress)?; let peer_id = match addr.pop() { - Some(AddrComponent::P2P(key)) => { - PeerId::from_bytes(key.into_bytes()).map_err(|_| ErrorKind::ParseAddress)? + Some(Protocol::P2p(key)) => { + PeerId::from_bytes(key.into_bytes()).map_err(|_| ConfigError::BadAddress)? } - _ => return Err(ErrorKind::ParseAddress.into()), + _ => return Err(ConfigError::BadAddress.into()), }; peers.push((peer_id, addr)) } @@ -127,12 +117,12 @@ impl NetworkConfig { for addr_str in &self.bootnodes { let mut addr = addr_str .to_multiaddr() - .map_err(|_| ErrorKind::ParseAddress)?; + .map_err(|_| ConfigError::BadAddress)?; let peer_id = match addr.pop() { - Some(AddrComponent::P2P(key)) => { - PeerId::from_bytes(key.into_bytes()).map_err(|_| ErrorKind::ParseAddress)? + Some(Protocol::P2p(key)) => { + PeerId::from_bytes(key.into_bytes()).map_err(|_| ConfigError::BadAddress)? } - _ => return Err(ErrorKind::ParseAddress.into()), + _ => return Err(ConfigError::BadAddress.into()), }; peers.push((peer_id, addr)); } @@ -143,8 +133,8 @@ impl NetworkConfig { impl Default for NetworkConfig { fn default() -> Self { NetworkConfig { - listen_addresses: vec![iter::once(AddrComponent::IP4(Ipv4Addr::new(0, 0, 0, 0))) - .chain(iter::once(AddrComponent::TCP(30333))) + listen_addresses: vec![iter::once(Protocol::Ip4(Ipv4Addr::new(0, 0, 0, 0))) + .chain(iter::once(Protocol::Tcp(30333))) .collect()], public_addresses: Vec::new(), client_version: "ckb".to_owned(), diff --git a/network/src/network_group.rs b/network/src/network_group.rs index 93c1d1d688..a3b797c4fc 100644 --- a/network/src/network_group.rs +++ b/network/src/network_group.rs @@ -1,4 +1,4 @@ -use libp2p::core::{AddrComponent, Multiaddr}; +use p2p::multiaddr::{Multiaddr, Protocol}; use std::net::IpAddr; #[derive(Hash, Eq, PartialEq, Debug)] @@ -21,8 +21,8 @@ impl MultiaddrExt for Multiaddr { fn extract_ip_addr(&self) -> Option { for addr_component in self { match addr_component { - AddrComponent::IP4(ipv4) => return Some(IpAddr::V4(ipv4)), - AddrComponent::IP6(ipv6) => return Some(IpAddr::V6(ipv6)), + Protocol::Ip4(ipv4) => return Some(IpAddr::V4(ipv4)), + Protocol::Ip6(ipv6) => return Some(IpAddr::V6(ipv6)), _ => (), } } diff --git a/network/src/network_service.rs b/network/src/network_service.rs index c55ce62cc8..631fcc8c1e 100644 --- a/network/src/network_service.rs +++ b/network/src/network_service.rs @@ -1,11 +1,12 @@ -use crate::ckb_protocol::CKBProtocol; +use crate::ckb_protocol::{CKBProtocol, Event as CKBEvent}; use crate::ckb_protocol_handler::CKBProtocolHandler; use crate::ckb_protocol_handler::{CKBProtocolContext, DefaultCKBProtocolContext}; -use crate::network::Network; +use crate::network::{CKBProtocols, Network}; use crate::NetworkConfig; -use crate::{Error, ErrorKind, ProtocolId}; +use crate::{errors::Error, ProtocolId}; use ckb_util::Mutex; use futures::future::Future; +use futures::sync::mpsc::{Receiver, Sender}; use futures::sync::oneshot; use libp2p::{Multiaddr, PeerId}; use log::{debug, info}; @@ -52,7 +53,7 @@ impl NetworkService { where F: FnOnce(&CKBProtocolContext) -> T, { - match self.network.ckb_protocols.find_protocol(protocol_id) { + match self.network.find_protocol(protocol_id) { Some(_) => Some(f(&DefaultCKBProtocolContext::new( Arc::clone(&self.network), protocol_id, @@ -63,9 +64,10 @@ impl NetworkService { pub fn run_in_thread( config: &NetworkConfig, - ckb_protocols: Vec>>, + ckb_protocols: CKBProtocols, + ckb_event_receiver: Receiver, ) -> Result { - let network = Network::inner_build(config, ckb_protocols)?; + let (network, p2p_service, timer_registry) = Network::inner_build(config, ckb_protocols)?; let (close_tx, close_rx) = oneshot::channel(); let (init_tx, init_rx) = oneshot::channel(); let join_handle = thread::spawn({ @@ -75,10 +77,17 @@ impl NetworkService { info!( target: "network", "network peer_id {:?}", - network.local_public_key().clone().into_peer_id() + network.local_public_key().peer_id() ); - let network_future = - Network::build_network_future(network, &config, close_rx).unwrap(); + let network_future = Network::build_network_future( + network, + &config, + close_rx, + p2p_service, + timer_registry, + ckb_event_receiver, + ) + .unwrap(); init_tx.send(()).unwrap(); // here we use default config let network_runtime = runtime::Runtime::new().unwrap(); @@ -88,11 +97,7 @@ impl NetworkService { } } }); - init_rx.wait().map_err(|err| { - Error::from(ErrorKind::Other( - format!("initialize network service error: {}", err.to_string()).to_owned(), - )) - })?; + init_rx.wait().map_err(|err| Error::Shutdown)?; Ok(NetworkService { network, stop_handler: Mutex::new(Some(StopHandler::new(close_tx, join_handle))), diff --git a/network/src/peer_store.rs b/network/src/peer_store.rs index 3d30797745..a7e08a4ac6 100644 --- a/network/src/peer_store.rs +++ b/network/src/peer_store.rs @@ -7,7 +7,7 @@ pub(crate) mod sqlite_peer_store; use crate::PeerId; use fnv::FnvHashMap; -use libp2p::core::{Endpoint, Multiaddr}; +use p2p::{multiaddr::Multiaddr, SessionType}; use std::time::Duration; #[allow(dead_code)] @@ -108,7 +108,7 @@ impl Default for ScoringSchema { pub trait PeerStore: Send + Sync { // initial or update peer_info in peer_store - fn new_connected_peer(&mut self, peer_id: &PeerId, address: Multiaddr, endpoint: Endpoint); + fn new_connected_peer(&mut self, peer_id: &PeerId, address: Multiaddr, endpoint: SessionType); // add peer discovered addresses, return numbers of new inserted line, return Err if peer not exists fn add_discovered_address(&mut self, peer_id: &PeerId, address: Multiaddr) -> Result<(), ()>; fn add_discovered_addresses( diff --git a/network/src/peer_store/db.rs b/network/src/peer_store/db.rs index 37653ef2e1..b7a338a962 100644 --- a/network/src/peer_store/db.rs +++ b/network/src/peer_store/db.rs @@ -1,7 +1,7 @@ use super::{Multiaddr, PeerId, Score, Status}; use crate::network_group::{Group, NetworkGroup}; use crate::peer_store::sqlite::Error as SqliteError; -use libp2p::core::Endpoint; +use p2p::SessionType; use rusqlite::types::ToSql; use rusqlite::OptionalExtension; use rusqlite::{Connection, NO_PARAMS}; @@ -51,7 +51,7 @@ pub struct PeerInfo { pub connected_addr: Multiaddr, pub score: Score, pub status: Status, - pub endpoint: Endpoint, + pub endpoint: SessionType, pub ban_time: Duration, pub connected_time: Duration, } @@ -61,7 +61,7 @@ impl PeerInfo { conn: &Connection, peer_id: &PeerId, connected_addr: &Multiaddr, - endpoint: Endpoint, + endpoint: SessionType, score: Score, connected_time: Duration, ) -> DBResult { @@ -83,7 +83,7 @@ impl PeerInfo { conn: &Connection, id: u32, connected_addr: &Multiaddr, - endpoint: Endpoint, + endpoint: SessionType, connected_time: Duration, ) -> DBResult { let mut stmt = conn @@ -257,15 +257,15 @@ fn duration_to_secs(duration: Duration) -> u32 { duration.as_secs() as u32 } -fn endpoint_to_bool(endpoint: Endpoint) -> bool { - endpoint == Endpoint::Listener +fn endpoint_to_bool(endpoint: SessionType) -> bool { + endpoint == SessionType::Server } -fn bool_to_endpoint(is_inbound: bool) -> Endpoint { +fn bool_to_endpoint(is_inbound: bool) -> SessionType { if is_inbound { - Endpoint::Listener + SessionType::Server } else { - Endpoint::Dialer + SessionType::Client } } diff --git a/network/src/peer_store/sqlite_peer_store.rs b/network/src/peer_store/sqlite_peer_store.rs index ca77998f68..c165bb2f5b 100644 --- a/network/src/peer_store/sqlite_peer_store.rs +++ b/network/src/peer_store/sqlite_peer_store.rs @@ -4,8 +4,8 @@ use crate::peer_store::db; use crate::peer_store::sqlite::{self, ConnectionPool, ConnectionPoolExt}; use faketime::unix_time; use fnv::FnvHashMap; -use libp2p::core::Endpoint; use log::debug; +use p2p::SessionType; use std::net::IpAddr; use std::time::Duration; @@ -161,7 +161,7 @@ impl SqlitePeerStore { &mut self, peer_id: &PeerId, addr: &Multiaddr, - endpoint: Endpoint, + endpoint: SessionType, connected_time: Duration, ) -> db::PeerInfo { self.pool @@ -204,7 +204,7 @@ impl SqlitePeerStore { conn, peer_id, &addr, - Endpoint::Listener, + SessionType::Server, self.scoring_schema().peer_init_score(), now, )?; @@ -224,7 +224,7 @@ impl SqlitePeerStore { } impl PeerStore for SqlitePeerStore { - fn new_connected_peer(&mut self, peer_id: &PeerId, addr: Multiaddr, endpoint: Endpoint) { + fn new_connected_peer(&mut self, peer_id: &PeerId, addr: Multiaddr, endpoint: SessionType) { if self.check_store_limit().is_err() { return; } @@ -311,7 +311,7 @@ impl PeerStore for SqlitePeerStore { } fn add_bootnode(&mut self, peer_id: PeerId, addr: Multiaddr) { - self.new_connected_peer(&peer_id, addr.clone(), Endpoint::Dialer); + self.new_connected_peer(&peer_id, addr.clone(), SessionType::Client); self.bootnodes.push((peer_id, addr)); } // should return high scored nodes if possible, otherwise, return boostrap nodes diff --git a/network/src/peers_registry.rs b/network/src/peers_registry.rs index da9657bea3..c4a00aa33c 100644 --- a/network/src/peers_registry.rs +++ b/network/src/peers_registry.rs @@ -1,14 +1,20 @@ +use crate::ckb_protocol::Version; use crate::network_group::{Group, NetworkGroup}; use crate::peer_store::PeerStore; -use crate::{Error, ErrorKind, PeerId, PeerIndex, ProtocolId}; +use crate::{ + errors::{Error, PeerError}, + PeerId, PeerIndex, ProtocolId, +}; use bytes::Bytes; use ckb_util::RwLock; use faketime::unix_time_as_millis; use fnv::{FnvHashMap, FnvHashSet}; use futures::sync::mpsc::UnboundedSender; -use libp2p::core::{Endpoint, Multiaddr, UniqueConnec}; -use libp2p::ping; use log::debug; +pub use p2p::{ + multiaddr::{Multiaddr, ToMultiaddr}, + SessionId, SessionType, +}; use rand::seq::SliceRandom; use rand::thread_rng; use std::sync::atomic::{AtomicUsize, Ordering}; @@ -16,15 +22,15 @@ use std::sync::Arc; pub(crate) const EVICTION_PROTECT_PEERS: usize = 8; -struct PeerConnections { +struct PeerManage { id_allocator: AtomicUsize, - peers: FnvHashMap, + peers: FnvHashMap, pub(crate) peer_id_by_index: FnvHashMap, } -impl PeerConnections { +impl PeerManage { #[inline] - fn get(&self, peer_id: &PeerId) -> Option<&PeerConnection> { + fn get(&self, peer_id: &PeerId) -> Option<&Peer> { self.peers.get(peer_id) } @@ -34,34 +40,43 @@ impl PeerConnections { } #[inline] - fn get_mut(&mut self, peer_id: &PeerId) -> Option<&mut PeerConnection> { + fn get_mut(&mut self, peer_id: &PeerId) -> Option<&mut Peer> { self.peers.get_mut(peer_id) } #[inline] - fn remove(&mut self, peer_id: &PeerId) -> Option { + fn remove(&mut self, peer_id: &PeerId) -> Option { if let Some(peer) = self.peers.remove(peer_id) { - self.peer_id_by_index.remove(&peer.peer_index.unwrap()); + self.peer_id_by_index.remove(&peer.peer_index); return Some(peer); } None } #[inline] - fn iter(&self) -> impl Iterator { + fn iter(&self) -> impl Iterator { self.peers.iter() } #[inline] - fn or_insert(&mut self, peer_id: PeerId, peer: PeerConnection) -> PeerIndex { - let mut peer = peer; - let peer_index = match peer.peer_index { - Some(peer_index) => peer_index, - None => { - let id = self.id_allocator.fetch_add(1, Ordering::Relaxed); - peer.peer_index = Some(id); - id - } + fn add_peer( + &mut self, + peer_id: PeerId, + connected_addr: Multiaddr, + session_type: SessionType, + ) -> PeerIndex { + let peer_index = self.id_allocator.fetch_add(1, Ordering::Relaxed); + let peer = Peer { + session_type, + connected_addr, + identify_info: None, + ping: None, + last_ping_time: None, + last_message_time: None, + connected_time: unix_time_as_millis(), + peer_index: peer_index, + sessions: Vec::with_capacity(1), }; + self.peers.entry(peer_id.clone()).or_insert(peer); self.peer_id_by_index.entry(peer_index).or_insert(peer_id); peer_index @@ -74,9 +89,9 @@ impl PeerConnections { } } -impl Default for PeerConnections { +impl Default for PeerManage { fn default() -> Self { - PeerConnections { + PeerManage { id_allocator: AtomicUsize::new(0), peers: FnvHashMap::with_capacity_and_hasher(20, Default::default()), peer_id_by_index: FnvHashMap::with_capacity_and_hasher(20, Default::default()), @@ -92,42 +107,29 @@ pub struct PeerIdentifyInfo { pub count_of_known_listen_addrs: usize, } -type ProtocolConnec = (ProtocolId, UniqueConnec<(UnboundedSender, u8)>); +pub struct Session { + pub id: SessionId, + pub protocol_id: ProtocolId, + pub protocol_version: Version, +} -pub struct PeerConnection { - pub(crate) peer_index: Option, +pub struct Peer { + pub(crate) peer_index: PeerIndex, pub connected_addr: Multiaddr, - // Dialer or Listener - pub endpoint_role: Endpoint, - // Used for send ping to peer - pub(crate) pinger_loader: UniqueConnec, + // Client or Server + pub session_type: SessionType, pub identify_info: Option, - pub(crate) ckb_protocols: Vec, pub last_ping_time: Option, pub last_message_time: Option, pub ping: Option, - pub connected_time: Option, + pub connected_time: u64, + pub sessions: Vec, } -impl PeerConnection { - pub fn new(connected_addr: Multiaddr, endpoint_role: Endpoint) -> Self { - PeerConnection { - endpoint_role, - connected_addr, - pinger_loader: UniqueConnec::empty(), - identify_info: None, - ckb_protocols: Vec::with_capacity(1), - ping: None, - last_ping_time: None, - last_message_time: None, - connected_time: None, - peer_index: None, - } - } - +impl Peer { #[inline] pub fn is_outbound(&self) -> bool { - self.endpoint_role == Endpoint::Dialer + self.session_type == SessionType::Client } #[allow(dead_code)] @@ -154,7 +156,7 @@ pub struct ConnectionStatus { pub(crate) struct PeersRegistry { // store all known peers peer_store: Arc>, - peers: PeerConnections, + peers: PeerManage, // max inbound limitation max_inbound: u32, // max outbound limitation @@ -165,7 +167,7 @@ pub(crate) struct PeersRegistry { } fn find_most_peers_in_same_network_group<'a>( - peers: impl Iterator, + peers: impl Iterator, ) -> Vec<&'a PeerId> { peers .fold( @@ -224,22 +226,20 @@ impl PeersRegistry { self.reserved_peers.contains(&peer_id) } - pub fn accept_inbound_peer(&mut self, peer_id: PeerId, addr: Multiaddr) -> Result<(), Error> { - if self.peers.get(&peer_id).is_some() { - return Ok(()); + pub fn accept_inbound_peer( + &mut self, + peer_id: PeerId, + addr: Multiaddr, + ) -> Result { + if let Some(peer) = self.peers.get(&peer_id) { + return Ok(peer.peer_index); } if !self.is_reserved(&peer_id) { if self.reserved_only { - return Err(ErrorKind::InvalidNewPeer(format!( - "We are in reserved_only mode, rejected non-reserved peer {:?}", - peer_id - )) - .into()); + return Err(Error::Peer(PeerError::NonReserved(peer_id))); } if self.peer_store.read().is_banned(&peer_id) { - return Err( - ErrorKind::InvalidNewPeer(format!("peer {:?} is denied", peer_id)).into(), - ); + return Err(Error::Peer(PeerError::Banned(peer_id))); } let connection_status = self.connection_status(); @@ -247,15 +247,10 @@ impl PeersRegistry { if connection_status.unreserved_inbound >= self.max_inbound && !self.try_evict_inbound_peer() { - return Err(ErrorKind::InvalidNewPeer(format!( - "reach max inbound peers limitation, reject peer {:?}", - peer_id - )) - .into()); + return Err(Error::Peer(PeerError::ReachMaxInboundLimit(peer_id))); } } - self.new_peer(peer_id, addr, Endpoint::Listener); - Ok(()) + Ok(self.new_peer(peer_id, addr, SessionType::Server)) } fn try_evict_inbound_peer(&mut self) -> bool { @@ -299,23 +294,12 @@ impl PeersRegistry { peer1_last_message_time.cmp(&peer2_last_message_time) }, ); - candidate_peers.sort_by(|(_, peer1), (_, peer2)| { - let peer1_last_connected_at = peer1.connected_time.unwrap_or_else(|| std::u64::MAX); - let peer2_last_connected_at = peer2.connected_time.unwrap_or_else(|| std::u64::MAX); - peer2_last_connected_at.cmp(&peer1_last_connected_at) - }); // Protect half peers which have the longest connection time let protect_peers = candidate_peers.len() / 2; sort_then_drop_last_n_elements( &mut candidate_peers, protect_peers, - |(_, peer1), (_, peer2)| { - let peer1_last_connected_at = - peer1.connected_time.unwrap_or_else(|| std::u64::MAX); - let peer2_last_connected_at = - peer2.connected_time.unwrap_or_else(|| std::u64::MAX); - peer2_last_connected_at.cmp(&peer1_last_connected_at) - }, + |(_, peer1), (_, peer2)| peer2.connected_time.cmp(&peer1.connected_time), ); let mut evict_group = @@ -336,68 +320,57 @@ impl PeersRegistry { true } - pub fn try_outbound_peer(&mut self, peer_id: PeerId, addr: Multiaddr) -> Result<(), Error> { - if self.peers.get(&peer_id).is_some() { - return Ok(()); + pub fn try_outbound_peer( + &mut self, + peer_id: PeerId, + addr: Multiaddr, + ) -> Result { + if let Some(peer) = self.peers.get(&peer_id) { + return Ok(peer.peer_index); } if !self.is_reserved(&peer_id) { if self.reserved_only { - return Err(ErrorKind::InvalidNewPeer(format!( - "We are in reserved_only mode, rejected non-reserved peer {:?}", - peer_id - )) - .into()); + return Err(Error::Peer(PeerError::NonReserved(peer_id))); } if self.peer_store.read().is_banned(&peer_id) { - return Err( - ErrorKind::InvalidNewPeer(format!("peer {:?} is denied", peer_id)).into(), - ); + return Err(Error::Peer(PeerError::Banned(peer_id))); } let connection_status = self.connection_status(); // check peers connection limitation // TODO: implement extra outbound peer logic if connection_status.unreserved_outbound >= self.max_outbound { - return Err(ErrorKind::InvalidNewPeer(format!( - "reach max outbound peers limitation, reject peer {:?}", - peer_id - )) - .into()); + return Err(Error::Peer(PeerError::ReachMaxOutboundLimit(peer_id))); } } - self.new_peer(peer_id, addr, Endpoint::Dialer); - Ok(()) + Ok(self.new_peer(peer_id, addr, SessionType::Client)) } // registry a new peer - #[allow(clippy::needless_pass_by_value)] fn new_peer( &mut self, peer_id: PeerId, connected_addr: Multiaddr, - endpoint: Endpoint, + session_type: SessionType, ) -> PeerIndex { self.peer_store .write() - .new_connected_peer(&peer_id, connected_addr.clone(), endpoint); - let mut peer = PeerConnection::new(connected_addr, endpoint); - peer.connected_time = Some(unix_time_as_millis()); - let peer_index = self.peers.or_insert(peer_id.clone(), peer); - debug!(target: "network", "allocate peer_index {} to peer {:?}", peer_index, peer_id); + .new_connected_peer(&peer_id, connected_addr.clone(), session_type); + let peer_index = self.peers.add_peer(peer_id, connected_addr, session_type); peer_index } #[inline] - pub fn peers_iter(&self) -> impl Iterator { + pub fn peers_iter(&self) -> impl Iterator { self.peers.iter() } #[inline] - pub fn get(&self, peer_id: &PeerId) -> Option<&PeerConnection> { + pub fn get(&self, peer_id: &PeerId) -> Option<&Peer> { self.peers.get(peer_id) } #[inline] - pub fn get_mut(&mut self, peer_id: &PeerId) -> Option<&mut PeerConnection> { + pub fn get_mut(&mut self, peer_id: &PeerId) -> Option<&mut Peer> { self.peers.get_mut(peer_id) } diff --git a/network/src/protocol.rs b/network/src/protocol.rs index a9c1214e45..8733ecfb82 100644 --- a/network/src/protocol.rs +++ b/network/src/protocol.rs @@ -1,7 +1,7 @@ use crate::ckb_protocol::CKBProtocolOutput; use crate::CKBProtocolHandler; use futures::Future; -use libp2p::core::{Multiaddr, PeerId}; +use p2p::{multiaddr::Multiaddr, PeerId}; use libp2p::identify::{IdentifyInfo, IdentifySender}; use libp2p::ping; use std::io::Error as IoError; diff --git a/network/src/tests/peers_registry.rs b/network/src/tests/peers_registry.rs index 14121cabde..2607b8fedc 100644 --- a/network/src/tests/peers_registry.rs +++ b/network/src/tests/peers_registry.rs @@ -1,7 +1,8 @@ use crate::{ + multiaddr::ToMultiaddr, peer_store::{Behaviour, PeerStore, SqlitePeerStore}, peers_registry::{PeersRegistry, EVICTION_PROTECT_PEERS}, - random_peer_id, ToMultiaddr, + random_peer_id, }; use ckb_util::RwLock; use faketime::unix_time_as_millis; @@ -14,7 +15,7 @@ fn new_peer_store() -> impl PeerStore { #[test] fn test_accept_inbound_peer_in_reserve_only_mode() { let peer_store: Arc> = Arc::new(RwLock::new(new_peer_store())); - let reserved_peer = random_peer_id().unwrap(); + let reserved_peer = random_peer_id(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); // reserved_only mode: only accept reserved_peer @@ -26,7 +27,7 @@ fn test_accept_inbound_peer_in_reserve_only_mode() { vec![reserved_peer.clone()], ); assert!(peers_registry - .accept_inbound_peer(random_peer_id().unwrap(), addr.clone()) + .accept_inbound_peer(random_peer_id(), addr.clone()) .is_err()); peers_registry .accept_inbound_peer(reserved_peer.clone(), addr.clone()) @@ -36,7 +37,7 @@ fn test_accept_inbound_peer_in_reserve_only_mode() { #[test] fn test_accept_inbound_peer_until_full() { let peer_store: Arc> = Arc::new(RwLock::new(new_peer_store())); - let reserved_peer = random_peer_id().unwrap(); + let reserved_peer = random_peer_id(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); // accept node until inbound connections is full let mut peers_registry = PeersRegistry::new( @@ -47,16 +48,16 @@ fn test_accept_inbound_peer_until_full() { vec![reserved_peer.clone()], ); peers_registry - .accept_inbound_peer(random_peer_id().unwrap(), addr.clone()) + .accept_inbound_peer(random_peer_id(), addr.clone()) .expect("accept"); peers_registry - .accept_inbound_peer(random_peer_id().unwrap(), addr.clone()) + .accept_inbound_peer(random_peer_id(), addr.clone()) .expect("accept"); peers_registry - .accept_inbound_peer(random_peer_id().unwrap(), addr.clone()) + .accept_inbound_peer(random_peer_id(), addr.clone()) .expect("accept"); assert!(peers_registry - .accept_inbound_peer(random_peer_id().unwrap(), addr.clone()) + .accept_inbound_peer(random_peer_id(), addr.clone()) .is_err(),); // should still accept reserved peer peers_registry @@ -64,7 +65,7 @@ fn test_accept_inbound_peer_until_full() { .expect("accept"); // should refuse accept low score peer assert!(peers_registry - .accept_inbound_peer(random_peer_id().unwrap(), addr.clone()) + .accept_inbound_peer(random_peer_id(), addr.clone()) .is_err()); } @@ -75,9 +76,9 @@ fn test_accept_inbound_peer_eviction() { // 2. should never evict reserved peer // 3. should evict lowest scored peer let peer_store: Arc> = Arc::new(RwLock::new(new_peer_store())); - let reserved_peer = random_peer_id().unwrap(); - let evict_target = random_peer_id().unwrap(); - let lowest_score_peer = random_peer_id().unwrap(); + let reserved_peer = random_peer_id(); + let evict_target = random_peer_id(); + let lowest_score_peer = random_peer_id(); let addr1 = "/ip4/127.0.0.1".to_multiaddr().unwrap(); let addr2 = "/ip4/192.168.0.1".to_multiaddr().unwrap(); // prepare protected peers @@ -92,7 +93,7 @@ fn test_accept_inbound_peer_eviction() { ); for _ in 0..protected_peers_count { assert!(peers_registry - .accept_inbound_peer(random_peer_id().unwrap(), addr2.clone()) + .accept_inbound_peer(random_peer_id(), addr2.clone()) .is_ok()); } let mut peers_iter = peers_registry @@ -126,11 +127,9 @@ fn test_accept_inbound_peer_eviction() { for _ in 0..longest_connection_time_peers_count { let peer_id = peers_iter.next().unwrap(); let mut peer = peers_registry.get_mut(&peer_id).unwrap(); - peer.connected_time = Some(now.saturating_sub(10000)); + peer.connected_time = now.saturating_sub(10000); } - let mut new_peer_ids = (0..3) - .map(|_| random_peer_id().unwrap()) - .collect::>(); + let mut new_peer_ids = (0..3).map(|_| random_peer_id()).collect::>(); // setup 3 node and 1 reserved node from addr1 peers_registry .accept_inbound_peer(reserved_peer.clone(), addr1.clone()) @@ -170,12 +169,12 @@ fn test_accept_inbound_peer_eviction() { for peer_id in new_peer_ids { let mut peer = peers_registry.get_mut(&peer_id).unwrap(); // push the connected_time to make sure peer is unprotect - peer.connected_time = Some(now + 10000); + peer.connected_time = now + 10000; } // should evict evict target assert!(peers_registry.get(&evict_target).is_some()); peers_registry - .accept_inbound_peer(random_peer_id().unwrap(), addr1.clone()) + .accept_inbound_peer(random_peer_id(), addr1.clone()) .expect("accept"); assert!(peers_registry.get(&evict_target).is_none()); } diff --git a/network/src/tests/sqlite_peer_store.rs b/network/src/tests/sqlite_peer_store.rs index 452e917e99..2fe6aabbab 100644 --- a/network/src/tests/sqlite_peer_store.rs +++ b/network/src/tests/sqlite_peer_store.rs @@ -1,18 +1,19 @@ use crate::{ + multiaddr::ToMultiaddr, peer_store::{ sqlite_peer_store::{PEER_NOT_SEEN_TIMEOUT_SECS, PEER_STORE_LIMIT}, Behaviour, PeerStore, SqlitePeerStore, Status, }, - random_peer_id, Endpoint, ToMultiaddr, + random_peer_id, SessionType, }; use std::time::Duration; #[test] fn test_new_connected_peer() { let mut peer_store: Box = Box::new(SqlitePeerStore::temp()); - let peer_id = random_peer_id().unwrap(); + let peer_id = random_peer_id(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); - peer_store.new_connected_peer(&peer_id, addr, Endpoint::Dialer); + peer_store.new_connected_peer(&peer_id, addr, SessionType::Client); assert_eq!( peer_store.peer_score(&peer_id).unwrap(), peer_store.scoring_schema().peer_init_score() @@ -23,7 +24,7 @@ fn test_new_connected_peer() { #[test] fn test_add_discovered_address() { let mut peer_store: Box = Box::new(SqlitePeerStore::temp()); - let peer_id = random_peer_id().unwrap(); + let peer_id = random_peer_id(); peer_store .add_discovered_address(&peer_id, "/ip4/127.0.0.1".to_multiaddr().unwrap()) .expect("add discovered address"); @@ -44,7 +45,7 @@ fn test_add_discovered_address() { #[test] fn test_report() { let mut peer_store: Box = Box::new(SqlitePeerStore::temp()); - let peer_id = random_peer_id().unwrap(); + let peer_id = random_peer_id(); assert!(peer_store.report(&peer_id, Behaviour::Ping).is_ok()); assert!( peer_store.peer_score_or_default(&peer_id) > peer_store.scoring_schema().peer_init_score() @@ -54,11 +55,11 @@ fn test_report() { #[test] fn test_update_status() { let mut peer_store: Box = Box::new(SqlitePeerStore::temp()); - let peer_id = random_peer_id().unwrap(); + let peer_id = random_peer_id(); peer_store.update_status(&peer_id, Status::Connected); assert_eq!(peer_store.peer_status(&peer_id), Status::Unknown); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); - peer_store.new_connected_peer(&peer_id, addr, Endpoint::Listener); + peer_store.new_connected_peer(&peer_id, addr, SessionType::Server); peer_store.update_status(&peer_id, Status::Connected); assert_eq!(peer_store.peer_status(&peer_id), Status::Connected); } @@ -66,11 +67,11 @@ fn test_update_status() { #[test] fn test_ban_peer() { let mut peer_store: Box = Box::new(SqlitePeerStore::temp()); - let peer_id = random_peer_id().unwrap(); + let peer_id = random_peer_id(); peer_store.ban_peer(&peer_id, Duration::from_secs(10)); assert!(!peer_store.is_banned(&peer_id)); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); - peer_store.new_connected_peer(&peer_id, addr, Endpoint::Listener); + peer_store.new_connected_peer(&peer_id, addr, SessionType::Server); peer_store.ban_peer(&peer_id, Duration::from_secs(10)); assert!(peer_store.is_banned(&peer_id)); } @@ -79,11 +80,11 @@ fn test_ban_peer() { fn test_bootnodes() { let mut peer_store: Box = Box::new(SqlitePeerStore::temp()); assert!(peer_store.bootnodes(1).is_empty()); - let peer_id = random_peer_id().unwrap(); + let peer_id = random_peer_id(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); peer_store.add_bootnode(peer_id.clone(), addr.clone()); assert_eq!(peer_store.bootnodes(2).len(), 1); - let peer_id2 = random_peer_id().unwrap(); + let peer_id2 = random_peer_id(); peer_store .add_discovered_address(&peer_id2, addr.clone()) .expect("add discovered address"); @@ -97,11 +98,11 @@ fn test_bootnodes() { fn test_peers_to_attempt() { let mut peer_store: Box = Box::new(SqlitePeerStore::temp()); assert!(peer_store.peers_to_attempt(1).is_empty()); - let peer_id = random_peer_id().unwrap(); + let peer_id = random_peer_id(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); peer_store.add_bootnode(peer_id.clone(), addr.clone()); assert!(peer_store.peers_to_attempt(1).is_empty()); - let peer_id2 = random_peer_id().unwrap(); + let peer_id2 = random_peer_id(); peer_store .add_discovered_address(&peer_id2, addr.clone()) .expect("add discovered address"); @@ -116,14 +117,10 @@ fn test_delete_peer_info() { let addr1 = "/ip4/127.0.0.1".to_multiaddr().unwrap(); let addr2 = "/ip4/192.163.1.1".to_multiaddr().unwrap(); for _ in 0..(PEER_STORE_LIMIT - 2) { - peer_store.new_connected_peer( - &random_peer_id().unwrap(), - addr1.clone(), - Endpoint::Listener, - ); + peer_store.new_connected_peer(&random_peer_id(), addr1.clone(), SessionType::Server); } - let evict_target = random_peer_id().unwrap(); - let fake_target = random_peer_id().unwrap(); + let evict_target = random_peer_id(); + let fake_target = random_peer_id(); { // make sure these 2 peers become candidate in eviction let recent_not_seen_time = @@ -131,8 +128,8 @@ fn test_delete_peer_info() { let faketime_file = faketime::millis_tempfile(recent_not_seen_time.as_secs() * 1000) .expect("create faketime file"); faketime::enable(&faketime_file); - peer_store.new_connected_peer(&evict_target, addr1.clone(), Endpoint::Listener); - peer_store.new_connected_peer(&fake_target, addr2, Endpoint::Listener); + peer_store.new_connected_peer(&evict_target, addr1.clone(), SessionType::Server); + peer_store.new_connected_peer(&fake_target, addr2, SessionType::Server); } peer_store.report(&evict_target, Behaviour::FailedToPing); peer_store.report(&fake_target, Behaviour::FailedToPing); @@ -143,7 +140,7 @@ fn test_delete_peer_info() { < peer_store.scoring_schema().peer_init_score() ); // should evict evict_target and accept this - peer_store.new_connected_peer(&random_peer_id().unwrap(), addr1, Endpoint::Listener); + peer_store.new_connected_peer(&random_peer_id(), addr1, SessionType::Server); // evict_target is evicted in previous step assert_eq!( peer_store.peer_score_or_default(&evict_target), diff --git a/network/src/timer_service.rs b/network/src/timer_service.rs index 100b9abae7..fb6e11f1f8 100644 --- a/network/src/timer_service.rs +++ b/network/src/timer_service.rs @@ -1,127 +1,74 @@ use crate::ckb_protocol_handler::DefaultCKBProtocolContext; -use crate::protocol::Protocol; -use crate::protocol_service::ProtocolService; -use crate::transport::TransportOutput; +use crate::errors::Error; use crate::Network; use crate::PeerId; -use crate::{CKBProtocolHandler, ProtocolId, TimerToken}; +use crate::{CKBProtocolHandler, ProtocolId}; use ckb_util::Mutex; use futures::future::{self, Future}; use futures::stream::FuturesUnordered; +use futures::IntoFuture; use futures::Stream; -use libp2p::core::Multiaddr; -use libp2p::core::{MuxedTransport, SwarmController}; use log::trace; +use p2p::multiaddr::Multiaddr; use std::boxed::Box; use std::io::{Error as IoError, ErrorKind as IoErrorKind}; use std::sync::Arc; use std::time::Duration; use tokio; use tokio::io::{AsyncRead, AsyncWrite}; -use tokio::timer::Interval; +use tokio::timer::{Error as TimerError, Interval}; -pub(crate) type Timer = (Arc, ProtocolId, TimerToken, Duration); +pub type TimerToken = usize; +pub type Timer = (Arc, ProtocolId, TimerToken, Duration); +pub type TimerRegistry = Arc>>>; +type TimerFutures = FuturesUnordered + Send>>; pub struct TimerService { - pub timer_registry: Arc>>>, + pub timer_futures: TimerFutures, } -impl ProtocolService for TimerService { - type Output = (); - fn convert_to_protocol( - _peer_id: Arc, - _addr: &Multiaddr, - _output: Self::Output, - ) -> Protocol { - unreachable!() - } - fn handle( - &self, - _network: Arc, - _protocol: Protocol, - ) -> Box + Send> { - unreachable!() - } - - fn start_protocol( - &self, - network: Arc, - _swarm_controller: SwarmController< - SwarmTran, - Box + Send>, - >, - _transport: Tran, - ) -> Box + Send> - where - SwarmTran: MuxedTransport> + Clone + Send + 'static, - SwarmTran::MultiaddrFuture: Send + 'static, - SwarmTran::Dial: Send, - SwarmTran::Listener: Send, - SwarmTran::ListenerUpgrade: Send, - SwarmTran::Incoming: Send, - SwarmTran::IncomingUpgrade: Send, - Tran: MuxedTransport> + Clone + Send + 'static, - Tran::MultiaddrFuture: Send + 'static, - Tran::Dial: Send, - Tran::Listener: Send, - Tran::ListenerUpgrade: Send, - Tran::Incoming: Send, - Tran::IncomingUpgrade: Send, - TranOut: AsyncRead + AsyncWrite + Send + 'static, - { - Box::new(future::lazy({ - let network = Arc::clone(&network); - let timer_registry = Arc::clone(&self.timer_registry); - move || -> Box + Send> { - let timer_registry = timer_registry.lock().take().unwrap(); - let mut timer_futures = FuturesUnordered::new(); - trace!(target: "network", "start timer service, timer count: {}", timer_registry.len()); - // register timers - for (handler, protocol_id, timer_symbol, duration) in timer_registry { - trace!( - target: "network", - "register timer: timer_symbol {} protocol {:?} duration: {:?}", - timer_symbol, - protocol_id, - duration - ); - let timer_interval = Interval::new_interval(duration); - let timer_future = Box::new( - timer_interval - .for_each({ - let network = Arc::clone(&network); - move |_| { - let network = Arc::clone(&network); - let handler = Arc::clone(&handler); - let handle_timer = future::lazy(move || { - handler.timer_triggered( - Box::new(DefaultCKBProtocolContext::new( - Arc::clone(&network), - protocol_id, - )), - timer_symbol, - ); - Ok(()) - }); - tokio::spawn(handle_timer); - Ok(()) - } - }) - .map_err(|err| IoError::new(IoErrorKind::Other, err)), - ); - timer_futures.push(timer_future); - } - if timer_futures.is_empty() { - Box::new(future::empty()) as Box + Send> - } else { - Box::new( - timer_futures - .into_future() - .map(|_| ()) - .map_err(|(err, _)| err), - ) as Box + Send> - } +impl TimerService { + pub fn new(timer_registry: TimerRegistry, network: Arc) -> Self { + let mut timer_futures: TimerFutures = FuturesUnordered::new(); + if let Some(timers) = timer_registry.lock().take() { + trace!(target: "network", "start timer service, timer count: {}", timers.len()); + // register timers + for (handler, protocol_id, timer_symbol, duration) in timers { + trace!( + target: "network", + "register timer: timer_symbol {} protocol {:?} duration: {:?}", + timer_symbol, + protocol_id, + duration + ); + let timer_interval = Interval::new_interval(duration); + let timer_future = Box::new(timer_interval.for_each({ + let network = Arc::clone(&network); + move |_| { + let network = Arc::clone(&network); + let handler = Arc::clone(&handler); + let handle_timer = future::lazy(move || { + handler.timer_triggered( + Box::new(DefaultCKBProtocolContext::new( + Arc::clone(&network), + protocol_id, + )), + timer_symbol, + ); + Ok(()) + }); + tokio::spawn(handle_timer); + Ok(()) + } + })); + timer_futures.push(Box::new(timer_future.into_future().map_err(|err| { + Error::Io(IoError::new( + IoErrorKind::Other, + format!("timer error : {}", err), + )) + }))); } - })) as Box + Send> + } + TimerService { timer_futures } } } From 5c96f5e1ef038b280f88b56dffcb8659065b8018 Mon Sep 17 00:00:00 2001 From: jjy Date: Mon, 25 Feb 2019 16:50:45 +0800 Subject: [PATCH 02/99] refactor: reorganize ckb-network modules --- network/src/ckb_protocol.rs | 147 --------------- network/src/lib.rs | 13 +- network/src/network.rs | 16 +- network/src/network_service.rs | 8 +- network/src/peers_registry.rs | 2 +- network/src/protocol.rs | 169 +++++++++++++++--- ...rotocol_handler.rs => protocol_handler.rs} | 0 network/src/protocol_service.rs | 55 ------ network/src/service.rs | 4 + network/src/{ => service}/ckb_service.rs | 12 +- network/src/{ => service}/identify_service.rs | 0 .../{ => service}/outbound_peer_service.rs | 0 network/src/{ => service}/ping_service.rs | 0 network/src/{ => service}/timer_service.rs | 2 +- network/src/transport.rs | 36 ---- 15 files changed, 172 insertions(+), 292 deletions(-) delete mode 100644 network/src/ckb_protocol.rs rename network/src/{ckb_protocol_handler.rs => protocol_handler.rs} (100%) delete mode 100644 network/src/protocol_service.rs create mode 100644 network/src/service.rs rename network/src/{ => service}/ckb_service.rs (94%) rename network/src/{ => service}/identify_service.rs (100%) rename network/src/{ => service}/outbound_peer_service.rs (100%) rename network/src/{ => service}/ping_service.rs (100%) rename network/src/{ => service}/timer_service.rs (97%) delete mode 100644 network/src/transport.rs diff --git a/network/src/ckb_protocol.rs b/network/src/ckb_protocol.rs deleted file mode 100644 index 3eee1bc694..0000000000 --- a/network/src/ckb_protocol.rs +++ /dev/null @@ -1,147 +0,0 @@ -use crate::errors::{Error, ProtocolError}; -use bytes::BufMut; -use bytes::{Buf, IntoBuf}; -use bytes::{Bytes, BytesMut}; -use futures::sync::mpsc; -use futures::{future, stream, Future, Sink, Stream}; -use log::{debug, error, trace}; -use p2p::{ - context::{ServiceContext, SessionContext}, - multiaddr::Multiaddr, - traits::{ProtocolMeta, ServiceProtocol}, - PeerId, ProtocolId, SessionId, SessionType, -}; -use std::io::{self, Error as IoError, ErrorKind as IoErrorKind}; -use std::string::ToString; -use std::vec::IntoIter as VecIntoIter; -use tokio::codec::Decoder; -use tokio::codec::LengthDelimitedCodec; -use tokio::io::{AsyncRead, AsyncWrite}; -use tokio::sync::mpsc::Sender; - -pub type Version = u8; - -#[derive(Clone)] -pub struct CKBProtocol { - id: ProtocolId, - // for example: b"/ckb/" - base_name: Bytes, - // supported version, used to check protocol version - supported_versions: Vec, - event_sender: Sender, -} - -impl CKBProtocol { - pub fn new( - base_name: String, - id: ProtocolId, - versions: &[Version], - event_sender: Sender, - ) -> Self { - let mut base_name_bytes = Bytes::from(format!("/{}/", base_name)); - base_name_bytes.extend_from_slice(format!("{}", id).as_bytes()); - base_name_bytes.extend_from_slice(b"/"); - CKBProtocol { - base_name: base_name_bytes, - id, - supported_versions: { - let mut versions: Vec<_> = versions.to_vec(); - versions.sort_by(|a, b| b.cmp(a)); - versions.to_vec() - }, - event_sender, - } - } - pub fn id(&self) -> ProtocolId { - self.id - } - pub fn base_name(&self) -> Bytes { - self.base_name.clone() - } -} - -impl ProtocolMeta for CKBProtocol { - fn id(&self) -> ProtocolId { - self.id - } - - fn codec(&self) -> LengthDelimitedCodec { - LengthDelimitedCodec::new() - } - - fn service_handle(&self) -> Option> { - let handler = Box::new(CKBHandler { - id: self.id, - event_sender: self.event_sender.clone(), - }); - Some(handler) - } - - fn support_versions(&self) -> Vec { - self.supported_versions - .iter() - .map(|v| format!("{}", v)) - .collect() - } -} - -pub enum Event { - Connected(PeerId, Multiaddr, ProtocolId, SessionType, Version), - ConnectedError(Multiaddr), - Disconnected(PeerId, ProtocolId), - Received(PeerId, ProtocolId, Vec), - Notify(ProtocolId, u64), -} - -struct CKBHandler { - id: ProtocolId, - event_sender: Sender, -} - -impl ServiceProtocol for CKBHandler { - fn init(&mut self, _control: &mut ServiceContext) {} - fn connected( - &mut self, - _control: &mut ServiceContext, - session: &SessionContext, - version: &str, - ) { - let event = match session.remote_pubkey { - Some(ref pubkey) => { - let peer_id = pubkey.peer_id(); - Event::Connected( - peer_id, - session.address.clone(), - self.id, - session.ty, - version.parse::().expect("version"), - ) - } - None => Event::ConnectedError(session.address.clone()), - }; - self.event_sender.try_send(event); - } - - fn disconnected(&mut self, _control: &mut ServiceContext, session: &SessionContext) { - let peer_id = session - .remote_pubkey - .as_ref() - .map(|pubkey| pubkey.peer_id()) - .expect("pubkey"); - self.event_sender - .try_send(Event::Disconnected(peer_id, self.id)); - } - - fn received(&mut self, control: &mut ServiceContext, session: &SessionContext, data: Vec) { - let peer_id = session - .remote_pubkey - .as_ref() - .map(|pubkey| pubkey.peer_id()) - .expect("pubkey"); - self.event_sender - .try_send(Event::Received(peer_id, self.id, data)); - } - fn notify(&mut self, control: &mut ServiceContext, token: u64) { - self.event_sender.try_send(Event::Notify(self.id, token)); - } -} diff --git a/network/src/lib.rs b/network/src/lib.rs index 32bf7d0adb..a9694b2802 100644 --- a/network/src/lib.rs +++ b/network/src/lib.rs @@ -1,27 +1,26 @@ #[macro_use] extern crate futures; -mod ckb_protocol; -mod ckb_protocol_handler; -mod ckb_service; pub mod errors; mod network; mod network_config; mod network_group; mod network_service; +mod protocol; +mod protocol_handler; +mod service; //mod outbound_peer_service; pub mod peer_store; mod peers_registry; //mod ping_service; #[cfg(test)] mod tests; -mod timer_service; -pub use crate::ckb_protocol::CKBProtocol; -pub use crate::ckb_protocol_handler::{CKBProtocolContext, CKBProtocolHandler, Severity}; pub use crate::network::{Network, PeerInfo, SessionInfo}; pub use crate::network_config::NetworkConfig; pub use crate::network_service::NetworkService; -pub use crate::timer_service::{Timer, TimerRegistry, TimerToken}; +pub use crate::protocol::{CKBProtocol, Event as CKBEvent}; +pub use crate::protocol_handler::{CKBProtocolContext, CKBProtocolHandler, Severity}; +pub use crate::service::timer_service::{Timer, TimerRegistry, TimerToken}; pub use p2p::{multiaddr, PeerId, ProtocolId, SessionType}; use serde_derive::Deserialize; use std::sync::Arc; diff --git a/network/src/network.rs b/network/src/network.rs index c8184edc47..e070e7c140 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -1,15 +1,13 @@ -use crate::ckb_protocol::{CKBProtocol, Event as CKBEvent}; -use crate::ckb_protocol_handler::CKBProtocolHandler; -use crate::ckb_protocol_handler::DefaultCKBProtocolContext; -use crate::ckb_service::CKBService; -//use crate::outbound_peer_service::OutboundPeerService; use crate::errors::{ConfigError, Error, PeerError}; use crate::peer_store::{Behaviour, PeerStore, SqlitePeerStore}; use crate::peers_registry::{ConnectionStatus, Peer, PeerIdentifyInfo, PeersRegistry}; -//use crate::ping_service::PingService; -use crate::timer_service::{TimerRegistry, TimerService}; -use crate::NetworkConfig; -use crate::{PeerIndex, ProtocolId}; +use crate::protocol_handler::{CKBProtocolHandler, DefaultCKBProtocolContext}; +use crate::service::{ + ckb_service::CKBService, + timer_service::{TimerRegistry, TimerService}, +}; +use crate::{CKBEvent, CKBProtocol}; +use crate::{NetworkConfig, PeerIndex, ProtocolId}; use bytes::Bytes; use ckb_util::{Mutex, RwLock}; use fnv::FnvHashMap; diff --git a/network/src/network_service.rs b/network/src/network_service.rs index 631fcc8c1e..b336cc96cd 100644 --- a/network/src/network_service.rs +++ b/network/src/network_service.rs @@ -1,9 +1,7 @@ -use crate::ckb_protocol::{CKBProtocol, Event as CKBEvent}; -use crate::ckb_protocol_handler::CKBProtocolHandler; -use crate::ckb_protocol_handler::{CKBProtocolContext, DefaultCKBProtocolContext}; use crate::network::{CKBProtocols, Network}; -use crate::NetworkConfig; -use crate::{errors::Error, ProtocolId}; +use crate::protocol_handler::CKBProtocolHandler; +use crate::protocol_handler::{CKBProtocolContext, DefaultCKBProtocolContext}; +use crate::{errors::Error, CKBEvent, CKBProtocol, NetworkConfig, ProtocolId}; use ckb_util::Mutex; use futures::future::Future; use futures::sync::mpsc::{Receiver, Sender}; diff --git a/network/src/peers_registry.rs b/network/src/peers_registry.rs index c4a00aa33c..c549a458de 100644 --- a/network/src/peers_registry.rs +++ b/network/src/peers_registry.rs @@ -1,6 +1,6 @@ -use crate::ckb_protocol::Version; use crate::network_group::{Group, NetworkGroup}; use crate::peer_store::PeerStore; +use crate::protocol::Version; use crate::{ errors::{Error, PeerError}, PeerId, PeerIndex, ProtocolId, diff --git a/network/src/protocol.rs b/network/src/protocol.rs index 8733ecfb82..3eee1bc694 100644 --- a/network/src/protocol.rs +++ b/network/src/protocol.rs @@ -1,24 +1,147 @@ -use crate::ckb_protocol::CKBProtocolOutput; -use crate::CKBProtocolHandler; -use futures::Future; -use p2p::{multiaddr::Multiaddr, PeerId}; -use libp2p::identify::{IdentifyInfo, IdentifySender}; -use libp2p::ping; -use std::io::Error as IoError; -use std::sync::Arc; - -pub enum Protocol { - Pong(Box + Send>, PeerId), - Ping( - ping::Pinger, - Box + Send>, - PeerId, - ), - IdentifyRequest(PeerId, IdentifyInfo, Multiaddr), - IdentifyResponse(PeerId, IdentifySender, Multiaddr), - CKBProtocol( - CKBProtocolOutput>, - PeerId, - Multiaddr, - ), +use crate::errors::{Error, ProtocolError}; +use bytes::BufMut; +use bytes::{Buf, IntoBuf}; +use bytes::{Bytes, BytesMut}; +use futures::sync::mpsc; +use futures::{future, stream, Future, Sink, Stream}; +use log::{debug, error, trace}; +use p2p::{ + context::{ServiceContext, SessionContext}, + multiaddr::Multiaddr, + traits::{ProtocolMeta, ServiceProtocol}, + PeerId, ProtocolId, SessionId, SessionType, +}; +use std::io::{self, Error as IoError, ErrorKind as IoErrorKind}; +use std::string::ToString; +use std::vec::IntoIter as VecIntoIter; +use tokio::codec::Decoder; +use tokio::codec::LengthDelimitedCodec; +use tokio::io::{AsyncRead, AsyncWrite}; +use tokio::sync::mpsc::Sender; + +pub type Version = u8; + +#[derive(Clone)] +pub struct CKBProtocol { + id: ProtocolId, + // for example: b"/ckb/" + base_name: Bytes, + // supported version, used to check protocol version + supported_versions: Vec, + event_sender: Sender, +} + +impl CKBProtocol { + pub fn new( + base_name: String, + id: ProtocolId, + versions: &[Version], + event_sender: Sender, + ) -> Self { + let mut base_name_bytes = Bytes::from(format!("/{}/", base_name)); + base_name_bytes.extend_from_slice(format!("{}", id).as_bytes()); + base_name_bytes.extend_from_slice(b"/"); + CKBProtocol { + base_name: base_name_bytes, + id, + supported_versions: { + let mut versions: Vec<_> = versions.to_vec(); + versions.sort_by(|a, b| b.cmp(a)); + versions.to_vec() + }, + event_sender, + } + } + pub fn id(&self) -> ProtocolId { + self.id + } + pub fn base_name(&self) -> Bytes { + self.base_name.clone() + } +} + +impl ProtocolMeta for CKBProtocol { + fn id(&self) -> ProtocolId { + self.id + } + + fn codec(&self) -> LengthDelimitedCodec { + LengthDelimitedCodec::new() + } + + fn service_handle(&self) -> Option> { + let handler = Box::new(CKBHandler { + id: self.id, + event_sender: self.event_sender.clone(), + }); + Some(handler) + } + + fn support_versions(&self) -> Vec { + self.supported_versions + .iter() + .map(|v| format!("{}", v)) + .collect() + } +} + +pub enum Event { + Connected(PeerId, Multiaddr, ProtocolId, SessionType, Version), + ConnectedError(Multiaddr), + Disconnected(PeerId, ProtocolId), + Received(PeerId, ProtocolId, Vec), + Notify(ProtocolId, u64), +} + +struct CKBHandler { + id: ProtocolId, + event_sender: Sender, +} + +impl ServiceProtocol for CKBHandler { + fn init(&mut self, _control: &mut ServiceContext) {} + fn connected( + &mut self, + _control: &mut ServiceContext, + session: &SessionContext, + version: &str, + ) { + let event = match session.remote_pubkey { + Some(ref pubkey) => { + let peer_id = pubkey.peer_id(); + Event::Connected( + peer_id, + session.address.clone(), + self.id, + session.ty, + version.parse::().expect("version"), + ) + } + None => Event::ConnectedError(session.address.clone()), + }; + self.event_sender.try_send(event); + } + + fn disconnected(&mut self, _control: &mut ServiceContext, session: &SessionContext) { + let peer_id = session + .remote_pubkey + .as_ref() + .map(|pubkey| pubkey.peer_id()) + .expect("pubkey"); + self.event_sender + .try_send(Event::Disconnected(peer_id, self.id)); + } + + fn received(&mut self, control: &mut ServiceContext, session: &SessionContext, data: Vec) { + let peer_id = session + .remote_pubkey + .as_ref() + .map(|pubkey| pubkey.peer_id()) + .expect("pubkey"); + self.event_sender + .try_send(Event::Received(peer_id, self.id, data)); + } + fn notify(&mut self, control: &mut ServiceContext, token: u64) { + self.event_sender.try_send(Event::Notify(self.id, token)); + } } diff --git a/network/src/ckb_protocol_handler.rs b/network/src/protocol_handler.rs similarity index 100% rename from network/src/ckb_protocol_handler.rs rename to network/src/protocol_handler.rs diff --git a/network/src/protocol_service.rs b/network/src/protocol_service.rs deleted file mode 100644 index 6bb6bcfbdf..0000000000 --- a/network/src/protocol_service.rs +++ /dev/null @@ -1,55 +0,0 @@ -use super::Network; -use crate::protocol::Protocol; -use crate::transport::TransportOutput; -use futures::future::{empty as empty_future, Future}; -use libp2p::core::Multiaddr; -use libp2p::core::SwarmController; -use libp2p::core::{MuxedTransport, PeerId}; -use std::boxed::Box; -use std::io::Error as IoError; -use std::sync::Arc; -use tokio::io::{AsyncRead, AsyncWrite}; - -pub trait ProtocolService { - type Output; - fn convert_to_protocol( - peer_id: Arc, - addr: &Multiaddr, - output: Self::Output, - ) -> Protocol; - - fn handle( - &self, - _network: Arc, - protocol: Protocol, - ) -> Box + Send>; - - fn start_protocol( - &self, - _network: Arc, - _swarm_controller: SwarmController< - SwarmTran, - Box + Send>, - >, - _transport: Tran, - ) -> Box + Send> - where - SwarmTran: MuxedTransport> + Clone + Send + 'static, - SwarmTran::MultiaddrFuture: Send + 'static, - SwarmTran::Dial: Send, - SwarmTran::Listener: Send, - SwarmTran::ListenerUpgrade: Send, - SwarmTran::Incoming: Send, - SwarmTran::IncomingUpgrade: Send, - Tran: MuxedTransport> + Clone + Send + 'static, - Tran::MultiaddrFuture: Send + 'static, - Tran::Dial: Send, - Tran::Listener: Send, - Tran::ListenerUpgrade: Send, - Tran::Incoming: Send, - Tran::IncomingUpgrade: Send, - TranOut: AsyncRead + AsyncWrite + Send + 'static, - { - Box::new(empty_future()) - } -} diff --git a/network/src/service.rs b/network/src/service.rs new file mode 100644 index 0000000000..befa18e705 --- /dev/null +++ b/network/src/service.rs @@ -0,0 +1,4 @@ +pub mod ckb_service; +pub mod timer_service; +//pub mod ping_service; +//pub mod outbound_service; diff --git a/network/src/ckb_service.rs b/network/src/service/ckb_service.rs similarity index 94% rename from network/src/ckb_service.rs rename to network/src/service/ckb_service.rs index a6aa0aa25b..351de9867f 100644 --- a/network/src/ckb_service.rs +++ b/network/src/service/ckb_service.rs @@ -1,10 +1,6 @@ -use crate::ckb_protocol::CKBProtocol; -use crate::ckb_protocol::Event; -use crate::ckb_protocol_handler::DefaultCKBProtocolContext; use crate::peer_store::{Behaviour, Status}; -use crate::CKBProtocolHandler; -use crate::Network; -use crate::PeerId; +use crate::protocol_handler::DefaultCKBProtocolContext; +use crate::{CKBEvent, CKBProtocol, CKBProtocolHandler, Network, PeerId}; use faketime::unix_time_as_millis; use futures::{ future::{self, Future}, @@ -19,7 +15,7 @@ use std::sync::Arc; use tokio; pub struct CKBService { - pub event_receiver: Receiver, + pub event_receiver: Receiver, pub network: Arc, } @@ -35,7 +31,7 @@ impl Stream for CKBService { type Item = (); type Error = (); fn poll(&mut self) -> Result>, Self::Error> { - use crate::ckb_protocol::Event::*; + use crate::CKBEvent::*; let network = Arc::clone(&self.network); match try_ready!(self.event_receiver.poll()) { diff --git a/network/src/identify_service.rs b/network/src/service/identify_service.rs similarity index 100% rename from network/src/identify_service.rs rename to network/src/service/identify_service.rs diff --git a/network/src/outbound_peer_service.rs b/network/src/service/outbound_peer_service.rs similarity index 100% rename from network/src/outbound_peer_service.rs rename to network/src/service/outbound_peer_service.rs diff --git a/network/src/ping_service.rs b/network/src/service/ping_service.rs similarity index 100% rename from network/src/ping_service.rs rename to network/src/service/ping_service.rs diff --git a/network/src/timer_service.rs b/network/src/service/timer_service.rs similarity index 97% rename from network/src/timer_service.rs rename to network/src/service/timer_service.rs index fb6e11f1f8..f9ac31963b 100644 --- a/network/src/timer_service.rs +++ b/network/src/service/timer_service.rs @@ -1,5 +1,5 @@ -use crate::ckb_protocol_handler::DefaultCKBProtocolContext; use crate::errors::Error; +use crate::protocol_handler::DefaultCKBProtocolContext; use crate::Network; use crate::PeerId; use crate::{CKBProtocolHandler, ProtocolId}; diff --git a/network/src/transport.rs b/network/src/transport.rs deleted file mode 100644 index 019b78cb1c..0000000000 --- a/network/src/transport.rs +++ /dev/null @@ -1,36 +0,0 @@ -use libp2p::core::{transport::BoxedMuxed, upgrade, Multiaddr, PeerId, Transport}; -use libp2p::{self, mplex, secio, yamux, TransportTimeout}; -use std::time::Duration; -use std::usize; -use tokio::io::{AsyncRead, AsyncWrite}; - -pub fn new_transport( - local_private_key: secio::SecioKeyPair, - timeout: Duration, -) -> BoxedMuxed<(PeerId, impl AsyncRead + AsyncWrite)> { - let mut mplex_config = mplex::MplexConfig::new(); - mplex_config.max_buffer_len_behaviour(mplex::MaxBufferBehaviour::Block); - mplex_config.max_buffer_len(usize::MAX); - - let transport = libp2p::CommonTransport::new() - .with_upgrade(secio::SecioConfig { - key: local_private_key, - }) - .and_then(move |out, endpoint, client_addr| { - let key = out.remote_key; - let upgrade = upgrade::map(yamux::Config::default(), move |muxer| (key, muxer)); - upgrade::apply(out.stream, upgrade, endpoint, client_addr) - // TODO: check key - }) - .into_connection_reuse() - .map(|(key, substream), _| (key.into_peer_id(), substream)); - - let transport = TransportTimeout::new(transport, timeout); - transport.boxed_muxed() -} - -pub struct TransportOutput { - pub socket: S, - pub peer_id: PeerId, - pub original_addr: Multiaddr, -} From aada764a05609fb6dc6ccaf1ace97c573ebbfdca Mon Sep 17 00:00:00 2001 From: jjy Date: Mon, 25 Feb 2019 17:38:34 +0800 Subject: [PATCH 03/99] fix: p2p disconnect peer --- network/src/network.rs | 25 ++++++++++++++++++++----- network/src/peers_registry.rs | 4 ++-- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/network/src/network.rs b/network/src/network.rs index e070e7c140..75363db084 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -96,7 +96,24 @@ impl Network { } pub fn drop_peer(&self, peer_id: &PeerId) { - self.peers_registry.write().drop_peer(&peer_id); + if let Some(peer) = self.peers_registry.write().drop_peer(&peer_id) { + let mut p2p_control = self.p2p_control.write(); + for s in peer.sessions { + p2p_control.disconnect(s.id); + } + } + } + + pub fn drop_all(&self) { + debug!(target: "network", "drop all connections..."); + let mut peers_registry = self.peers_registry.write(); + let mut p2p_control = self.p2p_control.write(); + for (peer_id, peer) in peers_registry.peers_iter() { + for s in &peer.sessions { + p2p_control.disconnect(s.id); + } + } + peers_registry.drop_all(); } pub fn local_peer_id(&self) -> &PeerId { @@ -194,8 +211,7 @@ impl Network { #[inline] pub(crate) fn ban_peer(&self, peer_id: &PeerId, timeout: Duration) { - let mut peers_registry = self.peers_registry.write(); - peers_registry.drop_peer(peer_id); + self.drop_peer(peer_id); self.peer_store.write().ban_peer(peer_id, timeout); } @@ -516,8 +532,7 @@ impl Network { let network = Arc::clone(&network); move |_| { let mut peers_registry = network.peers_registry.write(); - debug!(target: "network", "drop all connections..."); - peers_registry.drop_all(); + network.drop_all(); Ok(()) } }) diff --git a/network/src/peers_registry.rs b/network/src/peers_registry.rs index c549a458de..eb84f1d20a 100644 --- a/network/src/peers_registry.rs +++ b/network/src/peers_registry.rs @@ -404,8 +404,8 @@ impl PeersRegistry { } #[inline] - pub fn drop_peer(&mut self, peer_id: &PeerId) { - self.peers.remove(peer_id); + pub fn drop_peer(&mut self, peer_id: &PeerId) -> Option { + self.peers.remove(peer_id) } #[inline] From 269fd25af5f938eca453d3d46644bf4ae7414ccc Mon Sep 17 00:00:00 2001 From: jjy Date: Tue, 26 Feb 2019 16:05:54 +0800 Subject: [PATCH 04/99] refactor: network::outbound_peer_service --- network/src/network.rs | 18 ++- network/src/network_config.rs | 10 -- network/src/service.rs | 2 +- network/src/service/ckb_service.rs | 1 + network/src/service/outbound_peer_service.rs | 120 ++++++------------- 5 files changed, 44 insertions(+), 107 deletions(-) diff --git a/network/src/network.rs b/network/src/network.rs index 75363db084..4b44322c10 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -4,6 +4,7 @@ use crate::peers_registry::{ConnectionStatus, Peer, PeerIdentifyInfo, PeersRegis use crate::protocol_handler::{CKBProtocolHandler, DefaultCKBProtocolContext}; use crate::service::{ ckb_service::CKBService, + outbound_peer_service::OutboundPeerService, timer_service::{TimerRegistry, TimerService}, }; use crate::{CKBEvent, CKBProtocol}; @@ -468,7 +469,6 @@ impl Network { ) -> Result + Send>, Error> { let local_private_key = network.local_private_key().to_owned(); let local_peer_id: PeerId = local_private_key.to_peer_id(); - let basic_transport_timeout = config.transport_timeout; let client_version = config.client_version.clone(); let protocol_version = config.protocol_version.clone(); let max_outbound = config.max_outbound_peers as usize; @@ -487,10 +487,8 @@ impl Network { network: Arc::clone(&network), }; let timer_service = TimerService::new(timer_registry, Arc::clone(&network)); - //let outbound_peer_service = Arc::new(OutboundPeerService { - // try_connect_interval: config.try_outbound_connect_interval, - // timeout: config.try_outbound_connect_timeout, - //}); + let outbound_peer_service = + OutboundPeerService::new(Arc::clone(&network), config.try_outbound_connect_interval); // prepare services futures let futures: Vec + Send>> = vec![ Box::new( @@ -520,11 +518,11 @@ impl Network { // basic_transport.clone(), //), Box::new(timer_service.timer_futures.for_each(|_| Ok(()))), - //outbound_peer_service.start_protocol( - // Arc::clone(&network), - // swarm_controller.clone(), - // basic_transport.clone(), - //), + Box::new( + outbound_peer_service + .for_each(|_| Ok(())) + .map_err(|_| Error::Shutdown), + ), Box::new(close_rx.map_err(|err| Error::Shutdown)), ]; let service_futures = select_all(futures) diff --git a/network/src/network_config.rs b/network/src/network_config.rs index e2b348145a..8c0a1ebd89 100644 --- a/network/src/network_config.rs +++ b/network/src/network_config.rs @@ -20,7 +20,6 @@ pub struct NetworkConfig { pub public_addresses: Vec, pub client_version: String, pub protocol_version: String, - pub transport_timeout: Duration, pub reserved_only: bool, pub max_inbound_peers: u32, pub max_outbound_peers: u32, @@ -31,13 +30,9 @@ pub struct NetworkConfig { pub config_dir_path: Option, pub bootnodes: Vec, pub ping_interval: Duration, - pub ping_timeout: Duration, pub discovery_timeout: Duration, pub discovery_response_count: usize, pub discovery_interval: Duration, - pub identify_timeout: Duration, - pub identify_interval: Duration, - pub try_outbound_connect_timeout: Duration, pub try_outbound_connect_interval: Duration, } @@ -139,7 +134,6 @@ impl Default for NetworkConfig { public_addresses: Vec::new(), client_version: "ckb".to_owned(), protocol_version: "ckb".to_owned(), - transport_timeout: Duration::from_secs(20), reserved_only: false, max_outbound_peers: 15, max_inbound_peers: 10, @@ -150,13 +144,9 @@ impl Default for NetworkConfig { config_dir_path: None, // protocol services config ping_interval: Duration::from_secs(30), - ping_timeout: Duration::from_secs(30), discovery_timeout: Duration::from_secs(20), discovery_response_count: 20, discovery_interval: Duration::from_secs(15), - identify_timeout: Duration::from_secs(30), - identify_interval: Duration::from_secs(15), - try_outbound_connect_timeout: Duration::from_secs(30), try_outbound_connect_interval: Duration::from_secs(15), } } diff --git a/network/src/service.rs b/network/src/service.rs index befa18e705..e4875c94d4 100644 --- a/network/src/service.rs +++ b/network/src/service.rs @@ -1,4 +1,4 @@ pub mod ckb_service; +pub mod outbound_peer_service; pub mod timer_service; //pub mod ping_service; -//pub mod outbound_service; diff --git a/network/src/service/ckb_service.rs b/network/src/service/ckb_service.rs index 351de9867f..436202efa5 100644 --- a/network/src/service/ckb_service.rs +++ b/network/src/service/ckb_service.rs @@ -122,6 +122,7 @@ impl Stream for CKBService { } None => { error!(target: "network", "ckb service should not stop"); + return Ok(Async::Ready(None)); } } Ok(Async::Ready(Some(()))) diff --git a/network/src/service/outbound_peer_service.rs b/network/src/service/outbound_peer_service.rs index 9cdffc7ca5..dd65ed99fa 100644 --- a/network/src/service/outbound_peer_service.rs +++ b/network/src/service/outbound_peer_service.rs @@ -1,14 +1,12 @@ -use crate::protocol::Protocol; -use crate::protocol_service::ProtocolService; -use crate::transport::TransportOutput; use crate::Network; use crate::PeerId; -use futures::future::{self, lazy, Future}; -use futures::Stream; -use libp2p::core::Multiaddr; -use libp2p::core::MuxedTransport; -use libp2p::core::SwarmController; -use log::warn; +use futures::{ + future::{self, Future}, + sync::mpsc::Receiver, + Async, Stream, +}; +use log::{error, warn}; +use p2p::multiaddr::Multiaddr; use std::boxed::Box; use std::io::{Error as IoError, ErrorKind as IoErrorKind}; use std::sync::Arc; @@ -19,102 +17,52 @@ use tokio::io::{AsyncRead, AsyncWrite}; use tokio::timer::Interval; pub struct OutboundPeerService { - pub try_connect_interval: Duration, - pub timeout: Duration, + pub stream_interval: Interval, + pub network: Arc, } -impl ProtocolService for OutboundPeerService { - type Output = (); - fn convert_to_protocol( - _peer_id: Arc, - _addr: &Multiaddr, - _output: Self::Output, - ) -> Protocol { - unreachable!() - } - fn handle( - &self, - _network: Arc, - _protocol: Protocol, - ) -> Box + Send> { - unreachable!() +impl OutboundPeerService { + pub fn new(network: Arc, try_connect_interval: Duration) -> Self { + OutboundPeerService { + network, + stream_interval: Interval::new_interval(try_connect_interval), + } } +} + +impl Stream for OutboundPeerService { + type Item = (); + type Error = (); - // Periodicly connect to new peers - fn start_protocol( - &self, - network: Arc, - swarm_controller: SwarmController< - SwarmTran, - Box + Send>, - >, - transport: Tran, - ) -> Box + Send> - where - SwarmTran: MuxedTransport> + Clone + Send + 'static, - SwarmTran::MultiaddrFuture: Send + 'static, - SwarmTran::Dial: Send, - SwarmTran::Listener: Send, - SwarmTran::ListenerUpgrade: Send, - SwarmTran::Incoming: Send, - SwarmTran::IncomingUpgrade: Send, - Tran: MuxedTransport> + Clone + Send + 'static, - Tran::MultiaddrFuture: Send + 'static, - Tran::Dial: Send, - Tran::Listener: Send, - Tran::ListenerUpgrade: Send, - Tran::Incoming: Send, - Tran::IncomingUpgrade: Send, - TranOut: AsyncRead + AsyncWrite + Send + 'static, - { - let outbound_future = Interval::new( - Instant::now() + Duration::from_secs(5), - self.try_connect_interval, - ) - .map_err(|err| { - IoError::new( - IoErrorKind::Other, - format!("outbound peer service error {:?}", err), - ) - }) - .for_each({ - let transport = transport.clone(); - let timeout = self.timeout; - let network = Arc::clone(&network); - move |_| { - let connection_status = network.connection_status(); + fn poll(&mut self) -> Result>, Self::Error> { + match try_ready!(self.stream_interval.poll().map_err(|_| ())) { + Some(_tick) => { + let connection_status = self.network.connection_status(); let new_outbound = (connection_status.max_outbound - connection_status.unreserved_outbound) as usize; if new_outbound > 0 { - let attempt_peers = network + let attempt_peers = self + .network .peer_store() .read() .peers_to_attempt(new_outbound as u32); for (peer_id, addr) in attempt_peers.iter().filter_map(|(peer_id, addr)| { - if network.local_peer_id() != peer_id { + if self.network.local_peer_id() != peer_id { Some((peer_id.clone(), addr.clone())) } else { None } }) { - network.dial_to_peer( - transport.clone(), - &addr, - &peer_id, - &swarm_controller, - timeout, - ); + self.network.dial(&peer_id, addr); } } - - Box::new(lazy(|| future::ok(()))) as Box + Send> } - }) - .then(|err| { - warn!(target: "network", "Outbound peer service stopped, reason: {:?}", err); - err - }); - Box::new(outbound_future) as Box + Send> + None => { + error!(target: "network", "ckb outbound peer service stopped"); + return Ok(Async::Ready(None)); + } + } + Ok(Async::Ready(Some(()))) } } From 140171e3003ed78b9ebd0db1ef3e8fd89f21a314 Mon Sep 17 00:00:00 2001 From: jjy Date: Tue, 26 Feb 2019 17:14:46 +0800 Subject: [PATCH 05/99] refactor: use new ckb-network --- network/src/lib.rs | 2 +- network/src/protocol.rs | 3 +-- rpc/src/module/miner.rs | 22 +++++++++-------- rpc/src/module/pool.rs | 18 ++++++++------ rpc/src/module/trace.rs | 21 +++++++++-------- src/cli/run_impl.rs | 42 ++++++++++++++++++++------------- sync/src/lib.rs | 11 +++++---- sync/src/net_time_checker.rs | 2 +- sync/src/relayer/mod.rs | 4 ++-- sync/src/synchronizer/mod.rs | 10 ++++---- sync/src/tests/mod.rs | 4 ++-- sync/src/tests/relayer.rs | 43 ++++++++++++++++++++++++++-------- sync/src/tests/synchronizer.rs | 7 +++--- 13 files changed, 115 insertions(+), 74 deletions(-) diff --git a/network/src/lib.rs b/network/src/lib.rs index a9694b2802..924be26947 100644 --- a/network/src/lib.rs +++ b/network/src/lib.rs @@ -1,5 +1,5 @@ #[macro_use] -extern crate futures; +pub extern crate futures; pub mod errors; mod network; mod network_config; diff --git a/network/src/protocol.rs b/network/src/protocol.rs index 3eee1bc694..afda7fc3a8 100644 --- a/network/src/protocol.rs +++ b/network/src/protocol.rs @@ -2,7 +2,7 @@ use crate::errors::{Error, ProtocolError}; use bytes::BufMut; use bytes::{Buf, IntoBuf}; use bytes::{Bytes, BytesMut}; -use futures::sync::mpsc; +use futures::sync::mpsc::{self, Sender}; use futures::{future, stream, Future, Sink, Stream}; use log::{debug, error, trace}; use p2p::{ @@ -17,7 +17,6 @@ use std::vec::IntoIter as VecIntoIter; use tokio::codec::Decoder; use tokio::codec::LengthDelimitedCodec; use tokio::io::{AsyncRead, AsyncWrite}; -use tokio::sync::mpsc::Sender; pub type Version = u8; diff --git a/rpc/src/module/miner.rs b/rpc/src/module/miner.rs index 28b7f4e301..6e86276e28 100644 --- a/rpc/src/module/miner.rs +++ b/rpc/src/module/miner.rs @@ -1,11 +1,11 @@ use ckb_chain::chain::ChainController; use ckb_core::block::Block as CoreBlock; use ckb_miner::BlockAssemblerController; -use ckb_network::NetworkService; +use ckb_network::{NetworkService, ProtocolId}; use ckb_protocol::RelayMessage; use ckb_shared::{index::ChainIndex, shared::Shared}; -use ckb_sync::RELAY_PROTOCOL_ID; use ckb_traits::ChainProvider; +use ckb_sync::NetworkProtocol; use ckb_verification::{HeaderResolverWrapper, HeaderVerifier, Verifier}; use flatbuffers::FlatBufferBuilder; use jsonrpc_core::{Error, Result}; @@ -64,14 +64,16 @@ impl MinerRpc for MinerRpcImpl { let ret = self.chain.process_block(Arc::clone(&block)); if ret.is_ok() { // announce new block - self.network.with_protocol_context(RELAY_PROTOCOL_ID, |nc| { - let fbb = &mut FlatBufferBuilder::new(); - let message = RelayMessage::build_compact_block(fbb, &block, &HashSet::new()); - fbb.finish(message, None); - for peer in nc.connected_peers() { - let _ = nc.send(peer, fbb.finished_data().to_vec()); - } - }); + self.network + .with_protocol_context(NetworkProtocol::RELAY as ProtocolId, |nc| { + let fbb = &mut FlatBufferBuilder::new(); + let message = + RelayMessage::build_compact_block(fbb, &block, &HashSet::new()); + fbb.finish(message, None); + for peer in nc.connected_peers() { + let _ = nc.send(peer, fbb.finished_data().to_vec()); + } + }); Ok(Some(block.header().hash().clone())) } else { debug!(target: "rpc", "submit_block process_block {:?}", ret); diff --git a/rpc/src/module/pool.rs b/rpc/src/module/pool.rs index 7b54212320..3bb2894091 100644 --- a/rpc/src/module/pool.rs +++ b/rpc/src/module/pool.rs @@ -3,7 +3,10 @@ use ckb_network::NetworkService; use ckb_protocol::RelayMessage; use ckb_shared::index::ChainIndex; use ckb_shared::shared::Shared; -use ckb_sync::RELAY_PROTOCOL_ID; +use ckb_network::{NetworkService, ProtocolId}; +use ckb_pool::txs_pool::TransactionPoolController; +use ckb_protocol::RelayMessage; +use ckb_sync::NetworkProtocol; use flatbuffers::FlatBufferBuilder; use jsonrpc_core::Result; use jsonrpc_derive::rpc; @@ -43,12 +46,13 @@ impl PoolRpc for PoolRpcImpl { let message = RelayMessage::build_transaction(fbb, &tx); fbb.finish(message, None); - self.network.with_protocol_context(RELAY_PROTOCOL_ID, |nc| { - for peer in nc.connected_peers() { - debug!(target: "rpc", "relay transaction {} to peer#{}", tx_hash, peer); - let _ = nc.send(peer, fbb.finished_data().to_vec()); - } - }); + self.network + .with_protocol_context(NetworkProtocol::RELAY as ProtocolId, |nc| { + for peer in nc.connected_peers() { + debug!(target: "rpc", "relay transaction {} to peer#{}", tx_hash, peer); + let _ = nc.send(peer, fbb.finished_data().to_vec()); + } + }); Ok(tx_hash) } diff --git a/rpc/src/module/trace.rs b/rpc/src/module/trace.rs index fea01b8d43..fb5ba79f9c 100644 --- a/rpc/src/module/trace.rs +++ b/rpc/src/module/trace.rs @@ -1,10 +1,10 @@ use ckb_core::transaction::Transaction as CoreTransaction; -use ckb_network::NetworkService; -use ckb_protocol::RelayMessage; use ckb_shared::index::ChainIndex; use ckb_shared::shared::Shared; -use ckb_shared::tx_pool::TxTrace; -use ckb_sync::RELAY_PROTOCOL_ID; +use ckb_network::{NetworkService, ProtocolId}; +use ckb_pool::txs_pool::{TransactionPoolController, TxTrace}; +use ckb_protocol::RelayMessage; +use ckb_sync::NetworkProtocol; use flatbuffers::FlatBufferBuilder; use jsonrpc_core::Result; use jsonrpc_derive::rpc; @@ -39,12 +39,13 @@ impl TraceRpc for TraceRpcImpl { let message = RelayMessage::build_transaction(fbb, &tx); fbb.finish(message, None); - self.network.with_protocol_context(RELAY_PROTOCOL_ID, |nc| { - for peer in nc.connected_peers() { - debug!(target: "rpc", "relay transaction {} to peer#{}", tx_hash, peer); - let _ = nc.send(peer, fbb.finished_data().to_vec()); - } - }); + self.network + .with_protocol_context(NetworkProtocol::RELAY as ProtocolId, |nc| { + for peer in nc.connected_peers() { + debug!(target: "rpc", "relay transaction {} to peer#{}", tx_hash, peer); + let _ = nc.send(peer, fbb.finished_data().to_vec()); + } + }); Ok(tx_hash) } diff --git a/src/cli/run_impl.rs b/src/cli/run_impl.rs index e19a5631e8..ad27fc2766 100644 --- a/src/cli/run_impl.rs +++ b/src/cli/run_impl.rs @@ -3,10 +3,12 @@ use crate::Setup; use ckb_chain::chain::{ChainBuilder, ChainController}; use ckb_core::script::Script; use ckb_db::diskdb::RocksDB; -use ckb_miner::BlockAssembler; +use ckb_miner::{BlockAssembler, BlockAssemblerController}; +use ckb_network::futures::sync::mpsc::channel; use ckb_network::CKBProtocol; use ckb_network::NetworkConfig; use ckb_network::NetworkService; +use ckb_network::ProtocolId; use ckb_notify::{NotifyController, NotifyService}; use ckb_rpc::RpcServer; use ckb_shared::cachedb::CacheDB; @@ -15,7 +17,6 @@ use ckb_shared::shared::{Shared, SharedBuilder}; use ckb_sync::{ NetTimeProtocol, Relayer, Synchronizer, RELAY_PROTOCOL_ID, SYNC_PROTOCOL_ID, TIME_PROTOCOL_ID, }; -use ckb_traits::ChainProvider; use crypto::secp::Generator; use log::info; use numext_fixed_hash::H256; @@ -56,29 +57,38 @@ pub fn run(setup: Setup) { let net_time_checker = Arc::new(NetTimeProtocol::default()); let network_config = NetworkConfig::from(setup.configs.network); - let protocol_base_name = "ckb"; + let (sender, receiver) = channel(std::u8::MAX as usize); let protocols = vec![ - CKBProtocol::new( - protocol_base_name.to_string(), + ( + CKBProtocol::new( + "ckb/syn".to_string(), + NetworkProtocol::SYNC as ProtocolId, + &[1][..], + sender.clone(), + ), synchronizer as Arc<_>, - SYNC_PROTOCOL_ID, - &[1][..], ), - CKBProtocol::new( - protocol_base_name.to_string(), + ( + CKBProtocol::new( + "ckb/rel".to_string(), + NetworkProtocol::RELAY as ProtocolId, + &[1][..], + sender.clone(), + ), relayer as Arc<_>, - RELAY_PROTOCOL_ID, - &[1][..], ), - CKBProtocol::new( - protocol_base_name.to_string(), + ( + CKBProtocol::new( + "ckb/tim".to_string(), + NetworkProtocol::TIME as ProtocolId, + &[1][..], + sender.clone(), + ), net_time_checker as Arc<_>, - TIME_PROTOCOL_ID, - &[1][..], ), ]; let network = Arc::new( - NetworkService::run_in_thread(&network_config, protocols) + NetworkService::run_in_thread(&network_config, protocols, receiver) .expect("Create and start network"), ); diff --git a/sync/src/lib.rs b/sync/src/lib.rs index b5d4926f26..1eb2bf287b 100644 --- a/sync/src/lib.rs +++ b/sync/src/lib.rs @@ -17,8 +17,6 @@ pub use crate::net_time_checker::NetTimeProtocol; pub use crate::relayer::Relayer; pub use crate::synchronizer::Synchronizer; -use ckb_network::ProtocolId; - pub const MAX_HEADERS_LEN: usize = 2_000; pub const MAX_INVENTORY_LEN: usize = 50_000; pub const MAX_SCHEDULED_LEN: usize = 4 * 1024; @@ -29,9 +27,12 @@ pub const MAX_TIP_AGE: u64 = 60 * 60 * 1000; pub const STALE_RELAY_AGE_LIMIT: u64 = 30 * 24 * 60 * 60 * 1000; pub const BLOCK_DOWNLOAD_WINDOW: u64 = 1024; pub const PER_FETCH_BLOCK_LIMIT: usize = 128; -pub const SYNC_PROTOCOL_ID: ProtocolId = *b"syn"; -pub const RELAY_PROTOCOL_ID: ProtocolId = *b"rel"; -pub const TIME_PROTOCOL_ID: ProtocolId = *b"tim"; + +pub enum NetworkProtocol { + SYNC = 0, + RELAY = 1, + TIME = 2, +} // Timeout = base + per_header * (expected number of headers) pub const HEADERS_DOWNLOAD_TIMEOUT_BASE: u64 = 15 * 60 * 1000; // 15 minutes diff --git a/sync/src/net_time_checker.rs b/sync/src/net_time_checker.rs index ae28712402..5b621870fe 100644 --- a/sync/src/net_time_checker.rs +++ b/sync/src/net_time_checker.rs @@ -90,7 +90,7 @@ impl Default for NetTimeProtocol { impl CKBProtocolHandler for NetTimeProtocol { fn initialize(&self, _nc: Box) {} - fn received(&self, nc: Box, peer: PeerIndex, data: &[u8]) { + fn received(&self, nc: Box, peer: PeerIndex, data: Vec) { // collect time sample from outbound peer if nc.session_info(peer).map(|s| s.peer.is_outbound()) == Some(true) { let now: u64 = faketime::unix_time_as_millis(); diff --git a/sync/src/relayer/mod.rs b/sync/src/relayer/mod.rs index 12e9174f90..076d5a4c52 100644 --- a/sync/src/relayer/mod.rs +++ b/sync/src/relayer/mod.rs @@ -264,9 +264,9 @@ where let _ = nc.register_timer(TX_PROPOSAL_TOKEN, Duration::from_millis(100)); } - fn received(&self, nc: Box, peer: PeerIndex, data: &[u8]) { + fn received(&self, nc: Box, peer: PeerIndex, data: Vec) { // TODO use flatbuffers verifier - let msg = get_root::(data); + let msg = get_root::(&data); debug!(target: "relay", "msg {:?}", msg.payload_type()); self.process(nc.as_ref(), peer, msg); } diff --git a/sync/src/synchronizer/mod.rs b/sync/src/synchronizer/mod.rs index a2e688c518..46e4371dfb 100644 --- a/sync/src/synchronizer/mod.rs +++ b/sync/src/synchronizer/mod.rs @@ -686,7 +686,7 @@ where let _ = nc.register_timer(TIMEOUT_EVICTION_TOKEN, Duration::from_millis(1000)); } - fn received(&self, nc: Box, peer: PeerIndex, data: &[u8]) { + fn received(&self, nc: Box, peer: PeerIndex, data: Vec) { // TODO use flatbuffers verifier let msg = get_root::(&data); debug!(target: "sync", "msg {:?}", msg.payload_type()); @@ -735,8 +735,8 @@ mod tests { use ckb_core::transaction::{CellInput, CellOutput, Transaction, TransactionBuilder}; use ckb_db::memorydb::MemoryKeyValueDB; use ckb_network::{ - random_peer_id, CKBProtocolContext, Endpoint, Error as NetworkError, PeerIndex, PeerInfo, - ProtocolId, SessionInfo, Severity, TimerToken, ToMultiaddr, + errors::Error as NetworkError, multiaddr::ToMultiaddr, random_peer_id, CKBProtocolContext, + PeerIndex, PeerInfo, ProtocolId, SessionInfo, SessionType, Severity, TimerToken, }; use ckb_notify::{NotifyController, NotifyService}; use ckb_protocol::{Block as FbsBlock, Headers as FbsHeaders}; @@ -1056,8 +1056,8 @@ mod tests { fn mock_session_info() -> SessionInfo { SessionInfo { peer: PeerInfo { - peer_id: random_peer_id().unwrap(), - endpoint_role: Endpoint::Dialer, + peer_id: random_peer_id(), + session_type: SessionType::Client, last_ping_time: None, connected_addr: "/ip4/127.0.0.1".to_multiaddr().expect("parse multiaddr"), identify_info: None, diff --git a/sync/src/tests/mod.rs b/sync/src/tests/mod.rs index 44d78c0edc..5f80c262d6 100644 --- a/sync/src/tests/mod.rs +++ b/sync/src/tests/mod.rs @@ -1,5 +1,5 @@ use ckb_network::{ - CKBProtocolContext, CKBProtocolHandler, Error as NetworkError, PeerIndex, ProtocolId, + errors::Error as NetworkError, CKBProtocolContext, CKBProtocolHandler, PeerIndex, ProtocolId, SessionInfo, Severity, TimerToken, }; use std::collections::HashMap; @@ -101,7 +101,7 @@ impl TestNode { timer_senders: self.timer_senders.clone(), }), *peer, - &payload, + payload.clone(), ) }; diff --git a/sync/src/tests/relayer.rs b/sync/src/tests/relayer.rs index bd71c93ddc..b1105cfefe 100644 --- a/sync/src/tests/relayer.rs +++ b/sync/src/tests/relayer.rs @@ -1,6 +1,6 @@ use crate::relayer::TX_PROPOSAL_TOKEN; use crate::tests::TestNode; -use crate::{Relayer, RELAY_PROTOCOL_ID}; +use crate::{NetworkProtocol, Relayer}; use ckb_chain::chain::{ChainBuilder, ChainController}; use ckb_chain_spec::consensus::Consensus; use ckb_core::block::BlockBuilder; @@ -8,6 +8,7 @@ use ckb_core::header::HeaderBuilder; use ckb_core::script::Script; use ckb_core::transaction::{CellInput, CellOutput, OutPoint, TransactionBuilder}; use ckb_db::memorydb::MemoryKeyValueDB; +use ckb_network::ProtocolId; use ckb_notify::NotifyService; use ckb_protocol::RelayMessage; use ckb_shared::shared::{Shared, SharedBuilder}; @@ -39,7 +40,7 @@ fn relay_compact_block_with_one_tx() { let (mut node2, shared2, _chain_controller2) = setup_node(&thread_name, 3); let barrier = Arc::new(Barrier::new(2)); - node1.connect(&mut node2, RELAY_PROTOCOL_ID); + node1.connect(&mut node2, NetworkProtocol::RELAY as ProtocolId); let (signal_tx1, _) = channel(); let barrier1 = Arc::clone(&barrier); @@ -64,7 +65,10 @@ fn relay_compact_block_with_one_tx() { let fbb = &mut FlatBufferBuilder::new(); let message = RelayMessage::build_transaction(fbb, &tx); fbb.finish(message, None); - node1.broadcast(RELAY_PROTOCOL_ID, &fbb.finished_data().to_vec()); + node1.broadcast( + NetworkProtocol::RELAY as ProtocolId, + &fbb.finished_data().to_vec(), + ); } // building 1st compact block with tx proposal and broadcast it @@ -98,7 +102,10 @@ fn relay_compact_block_with_one_tx() { let fbb = &mut FlatBufferBuilder::new(); let message = RelayMessage::build_compact_block(fbb, &block, &HashSet::new()); fbb.finish(message, None); - node1.broadcast(RELAY_PROTOCOL_ID, &fbb.finished_data().to_vec()); + node1.broadcast( + NetworkProtocol::RELAY as ProtocolId, + &fbb.finished_data().to_vec(), + ); } // building 2nd compact block with tx and broadcast it @@ -134,7 +141,10 @@ fn relay_compact_block_with_one_tx() { let fbb = &mut FlatBufferBuilder::new(); let message = RelayMessage::build_compact_block(fbb, &block, &HashSet::new()); fbb.finish(message, None); - node1.broadcast(RELAY_PROTOCOL_ID, &fbb.finished_data().to_vec()); + node1.broadcast( + NetworkProtocol::RELAY as ProtocolId, + &fbb.finished_data().to_vec(), + ); } node1.start(&signal_tx1, |_| false); @@ -177,7 +187,7 @@ fn relay_compact_block_with_missing_indexs() { let (mut node1, shared1, chain_controller1) = setup_node(&thread_name, 3); let (mut node2, shared2, _chain_controller2) = setup_node(&thread_name, 3); - node1.connect(&mut node2, RELAY_PROTOCOL_ID); + node1.connect(&mut node2, NetworkProtocol::RELAY as ProtocolId); let (signal_tx1, _) = channel(); thread::Builder::new() @@ -205,7 +215,10 @@ fn relay_compact_block_with_missing_indexs() { let fbb = &mut FlatBufferBuilder::new(); let message = RelayMessage::build_transaction(fbb, &txs[*i]); fbb.finish(message, None); - node1.broadcast(RELAY_PROTOCOL_ID, &fbb.finished_data().to_vec()); + node1.broadcast( + NetworkProtocol::RELAY as ProtocolId, + &fbb.finished_data().to_vec(), + ); }); // building 1st compact block with tx proposal and broadcast it @@ -239,7 +252,10 @@ fn relay_compact_block_with_missing_indexs() { let fbb = &mut FlatBufferBuilder::new(); let message = RelayMessage::build_compact_block(fbb, &block, &HashSet::new()); fbb.finish(message, None); - node1.broadcast(RELAY_PROTOCOL_ID, &fbb.finished_data().to_vec()); + node1.broadcast( + NetworkProtocol::RELAY as ProtocolId, + &fbb.finished_data().to_vec(), + ); } // building 2nd compact block with txs and broadcast it @@ -275,7 +291,10 @@ fn relay_compact_block_with_missing_indexs() { let fbb = &mut FlatBufferBuilder::new(); let message = RelayMessage::build_compact_block(fbb, &block, &HashSet::new()); fbb.finish(message, None); - node1.broadcast(RELAY_PROTOCOL_ID, &fbb.finished_data().to_vec()); + node1.broadcast( + NetworkProtocol::RELAY as ProtocolId, + &fbb.finished_data().to_vec(), + ); } node1.start(&signal_tx1, |_| false); @@ -361,7 +380,11 @@ fn setup_node( let mut node = TestNode::default(); let protocol = Arc::new(relayer) as Arc<_>; - node.add_protocol(RELAY_PROTOCOL_ID, &protocol, &[TX_PROPOSAL_TOKEN]); + node.add_protocol( + NetworkProtocol::RELAY as ProtocolId, + &protocol, + &[TX_PROPOSAL_TOKEN], + ); (node, shared, chain_controller) } diff --git a/sync/src/tests/synchronizer.rs b/sync/src/tests/synchronizer.rs index a03cf0e052..8dd7257b19 100644 --- a/sync/src/tests/synchronizer.rs +++ b/sync/src/tests/synchronizer.rs @@ -1,12 +1,13 @@ use crate::synchronizer::{BLOCK_FETCH_TOKEN, SEND_GET_HEADERS_TOKEN, TIMEOUT_EVICTION_TOKEN}; use crate::tests::TestNode; -use crate::{Config, Synchronizer, SYNC_PROTOCOL_ID}; +use crate::{Config, NetworkProtocol, Synchronizer}; use ckb_chain::chain::ChainBuilder; use ckb_chain_spec::consensus::Consensus; use ckb_core::block::BlockBuilder; use ckb_core::header::HeaderBuilder; use ckb_core::transaction::{CellInput, CellOutput, TransactionBuilder}; use ckb_db::memorydb::MemoryKeyValueDB; +use ckb_network::ProtocolId; use ckb_notify::NotifyService; use ckb_protocol::SyncMessage; use ckb_shared::shared::{Shared, SharedBuilder}; @@ -28,7 +29,7 @@ fn basic_sync() { let (mut node1, shared1) = setup_node(&thread_name, 1); let (mut node2, shared2) = setup_node(&thread_name, 3); - node1.connect(&mut node2, SYNC_PROTOCOL_ID); + node1.connect(&mut node2, NetworkProtocol::SYNC as ProtocolId); let (signal_tx1, signal_rx1) = channel(); thread::Builder::new() @@ -112,7 +113,7 @@ fn setup_node( let mut node = TestNode::default(); let protocol = Arc::new(synchronizer) as Arc<_>; node.add_protocol( - SYNC_PROTOCOL_ID, + NetworkProtocol::SYNC as ProtocolId, &protocol, &[ SEND_GET_HEADERS_TOKEN, From 3c95227deac690f1ad4d9e757cf7ba751e4ce672 Mon Sep 17 00:00:00 2001 From: jjy Date: Wed, 27 Feb 2019 15:12:55 +0800 Subject: [PATCH 06/99] fix: network dial --- network/src/network.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/network/src/network.rs b/network/src/network.rs index 4b44322c10..f6822ff38c 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -17,11 +17,12 @@ use futures::sync::mpsc::UnboundedSender; use futures::sync::mpsc::{Receiver, Sender}; use futures::sync::oneshot; use futures::Stream; -use log::{debug, info, trace, warn}; +use log::{debug, error, info, trace, warn}; +use multiaddr::multihash::Multihash; use p2p::{ builder::ServiceBuilder, context::{ServiceContext, ServiceControl}, - multiaddr::Multiaddr, + multiaddr::{self, Multiaddr}, service::{Service, ServiceError, ServiceEvent}, traits::ServiceHandle, PeerId, PublicKey, SessionType, @@ -338,13 +339,21 @@ impl Network { }) } - pub fn dial(&self, expected_peer_id: &PeerId, addr: Multiaddr) { + pub fn dial(&self, expected_peer_id: &PeerId, mut addr: Multiaddr) { if expected_peer_id == self.local_peer_id() { debug!(target: "network", "ignore dial to self"); return; } debug!(target: "network", "dial to peer {:?} address {:?}", expected_peer_id, addr); - self.p2p_control.write().dial(addr); + match Multihash::from_bytes(expected_peer_id.as_bytes().to_vec()) { + Ok(peer_id_hash) => { + addr.append(multiaddr::Protocol::P2p(peer_id_hash)); + self.p2p_control.write().dial(addr); + } + Err(err) => { + error!(target: "network", "failed to convert peer_id to addr: {}", err); + } + } } pub(crate) fn inner_build( @@ -529,8 +538,8 @@ impl Network { .and_then({ let network = Arc::clone(&network); move |_| { - let mut peers_registry = network.peers_registry.write(); network.drop_all(); + debug!(target: "network", "network shutdown"); Ok(()) } }) From 81490333471ba284bc9b0e3555f61ae1f40f383f Mon Sep 17 00:00:00 2001 From: jjy Date: Thu, 28 Feb 2019 14:29:47 +0800 Subject: [PATCH 07/99] refactor: upgrade p2p to 0.1 version --- Cargo.lock | 109 +++++++------- network/Cargo.toml | 4 +- network/src/benches/sqlite_peer_store.rs | 29 ++-- network/src/errors.rs | 3 +- network/src/lib.rs | 17 ++- network/src/network.rs | 135 +++++++++-------- network/src/network_config.rs | 4 +- network/src/network_service.rs | 2 +- network/src/peer_store.rs | 6 +- network/src/peer_store/db.rs | 2 +- network/src/peer_store/sqlite_peer_store.rs | 2 +- network/src/peers_registry.rs | 153 ++++++++++++-------- network/src/protocol.rs | 75 ++++++---- network/src/service/ckb_service.rs | 39 +++-- network/src/tests/peers_registry.rs | 79 +++++----- sync/src/synchronizer/mod.rs | 2 +- 16 files changed, 359 insertions(+), 302 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d7d8cb70eb..9af7d2ee27 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -226,11 +226,6 @@ name = "byte-tools" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "byteorder" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "byteorder" version = "1.3.1" @@ -479,17 +474,17 @@ dependencies = [ "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "p2p 0.1.0 (git+https://github.com/nervosnetwork/p2p)", "r2d2 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2_sqlite 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "rusqlite 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)", - "secio 0.1.0 (git+https://github.com/nervosnetwork/p2p)", - "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)", "snap 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", + "tentacle 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tentacle-secio 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", "unsigned-varint 0.2.2 (git+https://github.com/paritytech/unsigned-varint)", ] @@ -1787,29 +1782,14 @@ dependencies = [ "stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "p2p" -version = "0.1.0" -source = "git+https://github.com/nervosnetwork/p2p#75a3b0b40663358b7eb81356f107f7bcf60c288b" -dependencies = [ - "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "parity-multiaddr 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "secio 0.1.0 (git+https://github.com/nervosnetwork/p2p)", - "tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-yamux 0.1.3 (git+https://github.com/nervosnetwork/p2p)", -] - [[package]] name = "parity-multiaddr" -version = "0.1.0" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "arrayref 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "bs58 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "byteorder 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "data-encoding 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "parity-multihash 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2262,29 +2242,6 @@ dependencies = [ "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "secio" -version = "0.1.0" -source = "git+https://github.com/nervosnetwork/p2p#75a3b0b40663358b7eb81356f107f7bcf60c288b" -dependencies = [ - "aes-ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "bs58 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "hmac 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", - "ring 0.14.6 (registry+https://github.com/rust-lang/crates.io-index)", - "secp256k1 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", - "sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", - "twofish 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "unsigned-varint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "untrusted 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "secp256k1" version = "0.9.1" @@ -2510,6 +2467,45 @@ dependencies = [ "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "tentacle" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bytes 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", + "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "parity-multiaddr 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tentacle-secio 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-threadpool 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-yamux 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "tentacle-secio" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aes-ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bs58 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "bytes 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", + "ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", + "hmac 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", + "ring 0.14.6 (registry+https://github.com/rust-lang/crates.io-index)", + "secp256k1 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", + "twofish 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unsigned-varint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "untrusted 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "termcolor" version = "1.0.4" @@ -2793,8 +2789,8 @@ dependencies = [ [[package]] name = "tokio-yamux" -version = "0.1.3" -source = "git+https://github.com/nervosnetwork/p2p#75a3b0b40663358b7eb81356f107f7bcf60c288b" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3046,7 +3042,6 @@ dependencies = [ "checksum bloom-filters 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b233192e97c6e528c071bd83180c5d4f35846b9aff4670ea8e05e3ba0daa5e30" "checksum bs58 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0de79cfb98e7aa9988188784d8664b4b5dad6eaaa0863b91d9a4ed871d4f7a42" "checksum byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" -"checksum byteorder 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "96c8b41881888cc08af32d47ac4edd52bc7fa27fef774be47a92443756451304" "checksum byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a019b10a2a7cdeb292db131fc8113e57ea2a908f6e7894b0c3c671893b65dbeb" "checksum bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c" "checksum cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "926013f2860c46252efceabb19f4a6b308197505082c609025aa6706c011d427" @@ -3165,8 +3160,7 @@ dependencies = [ "checksum numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5dc24e4bed29ec6706d11ebe8ee5a77eb99de62b60e3af22ec704aae3f548c0d" "checksum opaque-debug 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "93f5bb2e8e8dec81642920ccff6b61f1eb94fa3020c5a325c9851ff604152409" "checksum owning_ref 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "49a4b8ea2179e6a2e27411d3bca09ca6dd630821cf6894c6c7c8467a8ee7ef13" -"checksum p2p 0.1.0 (git+https://github.com/nervosnetwork/p2p)" = "" -"checksum parity-multiaddr 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9a8e5d637787fe097ec1bfca2aa3eb687396518003df991c6c7216d86682d5ff" +"checksum parity-multiaddr 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "61ae6944d4435d41f4d0f12108c5cbb9207cbb14bc8f2b4984c6e930dc9c8e41" "checksum parity-multihash 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3e8eab0287ccde7821e337a124dc5a4f1d6e4c25d10cc91e3f9361615dd95076" "checksum parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ab41b4aed082705d1056416ae4468b6ea99d52599ecf3169b00088d43113e337" "checksum parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94c8c7923936b28d546dfd14d4472eaf34c99b14e1c973a32b3e6d4eb04298c9" @@ -3218,7 +3212,6 @@ dependencies = [ "checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" "checksum scroll 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2f84d114ef17fd144153d608fba7c446b0145d038985e7a8cc5d08bb0ce20383" "checksum scroll_derive 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8f1aa96c45e7f5a91cb7fabe7b279f02fea7126239fc40b732316e8b6a2d0fcb" -"checksum secio 0.1.0 (git+https://github.com/nervosnetwork/p2p)" = "" "checksum secp256k1 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4070f3906e65249228094cf97b04a90799fba04468190bbbcfa812309cf86e32" "checksum secp256k1 0.9.1 (git+https://github.com/nervosnetwork/rust-secp256k1)" = "" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" @@ -3248,6 +3241,8 @@ dependencies = [ "checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015" "checksum take 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b157868d8ac1f56b64604539990685fa7611d8fa9e5476cf0c02cf34d32917c5" "checksum tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "b86c784c88d98c801132806dadd3819ed29d8600836c4088e855cdf3e178ed8a" +"checksum tentacle 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6c29b4a6d8dbf15e8191c4723be562689ec734edc86fa9a84585c256d53e04be" +"checksum tentacle-secio 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9562b522b885f057e39aac9feb9048e64d4b9cc7f5c0be5933ae6606eb56ad40" "checksum termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4096add70612622289f2fdcdbd5086dc81c1e2675e6ae58d6c4f62a16c6d7f2f" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum termios 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "72b620c5ea021d75a735c943269bb07d30c9b77d6ac6b236bc8b5c496ef05625" @@ -3272,7 +3267,7 @@ dependencies = [ "checksum tokio-timer 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "2910970404ba6fa78c5539126a9ae2045d62e3713041e447f695f41405a120c6" "checksum tokio-udp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "66268575b80f4a4a710ef83d087fdfeeabdce9b74c797535fbac18a2cb906e92" "checksum tokio-uds 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "037ffc3ba0e12a0ab4aca92e5234e0dedeb48fddf6ccd260f1f150a36a9f2445" -"checksum tokio-yamux 0.1.3 (git+https://github.com/nervosnetwork/p2p)" = "" +"checksum tokio-yamux 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "1815b4502bb86c8ffaae8521ecc26e148edb4fcc2ed24c1feba0807a492b4946" "checksum toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "758664fc71a3a69038656bee8b6be6477d2a6c315a6b81f7081f591bffa4111f" "checksum try-lock 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee2aa4715743892880f70885373966c83d73ef1b0838a664ef0c76fffd35e7c2" "checksum try-lock 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" diff --git a/network/Cargo.toml b/network/Cargo.toml index acd68e2bc9..7dbd94bfcc 100644 --- a/network/Cargo.toml +++ b/network/Cargo.toml @@ -20,8 +20,8 @@ bytes = "0.4.12" tokio = "0.1.16" futures = { version = "0.1.19", features = ["use_std"] } snap = "0.2" -p2p = { git = "https://github.com/nervosnetwork/p2p", branch = "master" } -secio = { git = "https://github.com/nervosnetwork/p2p", branch = "master" } +p2p = { version = "0.1", package="tentacle" } +secio = { version = "0.1", package="tentacle-secio" } faketime = "0.2.0" rusqlite = {version = "0.16.0", features = ["bundled"]} lazy_static = "1.3.0" diff --git a/network/src/benches/sqlite_peer_store.rs b/network/src/benches/sqlite_peer_store.rs index 93ed8903d5..0e74b08ff7 100644 --- a/network/src/benches/sqlite_peer_store.rs +++ b/network/src/benches/sqlite_peer_store.rs @@ -4,8 +4,9 @@ extern crate ckb_network; extern crate ckb_util; use ckb_network::{ + multiaddr::ToMultiaddr, peer_store::{PeerStore, SqlitePeerStore}, - random_peer_id, Endpoint, ToMultiaddr, + random_peer_id, SessionType, }; use ckb_util::Mutex; use criterion::Criterion; @@ -15,13 +16,11 @@ fn insert_peer_info_benchmark(c: &mut Criterion) { c.bench_function("insert 100 peer_info", |b| { b.iter({ let mut peer_store = SqlitePeerStore::default(); - let peer_ids = (0..100) - .map(|_| random_peer_id().unwrap()) - .collect::>(); + let peer_ids = (0..100).map(|_| random_peer_id()).collect::>(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); move || { for peer_id in peer_ids.clone() { - peer_store.new_connected_peer(&peer_id, addr.clone(), Endpoint::Dialer); + peer_store.new_connected_peer(&peer_id, addr.clone(), SessionType::Client); } } }) @@ -29,13 +28,11 @@ fn insert_peer_info_benchmark(c: &mut Criterion) { c.bench_function("insert 1000 peer_info", |b| { b.iter({ let mut peer_store = SqlitePeerStore::default(); - let peer_ids = (0..1000) - .map(|_| random_peer_id().unwrap()) - .collect::>(); + let peer_ids = (0..1000).map(|_| random_peer_id()).collect::>(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); move || { for peer_id in peer_ids.clone() { - peer_store.new_connected_peer(&peer_id, addr.clone(), Endpoint::Dialer); + peer_store.new_connected_peer(&peer_id, addr.clone(), SessionType::Client); } } }) @@ -45,13 +42,11 @@ fn insert_peer_info_benchmark(c: &mut Criterion) { c.bench_function("insert 100 peer_info on filesystem", move |b| { b.iter({ let mut peer_store = SqlitePeerStore::temp(); - let peer_ids = (0..100) - .map(|_| random_peer_id().unwrap()) - .collect::>(); + let peer_ids = (0..100).map(|_| random_peer_id()).collect::>(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); move || { for peer_id in peer_ids.clone() { - peer_store.new_connected_peer(&peer_id, addr.clone(), Endpoint::Dialer); + peer_store.new_connected_peer(&peer_id, addr.clone(), SessionType::Client); } } }) @@ -65,8 +60,8 @@ fn random_order_benchmark(c: &mut Criterion) { { let mut peer_store = peer_store.lock(); for _ in 0..8000 { - let peer_id = random_peer_id().unwrap(); - peer_store.new_connected_peer(&peer_id, addr.clone(), Endpoint::Dialer); + let peer_id = random_peer_id(); + peer_store.new_connected_peer(&peer_id, addr.clone(), SessionType::Client); let _ = peer_store.add_discovered_address(&peer_id, addr.clone()); } } @@ -112,8 +107,8 @@ fn random_order_benchmark(c: &mut Criterion) { let mut peer_store = SqlitePeerStore::temp(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); for _ in 0..8000 { - let peer_id = random_peer_id().unwrap(); - peer_store.new_connected_peer(&peer_id, addr.clone(), Endpoint::Dialer); + let peer_id = random_peer_id(); + peer_store.new_connected_peer(&peer_id, addr.clone(), SessionType::Client); let _ = peer_store.add_discovered_address(&peer_id, addr.clone()); } move || { diff --git a/network/src/errors.rs b/network/src/errors.rs index e6d10af34d..d302983ed9 100644 --- a/network/src/errors.rs +++ b/network/src/errors.rs @@ -1,5 +1,5 @@ use crate::{PeerIndex, ProtocolId}; -use p2p::PeerId; +use p2p::secio::PeerId; use std::error; use std::fmt; use std::fmt::Display; @@ -35,6 +35,7 @@ pub enum PeerError { pub enum ProtocolError { NotFound(ProtocolId), DisallowRegisterTimer, + Duplicate(ProtocolId), } impl From for Error { diff --git a/network/src/lib.rs b/network/src/lib.rs index 924be26947..29de26812d 100644 --- a/network/src/lib.rs +++ b/network/src/lib.rs @@ -5,23 +5,28 @@ mod network; mod network_config; mod network_group; mod network_service; +pub mod peer_store; +pub mod peers_registry; mod protocol; mod protocol_handler; mod service; -//mod outbound_peer_service; -pub mod peer_store; -mod peers_registry; -//mod ping_service; #[cfg(test)] mod tests; pub use crate::network::{Network, PeerInfo, SessionInfo}; pub use crate::network_config::NetworkConfig; pub use crate::network_service::NetworkService; +pub use crate::peers_registry::RegisterResult; pub use crate::protocol::{CKBProtocol, Event as CKBEvent}; pub use crate::protocol_handler::{CKBProtocolContext, CKBProtocolHandler, Severity}; pub use crate::service::timer_service::{Timer, TimerRegistry, TimerToken}; -pub use p2p::{multiaddr, PeerId, ProtocolId, SessionType}; +pub use p2p::{multiaddr, secio::PeerId, yamux::session::SessionType, ProtocolId}; +// p2p internal expose +pub(crate) use p2p::{ + context::{ServiceContext, SessionContext}, + service::ServiceControl, + SessionId, +}; use serde_derive::Deserialize; use std::sync::Arc; use std::time::Duration; @@ -92,7 +97,7 @@ impl From for NetworkConfig { } pub fn random_peer_id() -> PeerId { - use p2p::SecioKeyPair; + use p2p::secio::SecioKeyPair; let pubkey = SecioKeyPair::secp256k1_generated().to_public_key(); PeerId::from_public_key(&pubkey) } diff --git a/network/src/network.rs b/network/src/network.rs index f6822ff38c..953e719525 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -1,14 +1,19 @@ -use crate::errors::{ConfigError, Error, PeerError}; +use crate::errors::{ConfigError, Error, PeerError, ProtocolError}; use crate::peer_store::{Behaviour, PeerStore, SqlitePeerStore}; -use crate::peers_registry::{ConnectionStatus, Peer, PeerIdentifyInfo, PeersRegistry}; +use crate::peers_registry::{ + ConnectionStatus, Peer, PeerIdentifyInfo, PeersRegistry, RegisterResult, Session, +}; +use crate::protocol::Version as ProtocolVersion; use crate::protocol_handler::{CKBProtocolHandler, DefaultCKBProtocolContext}; use crate::service::{ ckb_service::CKBService, outbound_peer_service::OutboundPeerService, timer_service::{TimerRegistry, TimerService}, }; -use crate::{CKBEvent, CKBProtocol}; -use crate::{NetworkConfig, PeerIndex, ProtocolId}; +use crate::{ + CKBEvent, CKBProtocol, NetworkConfig, PeerIndex, ProtocolId, ServiceContext, ServiceControl, + SessionType, +}; use bytes::Bytes; use ckb_util::{Mutex, RwLock}; use fnv::FnvHashMap; @@ -21,11 +26,10 @@ use log::{debug, error, info, trace, warn}; use multiaddr::multihash::Multihash; use p2p::{ builder::ServiceBuilder, - context::{ServiceContext, ServiceControl}, multiaddr::{self, Multiaddr}, + secio::{PeerId, PublicKey}, service::{Service, ServiceError, ServiceEvent}, traits::ServiceHandle, - PeerId, PublicKey, SessionType, }; use secio; use std::boxed::Box; @@ -98,11 +102,10 @@ impl Network { } pub fn drop_peer(&self, peer_id: &PeerId) { + debug!(target: "network", "drop peer {:?}", peer_id); if let Some(peer) = self.peers_registry.write().drop_peer(&peer_id) { let mut p2p_control = self.p2p_control.write(); - for s in peer.sessions { - p2p_control.disconnect(s.id); - } + p2p_control.disconnect(peer.session.id); } } @@ -110,10 +113,8 @@ impl Network { debug!(target: "network", "drop all connections..."); let mut peers_registry = self.peers_registry.write(); let mut p2p_control = self.p2p_control.write(); - for (peer_id, peer) in peers_registry.peers_iter() { - for s in &peer.sessions { - p2p_control.disconnect(s.id); - } + for (_peer_id, peer) in peers_registry.peers_iter() { + p2p_control.disconnect(peer.session.id); } peers_registry.drop_all(); } @@ -268,68 +269,71 @@ impl Network { self.peers_registry .read() .get(peer_id) - .map( - |peer| match peer.sessions.iter().find(|s| s.protocol_id == protocol_id) { - Some(session) => { - self.p2p_control.write().send_message( - Some(vec![session.id]), - session.protocol_id, - data.to_vec(), - ); - //sender.unbounded_send(data).map_err(|err| { - // Error::from(ErrorKind::Other(format!("send to error: {:?}", err))) - //})?; - Ok(()) - } - None => { - Err(PeerError::ProtocolNotFound(peer_id.to_owned(), protocol_id).into()) - } - }, - ) + .map(|peer| match peer.protocol_version(protocol_id) { + Some(_) => { + self.p2p_control.write().send_message( + peer.session.id, + protocol_id, + data.to_vec(), + ); + Ok(()) + } + None => Err(PeerError::ProtocolNotFound(peer_id.to_owned(), protocol_id).into()), + }) .unwrap_or_else(|| Err(PeerError::NotFound(peer_id.to_owned()).into())) } - pub(crate) fn new_outbound_connection( - &self, - peer_id: PeerId, - connected_addr: Multiaddr, - ) -> Result { - self.peers_registry - .write() - .try_outbound_peer(peer_id, connected_addr) - } - - pub(crate) fn new_inbound_connection( + pub(crate) fn accept_connection( &self, peer_id: PeerId, connected_addr: Multiaddr, - ) -> Result { - self.peers_registry - .write() - .accept_inbound_peer(peer_id, connected_addr) + session: Session, + protocol_id: ProtocolId, + protocol_version: ProtocolVersion, + ) -> Result { + let mut peers_registry = self.peers_registry.write(); + let register_result = match session.session_type { + SessionType::Client => { + peers_registry.try_outbound_peer(peer_id.clone(), connected_addr, session) + } + SessionType::Server => { + peers_registry.accept_inbound_peer(peer_id.clone(), connected_addr, session) + } + }?; + // add session to peer + match peers_registry.get_mut(&peer_id) { + Some(peer) => match peer.protocol_version(protocol_id) { + Some(_) => return Err(ProtocolError::Duplicate(protocol_id).into()), + None => { + peer.protocols.push((protocol_id, protocol_version)); + } + }, + None => unreachable!("get peer after inserted"), + } + Ok(register_result) } pub fn peer_protocol_version(&self, peer_id: &PeerId, protocol_id: ProtocolId) -> Option { let peers_registry = self.peers_registry.read(); peers_registry.get(peer_id).and_then(|peer| { - peer.sessions + peer.protocols .iter() - .find(|s| s.protocol_id == protocol_id) - .map(|s| s.protocol_version) + .find(|(id, _)| id == &protocol_id) + .map(|(_, version)| *version) }) } pub fn session_info(&self, peer_id: &PeerId, protocol_id: ProtocolId) -> Option { let peers_registry = self.peers_registry.read(); peers_registry.get(peer_id).map(|peer| { let protocol_version = peer - .sessions + .protocols .iter() - .find(|s| s.protocol_id == protocol_id) - .map(|s| s.protocol_version); + .find(|(id, _)| id == &protocol_id) + .map(|(_, version)| *version); SessionInfo { peer: PeerInfo { peer_id: peer_id.to_owned(), - session_type: peer.session_type, + session_type: peer.session.session_type, last_ping_time: peer.last_ping_time, connected_addr: peer.connected_addr.clone(), identify_info: peer.identify_info.clone(), @@ -395,7 +399,10 @@ impl Network { for (ckb_protocol, _) in &ckb_protocols { p2p_service = p2p_service.insert_protocol(ckb_protocol.clone()); } - let mut p2p_service = p2p_service.build(EventHandler {}); + let mut p2p_service = p2p_service + .key_pair(local_private_key.clone()) + .build(EventHandler {}); + let p2p_control = p2p_service.control().clone(); let network: Arc = Arc::new(Network { peers_registry: RwLock::new(peers_registry), @@ -419,7 +426,7 @@ impl Network { } // listen local addresses for addr in &config.listen_addresses { - match p2p_service.listen(&addr) { + match p2p_service.listen(addr.to_owned()) { Ok(listen_address) => { info!( target: "network", @@ -447,7 +454,7 @@ impl Network { // dial reserved nodes and bootnodes { let network = Arc::clone(&network); - let dial_timeout = Duration::from_secs(DIAL_BOOTNODE_TIMEOUT); + let _dial_timeout = Duration::from_secs(DIAL_BOOTNODE_TIMEOUT); // dial reserved_nodes for (peer_id, addr) in config.reserved_peers()? { network.dial(&peer_id, addr); @@ -477,10 +484,10 @@ impl Network { ckb_event_receiver: Receiver, ) -> Result + Send>, Error> { let local_private_key = network.local_private_key().to_owned(); - let local_peer_id: PeerId = local_private_key.to_peer_id(); - let client_version = config.client_version.clone(); - let protocol_version = config.protocol_version.clone(); - let max_outbound = config.max_outbound_peers as usize; + let _local_peer_id: PeerId = local_private_key.to_peer_id(); + let _client_version = config.client_version.clone(); + let _protocol_version = config.protocol_version.clone(); + let _max_outbound = config.max_outbound_peers as usize; // initialize ckb_protocols //let ping_service = Arc::new(PingService::new(config.ping_interval, config.ping_timeout)); @@ -503,12 +510,12 @@ impl Network { Box::new( p2p_service .for_each(|_| Ok(())) - .map_err(|err| Error::Shutdown), + .map_err(|_err| Error::Shutdown), ), Box::new( ckb_service .for_each(|_| Ok(())) - .map_err(|err| Error::Shutdown), + .map_err(|_err| Error::Shutdown), ), // Box::new( // discovery_query_service @@ -532,7 +539,7 @@ impl Network { .for_each(|_| Ok(())) .map_err(|_| Error::Shutdown), ), - Box::new(close_rx.map_err(|err| Error::Shutdown)), + Box::new(close_rx.map_err(|_err| Error::Shutdown)), ]; let service_futures = select_all(futures) .and_then({ @@ -582,7 +589,7 @@ pub struct EventHandler {} impl ServiceHandle for EventHandler { fn handle_error(&mut self, _env: &mut ServiceContext, error: ServiceError) { - trace!(target: "network", "p2p service error: {:?}", error); + debug!(target: "network", "p2p service error: {:?}", error); } fn handle_event(&mut self, _env: &mut ServiceContext, event: ServiceEvent) { diff --git a/network/src/network_config.rs b/network/src/network_config.rs index 8c0a1ebd89..c07c1936f8 100644 --- a/network/src/network_config.rs +++ b/network/src/network_config.rs @@ -62,7 +62,7 @@ impl NetworkConfig { let mut key: [u8; 32] = [0; 32]; rand::thread_rng().fill(&mut key); self.secret_key = Some(Bytes::from(key.to_vec())); - secio::SecioKeyPair::secp256k1_raw_key(&key).map_err(|err| ConfigError::InvalidKey.into()) + secio::SecioKeyPair::secp256k1_raw_key(&key).map_err(|_err| ConfigError::InvalidKey.into()) } pub fn write_secret_key_to_file(&mut self) -> Result<(), IoError> { @@ -83,7 +83,7 @@ impl NetworkConfig { if let Some(secret) = self.read_secret_key() { Some( secio::SecioKeyPair::secp256k1_raw_key(&secret) - .map_err(|err| ConfigError::InvalidKey.into()), + .map_err(|_err| ConfigError::InvalidKey.into()), ) } else { None diff --git a/network/src/network_service.rs b/network/src/network_service.rs index b336cc96cd..d0e3074d44 100644 --- a/network/src/network_service.rs +++ b/network/src/network_service.rs @@ -95,7 +95,7 @@ impl NetworkService { } } }); - init_rx.wait().map_err(|err| Error::Shutdown)?; + init_rx.wait().map_err(|_err| Error::Shutdown)?; Ok(NetworkService { network, stop_handler: Mutex::new(Some(StopHandler::new(close_tx, join_handle))), diff --git a/network/src/peer_store.rs b/network/src/peer_store.rs index a7e08a4ac6..6b84dd7bd4 100644 --- a/network/src/peer_store.rs +++ b/network/src/peer_store.rs @@ -1,13 +1,13 @@ mod db; pub mod sqlite; -pub use crate::peer_store::sqlite_peer_store::SqlitePeerStore; +pub use crate::{peer_store::sqlite_peer_store::SqlitePeerStore, SessionType}; #[cfg(db_trace)] pub mod db_trace; pub(crate) mod sqlite_peer_store; -use crate::PeerId; +pub(crate) use crate::PeerId; use fnv::FnvHashMap; -use p2p::{multiaddr::Multiaddr, SessionType}; +use p2p::multiaddr::Multiaddr; use std::time::Duration; #[allow(dead_code)] diff --git a/network/src/peer_store/db.rs b/network/src/peer_store/db.rs index b7a338a962..37aed08d45 100644 --- a/network/src/peer_store/db.rs +++ b/network/src/peer_store/db.rs @@ -1,7 +1,7 @@ use super::{Multiaddr, PeerId, Score, Status}; use crate::network_group::{Group, NetworkGroup}; use crate::peer_store::sqlite::Error as SqliteError; -use p2p::SessionType; +use crate::SessionType; use rusqlite::types::ToSql; use rusqlite::OptionalExtension; use rusqlite::{Connection, NO_PARAMS}; diff --git a/network/src/peer_store/sqlite_peer_store.rs b/network/src/peer_store/sqlite_peer_store.rs index c165bb2f5b..c2e9310f29 100644 --- a/network/src/peer_store/sqlite_peer_store.rs +++ b/network/src/peer_store/sqlite_peer_store.rs @@ -2,10 +2,10 @@ use super::{Behaviour, Multiaddr, PeerId, PeerStore, ReportResult, Score, Scorin use crate::network_group::MultiaddrExt; use crate::peer_store::db; use crate::peer_store::sqlite::{self, ConnectionPool, ConnectionPoolExt}; +use crate::SessionType; use faketime::unix_time; use fnv::FnvHashMap; use log::debug; -use p2p::SessionType; use std::net::IpAddr; use std::time::Duration; diff --git a/network/src/peers_registry.rs b/network/src/peers_registry.rs index eb84f1d20a..593708f021 100644 --- a/network/src/peers_registry.rs +++ b/network/src/peers_registry.rs @@ -3,7 +3,7 @@ use crate::peer_store::PeerStore; use crate::protocol::Version; use crate::{ errors::{Error, PeerError}, - PeerId, PeerIndex, ProtocolId, + PeerId, PeerIndex, ProtocolId, SessionType, }; use bytes::Bytes; use ckb_util::RwLock; @@ -13,15 +13,31 @@ use futures::sync::mpsc::UnboundedSender; use log::debug; pub use p2p::{ multiaddr::{Multiaddr, ToMultiaddr}, - SessionId, SessionType, + SessionId, }; use rand::seq::SliceRandom; use rand::thread_rng; +use std::collections::hash_map::Entry; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc; pub(crate) const EVICTION_PROTECT_PEERS: usize = 8; +#[derive(Clone, Copy, Eq, PartialEq, Debug)] +pub enum RegisterResult { + New(PeerIndex), + Exist(PeerIndex), +} + +impl RegisterResult { + pub fn peer_index(&self) -> PeerIndex { + match self { + RegisterResult::New(peer_index) => *peer_index, + RegisterResult::Exist(peer_index) => *peer_index, + } + } +} + struct PeerManage { id_allocator: AtomicUsize, peers: FnvHashMap, @@ -62,24 +78,28 @@ impl PeerManage { &mut self, peer_id: PeerId, connected_addr: Multiaddr, - session_type: SessionType, - ) -> PeerIndex { - let peer_index = self.id_allocator.fetch_add(1, Ordering::Relaxed); - let peer = Peer { - session_type, - connected_addr, - identify_info: None, - ping: None, - last_ping_time: None, - last_message_time: None, - connected_time: unix_time_as_millis(), - peer_index: peer_index, - sessions: Vec::with_capacity(1), - }; - - self.peers.entry(peer_id.clone()).or_insert(peer); - self.peer_id_by_index.entry(peer_index).or_insert(peer_id); - peer_index + session: Session, + ) -> RegisterResult { + match self.peers.entry(peer_id.clone()) { + Entry::Occupied(entry) => RegisterResult::Exist(entry.get().peer_index), + Entry::Vacant(entry) => { + let peer_index = self.id_allocator.fetch_add(1, Ordering::Relaxed); + let peer = Peer { + connected_addr, + identify_info: None, + ping: None, + last_ping_time: None, + last_message_time: None, + connected_time: unix_time_as_millis(), + peer_index: peer_index, + session: session, + protocols: Vec::new(), + }; + entry.insert(peer); + self.peer_id_by_index.insert(peer_index, peer_id); + RegisterResult::New(peer_index) + } + } } fn clear(&mut self) { @@ -107,29 +127,29 @@ pub struct PeerIdentifyInfo { pub count_of_known_listen_addrs: usize, } +#[derive(Clone, Debug, Copy, Eq, PartialEq)] pub struct Session { pub id: SessionId, - pub protocol_id: ProtocolId, - pub protocol_version: Version, + pub session_type: SessionType, } pub struct Peer { pub(crate) peer_index: PeerIndex, pub connected_addr: Multiaddr, // Client or Server - pub session_type: SessionType, pub identify_info: Option, pub last_ping_time: Option, pub last_message_time: Option, pub ping: Option, pub connected_time: u64, - pub sessions: Vec, + pub session: Session, + pub protocols: Vec<(ProtocolId, Version)>, } impl Peer { #[inline] pub fn is_outbound(&self) -> bool { - self.session_type == SessionType::Client + self.session.session_type == SessionType::Client } #[allow(dead_code)] @@ -143,8 +163,16 @@ impl Peer { fn network_group(&self) -> Group { self.connected_addr.network_group() } + + pub fn protocol_version(&self, protocol_id: ProtocolId) -> Option { + self.protocols + .iter() + .find(|(id, _)| id == &protocol_id) + .map(|(_, version)| *version) + } } +#[derive(Clone, Copy, Debug)] pub struct ConnectionStatus { pub total: u32, pub unreserved_inbound: u32, @@ -230,9 +258,10 @@ impl PeersRegistry { &mut self, peer_id: PeerId, addr: Multiaddr, - ) -> Result { + session: Session, + ) -> Result { if let Some(peer) = self.peers.get(&peer_id) { - return Ok(peer.peer_index); + return Ok(RegisterResult::Exist(peer.peer_index)); } if !self.is_reserved(&peer_id) { if self.reserved_only { @@ -250,7 +279,33 @@ impl PeersRegistry { return Err(Error::Peer(PeerError::ReachMaxInboundLimit(peer_id))); } } - Ok(self.new_peer(peer_id, addr, SessionType::Server)) + Ok(self.register_peer(peer_id, addr, session)) + } + + pub fn try_outbound_peer( + &mut self, + peer_id: PeerId, + addr: Multiaddr, + session: Session, + ) -> Result { + if let Some(peer) = self.peers.get(&peer_id) { + return Ok(RegisterResult::Exist(peer.peer_index)); + } + if !self.is_reserved(&peer_id) { + if self.reserved_only { + return Err(Error::Peer(PeerError::NonReserved(peer_id))); + } + if self.peer_store.read().is_banned(&peer_id) { + return Err(Error::Peer(PeerError::Banned(peer_id))); + } + let connection_status = self.connection_status(); + // check peers connection limitation + // TODO: implement extra outbound peer logic + if connection_status.unreserved_outbound >= self.max_outbound { + return Err(Error::Peer(PeerError::ReachMaxOutboundLimit(peer_id))); + } + } + Ok(self.register_peer(peer_id, addr, session)) } fn try_evict_inbound_peer(&mut self) -> bool { @@ -320,43 +375,19 @@ impl PeersRegistry { true } - pub fn try_outbound_peer( - &mut self, - peer_id: PeerId, - addr: Multiaddr, - ) -> Result { - if let Some(peer) = self.peers.get(&peer_id) { - return Ok(peer.peer_index); - } - if !self.is_reserved(&peer_id) { - if self.reserved_only { - return Err(Error::Peer(PeerError::NonReserved(peer_id))); - } - if self.peer_store.read().is_banned(&peer_id) { - return Err(Error::Peer(PeerError::Banned(peer_id))); - } - let connection_status = self.connection_status(); - // check peers connection limitation - // TODO: implement extra outbound peer logic - if connection_status.unreserved_outbound >= self.max_outbound { - return Err(Error::Peer(PeerError::ReachMaxOutboundLimit(peer_id))); - } - } - Ok(self.new_peer(peer_id, addr, SessionType::Client)) - } - // registry a new peer - fn new_peer( + fn register_peer( &mut self, peer_id: PeerId, connected_addr: Multiaddr, - session_type: SessionType, - ) -> PeerIndex { - self.peer_store - .write() - .new_connected_peer(&peer_id, connected_addr.clone(), session_type); - let peer_index = self.peers.add_peer(peer_id, connected_addr, session_type); - peer_index + session: Session, + ) -> RegisterResult { + self.peer_store.write().new_connected_peer( + &peer_id, + connected_addr.clone(), + session.session_type, + ); + self.peers.add_peer(peer_id, connected_addr, session) } #[inline] diff --git a/network/src/protocol.rs b/network/src/protocol.rs index afda7fc3a8..a32faf91af 100644 --- a/network/src/protocol.rs +++ b/network/src/protocol.rs @@ -1,15 +1,17 @@ use crate::errors::{Error, ProtocolError}; +use crate::{ + peers_registry::Session, PeerId, ServiceContext, SessionContext, SessionId, SessionType, +}; use bytes::BufMut; use bytes::{Buf, IntoBuf}; use bytes::{Bytes, BytesMut}; use futures::sync::mpsc::{self, Sender}; use futures::{future, stream, Future, Sink, Stream}; -use log::{debug, error, trace}; +use log::{debug, error, info, trace}; use p2p::{ - context::{ServiceContext, SessionContext}, multiaddr::Multiaddr, traits::{ProtocolMeta, ServiceProtocol}, - PeerId, ProtocolId, SessionId, SessionType, + ProtocolId, }; use std::io::{self, Error as IoError, ErrorKind as IoErrorKind}; use std::string::ToString; @@ -85,8 +87,7 @@ impl ProtocolMeta for CKBProtocol { } pub enum Event { - Connected(PeerId, Multiaddr, ProtocolId, SessionType, Version), - ConnectedError(Multiaddr), + Connected(PeerId, Multiaddr, Session, ProtocolId, Version), Disconnected(PeerId, ProtocolId), Received(PeerId, ProtocolId, Vec), Notify(ProtocolId, u64), @@ -99,48 +100,58 @@ struct CKBHandler { impl ServiceProtocol for CKBHandler { fn init(&mut self, _control: &mut ServiceContext) {} - fn connected( - &mut self, - _control: &mut ServiceContext, - session: &SessionContext, - version: &str, - ) { - let event = match session.remote_pubkey { - Some(ref pubkey) => { - let peer_id = pubkey.peer_id(); - Event::Connected( - peer_id, - session.address.clone(), - self.id, - session.ty, - version.parse::().expect("version"), - ) + fn connected(&mut self, control: &mut ServiceContext, session: &SessionContext, version: &str) { + let (peer_id, version) = { + let parsed_version = version.parse::(); + if session.remote_pubkey.is_none() || parsed_version.is_err() { + error!(target: "network", "ckb protocol connected error, addr: {}, version: {}", session.address, version); + control.disconnect(session.id); + return; } - None => Event::ConnectedError(session.address.clone()), + ( + session + .remote_pubkey + .as_ref() + .map(|pubkey| pubkey.peer_id()) + .unwrap(), + parsed_version.unwrap(), + ) }; + let event = Event::Connected( + peer_id, + session.address.clone(), + Session { + id: session.id, + session_type: session.ty, + }, + self.id, + version, + ); self.event_sender.try_send(event); } fn disconnected(&mut self, _control: &mut ServiceContext, session: &SessionContext) { - let peer_id = session + if let Some(peer_id) = session .remote_pubkey .as_ref() .map(|pubkey| pubkey.peer_id()) - .expect("pubkey"); - self.event_sender - .try_send(Event::Disconnected(peer_id, self.id)); + { + self.event_sender + .try_send(Event::Disconnected(peer_id, self.id)); + } } - fn received(&mut self, control: &mut ServiceContext, session: &SessionContext, data: Vec) { - let peer_id = session + fn received(&mut self, _control: &mut ServiceContext, session: &SessionContext, data: Vec) { + if let Some(peer_id) = session .remote_pubkey .as_ref() .map(|pubkey| pubkey.peer_id()) - .expect("pubkey"); - self.event_sender - .try_send(Event::Received(peer_id, self.id, data)); + { + self.event_sender + .try_send(Event::Received(peer_id, self.id, data)); + } } - fn notify(&mut self, control: &mut ServiceContext, token: u64) { + fn notify(&mut self, _control: &mut ServiceContext, token: u64) { self.event_sender.try_send(Event::Notify(self.id, token)); } } diff --git a/network/src/service/ckb_service.rs b/network/src/service/ckb_service.rs index 436202efa5..0fdde40065 100644 --- a/network/src/service/ckb_service.rs +++ b/network/src/service/ckb_service.rs @@ -1,6 +1,9 @@ use crate::peer_store::{Behaviour, Status}; use crate::protocol_handler::DefaultCKBProtocolContext; -use crate::{CKBEvent, CKBProtocol, CKBProtocolHandler, Network, PeerId}; +use crate::{ + peers_registry::RegisterResult, CKBEvent, CKBProtocol, CKBProtocolHandler, Network, PeerId, + SessionType, +}; use faketime::unix_time_as_millis; use futures::{ future::{self, Future}, @@ -8,7 +11,7 @@ use futures::{ Async, Stream, }; use log::{debug, error, info, trace, warn}; -use p2p::{context::ServiceControl, multiaddr::Multiaddr, ProtocolId, SessionType}; +use p2p::{multiaddr::Multiaddr, service::ServiceControl, ProtocolId}; use std::boxed::Box; use std::io::{Error as IoError, ErrorKind as IoErrorKind}; use std::sync::Arc; @@ -35,20 +38,17 @@ impl Stream for CKBService { let network = Arc::clone(&self.network); match try_ready!(self.event_receiver.poll()) { - Some(Connected(peer_id, addr, protocol_id, session_type, version)) => { - let connect_result = match session_type { - SessionType::Client => { - network.new_outbound_connection(peer_id.clone(), addr.clone()) - } - SessionType::Server => { - network.new_inbound_connection(peer_id.clone(), addr.clone()) - } - }; - - match connect_result { - Ok(peer_index) => { + Some(Connected(peer_id, addr, session, protocol_id, protocol_version)) => { + match network.accept_connection( + peer_id.clone(), + addr.clone(), + session, + protocol_id, + protocol_version, + ) { + Ok(register_result) => { // update status in peer_store - { + if let RegisterResult::New(_) = register_result { let mut peer_store = network.peer_store().write(); peer_store.report(&peer_id, Behaviour::Connect); peer_store.update_status(&peer_id, Status::Connected); @@ -61,21 +61,20 @@ impl Stream for CKBService { Arc::clone(&network), protocol_id, )), - peer_index, + register_result.peer_index(), ), None => { - error!(target: "network", "can't find protocol handler for {}", protocol_id) + network.drop_peer(&peer_id); + error!(target: "network", "can't find protocol handler for {:?}",session) } } } Err(err) => { + network.drop_peer(&peer_id); info!(target: "network", "reject connection from {} {}, because {}", peer_id.to_base58(), addr, err) } } } - Some(ConnectedError(addr)) => { - error!(target: "network", "ckb protocol connected error, addr: {}", addr); - } Some(Disconnected(peer_id, protocol_id)) => { // update disconnect in peer_store { diff --git a/network/src/tests/peers_registry.rs b/network/src/tests/peers_registry.rs index 2607b8fedc..7280759e0e 100644 --- a/network/src/tests/peers_registry.rs +++ b/network/src/tests/peers_registry.rs @@ -1,71 +1,80 @@ use crate::{ multiaddr::ToMultiaddr, peer_store::{Behaviour, PeerStore, SqlitePeerStore}, - peers_registry::{PeersRegistry, EVICTION_PROTECT_PEERS}, - random_peer_id, + peers_registry::{PeersRegistry, Session, EVICTION_PROTECT_PEERS}, + random_peer_id, SessionType, }; use ckb_util::RwLock; use faketime::unix_time_as_millis; use std::sync::Arc; -fn new_peer_store() -> impl PeerStore { - SqlitePeerStore::temp() +fn new_peer_store() -> Arc> { + Arc::new(RwLock::new(SqlitePeerStore::temp())) } #[test] fn test_accept_inbound_peer_in_reserve_only_mode() { - let peer_store: Arc> = Arc::new(RwLock::new(new_peer_store())); + let peer_store = new_peer_store(); let reserved_peer = random_peer_id(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); + let session = Session { + id: 1, + session_type: SessionType::Server, + }; // reserved_only mode: only accept reserved_peer - let mut peers_registry = PeersRegistry::new( + let mut peers = PeersRegistry::new( Arc::clone(&peer_store), 3, 3, true, vec![reserved_peer.clone()], ); - assert!(peers_registry - .accept_inbound_peer(random_peer_id(), addr.clone()) + assert!(peers + .accept_inbound_peer(random_peer_id(), addr.clone(), session) .is_err()); - peers_registry - .accept_inbound_peer(reserved_peer.clone(), addr.clone()) + peers + .accept_inbound_peer(reserved_peer.clone(), addr.clone(), session) .expect("accept"); } #[test] fn test_accept_inbound_peer_until_full() { - let peer_store: Arc> = Arc::new(RwLock::new(new_peer_store())); + let peer_store = new_peer_store(); let reserved_peer = random_peer_id(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); + let session = Session { + id: 1, + session_type: SessionType::Server, + }; // accept node until inbound connections is full - let mut peers_registry = PeersRegistry::new( + let mut peers = PeersRegistry::new( Arc::clone(&peer_store), 3, 3, false, vec![reserved_peer.clone()], ); - peers_registry - .accept_inbound_peer(random_peer_id(), addr.clone()) + peers + .accept_inbound_peer(random_peer_id(), addr.clone(), session) .expect("accept"); - peers_registry - .accept_inbound_peer(random_peer_id(), addr.clone()) + peers + .accept_inbound_peer(random_peer_id(), addr.clone(), session) .expect("accept"); - peers_registry - .accept_inbound_peer(random_peer_id(), addr.clone()) + peers + .accept_inbound_peer(random_peer_id(), addr.clone(), session) .expect("accept"); - assert!(peers_registry - .accept_inbound_peer(random_peer_id(), addr.clone()) + println!("{:?}", peers.connection_status()); + assert!(peers + .accept_inbound_peer(random_peer_id(), addr.clone(), session) .is_err(),); // should still accept reserved peer - peers_registry - .accept_inbound_peer(reserved_peer.clone(), addr.clone()) + peers + .accept_inbound_peer(reserved_peer.clone(), addr.clone(), session) .expect("accept"); // should refuse accept low score peer - assert!(peers_registry - .accept_inbound_peer(random_peer_id(), addr.clone()) + assert!(peers + .accept_inbound_peer(random_peer_id(), addr.clone(), session) .is_err()); } @@ -75,12 +84,16 @@ fn test_accept_inbound_peer_eviction() { // 1. should evict from largest network groups // 2. should never evict reserved peer // 3. should evict lowest scored peer - let peer_store: Arc> = Arc::new(RwLock::new(new_peer_store())); + let peer_store = new_peer_store(); let reserved_peer = random_peer_id(); let evict_target = random_peer_id(); let lowest_score_peer = random_peer_id(); let addr1 = "/ip4/127.0.0.1".to_multiaddr().unwrap(); let addr2 = "/ip4/192.168.0.1".to_multiaddr().unwrap(); + let session = Session { + id: 1, + session_type: SessionType::Server, + }; // prepare protected peers let longest_connection_time_peers_count = 5; let protected_peers_count = 3 * EVICTION_PROTECT_PEERS + longest_connection_time_peers_count; @@ -93,7 +106,7 @@ fn test_accept_inbound_peer_eviction() { ); for _ in 0..protected_peers_count { assert!(peers_registry - .accept_inbound_peer(random_peer_id(), addr2.clone()) + .accept_inbound_peer(random_peer_id(), addr2.clone(), session) .is_ok()); } let mut peers_iter = peers_registry @@ -132,23 +145,23 @@ fn test_accept_inbound_peer_eviction() { let mut new_peer_ids = (0..3).map(|_| random_peer_id()).collect::>(); // setup 3 node and 1 reserved node from addr1 peers_registry - .accept_inbound_peer(reserved_peer.clone(), addr1.clone()) + .accept_inbound_peer(reserved_peer.clone(), addr1.clone(), session) .expect("accept"); peers_registry - .accept_inbound_peer(evict_target.clone(), addr1.clone()) + .accept_inbound_peer(evict_target.clone(), addr1.clone(), session) .expect("accept"); peers_registry - .accept_inbound_peer(new_peer_ids[0].clone(), addr1.clone()) + .accept_inbound_peer(new_peer_ids[0].clone(), addr1.clone(), session) .expect("accept"); peers_registry - .accept_inbound_peer(new_peer_ids[1].clone(), addr1.clone()) + .accept_inbound_peer(new_peer_ids[1].clone(), addr1.clone(), session) .expect("accept"); // setup 2 node from addr2 peers_registry - .accept_inbound_peer(lowest_score_peer.clone(), addr2.clone()) + .accept_inbound_peer(lowest_score_peer.clone(), addr2.clone(), session) .expect("accept"); peers_registry - .accept_inbound_peer(new_peer_ids[2].clone(), addr2.clone()) + .accept_inbound_peer(new_peer_ids[2].clone(), addr2.clone(), session) .expect("accept"); // setup score { @@ -174,7 +187,7 @@ fn test_accept_inbound_peer_eviction() { // should evict evict target assert!(peers_registry.get(&evict_target).is_some()); peers_registry - .accept_inbound_peer(random_peer_id(), addr1.clone()) + .accept_inbound_peer(random_peer_id(), addr1.clone(), session) .expect("accept"); assert!(peers_registry.get(&evict_target).is_none()); } diff --git a/sync/src/synchronizer/mod.rs b/sync/src/synchronizer/mod.rs index 46e4371dfb..9792b3adfc 100644 --- a/sync/src/synchronizer/mod.rs +++ b/sync/src/synchronizer/mod.rs @@ -670,7 +670,7 @@ impl Synchronizer { let fbb = &mut FlatBufferBuilder::new(); let message = SyncMessage::build_get_blocks(fbb, v_fetch); fbb.finish(message, None); - let _ = nc.send(peer, fbb.finished_data().to_vec()); + let _ = nc.send(peer, fbb.finished_data().to_vec()).unwrap(); debug!(target: "sync", "send_getblocks len={:?} to peer={}", v_fetch.len() , peer); } } From 4475f474242cedb93245a2410cd482516bd94291 Mon Sep 17 00:00:00 2001 From: jjy Date: Fri, 1 Mar 2019 11:44:00 +0800 Subject: [PATCH 08/99] refactor: apply clippy suggestions --- network/src/errors.rs | 9 +- network/src/lib.rs | 3 - network/src/network.rs | 90 +++++++------------- network/src/network_config.rs | 4 +- network/src/network_service.rs | 5 +- network/src/peers_registry.rs | 6 +- network/src/protocol.rs | 38 ++++----- network/src/protocol_handler.rs | 6 +- network/src/service/ckb_service.rs | 19 ++--- network/src/service/outbound_peer_service.rs | 14 +-- network/src/service/timer_service.rs | 5 +- sync/src/synchronizer/mod.rs | 2 +- 12 files changed, 73 insertions(+), 128 deletions(-) diff --git a/network/src/errors.rs b/network/src/errors.rs index d302983ed9..5b9fa1f801 100644 --- a/network/src/errors.rs +++ b/network/src/errors.rs @@ -1,5 +1,5 @@ use crate::{PeerIndex, ProtocolId}; -use p2p::secio::PeerId; +use p2p::{error::Error as P2PError, secio::PeerId, service::ServiceTask}; use std::error; use std::fmt; use std::fmt::Display; @@ -11,6 +11,7 @@ pub enum Error { Config(ConfigError), Protocol(ProtocolError), Io(IoError), + P2P(P2PError), Shutdown, } @@ -62,6 +63,12 @@ impl From for Error { } } +impl From> for Error { + fn from(err: P2PError) -> Error { + Error::P2P(err) + } +} + impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self) diff --git a/network/src/lib.rs b/network/src/lib.rs index 29de26812d..34e6c3799a 100644 --- a/network/src/lib.rs +++ b/network/src/lib.rs @@ -25,11 +25,8 @@ pub use p2p::{multiaddr, secio::PeerId, yamux::session::SessionType, ProtocolId} pub(crate) use p2p::{ context::{ServiceContext, SessionContext}, service::ServiceControl, - SessionId, }; use serde_derive::Deserialize; -use std::sync::Arc; -use std::time::Duration; const DEFAULT_OUTGOING_PEERS_RATIO: u32 = 3; diff --git a/network/src/network.rs b/network/src/network.rs index 953e719525..9839db9087 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -17,12 +17,11 @@ use crate::{ use bytes::Bytes; use ckb_util::{Mutex, RwLock}; use fnv::FnvHashMap; -use futures::future::{self, select_all, Future}; -use futures::sync::mpsc::UnboundedSender; -use futures::sync::mpsc::{Receiver, Sender}; +use futures::future::{select_all, Future}; +use futures::sync::mpsc::Receiver; use futures::sync::oneshot; use futures::Stream; -use log::{debug, error, info, trace, warn}; +use log::{debug, error, info, warn}; use multiaddr::multihash::Multihash; use p2p::{ builder::ServiceBuilder, @@ -34,17 +33,22 @@ use p2p::{ use secio; use std::boxed::Box; use std::cmp::max; -use std::io::{Error as IoError, ErrorKind as IoErrorKind}; use std::sync::Arc; use std::time::Duration; use std::usize; use tokio::codec::LengthDelimitedCodec; -use tokio::io::{AsyncRead, AsyncWrite}; const DIAL_BOOTNODE_TIMEOUT: u64 = 20; -const PEER_ADDRS_COUNT: u32 = 5; pub type CKBProtocols = Vec<(CKBProtocol, Arc)>; +type NetworkResult = Result< + ( + Arc, + oneshot::Sender<()>, + Box + Send>, + ), + Error, +>; #[derive(Debug, Clone)] pub struct SessionInfo { @@ -105,7 +109,9 @@ impl Network { debug!(target: "network", "drop peer {:?}", peer_id); if let Some(peer) = self.peers_registry.write().drop_peer(&peer_id) { let mut p2p_control = self.p2p_control.write(); - p2p_control.disconnect(peer.session.id); + if let Err(err) = p2p_control.disconnect(peer.session.id) { + error!(target: "network", "disconnect peer error {:?}", err); + } } } @@ -114,7 +120,9 @@ impl Network { let mut peers_registry = self.peers_registry.write(); let mut p2p_control = self.p2p_control.write(); for (_peer_id, peer) in peers_registry.peers_iter() { - p2p_control.disconnect(peer.session.id); + if let Err(err) = p2p_control.disconnect(peer.session.id) { + error!(target: "network", "disconnect peer error {:?}", err); + } } peers_registry.drop_all(); } @@ -123,12 +131,6 @@ impl Network { &self.local_peer_id } - pub(crate) fn discovery_listened_address(&self, addr: Multiaddr) { - let mut listened_addresses = self.listened_addresses.write(); - let score = listened_addresses.entry(addr).or_insert(0); - *score = score.saturating_add(1); - } - pub(crate) fn listened_addresses(&self, count: usize) -> Vec<(Multiaddr, u8)> { let listened_addresses = self.listened_addresses.read(); listened_addresses @@ -169,34 +171,6 @@ impl Network { } } - pub(crate) fn get_peer_identify_info(&self, peer_id: &PeerId) -> Option { - let peers_registry = self.peers_registry.read(); - peers_registry - .get(peer_id) - .and_then(|peer| peer.identify_info.clone()) - } - - pub(crate) fn set_peer_identify_info( - &self, - peer_id: &PeerId, - identify_info: PeerIdentifyInfo, - ) -> Result<(), ()> { - let mut peers_registry = self.peers_registry.write(); - match peers_registry.get_mut(peer_id) { - Some(peer) => { - peer.identify_info = Some(identify_info); - Ok(()) - } - None => Err(()), - } - } - - pub(crate) fn get_peer_addresses(&self, peer_id: &PeerId) -> Vec { - let peer_store = self.peer_store.read(); - let addrs = peer_store.peer_addrs(&peer_id, PEER_ADDRS_COUNT); - addrs.unwrap_or_default() - } - pub(crate) fn peers(&self) -> impl Iterator { let peers_registry = self.peers_registry.read(); let peers = peers_registry @@ -270,14 +244,11 @@ impl Network { .read() .get(peer_id) .map(|peer| match peer.protocol_version(protocol_id) { - Some(_) => { - self.p2p_control.write().send_message( - peer.session.id, - protocol_id, - data.to_vec(), - ); - Ok(()) - } + Some(_) => self + .p2p_control + .write() + .send_message(peer.session.id, protocol_id, data.to_vec()) + .map_err(Into::into), None => Err(PeerError::ProtocolNotFound(peer_id.to_owned(), protocol_id).into()), }) .unwrap_or_else(|| Err(PeerError::NotFound(peer_id.to_owned()).into())) @@ -343,6 +314,12 @@ impl Network { }) } + pub fn dial_addr(&self, addr: Multiaddr) { + if let Err(err) = self.p2p_control.write().dial(addr) { + error!(target: "network", "failed to dial: {}", err); + } + } + pub fn dial(&self, expected_peer_id: &PeerId, mut addr: Multiaddr) { if expected_peer_id == self.local_peer_id() { debug!(target: "network", "ignore dial to self"); @@ -352,7 +329,7 @@ impl Network { match Multihash::from_bytes(expected_peer_id.as_bytes().to_vec()) { Ok(peer_id_hash) => { addr.append(multiaddr::Protocol::P2p(peer_id_hash)); - self.p2p_control.write().dial(addr); + self.dial_addr(addr); } Err(err) => { error!(target: "network", "failed to convert peer_id to addr: {}", err); @@ -563,14 +540,7 @@ impl Network { config: &NetworkConfig, ckb_protocols: CKBProtocols, ckb_event_receiver: Receiver, - ) -> Result< - ( - Arc, - oneshot::Sender<()>, - Box + Send>, - ), - Error, - > { + ) -> NetworkResult { let (network, p2p_service, timer_registry) = Self::inner_build(config, ckb_protocols)?; let (close_tx, close_rx) = oneshot::channel(); let network_future = Self::build_network_future( diff --git a/network/src/network_config.rs b/network/src/network_config.rs index c07c1936f8..f2dd08a5c6 100644 --- a/network/src/network_config.rs +++ b/network/src/network_config.rs @@ -2,14 +2,14 @@ use crate::errors::{ConfigError, Error}; use crate::PeerId; use bytes::Bytes; use log::info; -use p2p::multiaddr::{self, Multiaddr, Protocol, ToMultiaddr}; +use p2p::multiaddr::{Multiaddr, Protocol, ToMultiaddr}; use rand; use rand::Rng; use secio; use std::fs; +use std::io::Error as IoError; use std::io::Read; use std::io::Write; -use std::io::{Error as IoError, ErrorKind as IoErrorKind}; use std::iter; use std::net::Ipv4Addr; use std::time::Duration; diff --git a/network/src/network_service.rs b/network/src/network_service.rs index d0e3074d44..12401c53ec 100644 --- a/network/src/network_service.rs +++ b/network/src/network_service.rs @@ -1,10 +1,9 @@ use crate::network::{CKBProtocols, Network}; -use crate::protocol_handler::CKBProtocolHandler; use crate::protocol_handler::{CKBProtocolContext, DefaultCKBProtocolContext}; -use crate::{errors::Error, CKBEvent, CKBProtocol, NetworkConfig, ProtocolId}; +use crate::{errors::Error, CKBEvent, NetworkConfig, ProtocolId}; use ckb_util::Mutex; use futures::future::Future; -use futures::sync::mpsc::{Receiver, Sender}; +use futures::sync::mpsc::Receiver; use futures::sync::oneshot; use libp2p::{Multiaddr, PeerId}; use log::{debug, info}; diff --git a/network/src/peers_registry.rs b/network/src/peers_registry.rs index 593708f021..27b26625f1 100644 --- a/network/src/peers_registry.rs +++ b/network/src/peers_registry.rs @@ -5,11 +5,9 @@ use crate::{ errors::{Error, PeerError}, PeerId, PeerIndex, ProtocolId, SessionType, }; -use bytes::Bytes; use ckb_util::RwLock; use faketime::unix_time_as_millis; use fnv::{FnvHashMap, FnvHashSet}; -use futures::sync::mpsc::UnboundedSender; use log::debug; pub use p2p::{ multiaddr::{Multiaddr, ToMultiaddr}, @@ -91,8 +89,8 @@ impl PeerManage { last_ping_time: None, last_message_time: None, connected_time: unix_time_as_millis(), - peer_index: peer_index, - session: session, + peer_index, + session, protocols: Vec::new(), }; entry.insert(peer); diff --git a/network/src/protocol.rs b/network/src/protocol.rs index a32faf91af..48115f4eba 100644 --- a/network/src/protocol.rs +++ b/network/src/protocol.rs @@ -1,24 +1,13 @@ -use crate::errors::{Error, ProtocolError}; -use crate::{ - peers_registry::Session, PeerId, ServiceContext, SessionContext, SessionId, SessionType, -}; -use bytes::BufMut; -use bytes::{Buf, IntoBuf}; -use bytes::{Bytes, BytesMut}; -use futures::sync::mpsc::{self, Sender}; -use futures::{future, stream, Future, Sink, Stream}; -use log::{debug, error, info, trace}; +use crate::{peers_registry::Session, PeerId, ServiceContext, SessionContext}; +use bytes::Bytes; +use futures::sync::mpsc::Sender; +use log::error; use p2p::{ multiaddr::Multiaddr, traits::{ProtocolMeta, ServiceProtocol}, ProtocolId, }; -use std::io::{self, Error as IoError, ErrorKind as IoErrorKind}; -use std::string::ToString; -use std::vec::IntoIter as VecIntoIter; -use tokio::codec::Decoder; use tokio::codec::LengthDelimitedCodec; -use tokio::io::{AsyncRead, AsyncWrite}; pub type Version = u8; @@ -86,6 +75,7 @@ impl ProtocolMeta for CKBProtocol { } } +#[derive(Debug)] pub enum Event { Connected(PeerId, Multiaddr, Session, ProtocolId, Version), Disconnected(PeerId, ProtocolId), @@ -98,6 +88,14 @@ struct CKBHandler { event_sender: Sender, } +impl CKBHandler { + fn send_event(&mut self, event: Event) { + if let Err(err) = self.event_sender.try_send(event) { + error!(target: "network", "ckb protocol send event error : {:?}", err) + } + } +} + impl ServiceProtocol for CKBHandler { fn init(&mut self, _control: &mut ServiceContext) {} fn connected(&mut self, control: &mut ServiceContext, session: &SessionContext, version: &str) { @@ -127,7 +125,7 @@ impl ServiceProtocol for CKBHandler { self.id, version, ); - self.event_sender.try_send(event); + self.send_event(event); } fn disconnected(&mut self, _control: &mut ServiceContext, session: &SessionContext) { @@ -136,8 +134,7 @@ impl ServiceProtocol for CKBHandler { .as_ref() .map(|pubkey| pubkey.peer_id()) { - self.event_sender - .try_send(Event::Disconnected(peer_id, self.id)); + self.send_event(Event::Disconnected(peer_id, self.id)); } } @@ -147,11 +144,10 @@ impl ServiceProtocol for CKBHandler { .as_ref() .map(|pubkey| pubkey.peer_id()) { - self.event_sender - .try_send(Event::Received(peer_id, self.id, data)); + self.send_event(Event::Received(peer_id, self.id, data)); } } fn notify(&mut self, _control: &mut ServiceContext, token: u64) { - self.event_sender.try_send(Event::Notify(self.id, token)); + self.send_event(Event::Notify(self.id, token)); } } diff --git a/network/src/protocol_handler.rs b/network/src/protocol_handler.rs index 47f9707fcc..4b1c7e01d1 100644 --- a/network/src/protocol_handler.rs +++ b/network/src/protocol_handler.rs @@ -1,5 +1,5 @@ use crate::errors::{Error, PeerError, ProtocolError}; -use crate::{Network, PeerIndex, ProtocolId, SessionInfo, Timer, TimerRegistry, TimerToken}; +use crate::{Network, PeerIndex, ProtocolId, SessionInfo, TimerRegistry, TimerToken}; use ckb_util::Mutex; use log::debug; use log::info; @@ -97,14 +97,14 @@ impl CKBProtocolContext for DefaultCKBProtocolContext { fn disconnect(&self, peer_index: PeerIndex) { debug!(target: "network", "disconnect peer {}", peer_index); if let Some(peer_id) = self.network.get_peer_id(peer_index) { - self.network.drop_peer(&peer_id) + self.network.drop_peer(&peer_id); } } fn register_timer(&self, token: TimerToken, duration: Duration) -> Result<(), Error> { let (_, handler) = self .network .find_protocol(self.protocol_id) - .ok_or(ProtocolError::NotFound(self.protocol_id))?; + .ok_or_else(|| ProtocolError::NotFound(self.protocol_id))?; match *self.timer_registry.lock() { Some(ref mut timer_registry) => { timer_registry.push((handler, self.protocol_id, token, duration)) diff --git a/network/src/service/ckb_service.rs b/network/src/service/ckb_service.rs index 0fdde40065..c3be5a3b2b 100644 --- a/network/src/service/ckb_service.rs +++ b/network/src/service/ckb_service.rs @@ -1,21 +1,12 @@ use crate::peer_store::{Behaviour, Status}; use crate::protocol_handler::DefaultCKBProtocolContext; -use crate::{ - peers_registry::RegisterResult, CKBEvent, CKBProtocol, CKBProtocolHandler, Network, PeerId, - SessionType, -}; +use crate::{peers_registry::RegisterResult, CKBEvent, CKBProtocolHandler, Network}; use faketime::unix_time_as_millis; -use futures::{ - future::{self, Future}, - sync::mpsc::Receiver, - Async, Stream, -}; -use log::{debug, error, info, trace, warn}; -use p2p::{multiaddr::Multiaddr, service::ServiceControl, ProtocolId}; +use futures::{sync::mpsc::Receiver, Async, Stream}; +use log::{debug, error, info}; +use p2p::ProtocolId; use std::boxed::Box; -use std::io::{Error as IoError, ErrorKind as IoErrorKind}; use std::sync::Arc; -use tokio; pub struct CKBService { pub event_receiver: Receiver, @@ -52,7 +43,7 @@ impl Stream for CKBService { let mut peer_store = network.peer_store().write(); peer_store.report(&peer_id, Behaviour::Connect); peer_store.update_status(&peer_id, Status::Connected); - peer_store.add_discovered_address(&peer_id, addr); + let _ = peer_store.add_discovered_address(&peer_id, addr); } // call handler match self.find_handler(protocol_id) { diff --git a/network/src/service/outbound_peer_service.rs b/network/src/service/outbound_peer_service.rs index dd65ed99fa..6b80be0cb8 100644 --- a/network/src/service/outbound_peer_service.rs +++ b/network/src/service/outbound_peer_service.rs @@ -1,19 +1,9 @@ use crate::Network; -use crate::PeerId; -use futures::{ - future::{self, Future}, - sync::mpsc::Receiver, - Async, Stream, -}; -use log::{error, warn}; -use p2p::multiaddr::Multiaddr; -use std::boxed::Box; -use std::io::{Error as IoError, ErrorKind as IoErrorKind}; +use futures::{Async, Stream}; +use log::error; use std::sync::Arc; use std::time::Duration; -use std::time::Instant; use std::usize; -use tokio::io::{AsyncRead, AsyncWrite}; use tokio::timer::Interval; pub struct OutboundPeerService { diff --git a/network/src/service/timer_service.rs b/network/src/service/timer_service.rs index f9ac31963b..7242d7a265 100644 --- a/network/src/service/timer_service.rs +++ b/network/src/service/timer_service.rs @@ -1,7 +1,6 @@ use crate::errors::Error; use crate::protocol_handler::DefaultCKBProtocolContext; use crate::Network; -use crate::PeerId; use crate::{CKBProtocolHandler, ProtocolId}; use ckb_util::Mutex; use futures::future::{self, Future}; @@ -9,14 +8,12 @@ use futures::stream::FuturesUnordered; use futures::IntoFuture; use futures::Stream; use log::trace; -use p2p::multiaddr::Multiaddr; use std::boxed::Box; use std::io::{Error as IoError, ErrorKind as IoErrorKind}; use std::sync::Arc; use std::time::Duration; use tokio; -use tokio::io::{AsyncRead, AsyncWrite}; -use tokio::timer::{Error as TimerError, Interval}; +use tokio::timer::Interval; pub type TimerToken = usize; pub type Timer = (Arc, ProtocolId, TimerToken, Duration); diff --git a/sync/src/synchronizer/mod.rs b/sync/src/synchronizer/mod.rs index 9792b3adfc..46e4371dfb 100644 --- a/sync/src/synchronizer/mod.rs +++ b/sync/src/synchronizer/mod.rs @@ -670,7 +670,7 @@ impl Synchronizer { let fbb = &mut FlatBufferBuilder::new(); let message = SyncMessage::build_get_blocks(fbb, v_fetch); fbb.finish(message, None); - let _ = nc.send(peer, fbb.finished_data().to_vec()).unwrap(); + let _ = nc.send(peer, fbb.finished_data().to_vec()); debug!(target: "sync", "send_getblocks len={:?} to peer={}", v_fetch.len() , peer); } } From dc3d5773995f684eba21f841c7032661fa2cd5e2 Mon Sep 17 00:00:00 2001 From: jjy Date: Fri, 1 Mar 2019 15:31:01 +0800 Subject: [PATCH 09/99] refactor: upgrade secp256k1 to 0.12.0 --- Cargo.lock | 13 +------------ util/crypto/Cargo.toml | 4 ++-- util/crypto/src/secp/generator.rs | 23 +++++++++++++++------- util/crypto/src/secp/mod.rs | 2 +- util/crypto/src/secp/privkey.rs | 19 +++++------------- util/crypto/src/secp/pubkey.rs | 26 +++---------------------- util/crypto/src/secp/signature.rs | 32 +------------------------------ 7 files changed, 29 insertions(+), 90 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9af7d2ee27..16b5c27e66 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -933,7 +933,7 @@ dependencies = [ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", - "secp256k1 0.9.1 (git+https://github.com/nervosnetwork/rust-secp256k1)", + "secp256k1 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2242,16 +2242,6 @@ dependencies = [ "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "secp256k1" -version = "0.9.1" -source = "git+https://github.com/nervosnetwork/rust-secp256k1#09bf257132def11f29fdf0f08b6af08850ac3cd4" -dependencies = [ - "cc 1.0.29 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "secp256k1" version = "0.12.0" @@ -3213,7 +3203,6 @@ dependencies = [ "checksum scroll 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2f84d114ef17fd144153d608fba7c446b0145d038985e7a8cc5d08bb0ce20383" "checksum scroll_derive 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8f1aa96c45e7f5a91cb7fabe7b279f02fea7126239fc40b732316e8b6a2d0fcb" "checksum secp256k1 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4070f3906e65249228094cf97b04a90799fba04468190bbbcfa812309cf86e32" -"checksum secp256k1 0.9.1 (git+https://github.com/nervosnetwork/rust-secp256k1)" = "" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" "checksum serde 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)" = "9dad3f759919b92c3068c696c15c3d17238234498bbdcc80f2c469606f948ac8" diff --git a/util/crypto/Cargo.toml b/util/crypto/Cargo.toml index 298057ecf7..9109598310 100644 --- a/util/crypto/Cargo.toml +++ b/util/crypto/Cargo.toml @@ -6,8 +6,8 @@ authors = ["Nervos Core Dev "] edition = "2018" [dependencies] -secp256k1 = { git = "https://github.com/nervosnetwork/rust-secp256k1", optional = true, features = ["rand"] } lazy_static = "1.3" +secp256k1 = {version = "0.12.0", optional = true } failure = "0.1.5" rand = "0.6" numext-fixed-hash = { version = "0.1", features = ["support_rand", "support_heapsize", "support_serde"] } @@ -15,6 +15,6 @@ faster-hex = "0.3" crunchy = { version = "0.2", optional= true } [features] -default = ["secp"] +default = [ "secp" ] secp = ["secp256k1"] bech32 = ["crunchy"] diff --git a/util/crypto/src/secp/generator.rs b/util/crypto/src/secp/generator.rs index d6dbfdb09f..04915ba35c 100644 --- a/util/crypto/src/secp/generator.rs +++ b/util/crypto/src/secp/generator.rs @@ -2,8 +2,8 @@ use super::error::Error; use super::privkey::Privkey; use super::pubkey::Pubkey; use super::SECP256K1; -use rand; -use secp256k1::key; +use rand::{self, Rng}; +use secp256k1::{PublicKey, SecretKey}; pub struct Generator; @@ -13,16 +13,25 @@ impl Generator { } pub fn random_privkey(&self) -> Privkey { - let mut rng = rand::thread_rng(); - key::SecretKey::new(&SECP256K1, &mut rng).into() + self.random_secret_key().into() } pub fn random_keypair(self) -> Result<(Privkey, Pubkey), Error> { - let mut rng = rand::thread_rng(); + let secret_key = self.random_secret_key(); + let pubkey = PublicKey::from_secret_key(&*SECP256K1, &secret_key); - let (sec, publ) = SECP256K1.generate_keypair(&mut rng)?; + Ok((secret_key.into(), pubkey.into())) + } - Ok((sec.into(), publ.into())) + pub fn random_secret_key(&self) -> SecretKey { + let mut seed = vec![0; 32]; + let mut rng = rand::thread_rng(); + loop { + rng.fill(seed.as_mut_slice()); + if let Ok(key) = SecretKey::from_slice(&seed) { + return key; + } + } } } diff --git a/util/crypto/src/secp/mod.rs b/util/crypto/src/secp/mod.rs index e09740ee96..4d15975449 100644 --- a/util/crypto/src/secp/mod.rs +++ b/util/crypto/src/secp/mod.rs @@ -6,7 +6,7 @@ use numext_fixed_hash::H256; pub type Message = H256; lazy_static! { - pub static ref SECP256K1: secp256k1::Secp256k1 = secp256k1::Secp256k1::new(); + pub static ref SECP256K1: secp256k1::Secp256k1 = secp256k1::Secp256k1::new(); } mod error; diff --git a/util/crypto/src/secp/privkey.rs b/util/crypto/src/secp/privkey.rs index 36bce1ae12..7d649ff5e2 100644 --- a/util/crypto/src/secp/privkey.rs +++ b/util/crypto/src/secp/privkey.rs @@ -18,26 +18,17 @@ impl Privkey { pub fn sign_recoverable(&self, message: &Message) -> Result { let context = &SECP256K1; let message = message.as_ref(); - let privkey = key::SecretKey::from_slice(context, &self.inner.as_bytes())?; + let privkey = key::SecretKey::from_slice(&self.inner.as_bytes())?; let message = SecpMessage::from_slice(message)?; - let data = context.sign_recoverable(&message, &privkey)?; - let (rec_id, data) = data.serialize_compact(context); + let data = context.sign_recoverable(&message, &privkey); + let (rec_id, data) = data.serialize_compact(); Ok(Signature::from_compact(rec_id, data)) } - pub fn sign_schnorr(&self, message: &Message) -> Result { - let context = &SECP256K1; - let message = message.as_ref(); - let privkey = key::SecretKey::from_slice(context, &self.inner.as_bytes())?; - let message = SecpMessage::from_slice(message)?; - let data = context.sign_schnorr(&message, &privkey)?; - Ok(Signature::from_schnorr(data)) - } - pub fn pubkey(&self) -> Result { let context = &SECP256K1; - let privkey = key::SecretKey::from_slice(context, &self.inner.as_bytes())?; - let pubkey = key::PublicKey::from_secret_key(context, &privkey)?; + let privkey = key::SecretKey::from_slice(&self.inner.as_bytes())?; + let pubkey = key::PublicKey::from_secret_key(context, &privkey); Ok(Pubkey::from(pubkey)) } diff --git a/util/crypto/src/secp/pubkey.rs b/util/crypto/src/secp/pubkey.rs index 88434d9a6c..7df06ecfa3 100644 --- a/util/crypto/src/secp/pubkey.rs +++ b/util/crypto/src/secp/pubkey.rs @@ -25,43 +25,23 @@ impl Pubkey { temp }; - let pubkey = key::PublicKey::from_slice(context, &prefix_key)?; + let pubkey = key::PublicKey::from_slice(&prefix_key)?; let recoverable_signature = signature.to_recoverable()?; - let signature = recoverable_signature.to_standard(context); + let signature = recoverable_signature.to_standard(); let message = SecpMessage::from_slice(message.as_bytes())?; context.verify(&message, &signature, &pubkey)?; Ok(()) } - pub fn verify_schnorr(&self, message: &Message, signature: &Signature) -> Result<(), Error> { - let context = &SECP256K1; - - // non-compressed key prefix 4 - let prefix_key: [u8; 65] = { - let mut temp = [4u8; 65]; - temp[1..65].copy_from_slice(self.inner.as_bytes()); - temp - }; - - let pubkey = key::PublicKey::from_slice(context, &prefix_key)?; - let schnorr_signature = signature.to_schnorr(); - - let message = SecpMessage::from_slice(message.as_bytes())?; - context.verify_schnorr(&message, &schnorr_signature, &pubkey)?; - Ok(()) - } - pub fn serialize(&self) -> Vec { - let context = &SECP256K1; - // non-compressed key prefix 4 let prefix_key: [u8; 65] = { let mut temp = [4u8; 65]; temp[1..65].copy_from_slice(self.inner.as_bytes()); temp }; - let pubkey = key::PublicKey::from_slice(context, &prefix_key).unwrap(); + let pubkey = key::PublicKey::from_slice(&prefix_key).unwrap(); Vec::from(&pubkey.serialize()[..]) } } diff --git a/util/crypto/src/secp/signature.rs b/util/crypto/src/secp/signature.rs index f9a3feacfc..2a67d64e1a 100644 --- a/util/crypto/src/secp/signature.rs +++ b/util/crypto/src/secp/signature.rs @@ -4,7 +4,6 @@ use super::Message; use super::SECP256K1; use faster_hex::hex_string; use numext_fixed_hash::{H256, H520}; -use secp256k1::schnorr::Signature as SchnorrSignature; use secp256k1::Message as SecpMessage; use secp256k1::{RecoverableSignature, RecoveryId}; use std::fmt; @@ -50,17 +49,6 @@ impl Signature { Signature(data) } - pub fn from_schnorr(sig: SchnorrSignature) -> Self { - let mut data = [0; 65]; - data[0..64].copy_from_slice(&sig.serialize()); - data[64] = 0u8; - Signature(data) - } - - pub fn to_schnorr(&self) -> SchnorrSignature { - SchnorrSignature::deserialize(&self.0[0..64]) - } - /// Create a signature object from the sig. pub fn from_rsv(r: &H256, s: &H256, v: u8) -> Self { let mut sig = [0u8; 65]; @@ -99,10 +87,8 @@ impl Signature { /// Converts compact signature to a recoverable signature pub fn to_recoverable(&self) -> Result { - let context = &SECP256K1; let recovery_id = RecoveryId::from_i32(i32::from(self.0[64]))?; Ok(RecoverableSignature::from_compact( - context, &self.0[0..64], recovery_id, )?) @@ -121,28 +107,12 @@ impl Signature { Ok(pubkey.into()) } - pub fn recover_schnorr(&self, message: &Message) -> Result { - let context = &SECP256K1; - let schnorr_signature = self.to_schnorr(); - let message = SecpMessage::from_slice(&message[..])?; - let pubkey = context.recover_schnorr(&message, &schnorr_signature)?; - let serialized = pubkey.serialize_uncompressed(); - - let mut pubkey = [0u8; 64]; - pubkey.copy_from_slice(&serialized[1..65]); - Ok(pubkey.into()) - } - pub fn serialize(&self) -> Vec { Vec::from(&self.0[..]) } pub fn serialize_der(&self) -> Vec { - let context = &SECP256K1; - self.to_recoverable() - .unwrap() - .to_standard(context) - .serialize_der(context) + self.to_recoverable().unwrap().to_standard().serialize_der() } } From 86402493c1d2cfb7ddcb8b76fc676ff334150be5 Mon Sep 17 00:00:00 2001 From: jjy Date: Sun, 3 Mar 2019 18:06:29 +0800 Subject: [PATCH 10/99] refactor: integrate with p2p_ping --- Cargo.lock | 26 ++++ network/Cargo.toml | 2 + network/src/network.rs | 47 +++--- network/src/network_config.rs | 4 +- network/src/network_service.rs | 4 +- network/src/peers_registry.rs | 34 +++-- network/src/protocol.rs | 3 +- network/src/service.rs | 2 +- network/src/service/ckb_service.rs | 7 +- network/src/service/ping_service.rs | 221 +++++----------------------- network/src/tests/peers_registry.rs | 14 +- 11 files changed, 136 insertions(+), 228 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 16b5c27e66..55fd78da67 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -473,6 +473,7 @@ dependencies = [ "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "generic-channel 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2_sqlite 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -483,6 +484,7 @@ dependencies = [ "snap 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)", "tentacle 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tentacle-ping 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "tentacle-secio 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", "unsigned-varint 0.2.2 (git+https://github.com/paritytech/unsigned-varint)", @@ -1129,6 +1131,15 @@ dependencies = [ "typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "generic-channel" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", + "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "glob" version = "0.2.11" @@ -2473,6 +2484,19 @@ dependencies = [ "tokio-yamux 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "tentacle-ping" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "generic-channel 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "tentacle 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tentacle-secio" version = "0.1.0" @@ -3086,6 +3110,7 @@ dependencies = [ "checksum futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)" = "49e7653e374fe0d0c12de4250f0bdb60680b8c80eed558c5c7538eec9c89e21b" "checksum futures-cpupool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "ab90cde24b3319636588d0c35fe03b1333857621051837ed769faefb4c2162e4" "checksum generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3c0f28c2f5bfb5960175af447a2da7c18900693738343dc896ffbcabd9839592" +"checksum generic-channel 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8d1a0a427708e0f861025065b4c490e501e0eb13b7fb773a75b829576f6fc8a0" "checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" "checksum globset 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4743617a7464bbda3c8aec8558ff2f9429047e025771037df561d383337ff865" "checksum goblin 0.0.19 (registry+https://github.com/rust-lang/crates.io-index)" = "c65cd533b33e3d04c6e393225fa8919ddfcf5862ca8919c7f9a167c312ef41c2" @@ -3231,6 +3256,7 @@ dependencies = [ "checksum take 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b157868d8ac1f56b64604539990685fa7611d8fa9e5476cf0c02cf34d32917c5" "checksum tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "b86c784c88d98c801132806dadd3819ed29d8600836c4088e855cdf3e178ed8a" "checksum tentacle 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6c29b4a6d8dbf15e8191c4723be562689ec734edc86fa9a84585c256d53e04be" +"checksum tentacle-ping 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "853dcfad34c9521fbb0c1dfc64d363b6efc774aae044a89411a25e3155130e24" "checksum tentacle-secio 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9562b522b885f057e39aac9feb9048e64d4b9cc7f5c0be5933ae6606eb56ad40" "checksum termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4096add70612622289f2fdcdbd5086dc81c1e2675e6ae58d6c4f62a16c6d7f2f" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" diff --git a/network/Cargo.toml b/network/Cargo.toml index 7dbd94bfcc..96791f152d 100644 --- a/network/Cargo.toml +++ b/network/Cargo.toml @@ -21,12 +21,14 @@ tokio = "0.1.16" futures = { version = "0.1.19", features = ["use_std"] } snap = "0.2" p2p = { version = "0.1", package="tentacle" } +p2p-ping = { version = "0.1", package="tentacle-ping" } secio = { version = "0.1", package="tentacle-secio" } faketime = "0.2.0" rusqlite = {version = "0.16.0", features = ["bundled"]} lazy_static = "1.3.0" r2d2 = "0.8.3" r2d2_sqlite = "0.8.0" +generic-channel = { version = "0.2.0", features = ["all"] } [dev-dependencies] criterion = "0.2" diff --git a/network/src/network.rs b/network/src/network.rs index 9839db9087..2dd935e0cc 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -8,6 +8,7 @@ use crate::protocol_handler::{CKBProtocolHandler, DefaultCKBProtocolContext}; use crate::service::{ ckb_service::CKBService, outbound_peer_service::OutboundPeerService, + ping_service::PingService, timer_service::{TimerRegistry, TimerService}, }; use crate::{ @@ -18,6 +19,7 @@ use bytes::Bytes; use ckb_util::{Mutex, RwLock}; use fnv::FnvHashMap; use futures::future::{select_all, Future}; +use futures::sync::mpsc::channel; use futures::sync::mpsc::Receiver; use futures::sync::oneshot; use futures::Stream; @@ -30,15 +32,17 @@ use p2p::{ service::{Service, ServiceError, ServiceEvent}, traits::ServiceHandle, }; +use p2p_ping::{Event as PingEvent, PingProtocol}; use secio; use std::boxed::Box; use std::cmp::max; use std::sync::Arc; -use std::time::Duration; +use std::time::{Duration, Instant}; use std::usize; use tokio::codec::LengthDelimitedCodec; const DIAL_BOOTNODE_TIMEOUT: u64 = 20; +const PING_PROTOCOL_ID: ProtocolId = 0; pub type CKBProtocols = Vec<(CKBProtocol, Arc)>; type NetworkResult = Result< @@ -60,7 +64,7 @@ pub struct SessionInfo { pub struct PeerInfo { pub peer_id: PeerId, pub session_type: SessionType, - pub last_ping_time: Option, + pub last_ping_time: Option, pub connected_addr: Multiaddr, pub identify_info: Option, } @@ -219,7 +223,7 @@ impl Network { } pub fn node_id(&self) -> String { - self.local_private_key.to_peer_id().to_base58() + self.local_private_key().to_peer_id().to_base58() } // A workaround method for `add_node` rpc call, need to re-write it after new p2p lib integration. @@ -340,7 +344,7 @@ impl Network { pub(crate) fn inner_build( config: &NetworkConfig, ckb_protocols: CKBProtocols, - ) -> Result<(Arc, P2PService, TimerRegistry), Error> { + ) -> Result<(Arc, P2PService, TimerRegistry, Receiver), Error> { let local_private_key = match config.fetch_private_key() { Some(private_key) => private_key?, None => return Err(ConfigError::InvalidKey.into()), @@ -373,6 +377,13 @@ impl Network { ); let mut p2p_service = ServiceBuilder::default().forever(true); // register protocols + let (ping_sender, ping_receiver) = channel(std::u8::MAX as usize); + p2p_service = p2p_service.insert_protocol(PingProtocol::new( + PING_PROTOCOL_ID, + config.ping_interval, + config.ping_timeout, + ping_sender, + )); for (ckb_protocol, _) in &ckb_protocols { p2p_service = p2p_service.insert_protocol(ckb_protocol.clone()); } @@ -449,7 +460,7 @@ impl Network { } } - Ok((network, p2p_service, timer_registry)) + Ok((network, p2p_service, timer_registry, ping_receiver)) } pub(crate) fn build_network_future( @@ -459,15 +470,13 @@ impl Network { p2p_service: P2PService, timer_registry: TimerRegistry, ckb_event_receiver: Receiver, + ping_event_receiver: Receiver, ) -> Result + Send>, Error> { - let local_private_key = network.local_private_key().to_owned(); - let _local_peer_id: PeerId = local_private_key.to_peer_id(); - let _client_version = config.client_version.clone(); - let _protocol_version = config.protocol_version.clone(); - let _max_outbound = config.max_outbound_peers as usize; - // initialize ckb_protocols - //let ping_service = Arc::new(PingService::new(config.ping_interval, config.ping_timeout)); + let ping_service = PingService { + network: Arc::clone(&network), + event_receiver: ping_event_receiver, + }; //let identify_service = Arc::new(IdentifyService { // client_version, // protocol_version, @@ -494,17 +503,17 @@ impl Network { .for_each(|_| Ok(())) .map_err(|_err| Error::Shutdown), ), + Box::new( + ping_service + .for_each(|_| Ok(())) + .map_err(|_err| Error::Shutdown), + ), // Box::new( // discovery_query_service // .into_future() // .map(|_| ()) // .map_err(|(err, _)| err), // ) as Box + Send>, - //ping_service.start_protocol( - // Arc::clone(&network), - // swarm_controller.clone(), - // basic_transport.clone(), - //), //identify_service.start_protocol( // Arc::clone(&network), // swarm_controller.clone(), @@ -541,7 +550,8 @@ impl Network { ckb_protocols: CKBProtocols, ckb_event_receiver: Receiver, ) -> NetworkResult { - let (network, p2p_service, timer_registry) = Self::inner_build(config, ckb_protocols)?; + let (network, p2p_service, timer_registry, ping_event_receiver) = + Self::inner_build(config, ckb_protocols)?; let (close_tx, close_rx) = oneshot::channel(); let network_future = Self::build_network_future( Arc::clone(&network), @@ -550,6 +560,7 @@ impl Network { p2p_service, timer_registry, ckb_event_receiver, + ping_event_receiver, )?; Ok((network, close_tx, network_future)) } diff --git a/network/src/network_config.rs b/network/src/network_config.rs index f2dd08a5c6..5990ccdeb2 100644 --- a/network/src/network_config.rs +++ b/network/src/network_config.rs @@ -30,6 +30,7 @@ pub struct NetworkConfig { pub config_dir_path: Option, pub bootnodes: Vec, pub ping_interval: Duration, + pub ping_timeout: Duration, pub discovery_timeout: Duration, pub discovery_response_count: usize, pub discovery_interval: Duration, @@ -143,7 +144,8 @@ impl Default for NetworkConfig { bootnodes: vec![], config_dir_path: None, // protocol services config - ping_interval: Duration::from_secs(30), + ping_interval: Duration::from_secs(15), + ping_timeout: Duration::from_secs(20), discovery_timeout: Duration::from_secs(20), discovery_response_count: 20, discovery_interval: Duration::from_secs(15), diff --git a/network/src/network_service.rs b/network/src/network_service.rs index 12401c53ec..4390953b57 100644 --- a/network/src/network_service.rs +++ b/network/src/network_service.rs @@ -64,7 +64,8 @@ impl NetworkService { ckb_protocols: CKBProtocols, ckb_event_receiver: Receiver, ) -> Result { - let (network, p2p_service, timer_registry) = Network::inner_build(config, ckb_protocols)?; + let (network, p2p_service, timer_registry, ping_event_receiver) = + Network::inner_build(config, ckb_protocols)?; let (close_tx, close_rx) = oneshot::channel(); let (init_tx, init_rx) = oneshot::channel(); let join_handle = thread::spawn({ @@ -83,6 +84,7 @@ impl NetworkService { p2p_service, timer_registry, ckb_event_receiver, + ping_event_receiver, ) .unwrap(); init_tx.send(()).unwrap(); diff --git a/network/src/peers_registry.rs b/network/src/peers_registry.rs index 27b26625f1..6a9ff2ff47 100644 --- a/network/src/peers_registry.rs +++ b/network/src/peers_registry.rs @@ -6,7 +6,6 @@ use crate::{ PeerId, PeerIndex, ProtocolId, SessionType, }; use ckb_util::RwLock; -use faketime::unix_time_as_millis; use fnv::{FnvHashMap, FnvHashSet}; use log::debug; pub use p2p::{ @@ -18,6 +17,7 @@ use rand::thread_rng; use std::collections::hash_map::Entry; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc; +use std::time::{Duration, Instant}; pub(crate) const EVICTION_PROTECT_PEERS: usize = 8; @@ -88,7 +88,7 @@ impl PeerManage { ping: None, last_ping_time: None, last_message_time: None, - connected_time: unix_time_as_millis(), + connected_time: Instant::now(), peer_index, session, protocols: Vec::new(), @@ -136,10 +136,10 @@ pub struct Peer { pub connected_addr: Multiaddr, // Client or Server pub identify_info: Option, - pub last_ping_time: Option, - pub last_message_time: Option, - pub ping: Option, - pub connected_time: u64, + pub last_ping_time: Option, + pub last_message_time: Option, + pub ping: Option, + pub connected_time: Instant, pub session: Session, pub protocols: Vec<(ProtocolId, Version)>, } @@ -331,8 +331,14 @@ impl PeersRegistry { &mut candidate_peers, EVICTION_PROTECT_PEERS, |(_, peer1), (_, peer2)| { - let peer1_ping = peer1.ping.unwrap_or_else(|| std::u64::MAX); - let peer2_ping = peer2.ping.unwrap_or_else(|| std::u64::MAX); + let peer1_ping = peer1 + .ping + .map(|p| p.as_secs()) + .unwrap_or_else(|| std::u64::MAX); + let peer2_ping = peer2 + .ping + .map(|p| p.as_secs()) + .unwrap_or_else(|| std::u64::MAX); peer2_ping.cmp(&peer1_ping) }, ); @@ -342,9 +348,15 @@ impl PeersRegistry { &mut candidate_peers, EVICTION_PROTECT_PEERS, |(_, peer1), (_, peer2)| { - let peer1_last_message_time = peer1.last_message_time.unwrap_or_default(); - let peer2_last_message_time = peer2.last_message_time.unwrap_or_default(); - peer1_last_message_time.cmp(&peer2_last_message_time) + let peer1_last_message = peer1 + .last_message_time + .map(|t| t.elapsed().as_secs()) + .unwrap_or_else(|| std::u64::MAX); + let peer2_last_message = peer2 + .last_message_time + .map(|t| t.elapsed().as_secs()) + .unwrap_or_else(|| std::u64::MAX); + peer2_last_message.cmp(&peer1_last_message) }, ); // Protect half peers which have the longest connection time diff --git a/network/src/protocol.rs b/network/src/protocol.rs index 48115f4eba..13bdfe0d59 100644 --- a/network/src/protocol.rs +++ b/network/src/protocol.rs @@ -10,6 +10,7 @@ use p2p::{ use tokio::codec::LengthDelimitedCodec; pub type Version = u8; +const CKB_PROTOCOL_ID_OFFSET: ProtocolId = 100; #[derive(Clone)] pub struct CKBProtocol { @@ -43,7 +44,7 @@ impl CKBProtocol { } } pub fn id(&self) -> ProtocolId { - self.id + self.id + CKB_PROTOCOL_ID_OFFSET } pub fn base_name(&self) -> Bytes { self.base_name.clone() diff --git a/network/src/service.rs b/network/src/service.rs index e4875c94d4..18a76a1dc9 100644 --- a/network/src/service.rs +++ b/network/src/service.rs @@ -1,4 +1,4 @@ pub mod ckb_service; pub mod outbound_peer_service; +pub mod ping_service; pub mod timer_service; -//pub mod ping_service; diff --git a/network/src/service/ckb_service.rs b/network/src/service/ckb_service.rs index c3be5a3b2b..351c2fddba 100644 --- a/network/src/service/ckb_service.rs +++ b/network/src/service/ckb_service.rs @@ -1,12 +1,12 @@ use crate::peer_store::{Behaviour, Status}; use crate::protocol_handler::DefaultCKBProtocolContext; use crate::{peers_registry::RegisterResult, CKBEvent, CKBProtocolHandler, Network}; -use faketime::unix_time_as_millis; use futures::{sync::mpsc::Receiver, Async, Stream}; use log::{debug, error, info}; use p2p::ProtocolId; use std::boxed::Box; use std::sync::Arc; +use std::time::Instant; pub struct CKBService { pub event_receiver: Receiver, @@ -92,8 +92,9 @@ impl Stream for CKBService { network.drop_peer(&peer_id); } Some(Received(peer_id, protocol_id, data)) => { + let now = Instant::now(); network.modify_peer(&peer_id, |peer| { - peer.last_message_time = Some(unix_time_as_millis()) + peer.last_message_time = Some(now); }); let peer_index = network.get_peer_index(&peer_id).expect("peer_index"); match self.find_handler(protocol_id) { @@ -111,7 +112,7 @@ impl Stream for CKBService { debug!(target: "network", "receive ckb timer notify, protocol_id: {} token: {}", protocol_id, token); } None => { - error!(target: "network", "ckb service should not stop"); + debug!(target: "network", "ckb service shutdown"); return Ok(Async::Ready(None)); } } diff --git a/network/src/service/ping_service.rs b/network/src/service/ping_service.rs index a6a1906f4b..663b3df390 100644 --- a/network/src/service/ping_service.rs +++ b/network/src/service/ping_service.rs @@ -1,200 +1,49 @@ use crate::peer_store::Behaviour; -use crate::protocol::Protocol; -use crate::protocol_service::ProtocolService; -use crate::transport::TransportOutput; use crate::Network; -use crate::PeerId; -use faketime::unix_time_as_millis; -use futures::future::{self, Future}; -use futures::stream::FuturesUnordered; -use futures::Stream; -use libp2p::core::Multiaddr; -use libp2p::core::SwarmController; -use libp2p::core::{upgrade, MuxedTransport}; -use libp2p::{self, ping}; -use log::{trace, warn}; -use std::boxed::Box; -use std::io::{Error as IoError, ErrorKind as IoErrorKind}; +use futures::{sync::mpsc::Receiver, Async, Stream}; +use log::{debug, trace}; +use p2p_ping::Event; use std::sync::Arc; -use std::time::Duration; use std::time::Instant; -use tokio::io::{AsyncRead, AsyncWrite}; -use tokio::timer::{Interval, Timeout}; pub struct PingService { - ping_interval: Duration, - ping_timeout: Duration, + pub event_receiver: Receiver, + pub network: Arc, } -impl PingService { - pub fn new(ping_interval: Duration, ping_timeout: Duration) -> Self { - PingService { - ping_interval, - ping_timeout, - } - } - fn ping_to_protocol(peer_id: PeerId, output: ping::PingOutput) -> Protocol { - match output { - ping::PingOutput::Ponger(processing) => Protocol::Pong(processing, peer_id), - ping::PingOutput::Pinger { pinger, processing } => { - Protocol::Ping(pinger, processing, peer_id) - } - } - } -} +impl Stream for PingService { + type Item = (); + type Error = (); + fn poll(&mut self) -> Result>, Self::Error> { + use Event::*; -impl ProtocolService for PingService { - type Output = ping::PingOutput; - fn convert_to_protocol( - peer_id: Arc, - _addr: &Multiaddr, - output: Self::Output, - ) -> Protocol { - Self::ping_to_protocol(PeerId::clone(&peer_id), output) - } - fn handle( - &self, - network: Arc, - protocol: Protocol, - ) -> Box + Send> { - match protocol { - Protocol::Pong(processing, _peer_id) => { - Box::new(processing) as Box + Send> + match try_ready!(self.event_receiver.poll()) { + Some(Ping(peer_id)) => { + trace!(target: "network", "send ping to {:?}", peer_id); } - Protocol::Ping(pinger, processing, peer_id) => { - match network.get_peer_pinger(&peer_id) { - Some(pinger_loader) => { - // ping and store pinger - Box::new(pinger_loader.tie_or_passthrough(pinger, processing)) - as Box + Send> - } - None => Box::new(future::err(IoError::new( - IoErrorKind::Other, - "ping protocol can't find peer", - ))) as Box + Send>, - } + Some(Pong(peer_id, duration)) => { + trace!(target: "network", "receive pong from {:?} duration {:?}", peer_id, duration); + self.network.modify_peer(&peer_id, |peer| { + peer.ping = Some(duration); + peer.last_ping_time = Some(Instant::now()); + }); + self.network.report(&peer_id, Behaviour::Ping); } - _ => Box::new(future::ok(())) as Box + Send>, - } - } - - // Periodicly ping peers - fn start_protocol( - &self, - network: Arc, - swarm_controller: SwarmController< - SwarmTran, - Box + Send>, - >, - transport: Tran, - ) -> Box + Send> - where - SwarmTran: MuxedTransport> + Clone + Send + 'static, - SwarmTran::MultiaddrFuture: Send + 'static, - SwarmTran::Dial: Send, - SwarmTran::Listener: Send, - SwarmTran::ListenerUpgrade: Send, - SwarmTran::Incoming: Send, - SwarmTran::IncomingUpgrade: Send, - Tran: MuxedTransport> + Clone + Send + 'static, - Tran::MultiaddrFuture: Send + 'static, - Tran::Dial: Send, - Tran::Listener: Send, - Tran::ListenerUpgrade: Send, - Tran::Incoming: Send, - Tran::IncomingUpgrade: Send, - TranOut: AsyncRead + AsyncWrite + Send + 'static, - { - let transport = transport.and_then(move |out, endpoint, client_addr| { - let peer_id = out.peer_id; - upgrade::apply(out.socket, libp2p::ping::Ping, endpoint, client_addr) - .map(move |(out, addr)| (Self::ping_to_protocol(peer_id, out), addr)) - }); - - let periodic_ping_future = Interval::new( - Instant::now() + Duration::from_secs(5), - self.ping_interval, - ) - .map_err(|err| IoError::new(IoErrorKind::Other, err)) - .for_each({ - let network = Arc::clone(&network); - let transport = transport.clone(); - let ping_timeout = self.ping_timeout; - move |_| { - let mut ping_futures = FuturesUnordered::new(); - // build ping future for each peer - for peer_id in network.peers() { - let peer_id = peer_id.clone(); - // only ping first address? - if let Some(addr) = network.get_peer_addresses(&peer_id).get(0) { - if let Some(pinger_loader) = network.get_peer_pinger(&peer_id) { - let ping_future = pinger_loader - .dial(&swarm_controller, &addr, transport.clone()) - .and_then({ - let peer_id = peer_id.clone(); - move |mut pinger| { - pinger.ping().map(|_| peer_id).map_err(|err| { - IoError::new( - IoErrorKind::Other, - format!("pinger error {}", err), - ) - }) - } - }); - let ping_start_time = unix_time_as_millis(); - let ping_future = - Future::then(Timeout::new(ping_future, ping_timeout), { - let network = Arc::clone(&network); - move |result| -> Result<(), IoError> { - match result { - Ok(peer_id) => { - let now = unix_time_as_millis(); - let ping = now - ping_start_time; - network.modify_peer(&peer_id, |peer| { - peer.ping = Some(ping); - peer.last_ping_time = Some(now); - }); - network.report(&peer_id, Behaviour::Ping); - trace!( - target: "network", - "received pong from {:?} in {:?}", - peer_id, - ping - ); - Ok(()) - } - Err(err) => { - network.report(&peer_id, Behaviour::FailedToPing); - network.drop_peer(&peer_id); - trace!( - target: "network", - "error when send ping to {:?}, error: {:?}", - peer_id, - err - ); - Ok(()) - } - } - } - }); - ping_futures - .push(Box::new(ping_future) - as Box + Send>); - } - } - } - Box::new( - ping_futures - .into_future() - .map(|_| ()) - .map_err(|(err, _)| err), - ) as Box + Send> + Some(Timeout(peer_id)) => { + debug!(target: "network", "timeout to ping {:?}", peer_id); + self.network.report(&peer_id, Behaviour::FailedToPing); + self.network.drop_peer(&peer_id); } - }) - .then(|err| { - warn!(target: "network", "Ping service stopped, reason: {:?}", err); - err - }); - Box::new(periodic_ping_future) as Box + Send> + Some(UnexpectedError(peer_id)) => { + debug!(target: "network", "failed to ping {:?}", peer_id); + self.network.report(&peer_id, Behaviour::FailedToPing); + self.network.drop_peer(&peer_id); + } + None => { + debug!(target: "network", "ping service shutdown"); + return Ok(Async::Ready(None)); + } + } + Ok(Async::Ready(Some(()))) } } diff --git a/network/src/tests/peers_registry.rs b/network/src/tests/peers_registry.rs index 7280759e0e..ad50a50e06 100644 --- a/network/src/tests/peers_registry.rs +++ b/network/src/tests/peers_registry.rs @@ -5,8 +5,8 @@ use crate::{ random_peer_id, SessionType, }; use ckb_util::RwLock; -use faketime::unix_time_as_millis; use std::sync::Arc; +use std::time::{Duration, Instant}; fn new_peer_store() -> Arc> { Arc::new(RwLock::new(SqlitePeerStore::temp())) @@ -127,20 +127,22 @@ fn test_accept_inbound_peer_eviction() { for _ in 0..EVICTION_PROTECT_PEERS { let peer_id = peers_iter.next().unwrap(); let mut peer = peers_registry.get_mut(&peer_id).unwrap(); - peer.ping = Some(0); + peer.ping = Some(Duration::from_secs(0)); } + + // to prevent time error, we set now to 10000s ago. + let now = Instant::now() - Duration::from_secs(10000); // peers which most recently sent messages - let now = unix_time_as_millis(); for _ in 0..EVICTION_PROTECT_PEERS { let peer_id = peers_iter.next().unwrap(); let mut peer = peers_registry.get_mut(&peer_id).unwrap(); - peer.last_message_time = Some(now + 10000); + peer.last_message_time = Some(now + Duration::from_secs(10000)); } // protect 5 peers which have the longest connection time for _ in 0..longest_connection_time_peers_count { let peer_id = peers_iter.next().unwrap(); let mut peer = peers_registry.get_mut(&peer_id).unwrap(); - peer.connected_time = now.saturating_sub(10000); + peer.connected_time = now - Duration::from_secs(10000); } let mut new_peer_ids = (0..3).map(|_| random_peer_id()).collect::>(); // setup 3 node and 1 reserved node from addr1 @@ -182,7 +184,7 @@ fn test_accept_inbound_peer_eviction() { for peer_id in new_peer_ids { let mut peer = peers_registry.get_mut(&peer_id).unwrap(); // push the connected_time to make sure peer is unprotect - peer.connected_time = now + 10000; + peer.connected_time = now + Duration::from_secs(10000); } // should evict evict target assert!(peers_registry.get(&evict_target).is_some()); From bd5bf69af64732e57b8266280ed40baf4f6890af Mon Sep 17 00:00:00 2001 From: jjy Date: Mon, 4 Mar 2019 17:20:56 +0800 Subject: [PATCH 11/99] fix: crypto tests --- util/crypto/src/secp/mod.rs | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/util/crypto/src/secp/mod.rs b/util/crypto/src/secp/mod.rs index 4d15975449..b77220fdee 100644 --- a/util/crypto/src/secp/mod.rs +++ b/util/crypto/src/secp/mod.rs @@ -24,12 +24,20 @@ pub use self::signature::Signature; #[cfg(test)] mod tests { use super::*; + use rand::{self, Rng}; + + fn random_message() -> Message { + let mut message = Message::default(); + let mut rng = rand::thread_rng(); + rng.fill(message.as_mut()); + message + } #[test] fn test_sign_verify() { let gen = Generator::new(); let (privkey, pubkey) = gen.random_keypair().unwrap(); - let message = Message::default(); + let message = random_message(); let signature = privkey.sign_recoverable(&message).unwrap(); assert!(pubkey.verify(&message, &signature).is_ok()); } @@ -38,27 +46,9 @@ mod tests { fn test_recover() { let gen = Generator::new(); let (privkey, pubkey) = gen.random_keypair().unwrap(); - let message = Message::default(); + let message = random_message(); let signature = privkey.sign_recoverable(&message).unwrap(); assert_eq!(pubkey, signature.recover(&message).unwrap()); } - #[test] - fn test_schnorr_sign_verify() { - let gen = Generator::new(); - let (privkey, pubkey) = gen.random_keypair().unwrap(); - let message = Message::default(); - let signature = privkey.sign_schnorr(&message).unwrap(); - assert!(pubkey.verify_schnorr(&message, &signature).is_ok()); - } - - #[test] - fn test_schnorr_recover() { - let gen = Generator::new(); - let (privkey, pubkey) = gen.random_keypair().unwrap(); - let message = Message::default(); - let signature = privkey.sign_schnorr(&message).unwrap(); - assert_eq!(pubkey, signature.recover_schnorr(&message).unwrap()); - } - } From 01b8ba14002738e787d135c9d5135ef4c3ff8539 Mon Sep 17 00:00:00 2001 From: jjy Date: Mon, 11 Mar 2019 14:39:58 +0800 Subject: [PATCH 12/99] fix: p2p rebase --- Cargo.lock | 20 ++++++++++---------- rpc/src/module/miner.rs | 2 +- rpc/src/module/pool.rs | 3 ++- rpc/src/module/trace.rs | 6 +++--- src/cli/run_impl.rs | 5 ++--- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 55fd78da67..7579a8aa2e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -472,21 +472,21 @@ dependencies = [ "faketime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "generic-channel 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2_sqlite 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "rusqlite 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "snap 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)", "tentacle 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "tentacle-ping 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "tentacle-secio 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", "unsigned-varint 0.2.2 (git+https://github.com/paritytech/unsigned-varint)", ] @@ -2473,14 +2473,14 @@ name = "tentacle" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "bytes 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", + "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "parity-multiaddr 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "tentacle-secio 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-threadpool 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-threadpool 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-yamux 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2494,7 +2494,7 @@ dependencies = [ "generic-channel 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "tentacle 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2504,7 +2504,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "aes-ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "bs58 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "bytes 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", + "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2514,7 +2514,7 @@ dependencies = [ "ring 0.14.6 (registry+https://github.com/rust-lang/crates.io-index)", "secp256k1 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", "twofish 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "unsigned-varint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "untrusted 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/rpc/src/module/miner.rs b/rpc/src/module/miner.rs index 6e86276e28..c26c03e170 100644 --- a/rpc/src/module/miner.rs +++ b/rpc/src/module/miner.rs @@ -4,8 +4,8 @@ use ckb_miner::BlockAssemblerController; use ckb_network::{NetworkService, ProtocolId}; use ckb_protocol::RelayMessage; use ckb_shared::{index::ChainIndex, shared::Shared}; -use ckb_traits::ChainProvider; use ckb_sync::NetworkProtocol; +use ckb_traits::ChainProvider; use ckb_verification::{HeaderResolverWrapper, HeaderVerifier, Verifier}; use flatbuffers::FlatBufferBuilder; use jsonrpc_core::{Error, Result}; diff --git a/rpc/src/module/pool.rs b/rpc/src/module/pool.rs index 3bb2894091..5abd517503 100644 --- a/rpc/src/module/pool.rs +++ b/rpc/src/module/pool.rs @@ -4,8 +4,9 @@ use ckb_protocol::RelayMessage; use ckb_shared::index::ChainIndex; use ckb_shared::shared::Shared; use ckb_network::{NetworkService, ProtocolId}; -use ckb_pool::txs_pool::TransactionPoolController; use ckb_protocol::RelayMessage; +use ckb_shared::index::ChainIndex; +use ckb_shared::shared::Shared; use ckb_sync::NetworkProtocol; use flatbuffers::FlatBufferBuilder; use jsonrpc_core::Result; diff --git a/rpc/src/module/trace.rs b/rpc/src/module/trace.rs index fb5ba79f9c..fd40318397 100644 --- a/rpc/src/module/trace.rs +++ b/rpc/src/module/trace.rs @@ -1,9 +1,9 @@ use ckb_core::transaction::Transaction as CoreTransaction; -use ckb_shared::index::ChainIndex; -use ckb_shared::shared::Shared; use ckb_network::{NetworkService, ProtocolId}; -use ckb_pool::txs_pool::{TransactionPoolController, TxTrace}; use ckb_protocol::RelayMessage; +use ckb_shared::index::ChainIndex; +use ckb_shared::shared::Shared; +use ckb_shared::tx_pool::TxTrace; use ckb_sync::NetworkProtocol; use flatbuffers::FlatBufferBuilder; use jsonrpc_core::Result; diff --git a/src/cli/run_impl.rs b/src/cli/run_impl.rs index ad27fc2766..daf99c0a7c 100644 --- a/src/cli/run_impl.rs +++ b/src/cli/run_impl.rs @@ -14,9 +14,8 @@ use ckb_rpc::RpcServer; use ckb_shared::cachedb::CacheDB; use ckb_shared::index::ChainIndex; use ckb_shared::shared::{Shared, SharedBuilder}; -use ckb_sync::{ - NetTimeProtocol, Relayer, Synchronizer, RELAY_PROTOCOL_ID, SYNC_PROTOCOL_ID, TIME_PROTOCOL_ID, -}; +use ckb_sync::{NetTimeProtocol, NetworkProtocol, Relayer, Synchronizer}; +use ckb_traits::chain_provider::ChainProvider; use crypto::secp::Generator; use log::info; use numext_fixed_hash::H256; From c11c49cb128dabf7d55a4718c5cc3a768c998e3c Mon Sep 17 00:00:00 2001 From: jjy Date: Tue, 12 Mar 2019 14:04:00 +0800 Subject: [PATCH 13/99] feat: support select network protocol by version --- network/src/lib.rs | 2 +- network/src/network.rs | 20 +++++++++++--------- network/src/network_service.rs | 2 +- network/src/protocol.rs | 4 ++++ network/src/protocol_handler.rs | 3 ++- network/src/service/ckb_service.rs | 22 +++++++++++++++------- 6 files changed, 34 insertions(+), 19 deletions(-) diff --git a/network/src/lib.rs b/network/src/lib.rs index 34e6c3799a..f929286355 100644 --- a/network/src/lib.rs +++ b/network/src/lib.rs @@ -17,7 +17,7 @@ pub use crate::network::{Network, PeerInfo, SessionInfo}; pub use crate::network_config::NetworkConfig; pub use crate::network_service::NetworkService; pub use crate::peers_registry::RegisterResult; -pub use crate::protocol::{CKBProtocol, Event as CKBEvent}; +pub use crate::protocol::{CKBProtocol, Event as CKBEvent, Version as ProtocolVersion}; pub use crate::protocol_handler::{CKBProtocolContext, CKBProtocolHandler, Severity}; pub use crate::service::timer_service::{Timer, TimerRegistry, TimerToken}; pub use p2p::{multiaddr, secio::PeerId, yamux::session::SessionType, ProtocolId}; diff --git a/network/src/network.rs b/network/src/network.rs index 2dd935e0cc..a4d5a96ef1 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -98,6 +98,17 @@ impl Network { pub fn find_protocol( &self, id: ProtocolId, + version: ProtocolVersion, + ) -> Option<(&CKBProtocol, Arc)> { + self.ckb_protocols + .iter() + .find(|(protocol, _)| protocol.id() == id && protocol.match_version(version)) + .map(|(protocol, handler)| (protocol, Arc::clone(handler))) + } + + pub fn find_protocol_without_version( + &self, + id: ProtocolId, ) -> Option<(&CKBProtocol, Arc)> { self.ckb_protocols .iter() @@ -175,15 +186,6 @@ impl Network { } } - pub(crate) fn peers(&self) -> impl Iterator { - let peers_registry = self.peers_registry.read(); - let peers = peers_registry - .peers_iter() - .map(|(peer_id, _peer)| peer_id.to_owned()) - .collect::>(); - peers.into_iter() - } - pub(crate) fn peers_indexes(&self) -> Vec { let peers_registry = self.peers_registry.read(); let iter = peers_registry.connected_peers_indexes(); diff --git a/network/src/network_service.rs b/network/src/network_service.rs index 4390953b57..c18519c57e 100644 --- a/network/src/network_service.rs +++ b/network/src/network_service.rs @@ -50,7 +50,7 @@ impl NetworkService { where F: FnOnce(&CKBProtocolContext) -> T, { - match self.network.find_protocol(protocol_id) { + match self.network.find_protocol_without_version(protocol_id) { Some(_) => Some(f(&DefaultCKBProtocolContext::new( Arc::clone(&self.network), protocol_id, diff --git a/network/src/protocol.rs b/network/src/protocol.rs index 13bdfe0d59..4db3df39e1 100644 --- a/network/src/protocol.rs +++ b/network/src/protocol.rs @@ -49,6 +49,10 @@ impl CKBProtocol { pub fn base_name(&self) -> Bytes { self.base_name.clone() } + + pub fn match_version(&self, version: Version) -> bool { + self.supported_versions.contains(&version) + } } impl ProtocolMeta for CKBProtocol { diff --git a/network/src/protocol_handler.rs b/network/src/protocol_handler.rs index 4b1c7e01d1..6f62a8d195 100644 --- a/network/src/protocol_handler.rs +++ b/network/src/protocol_handler.rs @@ -103,8 +103,9 @@ impl CKBProtocolContext for DefaultCKBProtocolContext { fn register_timer(&self, token: TimerToken, duration: Duration) -> Result<(), Error> { let (_, handler) = self .network - .find_protocol(self.protocol_id) + .find_protocol_without_version(self.protocol_id) .ok_or_else(|| ProtocolError::NotFound(self.protocol_id))?; + match *self.timer_registry.lock() { Some(ref mut timer_registry) => { timer_registry.push((handler, self.protocol_id, token, duration)) diff --git a/network/src/service/ckb_service.rs b/network/src/service/ckb_service.rs index 351c2fddba..3d32ed3082 100644 --- a/network/src/service/ckb_service.rs +++ b/network/src/service/ckb_service.rs @@ -1,6 +1,6 @@ use crate::peer_store::{Behaviour, Status}; use crate::protocol_handler::DefaultCKBProtocolContext; -use crate::{peers_registry::RegisterResult, CKBEvent, CKBProtocolHandler, Network}; +use crate::{peers_registry::RegisterResult, CKBEvent, CKBProtocolHandler, Network, PeerId}; use futures::{sync::mpsc::Receiver, Async, Stream}; use log::{debug, error, info}; use p2p::ProtocolId; @@ -14,10 +14,18 @@ pub struct CKBService { } impl CKBService { - fn find_handler(&self, protocol_id: ProtocolId) -> Option> { + fn find_handler( + &self, + peer_id: &PeerId, + protocol_id: ProtocolId, + ) -> Option> { self.network - .find_protocol(protocol_id) - .map(|(_, handler)| handler) + .peer_protocol_version(peer_id, protocol_id) + .and_then(|version| { + self.network + .find_protocol(protocol_id, version) + .map(|(_, handler)| handler) + }) } } @@ -46,7 +54,7 @@ impl Stream for CKBService { let _ = peer_store.add_discovered_address(&peer_id, addr); } // call handler - match self.find_handler(protocol_id) { + match self.find_handler(&peer_id, protocol_id) { Some(handler) => handler.connected( Box::new(DefaultCKBProtocolContext::new( Arc::clone(&network), @@ -75,7 +83,7 @@ impl Stream for CKBService { } if let Some(peer_index) = network.get_peer_index(&peer_id) { // call handler - match self.find_handler(protocol_id) { + match self.find_handler(&peer_id, protocol_id) { Some(handler) => handler.disconnected( Box::new(DefaultCKBProtocolContext::new( Arc::clone(&network), @@ -97,7 +105,7 @@ impl Stream for CKBService { peer.last_message_time = Some(now); }); let peer_index = network.get_peer_index(&peer_id).expect("peer_index"); - match self.find_handler(protocol_id) { + match self.find_handler(&peer_id, protocol_id) { Some(handler) => handler.received( Box::new(DefaultCKBProtocolContext::new(network, protocol_id)), peer_index, From 9481ee1ab850fe1d175600471302aaaaa4f886be Mon Sep 17 00:00:00 2001 From: jjy Date: Tue, 12 Mar 2019 15:15:09 +0800 Subject: [PATCH 14/99] fix: p2p rebase --- network/src/network_service.rs | 7 +++++-- network/src/tests/peers_registry.rs | 10 +++++----- rpc/src/module/pool.rs | 4 ---- src/cli/run_impl.rs | 2 +- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/network/src/network_service.rs b/network/src/network_service.rs index c18519c57e..5cc4574984 100644 --- a/network/src/network_service.rs +++ b/network/src/network_service.rs @@ -1,11 +1,14 @@ -use crate::network::{CKBProtocols, Network}; use crate::protocol_handler::{CKBProtocolContext, DefaultCKBProtocolContext}; use crate::{errors::Error, CKBEvent, NetworkConfig, ProtocolId}; +use crate::{ + multiaddr::Multiaddr, + network::{CKBProtocols, Network}, + PeerId, +}; use ckb_util::Mutex; use futures::future::Future; use futures::sync::mpsc::Receiver; use futures::sync::oneshot; -use libp2p::{Multiaddr, PeerId}; use log::{debug, info}; use std::sync::Arc; use std::thread; diff --git a/network/src/tests/peers_registry.rs b/network/src/tests/peers_registry.rs index ad50a50e06..4a20df8665 100644 --- a/network/src/tests/peers_registry.rs +++ b/network/src/tests/peers_registry.rs @@ -130,19 +130,19 @@ fn test_accept_inbound_peer_eviction() { peer.ping = Some(Duration::from_secs(0)); } - // to prevent time error, we set now to 10000s ago. - let now = Instant::now() - Duration::from_secs(10000); + // to prevent time error, we set now to 10ago. + let now = Instant::now() - Duration::from_secs(10); // peers which most recently sent messages for _ in 0..EVICTION_PROTECT_PEERS { let peer_id = peers_iter.next().unwrap(); let mut peer = peers_registry.get_mut(&peer_id).unwrap(); - peer.last_message_time = Some(now + Duration::from_secs(10000)); + peer.last_message_time = Some(now + Duration::from_secs(10)); } // protect 5 peers which have the longest connection time for _ in 0..longest_connection_time_peers_count { let peer_id = peers_iter.next().unwrap(); let mut peer = peers_registry.get_mut(&peer_id).unwrap(); - peer.connected_time = now - Duration::from_secs(10000); + peer.connected_time = now - Duration::from_secs(10); } let mut new_peer_ids = (0..3).map(|_| random_peer_id()).collect::>(); // setup 3 node and 1 reserved node from addr1 @@ -184,7 +184,7 @@ fn test_accept_inbound_peer_eviction() { for peer_id in new_peer_ids { let mut peer = peers_registry.get_mut(&peer_id).unwrap(); // push the connected_time to make sure peer is unprotect - peer.connected_time = now + Duration::from_secs(10000); + peer.connected_time = now + Duration::from_secs(10); } // should evict evict target assert!(peers_registry.get(&evict_target).is_some()); diff --git a/rpc/src/module/pool.rs b/rpc/src/module/pool.rs index 5abd517503..62dbacdb0a 100644 --- a/rpc/src/module/pool.rs +++ b/rpc/src/module/pool.rs @@ -1,8 +1,4 @@ use ckb_core::transaction::{ProposalShortId, Transaction as CoreTransaction}; -use ckb_network::NetworkService; -use ckb_protocol::RelayMessage; -use ckb_shared::index::ChainIndex; -use ckb_shared::shared::Shared; use ckb_network::{NetworkService, ProtocolId}; use ckb_protocol::RelayMessage; use ckb_shared::index::ChainIndex; diff --git a/src/cli/run_impl.rs b/src/cli/run_impl.rs index daf99c0a7c..beec833520 100644 --- a/src/cli/run_impl.rs +++ b/src/cli/run_impl.rs @@ -3,7 +3,7 @@ use crate::Setup; use ckb_chain::chain::{ChainBuilder, ChainController}; use ckb_core::script::Script; use ckb_db::diskdb::RocksDB; -use ckb_miner::{BlockAssembler, BlockAssemblerController}; +use ckb_miner::BlockAssembler; use ckb_network::futures::sync::mpsc::channel; use ckb_network::CKBProtocol; use ckb_network::NetworkConfig; From c15cf6a9bd6fe5256fc1c745a4b0753d0af437d5 Mon Sep 17 00:00:00 2001 From: jjy Date: Tue, 12 Mar 2019 17:01:13 +0800 Subject: [PATCH 15/99] fix: impl protocol name for CKBProtocol --- network/src/protocol.rs | 18 +++++++++--------- src/cli/run_impl.rs | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/network/src/protocol.rs b/network/src/protocol.rs index 4db3df39e1..fd12de1ed9 100644 --- a/network/src/protocol.rs +++ b/network/src/protocol.rs @@ -1,5 +1,4 @@ use crate::{peers_registry::Session, PeerId, ServiceContext, SessionContext}; -use bytes::Bytes; use futures::sync::mpsc::Sender; use log::error; use p2p::{ @@ -16,7 +15,7 @@ const CKB_PROTOCOL_ID_OFFSET: ProtocolId = 100; pub struct CKBProtocol { id: ProtocolId, // for example: b"/ckb/" - base_name: Bytes, + protocol_name: String, // supported version, used to check protocol version supported_versions: Vec, event_sender: Sender, @@ -24,16 +23,13 @@ pub struct CKBProtocol { impl CKBProtocol { pub fn new( - base_name: String, + protocol_name: String, id: ProtocolId, versions: &[Version], event_sender: Sender, ) -> Self { - let mut base_name_bytes = Bytes::from(format!("/{}/", base_name)); - base_name_bytes.extend_from_slice(format!("{}", id).as_bytes()); - base_name_bytes.extend_from_slice(b"/"); CKBProtocol { - base_name: base_name_bytes, + protocol_name: format!("/ckb/{}/", protocol_name).to_string(), id, supported_versions: { let mut versions: Vec<_> = versions.to_vec(); @@ -46,8 +42,8 @@ impl CKBProtocol { pub fn id(&self) -> ProtocolId { self.id + CKB_PROTOCOL_ID_OFFSET } - pub fn base_name(&self) -> Bytes { - self.base_name.clone() + pub fn protocol_name(&self) -> String { + self.protocol_name.clone() } pub fn match_version(&self, version: Version) -> bool { @@ -56,6 +52,10 @@ impl CKBProtocol { } impl ProtocolMeta for CKBProtocol { + fn name(&self) -> String { + self.protocol_name() + } + fn id(&self) -> ProtocolId { self.id } diff --git a/src/cli/run_impl.rs b/src/cli/run_impl.rs index beec833520..bf3218659d 100644 --- a/src/cli/run_impl.rs +++ b/src/cli/run_impl.rs @@ -60,7 +60,7 @@ pub fn run(setup: Setup) { let protocols = vec![ ( CKBProtocol::new( - "ckb/syn".to_string(), + "syn".to_string(), NetworkProtocol::SYNC as ProtocolId, &[1][..], sender.clone(), @@ -69,7 +69,7 @@ pub fn run(setup: Setup) { ), ( CKBProtocol::new( - "ckb/rel".to_string(), + "rel".to_string(), NetworkProtocol::RELAY as ProtocolId, &[1][..], sender.clone(), @@ -78,7 +78,7 @@ pub fn run(setup: Setup) { ), ( CKBProtocol::new( - "ckb/tim".to_string(), + "tim".to_string(), NetworkProtocol::TIME as ProtocolId, &[1][..], sender.clone(), From 645c8135a470aea88d9650bf97dab6bb11a7ddeb Mon Sep 17 00:00:00 2001 From: jjy Date: Tue, 12 Mar 2019 20:35:55 +0800 Subject: [PATCH 16/99] refactor: use HashMap store peer protocol version --- network/src/network.rs | 19 +++++-------------- network/src/peers_registry.rs | 9 +++------ 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/network/src/network.rs b/network/src/network.rs index a4d5a96ef1..bf172bab5d 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -41,7 +41,6 @@ use std::time::{Duration, Instant}; use std::usize; use tokio::codec::LengthDelimitedCodec; -const DIAL_BOOTNODE_TIMEOUT: u64 = 20; const PING_PROTOCOL_ID: ProtocolId = 0; pub type CKBProtocols = Vec<(CKBProtocol, Arc)>; @@ -282,7 +281,7 @@ impl Network { Some(peer) => match peer.protocol_version(protocol_id) { Some(_) => return Err(ProtocolError::Duplicate(protocol_id).into()), None => { - peer.protocols.push((protocol_id, protocol_version)); + peer.protocols.insert(protocol_id, protocol_version); } }, None => unreachable!("get peer after inserted"), @@ -292,21 +291,14 @@ impl Network { pub fn peer_protocol_version(&self, peer_id: &PeerId, protocol_id: ProtocolId) -> Option { let peers_registry = self.peers_registry.read(); - peers_registry.get(peer_id).and_then(|peer| { - peer.protocols - .iter() - .find(|(id, _)| id == &protocol_id) - .map(|(_, version)| *version) - }) + peers_registry + .get(peer_id) + .and_then(|peer| peer.protocol_version(protocol_id)) } pub fn session_info(&self, peer_id: &PeerId, protocol_id: ProtocolId) -> Option { let peers_registry = self.peers_registry.read(); peers_registry.get(peer_id).map(|peer| { - let protocol_version = peer - .protocols - .iter() - .find(|(id, _)| id == &protocol_id) - .map(|(_, version)| *version); + let protocol_version = peer.protocol_version(protocol_id); SessionInfo { peer: PeerInfo { peer_id: peer_id.to_owned(), @@ -444,7 +436,6 @@ impl Network { // dial reserved nodes and bootnodes { let network = Arc::clone(&network); - let _dial_timeout = Duration::from_secs(DIAL_BOOTNODE_TIMEOUT); // dial reserved_nodes for (peer_id, addr) in config.reserved_peers()? { network.dial(&peer_id, addr); diff --git a/network/src/peers_registry.rs b/network/src/peers_registry.rs index 6a9ff2ff47..16b711e002 100644 --- a/network/src/peers_registry.rs +++ b/network/src/peers_registry.rs @@ -91,7 +91,7 @@ impl PeerManage { connected_time: Instant::now(), peer_index, session, - protocols: Vec::new(), + protocols: FnvHashMap::with_capacity_and_hasher(1, Default::default()), }; entry.insert(peer); self.peer_id_by_index.insert(peer_index, peer_id); @@ -141,7 +141,7 @@ pub struct Peer { pub ping: Option, pub connected_time: Instant, pub session: Session, - pub protocols: Vec<(ProtocolId, Version)>, + pub protocols: FnvHashMap, } impl Peer { @@ -163,10 +163,7 @@ impl Peer { } pub fn protocol_version(&self, protocol_id: ProtocolId) -> Option { - self.protocols - .iter() - .find(|(id, _)| id == &protocol_id) - .map(|(_, version)| *version) + self.protocols.get(&protocol_id).map(|v| *v) } } From febbcb936bf7160b8a1a3f2c44ea7c07f53f59af Mon Sep 17 00:00:00 2001 From: jjy Date: Tue, 12 Mar 2019 22:30:56 +0800 Subject: [PATCH 17/99] fix: network clippy --- network/src/peers_registry.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/network/src/peers_registry.rs b/network/src/peers_registry.rs index 16b711e002..8807edc114 100644 --- a/network/src/peers_registry.rs +++ b/network/src/peers_registry.rs @@ -163,7 +163,7 @@ impl Peer { } pub fn protocol_version(&self, protocol_id: ProtocolId) -> Option { - self.protocols.get(&protocol_id).map(|v| *v) + self.protocols.get(&protocol_id).cloned() } } From 464b333367ec5bf69b4028e8712c2b8c3e15b4b8 Mon Sep 17 00:00:00 2001 From: jjy Date: Thu, 14 Mar 2019 11:49:52 +0800 Subject: [PATCH 18/99] fix: network protocol_id mismatch --- network/src/network.rs | 2 +- network/src/peers_registry.rs | 1 + network/src/protocol.rs | 16 ++++++++++------ network/src/service/ckb_service.rs | 2 +- sync/src/lib.rs | 6 +++--- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/network/src/network.rs b/network/src/network.rs index bf172bab5d..bdf9d71efe 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -83,7 +83,7 @@ impl PeerInfo { type P2PService = Service; pub struct Network { - peers_registry: RwLock, + pub(crate) peers_registry: RwLock, peer_store: Arc>, listened_addresses: RwLock>, pub(crate) original_listened_addresses: RwLock>, diff --git a/network/src/peers_registry.rs b/network/src/peers_registry.rs index 8807edc114..7324f6c92e 100644 --- a/network/src/peers_registry.rs +++ b/network/src/peers_registry.rs @@ -131,6 +131,7 @@ pub struct Session { pub session_type: SessionType, } +#[derive(Clone, Debug)] pub struct Peer { pub(crate) peer_index: PeerIndex, pub connected_addr: Multiaddr, diff --git a/network/src/protocol.rs b/network/src/protocol.rs index fd12de1ed9..9dd3fb80c1 100644 --- a/network/src/protocol.rs +++ b/network/src/protocol.rs @@ -1,6 +1,6 @@ use crate::{peers_registry::Session, PeerId, ServiceContext, SessionContext}; use futures::sync::mpsc::Sender; -use log::error; +use log::{debug, error}; use p2p::{ multiaddr::Multiaddr, traits::{ProtocolMeta, ServiceProtocol}, @@ -9,7 +9,6 @@ use p2p::{ use tokio::codec::LengthDelimitedCodec; pub type Version = u8; -const CKB_PROTOCOL_ID_OFFSET: ProtocolId = 100; #[derive(Clone)] pub struct CKBProtocol { @@ -39,9 +38,11 @@ impl CKBProtocol { event_sender, } } + pub fn id(&self) -> ProtocolId { - self.id + CKB_PROTOCOL_ID_OFFSET + self.id } + pub fn protocol_name(&self) -> String { self.protocol_name.clone() } @@ -57,7 +58,7 @@ impl ProtocolMeta for CKBProtocol { } fn id(&self) -> ProtocolId { - self.id + CKBProtocol::id(&self) } fn codec(&self) -> LengthDelimitedCodec { @@ -66,7 +67,7 @@ impl ProtocolMeta for CKBProtocol { fn service_handle(&self) -> Option> { let handler = Box::new(CKBHandler { - id: self.id, + id: self.id(), event_sender: self.event_sender.clone(), }); Some(handler) @@ -107,7 +108,7 @@ impl ServiceProtocol for CKBHandler { let (peer_id, version) = { let parsed_version = version.parse::(); if session.remote_pubkey.is_none() || parsed_version.is_err() { - error!(target: "network", "ckb protocol connected error, addr: {}, version: {}", session.address, version); + error!(target: "network", "ckb protocol connected error, addr: {}, protocol:{}, version: {}", session.address, self.id, version); control.disconnect(session.id); return; } @@ -120,6 +121,7 @@ impl ServiceProtocol for CKBHandler { parsed_version.unwrap(), ) }; + debug!(target: "network", "ckb protocol connected, addr: {}, protocol: {}, version: {}, peer_id: {:?}", session.address, self.id, version, &peer_id); let event = Event::Connected( peer_id, session.address.clone(), @@ -139,6 +141,7 @@ impl ServiceProtocol for CKBHandler { .as_ref() .map(|pubkey| pubkey.peer_id()) { + debug!(target: "network", "ckb protocol disconnect, addr: {}, protocol: {}, peer_id: {:?}", session.address, self.id, &peer_id); self.send_event(Event::Disconnected(peer_id, self.id)); } } @@ -149,6 +152,7 @@ impl ServiceProtocol for CKBHandler { .as_ref() .map(|pubkey| pubkey.peer_id()) { + debug!(target: "network", "ckb protocol received, addr: {}, protocol: {}, peer_id: {:?}", session.address, self.id, &peer_id); self.send_event(Event::Received(peer_id, self.id, data)); } } diff --git a/network/src/service/ckb_service.rs b/network/src/service/ckb_service.rs index 3d32ed3082..f9bff7f7ee 100644 --- a/network/src/service/ckb_service.rs +++ b/network/src/service/ckb_service.rs @@ -64,7 +64,7 @@ impl Stream for CKBService { ), None => { network.drop_peer(&peer_id); - error!(target: "network", "can't find protocol handler for {:?}",session) + error!(target: "network", "can't find protocol handler for {:?} {}",session, protocol_id) } } } diff --git a/sync/src/lib.rs b/sync/src/lib.rs index 1eb2bf287b..719bb4c55c 100644 --- a/sync/src/lib.rs +++ b/sync/src/lib.rs @@ -29,9 +29,9 @@ pub const BLOCK_DOWNLOAD_WINDOW: u64 = 1024; pub const PER_FETCH_BLOCK_LIMIT: usize = 128; pub enum NetworkProtocol { - SYNC = 0, - RELAY = 1, - TIME = 2, + SYNC = 100, + RELAY = 101, + TIME = 102, } // Timeout = base + per_header * (expected number of headers) From 549136a4edf6c133e358d41cb77e8acc550f4d9c Mon Sep 17 00:00:00 2001 From: jjy Date: Thu, 14 Mar 2019 12:35:42 +0800 Subject: [PATCH 19/99] fix: network integration tests --- network/src/lib.rs | 6 +++++- network/src/service/outbound_peer_service.rs | 3 ++- nodes_template/default.toml | 1 - test/fixtures/nodes_template/default.toml | 5 ++--- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/network/src/lib.rs b/network/src/lib.rs index f929286355..493d835114 100644 --- a/network/src/lib.rs +++ b/network/src/lib.rs @@ -27,6 +27,7 @@ pub(crate) use p2p::{ service::ServiceControl, }; use serde_derive::Deserialize; +use std::time::Duration; const DEFAULT_OUTGOING_PEERS_RATIO: u32 = 3; @@ -37,7 +38,7 @@ pub type PeerIndex = usize; pub struct Config { pub listen_addresses: Vec, pub secret_file: Option, - pub nodes_file: Option, + pub try_outbound_connect_secs: Option, /// List of initial node addresses pub bootnodes: Vec, /// List of reserved node addresses. @@ -70,6 +71,9 @@ impl From for NetworkConfig { cfg.listen_addresses = config.listen_addresses; cfg.bootnodes = config.bootnodes; cfg.reserved_peers = config.reserved_nodes; + if let Some(try_outbound_connect_secs) = config.try_outbound_connect_secs { + cfg.try_outbound_connect_interval = Duration::from_secs(try_outbound_connect_secs); + } if let Some(value) = config.non_reserved_mode { cfg.reserved_only = match value.as_str() { "Accept" => false, diff --git a/network/src/service/outbound_peer_service.rs b/network/src/service/outbound_peer_service.rs index 6b80be0cb8..4f3906ff7f 100644 --- a/network/src/service/outbound_peer_service.rs +++ b/network/src/service/outbound_peer_service.rs @@ -1,6 +1,6 @@ use crate::Network; use futures::{Async, Stream}; -use log::error; +use log::{debug, error}; use std::sync::Arc; use std::time::Duration; use std::usize; @@ -13,6 +13,7 @@ pub struct OutboundPeerService { impl OutboundPeerService { pub fn new(network: Arc, try_connect_interval: Duration) -> Self { + debug!(target: "network", "outbound peer service start, interval: {:?}", try_connect_interval); OutboundPeerService { network, stream_interval: Interval::new_interval(try_connect_interval), diff --git a/nodes_template/default.toml b/nodes_template/default.toml index c0071247f0..1696b611b3 100644 --- a/nodes_template/default.toml +++ b/nodes_template/default.toml @@ -19,7 +19,6 @@ only_reserved_peers = false min_peers = 4 max_peers = 8 secret_file = "secret" -nodes_file = "nodes.toml" [rpc] listen_address = "0.0.0.0:8114" diff --git a/test/fixtures/nodes_template/default.toml b/test/fixtures/nodes_template/default.toml index 4ede8a0e17..10cfb71fdc 100644 --- a/test/fixtures/nodes_template/default.toml +++ b/test/fixtures/nodes_template/default.toml @@ -8,7 +8,7 @@ spec = "spec/integration_test.toml" [logger] file = "ckb.log" -filter = "info,network=trace,rpc=debug" +filter = "info,network=trace,rpc=debug,sync=debug,relay=debug" color = true [network] @@ -19,8 +19,7 @@ only_reserved_peers = false min_peers = 4 max_peers = 8 secret_file = "secret" -nodes_file = "nodes.toml" -try_outbound_connect_interval = 1 +try_outbound_connect_secs = 1 [rpc] listen_address = "0.0.0.0:RPC_PORT" From f2ba52334b1140618305862868d2a614d2451436 Mon Sep 17 00:00:00 2001 From: jjy Date: Thu, 14 Mar 2019 12:44:27 +0800 Subject: [PATCH 20/99] chore: rebase Cargo.lock --- Cargo.lock | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7579a8aa2e..5a485191d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -785,23 +785,6 @@ dependencies = [ "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "core-foundation" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "core-foundation-sys 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "core-foundation-sys" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "criterion" version = "0.2.10" @@ -3051,8 +3034,11 @@ dependencies = [ "checksum bit-set 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6f1efcc46c18245a69c38fcc5cc650f16d3a59d034f3106e9ed63748f695730a" "checksum bit-vec 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4440d5cb623bb7390ae27fec0bb6c61111969860f8e3ae198bfa0663645e67cf" "checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" +"checksum blake2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "91721a6330935673395a0607df4d49a9cb90ae12d259f1b3e0a3f6e1d486872e" "checksum blake2b-rs 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "895ae726056ce2ab1b19854be889ca43a4a2c0b02c355bee90aa9e8ee6d0379b" -"checksum block-buffer 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a076c298b9ecdb530ed9d967e74a6027d6a7478924520acddcddc24c1c8ab3ab" +"checksum block-buffer 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "49665c62e0e700857531fa5d3763e91b539ff1abeebd56808d378b495870d60d" +"checksum block-cipher-trait 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1c924d49bd09e7c06003acda26cd9742e796e34282ec6c1189404dee0c1f4774" +"checksum block-padding 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d75255892aeb580d3c566f213a2b6fdc1c66667839f45719ee1d30ebf2aea591" "checksum bloom-filters 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b233192e97c6e528c071bd83180c5d4f35846b9aff4670ea8e05e3ba0daa5e30" "checksum bs58 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0de79cfb98e7aa9988188784d8664b4b5dad6eaaa0863b91d9a4ed871d4f7a42" "checksum byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" @@ -3070,8 +3056,6 @@ dependencies = [ "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum config 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3e82d07fac0a5eeaa9d959b5194d01bb66e414665f547416958d2b430f8f4852" "checksum console 0.7.5 (registry+https://github.com/rust-lang/crates.io-index)" = "2bf3720d3f3fc30b721ef1ae54e13af3264af4af39dc476a8de56a6ee1e2184b" -"checksum core-foundation 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "25bfd746d203017f7d5cbd31ee5d8e17f94b6521c7af77ece6c9e4b2d4b16c67" -"checksum core-foundation-sys 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "065a5d7ffdcbc8fa145d6f0746f3555025b9097a9e9cda59f7467abae670c78d" "checksum criterion 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "1c6e5ee5b9652d4f851418c448af105642e1f99e9a2741a8ff45c0d2c911b1e0" "checksum criterion-plot 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4107e4a5abb94267e0149922b8ff49dc70a87cc202820fdbfc0d3e1edbdc4b16" "checksum crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "0f0ed1a4de2235cabda8558ff5840bffb97fcb64c97827f354a451307df5f72b" @@ -3156,7 +3140,6 @@ dependencies = [ "checksum memchr 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e1dd4eaac298c32ce07eb6ed9242eda7d82955b9170b7d6db59b2e02cc63fcb8" "checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3" "checksum merkle-cbt 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d589b5a7ca642540e7ccfbca3bcd0aa18693eb9287e2a6b17c79b1d062d52863" -"checksum mime 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ba626b8a6de5da682e1caa06bdb42a335aee5a84db8e5046a3e8ab17ba0a3ae0" "checksum mime 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)" = "3e27ca21f40a310bd06d9031785f4801710d566c184a6e15bad4f1d9b65f9425" "checksum mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)" = "71646331f2619b1026cc302f87a2b8b648d5c6dd6937846a16cc8ce0f347f432" "checksum mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "966257a94e196b11bb43aca423754d87429960a768de9414f3691d6957abf125" From 9fa25d4afd63768d91511edd4f076a4fa9ee3906 Mon Sep 17 00:00:00 2001 From: quake wang Date: Fri, 15 Mar 2019 14:26:55 +0900 Subject: [PATCH 21/99] fix: use reference script in integration test --- nodes_template/default.toml | 1 + spec/src/lib.rs | 21 +++++++++++++----- test/fixtures/nodes_template/default.toml | 2 +- test/src/node.rs | 27 ++++++++++++++++------- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/nodes_template/default.toml b/nodes_template/default.toml index 1696b611b3..9af7b7c55d 100644 --- a/nodes_template/default.toml +++ b/nodes_template/default.toml @@ -43,4 +43,5 @@ trace = 100 txs_verify_cache_size = 100000 [block_assembler] +# value is from spec#always_success_type_hash test type_hash = "0x8954a4ac5e5c33eb7aa8bb91e0a000179708157729859bd8cf7e2278e1e12980" diff --git a/spec/src/lib.rs b/spec/src/lib.rs index ebf00bd144..8cd91453bc 100644 --- a/spec/src/lib.rs +++ b/spec/src/lib.rs @@ -174,11 +174,22 @@ pub mod test { }]) .unwrap(); - let script = Script::new(0, vec![], Some(tx.outputs()[0].data_hash()), None, vec![]); - let expect = - H256::from_hex_str("8954a4ac5e5c33eb7aa8bb91e0a000179708157729859bd8cf7e2278e1e12980") - .unwrap(); + // Tx and Output hash will be used in some test cases directly, assert here for convenience + assert_eq!( + format!("{:x}", tx.hash()), + "06d185ca44a1426b01d8809738c84259b86dc33bfe99f271938432a9de4cc3aa" + ); + + let reference = tx.outputs()[0].data_hash(); + assert_eq!( + format!("{:x}", reference), + "61d7e01908bafa29d742e37b470dc906fb05c2115b0beba7b1c4fa3e66ca3e44" + ); - assert_eq!(script.type_hash(), expect); + let script = Script::new(0, vec![], Some(reference), None, vec![]); + assert_eq!( + format!("{:x}", script.type_hash()), + "8954a4ac5e5c33eb7aa8bb91e0a000179708157729859bd8cf7e2278e1e12980" + ); } } diff --git a/test/fixtures/nodes_template/default.toml b/test/fixtures/nodes_template/default.toml index 10cfb71fdc..68ba5b876c 100644 --- a/test/fixtures/nodes_template/default.toml +++ b/test/fixtures/nodes_template/default.toml @@ -42,5 +42,5 @@ trace = 100 txs_verify_cache_size = 100000 [block_assembler] -# type hash for `always_success` script +# value is from spec#always_success_type_hash test type_hash = "0x8954a4ac5e5c33eb7aa8bb91e0a000179708157729859bd8cf7e2278e1e12980" diff --git a/test/src/node.rs b/test/src/node.rs index e29b6a7503..587da88da8 100644 --- a/test/src/node.rs +++ b/test/src/node.rs @@ -175,16 +175,27 @@ impl Node { } pub fn new_transaction(&self, hash: H256) -> Transaction { - let mut file = File::open( - PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("fixtures/nodes_template/spec/cells/always_success"), - ) - .expect("faild to load script"); - let mut buffer = Vec::new(); - file.read_to_end(&mut buffer).unwrap(); - let script = Script::new(0, Vec::new(), None, Some(buffer), Vec::new()); + // OutPoint and Script reference hash values are from spec#always_success_type_hash test + let out_point = OutPoint::new( + H256::from_hex_str("06d185ca44a1426b01d8809738c84259b86dc33bfe99f271938432a9de4cc3aa") + .unwrap(), + 0, + ); + let script = Script::new( + 0, + vec![], + Some( + H256::from_hex_str( + "61d7e01908bafa29d742e37b470dc906fb05c2115b0beba7b1c4fa3e66ca3e44", + ) + .unwrap(), + ), + None, + vec![], + ); TransactionBuilder::default() + .dep(out_point) .output(CellOutput::new(50000, vec![], script.type_hash(), None)) .input(CellInput::new(OutPoint::new(hash, 0), script)) .build() From 5393c68cd5fc9edda9fe2e0936f9ca366e77755b Mon Sep 17 00:00:00 2001 From: zhangsoledad <787953403@qq.com> Date: Fri, 15 Mar 2019 15:16:10 +0800 Subject: [PATCH 22/99] feat: version verification --- core/src/header.rs | 2 ++ core/src/transaction.rs | 2 ++ verification/src/error.rs | 3 +++ verification/src/header_verifier.rs | 21 +++++++++++++++++++-- verification/src/transaction_verifier.rs | 22 +++++++++++++++++++++- 5 files changed, 47 insertions(+), 3 deletions(-) diff --git a/core/src/header.rs b/core/src/header.rs index 688f8342a0..1b2dd52938 100644 --- a/core/src/header.rs +++ b/core/src/header.rs @@ -8,6 +8,8 @@ use std::fmt; pub use crate::{BlockNumber, Version}; +pub const HEADER_VERSION: Version = 0; + #[derive(Clone, Serialize, Deserialize, PartialEq, Eq, Default)] pub struct Seal { nonce: u64, diff --git a/core/src/transaction.rs b/core/src/transaction.rs index 275e700454..06119ebb13 100644 --- a/core/src/transaction.rs +++ b/core/src/transaction.rs @@ -13,6 +13,8 @@ use std::fmt; use std::hash::{Hash, Hasher}; use std::ops::{Deref, DerefMut}; +pub const TX_VERSION: Version = 0; + #[derive(Clone, Serialize, Deserialize, Eq, PartialEq, Hash, OccupiedCapacity)] pub struct OutPoint { // Hash of Transaction diff --git a/verification/src/error.rs b/verification/src/error.rs index 80a713bed1..79417af82a 100644 --- a/verification/src/error.rs +++ b/verification/src/error.rs @@ -49,6 +49,8 @@ pub enum Error { /// Cycles consumed by all scripts in all commit transactions of the block exceed /// the maximum allowed cycles in consensus rules ExceededMaximumCycles, + /// The field version in block header is not allowed. + Version, } impl fmt::Display for Error { @@ -140,4 +142,5 @@ pub enum TransactionError { InvalidSignature, Conflict, UnknownInput, + Version, } diff --git a/verification/src/header_verifier.rs b/verification/src/header_verifier.rs index c2f6e4c509..80644412db 100644 --- a/verification/src/header_verifier.rs +++ b/verification/src/header_verifier.rs @@ -1,7 +1,7 @@ use super::Verifier; use crate::error::{DifficultyError, Error, NumberError, PowError, TimestampError}; use crate::shared::ALLOWED_FUTURE_BLOCKTIME; -use ckb_core::header::Header; +use ckb_core::header::{Header, HEADER_VERSION}; use ckb_pow::PowEngine; use ckb_traits::BlockMedianTimeContext; use faketime::unix_time_as_millis; @@ -37,7 +37,7 @@ impl Verifier for HeaderVe type Target = T; fn verify(&self, target: &T) -> Result<(), Error> { let header = target.header(); - + VersionVerifier::new(header).verify()?; // POW check first PowVerifier::new(header, &self.pow).verify()?; let parent = target @@ -50,6 +50,23 @@ impl Verifier for HeaderVe } } +pub struct VersionVerifier<'a> { + header: &'a Header, +} + +impl<'a> VersionVerifier<'a> { + pub fn new(header: &'a Header) -> Self { + VersionVerifier { header } + } + + pub fn verify(&self) -> Result<(), Error> { + if self.header.version() != HEADER_VERSION { + return Err(Error::Version); + } + Ok(()) + } +} + pub struct TimestampVerifier<'a, M> { header: &'a Header, block_median_time_context: M, diff --git a/verification/src/transaction_verifier.rs b/verification/src/transaction_verifier.rs index 762d9713ab..38a799f608 100644 --- a/verification/src/transaction_verifier.rs +++ b/verification/src/transaction_verifier.rs @@ -1,11 +1,12 @@ use crate::error::TransactionError; -use ckb_core::transaction::{Capacity, Transaction}; +use ckb_core::transaction::{Capacity, Transaction, TX_VERSION}; use ckb_core::{cell::ResolvedTransaction, Cycle}; use ckb_script::TransactionScriptsVerifier; use occupied_capacity::OccupiedCapacity; use std::collections::HashSet; pub struct TransactionVerifier<'a> { + pub version: VersionVerifier<'a>, pub null: NullVerifier<'a>, pub empty: EmptyVerifier<'a>, pub capacity: CapacityVerifier<'a>, @@ -17,6 +18,7 @@ pub struct TransactionVerifier<'a> { impl<'a> TransactionVerifier<'a> { pub fn new(rtx: &'a ResolvedTransaction) -> Self { TransactionVerifier { + version: VersionVerifier::new(&rtx.transaction), null: NullVerifier::new(&rtx.transaction), empty: EmptyVerifier::new(&rtx.transaction), duplicate_inputs: DuplicateInputsVerifier::new(&rtx.transaction), @@ -27,6 +29,7 @@ impl<'a> TransactionVerifier<'a> { } pub fn verify(&self, max_cycles: Cycle) -> Result { + self.version.verify()?; self.empty.verify()?; self.null.verify()?; self.capacity.verify()?; @@ -38,6 +41,23 @@ impl<'a> TransactionVerifier<'a> { } } +pub struct VersionVerifier<'a> { + transaction: &'a Transaction, +} + +impl<'a> VersionVerifier<'a> { + pub fn new(transaction: &'a Transaction) -> Self { + VersionVerifier { transaction } + } + + pub fn verify(&self) -> Result<(), TransactionError> { + if self.transaction.version() != TX_VERSION { + return Err(TransactionError::Version); + } + Ok(()) + } +} + pub struct InputVerifier<'a> { resolved_transaction: &'a ResolvedTransaction, } From 76fc0edc10343aa8a5a989035bf66ec20827c0fc Mon Sep 17 00:00:00 2001 From: ian Date: Fri, 15 Mar 2019 14:25:10 +0800 Subject: [PATCH 23/99] test: setup integration test in Travis --- bors.toml | 4 ++++ devtools/ci/script.sh | 11 +++++++++++ 2 files changed, 15 insertions(+) create mode 100644 bors.toml diff --git a/bors.toml b/bors.toml new file mode 100644 index 0000000000..4ee923aedd --- /dev/null +++ b/bors.toml @@ -0,0 +1,4 @@ +status = [ + "continuous-integration/travis-ci/push" +] +required_approvals = 1 diff --git a/devtools/ci/script.sh b/devtools/ci/script.sh index 39de5aad0e..91f61bcb0f 100755 --- a/devtools/ci/script.sh +++ b/devtools/ci/script.sh @@ -1,6 +1,8 @@ #!/bin/bash set -ev +echo "TRAVIS_BRANCH=$TRAVIS_BRANCH" + cargo sweep -s if [ "$FMT" = true ]; then @@ -15,3 +17,12 @@ if [ "$TEST" = true ]; then fi git diff --exit-code Cargo.lock + +if [ "$TRAVIS_BRANCH" = master -o "$TRAVIS_BRANCH" = staging -o "$TRAVIS_BRANCH" = trying ]; then + cargo build + cargo run -p ckb-test target/debug/ckb + + # Switch to release mode when the running time is much longer than the build time. + # cargo build --release + # cargo run --release -p ckb-test target/release/ckb +fi From eb1539d83632e68ec6e035e00f2bb38d9fb9163d Mon Sep 17 00:00:00 2001 From: zhangsoledad <787953403@qq.com> Date: Fri, 15 Mar 2019 16:32:58 +0800 Subject: [PATCH 24/99] feat: enforce `type` field of a cellbase output cell must be absent --- verification/src/block_verifier.rs | 38 +++++++----------------- verification/src/error.rs | 3 +- verification/src/tests/block_verifier.rs | 9 ++---- verification/src/transaction_verifier.rs | 9 +++--- 4 files changed, 19 insertions(+), 40 deletions(-) diff --git a/verification/src/block_verifier.rs b/verification/src/block_verifier.rs index 42193e85ea..3f1fd78020 100644 --- a/verification/src/block_verifier.rs +++ b/verification/src/block_verifier.rs @@ -18,8 +18,6 @@ use std::collections::HashSet; //TODO: cellbase, witness #[derive(Clone)] pub struct BlockVerifier

{ - // Verify if the committed transactions is empty - empty: EmptyVerifier, // Verify if the committed and proposed transactions contains duplicate duplicate: DuplicateVerifier, // Verify the cellbase @@ -39,7 +37,6 @@ where pub fn new(provider: P) -> Self { BlockVerifier { // TODO change all new fn's chain to reference - empty: EmptyVerifier::new(), duplicate: DuplicateVerifier::new(), cellbase: CellbaseVerifier::new(provider.clone()), merkle_root: MerkleRootVerifier::new(), @@ -53,11 +50,8 @@ impl Verifier for BlockVerifier

{ type Target = Block; fn verify(&self, target: &Block) -> Result<(), Error> { - // EmptyTransactionsVerifier must be executed first. Other verifiers may depend on the - // assumption that the transactions list is not empty. - self.empty.verify(target)?; - self.duplicate.verify(target)?; self.cellbase.verify(target)?; + self.duplicate.verify(target)?; self.merkle_root.verify(target)?; self.commit.verify(target)?; self.uncles.verify(target) @@ -75,9 +69,6 @@ impl CellbaseVerifier { } pub fn verify(&self, block: &Block) -> Result<(), Error> { - if block.commit_transactions().is_empty() { - return Ok(()); - } let cellbase_len = block .commit_transactions() .iter() @@ -99,6 +90,16 @@ impl CellbaseVerifier { { return Err(Error::Cellbase(CellbaseError::InvalidInput)); } + + // currently, we enforce`type` field of a cellbase output cell must be absent + if cellbase_transaction + .outputs() + .iter() + .any(|op| op.type_.is_some()) + { + return Err(Error::Cellbase(CellbaseError::InvalidOutput)); + } + let block_reward = self.provider.block_reward(block.header().number()); let mut fee = 0; for transaction in block.commit_transactions().iter().skip(1) { @@ -121,23 +122,6 @@ impl CellbaseVerifier { } } -#[derive(Clone)] -pub struct EmptyVerifier {} - -impl EmptyVerifier { - pub fn new() -> Self { - EmptyVerifier {} - } - - pub fn verify(&self, block: &Block) -> Result<(), Error> { - if block.commit_transactions().is_empty() { - Err(Error::CommitTransactionsEmpty) - } else { - Ok(()) - } - } -} - #[derive(Clone)] pub struct DuplicateVerifier {} diff --git a/verification/src/error.rs b/verification/src/error.rs index 80a713bed1..5fe9e79142 100644 --- a/verification/src/error.rs +++ b/verification/src/error.rs @@ -27,8 +27,6 @@ pub enum Error { Transactions((usize, TransactionError)), /// This is a wrapper of error encountered when invoking chain API. Chain(String), - /// The committed transactions list is empty. - CommitTransactionsEmpty, /// There are duplicate proposed transactions. ProposalTransactionDuplicate, /// There are duplicate committed transactions. @@ -71,6 +69,7 @@ pub enum CellbaseError { InvalidReward, InvalidQuantity, InvalidPosition, + InvalidOutput, } #[derive(Debug, PartialEq, Clone, Eq)] diff --git a/verification/src/tests/block_verifier.rs b/verification/src/tests/block_verifier.rs index 2f327ec5ab..5d7cd96100 100644 --- a/verification/src/tests/block_verifier.rs +++ b/verification/src/tests/block_verifier.rs @@ -1,4 +1,4 @@ -use super::super::block_verifier::{BlockVerifier, CellbaseVerifier, EmptyVerifier}; +use super::super::block_verifier::{BlockVerifier, CellbaseVerifier}; use super::super::error::{CellbaseError, Error as VerifyError}; use super::dummy::DummyChainProvider; use crate::Verifier; @@ -244,15 +244,10 @@ pub fn test_empty_transactions() { transaction_fees, }; - let verifier = EmptyVerifier::new(); let full_verifier = BlockVerifier::new(provider); - assert_eq!( - verifier.verify(&block), - Err(VerifyError::CommitTransactionsEmpty) - ); // short-circuit, Empty check first assert_eq!( full_verifier.verify(&block), - Err(VerifyError::CommitTransactionsEmpty) + Err(VerifyError::Cellbase(CellbaseError::InvalidQuantity)) ); } diff --git a/verification/src/transaction_verifier.rs b/verification/src/transaction_verifier.rs index 762d9713ab..ba90a55849 100644 --- a/verification/src/transaction_verifier.rs +++ b/verification/src/transaction_verifier.rs @@ -50,13 +50,14 @@ impl<'a> InputVerifier<'a> { } pub fn verify(&self) -> Result<(), TransactionError> { - let mut inputs = self.resolved_transaction.transaction.inputs().iter(); - for cs in &self.resolved_transaction.input_cells { + let inputs = self.resolved_transaction.transaction.inputs().iter(); + let input_cells = self.resolved_transaction.input_cells.iter(); + for (input, cs) in inputs.zip(input_cells) { if cs.is_live() { - if let Some(ref input) = cs.get_live() { + if let Some(ref input_cell) = cs.get_live() { // TODO: remove this once VM mmap is in place so we can // do P2SH within the VM. - if input.lock != inputs.next().unwrap().unlock.type_hash() { + if input_cell.lock != input.unlock.type_hash() { return Err(TransactionError::InvalidScript); } } From d716482c7de095881f43a265ed47ad7ddd9d6507 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" Date: Sun, 17 Mar 2019 21:13:52 +0000 Subject: [PATCH 25/99] chore(deps): bump secp256k1 from 0.12.0 to 0.12.2 Bumps [secp256k1](https://github.com/rust-bitcoin/rust-secp256k1) from 0.12.0 to 0.12.2. - [Release notes](https://github.com/rust-bitcoin/rust-secp256k1/releases) - [Changelog](https://github.com/rust-bitcoin/rust-secp256k1/blob/master/CHANGELOG.md) - [Commits](https://github.com/rust-bitcoin/rust-secp256k1/compare/0.12.0...0.12.2) Signed-off-by: dependabot[bot] --- Cargo.lock | 29 ++++++++++++++--------------- util/crypto/Cargo.toml | 2 +- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5a485191d6..1a37a40c4c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -96,7 +96,7 @@ name = "backtrace-sys" version = "0.1.28" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cc 1.0.29 (registry+https://github.com/rust-lang/crates.io-index)", + "cc 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -174,7 +174,7 @@ name = "blake2b-rs" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cc 1.0.29 (registry+https://github.com/rust-lang/crates.io-index)", + "cc 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -247,7 +247,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "cc" -version = "1.0.29" +version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "rayon 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -918,7 +918,7 @@ dependencies = [ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", - "secp256k1 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", + "secp256k1 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1464,7 +1464,7 @@ name = "libloading" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cc 1.0.29 (registry+https://github.com/rust-lang/crates.io-index)", + "cc 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1474,7 +1474,7 @@ version = "5.17.2" source = "git+https://github.com/rust-rocksdb/rust-rocksdb?rev=1038c71df85a58b7f48894ca4cbf7c9228cb1cdf#1038c71df85a58b7f48894ca4cbf7c9228cb1cdf" dependencies = [ "bindgen 0.47.1 (registry+https://github.com/rust-lang/crates.io-index)", - "cc 1.0.29 (registry+https://github.com/rust-lang/crates.io-index)", + "cc 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1484,7 +1484,7 @@ name = "libsqlite3-sys" version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cc 1.0.29 (registry+https://github.com/rust-lang/crates.io-index)", + "cc 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", "pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "vcpkg 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1653,7 +1653,7 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", - "cc 1.0.29 (registry+https://github.com/rust-lang/crates.io-index)", + "cc 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2124,7 +2124,7 @@ name = "ring" version = "0.14.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cc 1.0.29 (registry+https://github.com/rust-lang/crates.io-index)", + "cc 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", "spin 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2238,11 +2238,10 @@ dependencies = [ [[package]] name = "secp256k1" -version = "0.12.0" +version = "0.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cc 1.0.29 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "cc 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2495,7 +2494,7 @@ dependencies = [ "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "ring 0.14.6 (registry+https://github.com/rust-lang/crates.io-index)", - "secp256k1 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", + "secp256k1 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)", "sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", "twofish 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3045,7 +3044,7 @@ dependencies = [ "checksum byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a019b10a2a7cdeb292db131fc8113e57ea2a908f6e7894b0c3c671893b65dbeb" "checksum bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c" "checksum cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "926013f2860c46252efceabb19f4a6b308197505082c609025aa6706c011d427" -"checksum cc 1.0.29 (registry+https://github.com/rust-lang/crates.io-index)" = "4390a3b5f4f6bce9c1d0c00128379df433e53777fdd30e92f16a529332baec4e" +"checksum cc 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)" = "389803e36973d242e7fecb092b2de44a3d35ac62524b3b9339e51d577d668e02" "checksum cexpr 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "644d693ecfa91955ed32dcc7eda4914e1be97a641fb6f0645a37348e20b230da" "checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" "checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878" @@ -3210,7 +3209,7 @@ dependencies = [ "checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" "checksum scroll 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2f84d114ef17fd144153d608fba7c446b0145d038985e7a8cc5d08bb0ce20383" "checksum scroll_derive 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8f1aa96c45e7f5a91cb7fabe7b279f02fea7126239fc40b732316e8b6a2d0fcb" -"checksum secp256k1 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4070f3906e65249228094cf97b04a90799fba04468190bbbcfa812309cf86e32" +"checksum secp256k1 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bfaccd3a23619349e0878d9a241f34b1982343cdf67367058cd7d078d326b63e" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" "checksum serde 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)" = "9dad3f759919b92c3068c696c15c3d17238234498bbdcc80f2c469606f948ac8" diff --git a/util/crypto/Cargo.toml b/util/crypto/Cargo.toml index 9109598310..19d8586020 100644 --- a/util/crypto/Cargo.toml +++ b/util/crypto/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" [dependencies] lazy_static = "1.3" -secp256k1 = {version = "0.12.0", optional = true } +secp256k1 = {version = "0.12.2", optional = true } failure = "0.1.5" rand = "0.6" numext-fixed-hash = { version = "0.1", features = ["support_rand", "support_heapsize", "support_serde"] } From 75c119b75e95cd7ca6efa3f301cfc7e725d7532e Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" Date: Sun, 17 Mar 2019 21:13:58 +0000 Subject: [PATCH 26/99] chore(deps): bump toml from 0.4.10 to 0.5.0 Bumps [toml](https://github.com/alexcrichton/toml-rs) from 0.4.10 to 0.5.0. - [Release notes](https://github.com/alexcrichton/toml-rs/releases) - [Commits](https://github.com/alexcrichton/toml-rs/compare/0.4.10...0.5.0) Signed-off-by: dependabot[bot] --- Cargo.lock | 13 +++++++++++-- Cargo.toml | 2 +- spec/Cargo.toml | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5a485191d6..55d09be8db 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -309,7 +309,7 @@ dependencies = [ "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)", - "toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", + "toml 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -369,7 +369,7 @@ dependencies = [ "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", - "toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", + "toml 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2805,6 +2805,14 @@ dependencies = [ "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "toml" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "try-lock" version = "0.1.0" @@ -3267,6 +3275,7 @@ dependencies = [ "checksum tokio-uds 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "037ffc3ba0e12a0ab4aca92e5234e0dedeb48fddf6ccd260f1f150a36a9f2445" "checksum tokio-yamux 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "1815b4502bb86c8ffaae8521ecc26e148edb4fcc2ed24c1feba0807a492b4946" "checksum toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "758664fc71a3a69038656bee8b6be6477d2a6c315a6b81f7081f591bffa4111f" +"checksum toml 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "87c5890a989fa47ecdc7bcb4c63a77a82c18f306714104b1decfd722db17b39e" "checksum try-lock 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee2aa4715743892880f70885373966c83d73ef1b0838a664ef0c76fffd35e7c2" "checksum try-lock 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" "checksum twofish 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712d261e83e727c8e2dbb75dacac67c36e35db36a958ee504f2164fc052434e1" diff --git a/Cargo.toml b/Cargo.toml index 01d7759c4b..282a97e547 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ build = "build.rs" clap = { version = "2" } serde = "1.0" serde_derive = "1.0" -toml = "0.4" +toml = "0.5" log = "0.4" crossbeam-channel = "0.3" config-tool = { package= "config", version = "0.9" } diff --git a/spec/Cargo.toml b/spec/Cargo.toml index 2bb8232e3b..1b53b3a993 100644 --- a/spec/Cargo.toml +++ b/spec/Cargo.toml @@ -8,7 +8,7 @@ edition = "2018" [dependencies] serde = "1.0" serde_derive = "1.0" -toml = "0.4" +toml = "0.5" numext-fixed-hash = { version = "0.1", features = ["support_rand", "support_heapsize", "support_serde"] } numext-fixed-uint = { version = "0.1", features = ["support_rand", "support_heapsize", "support_serde"] } ckb-core = { path = "../core" } From b382135226ea180dee0cd8e829abe5bc82d2aa64 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" Date: Mon, 18 Mar 2019 03:56:17 +0000 Subject: [PATCH 27/99] chore(deps): bump tokio from 0.1.16 to 0.1.17 Bumps [tokio](https://github.com/tokio-rs/tokio) from 0.1.16 to 0.1.17. - [Release notes](https://github.com/tokio-rs/tokio/releases) - [Changelog](https://github.com/tokio-rs/tokio/blob/master/CHANGELOG.md) - [Commits](https://github.com/tokio-rs/tokio/compare/tokio-0.1.16...tokio-0.1.17) Signed-off-by: dependabot[bot] --- Cargo.lock | 32 +++++++++++++++++++++----------- network/Cargo.toml | 2 +- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5bb6b45c70..19cc16346b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -486,7 +486,7 @@ dependencies = [ "tentacle 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "tentacle-ping 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "tentacle-secio 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "unsigned-varint 0.2.2 (git+https://github.com/paritytech/unsigned-varint)", ] @@ -1268,7 +1268,7 @@ dependencies = [ "net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-executor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-reactor 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1387,7 +1387,7 @@ dependencies = [ "jsonrpc-server-utils 10.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "unicase 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1402,7 +1402,7 @@ dependencies = [ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "unicase 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2461,7 +2461,7 @@ dependencies = [ "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "parity-multiaddr 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "tentacle-secio 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-threadpool 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-yamux 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2476,7 +2476,7 @@ dependencies = [ "generic-channel 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "tentacle 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2496,7 +2496,7 @@ dependencies = [ "ring 0.14.6 (registry+https://github.com/rust-lang/crates.io-index)", "secp256k1 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)", "sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "twofish 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "unsigned-varint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "untrusted 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2573,7 +2573,7 @@ dependencies = [ [[package]] name = "tokio" -version = "0.1.16" +version = "0.1.17" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2590,6 +2590,7 @@ dependencies = [ "tokio-tcp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-threadpool 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-timer 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-trace-core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-udp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-uds 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2615,7 +2616,7 @@ dependencies = [ "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)", "scoped-tls 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-executor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-reactor 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2752,6 +2753,14 @@ dependencies = [ "tokio-executor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "tokio-trace-core" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tokio-udp" version = "0.1.3" @@ -2793,7 +2802,7 @@ dependencies = [ "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -3256,7 +3265,7 @@ dependencies = [ "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" "checksum tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e9175261fbdb60781fcd388a4d6cc7e14764a2b629a7ad94abb439aed223a44f" "checksum tinytemplate 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7655088894274afb52b807bd3c87072daa1fedd155068b8705cabfd628956115" -"checksum tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "fcaabb3cec70485d0df6e9454fe514393ad1c4070dee8915f11041e95630b230" +"checksum tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)" = "1021bb1f4150435ab8f222eb7ed37c60b2d57037def63ba43085a79f387512d7" "checksum tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5c501eceaf96f0e1793cf26beb63da3d11c738c4a943fdf3746d81d64684c39f" "checksum tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)" = "aeeffbbb94209023feaef3c196a41cbcdafa06b4a6f893f68779bb5e53796f71" "checksum tokio-current-thread 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "c756b04680eea21902a46fca4e9f410a2332c04995af590e07ff262e2193a9a3" @@ -3270,6 +3279,7 @@ dependencies = [ "checksum tokio-tcp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1d14b10654be682ac43efee27401d792507e30fd8d26389e1da3b185de2e4119" "checksum tokio-threadpool 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "742e511f6ce2298aeb86fc9ea0d8df81c2388c6ebae3dc8a7316e8c9df0df801" "checksum tokio-timer 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "2910970404ba6fa78c5539126a9ae2045d62e3713041e447f695f41405a120c6" +"checksum tokio-trace-core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "350c9edade9830dc185ae48ba45667a445ab59f6167ef6d0254ec9d2430d9dd3" "checksum tokio-udp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "66268575b80f4a4a710ef83d087fdfeeabdce9b74c797535fbac18a2cb906e92" "checksum tokio-uds 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "037ffc3ba0e12a0ab4aca92e5234e0dedeb48fddf6ccd260f1f150a36a9f2445" "checksum tokio-yamux 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "1815b4502bb86c8ffaae8521ecc26e148edb4fcc2ed24c1feba0807a492b4946" diff --git a/network/Cargo.toml b/network/Cargo.toml index 96791f152d..fb20c1d48b 100644 --- a/network/Cargo.toml +++ b/network/Cargo.toml @@ -17,7 +17,7 @@ ckb-util = { path = "../util" } unsigned-varint = {git = "https://github.com/paritytech/unsigned-varint", features = ["codec"]} log = "0.4.5" bytes = "0.4.12" -tokio = "0.1.16" +tokio = "0.1.17" futures = { version = "0.1.19", features = ["use_std"] } snap = "0.2" p2p = { version = "0.1", package="tentacle" } From 097132ba578709686d73e06a1bb660b78d3c3820 Mon Sep 17 00:00:00 2001 From: zhangsoledad <787953403@qq.com> Date: Tue, 19 Mar 2019 18:05:39 +0800 Subject: [PATCH 28/99] perf: shrink lock-acquisition --- rpc/src/module/trace.rs | 8 +++++--- sync/src/relayer/mod.rs | 18 ++++++++++-------- sync/src/relayer/transaction_process.rs | 13 +++++++------ sync/src/synchronizer/mod.rs | 15 ++++++++++----- 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/rpc/src/module/trace.rs b/rpc/src/module/trace.rs index fd40318397..1b29b66454 100644 --- a/rpc/src/module/trace.rs +++ b/rpc/src/module/trace.rs @@ -31,9 +31,11 @@ impl TraceRpc for TraceRpcImpl { fn trace_transaction(&self, tx: Transaction) -> Result { let tx: CoreTransaction = tx.into(); let tx_hash = tx.hash().clone(); - let mut chain_state = self.shared.chain_state().lock(); - let tx_pool = chain_state.mut_tx_pool(); - tx_pool.trace_tx(tx.clone()); + { + let mut chain_state = self.shared.chain_state().lock(); + let tx_pool = chain_state.mut_tx_pool(); + tx_pool.trace_tx(tx.clone()); + } let fbb = &mut FlatBufferBuilder::new(); let message = RelayMessage::build_transaction(fbb, &tx); diff --git a/sync/src/relayer/mod.rs b/sync/src/relayer/mod.rs index 076d5a4c52..72e6501399 100644 --- a/sync/src/relayer/mod.rs +++ b/sync/src/relayer/mod.rs @@ -221,16 +221,18 @@ where let mut pending_proposals_request = self.state.pending_proposals_request.lock(); let mut peer_txs = FnvHashMap::default(); let mut remove_ids = Vec::new(); - let chain_state = self.shared.chain_state().lock(); - let tx_pool = chain_state.tx_pool(); - for (id, peers) in pending_proposals_request.iter() { - if let Some(tx) = tx_pool.get_tx(id) { - for peer in peers { - let tx_set = peer_txs.entry(*peer).or_insert_with(Vec::new); - tx_set.push(tx.clone()); + { + let chain_state = self.shared.chain_state().lock(); + let tx_pool = chain_state.tx_pool(); + for (id, peers) in pending_proposals_request.iter() { + if let Some(tx) = tx_pool.get_tx(id) { + for peer in peers { + let tx_set = peer_txs.entry(*peer).or_insert_with(Vec::new); + tx_set.push(tx.clone()); + } } + remove_ids.push(*id); } - remove_ids.push(*id); } for id in remove_ids { diff --git a/sync/src/relayer/transaction_process.rs b/sync/src/relayer/transaction_process.rs index fc2d965959..57d5b62626 100644 --- a/sync/src/relayer/transaction_process.rs +++ b/sync/src/relayer/transaction_process.rs @@ -33,12 +33,13 @@ where pub fn execute(self) { let tx: Transaction = (*self.message).into(); - let chain_state = self.relayer.shared.chain_state().lock(); - let max_block_cycles = self.relayer.shared.consensus().max_block_cycles(); - if chain_state - .add_tx_to_pool(tx.clone(), max_block_cycles) - .is_ok() - { + let ret = { + let chain_state = self.relayer.shared.chain_state().lock(); + let max_block_cycles = self.relayer.shared.consensus().max_block_cycles(); + chain_state.add_tx_to_pool(tx.clone(), max_block_cycles) + }; + + if ret.is_ok() { let fbb = &mut FlatBufferBuilder::new(); let message = RelayMessage::build_transaction(fbb, &tx); fbb.finish(message, None); diff --git a/sync/src/synchronizer/mod.rs b/sync/src/synchronizer/mod.rs index 46e4371dfb..56d99a2d30 100644 --- a/sync/src/synchronizer/mod.rs +++ b/sync/src/synchronizer/mod.rs @@ -549,9 +549,15 @@ impl Synchronizer { if !state.chain_sync.protect && is_outbound { let best_known_header = best_known_headers.get(peer); - let chain_state = self.shared.chain_state().lock(); + let (tip_header, local_total_difficulty) = { + let chain_state = self.shared.chain_state().lock(); + ( + chain_state.tip_header().clone(), + chain_state.total_difficulty().clone(), + ) + }; if best_known_header.map(|h| h.total_difficulty()) - >= Some(chain_state.total_difficulty()) + >= Some(&local_total_difficulty) { if state.chain_sync.timeout != 0 { state.chain_sync.timeout = 0; @@ -569,9 +575,8 @@ impl Synchronizer { // where we checked against our tip. // Either way, set a new timeout based on current tip. state.chain_sync.timeout = now + CHAIN_SYNC_TIMEOUT; - state.chain_sync.work_header = Some(chain_state.tip_header().clone()); - state.chain_sync.total_difficulty = - Some(chain_state.total_difficulty().clone()); + state.chain_sync.work_header = Some(tip_header); + state.chain_sync.total_difficulty = Some(local_total_difficulty); state.chain_sync.sent_getheaders = false; } else if state.chain_sync.timeout > 0 && now > state.chain_sync.timeout { // No evidence yet that our peer has synced to a chain with work equal to that From 4d074e24e26b21ea2cd3a5e32d6a391357a59527 Mon Sep 17 00:00:00 2001 From: jjy Date: Tue, 19 Mar 2019 12:15:12 +0800 Subject: [PATCH 29/99] feat: verify tx cycles in relay protocol --- protocol/src/builder.rs | 23 +++++- protocol/src/convert.rs | 9 +++ protocol/src/protocol.fbs | 7 +- protocol/src/protocol_generated.rs | 100 ++++++++++++++++++++++-- rpc/src/error.rs | 17 ++++ rpc/src/lib.rs | 1 + rpc/src/module/pool.rs | 52 +++++++----- rpc/src/module/trace.rs | 49 ++++++++---- shared/src/chain_state.rs | 31 ++++++-- shared/src/tx_pool/pool.rs | 14 ++-- shared/src/tx_pool/types.rs | 4 +- sync/src/relayer/mod.rs | 11 ++- sync/src/relayer/transaction_process.rs | 75 ++++++++++++------ sync/src/tests/relayer.rs | 17 +++- 14 files changed, 316 insertions(+), 94 deletions(-) create mode 100644 rpc/src/error.rs diff --git a/protocol/src/builder.rs b/protocol/src/builder.rs index 9b2cc00232..9e22015332 100644 --- a/protocol/src/builder.rs +++ b/protocol/src/builder.rs @@ -9,7 +9,8 @@ use crate::protocol_generated::ckb::protocol::{ ProposalShortId as FbsProposalShortId, RelayMessage, RelayMessageBuilder, RelayPayload, Script as FbsScript, ScriptBuilder, SyncMessage, SyncMessageBuilder, SyncPayload, Time as FbsTime, TimeBuilder, TimeMessage, TimeMessageBuilder, Transaction as FbsTransaction, - TransactionBuilder, UncleBlock as FbsUncleBlock, UncleBlockBuilder, H256 as FbsH256, + TransactionBuilder, UncleBlock as FbsUncleBlock, UncleBlockBuilder, + ValidTransaction as FbsValidTransaction, ValidTransactionBuilder, H256 as FbsH256, }; use crate::{short_transaction_id, short_transaction_id_keys}; use ckb_core::block::Block; @@ -17,6 +18,7 @@ use ckb_core::header::{BlockNumber, Header}; use ckb_core::script::Script; use ckb_core::transaction::{CellInput, CellOutput, OutPoint, ProposalShortId, Transaction}; use ckb_core::uncle::UncleBlock; +use ckb_core::Cycle; use ckb_merkle_tree::build_merkle_proof; use flatbuffers::{FlatBufferBuilder, WIPOffset}; use numext_fixed_hash::H256; @@ -114,6 +116,20 @@ impl<'a> FbsOutPoint<'a> { } } +impl<'a> FbsValidTransaction<'a> { + pub fn build<'b>( + fbb: &mut FlatBufferBuilder<'b>, + transaction: &Transaction, + cycles: Cycle, + ) -> WIPOffset> { + let tx = FbsTransaction::build(fbb, transaction); + let mut builder = ValidTransactionBuilder::new(fbb); + builder.add_transaction(tx); + builder.add_cycles(cycles); + builder.finish() + } +} + impl<'a> FbsCellInput<'a> { pub fn build<'b>( fbb: &mut FlatBufferBuilder<'b>, @@ -488,10 +504,11 @@ impl<'a> RelayMessage<'a> { pub fn build_transaction<'b>( fbb: &mut FlatBufferBuilder<'b>, transaction: &Transaction, + cycles: Cycle, ) -> WIPOffset> { - let fbs_transaction = FbsTransaction::build(fbb, transaction); + let fbs_transaction = FbsValidTransaction::build(fbb, transaction, cycles); let mut builder = RelayMessageBuilder::new(fbb); - builder.add_payload_type(RelayPayload::Transaction); + builder.add_payload_type(RelayPayload::ValidTransaction); builder.add_payload(fbs_transaction.as_union_value()); builder.finish() } diff --git a/protocol/src/convert.rs b/protocol/src/convert.rs index 95595f3f7b..764747d95e 100644 --- a/protocol/src/convert.rs +++ b/protocol/src/convert.rs @@ -171,6 +171,15 @@ impl<'a> From> for ckb_core::transaction::Transact } } +impl<'a> From> + for (ckb_core::transaction::Transaction, ckb_core::Cycle) +{ + fn from(tx: ckb_protocol::ValidTransaction<'a>) -> Self { + let cycles = tx.cycles(); + (tx.transaction().unwrap().into(), cycles) + } +} + impl<'a> From> for ckb_core::transaction::OutPoint { fn from(out_point: ckb_protocol::OutPoint<'a>) -> Self { ckb_core::transaction::OutPoint { diff --git a/protocol/src/protocol.fbs b/protocol/src/protocol.fbs index a271154146..621a7a8999 100644 --- a/protocol/src/protocol.fbs +++ b/protocol/src/protocol.fbs @@ -98,7 +98,7 @@ root_type SyncMessage; union RelayPayload { CompactBlock, - Transaction, + ValidTransaction, GetBlockTransactions, BlockTransactions, GetBlockProposal, @@ -123,6 +123,11 @@ table IndexTransaction { transaction: Transaction; } +table ValidTransaction { + cycles: uint64; + transaction: Transaction; +} + table GetBlockTransactions { hash: H256; indexes: [uint32]; diff --git a/protocol/src/protocol_generated.rs b/protocol/src/protocol_generated.rs index 55f4a8891a..76c0df8807 100644 --- a/protocol/src/protocol_generated.rs +++ b/protocol/src/protocol_generated.rs @@ -112,7 +112,7 @@ pub struct SyncPayloadUnionTableOffset {} pub enum RelayPayload { NONE = 0, CompactBlock = 1, - Transaction = 2, + ValidTransaction = 2, GetBlockTransactions = 3, BlockTransactions = 4, GetBlockProposal = 5, @@ -158,7 +158,7 @@ impl flatbuffers::Push for RelayPayload { const ENUM_VALUES_RELAY_PAYLOAD:[RelayPayload; 7] = [ RelayPayload::NONE, RelayPayload::CompactBlock, - RelayPayload::Transaction, + RelayPayload::ValidTransaction, RelayPayload::GetBlockTransactions, RelayPayload::BlockTransactions, RelayPayload::GetBlockProposal, @@ -169,7 +169,7 @@ const ENUM_VALUES_RELAY_PAYLOAD:[RelayPayload; 7] = [ const ENUM_NAMES_RELAY_PAYLOAD:[&'static str; 7] = [ "NONE", "CompactBlock", - "Transaction", + "ValidTransaction", "GetBlockTransactions", "BlockTransactions", "GetBlockProposal", @@ -2003,9 +2003,9 @@ impl<'a> RelayMessage<'a> { #[inline] #[allow(non_snake_case)] - pub fn payload_as_transaction(&'a self) -> Option { - if self.payload_type() == RelayPayload::Transaction { - self.payload().map(|u| Transaction::init_from_table(u)) + pub fn payload_as_valid_transaction(&'a self) -> Option { + if self.payload_type() == RelayPayload::ValidTransaction { + self.payload().map(|u| ValidTransaction::init_from_table(u)) } else { None } @@ -2318,6 +2318,94 @@ impl<'a: 'b, 'b> IndexTransactionBuilder<'a, 'b> { } } +pub enum ValidTransactionOffset {} +#[derive(Copy, Clone, Debug, PartialEq)] + +pub struct ValidTransaction<'a> { + pub _tab: flatbuffers::Table<'a>, +} + +impl<'a> flatbuffers::Follow<'a> for ValidTransaction<'a> { + type Inner = ValidTransaction<'a>; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + Self { + _tab: flatbuffers::Table { buf: buf, loc: loc }, + } + } +} + +impl<'a> ValidTransaction<'a> { + #[inline] + pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { + ValidTransaction { + _tab: table, + } + } + #[allow(unused_mut)] + pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( + _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, + args: &'args ValidTransactionArgs<'args>) -> flatbuffers::WIPOffset> { + let mut builder = ValidTransactionBuilder::new(_fbb); + builder.add_cycles(args.cycles); + if let Some(x) = args.transaction { builder.add_transaction(x); } + builder.finish() + } + + pub const VT_CYCLES: flatbuffers::VOffsetT = 4; + pub const VT_TRANSACTION: flatbuffers::VOffsetT = 6; + + #[inline] + pub fn cycles(&self) -> u64 { + self._tab.get::(ValidTransaction::VT_CYCLES, Some(0)).unwrap() + } + #[inline] + pub fn transaction(&self) -> Option> { + self._tab.get::>>(ValidTransaction::VT_TRANSACTION, None) + } +} + +pub struct ValidTransactionArgs<'a> { + pub cycles: u64, + pub transaction: Option>>, +} +impl<'a> Default for ValidTransactionArgs<'a> { + #[inline] + fn default() -> Self { + ValidTransactionArgs { + cycles: 0, + transaction: None, + } + } +} +pub struct ValidTransactionBuilder<'a: 'b, 'b> { + fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, + start_: flatbuffers::WIPOffset, +} +impl<'a: 'b, 'b> ValidTransactionBuilder<'a, 'b> { + #[inline] + pub fn add_cycles(&mut self, cycles: u64) { + self.fbb_.push_slot::(ValidTransaction::VT_CYCLES, cycles, 0); + } + #[inline] + pub fn add_transaction(&mut self, transaction: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(ValidTransaction::VT_TRANSACTION, transaction); + } + #[inline] + pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> ValidTransactionBuilder<'a, 'b> { + let start = _fbb.start_table(); + ValidTransactionBuilder { + fbb_: _fbb, + start_: start, + } + } + #[inline] + pub fn finish(self) -> flatbuffers::WIPOffset> { + let o = self.fbb_.end_table(self.start_); + flatbuffers::WIPOffset::new(o.value()) + } +} + pub enum GetBlockTransactionsOffset {} #[derive(Copy, Clone, Debug, PartialEq)] diff --git a/rpc/src/error.rs b/rpc/src/error.rs new file mode 100644 index 0000000000..b1611f01e4 --- /dev/null +++ b/rpc/src/error.rs @@ -0,0 +1,17 @@ +use jsonrpc_core::{Error, ErrorCode}; + +#[derive(Debug, PartialEq, Clone, Copy)] +pub enum RPCError { + Staging = -2, + Invalid = -3, +} + +impl RPCError { + pub fn custom(err: RPCError, message: String) -> Error { + Error { + code: ErrorCode::ServerError(err as i64), + message, + data: None, + } + } +} diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index c963ba9ff5..714f74959c 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -1,4 +1,5 @@ mod config; +mod error; mod module; mod server; diff --git a/rpc/src/module/pool.rs b/rpc/src/module/pool.rs index 62dbacdb0a..5924a86429 100644 --- a/rpc/src/module/pool.rs +++ b/rpc/src/module/pool.rs @@ -1,9 +1,13 @@ +use crate::error::RPCError; use ckb_core::transaction::{ProposalShortId, Transaction as CoreTransaction}; use ckb_network::{NetworkService, ProtocolId}; use ckb_protocol::RelayMessage; use ckb_shared::index::ChainIndex; use ckb_shared::shared::Shared; +use ckb_shared::tx_pool::types::PoolEntry; use ckb_sync::NetworkProtocol; +use ckb_traits::chain_provider::ChainProvider; +use ckb_verification::TransactionError; use flatbuffers::FlatBufferBuilder; use jsonrpc_core::Result; use jsonrpc_derive::rpc; @@ -32,25 +36,37 @@ impl PoolRpc for PoolRpcImpl { fn send_transaction(&self, tx: Transaction) -> Result { let tx: CoreTransaction = tx.into(); let tx_hash = tx.hash().clone(); - { - let mut chain_state = self.shared.chain_state().lock(); - let tx_pool = chain_state.mut_tx_pool(); - let pool_result = tx_pool.enqueue_tx(tx.clone()); - debug!(target: "rpc", "send_transaction add to pool result: {:?}", pool_result); - } - - let fbb = &mut FlatBufferBuilder::new(); - let message = RelayMessage::build_transaction(fbb, &tx); - fbb.finish(message, None); + let mut chain_state = self.shared.chain_state().lock(); + let rtx = chain_state.resolve_tx_from_pool(&tx, &chain_state.tx_pool()); + let tx_result = chain_state.verify_rtx(&rtx, self.shared.consensus().max_block_cycles()); + debug!(target: "rpc", "send_transaction add to pool result: {:?}", tx_result); + match tx_result { + Ok(cycles) => { + let entry = PoolEntry::new(tx.clone(), 0, Some(cycles)); + chain_state.mut_tx_pool().enqueue_tx(entry); + let fbb = &mut FlatBufferBuilder::new(); + let message = RelayMessage::build_transaction(fbb, &tx, cycles); + fbb.finish(message, None); - self.network - .with_protocol_context(NetworkProtocol::RELAY as ProtocolId, |nc| { - for peer in nc.connected_peers() { - debug!(target: "rpc", "relay transaction {} to peer#{}", tx_hash, peer); - let _ = nc.send(peer, fbb.finished_data().to_vec()); - } - }); - Ok(tx_hash) + self.network + .with_protocol_context(NetworkProtocol::RELAY as ProtocolId, |nc| { + for peer in nc.connected_peers() { + debug!(target: "rpc", "relay transaction {} to peer#{}", tx_hash, peer); + let _ = nc.send(peer, fbb.finished_data().to_vec()); + } + }); + Ok(tx_hash) + } + Err(TransactionError::UnknownInput) => { + let entry = PoolEntry::new(tx, 0, None); + chain_state.mut_tx_pool().enqueue_tx(entry); + Err(RPCError::custom( + RPCError::Staging, + "tx missing inputs".to_string(), + )) + } + Err(err) => Err(RPCError::custom(RPCError::Invalid, format!("{:?}", err))), + } } fn get_pool_transaction(&self, hash: H256) -> Result> { diff --git a/rpc/src/module/trace.rs b/rpc/src/module/trace.rs index 1b29b66454..70c8ac215e 100644 --- a/rpc/src/module/trace.rs +++ b/rpc/src/module/trace.rs @@ -1,10 +1,14 @@ +use crate::error::RPCError; use ckb_core::transaction::Transaction as CoreTransaction; use ckb_network::{NetworkService, ProtocolId}; use ckb_protocol::RelayMessage; use ckb_shared::index::ChainIndex; use ckb_shared::shared::Shared; +use ckb_shared::tx_pool::types::PoolEntry; use ckb_shared::tx_pool::TxTrace; use ckb_sync::NetworkProtocol; +use ckb_traits::chain_provider::ChainProvider; +use ckb_verification::TransactionError; use flatbuffers::FlatBufferBuilder; use jsonrpc_core::Result; use jsonrpc_derive::rpc; @@ -31,24 +35,37 @@ impl TraceRpc for TraceRpcImpl { fn trace_transaction(&self, tx: Transaction) -> Result { let tx: CoreTransaction = tx.into(); let tx_hash = tx.hash().clone(); - { - let mut chain_state = self.shared.chain_state().lock(); - let tx_pool = chain_state.mut_tx_pool(); - tx_pool.trace_tx(tx.clone()); - } + let mut chain_state = self.shared.chain_state().lock(); + let rtx = chain_state.resolve_tx_from_pool(&tx, &chain_state.tx_pool()); + let tx_result = chain_state.verify_rtx(&rtx, self.shared.consensus().max_block_cycles()); - let fbb = &mut FlatBufferBuilder::new(); - let message = RelayMessage::build_transaction(fbb, &tx); - fbb.finish(message, None); + match tx_result { + Ok(cycles) => { + let entry = PoolEntry::new(tx.clone(), 0, Some(cycles)); + chain_state.mut_tx_pool().enqueue_tx(entry); + let fbb = &mut FlatBufferBuilder::new(); + let message = RelayMessage::build_transaction(fbb, &tx, cycles); + fbb.finish(message, None); - self.network - .with_protocol_context(NetworkProtocol::RELAY as ProtocolId, |nc| { - for peer in nc.connected_peers() { - debug!(target: "rpc", "relay transaction {} to peer#{}", tx_hash, peer); - let _ = nc.send(peer, fbb.finished_data().to_vec()); - } - }); - Ok(tx_hash) + self.network + .with_protocol_context(NetworkProtocol::RELAY as ProtocolId, |nc| { + for peer in nc.connected_peers() { + debug!(target: "rpc", "relay transaction {} to peer#{}", tx_hash, peer); + let _ = nc.send(peer, fbb.finished_data().to_vec()); + } + }); + Ok(tx_hash) + } + Err(TransactionError::UnknownInput) => { + let entry = PoolEntry::new(tx, 0, None); + chain_state.mut_tx_pool().enqueue_tx(entry); + Err(RPCError::custom( + RPCError::Staging, + "tx missing inputs".to_string(), + )) + } + Err(err) => Err(RPCError::custom(RPCError::Invalid, format!("{:?}", err))), + } } fn get_transaction_trace(&self, hash: H256) -> Result>> { diff --git a/shared/src/chain_state.rs b/shared/src/chain_state.rs index 07243efacc..b351ea94bd 100644 --- a/shared/src/chain_state.rs +++ b/shared/src/chain_state.rs @@ -97,16 +97,31 @@ impl ChainState { self.cell_set.update(txo_diff); } - pub fn add_tx_to_pool(&self, tx: Transaction, max_cycles: Cycle) -> Result<(), PoolError> { + pub fn add_tx_to_pool(&self, tx: Transaction, max_cycles: Cycle) -> Result { let mut tx_pool = self.tx_pool.borrow_mut(); let short_id = tx.proposal_short_id(); + let rtx = self.resolve_tx_from_pool(&tx, &tx_pool); + let verify_result = self.verify_rtx(&rtx, max_cycles); if self.contains_proposal_id(&short_id) { - let entry = PoolEntry::new(tx, 0, None); + let entry = PoolEntry::new(tx, 0, verify_result.map(Some).unwrap_or(None)); self.staging_tx(&mut tx_pool, entry, max_cycles)?; + Ok(verify_result.map_err(PoolError::InvalidTx)?) } else { - tx_pool.enqueue_tx(tx); + match verify_result { + Ok(cycles) => { + // enqueue tx with cycles + let entry = PoolEntry::new(tx, 0, Some(cycles)); + tx_pool.enqueue_tx(entry); + Ok(cycles) + } + Err(TransactionError::UnknownInput) => { + let entry = PoolEntry::new(tx, 0, None); + tx_pool.enqueue_tx(entry); + Err(PoolError::InvalidTx(TransactionError::UnknownInput)) + } + Err(err) => Err(PoolError::InvalidTx(err)), + } } - Ok(()) } fn get_cell_status_from_store(&self, out_point: &OutPoint) -> CellStatus { @@ -163,7 +178,7 @@ impl ChainState { } } - fn verify_rtx( + pub fn verify_rtx( &self, rtx: &ResolvedTransaction, max_cycles: Cycle, @@ -267,9 +282,9 @@ impl ChainState { tx_pool.add_orphan(entry, unknowns); return Ok(StagingTxResult::Orphan); } - + let cycles = entry.cycles.expect("cycles must exists"); tx_pool.add_staging(entry); - Ok(StagingTxResult::Normal) + Ok(StagingTxResult::Normal(cycles)) } pub fn update_tx_pool_for_reorg( @@ -316,7 +331,7 @@ impl ChainState { let tx = entry.transaction.clone(); let tx_hash = tx.hash(); match self.staging_tx(&mut tx_pool, entry, max_cycles) { - Ok(StagingTxResult::Normal) => { + Ok(StagingTxResult::Normal(_)) => { self.update_orphan_from_tx(&mut tx_pool, &tx, max_cycles); } Err(e) => { diff --git a/shared/src/tx_pool/pool.rs b/shared/src/tx_pool/pool.rs index b63266bfe4..c948b253f0 100644 --- a/shared/src/tx_pool/pool.rs +++ b/shared/src/tx_pool/pool.rs @@ -62,27 +62,25 @@ impl TxPool { } // enqueue_tx inserts a new transaction into the non-verifiable transaction queue. - pub fn enqueue_tx(&mut self, tx: Transaction) -> bool { - let tx_hash = tx.hash(); + pub fn enqueue_tx(&mut self, entry: PoolEntry) -> bool { + let tx_hash = entry.transaction.hash(); if !self.filter.insert(tx_hash.clone()) { trace!(target: "tx_pool", "discarding already known transaction {:#x}", tx_hash); return false; } - let short_id = tx.proposal_short_id(); - let entry = PoolEntry::new(tx, 0, None); + let short_id = entry.transaction.proposal_short_id(); self.pending.insert(short_id, entry).is_none() } // trace_tx basically same as enqueue_tx, but additional register a trace. - pub fn trace_tx(&mut self, tx: Transaction) -> bool { - let tx_hash = tx.hash(); + pub fn trace_tx(&mut self, entry: PoolEntry) -> bool { + let tx_hash = entry.transaction.hash(); if !self.filter.insert(tx_hash.clone()) { trace!(target: "tx_pool", "discarding already known transaction {:#x}", tx_hash); return false; } - let short_id = tx.proposal_short_id(); - let entry = PoolEntry::new(tx, 0, None); + let short_id = entry.transaction.proposal_short_id(); if self.config.trace_enable() { self.trace.add_pending( diff --git a/shared/src/tx_pool/types.rs b/shared/src/tx_pool/types.rs index 83866c07f9..8b21872958 100644 --- a/shared/src/tx_pool/types.rs +++ b/shared/src/tx_pool/types.rs @@ -53,7 +53,7 @@ impl TxPoolConfig { #[derive(Debug, PartialEq, Clone, Eq)] pub enum StagingTxResult { - Normal, + Normal(Cycle), Orphan, Proposed, Unknown, @@ -61,7 +61,7 @@ pub enum StagingTxResult { // TODO document this enum more accurately /// Enum of errors -#[derive(Debug, Clone, Fail)] +#[derive(Debug, Clone, PartialEq, Fail)] pub enum PoolError { /// An invalid pool entry caused by underlying tx validation error InvalidTx(TransactionError), diff --git a/sync/src/relayer/mod.rs b/sync/src/relayer/mod.rs index 72e6501399..115469220f 100644 --- a/sync/src/relayer/mod.rs +++ b/sync/src/relayer/mod.rs @@ -68,10 +68,13 @@ where nc, ) .execute(), - RelayPayload::Transaction => { - TransactionProcess::new(&message.payload_as_transaction().unwrap(), self, peer, nc) - .execute() - } + RelayPayload::ValidTransaction => TransactionProcess::new( + &message.payload_as_valid_transaction().unwrap(), + self, + peer, + nc, + ) + .execute(), RelayPayload::GetBlockTransactions => GetBlockTransactionsProcess::new( &message.payload_as_get_block_transactions().unwrap(), self, diff --git a/sync/src/relayer/transaction_process.rs b/sync/src/relayer/transaction_process.rs index 57d5b62626..f00544b3aa 100644 --- a/sync/src/relayer/transaction_process.rs +++ b/sync/src/relayer/transaction_process.rs @@ -1,13 +1,19 @@ use crate::relayer::Relayer; -use ckb_core::transaction::Transaction; +use ckb_core::{transaction::Transaction, Cycle}; use ckb_network::{CKBProtocolContext, PeerIndex}; -use ckb_protocol::{RelayMessage, Transaction as FbsTransaction}; +use ckb_protocol::{RelayMessage, ValidTransaction as FbsValidTransaction}; use ckb_shared::index::ChainIndex; +use ckb_shared::tx_pool::types::PoolError; use ckb_traits::chain_provider::ChainProvider; +use ckb_verification::TransactionError; use flatbuffers::FlatBufferBuilder; +use log::debug; +use std::time::Duration; + +const DEFAULT_BAN_TIME: Duration = Duration::from_secs(3600 * 24 * 3); pub struct TransactionProcess<'a, CI: ChainIndex + 'a> { - message: &'a FbsTransaction<'a>, + message: &'a FbsValidTransaction<'a>, relayer: &'a Relayer, peer: PeerIndex, nc: &'a CKBProtocolContext, @@ -18,7 +24,7 @@ where CI: ChainIndex + 'static, { pub fn new( - message: &'a FbsTransaction, + message: &'a FbsValidTransaction, relayer: &'a Relayer, peer: PeerIndex, nc: &'a CKBProtocolContext, @@ -32,31 +38,48 @@ where } pub fn execute(self) { - let tx: Transaction = (*self.message).into(); - let ret = { - let chain_state = self.relayer.shared.chain_state().lock(); - let max_block_cycles = self.relayer.shared.consensus().max_block_cycles(); - chain_state.add_tx_to_pool(tx.clone(), max_block_cycles) - }; - - if ret.is_ok() { - let fbb = &mut FlatBufferBuilder::new(); - let message = RelayMessage::build_transaction(fbb, &tx); - fbb.finish(message, None); + let (tx, relay_cycles): (Transaction, Cycle) = (*self.message).into(); + let chain_state = self.relayer.shared.chain_state().lock(); + let max_block_cycles = self.relayer.shared.consensus().max_block_cycles(); + let tx_result = chain_state.add_tx_to_pool(tx.clone(), max_block_cycles); + // disconnect peer if cycles mismatch + match tx_result { + Ok(cycles) if cycles == relay_cycles => { + // broadcast tx + let fbb = &mut FlatBufferBuilder::new(); + let message = RelayMessage::build_transaction(fbb, &tx, cycles); + fbb.finish(message, None); - for peer_id in self.nc.connected_peers() { - if peer_id != self.peer - && self - .relayer - .peers() - .transaction_filters - .read() - .get(&peer_id) - .map_or(true, |filter| filter.contains(&tx)) - { - let _ = self.nc.send(peer_id, fbb.finished_data().to_vec()); + for peer in self.nc.connected_peers() { + if peer != self.peer + && self + .relayer + .peers() + .transaction_filters + .read() + .get(&peer) + .map_or(true, |filter| filter.contains(&tx)) + { + let _ = self.nc.send(peer, fbb.finished_data().to_vec()); + } } } + Err(PoolError::InvalidTx(TransactionError::UnknownInput)) + | Err(PoolError::InvalidTx(TransactionError::Conflict)) => { + // this error may occured when peer's tip is different with us, + // we can't proof peer is bad so just ignore this + debug!(target: "relay", "peer {} relay a conflict or missing input tx: {:?}", self.peer, tx); + } + Ok(cycles) => { + debug!(target: "relay", "peer {} relay wrong cycles tx: {:?} real cycles {} wrong cycles {}", self.peer, tx, cycles, relay_cycles); + // TODO use report score interface + self.nc.ban_peer(self.peer, DEFAULT_BAN_TIME); + } + Err(err) => { + debug!(target: "relay", "peer {} relay a invalid tx: {:?}, error: {:?}", self.peer, tx, err); + // TODO use report score interface + self.nc.ban_peer(self.peer, DEFAULT_BAN_TIME); + } } } } diff --git a/sync/src/tests/relayer.rs b/sync/src/tests/relayer.rs index b1105cfefe..c34a1c6b0b 100644 --- a/sync/src/tests/relayer.rs +++ b/sync/src/tests/relayer.rs @@ -62,8 +62,13 @@ fn relay_compact_block_with_one_tx() { .build(); { + let chain_state = shared1.chain_state().lock(); + let rtx = chain_state.resolve_tx_from_pool(&tx, &chain_state.tx_pool()); + let cycles = chain_state + .verify_rtx(&rtx, shared1.consensus().max_block_cycles()) + .expect("verify relay tx"); let fbb = &mut FlatBufferBuilder::new(); - let message = RelayMessage::build_transaction(fbb, &tx); + let message = RelayMessage::build_transaction(fbb, &tx, cycles); fbb.finish(message, None); node1.broadcast( NetworkProtocol::RELAY as ProtocolId, @@ -212,8 +217,16 @@ fn relay_compact_block_with_missing_indexs() { .collect::>(); [3, 5].iter().for_each(|i| { + let tx = &txs[*i]; + let cycles = { + let chain_state = shared1.chain_state().lock(); + let rtx = chain_state.resolve_tx_from_pool(tx, &chain_state.tx_pool()); + chain_state + .verify_rtx(&rtx, shared1.consensus().max_block_cycles()) + .expect("verify relay tx") + }; let fbb = &mut FlatBufferBuilder::new(); - let message = RelayMessage::build_transaction(fbb, &txs[*i]); + let message = RelayMessage::build_transaction(fbb, tx, cycles); fbb.finish(message, None); node1.broadcast( NetworkProtocol::RELAY as ProtocolId, From 72ebd2c6f91f7f9017b7d0f375966306ab9c35e7 Mon Sep 17 00:00:00 2001 From: jjy Date: Tue, 19 Mar 2019 13:52:43 +0800 Subject: [PATCH 30/99] refactor: release chain lock in send tx RPC --- rpc/src/module/pool.rs | 20 +++++++++++++++----- rpc/src/module/trace.rs | 20 +++++++++++++++----- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/rpc/src/module/pool.rs b/rpc/src/module/pool.rs index 5924a86429..f2d2f39efc 100644 --- a/rpc/src/module/pool.rs +++ b/rpc/src/module/pool.rs @@ -36,14 +36,20 @@ impl PoolRpc for PoolRpcImpl { fn send_transaction(&self, tx: Transaction) -> Result { let tx: CoreTransaction = tx.into(); let tx_hash = tx.hash().clone(); - let mut chain_state = self.shared.chain_state().lock(); - let rtx = chain_state.resolve_tx_from_pool(&tx, &chain_state.tx_pool()); - let tx_result = chain_state.verify_rtx(&rtx, self.shared.consensus().max_block_cycles()); + let tx_result = { + let chain_state = self.shared.chain_state().lock(); + let rtx = chain_state.resolve_tx_from_pool(&tx, &chain_state.tx_pool()); + chain_state.verify_rtx(&rtx, self.shared.consensus().max_block_cycles()) + }; debug!(target: "rpc", "send_transaction add to pool result: {:?}", tx_result); match tx_result { Ok(cycles) => { let entry = PoolEntry::new(tx.clone(), 0, Some(cycles)); - chain_state.mut_tx_pool().enqueue_tx(entry); + self.shared + .chain_state() + .lock() + .mut_tx_pool() + .enqueue_tx(entry); let fbb = &mut FlatBufferBuilder::new(); let message = RelayMessage::build_transaction(fbb, &tx, cycles); fbb.finish(message, None); @@ -59,7 +65,11 @@ impl PoolRpc for PoolRpcImpl { } Err(TransactionError::UnknownInput) => { let entry = PoolEntry::new(tx, 0, None); - chain_state.mut_tx_pool().enqueue_tx(entry); + self.shared + .chain_state() + .lock() + .mut_tx_pool() + .enqueue_tx(entry); Err(RPCError::custom( RPCError::Staging, "tx missing inputs".to_string(), diff --git a/rpc/src/module/trace.rs b/rpc/src/module/trace.rs index 70c8ac215e..ff301c6dac 100644 --- a/rpc/src/module/trace.rs +++ b/rpc/src/module/trace.rs @@ -35,14 +35,20 @@ impl TraceRpc for TraceRpcImpl { fn trace_transaction(&self, tx: Transaction) -> Result { let tx: CoreTransaction = tx.into(); let tx_hash = tx.hash().clone(); - let mut chain_state = self.shared.chain_state().lock(); - let rtx = chain_state.resolve_tx_from_pool(&tx, &chain_state.tx_pool()); - let tx_result = chain_state.verify_rtx(&rtx, self.shared.consensus().max_block_cycles()); + let tx_result = { + let chain_state = self.shared.chain_state().lock(); + let rtx = chain_state.resolve_tx_from_pool(&tx, &chain_state.tx_pool()); + chain_state.verify_rtx(&rtx, self.shared.consensus().max_block_cycles()) + }; match tx_result { Ok(cycles) => { let entry = PoolEntry::new(tx.clone(), 0, Some(cycles)); - chain_state.mut_tx_pool().enqueue_tx(entry); + self.shared + .chain_state() + .lock() + .mut_tx_pool() + .enqueue_tx(entry); let fbb = &mut FlatBufferBuilder::new(); let message = RelayMessage::build_transaction(fbb, &tx, cycles); fbb.finish(message, None); @@ -58,7 +64,11 @@ impl TraceRpc for TraceRpcImpl { } Err(TransactionError::UnknownInput) => { let entry = PoolEntry::new(tx, 0, None); - chain_state.mut_tx_pool().enqueue_tx(entry); + self.shared + .chain_state() + .lock() + .mut_tx_pool() + .enqueue_tx(entry); Err(RPCError::custom( RPCError::Staging, "tx missing inputs".to_string(), From 3d2dc0992376d5b16e5a31e897195eea467a32b0 Mon Sep 17 00:00:00 2001 From: jjy Date: Tue, 19 Mar 2019 21:54:51 +0800 Subject: [PATCH 31/99] refactor: remove unnecessary lock acquisition --- rpc/src/module/pool.rs | 44 ++++++++++++++++++----------------------- rpc/src/module/trace.rs | 42 +++++++++++++++++---------------------- 2 files changed, 37 insertions(+), 49 deletions(-) diff --git a/rpc/src/module/pool.rs b/rpc/src/module/pool.rs index f2d2f39efc..22996e0a56 100644 --- a/rpc/src/module/pool.rs +++ b/rpc/src/module/pool.rs @@ -36,20 +36,23 @@ impl PoolRpc for PoolRpcImpl { fn send_transaction(&self, tx: Transaction) -> Result { let tx: CoreTransaction = tx.into(); let tx_hash = tx.hash().clone(); - let tx_result = { - let chain_state = self.shared.chain_state().lock(); + let cycles = { + let mut chain_state = self.shared.chain_state().lock(); let rtx = chain_state.resolve_tx_from_pool(&tx, &chain_state.tx_pool()); - chain_state.verify_rtx(&rtx, self.shared.consensus().max_block_cycles()) + let tx_result = + chain_state.verify_rtx(&rtx, self.shared.consensus().max_block_cycles()); + debug!(target: "rpc", "send_transaction add to pool result: {:?}", tx_result); + let cycles = match tx_result { + Err(TransactionError::UnknownInput) => None, + Err(err) => return Err(RPCError::custom(RPCError::Invalid, format!("{:?}", err))), + Ok(cycles) => Some(cycles), + }; + let entry = PoolEntry::new(tx.clone(), 0, cycles); + chain_state.mut_tx_pool().enqueue_tx(entry); + cycles }; - debug!(target: "rpc", "send_transaction add to pool result: {:?}", tx_result); - match tx_result { - Ok(cycles) => { - let entry = PoolEntry::new(tx.clone(), 0, Some(cycles)); - self.shared - .chain_state() - .lock() - .mut_tx_pool() - .enqueue_tx(entry); + match cycles { + Some(cycles) => { let fbb = &mut FlatBufferBuilder::new(); let message = RelayMessage::build_transaction(fbb, &tx, cycles); fbb.finish(message, None); @@ -63,19 +66,10 @@ impl PoolRpc for PoolRpcImpl { }); Ok(tx_hash) } - Err(TransactionError::UnknownInput) => { - let entry = PoolEntry::new(tx, 0, None); - self.shared - .chain_state() - .lock() - .mut_tx_pool() - .enqueue_tx(entry); - Err(RPCError::custom( - RPCError::Staging, - "tx missing inputs".to_string(), - )) - } - Err(err) => Err(RPCError::custom(RPCError::Invalid, format!("{:?}", err))), + None => Err(RPCError::custom( + RPCError::Staging, + "tx missing inputs".to_string(), + )), } } diff --git a/rpc/src/module/trace.rs b/rpc/src/module/trace.rs index ff301c6dac..0268d1b91d 100644 --- a/rpc/src/module/trace.rs +++ b/rpc/src/module/trace.rs @@ -35,20 +35,23 @@ impl TraceRpc for TraceRpcImpl { fn trace_transaction(&self, tx: Transaction) -> Result { let tx: CoreTransaction = tx.into(); let tx_hash = tx.hash().clone(); - let tx_result = { - let chain_state = self.shared.chain_state().lock(); + let cycles = { + let mut chain_state = self.shared.chain_state().lock(); let rtx = chain_state.resolve_tx_from_pool(&tx, &chain_state.tx_pool()); - chain_state.verify_rtx(&rtx, self.shared.consensus().max_block_cycles()) + let cycles = match chain_state + .verify_rtx(&rtx, self.shared.consensus().max_block_cycles()) + { + Err(TransactionError::UnknownInput) => None, + Err(err) => return Err(RPCError::custom(RPCError::Invalid, format!("{:?}", err))), + Ok(cycles) => Some(cycles), + }; + let entry = PoolEntry::new(tx.clone(), 0, cycles); + chain_state.mut_tx_pool().enqueue_tx(entry); + cycles }; - match tx_result { - Ok(cycles) => { - let entry = PoolEntry::new(tx.clone(), 0, Some(cycles)); - self.shared - .chain_state() - .lock() - .mut_tx_pool() - .enqueue_tx(entry); + match cycles { + Some(cycles) => { let fbb = &mut FlatBufferBuilder::new(); let message = RelayMessage::build_transaction(fbb, &tx, cycles); fbb.finish(message, None); @@ -62,19 +65,10 @@ impl TraceRpc for TraceRpcImpl { }); Ok(tx_hash) } - Err(TransactionError::UnknownInput) => { - let entry = PoolEntry::new(tx, 0, None); - self.shared - .chain_state() - .lock() - .mut_tx_pool() - .enqueue_tx(entry); - Err(RPCError::custom( - RPCError::Staging, - "tx missing inputs".to_string(), - )) - } - Err(err) => Err(RPCError::custom(RPCError::Invalid, format!("{:?}", err))), + None => Err(RPCError::custom( + RPCError::Staging, + "tx missing inputs".to_string(), + )), } } From 3aa100a796d11e2f3396cc829f8a8ad47f40dce0 Mon Sep 17 00:00:00 2001 From: jjy Date: Wed, 20 Mar 2019 15:21:01 +0800 Subject: [PATCH 32/99] fix: integration test generate invalid tx --- test/fixtures/nodes_template/default.toml | 2 +- test/src/specs/transaction_relay.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/test/fixtures/nodes_template/default.toml b/test/fixtures/nodes_template/default.toml index 68ba5b876c..10cfb71fdc 100644 --- a/test/fixtures/nodes_template/default.toml +++ b/test/fixtures/nodes_template/default.toml @@ -42,5 +42,5 @@ trace = 100 txs_verify_cache_size = 100000 [block_assembler] -# value is from spec#always_success_type_hash test +# type hash for `always_success` script type_hash = "0x8954a4ac5e5c33eb7aa8bb91e0a000179708157729859bd8cf7e2278e1e12980" diff --git a/test/src/specs/transaction_relay.rs b/test/src/specs/transaction_relay.rs index 73b02410df..ccde57146f 100644 --- a/test/src/specs/transaction_relay.rs +++ b/test/src/specs/transaction_relay.rs @@ -11,6 +11,7 @@ impl Spec for TransactionRelayBasic { let node2 = &net.nodes[2]; info!("Generate new transaction on node1"); + node1.generate_block(); let hash = node1.generate_transaction(); info!("Waiting for relay"); From 0f2dfd39c8dbb777d709509debe6e45981a41bec Mon Sep 17 00:00:00 2001 From: jjy Date: Wed, 20 Mar 2019 16:12:58 +0800 Subject: [PATCH 33/99] feat: Add random_peers function to PeerStore --- network/src/peer_store.rs | 1 + network/src/peer_store/db.rs | 21 +++++++++++++++++++++ network/src/peer_store/sqlite_peer_store.rs | 7 +++++++ network/src/tests/sqlite_peer_store.rs | 17 +++++++++++++++++ 4 files changed, 46 insertions(+) diff --git a/network/src/peer_store.rs b/network/src/peer_store.rs index 6b84dd7bd4..6b3c0bd268 100644 --- a/network/src/peer_store.rs +++ b/network/src/peer_store.rs @@ -125,6 +125,7 @@ pub trait PeerStore: Send + Sync { fn bootnodes(&self, count: u32) -> Vec<(PeerId, Multiaddr)>; fn peer_addrs(&self, peer_id: &PeerId, count: u32) -> Option>; fn peers_to_attempt(&self, count: u32) -> Vec<(PeerId, Multiaddr)>; + fn random_peers(&self, count: u32) -> Vec<(PeerId, Multiaddr)>; fn ban_peer(&mut self, peer_id: &PeerId, timeout: Duration); fn is_banned(&self, peer_id: &PeerId) -> bool; fn scoring_schema(&self) -> &ScoringSchema; diff --git a/network/src/peer_store/db.rs b/network/src/peer_store/db.rs index 37aed08d45..b2284bf628 100644 --- a/network/src/peer_store/db.rs +++ b/network/src/peer_store/db.rs @@ -186,6 +186,27 @@ impl PeerAddr { } } +pub fn get_random_peers(conn: &Connection, count: u32) -> DBResult> { + // random select peers + let mut stmt = conn.prepare("SELECT id, peer_id FROM peer_info WHERE ban_time < strftime('%s','now') ORDER BY RANDOM() LIMIT :count")?; + let rows = stmt.query_map_named(&[(":count", &count)], |row| { + ( + row.get::<_, u32>(0), + PeerId::from_bytes(row.get(1)).expect("parse peer_id"), + ) + })?; + + let mut peers = Vec::with_capacity(count as usize); + for row in rows { + let (id, peer_id) = row?; + let mut addrs = PeerAddr::get_addrs(conn, id, 1)?; + if let Some(addr) = addrs.pop() { + peers.push((peer_id, addr)); + } + } + Ok(peers) +} + pub fn get_peers_to_attempt(conn: &Connection, count: u32) -> DBResult> { // random select peers let mut stmt = conn.prepare("SELECT id, peer_id FROM peer_info WHERE status != :connected_status AND ban_time < strftime('%s','now') ORDER BY RANDOM() LIMIT :count")?; diff --git a/network/src/peer_store/sqlite_peer_store.rs b/network/src/peer_store/sqlite_peer_store.rs index c2e9310f29..b6aba605ba 100644 --- a/network/src/peer_store/sqlite_peer_store.rs +++ b/network/src/peer_store/sqlite_peer_store.rs @@ -344,6 +344,13 @@ impl PeerStore for SqlitePeerStore { .expect("get peers to attempt") } + //TODO Only return connected addresses after network support feeler connection + fn random_peers(&self, count: u32) -> Vec<(PeerId, Multiaddr)> { + self.pool + .fetch(|conn| db::get_random_peers(&conn, count)) + .expect("get random peers") + } + fn ban_peer(&mut self, peer_id: &PeerId, timeout: Duration) { if let Some(peer) = self.get_peer_info(peer_id) { self.ban_ip(&peer.connected_addr, timeout); diff --git a/network/src/tests/sqlite_peer_store.rs b/network/src/tests/sqlite_peer_store.rs index 2fe6aabbab..5611467388 100644 --- a/network/src/tests/sqlite_peer_store.rs +++ b/network/src/tests/sqlite_peer_store.rs @@ -111,6 +111,23 @@ fn test_peers_to_attempt() { assert!(peer_store.peers_to_attempt(1).is_empty()); } +#[test] +fn test_random_peers() { + let mut peer_store: Box = Box::new(SqlitePeerStore::temp()); + assert!(peer_store.random_peers(1).is_empty()); + let peer_id = random_peer_id(); + let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); + peer_store.add_bootnode(peer_id.clone(), addr.clone()); + assert!(peer_store.random_peers(1).is_empty()); + let peer_id2 = random_peer_id(); + peer_store + .add_discovered_address(&peer_id2, addr.clone()) + .expect("add discovered address"); + assert_eq!(peer_store.random_peers(2).len(), 1); + peer_store.update_status(&peer_id2, Status::Connected); + assert_eq!(peer_store.random_peers(1).len(), 1); +} + #[test] fn test_delete_peer_info() { let mut peer_store = SqlitePeerStore::temp(); From 789b7b72a8b7c74366b2fb66e820b2af385e5f80 Mon Sep 17 00:00:00 2001 From: jjy Date: Wed, 20 Mar 2019 16:27:18 +0800 Subject: [PATCH 34/99] fix: reduce lock scope --- sync/src/relayer/transaction_process.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/sync/src/relayer/transaction_process.rs b/sync/src/relayer/transaction_process.rs index f00544b3aa..750850ebca 100644 --- a/sync/src/relayer/transaction_process.rs +++ b/sync/src/relayer/transaction_process.rs @@ -39,9 +39,11 @@ where pub fn execute(self) { let (tx, relay_cycles): (Transaction, Cycle) = (*self.message).into(); - let chain_state = self.relayer.shared.chain_state().lock(); - let max_block_cycles = self.relayer.shared.consensus().max_block_cycles(); - let tx_result = chain_state.add_tx_to_pool(tx.clone(), max_block_cycles); + let tx_result = { + let chain_state = self.relayer.shared.chain_state().lock(); + let max_block_cycles = self.relayer.shared.consensus().max_block_cycles(); + chain_state.add_tx_to_pool(tx.clone(), max_block_cycles) + }; // disconnect peer if cycles mismatch match tx_result { Ok(cycles) if cycles == relay_cycles => { From fdb8c71207b9bae9e2ffcc312506bfc91c776a54 Mon Sep 17 00:00:00 2001 From: jjy Date: Wed, 20 Mar 2019 18:33:14 +0800 Subject: [PATCH 35/99] fix: node not connected yet when invoke add_bootnode --- network/src/peer_store/sqlite_peer_store.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/network/src/peer_store/sqlite_peer_store.rs b/network/src/peer_store/sqlite_peer_store.rs index b6aba605ba..d34cbab3e5 100644 --- a/network/src/peer_store/sqlite_peer_store.rs +++ b/network/src/peer_store/sqlite_peer_store.rs @@ -311,7 +311,6 @@ impl PeerStore for SqlitePeerStore { } fn add_bootnode(&mut self, peer_id: PeerId, addr: Multiaddr) { - self.new_connected_peer(&peer_id, addr.clone(), SessionType::Client); self.bootnodes.push((peer_id, addr)); } // should return high scored nodes if possible, otherwise, return boostrap nodes From a4952962395a2c969c684676d6efcd22ffe419fe Mon Sep 17 00:00:00 2001 From: jjy Date: Wed, 20 Mar 2019 18:44:38 +0800 Subject: [PATCH 36/99] fix: ban_peer with incorrect timeout --- network/src/peer_store/sqlite_peer_store.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/network/src/peer_store/sqlite_peer_store.rs b/network/src/peer_store/sqlite_peer_store.rs index d34cbab3e5..eebfb3338c 100644 --- a/network/src/peer_store/sqlite_peer_store.rs +++ b/network/src/peer_store/sqlite_peer_store.rs @@ -279,11 +279,10 @@ impl PeerStore for SqlitePeerStore { } }; let peer = self.get_or_insert_peer_info(peer_id); - let now = unix_time(); let score = peer.score.saturating_add(behaviour_score); if score < self.schema.ban_score() { - let ban_time = self.schema.default_ban_timeout() + now; - self.ban_peer(peer_id, ban_time); + let ban_timeout = self.schema.default_ban_timeout(); + self.ban_peer(peer_id, ban_timeout); return ReportResult::Banned; } self.pool From e68d90a38b5bdb60adac49284dd65184f6e4c690 Mon Sep 17 00:00:00 2001 From: zhangsoledad <787953403@qq.com> Date: Wed, 20 Mar 2019 18:47:32 +0800 Subject: [PATCH 37/99] refactor: replace unwrap with expect --- chain/src/chain.rs | 40 +++++++++++++++++++++++++++------- core/src/header.rs | 4 ++-- core/src/script.rs | 13 +++++++---- core/src/transaction.rs | 4 ++-- core/src/uncle.rs | 2 +- db/src/diskdb.rs | 7 ++---- network/src/network_service.rs | 6 ++--- network/src/protocol.rs | 4 ++-- pow/src/cuckoo.rs | 6 ++--- protocol/src/lib.rs | 23 +++++++++++-------- rpc/src/server.rs | 7 +++++- spec/src/lib.rs | 2 +- src/cli/miner.rs | 5 ++++- util/build-info/src/lib.rs | 12 +++++++--- util/logger/src/lib.rs | 6 ++--- 15 files changed, 93 insertions(+), 48 deletions(-) diff --git a/chain/src/chain.rs b/chain/src/chain.rs index f95e4f3d74..1d9d00eb7e 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -316,21 +316,33 @@ impl ChainService { ) { if new_tip_number <= current_tip_number { for bn in new_tip_number..=current_tip_number { - let hash = self.shared.block_hash(bn).unwrap(); - let old_block = self.shared.block(&hash).unwrap(); + let hash = self + .shared + .block_hash(bn) + .expect("block hash stored before alignment_fork"); + let old_block = self + .shared + .block(&hash) + .expect("block data stored before alignment_fork"); fork.detached_blocks.push(old_block); } } else { while index.number > current_tip_number { if index.unseen { - let ext = self.shared.block_ext(&index.hash).unwrap(); + let ext = self + .shared + .block_ext(&index.hash) + .expect("block ext stored before alignment_fork"); if ext.txs_verified.is_none() { fork.dirty_exts.push(ext) } else { index.unseen = false; } } - let new_block = self.shared.block(&index.hash).unwrap(); + let new_block = self + .shared + .block(&index.hash) + .expect("block data stored before alignment_fork"); index.forward(new_block.header().parent_hash().clone()); fork.attached_blocks.push(new_block); } @@ -342,15 +354,24 @@ impl ChainService { if index.number == 0 { break; } - let detached_hash = self.shared.block_hash(index.number).unwrap(); + let detached_hash = self + .shared + .block_hash(index.number) + .expect("detached hash stored before find_fork_until_latest_common"); if detached_hash == index.hash { break; } - let detached_blocks = self.shared.block(&detached_hash).unwrap(); + let detached_blocks = self + .shared + .block(&detached_hash) + .expect("detached block stored before find_fork_until_latest_common"); fork.detached_blocks.push(detached_blocks); if index.unseen { - let ext = self.shared.block_ext(&index.hash).unwrap(); + let ext = self + .shared + .block_ext(&index.hash) + .expect("block ext stored before find_fork_until_latest_common"); if ext.txs_verified.is_none() { fork.dirty_exts.push(ext) } else { @@ -358,7 +379,10 @@ impl ChainService { } } - let attached_block = self.shared.block(&index.hash).unwrap(); + let attached_block = self + .shared + .block(&index.hash) + .expect("attached block stored before find_fork_until_latest_common"); index.forward(attached_block.header().parent_hash().clone()); fork.attached_blocks.push(attached_block); } diff --git a/core/src/header.rs b/core/src/header.rs index 1b2dd52938..5ed65fe046 100644 --- a/core/src/header.rs +++ b/core/src/header.rs @@ -65,7 +65,7 @@ pub struct RawHeader { impl RawHeader { pub fn pow_hash(&self) -> H256 { - blake2b_256(serialize(self).unwrap()).into() + blake2b_256(serialize(self).expect("RawHeader serialize should not fail")).into() } pub fn with_seal(self, seal: Seal) -> Header { @@ -151,7 +151,7 @@ impl Header { } pub fn hash(&self) -> H256 { - blake2b_256(serialize(&self).unwrap()).into() + blake2b_256(serialize(&self).expect("Header serialize should not fail")).into() } pub fn pow_hash(&self) -> H256 { diff --git a/core/src/script.rs b/core/src/script.rs index e28814c571..83de334772 100644 --- a/core/src/script.rs +++ b/core/src/script.rs @@ -116,6 +116,9 @@ type ScriptTuple = ( Vec>, ); +const VEC_WRITE_ALL_EXPECT: &str = + "Essentially, Vec::write_all invoke extend_from_slice, should not fail"; + impl Script { pub fn new( version: u8, @@ -151,7 +154,9 @@ impl Script { // TODO: switch to flatbuffer serialization once we // can do stable serialization using flatbuffer. if let Some(ref data) = self.reference { - bytes.write_all(data.as_bytes()).unwrap(); + bytes + .write_all(data.as_bytes()) + .expect(VEC_WRITE_ALL_EXPECT); } // A separator is used here to prevent the rare case // that some binary might contain the exactly @@ -160,12 +165,12 @@ impl Script { // the hash. Note this might not solve every problem, // when flatbuffer change is done, we can leverage flatbuffer // serialization directly, which will be more reliable. - bytes.write_all(b"|").unwrap(); + bytes.write_all(b"|").expect(VEC_WRITE_ALL_EXPECT); if let Some(ref data) = self.binary { - bytes.write_all(&data).unwrap() + bytes.write_all(&data).expect(VEC_WRITE_ALL_EXPECT) } for argument in &self.signed_args { - bytes.write_all(argument).unwrap(); + bytes.write_all(argument).expect(VEC_WRITE_ALL_EXPECT); } blake2b_256(bytes).into() } diff --git a/core/src/transaction.rs b/core/src/transaction.rs index 06119ebb13..d35d1d861c 100644 --- a/core/src/transaction.rs +++ b/core/src/transaction.rs @@ -223,7 +223,7 @@ impl ProposalShortId { } pub fn hash(&self) -> H256 { - blake2b_256(serialize(self).unwrap()).into() + blake2b_256(serialize(self).expect("ProposalShortId serialize should not fail")).into() } pub fn zero() -> Self { @@ -257,7 +257,7 @@ impl Transaction { } pub fn hash(&self) -> H256 { - blake2b_256(serialize(&self).unwrap()).into() + blake2b_256(serialize(&self).expect("Transaction serialize should not fail")).into() } pub fn out_points_iter(&self) -> impl Iterator { diff --git a/core/src/uncle.rs b/core/src/uncle.rs index 979b33444a..cbc54b1eb5 100644 --- a/core/src/uncle.rs +++ b/core/src/uncle.rs @@ -62,6 +62,6 @@ pub fn uncles_hash(uncles: &[UncleBlock]) -> H256 { if uncles.is_empty() { H256::zero() } else { - blake2b_256(serialize(uncles).unwrap()).into() + blake2b_256(serialize(uncles).expect("Uncle serialize should not fail")).into() } } diff --git a/db/src/diskdb.rs b/db/src/diskdb.rs index ecc6cde450..4990b1fd4b 100644 --- a/db/src/diskdb.rs +++ b/db/src/diskdb.rs @@ -23,11 +23,8 @@ impl RocksDB { let cf_options: Vec<&str> = cfnames.iter().map(|n| n as &str).collect(); let db = DB::open_cf(&opts, &config.path, &cf_options).expect("Failed to open rocksdb"); - if config.options.is_some() { - let rocksdb_options: Vec<(&str, &str)> = config - .options - .as_ref() - .unwrap() + if let Some(db_opt) = config.options.as_ref() { + let rocksdb_options: Vec<(&str, &str)> = db_opt .iter() .map(|(k, v)| (k.as_str(), v.as_str())) .collect(); diff --git a/network/src/network_service.rs b/network/src/network_service.rs index 5cc4574984..3eb367ffc8 100644 --- a/network/src/network_service.rs +++ b/network/src/network_service.rs @@ -89,10 +89,10 @@ impl NetworkService { ckb_event_receiver, ping_event_receiver, ) - .unwrap(); - init_tx.send(()).unwrap(); + .expect("Network thread init"); + init_tx.send(()).expect("Network init signal send"); // here we use default config - let network_runtime = runtime::Runtime::new().unwrap(); + let network_runtime = runtime::Runtime::new().expect("Network tokio runtime init");; match network_runtime.block_on_all(network_future) { Ok(_) => info!(target: "network", "network service exit"), Err(err) => panic!("network service exit unexpected {}", err), diff --git a/network/src/protocol.rs b/network/src/protocol.rs index 9dd3fb80c1..a68043c5cb 100644 --- a/network/src/protocol.rs +++ b/network/src/protocol.rs @@ -117,8 +117,8 @@ impl ServiceProtocol for CKBHandler { .remote_pubkey .as_ref() .map(|pubkey| pubkey.peer_id()) - .unwrap(), - parsed_version.unwrap(), + .expect("remote_pubkey existence checked"), + parsed_version.expect("parsed_version existence checked"), ) }; debug!(target: "network", "ckb protocol connected, addr: {}, protocol: {}, version: {}, peer_id: {:?}", session.address, self.id, version, &peer_id); diff --git a/pow/src/cuckoo.rs b/pow/src/cuckoo.rs index a90ed4dec7..80ac382945 100644 --- a/pow/src/cuckoo.rs +++ b/pow/src/cuckoo.rs @@ -201,11 +201,11 @@ impl Cuckoo { let next_lower = *from_upper[&cur_edge.0] .iter() .find(|v| **v != cur_edge.1) - .unwrap(); + .expect("next_lower should be found"); let next_upper = *from_lower[&next_lower] .iter() .find(|u| **u != cur_edge.0) - .unwrap(); + .expect("next_upper should be found"); cur_edge = (next_upper, next_lower); cycle_length += 2; @@ -232,7 +232,7 @@ impl Cuckoo { } let path_u = Cuckoo::path(&graph, u); let path_v = Cuckoo::path(&graph, v); - if path_u.last().unwrap() == path_v.last().unwrap() { + if path_u.last().is_some() && (path_u.last() == path_v.last()) { let common = path_u .iter() .rev() diff --git a/protocol/src/lib.rs b/protocol/src/lib.rs index ecf88db09b..83f4b24529 100644 --- a/protocol/src/lib.rs +++ b/protocol/src/lib.rs @@ -5,8 +5,8 @@ mod convert; mod protocol_generated; pub use crate::protocol_generated::ckb::protocol::*; -use byteorder::{ByteOrder, LittleEndian, WriteBytesExt}; -use hash::blake2b_256; +use byteorder::{LittleEndian, ReadBytesExt}; +use hash::new_blake2b; use numext_fixed_hash::H256; use siphasher::sip::SipHasher; use std::hash::Hasher; @@ -39,14 +39,19 @@ impl<'a, T: flatbuffers::Follow<'a> + 'a> Iterator for FlatbuffersVectorIterator pub type ShortTransactionID = [u8; 6]; pub fn short_transaction_id_keys(header_nonce: u64, random_nonce: u64) -> (u64, u64) { - // sha3-256(header nonce + random nonce) in little-endian - let mut bytes = vec![]; - bytes.write_u64::(header_nonce).unwrap(); - bytes.write_u64::(random_nonce).unwrap(); - let block_header_with_nonce_hash = blake2b_256(bytes); + // blake2b-256(header nonce + random nonce) in little-endian + let mut block_header_with_nonce_hash = [0; 32]; + let mut blake2b = new_blake2b(); + blake2b.update(&header_nonce.to_le_bytes()); + blake2b.update(&random_nonce.to_le_bytes()); + blake2b.finalize(&mut block_header_with_nonce_hash); - let key0 = LittleEndian::read_u64(&block_header_with_nonce_hash[0..8]); - let key1 = LittleEndian::read_u64(&block_header_with_nonce_hash[8..16]); + let key0 = (&block_header_with_nonce_hash[0..8]) + .read_u64::() + .expect("read bound checked, should not fail"); + let key1 = (&block_header_with_nonce_hash[8..16]) + .read_u64::() + .expect("read bound checked, should not fail"); (key0, key1) } diff --git a/rpc/src/server.rs b/rpc/src/server.rs index 15639a0b7e..20b9ab11e7 100644 --- a/rpc/src/server.rs +++ b/rpc/src/server.rs @@ -92,7 +92,12 @@ impl RpcServer { ])) .threads(config.threads.unwrap_or_else(num_cpus::get)) .max_request_body_size(config.max_request_body_size) - .start_http(&config.listen_address.parse().unwrap()) + .start_http( + &config + .listen_address + .parse() + .expect("config listen_address parsed"), + ) .expect("Jsonrpc initialize"); RpcServer { server } diff --git a/spec/src/lib.rs b/spec/src/lib.rs index 8cd91453bc..2abb1ccc05 100644 --- a/spec/src/lib.rs +++ b/spec/src/lib.rs @@ -97,7 +97,7 @@ impl ChainSpec { pub fn read_from_file>(path: P) -> Result> { let config_str = std::fs::read_to_string(path.as_ref())?; let mut spec: Self = toml::from_str(&config_str)?; - spec.resolve_paths(path.as_ref().parent().unwrap()); + spec.resolve_paths(path.as_ref().parent().expect("chain spec path resolve")); Ok(spec) } diff --git a/src/cli/miner.rs b/src/cli/miner.rs index 768336a802..588f2b179a 100644 --- a/src/cli/miner.rs +++ b/src/cli/miner.rs @@ -43,7 +43,10 @@ impl Config { pub fn read_from_file>(path: P) -> Result> { let config_str = std::fs::read_to_string(path.as_ref())?; let mut config: Self = toml::from_str(&config_str)?; - config.resolve_paths(path.as_ref().parent().unwrap()); + config.resolve_paths(path.as_ref().parent().unwrap_or_else(|| { + eprintln!("Invalid config file path {:?}", path.as_ref()); + ::std::process::exit(1); + })); Ok(config) } } diff --git a/util/build-info/src/lib.rs b/util/build-info/src/lib.rs index d184826ae3..b300e3d7ac 100644 --- a/util/build-info/src/lib.rs +++ b/util/build-info/src/lib.rs @@ -3,9 +3,15 @@ use std::env; #[macro_export] macro_rules! get_version { () => {{ - let major = env!("CARGO_PKG_VERSION_MAJOR").parse::().unwrap(); - let minor = env!("CARGO_PKG_VERSION_MINOR").parse::().unwrap(); - let patch = env!("CARGO_PKG_VERSION_PATCH").parse::().unwrap(); + let major = env!("CARGO_PKG_VERSION_MAJOR") + .parse::() + .expect("CARGO_PKG_VERSION_MAJOR parse success"); + let minor = env!("CARGO_PKG_VERSION_MINOR") + .parse::() + .expect("CARGO_PKG_VERSION_MINOR parse success"); + let patch = env!("CARGO_PKG_VERSION_PATCH") + .parse::() + .expect("CARGO_PKG_VERSION_PATCH parse success"); let host_compiler = $crate::get_channel(); let commit_describe = option_env!("COMMIT_DESCRIBE").map(|s| s.to_string()); diff --git a/util/logger/src/lib.rs b/util/logger/src/lib.rs index b03969dfab..571626f8c4 100644 --- a/util/logger/src/lib.rs +++ b/util/logger/src/lib.rs @@ -74,7 +74,7 @@ impl Logger { } } }) - .unwrap(); + .expect("Logger thread init should not fail"); Logger { sender, @@ -134,7 +134,7 @@ impl Log for Logger { } fn flush(&self) { - let handle = self.handle.lock().take().unwrap(); + let handle = self.handle.lock().take().expect("Logger flush only once"); let _ = self.sender.send(Message::Terminate); let _ = handle.join(); } @@ -142,7 +142,7 @@ impl Log for Logger { fn sanitize_color(s: &str) -> String { lazy_static! { - static ref RE: Regex = Regex::new("\x1b\\[[^m]+m").unwrap(); + static ref RE: Regex = Regex::new("\x1b\\[[^m]+m").expect("Regex compile success"); } RE.replace_all(s, "").to_string() } From b60c86baba3b1c7d07183ad856cc73883f53d767 Mon Sep 17 00:00:00 2001 From: u2 Date: Thu, 21 Mar 2019 14:40:08 +0800 Subject: [PATCH 38/99] chore: rename doc.md to README.md [skip ci] --- rpc/{doc.md => README.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename rpc/{doc.md => README.md} (100%) diff --git a/rpc/doc.md b/rpc/README.md similarity index 100% rename from rpc/doc.md rename to rpc/README.md From b5a488068e1cab3a260efc5c809d64f1c1dd28bf Mon Sep 17 00:00:00 2001 From: Qian Linfeng Date: Thu, 14 Mar 2019 23:42:47 +0800 Subject: [PATCH 39/99] chore: Update tentacle version --- Cargo.lock | 231 ++++++++++++++++++++++++++++++++++++++-- network/Cargo.toml | 7 +- network/src/network.rs | 31 +++--- network/src/protocol.rs | 48 +++------ 4 files changed, 262 insertions(+), 55 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 19cc16346b..b8b75a5e3a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -483,8 +483,9 @@ dependencies = [ "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "snap 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)", - "tentacle 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tentacle-ping 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tentacle 0.2.0-alpha.1 (registry+https://github.com/rust-lang/crates.io-index)", + "tentacle-discovery 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tentacle-ping 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "tentacle-secio 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "unsigned-varint 0.2.2 (git+https://github.com/paritytech/unsigned-varint)", @@ -970,6 +971,26 @@ name = "data-encoding" version = "2.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "data-encoding-macro" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "data-encoding 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "data-encoding-macro-internal 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro-hack 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "data-encoding-macro-internal" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "data-encoding 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro-hack 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "digest" version = "0.8.0" @@ -992,6 +1013,11 @@ name = "encode_unicode" version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "endian-type" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "env_logger" version = "0.6.1" @@ -1278,6 +1304,16 @@ dependencies = [ "want 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "idna" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-normalization 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "indexmap" version = "1.0.2" @@ -1571,6 +1607,11 @@ dependencies = [ "linked-hash-map 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "matches" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "memchr" version = "2.1.3" @@ -1647,6 +1688,11 @@ dependencies = [ "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "nibble_vec" +version = "0.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "nix" version = "0.11.0" @@ -1844,6 +1890,19 @@ name = "plain" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "proc-macro-hack" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro-hack-impl 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "proc-macro-hack-impl" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "proc-macro2" version = "0.4.27" @@ -1876,6 +1935,11 @@ name = "quick-error" version = "1.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "quote" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "quote" version = "0.6.11" @@ -1903,6 +1967,15 @@ dependencies = [ "rusqlite 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "radix_trie" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "endian-type 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "nibble_vec 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rand" version = "0.3.23" @@ -2368,6 +2441,17 @@ dependencies = [ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "socket2" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "spin" version = "0.5.0" @@ -2411,6 +2495,16 @@ name = "subtle" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "syn" +version = "0.11.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "syn" version = "0.15.26" @@ -2421,6 +2515,14 @@ dependencies = [ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "synom" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "synstructure" version = "0.10.1" @@ -2452,7 +2554,7 @@ dependencies = [ [[package]] name = "tentacle" -version = "0.1.0" +version = "0.2.0-alpha.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2467,16 +2569,35 @@ dependencies = [ ] [[package]] -name = "tentacle-ping" +name = "tentacle-discovery" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ + "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", - "generic-channel 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "tentacle 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", + "tentacle 0.2.0-alpha.1 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", + "trust-dns 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "tentacle-ping" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", + "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "generic-channel 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "tentacle 0.2.0-alpha.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2821,6 +2942,49 @@ dependencies = [ "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "trust-dns" +version = "0.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "data-encoding 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "data-encoding-macro 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "radix_trie 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-tcp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "trust-dns-proto 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "trust-dns-proto" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "data-encoding 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", + "idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", + "socket2 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-executor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-reactor 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-tcp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-timer 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-udp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "try-lock" version = "0.1.0" @@ -2859,11 +3023,32 @@ dependencies = [ "version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "unicode-bidi" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-normalization" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "smallvec 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "unicode-width" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "unicode-xid" +version = "0.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "unicode-xid" version = "0.1.0" @@ -2896,6 +3081,16 @@ name = "untrusted" version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "url" +version = "1.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "utf8-ranges" version = "1.0.2" @@ -3090,9 +3285,12 @@ dependencies = [ "checksum ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "044f882973b245404e90c90e7e42e8ee8d7a64edfd7adf83d684fb97e8e2c1b6" "checksum ctrlc 3.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "630391922b1b893692c6334369ff528dcc3a9d8061ccf4c803aa8f83cb13db5e" "checksum data-encoding 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f4f47ca1860a761136924ddd2422ba77b2ea54fe8cc75b9040804a0d9d32ad97" +"checksum data-encoding-macro 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "9b87bf377fc964606c3679c1de6822a9a9d8b69aae2651ca4af28cb2d1550b37" +"checksum data-encoding-macro-internal 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "22b318a8d6d56c45df45c61fcc7d2dbf98ea014d4987e7c74ef1f86c9b87e503" "checksum digest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "05f47366984d3ad862010e22c7ce81a7dbcaebbdfb37241a620f8b6596ee135c" "checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0" "checksum encode_unicode 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "90b2c9496c001e8cb61827acdefad780795c42264c137744cae6f7d9e3450abd" +"checksum endian-type 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" "checksum env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b61fa891024a945da30a9581546e8cfaf5602c7b3f4c137a2805cf388f92075a" "checksum error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "07e791d3be96241c77c43846b665ef1384606da2cd2a48730abe606a12906e02" "checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2" @@ -3123,6 +3321,7 @@ dependencies = [ "checksum humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ca7e5f2e110db35f93b837c81797f3714500b81d517bf20c431b16d3ca4f114" "checksum hyper 0.11.27 (registry+https://github.com/rust-lang/crates.io-index)" = "34a590ca09d341e94cddf8e5af0bbccde205d5fbc2fa3c09dd67c7f85cea59d7" "checksum hyper 0.12.25 (registry+https://github.com/rust-lang/crates.io-index)" = "7d5b6658b016965ae301fa995306db965c93677880ea70765a84235a96eae896" +"checksum idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e" "checksum indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7e81a7c05f79578dbc15793d8b619db9ba32b4577003ef3af1a91c416798c58d" "checksum indicatif 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2c60da1c9abea75996b70a931bba6c750730399005b61ccd853cee50ef3d0d0c" "checksum iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dbe6e417e7d0975db6512b90796e8ce223145ac4e33c377e4a42882a0e88bb08" @@ -3153,6 +3352,7 @@ dependencies = [ "checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6" "checksum lru-cache 0.1.0 (git+https://github.com/nervosnetwork/lru-cache)" = "" "checksum lru-cache 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4d06ff7ff06f729ce5f4e227876cb88d10bc59cd4ae1e09fbb2bde15c850dc21" +"checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" "checksum memchr 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e1dd4eaac298c32ce07eb6ed9242eda7d82955b9170b7d6db59b2e02cc63fcb8" "checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3" "checksum merkle-cbt 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d589b5a7ca642540e7ccfbca3bcd0aa18693eb9287e2a6b17c79b1d062d52863" @@ -3161,6 +3361,7 @@ dependencies = [ "checksum mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "966257a94e196b11bb43aca423754d87429960a768de9414f3691d6957abf125" "checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" "checksum net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "42550d9fb7b6684a6d404d9fa7250c2eb2646df731d1c06afc06dcee9e1bcf88" +"checksum nibble_vec 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "c8d77f3db4bce033f4d04db08079b2ef1c3d02b44e86f25d08886fafa7756ffa" "checksum nix 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d37e713a259ff641624b6cb20e3b12b2952313ba36b6823c0f16e6cfd9e5de17" "checksum nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9667ddcc6cc8a43afc9b7917599d7216aa09c463919ea32c59ed6cac8bc945" "checksum nom 4.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b30adc557058ce00c9d0d7cb3c6e0b5bc6f36e2e2eabe74b0ba726d194abd588" @@ -3182,12 +3383,16 @@ dependencies = [ "checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" "checksum pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "676e8eb2b1b4c9043511a9b7bea0915320d7e502b0a079fb03f9635a5252b18c" "checksum plain 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" +"checksum proc-macro-hack 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2c725b36c99df7af7bf9324e9c999b9e37d92c8f8caf106d82e1d7953218d2d8" +"checksum proc-macro-hack-impl 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2b753ad9ed99dd8efeaa7d2fb8453c8f6bc3e54b97966d35f1bc77ca6865254a" "checksum proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)" = "4d317f9caece796be1980837fd5cb3dfec5613ebdb04ad0956deea83ce168915" "checksum proptest 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea66c78d75f2c6e9f304269eaef90899798daecc69f1a625d5a3dd793ff3522" "checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0" +"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" "checksum quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "cdd8e04bd9c52e0342b406469d494fcb033be4bdbe5c606016defbb1681411e1" "checksum r2d2 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)" = "5d746fc8a0dab19ccea7ff73ad535854e90ddb3b4b8cdce953dd5cd0b2e7bd22" "checksum r2d2_sqlite 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a7cba990b29ae565b1a765ef45f6b84a89a77736b91582e0243c12f613653857" +"checksum radix_trie 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ebcf72e767017c1aa4b63d4dd0b0b836a243b648fd81d41c6bf6e850ef7a95c7" "checksum rand 0.3.23 (registry+https://github.com/rust-lang/crates.io-index)" = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c" "checksum rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" "checksum rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c618c47cd3ebd209790115ab837de41425723956ad3ce2e6a7f09890947cacb9" @@ -3244,18 +3449,22 @@ dependencies = [ "checksum smallvec 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4c8cbcd6df1e117c2210e13ab5109635ad68a929fcbb8964dc965b76cb5ee013" "checksum smallvec 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)" = "88aea073965ab29f6edb5493faf96ad662fb18aa9eeb186a3b7057951605ed15" "checksum snap 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "95d697d63d44ad8b78b8d235bf85b34022a78af292c8918527c5f0cffdde7f43" +"checksum socket2 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "c4d11a52082057d87cb5caa31ad812f4504b97ab44732cd8359df2e9ff9f48e7" "checksum spin 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "44363f6f51401c34e7be73db0db371c04705d35efbe9f7d6082e03a921a32c55" "checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8" "checksum stream-cipher 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8861bc80f649f5b4c9bd38b696ae9af74499d479dbfb327f0607de6b326a36bc" "checksum string 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "b639411d0b9c738748b5397d5ceba08e648f4f1992231aa859af1a017f31f60b" "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" "checksum subtle 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2d67a5a62ba6e01cb2192ff309324cb4875d0c451d55fe2319433abe7a05a8ee" +"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" "checksum syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)" = "f92e629aa1d9c827b2bb8297046c1ccffc57c99b947a680d3ccff1f136a3bee9" +"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015" "checksum take 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b157868d8ac1f56b64604539990685fa7611d8fa9e5476cf0c02cf34d32917c5" "checksum tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "b86c784c88d98c801132806dadd3819ed29d8600836c4088e855cdf3e178ed8a" -"checksum tentacle 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6c29b4a6d8dbf15e8191c4723be562689ec734edc86fa9a84585c256d53e04be" -"checksum tentacle-ping 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "853dcfad34c9521fbb0c1dfc64d363b6efc774aae044a89411a25e3155130e24" +"checksum tentacle 0.2.0-alpha.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6ca96f03210576ee49f63cca4683b5c3001d0482c6b8c00d6792553f7d9beec5" +"checksum tentacle-discovery 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d458765c30402c4265902cf95ff56a85c11306efd0d2d27776e3e200e310e39c" +"checksum tentacle-ping 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9da20c040b0cf48d93c0c91cbd4c3c2fb77e0e39cbc99f9ee474af15a4a20050" "checksum tentacle-secio 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9562b522b885f057e39aac9feb9048e64d4b9cc7f5c0be5933ae6606eb56ad40" "checksum termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4096add70612622289f2fdcdbd5086dc81c1e2675e6ae58d6c4f62a16c6d7f2f" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" @@ -3285,18 +3494,24 @@ dependencies = [ "checksum tokio-yamux 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "1815b4502bb86c8ffaae8521ecc26e148edb4fcc2ed24c1feba0807a492b4946" "checksum toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "758664fc71a3a69038656bee8b6be6477d2a6c315a6b81f7081f591bffa4111f" "checksum toml 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "87c5890a989fa47ecdc7bcb4c63a77a82c18f306714104b1decfd722db17b39e" +"checksum trust-dns 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)" = "65096825b064877da37eeeb9a83390bd23433eabfc503a6476dc5b1949034aa7" +"checksum trust-dns-proto 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "09144f0992b0870fa8d2972cc069cbf1e3c0fda64d1f3d45c4d68d0e0b52ad4e" "checksum try-lock 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee2aa4715743892880f70885373966c83d73ef1b0838a664ef0c76fffd35e7c2" "checksum try-lock 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" "checksum twofish 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712d261e83e727c8e2dbb75dacac67c36e35db36a958ee504f2164fc052434e1" "checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169" "checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86" "checksum unicase 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9d3218ea14b4edcaccfa0df0a64a3792a2c32cc706f1b336e48867f9d3147f90" +"checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" +"checksum unicode-normalization 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "141339a08b982d942be2ca06ff8b076563cbe223d1befd5450716790d44e2426" "checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" +"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" "checksum unsigned-varint 0.2.2 (git+https://github.com/paritytech/unsigned-varint)" = "" "checksum unsigned-varint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2c64cdf40b4a9645534a943668681bcb219faf51874d4b65d2e0abda1b10a2ab" "checksum untrusted 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "55cd1f4b4e96b46aeb8d4855db4a7a9bd96eeeb5c6a1ab54593328761642ce2f" +"checksum url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a" "checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737" "checksum vcpkg 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "def296d3eb3b12371b2c7d0e83bfe1403e4db2d7a0bba324a12b21c4ee13143d" "checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" diff --git a/network/Cargo.toml b/network/Cargo.toml index fb20c1d48b..0d4ff2a07b 100644 --- a/network/Cargo.toml +++ b/network/Cargo.toml @@ -20,9 +20,10 @@ bytes = "0.4.12" tokio = "0.1.17" futures = { version = "0.1.19", features = ["use_std"] } snap = "0.2" -p2p = { version = "0.1", package="tentacle" } -p2p-ping = { version = "0.1", package="tentacle-ping" } -secio = { version = "0.1", package="tentacle-secio" } +p2p = { version = "0.2.0-alpha.1", package="tentacle" } +secio = { version = "0.1.0", package="tentacle-secio" } +p2p-ping = { version = "0.2.0", package="tentacle-ping" } +p2p-discovery = { version = "0.1.0", package="tentacle-discovery" } faketime = "0.2.0" rusqlite = {version = "0.16.0", features = ["bundled"]} lazy_static = "1.3.0" diff --git a/network/src/network.rs b/network/src/network.rs index bdf9d71efe..bd0d21d1f8 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -26,20 +26,19 @@ use futures::Stream; use log::{debug, error, info, warn}; use multiaddr::multihash::Multihash; use p2p::{ - builder::ServiceBuilder, + builder::{MetaBuilder, ServiceBuilder}, multiaddr::{self, Multiaddr}, secio::{PeerId, PublicKey}, - service::{Service, ServiceError, ServiceEvent}, + service::{DialProtocol, ProtocolHandle, Service, ServiceError, ServiceEvent}, traits::ServiceHandle, }; -use p2p_ping::{Event as PingEvent, PingProtocol}; +use p2p_ping::{Event as PingEvent, PingHandler}; use secio; use std::boxed::Box; use std::cmp::max; use std::sync::Arc; use std::time::{Duration, Instant}; use std::usize; -use tokio::codec::LengthDelimitedCodec; const PING_PROTOCOL_ID: ProtocolId = 0; @@ -80,7 +79,7 @@ impl PeerInfo { } } -type P2PService = Service; +type P2PService = Service; pub struct Network { pub(crate) peers_registry: RwLock, @@ -313,7 +312,7 @@ impl Network { } pub fn dial_addr(&self, addr: Multiaddr) { - if let Err(err) = self.p2p_control.write().dial(addr) { + if let Err(err) = self.p2p_control.write().dial(addr, DialProtocol::All) { error!(target: "network", "failed to dial: {}", err); } } @@ -372,14 +371,20 @@ impl Network { let mut p2p_service = ServiceBuilder::default().forever(true); // register protocols let (ping_sender, ping_receiver) = channel(std::u8::MAX as usize); - p2p_service = p2p_service.insert_protocol(PingProtocol::new( - PING_PROTOCOL_ID, - config.ping_interval, - config.ping_timeout, - ping_sender, - )); + let ping_meta = MetaBuilder::default() + .id(PING_PROTOCOL_ID) + .service_handle(move || { + ProtocolHandle::Callback(Box::new(PingHandler::new( + PING_PROTOCOL_ID, + config.ping_interval, + config.ping_timeout, + ping_sender, + ))) + }) + .build(); + p2p_service = p2p_service.insert_protocol(ping_meta); for (ckb_protocol, _) in &ckb_protocols { - p2p_service = p2p_service.insert_protocol(ckb_protocol.clone()); + p2p_service = p2p_service.insert_protocol(ckb_protocol.build()); } let mut p2p_service = p2p_service .key_pair(local_private_key.clone()) diff --git a/network/src/protocol.rs b/network/src/protocol.rs index a68043c5cb..22e847fa6f 100644 --- a/network/src/protocol.rs +++ b/network/src/protocol.rs @@ -1,12 +1,14 @@ use crate::{peers_registry::Session, PeerId, ServiceContext, SessionContext}; +use bytes::Bytes; use futures::sync::mpsc::Sender; use log::{debug, error}; use p2p::{ + builder::MetaBuilder, multiaddr::Multiaddr, - traits::{ProtocolMeta, ServiceProtocol}, + service::{ProtocolHandle, ProtocolMeta}, + traits::ServiceProtocol, ProtocolId, }; -use tokio::codec::LengthDelimitedCodec; pub type Version = u8; @@ -50,34 +52,18 @@ impl CKBProtocol { pub fn match_version(&self, version: Version) -> bool { self.supported_versions.contains(&version) } -} - -impl ProtocolMeta for CKBProtocol { - fn name(&self) -> String { - self.protocol_name() - } - - fn id(&self) -> ProtocolId { - CKBProtocol::id(&self) - } - - fn codec(&self) -> LengthDelimitedCodec { - LengthDelimitedCodec::new() - } - - fn service_handle(&self) -> Option> { - let handler = Box::new(CKBHandler { - id: self.id(), - event_sender: self.event_sender.clone(), - }); - Some(handler) - } - fn support_versions(&self) -> Vec { - self.supported_versions - .iter() - .map(|v| format!("{}", v)) - .collect() + pub fn build(&self) -> ProtocolMeta { + let event_sender = self.event_sender.clone(); + MetaBuilder::default() + .id(self.id) + .service_handle(move || { + ProtocolHandle::Callback(Box::new(CKBHandler { + id: self.id, + event_sender, + })) + }) + .build() } } @@ -146,14 +132,14 @@ impl ServiceProtocol for CKBHandler { } } - fn received(&mut self, _control: &mut ServiceContext, session: &SessionContext, data: Vec) { + fn received(&mut self, _control: &mut ServiceContext, session: &SessionContext, data: Bytes) { if let Some(peer_id) = session .remote_pubkey .as_ref() .map(|pubkey| pubkey.peer_id()) { debug!(target: "network", "ckb protocol received, addr: {}, protocol: {}, peer_id: {:?}", session.address, self.id, &peer_id); - self.send_event(Event::Received(peer_id, self.id, data)); + self.send_event(Event::Received(peer_id, self.id, data.to_vec())); } } fn notify(&mut self, _control: &mut ServiceContext, token: u64) { From 79e4277c5103cce40cc00dcbea05a309c6ccabea Mon Sep 17 00:00:00 2001 From: Linfeng Qian Date: Mon, 18 Mar 2019 13:14:24 +0800 Subject: [PATCH 40/99] feat: Add discovery service --- Cargo.lock | 36 +-- network/Cargo.toml | 12 +- network/src/network.rs | 62 +++-- network/src/network_service.rs | 3 +- network/src/service.rs | 1 + network/src/service/discovery_service.rs | 291 +++++++++++++++++++++++ network/src/service/identify_service.rs | 236 ------------------ 7 files changed, 362 insertions(+), 279 deletions(-) create mode 100644 network/src/service/discovery_service.rs delete mode 100644 network/src/service/identify_service.rs diff --git a/Cargo.lock b/Cargo.lock index b8b75a5e3a..1d3cb48251 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -483,10 +483,10 @@ dependencies = [ "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "snap 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)", - "tentacle 0.2.0-alpha.1 (registry+https://github.com/rust-lang/crates.io-index)", - "tentacle-discovery 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tentacle-ping 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tentacle-secio 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)", + "tentacle-discovery 0.1.0 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)", + "tentacle-ping 0.2.0 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)", + "tentacle-secio 0.1.0 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)", "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "unsigned-varint 0.2.2 (git+https://github.com/paritytech/unsigned-varint)", ] @@ -2555,23 +2555,23 @@ dependencies = [ [[package]] name = "tentacle" version = "0.2.0-alpha.1" -source = "registry+https://github.com/rust-lang/crates.io-index" +source = "git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format#9c42be05724c3e2decb73716d0f515c36a7af1cb" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "parity-multiaddr 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tentacle-secio 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tentacle-secio 0.1.0 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)", "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-threadpool 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-yamux 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-yamux 0.1.4 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)", ] [[package]] name = "tentacle-discovery" version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" +source = "git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format#9c42be05724c3e2decb73716d0f515c36a7af1cb" dependencies = [ "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2582,7 +2582,7 @@ dependencies = [ "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", - "tentacle 0.2.0-alpha.1 (registry+https://github.com/rust-lang/crates.io-index)", + "tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)", "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "trust-dns 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2590,20 +2590,20 @@ dependencies = [ [[package]] name = "tentacle-ping" version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" +source = "git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format#9c42be05724c3e2decb73716d0f515c36a7af1cb" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "generic-channel 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "tentacle 0.2.0-alpha.1 (registry+https://github.com/rust-lang/crates.io-index)", + "tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)", ] [[package]] name = "tentacle-secio" version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" +source = "git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format#9c42be05724c3e2decb73716d0f515c36a7af1cb" dependencies = [ "aes-ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "bs58 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2916,7 +2916,7 @@ dependencies = [ [[package]] name = "tokio-yamux" version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" +source = "git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format#9c42be05724c3e2decb73716d0f515c36a7af1cb" dependencies = [ "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3462,10 +3462,10 @@ dependencies = [ "checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015" "checksum take 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b157868d8ac1f56b64604539990685fa7611d8fa9e5476cf0c02cf34d32917c5" "checksum tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "b86c784c88d98c801132806dadd3819ed29d8600836c4088e855cdf3e178ed8a" -"checksum tentacle 0.2.0-alpha.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6ca96f03210576ee49f63cca4683b5c3001d0482c6b8c00d6792553f7d9beec5" -"checksum tentacle-discovery 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d458765c30402c4265902cf95ff56a85c11306efd0d2d27776e3e200e310e39c" -"checksum tentacle-ping 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9da20c040b0cf48d93c0c91cbd4c3c2fb77e0e39cbc99f9ee474af15a4a20050" -"checksum tentacle-secio 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9562b522b885f057e39aac9feb9048e64d4b9cc7f5c0be5933ae6606eb56ad40" +"checksum tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)" = "" +"checksum tentacle-discovery 0.1.0 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)" = "" +"checksum tentacle-ping 0.2.0 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)" = "" +"checksum tentacle-secio 0.1.0 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)" = "" "checksum termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4096add70612622289f2fdcdbd5086dc81c1e2675e6ae58d6c4f62a16c6d7f2f" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum termios 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "72b620c5ea021d75a735c943269bb07d30c9b77d6ac6b236bc8b5c496ef05625" @@ -3491,7 +3491,7 @@ dependencies = [ "checksum tokio-trace-core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "350c9edade9830dc185ae48ba45667a445ab59f6167ef6d0254ec9d2430d9dd3" "checksum tokio-udp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "66268575b80f4a4a710ef83d087fdfeeabdce9b74c797535fbac18a2cb906e92" "checksum tokio-uds 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "037ffc3ba0e12a0ab4aca92e5234e0dedeb48fddf6ccd260f1f150a36a9f2445" -"checksum tokio-yamux 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "1815b4502bb86c8ffaae8521ecc26e148edb4fcc2ed24c1feba0807a492b4946" +"checksum tokio-yamux 0.1.4 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)" = "" "checksum toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "758664fc71a3a69038656bee8b6be6477d2a6c315a6b81f7081f591bffa4111f" "checksum toml 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "87c5890a989fa47ecdc7bcb4c63a77a82c18f306714104b1decfd722db17b39e" "checksum trust-dns 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)" = "65096825b064877da37eeeb9a83390bd23433eabfc503a6476dc5b1949034aa7" diff --git a/network/Cargo.toml b/network/Cargo.toml index 0d4ff2a07b..6ca30e8341 100644 --- a/network/Cargo.toml +++ b/network/Cargo.toml @@ -20,10 +20,14 @@ bytes = "0.4.12" tokio = "0.1.17" futures = { version = "0.1.19", features = ["use_std"] } snap = "0.2" -p2p = { version = "0.2.0-alpha.1", package="tentacle" } -secio = { version = "0.1.0", package="tentacle-secio" } -p2p-ping = { version = "0.2.0", package="tentacle-ping" } -p2p-discovery = { version = "0.1.0", package="tentacle-discovery" } +# p2p = { version = "0.2.0-alpha.1", package="tentacle" } +# secio = { version = "0.1.0", package="tentacle-secio" } +# p2p-ping = { version = "0.2.0", package="tentacle-ping" } +# p2p-discovery = { git = "https://github.com/nervosnetwork/p2p", branch="discovery-use-multiaddr-format", package="tentacle-discovery" } +p2p = { git = "https://github.com/nervosnetwork/p2p", branch="discovery-use-multiaddr-format", package="tentacle" } +secio = { git = "https://github.com/nervosnetwork/p2p", branch="discovery-use-multiaddr-format", package="tentacle-secio" } +p2p-ping = { git = "https://github.com/nervosnetwork/p2p", branch="discovery-use-multiaddr-format", package="tentacle-ping" } +p2p-discovery = { git = "https://github.com/nervosnetwork/p2p", branch="discovery-use-multiaddr-format", package="tentacle-discovery" } faketime = "0.2.0" rusqlite = {version = "0.16.0", features = ["bundled"]} lazy_static = "1.3.0" diff --git a/network/src/network.rs b/network/src/network.rs index bd0d21d1f8..4f191e1d50 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -7,6 +7,7 @@ use crate::protocol::Version as ProtocolVersion; use crate::protocol_handler::{CKBProtocolHandler, DefaultCKBProtocolContext}; use crate::service::{ ckb_service::CKBService, + discovery_service::{DiscoveryEvent, DiscoveryProtocol, DiscoveryService}, outbound_peer_service::OutboundPeerService, ping_service::PingService, timer_service::{TimerRegistry, TimerService}, @@ -19,6 +20,7 @@ use bytes::Bytes; use ckb_util::{Mutex, RwLock}; use fnv::FnvHashMap; use futures::future::{select_all, Future}; +use futures::sync::mpsc; use futures::sync::mpsc::channel; use futures::sync::mpsc::Receiver; use futures::sync::oneshot; @@ -41,6 +43,7 @@ use std::time::{Duration, Instant}; use std::usize; const PING_PROTOCOL_ID: ProtocolId = 0; +const DISCOVERY_PROTOCOL_ID: ProtocolId = 1; pub type CKBProtocols = Vec<(CKBProtocol, Arc)>; type NetworkResult = Result< @@ -337,7 +340,16 @@ impl Network { pub(crate) fn inner_build( config: &NetworkConfig, ckb_protocols: CKBProtocols, - ) -> Result<(Arc, P2PService, TimerRegistry, Receiver), Error> { + ) -> Result< + ( + Arc, + P2PService, + TimerRegistry, + Receiver, + mpsc::UnboundedReceiver, + ), + Error, + > { let local_private_key = match config.fetch_private_key() { Some(private_key) => private_key?, None => return Err(ConfigError::InvalidKey.into()), @@ -383,6 +395,19 @@ impl Network { }) .build(); p2p_service = p2p_service.insert_protocol(ping_meta); + + let (disc_sender, disc_receiver) = mpsc::unbounded(); + let disc_meta = MetaBuilder::default() + .id(DISCOVERY_PROTOCOL_ID) + .service_handle(move || { + ProtocolHandle::Callback(Box::new(DiscoveryProtocol::new( + DISCOVERY_PROTOCOL_ID, + disc_sender.clone(), + ))) + }) + .build(); + p2p_service = p2p_service.insert_protocol(disc_meta); + for (ckb_protocol, _) in &ckb_protocols { p2p_service = p2p_service.insert_protocol(ckb_protocol.build()); } @@ -458,7 +483,13 @@ impl Network { } } - Ok((network, p2p_service, timer_registry, ping_receiver)) + Ok(( + network, + p2p_service, + timer_registry, + ping_receiver, + disc_receiver, + )) } pub(crate) fn build_network_future( @@ -469,18 +500,14 @@ impl Network { timer_registry: TimerRegistry, ckb_event_receiver: Receiver, ping_event_receiver: Receiver, + disc_event_receiver: mpsc::UnboundedReceiver, ) -> Result + Send>, Error> { // initialize ckb_protocols let ping_service = PingService { network: Arc::clone(&network), event_receiver: ping_event_receiver, }; - //let identify_service = Arc::new(IdentifyService { - // client_version, - // protocol_version, - // identify_timeout: config.identify_timeout, - // identify_interval: config.identify_interval, - //}); + let disc_service = DiscoveryService::new(Arc::clone(&network), disc_event_receiver); let ckb_service = CKBService { event_receiver: ckb_event_receiver, @@ -506,17 +533,11 @@ impl Network { .for_each(|_| Ok(())) .map_err(|_err| Error::Shutdown), ), - // Box::new( - // discovery_query_service - // .into_future() - // .map(|_| ()) - // .map_err(|(err, _)| err), - // ) as Box + Send>, - //identify_service.start_protocol( - // Arc::clone(&network), - // swarm_controller.clone(), - // basic_transport.clone(), - //), + Box::new( + disc_service + .for_each(|_| Ok(())) + .map_err(|_err| Error::Shutdown), + ), Box::new(timer_service.timer_futures.for_each(|_| Ok(()))), Box::new( outbound_peer_service @@ -548,7 +569,7 @@ impl Network { ckb_protocols: CKBProtocols, ckb_event_receiver: Receiver, ) -> NetworkResult { - let (network, p2p_service, timer_registry, ping_event_receiver) = + let (network, p2p_service, timer_registry, ping_event_receiver, disc_event_receiver) = Self::inner_build(config, ckb_protocols)?; let (close_tx, close_rx) = oneshot::channel(); let network_future = Self::build_network_future( @@ -559,6 +580,7 @@ impl Network { timer_registry, ckb_event_receiver, ping_event_receiver, + disc_event_receiver, )?; Ok((network, close_tx, network_future)) } diff --git a/network/src/network_service.rs b/network/src/network_service.rs index 3eb367ffc8..56a8535b91 100644 --- a/network/src/network_service.rs +++ b/network/src/network_service.rs @@ -67,7 +67,7 @@ impl NetworkService { ckb_protocols: CKBProtocols, ckb_event_receiver: Receiver, ) -> Result { - let (network, p2p_service, timer_registry, ping_event_receiver) = + let (network, p2p_service, timer_registry, ping_event_receiver, disc_event_receiver) = Network::inner_build(config, ckb_protocols)?; let (close_tx, close_rx) = oneshot::channel(); let (init_tx, init_rx) = oneshot::channel(); @@ -88,6 +88,7 @@ impl NetworkService { timer_registry, ckb_event_receiver, ping_event_receiver, + disc_event_receiver, ) .expect("Network thread init"); init_tx.send(()).expect("Network init signal send"); diff --git a/network/src/service.rs b/network/src/service.rs index 18a76a1dc9..a815acd24e 100644 --- a/network/src/service.rs +++ b/network/src/service.rs @@ -1,4 +1,5 @@ pub mod ckb_service; +pub mod discovery_service; pub mod outbound_peer_service; pub mod ping_service; pub mod timer_service; diff --git a/network/src/service/discovery_service.rs b/network/src/service/discovery_service.rs new file mode 100644 index 0000000000..4b82f474b9 --- /dev/null +++ b/network/src/service/discovery_service.rs @@ -0,0 +1,291 @@ +use crate::peer_store::Behaviour; +use crate::Network; +use fnv::FnvHashMap; +use futures::{sync::mpsc, sync::oneshot, Async, Future, Stream}; +use log::{debug, warn}; +use std::sync::Arc; + +use p2p::{ + context::{ServiceContext, SessionContext}, + multiaddr::Multiaddr, + secio::PeerId, + traits::ServiceProtocol, + yamux::session::SessionType, + ProtocolId, SessionId, +}; +use p2p_discovery::{ + AddressManager, Direction, Discovery, DiscoveryHandle, MisbehaveResult, Misbehavior, Substream, +}; + +pub struct DiscoveryProtocol { + id: ProtocolId, + discovery: Option>, + discovery_handle: DiscoveryHandle, + discovery_senders: FnvHashMap>>, + event_sender: mpsc::UnboundedSender, +} + +impl DiscoveryProtocol { + pub fn new( + id: ProtocolId, + event_sender: mpsc::UnboundedSender, + ) -> DiscoveryProtocol { + let addr_mgr = DiscoveryAddressManager { + event_sender: event_sender.clone(), + }; + let discovery = Discovery::new(addr_mgr); + let discovery_handle = discovery.handle(); + DiscoveryProtocol { + id, + discovery: Some(discovery), + discovery_handle, + discovery_senders: FnvHashMap::default(), + event_sender, + } + } +} + +impl ServiceProtocol for DiscoveryProtocol { + fn init(&mut self, control: &mut ServiceContext) { + debug!("protocol [discovery({})]: init", self.id); + + let discovery_task = self + .discovery + .take() + .map(|discovery| { + debug!("Start discovery future_task"); + discovery + .for_each(|()| Ok(())) + .map_err(|err| { + warn!("discovery stream error: {:?}", err); + }) + .then(|_| { + debug!("End of discovery"); + Ok(()) + }) + }) + .unwrap(); + control.future_task(discovery_task); + } + + fn connected(&mut self, control: &mut ServiceContext, session: &SessionContext, _: &str) { + debug!( + "protocol [discovery] open on session [{}], address: [{}], type: [{:?}]", + session.id, session.address, session.ty + ); + let event = DiscoveryEvent::Connected { + session_id: session.id, + peer_id: session.remote_pubkey.clone().map(|pubkey| pubkey.peer_id()), + }; + // TODO: handle send failed + self.event_sender + .unbounded_send(event) + .expect("Send connected event failed"); + + let direction = if session.ty == SessionType::Server { + Direction::Inbound + } else { + Direction::Outbound + }; + let (sender, receiver) = mpsc::channel(8); + self.discovery_senders.insert(session.id, sender); + let substream = Substream::new( + session.address.clone(), + direction, + self.id, + session.id, + receiver, + control.control().clone(), + control.listens(), + ); + match self.discovery_handle.substream_sender.try_send(substream) { + Ok(_) => { + debug!("Send substream success"); + } + Err(err) => { + // TODO: handle channel is full (wait for poll API?) + warn!("Send substream failed : {:?}", err); + } + } + } + + fn disconnected(&mut self, _control: &mut ServiceContext, session: &SessionContext) { + let event = DiscoveryEvent::Disconnected(session.id); + // TODO: handle send failed + self.event_sender + .unbounded_send(event) + .expect("Send disconnected event failed"); + self.discovery_senders.remove(&session.id); + debug!("protocol [discovery] close on session [{}]", session.id); + } + + fn received( + &mut self, + _control: &mut ServiceContext, + session: &SessionContext, + data: bytes::Bytes, + ) { + debug!("[received message]: length={}", data.len()); + + if let Some(ref mut sender) = self.discovery_senders.get_mut(&session.id) { + // TODO: handle channel is full (wait for poll API?) + if let Err(err) = sender.try_send(data.to_vec()) { + if err.is_full() { + warn!("channel is full"); + } else if err.is_disconnected() { + warn!("channel is disconnected"); + } else { + warn!("other channel error: {:?}", err); + } + } + } + } +} + +pub enum DiscoveryEvent { + Connected { + session_id: SessionId, + peer_id: Option, + }, + Disconnected(SessionId), + AddNewAddr { + session_id: SessionId, + addr: Multiaddr, + }, + AddNewAddrs { + session_id: SessionId, + addrs: Vec, + }, + Misbehave { + session_id: SessionId, + kind: Misbehavior, + result: oneshot::Sender, + }, + GetRandom { + n: usize, + result: oneshot::Sender>, + }, +} + +pub struct DiscoveryService { + event_receiver: mpsc::UnboundedReceiver, + network: Arc, + sessions: FnvHashMap, +} + +impl DiscoveryService { + pub fn new( + network: Arc, + event_receiver: mpsc::UnboundedReceiver, + ) -> DiscoveryService { + DiscoveryService { + event_receiver, + network, + sessions: FnvHashMap::default(), + } + } +} + +impl Stream for DiscoveryService { + type Item = (); + type Error = (); + fn poll(&mut self) -> Result>, Self::Error> { + match try_ready!(self.event_receiver.poll()) { + Some(DiscoveryEvent::Connected { + session_id, + peer_id, + }) => { + if let Some(peer_id) = peer_id { + self.sessions.insert(session_id, peer_id); + } + } + Some(DiscoveryEvent::Disconnected(session_id)) => { + self.sessions.remove(&session_id); + } + Some(DiscoveryEvent::AddNewAddr { session_id, addr }) => { + if let Some(peer_id) = self.sessions.get(&session_id) { + let _ = self + .network + .peer_store() + .write() + .add_discovered_address(peer_id, addr); + } + } + Some(DiscoveryEvent::AddNewAddrs { session_id, addrs }) => { + if let Some(peer_id) = self.sessions.get(&session_id) { + let _ = self + .network + .peer_store() + .write() + .add_discovered_addresses(peer_id, addrs); + } + } + Some(DiscoveryEvent::Misbehave { + session_id: _session_id, + kind: _kind, + result: _result, + }) => { + // FIXME: + } + Some(DiscoveryEvent::GetRandom { n, result }) => { + let addrs = self + .network + .peer_store() + .read() + .peers_to_attempt(n as u32) + .into_iter() + .map(|(_peer_id, addr)| addr) + .collect(); + result + .send(addrs) + .expect("Send failed (should not happened)"); + } + None => { + debug!(target: "network", "discovery service shutdown"); + return Ok(Async::Ready(None)); + } + } + Ok(Async::Ready(Some(()))) + } +} + +pub struct DiscoveryAddressManager { + pub event_sender: mpsc::UnboundedSender, +} + +impl AddressManager for DiscoveryAddressManager { + fn add_new_addr(&mut self, session_id: SessionId, addr: Multiaddr) { + // FIXME: what if send failed + let event = DiscoveryEvent::AddNewAddr { session_id, addr }; + self.event_sender.unbounded_send(event).unwrap(); + } + + fn add_new_addrs(&mut self, session_id: SessionId, addrs: Vec) { + // FIXME: what if send failed + let event = DiscoveryEvent::AddNewAddrs { session_id, addrs }; + self.event_sender.unbounded_send(event).unwrap(); + } + + fn misbehave(&mut self, session_id: SessionId, kind: Misbehavior) -> MisbehaveResult { + let (sender, receiver) = oneshot::channel(); + // FIXME: what if send failed + let event = DiscoveryEvent::Misbehave { + session_id, + kind, + result: sender, + }; + self.event_sender.unbounded_send(event).unwrap(); + // FIXME: what if receive failed + receiver.wait().unwrap() + } + + fn get_random(&mut self, n: usize) -> Vec { + let (sender, receiver) = oneshot::channel(); + // FIXME: what if send failed + self.event_sender + .unbounded_send(DiscoveryEvent::GetRandom { n, result: sender }) + .unwrap(); + // FIXME: what if receive failed + receiver.wait().unwrap() + } +} diff --git a/network/src/service/identify_service.rs b/network/src/service/identify_service.rs deleted file mode 100644 index 079be95127..0000000000 --- a/network/src/service/identify_service.rs +++ /dev/null @@ -1,236 +0,0 @@ -#![allow(clippy::needless_pass_by_value)] - -use super::Network; -use super::PeerId; -use crate::peers_registry::PeerIdentifyInfo; -use crate::protocol::Protocol; -use crate::protocol_service::ProtocolService; -use crate::transport::TransportOutput; -use futures::future::{self, Future}; -use futures::Stream; -use libp2p::core::Multiaddr; -use libp2p::core::SwarmController; -use libp2p::core::{upgrade, MuxedTransport}; -use libp2p::identify::IdentifyProtocolConfig; -use libp2p::identify::{IdentifyInfo, IdentifyOutput}; -use libp2p::{self, Transport}; -use log::{debug, error, trace, warn}; -use std::boxed::Box; -use std::io::{Error as IoError, ErrorKind as IoErrorKind}; -use std::sync::Arc; -use std::time::Duration; -use std::time::Instant; -use tokio::io::{AsyncRead, AsyncWrite}; -use tokio::timer::Interval; - -const MAX_LISTENED_ADDRS: usize = 10; - -pub struct IdentifyService { - pub client_version: String, - pub protocol_version: String, - pub identify_timeout: Duration, - pub identify_interval: Duration, -} - -impl IdentifyService { - fn process_identify_info( - &self, - network: Arc, - peer_id: &PeerId, - info: &IdentifyInfo, - observed_addr: &Multiaddr, - ) -> Result<(), IoError> { - trace!("process identify for peer_id {:?} with {:?}", peer_id, info); - // set identify info to peer - { - let identify_info = PeerIdentifyInfo { - client_version: info.agent_version.clone(), - protocol_version: info.protocol_version.clone(), - supported_protocols: info.protocols.clone(), - count_of_known_listen_addrs: info.listen_addrs.len(), - }; - if network - .set_peer_identify_info(&peer_id, identify_info) - .is_err() - { - error!( - target: "network", - "can't find peer_id {:?} during process identify info", - peer_id - ) - } - } - - // add obserevd listened addr - for original_address in network.original_listened_addresses.read().iter() { - let transport = libp2p::tcp::TcpConfig::new(); - trace!( - target: "network", - "try get address use original_address {:?} and observed_address {:?}", - original_address, - observed_addr - ); - // get an external addrs for our node - if let Some(ext_addr) = transport.nat_traversal(original_address, &observed_addr) { - debug!(target: "network", "get new external address {:?}", ext_addr); - network.discovery_listened_address(ext_addr.to_owned()); - } - } - - // update peer addrs in peerstore - let _ = network - .peer_store() - .write() - .add_discovered_addresses(peer_id, info.listen_addrs.clone()); - Ok(()) - } -} - -impl ProtocolService for IdentifyService -where - T: AsyncRead + AsyncWrite + Send + 'static, -{ - type Output = IdentifyOutput; - fn convert_to_protocol( - peer_id: Arc, - addr: &Multiaddr, - output: Self::Output, - ) -> Protocol { - let peer_id = PeerId::clone(&peer_id); - match output { - IdentifyOutput::RemoteInfo { - info, - observed_addr, - } => Protocol::IdentifyRequest(peer_id, info, observed_addr), - IdentifyOutput::Sender { sender } => { - Protocol::IdentifyResponse(peer_id, sender, addr.to_owned()) - } - } - } - - fn handle( - &self, - network: Arc, - protocol: Protocol, - ) -> Box + Send> { - match protocol { - Protocol::IdentifyRequest(peer_id, info, ovserved_addr) => match self - .process_identify_info(Arc::clone(&network), &peer_id, &info, &ovserved_addr) - { - Ok(_) => Box::new(future::ok(())), - Err(err) => Box::new(future::err(err)), - }, - Protocol::IdentifyResponse(_peer_id, sender, addr) => { - sender.send( - IdentifyInfo { - public_key: network.local_public_key().clone(), - protocol_version: format!("ckb/{}", self.protocol_version).to_owned(), - agent_version: format!("ckb/{}", self.client_version).to_owned(), - listen_addrs: network - .listened_addresses(MAX_LISTENED_ADDRS) - .into_iter() - .map(|(addr, _)| addr) - .collect(), - protocols: vec![], // TODO FIXME: report local protocols - }, - &addr, - ) - } - _ => Box::new(future::ok(())) as Box + Send>, - } - } - - fn start_protocol( - &self, - network: Arc, - swarm_controller: SwarmController< - SwarmTran, - Box + Send>, - >, - transport: Tran, - ) -> Box + Send> - where - SwarmTran: MuxedTransport> + Clone + Send + 'static, - SwarmTran::MultiaddrFuture: Send + 'static, - SwarmTran::Dial: Send, - SwarmTran::Listener: Send, - SwarmTran::ListenerUpgrade: Send, - SwarmTran::Incoming: Send, - SwarmTran::IncomingUpgrade: Send, - Tran: MuxedTransport> + Clone + Send + 'static, - Tran::MultiaddrFuture: Send + 'static, - Tran::Dial: Send, - Tran::Listener: Send, - Tran::ListenerUpgrade: Send, - Tran::Incoming: Send, - Tran::IncomingUpgrade: Send, - TranOut: AsyncRead + AsyncWrite + Send + 'static, - { - let transport = transport.and_then(move |out, endpoint, client_addr| { - let peer_id = out.peer_id; - upgrade::apply(out.socket, IdentifyProtocolConfig, endpoint, client_addr).map( - move |(output, addr)| { - let protocol = match output { - IdentifyOutput::RemoteInfo { - info, - observed_addr, - } => Protocol::IdentifyRequest(peer_id, info, observed_addr), - IdentifyOutput::Sender { .. } => { - panic!("should not reach here because we are dialer") - } - }; - (protocol, addr) - }, - ) - }); - - let periodic_identify_future = Interval::new( - Instant::now() + Duration::from_secs(5), - self.identify_interval, - ) - .map_err(|err| { - debug!(target: "network", "identify periodic error {:?}", err); - IoError::new( - IoErrorKind::Other, - format!("identify periodic error {:?}", err), - ) - }) - .for_each({ - let transport = transport.clone(); - let _identify_timeout = self.identify_timeout; - let network = Arc::clone(&network); - move |_| { - for peer_id in network.peers() { - if let Some(ref identify_info) = network.get_peer_identify_info(&peer_id) { - if identify_info.count_of_known_listen_addrs > 0 { - continue; - } - } - // TODO should we try all addresses? - if let Some(addr) = network.get_peer_addresses(&peer_id).get(0) { - trace!( - target: "network", - "request identify to peer {:?} {:?}", - peer_id, - addr - ); - // dial identify - let _ = swarm_controller.dial(addr.clone(), transport.clone()); - } else { - error!( - target: "network", - "error when prepare identify : can't find addresses for peer {:?}", - peer_id - ); - } - } - Ok(()) - } - }) - .then(|err| { - warn!(target: "network", "Identify service stopped, reason: {:?}", err); - err - }); - Box::new(periodic_identify_future) as Box + Send> - } -} From 2b4a02b05fa15ee4590d83235cd137ea67d5b921 Mon Sep 17 00:00:00 2001 From: Linfeng Qian Date: Tue, 19 Mar 2019 10:37:32 +0800 Subject: [PATCH 41/99] refactor: Use Bytes instead of Vec as data type --- Cargo.lock | 1 + network/src/protocol.rs | 4 ++-- network/src/protocol_handler.rs | 3 ++- network/src/service/discovery_service.rs | 12 +++--------- sync/Cargo.toml | 1 + sync/src/net_time_checker.rs | 3 ++- sync/src/relayer/mod.rs | 3 ++- sync/src/synchronizer/mod.rs | 3 ++- 8 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1d3cb48251..8422850c5c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -617,6 +617,7 @@ version = "0.7.0-pre" dependencies = [ "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "bloom-filters 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "ckb-chain 0.7.0-pre", "ckb-chain-spec 0.7.0-pre", "ckb-core 0.7.0-pre", diff --git a/network/src/protocol.rs b/network/src/protocol.rs index 22e847fa6f..4029f77f9d 100644 --- a/network/src/protocol.rs +++ b/network/src/protocol.rs @@ -71,7 +71,7 @@ impl CKBProtocol { pub enum Event { Connected(PeerId, Multiaddr, Session, ProtocolId, Version), Disconnected(PeerId, ProtocolId), - Received(PeerId, ProtocolId, Vec), + Received(PeerId, ProtocolId, Bytes), Notify(ProtocolId, u64), } @@ -139,7 +139,7 @@ impl ServiceProtocol for CKBHandler { .map(|pubkey| pubkey.peer_id()) { debug!(target: "network", "ckb protocol received, addr: {}, protocol: {}, peer_id: {:?}", session.address, self.id, &peer_id); - self.send_event(Event::Received(peer_id, self.id, data.to_vec())); + self.send_event(Event::Received(peer_id, self.id, data)); } } fn notify(&mut self, _control: &mut ServiceContext, token: u64) { diff --git a/network/src/protocol_handler.rs b/network/src/protocol_handler.rs index 6f62a8d195..61ca5aca1b 100644 --- a/network/src/protocol_handler.rs +++ b/network/src/protocol_handler.rs @@ -1,5 +1,6 @@ use crate::errors::{Error, PeerError, ProtocolError}; use crate::{Network, PeerIndex, ProtocolId, SessionInfo, TimerRegistry, TimerToken}; +use bytes::Bytes; use ckb_util::Mutex; use log::debug; use log::info; @@ -148,7 +149,7 @@ impl CKBProtocolContext for DefaultCKBProtocolContext { pub trait CKBProtocolHandler: Sync + Send { fn initialize(&self, _nc: Box); - fn received(&self, _nc: Box, _peer: PeerIndex, _data: Vec); + fn received(&self, _nc: Box, _peer: PeerIndex, _data: Bytes); fn connected(&self, _nc: Box, _peer: PeerIndex); fn disconnected(&self, _nc: Box, _peer: PeerIndex); fn timer_triggered(&self, _nc: Box, _timer: TimerToken) {} diff --git a/network/src/service/discovery_service.rs b/network/src/service/discovery_service.rs index 4b82f474b9..ddcc97dc46 100644 --- a/network/src/service/discovery_service.rs +++ b/network/src/service/discovery_service.rs @@ -1,4 +1,4 @@ -use crate::peer_store::Behaviour; +// use crate::peer_store::Behaviour; use crate::Network; use fnv::FnvHashMap; use futures::{sync::mpsc, sync::oneshot, Async, Future, Stream}; @@ -202,14 +202,8 @@ impl Stream for DiscoveryService { Some(DiscoveryEvent::Disconnected(session_id)) => { self.sessions.remove(&session_id); } - Some(DiscoveryEvent::AddNewAddr { session_id, addr }) => { - if let Some(peer_id) = self.sessions.get(&session_id) { - let _ = self - .network - .peer_store() - .write() - .add_discovered_address(peer_id, addr); - } + Some(DiscoveryEvent::AddNewAddr { .. }) => { + // NOTE: ignore add new addr message, handle this in identify protocol } Some(DiscoveryEvent::AddNewAddrs { session_id, addrs }) => { if let Some(peer_id) = self.sessions.get(&session_id) { diff --git a/sync/Cargo.toml b/sync/Cargo.toml index 92881ac0bf..2a996dc998 100644 --- a/sync/Cargo.toml +++ b/sync/Cargo.toml @@ -26,6 +26,7 @@ ckb-chain-spec = { path = "../spec" } bloom-filters = "0.1.0" ckb-traits = { path = "../traits" } failure = "0.1.5" +bytes = "0.4.12" [dev-dependencies] ckb-notify = { path = "../notify" } diff --git a/sync/src/net_time_checker.rs b/sync/src/net_time_checker.rs index 5b621870fe..2750f7f3cc 100644 --- a/sync/src/net_time_checker.rs +++ b/sync/src/net_time_checker.rs @@ -1,3 +1,4 @@ +use bytes::Bytes; use ckb_network::{CKBProtocolContext, CKBProtocolHandler, PeerIndex}; use ckb_protocol::TimeMessage; use ckb_util::RwLock; @@ -90,7 +91,7 @@ impl Default for NetTimeProtocol { impl CKBProtocolHandler for NetTimeProtocol { fn initialize(&self, _nc: Box) {} - fn received(&self, nc: Box, peer: PeerIndex, data: Vec) { + fn received(&self, nc: Box, peer: PeerIndex, data: Bytes) { // collect time sample from outbound peer if nc.session_info(peer).map(|s| s.peer.is_outbound()) == Some(true) { let now: u64 = faketime::unix_time_as_millis(); diff --git a/sync/src/relayer/mod.rs b/sync/src/relayer/mod.rs index 115469220f..2c2f28f8f9 100644 --- a/sync/src/relayer/mod.rs +++ b/sync/src/relayer/mod.rs @@ -17,6 +17,7 @@ use self::get_block_transactions_process::GetBlockTransactionsProcess; use self::transaction_process::TransactionProcess; use crate::relayer::compact_block::ShortTransactionID; use crate::types::Peers; +use bytes::Bytes; use ckb_chain::chain::ChainController; use ckb_core::block::{Block, BlockBuilder}; use ckb_core::transaction::{ProposalShortId, Transaction}; @@ -269,7 +270,7 @@ where let _ = nc.register_timer(TX_PROPOSAL_TOKEN, Duration::from_millis(100)); } - fn received(&self, nc: Box, peer: PeerIndex, data: Vec) { + fn received(&self, nc: Box, peer: PeerIndex, data: Bytes) { // TODO use flatbuffers verifier let msg = get_root::(&data); debug!(target: "relay", "msg {:?}", msg.payload_type()); diff --git a/sync/src/synchronizer/mod.rs b/sync/src/synchronizer/mod.rs index 56d99a2d30..886ac968be 100644 --- a/sync/src/synchronizer/mod.rs +++ b/sync/src/synchronizer/mod.rs @@ -21,6 +21,7 @@ use crate::{ MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT, MAX_TIP_AGE, POW_SPACE, }; use bitflags::bitflags; +use bytes::Bytes; use ckb_chain::chain::ChainController; use ckb_chain_spec::consensus::Consensus; use ckb_core::block::Block; @@ -691,7 +692,7 @@ where let _ = nc.register_timer(TIMEOUT_EVICTION_TOKEN, Duration::from_millis(1000)); } - fn received(&self, nc: Box, peer: PeerIndex, data: Vec) { + fn received(&self, nc: Box, peer: PeerIndex, data: Bytes) { // TODO use flatbuffers verifier let msg = get_root::(&data); debug!(target: "sync", "msg {:?}", msg.payload_type()); From c367508b48955b94cc06c08fdb904f936c897972 Mon Sep 17 00:00:00 2001 From: Linfeng Qian Date: Tue, 19 Mar 2019 10:39:56 +0800 Subject: [PATCH 42/99] fix: Add target field for log --- network/src/service/discovery_service.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/network/src/service/discovery_service.rs b/network/src/service/discovery_service.rs index ddcc97dc46..869dc2f546 100644 --- a/network/src/service/discovery_service.rs +++ b/network/src/service/discovery_service.rs @@ -47,20 +47,20 @@ impl DiscoveryProtocol { impl ServiceProtocol for DiscoveryProtocol { fn init(&mut self, control: &mut ServiceContext) { - debug!("protocol [discovery({})]: init", self.id); + debug!(target: "network", "protocol [discovery({})]: init", self.id); let discovery_task = self .discovery .take() .map(|discovery| { - debug!("Start discovery future_task"); + debug!(target: "network", "Start discovery future_task"); discovery .for_each(|()| Ok(())) .map_err(|err| { - warn!("discovery stream error: {:?}", err); + warn!(target: "network", "discovery stream error: {:?}", err); }) .then(|_| { - debug!("End of discovery"); + debug!(target: "network", "End of discovery"); Ok(()) }) }) @@ -70,6 +70,7 @@ impl ServiceProtocol for DiscoveryProtocol { fn connected(&mut self, control: &mut ServiceContext, session: &SessionContext, _: &str) { debug!( + target: "network", "protocol [discovery] open on session [{}], address: [{}], type: [{:?}]", session.id, session.address, session.ty ); @@ -100,11 +101,11 @@ impl ServiceProtocol for DiscoveryProtocol { ); match self.discovery_handle.substream_sender.try_send(substream) { Ok(_) => { - debug!("Send substream success"); + debug!(target: "network", "Send substream success"); } Err(err) => { // TODO: handle channel is full (wait for poll API?) - warn!("Send substream failed : {:?}", err); + warn!(target: "network", "Send substream failed : {:?}", err); } } } @@ -116,7 +117,7 @@ impl ServiceProtocol for DiscoveryProtocol { .unbounded_send(event) .expect("Send disconnected event failed"); self.discovery_senders.remove(&session.id); - debug!("protocol [discovery] close on session [{}]", session.id); + debug!(target: "network", "protocol [discovery] close on session [{}]", session.id); } fn received( @@ -125,17 +126,17 @@ impl ServiceProtocol for DiscoveryProtocol { session: &SessionContext, data: bytes::Bytes, ) { - debug!("[received message]: length={}", data.len()); + debug!(target: "network", "[received message]: length={}", data.len()); if let Some(ref mut sender) = self.discovery_senders.get_mut(&session.id) { // TODO: handle channel is full (wait for poll API?) if let Err(err) = sender.try_send(data.to_vec()) { if err.is_full() { - warn!("channel is full"); + warn!(target: "network", "channel is full"); } else if err.is_disconnected() { - warn!("channel is disconnected"); + warn!(target: "network", "channel is disconnected"); } else { - warn!("other channel error: {:?}", err); + warn!(target: "network", "other channel error: {:?}", err); } } } From 1b607ebc51d0416b3d73736b91b01c325704fa06 Mon Sep 17 00:00:00 2001 From: Linfeng Qian Date: Tue, 19 Mar 2019 10:47:50 +0800 Subject: [PATCH 43/99] fix: Handle channel errors --- network/src/service/discovery_service.rs | 46 ++++++++++++++---------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/network/src/service/discovery_service.rs b/network/src/service/discovery_service.rs index 869dc2f546..10dc4b05ed 100644 --- a/network/src/service/discovery_service.rs +++ b/network/src/service/discovery_service.rs @@ -78,10 +78,10 @@ impl ServiceProtocol for DiscoveryProtocol { session_id: session.id, peer_id: session.remote_pubkey.clone().map(|pubkey| pubkey.peer_id()), }; - // TODO: handle send failed - self.event_sender - .unbounded_send(event) - .expect("Send connected event failed"); + if self.event_sender.unbounded_send(event).is_err() { + warn!(target: "network", "receiver maybe dropped!"); + return; + } let direction = if session.ty == SessionType::Server { Direction::Inbound @@ -112,10 +112,10 @@ impl ServiceProtocol for DiscoveryProtocol { fn disconnected(&mut self, _control: &mut ServiceContext, session: &SessionContext) { let event = DiscoveryEvent::Disconnected(session.id); - // TODO: handle send failed - self.event_sender - .unbounded_send(event) - .expect("Send disconnected event failed"); + if self.event_sender.unbounded_send(event).is_err() { + warn!(target: "network", "receiver maybe dropped!"); + return; + } self.discovery_senders.remove(&session.id); debug!(target: "network", "protocol [discovery] close on session [{}]", session.id); } @@ -252,13 +252,17 @@ impl AddressManager for DiscoveryAddressManager { fn add_new_addr(&mut self, session_id: SessionId, addr: Multiaddr) { // FIXME: what if send failed let event = DiscoveryEvent::AddNewAddr { session_id, addr }; - self.event_sender.unbounded_send(event).unwrap(); + if self.event_sender.unbounded_send(event).is_err() { + warn!(target: "network", "receiver maybe dropped!"); + } } fn add_new_addrs(&mut self, session_id: SessionId, addrs: Vec) { // FIXME: what if send failed let event = DiscoveryEvent::AddNewAddrs { session_id, addrs }; - self.event_sender.unbounded_send(event).unwrap(); + if self.event_sender.unbounded_send(event).is_err() { + warn!(target: "network", "receiver maybe dropped!"); + } } fn misbehave(&mut self, session_id: SessionId, kind: Misbehavior) -> MisbehaveResult { @@ -269,18 +273,24 @@ impl AddressManager for DiscoveryAddressManager { kind, result: sender, }; - self.event_sender.unbounded_send(event).unwrap(); - // FIXME: what if receive failed - receiver.wait().unwrap() + if self.event_sender.unbounded_send(event).is_err() { + warn!(target: "network", "receiver maybe dropped!"); + MisbehaveResult::Disconnect + } else { + // FIXME: what if receive failed + receiver.wait().unwrap_or(MisbehaveResult::Disconnect) + } } fn get_random(&mut self, n: usize) -> Vec { let (sender, receiver) = oneshot::channel(); // FIXME: what if send failed - self.event_sender - .unbounded_send(DiscoveryEvent::GetRandom { n, result: sender }) - .unwrap(); - // FIXME: what if receive failed - receiver.wait().unwrap() + let event = DiscoveryEvent::GetRandom { n, result: sender }; + if self.event_sender.unbounded_send(event).is_err() { + warn!(target: "network", "receiver maybe dropped!"); + Vec::new() + } else { + receiver.wait().ok().unwrap_or_else(Vec::new) + } } } From d662898c89b62fa28a5e6e0bb66113b778de8668 Mon Sep 17 00:00:00 2001 From: Linfeng Qian Date: Tue, 19 Mar 2019 10:51:11 +0800 Subject: [PATCH 44/99] fix: Use change p2p dependency from branch to reversion --- Cargo.lock | 36 ++++++++++++++++++------------------ network/Cargo.toml | 8 ++++---- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8422850c5c..fa3e9efaa4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -483,10 +483,10 @@ dependencies = [ "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "snap 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)", - "tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)", - "tentacle-discovery 0.1.0 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)", - "tentacle-ping 0.2.0 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)", - "tentacle-secio 0.1.0 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)", + "tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", + "tentacle-discovery 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", + "tentacle-ping 0.2.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", + "tentacle-secio 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "unsigned-varint 0.2.2 (git+https://github.com/paritytech/unsigned-varint)", ] @@ -2556,23 +2556,23 @@ dependencies = [ [[package]] name = "tentacle" version = "0.2.0-alpha.1" -source = "git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format#9c42be05724c3e2decb73716d0f515c36a7af1cb" +source = "git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb#9c42be05724c3e2decb73716d0f515c36a7af1cb" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "parity-multiaddr 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tentacle-secio 0.1.0 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)", + "tentacle-secio 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-threadpool 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-yamux 0.1.4 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)", + "tokio-yamux 0.1.4 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", ] [[package]] name = "tentacle-discovery" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format#9c42be05724c3e2decb73716d0f515c36a7af1cb" +source = "git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb#9c42be05724c3e2decb73716d0f515c36a7af1cb" dependencies = [ "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2583,7 +2583,7 @@ dependencies = [ "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", - "tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)", + "tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "trust-dns 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2591,20 +2591,20 @@ dependencies = [ [[package]] name = "tentacle-ping" version = "0.2.0" -source = "git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format#9c42be05724c3e2decb73716d0f515c36a7af1cb" +source = "git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb#9c42be05724c3e2decb73716d0f515c36a7af1cb" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "generic-channel 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)", + "tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", ] [[package]] name = "tentacle-secio" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format#9c42be05724c3e2decb73716d0f515c36a7af1cb" +source = "git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb#9c42be05724c3e2decb73716d0f515c36a7af1cb" dependencies = [ "aes-ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "bs58 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2917,7 +2917,7 @@ dependencies = [ [[package]] name = "tokio-yamux" version = "0.1.4" -source = "git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format#9c42be05724c3e2decb73716d0f515c36a7af1cb" +source = "git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb#9c42be05724c3e2decb73716d0f515c36a7af1cb" dependencies = [ "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3463,10 +3463,10 @@ dependencies = [ "checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015" "checksum take 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b157868d8ac1f56b64604539990685fa7611d8fa9e5476cf0c02cf34d32917c5" "checksum tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "b86c784c88d98c801132806dadd3819ed29d8600836c4088e855cdf3e178ed8a" -"checksum tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)" = "" -"checksum tentacle-discovery 0.1.0 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)" = "" -"checksum tentacle-ping 0.2.0 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)" = "" -"checksum tentacle-secio 0.1.0 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)" = "" +"checksum tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)" = "" +"checksum tentacle-discovery 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)" = "" +"checksum tentacle-ping 0.2.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)" = "" +"checksum tentacle-secio 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)" = "" "checksum termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4096add70612622289f2fdcdbd5086dc81c1e2675e6ae58d6c4f62a16c6d7f2f" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum termios 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "72b620c5ea021d75a735c943269bb07d30c9b77d6ac6b236bc8b5c496ef05625" @@ -3492,7 +3492,7 @@ dependencies = [ "checksum tokio-trace-core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "350c9edade9830dc185ae48ba45667a445ab59f6167ef6d0254ec9d2430d9dd3" "checksum tokio-udp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "66268575b80f4a4a710ef83d087fdfeeabdce9b74c797535fbac18a2cb906e92" "checksum tokio-uds 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "037ffc3ba0e12a0ab4aca92e5234e0dedeb48fddf6ccd260f1f150a36a9f2445" -"checksum tokio-yamux 0.1.4 (git+https://github.com/nervosnetwork/p2p?branch=discovery-use-multiaddr-format)" = "" +"checksum tokio-yamux 0.1.4 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)" = "" "checksum toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "758664fc71a3a69038656bee8b6be6477d2a6c315a6b81f7081f591bffa4111f" "checksum toml 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "87c5890a989fa47ecdc7bcb4c63a77a82c18f306714104b1decfd722db17b39e" "checksum trust-dns 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)" = "65096825b064877da37eeeb9a83390bd23433eabfc503a6476dc5b1949034aa7" diff --git a/network/Cargo.toml b/network/Cargo.toml index 6ca30e8341..a889e38c34 100644 --- a/network/Cargo.toml +++ b/network/Cargo.toml @@ -24,10 +24,10 @@ snap = "0.2" # secio = { version = "0.1.0", package="tentacle-secio" } # p2p-ping = { version = "0.2.0", package="tentacle-ping" } # p2p-discovery = { git = "https://github.com/nervosnetwork/p2p", branch="discovery-use-multiaddr-format", package="tentacle-discovery" } -p2p = { git = "https://github.com/nervosnetwork/p2p", branch="discovery-use-multiaddr-format", package="tentacle" } -secio = { git = "https://github.com/nervosnetwork/p2p", branch="discovery-use-multiaddr-format", package="tentacle-secio" } -p2p-ping = { git = "https://github.com/nervosnetwork/p2p", branch="discovery-use-multiaddr-format", package="tentacle-ping" } -p2p-discovery = { git = "https://github.com/nervosnetwork/p2p", branch="discovery-use-multiaddr-format", package="tentacle-discovery" } +p2p = { git = "https://github.com/nervosnetwork/p2p", rev="9c42be05724c3e2decb73716d0f515c36a7af1cb", package="tentacle" } +secio = { git = "https://github.com/nervosnetwork/p2p", rev="9c42be05724c3e2decb73716d0f515c36a7af1cb", package="tentacle-secio" } +p2p-ping = { git = "https://github.com/nervosnetwork/p2p", rev="9c42be05724c3e2decb73716d0f515c36a7af1cb", package="tentacle-ping" } +p2p-discovery = { git = "https://github.com/nervosnetwork/p2p", rev="9c42be05724c3e2decb73716d0f515c36a7af1cb", package="tentacle-discovery" } faketime = "0.2.0" rusqlite = {version = "0.16.0", features = ["bundled"]} lazy_static = "1.3.0" From ffaf8257ebdb1bb42c2ea71ce0bc7737f50fb36f Mon Sep 17 00:00:00 2001 From: Linfeng Qian Date: Tue, 19 Mar 2019 11:31:25 +0800 Subject: [PATCH 45/99] chore: Remove outdated FIXME mark --- network/src/service/discovery_service.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/network/src/service/discovery_service.rs b/network/src/service/discovery_service.rs index 10dc4b05ed..7a872a9321 100644 --- a/network/src/service/discovery_service.rs +++ b/network/src/service/discovery_service.rs @@ -250,7 +250,6 @@ pub struct DiscoveryAddressManager { impl AddressManager for DiscoveryAddressManager { fn add_new_addr(&mut self, session_id: SessionId, addr: Multiaddr) { - // FIXME: what if send failed let event = DiscoveryEvent::AddNewAddr { session_id, addr }; if self.event_sender.unbounded_send(event).is_err() { warn!(target: "network", "receiver maybe dropped!"); @@ -258,7 +257,6 @@ impl AddressManager for DiscoveryAddressManager { } fn add_new_addrs(&mut self, session_id: SessionId, addrs: Vec) { - // FIXME: what if send failed let event = DiscoveryEvent::AddNewAddrs { session_id, addrs }; if self.event_sender.unbounded_send(event).is_err() { warn!(target: "network", "receiver maybe dropped!"); @@ -267,7 +265,6 @@ impl AddressManager for DiscoveryAddressManager { fn misbehave(&mut self, session_id: SessionId, kind: Misbehavior) -> MisbehaveResult { let (sender, receiver) = oneshot::channel(); - // FIXME: what if send failed let event = DiscoveryEvent::Misbehave { session_id, kind, @@ -277,14 +274,12 @@ impl AddressManager for DiscoveryAddressManager { warn!(target: "network", "receiver maybe dropped!"); MisbehaveResult::Disconnect } else { - // FIXME: what if receive failed receiver.wait().unwrap_or(MisbehaveResult::Disconnect) } } fn get_random(&mut self, n: usize) -> Vec { let (sender, receiver) = oneshot::channel(); - // FIXME: what if send failed let event = DiscoveryEvent::GetRandom { n, result: sender }; if self.event_sender.unbounded_send(event).is_err() { warn!(target: "network", "receiver maybe dropped!"); From 4a27aaf9482587f1921e8a74fda940f679764478 Mon Sep 17 00:00:00 2001 From: Linfeng Qian Date: Tue, 19 Mar 2019 15:25:02 +0800 Subject: [PATCH 46/99] feat: Add identify protocol --- Cargo.lock | 14 +++ network/Cargo.toml | 1 + network/src/network.rs | 68 ++++++++----- network/src/network_service.rs | 5 +- network/src/service.rs | 1 + network/src/service/identify_service.rs | 121 ++++++++++++++++++++++++ 6 files changed, 184 insertions(+), 26 deletions(-) create mode 100644 network/src/service/identify_service.rs diff --git a/Cargo.lock b/Cargo.lock index fa3e9efaa4..d6878be97a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -485,6 +485,7 @@ dependencies = [ "tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)", "tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", "tentacle-discovery 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", + "tentacle-identify 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", "tentacle-ping 0.2.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", "tentacle-secio 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2588,6 +2589,18 @@ dependencies = [ "trust-dns 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "tentacle-identify" +version = "0.1.0" +source = "git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb#9c42be05724c3e2decb73716d0f515c36a7af1cb" +dependencies = [ + "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", + "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", + "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tentacle-ping" version = "0.2.0" @@ -3465,6 +3478,7 @@ dependencies = [ "checksum tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "b86c784c88d98c801132806dadd3819ed29d8600836c4088e855cdf3e178ed8a" "checksum tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)" = "" "checksum tentacle-discovery 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)" = "" +"checksum tentacle-identify 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)" = "" "checksum tentacle-ping 0.2.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)" = "" "checksum tentacle-secio 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)" = "" "checksum termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4096add70612622289f2fdcdbd5086dc81c1e2675e6ae58d6c4f62a16c6d7f2f" diff --git a/network/Cargo.toml b/network/Cargo.toml index a889e38c34..d44f936757 100644 --- a/network/Cargo.toml +++ b/network/Cargo.toml @@ -28,6 +28,7 @@ p2p = { git = "https://github.com/nervosnetwork/p2p", rev="9c42be05724c3e2decb73 secio = { git = "https://github.com/nervosnetwork/p2p", rev="9c42be05724c3e2decb73716d0f515c36a7af1cb", package="tentacle-secio" } p2p-ping = { git = "https://github.com/nervosnetwork/p2p", rev="9c42be05724c3e2decb73716d0f515c36a7af1cb", package="tentacle-ping" } p2p-discovery = { git = "https://github.com/nervosnetwork/p2p", rev="9c42be05724c3e2decb73716d0f515c36a7af1cb", package="tentacle-discovery" } +p2p-identify = { git = "https://github.com/nervosnetwork/p2p", rev="9c42be05724c3e2decb73716d0f515c36a7af1cb", package="tentacle-identify" } faketime = "0.2.0" rusqlite = {version = "0.16.0", features = ["bundled"]} lazy_static = "1.3.0" diff --git a/network/src/network.rs b/network/src/network.rs index 4f191e1d50..5a503017e4 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -8,6 +8,7 @@ use crate::protocol_handler::{CKBProtocolHandler, DefaultCKBProtocolContext}; use crate::service::{ ckb_service::CKBService, discovery_service::{DiscoveryEvent, DiscoveryProtocol, DiscoveryService}, + identify_service::{IdentifyAddressManager, IdentifyEvent, IdentifyProtocol, IdentifyService}, outbound_peer_service::OutboundPeerService, ping_service::PingService, timer_service::{TimerRegistry, TimerService}, @@ -44,6 +45,7 @@ use std::usize; const PING_PROTOCOL_ID: ProtocolId = 0; const DISCOVERY_PROTOCOL_ID: ProtocolId = 1; +const IDENTIFY_PROTOCOL_ID: ProtocolId = 2; pub type CKBProtocols = Vec<(CKBProtocol, Arc)>; type NetworkResult = Result< @@ -82,6 +84,12 @@ impl PeerInfo { } } +pub struct EventReceivers { + pub ping_event_receiver: Receiver, + pub disc_event_receiver: mpsc::UnboundedReceiver, + pub identify_event_receiver: mpsc::UnboundedReceiver, +} + type P2PService = Service; pub struct Network { @@ -340,16 +348,7 @@ impl Network { pub(crate) fn inner_build( config: &NetworkConfig, ckb_protocols: CKBProtocols, - ) -> Result< - ( - Arc, - P2PService, - TimerRegistry, - Receiver, - mpsc::UnboundedReceiver, - ), - Error, - > { + ) -> Result<(Arc, P2PService, TimerRegistry, EventReceivers), Error> { let local_private_key = match config.fetch_private_key() { Some(private_key) => private_key?, None => return Err(ConfigError::InvalidKey.into()), @@ -380,7 +379,6 @@ impl Network { config.reserved_only, reserved_peers, ); - let mut p2p_service = ServiceBuilder::default().forever(true); // register protocols let (ping_sender, ping_receiver) = channel(std::u8::MAX as usize); let ping_meta = MetaBuilder::default() @@ -394,7 +392,6 @@ impl Network { ))) }) .build(); - p2p_service = p2p_service.insert_protocol(ping_meta); let (disc_sender, disc_receiver) = mpsc::unbounded(); let disc_meta = MetaBuilder::default() @@ -406,8 +403,24 @@ impl Network { ))) }) .build(); - p2p_service = p2p_service.insert_protocol(disc_meta); + let (identify_sender, identify_receiver) = mpsc::unbounded(); + let identify_addr_mgr = IdentifyAddressManager::new(identify_sender.clone()); + let identify_meta = MetaBuilder::default() + .id(IDENTIFY_PROTOCOL_ID) + .service_handle(move || { + ProtocolHandle::Callback(Box::new(IdentifyProtocol::new( + IDENTIFY_PROTOCOL_ID, + identify_addr_mgr.clone(), + ))) + }) + .build(); + + let mut p2p_service = ServiceBuilder::default() + .forever(true) + .insert_protocol(ping_meta) + .insert_protocol(disc_meta) + .insert_protocol(identify_meta); for (ckb_protocol, _) in &ckb_protocols { p2p_service = p2p_service.insert_protocol(ckb_protocol.build()); } @@ -487,8 +500,11 @@ impl Network { network, p2p_service, timer_registry, - ping_receiver, - disc_receiver, + EventReceivers { + ping_event_receiver: ping_receiver, + disc_event_receiver: disc_receiver, + identify_event_receiver: identify_receiver, + }, )) } @@ -499,19 +515,21 @@ impl Network { p2p_service: P2PService, timer_registry: TimerRegistry, ckb_event_receiver: Receiver, - ping_event_receiver: Receiver, - disc_event_receiver: mpsc::UnboundedReceiver, + receivers: EventReceivers, ) -> Result + Send>, Error> { // initialize ckb_protocols let ping_service = PingService { network: Arc::clone(&network), - event_receiver: ping_event_receiver, + event_receiver: receivers.ping_event_receiver, }; - let disc_service = DiscoveryService::new(Arc::clone(&network), disc_event_receiver); + let disc_service = + DiscoveryService::new(Arc::clone(&network), receivers.disc_event_receiver); + let identify_service = + IdentifyService::new(Arc::clone(&network), receivers.identify_event_receiver); let ckb_service = CKBService { - event_receiver: ckb_event_receiver, network: Arc::clone(&network), + event_receiver: ckb_event_receiver, }; let timer_service = TimerService::new(timer_registry, Arc::clone(&network)); let outbound_peer_service = @@ -538,6 +556,11 @@ impl Network { .for_each(|_| Ok(())) .map_err(|_err| Error::Shutdown), ), + Box::new( + identify_service + .for_each(|_| Ok(())) + .map_err(|_err| Error::Shutdown), + ), Box::new(timer_service.timer_futures.for_each(|_| Ok(()))), Box::new( outbound_peer_service @@ -569,7 +592,7 @@ impl Network { ckb_protocols: CKBProtocols, ckb_event_receiver: Receiver, ) -> NetworkResult { - let (network, p2p_service, timer_registry, ping_event_receiver, disc_event_receiver) = + let (network, p2p_service, timer_registry, receivers) = Self::inner_build(config, ckb_protocols)?; let (close_tx, close_rx) = oneshot::channel(); let network_future = Self::build_network_future( @@ -579,8 +602,7 @@ impl Network { p2p_service, timer_registry, ckb_event_receiver, - ping_event_receiver, - disc_event_receiver, + receivers, )?; Ok((network, close_tx, network_future)) } diff --git a/network/src/network_service.rs b/network/src/network_service.rs index 56a8535b91..afbdfd79f4 100644 --- a/network/src/network_service.rs +++ b/network/src/network_service.rs @@ -67,7 +67,7 @@ impl NetworkService { ckb_protocols: CKBProtocols, ckb_event_receiver: Receiver, ) -> Result { - let (network, p2p_service, timer_registry, ping_event_receiver, disc_event_receiver) = + let (network, p2p_service, timer_registry, receivers) = Network::inner_build(config, ckb_protocols)?; let (close_tx, close_rx) = oneshot::channel(); let (init_tx, init_rx) = oneshot::channel(); @@ -87,8 +87,7 @@ impl NetworkService { p2p_service, timer_registry, ckb_event_receiver, - ping_event_receiver, - disc_event_receiver, + receivers, ) .expect("Network thread init"); init_tx.send(()).expect("Network init signal send"); diff --git a/network/src/service.rs b/network/src/service.rs index a815acd24e..9dd961cdfe 100644 --- a/network/src/service.rs +++ b/network/src/service.rs @@ -1,5 +1,6 @@ pub mod ckb_service; pub mod discovery_service; +pub mod identify_service; pub mod outbound_peer_service; pub mod ping_service; pub mod timer_service; diff --git a/network/src/service/identify_service.rs b/network/src/service/identify_service.rs new file mode 100644 index 0000000000..8155e6d1f4 --- /dev/null +++ b/network/src/service/identify_service.rs @@ -0,0 +1,121 @@ +// use crate::peer_store::Behaviour; +use crate::Network; +use futures::{sync::mpsc, sync::oneshot, Async, Future, Stream}; +use log::{debug, warn}; +use std::sync::Arc; + +use p2p::{multiaddr::Multiaddr, secio::PeerId}; + +pub use p2p_identify::IdentifyProtocol; +use p2p_identify::{AddrManager, MisbehaveResult, Misbehavior}; + +#[derive(Clone)] +pub(crate) struct IdentifyAddressManager { + event_sender: mpsc::UnboundedSender, +} + +impl IdentifyAddressManager { + pub(crate) fn new( + event_sender: mpsc::UnboundedSender, + ) -> IdentifyAddressManager { + IdentifyAddressManager { event_sender } + } +} + +impl AddrManager for IdentifyAddressManager { + fn add_listen_addrs(&mut self, peer_id: &PeerId, addrs: Vec) { + let event = IdentifyEvent::AddListenAddrs { + peer_id: peer_id.clone(), + addrs, + }; + if self.event_sender.unbounded_send(event).is_err() { + warn!(target: "network", "receiver maybe dropped!"); + } + } + + fn add_observed_addr(&mut self, peer_id: &PeerId, addr: Multiaddr) -> MisbehaveResult { + let event = IdentifyEvent::AddObservedAddr { + peer_id: peer_id.clone(), + addr, + }; + if self.event_sender.unbounded_send(event).is_err() { + warn!(target: "network", "receiver maybe dropped!"); + } + // NOTE: for future usage + MisbehaveResult::Continue + } + + fn misbehave(&mut self, peer_id: &PeerId, kind: Misbehavior) -> MisbehaveResult { + let (sender, receiver) = oneshot::channel(); + let event = IdentifyEvent::Misbehave { + peer_id: peer_id.clone(), + kind, + result: sender, + }; + if self.event_sender.unbounded_send(event).is_err() { + warn!(target: "network", "receiver maybe dropped!"); + MisbehaveResult::Disconnect + } else { + receiver.wait().unwrap_or(MisbehaveResult::Disconnect) + } + } +} + +pub enum IdentifyEvent { + AddListenAddrs { + peer_id: PeerId, + addrs: Vec, + }, + AddObservedAddr { + peer_id: PeerId, + addr: Multiaddr, + }, + Misbehave { + peer_id: PeerId, + kind: Misbehavior, + result: oneshot::Sender, + }, +} + +pub(crate) struct IdentifyService { + event_receiver: mpsc::UnboundedReceiver, + network: Arc, +} + +impl IdentifyService { + pub(crate) fn new( + network: Arc, + event_receiver: mpsc::UnboundedReceiver, + ) -> IdentifyService { + IdentifyService { + event_receiver, + network, + } + } +} + +impl Stream for IdentifyService { + type Item = (); + type Error = (); + fn poll(&mut self) -> Result>, Self::Error> { + match try_ready!(self.event_receiver.poll()) { + Some(IdentifyEvent::AddListenAddrs { .. }) => { + // TODO: how to transform those addresses + } + Some(IdentifyEvent::AddObservedAddr { .. }) => { + // TODO: how to transform this address + } + Some(IdentifyEvent::Misbehave { result, .. }) => { + // TODO: report misbehave + if result.send(MisbehaveResult::Continue).is_err() { + return Err(()); + } + } + None => { + debug!(target: "network", "identify service shutdown"); + return Ok(Async::Ready(None)); + } + } + Ok(Async::Ready(Some(()))) + } +} From bc4260ec8f0d98eacd48a51b101b39c0991942ef Mon Sep 17 00:00:00 2001 From: Linfeng Qian Date: Wed, 20 Mar 2019 11:17:11 +0800 Subject: [PATCH 47/99] fix: Add observed address to peer store --- network/Cargo.toml | 4 --- network/src/service/identify_service.rs | 40 +++++++++++++++++++++---- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/network/Cargo.toml b/network/Cargo.toml index d44f936757..1abab69d32 100644 --- a/network/Cargo.toml +++ b/network/Cargo.toml @@ -20,10 +20,6 @@ bytes = "0.4.12" tokio = "0.1.17" futures = { version = "0.1.19", features = ["use_std"] } snap = "0.2" -# p2p = { version = "0.2.0-alpha.1", package="tentacle" } -# secio = { version = "0.1.0", package="tentacle-secio" } -# p2p-ping = { version = "0.2.0", package="tentacle-ping" } -# p2p-discovery = { git = "https://github.com/nervosnetwork/p2p", branch="discovery-use-multiaddr-format", package="tentacle-discovery" } p2p = { git = "https://github.com/nervosnetwork/p2p", rev="9c42be05724c3e2decb73716d0f515c36a7af1cb", package="tentacle" } secio = { git = "https://github.com/nervosnetwork/p2p", rev="9c42be05724c3e2decb73716d0f515c36a7af1cb", package="tentacle-secio" } p2p-ping = { git = "https://github.com/nervosnetwork/p2p", rev="9c42be05724c3e2decb73716d0f515c36a7af1cb", package="tentacle-ping" } diff --git a/network/src/service/identify_service.rs b/network/src/service/identify_service.rs index 8155e6d1f4..a3891ea231 100644 --- a/network/src/service/identify_service.rs +++ b/network/src/service/identify_service.rs @@ -2,9 +2,14 @@ use crate::Network; use futures::{sync::mpsc, sync::oneshot, Async, Future, Stream}; use log::{debug, warn}; +use std::collections::HashMap; use std::sync::Arc; -use p2p::{multiaddr::Multiaddr, secio::PeerId}; +use p2p::{ + multiaddr::{Multiaddr, Protocol}, + secio::PeerId, + utils::multiaddr_to_socketaddr, +}; pub use p2p_identify::IdentifyProtocol; use p2p_identify::{AddrManager, MisbehaveResult, Misbehavior}; @@ -80,6 +85,7 @@ pub enum IdentifyEvent { pub(crate) struct IdentifyService { event_receiver: mpsc::UnboundedReceiver, network: Arc, + listen_addrs: HashMap>, } impl IdentifyService { @@ -90,6 +96,7 @@ impl IdentifyService { IdentifyService { event_receiver, network, + listen_addrs: HashMap::default(), } } } @@ -99,11 +106,34 @@ impl Stream for IdentifyService { type Error = (); fn poll(&mut self) -> Result>, Self::Error> { match try_ready!(self.event_receiver.poll()) { - Some(IdentifyEvent::AddListenAddrs { .. }) => { - // TODO: how to transform those addresses + Some(IdentifyEvent::AddListenAddrs { peer_id, addrs }) => { + self.listen_addrs.insert(peer_id, addrs); } - Some(IdentifyEvent::AddObservedAddr { .. }) => { - // TODO: how to transform this address + Some(IdentifyEvent::AddObservedAddr { peer_id, addr }) => { + // TODO: how to use listen addresses + if let Some(addr) = self + .listen_addrs + .get(&peer_id) + .map(|addrs| addrs.iter().next()) + .unwrap_or(None) + .map(|addr| multiaddr_to_socketaddr(addr)) + .unwrap_or(None) + .map(|socket_addr| socket_addr.port()) + .map(move |port| { + addr.into_iter() + .map(|proto| match proto { + Protocol::Tcp(_) => Protocol::Tcp(port), + value => value, + }) + .collect() + }) + { + let _ = self + .network + .peer_store() + .write() + .add_discovered_address(&peer_id, addr); + } } Some(IdentifyEvent::Misbehave { result, .. }) => { // TODO: report misbehave From 9492854748764df0d6c496ebf7c25d6e0658ae4f Mon Sep 17 00:00:00 2001 From: Linfeng Qian Date: Wed, 20 Mar 2019 13:25:20 +0800 Subject: [PATCH 48/99] fix: Parse protocol version bug --- network/src/protocol.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/network/src/protocol.rs b/network/src/protocol.rs index 4029f77f9d..9372bc3e9f 100644 --- a/network/src/protocol.rs +++ b/network/src/protocol.rs @@ -92,8 +92,12 @@ impl ServiceProtocol for CKBHandler { fn init(&mut self, _control: &mut ServiceContext) {} fn connected(&mut self, control: &mut ServiceContext, session: &SessionContext, version: &str) { let (peer_id, version) = { - let parsed_version = version.parse::(); - if session.remote_pubkey.is_none() || parsed_version.is_err() { + // FIXME: version number should be discussed. + let parsed_version = version + .split(".") + .last() + .and_then(|v| v.parse::().ok()); + if session.remote_pubkey.is_none() || parsed_version.is_none() { error!(target: "network", "ckb protocol connected error, addr: {}, protocol:{}, version: {}", session.address, self.id, version); control.disconnect(session.id); return; From cbae925d261a52708e97494c1c73a3134a1cbaed Mon Sep 17 00:00:00 2001 From: Linfeng Qian Date: Wed, 20 Mar 2019 15:27:40 +0800 Subject: [PATCH 49/99] test: Fix build test (network API change) --- network/src/protocol.rs | 5 +---- sync/src/tests/mod.rs | 3 ++- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/network/src/protocol.rs b/network/src/protocol.rs index 9372bc3e9f..b2c730a83c 100644 --- a/network/src/protocol.rs +++ b/network/src/protocol.rs @@ -93,10 +93,7 @@ impl ServiceProtocol for CKBHandler { fn connected(&mut self, control: &mut ServiceContext, session: &SessionContext, version: &str) { let (peer_id, version) = { // FIXME: version number should be discussed. - let parsed_version = version - .split(".") - .last() - .and_then(|v| v.parse::().ok()); + let parsed_version = version.split('.').last().and_then(|v| v.parse::().ok()); if session.remote_pubkey.is_none() || parsed_version.is_none() { error!(target: "network", "ckb protocol connected error, addr: {}, protocol:{}, version: {}", session.address, self.id, version); control.disconnect(session.id); diff --git a/sync/src/tests/mod.rs b/sync/src/tests/mod.rs index 5f80c262d6..32a2c8ff2e 100644 --- a/sync/src/tests/mod.rs +++ b/sync/src/tests/mod.rs @@ -1,3 +1,4 @@ +use bytes::Bytes; use ckb_network::{ errors::Error as NetworkError, CKBProtocolContext, CKBProtocolHandler, PeerIndex, ProtocolId, SessionInfo, Severity, TimerToken, @@ -101,7 +102,7 @@ impl TestNode { timer_senders: self.timer_senders.clone(), }), *peer, - payload.clone(), + Bytes::from(payload.clone()), ) }; From 15b69b0d8804bdadd963c0ecc9be928e801fd858 Mon Sep 17 00:00:00 2001 From: Linfeng Qian Date: Thu, 21 Mar 2019 11:44:20 +0800 Subject: [PATCH 50/99] fix: Outbound connection select address bug --- network/src/network.rs | 5 ++--- network/src/service/discovery_service.rs | 20 +++++++++++++------- network/src/service/identify_service.rs | 6 ++---- network/src/service/outbound_peer_service.rs | 20 ++++++++++---------- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/network/src/network.rs b/network/src/network.rs index 5a503017e4..29fd8b93ec 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -328,15 +328,14 @@ impl Network { } } - pub fn dial(&self, expected_peer_id: &PeerId, mut addr: Multiaddr) { + pub fn dial(&self, expected_peer_id: &PeerId, addr: Multiaddr) { if expected_peer_id == self.local_peer_id() { debug!(target: "network", "ignore dial to self"); return; } debug!(target: "network", "dial to peer {:?} address {:?}", expected_peer_id, addr); match Multihash::from_bytes(expected_peer_id.as_bytes().to_vec()) { - Ok(peer_id_hash) => { - addr.append(multiaddr::Protocol::P2p(peer_id_hash)); + Ok(_peer_id_hash) => { self.dial_addr(addr); } Err(err) => { diff --git a/network/src/service/discovery_service.rs b/network/src/service/discovery_service.rs index 7a872a9321..fbcef16c40 100644 --- a/network/src/service/discovery_service.rs +++ b/network/src/service/discovery_service.rs @@ -10,6 +10,7 @@ use p2p::{ multiaddr::Multiaddr, secio::PeerId, traits::ServiceProtocol, + utils::extract_peer_id, yamux::session::SessionType, ProtocolId, SessionId, }; @@ -207,12 +208,17 @@ impl Stream for DiscoveryService { // NOTE: ignore add new addr message, handle this in identify protocol } Some(DiscoveryEvent::AddNewAddrs { session_id, addrs }) => { - if let Some(peer_id) = self.sessions.get(&session_id) { - let _ = self - .network - .peer_store() - .write() - .add_discovered_addresses(peer_id, addrs); + if let Some(_peer_id) = self.sessions.get(&session_id) { + // TODO: wait for peer store update + for addr in addrs.into_iter() { + if let Some(peer_id) = extract_peer_id(&addr) { + let _ = self + .network + .peer_store() + .write() + .add_discovered_address(&peer_id, addr); + } + } } } Some(DiscoveryEvent::Misbehave { @@ -227,7 +233,7 @@ impl Stream for DiscoveryService { .network .peer_store() .read() - .peers_to_attempt(n as u32) + .random_peers(n as u32) .into_iter() .map(|(_peer_id, addr)| addr) .collect(); diff --git a/network/src/service/identify_service.rs b/network/src/service/identify_service.rs index a3891ea231..04bde659d1 100644 --- a/network/src/service/identify_service.rs +++ b/network/src/service/identify_service.rs @@ -114,10 +114,8 @@ impl Stream for IdentifyService { if let Some(addr) = self .listen_addrs .get(&peer_id) - .map(|addrs| addrs.iter().next()) - .unwrap_or(None) - .map(|addr| multiaddr_to_socketaddr(addr)) - .unwrap_or(None) + .and_then(|addrs| addrs.iter().next()) + .and_then(|addr| multiaddr_to_socketaddr(addr)) .map(|socket_addr| socket_addr.port()) .map(move |port| { addr.into_iter() diff --git a/network/src/service/outbound_peer_service.rs b/network/src/service/outbound_peer_service.rs index 4f3906ff7f..b068cfc4a6 100644 --- a/network/src/service/outbound_peer_service.rs +++ b/network/src/service/outbound_peer_service.rs @@ -1,6 +1,6 @@ use crate::Network; use futures::{Async, Stream}; -use log::{debug, error}; +use log::{debug, warn}; use std::sync::Arc; use std::time::Duration; use std::usize; @@ -37,20 +37,20 @@ impl Stream for OutboundPeerService { .network .peer_store() .read() - .peers_to_attempt(new_outbound as u32); - for (peer_id, addr) in attempt_peers.iter().filter_map(|(peer_id, addr)| { - if self.network.local_peer_id() != peer_id { - Some((peer_id.clone(), addr.clone())) - } else { - None - } - }) { + .random_peers(new_outbound as u32) + .into_iter() + .filter(|(peer_id, _addr)| { + self.network.get_peer_index(peer_id).is_none() + && self.network.local_peer_id() != peer_id + }) + .collect::>(); + for (peer_id, addr) in attempt_peers.into_iter() { self.network.dial(&peer_id, addr); } } } None => { - error!(target: "network", "ckb outbound peer service stopped"); + warn!(target: "network", "ckb outbound peer service stopped"); return Ok(Async::Ready(None)); } } From 7650a8f78068d005327608ef4ca9348a81b132a5 Mon Sep 17 00:00:00 2001 From: Linfeng Qian Date: Thu, 21 Mar 2019 11:44:38 +0800 Subject: [PATCH 51/99] fix: Can not shutdown network bug --- network/src/network_service.rs | 76 ++++++++++++++++++---------------- src/cli/run_impl.rs | 1 + 2 files changed, 41 insertions(+), 36 deletions(-) diff --git a/network/src/network_service.rs b/network/src/network_service.rs index afbdfd79f4..30bcb497d4 100644 --- a/network/src/network_service.rs +++ b/network/src/network_service.rs @@ -9,27 +9,34 @@ use ckb_util::Mutex; use futures::future::Future; use futures::sync::mpsc::Receiver; use futures::sync::oneshot; -use log::{debug, info}; +use log::{debug, error, info}; use std::sync::Arc; -use std::thread; use tokio::runtime; pub struct StopHandler { signal: oneshot::Sender<()>, - thread: thread::JoinHandle<()>, + network_runtime: runtime::Runtime, } impl StopHandler { - pub fn new(signal: oneshot::Sender<()>, thread: thread::JoinHandle<()>) -> StopHandler { - StopHandler { signal, thread } + pub fn new(signal: oneshot::Sender<()>, network_runtime: runtime::Runtime) -> StopHandler { + StopHandler { + signal, + network_runtime, + } } pub fn close(self) { - let StopHandler { signal, thread } = self; + let StopHandler { + signal, + network_runtime, + } = self; if let Err(e) = signal.send(()) { debug!(target: "network", "send shutdown signal error, ignoring error: {:?}", e) }; - thread.join().expect("join network_service thread"); + // TODO: not that gracefully shutdown, will output below error message: + // "terminate called after throwing an instance of 'std::system_error'" + network_runtime.shutdown_now(); } } @@ -71,38 +78,35 @@ impl NetworkService { Network::inner_build(config, ckb_protocols)?; let (close_tx, close_rx) = oneshot::channel(); let (init_tx, init_rx) = oneshot::channel(); - let join_handle = thread::spawn({ - let network = Arc::clone(&network); - let config = config.clone(); - move || { - info!( - target: "network", - "network peer_id {:?}", - network.local_public_key().peer_id() - ); - let network_future = Network::build_network_future( - network, - &config, - close_rx, - p2p_service, - timer_registry, - ckb_event_receiver, - receivers, - ) - .expect("Network thread init"); - init_tx.send(()).expect("Network init signal send"); - // here we use default config - let network_runtime = runtime::Runtime::new().expect("Network tokio runtime init");; - match network_runtime.block_on_all(network_future) { - Ok(_) => info!(target: "network", "network service exit"), - Err(err) => panic!("network service exit unexpected {}", err), - } - } - }); + + info!( + target: "network", + "network peer_id {:?}", + network.local_public_key().peer_id() + ); + let network_future = Network::build_network_future( + Arc::clone(&network), + &config, + close_rx, + p2p_service, + timer_registry, + ckb_event_receiver, + receivers, + ) + .expect("Network thread init"); + init_tx.send(()).expect("Network init signal send"); + // here we use default config + let mut network_runtime = runtime::Runtime::new().expect("Network tokio runtime init"); + network_runtime.spawn( + network_future + .map(|_| info!(target: "network", "network service exit")) + .map_err(|err| error!("network service exit unexpected {}", err)), + ); + init_rx.wait().map_err(|_err| Error::Shutdown)?; Ok(NetworkService { network, - stop_handler: Mutex::new(Some(StopHandler::new(close_tx, join_handle))), + stop_handler: Mutex::new(Some(StopHandler::new(close_tx, network_runtime))), }) } diff --git a/src/cli/run_impl.rs b/src/cli/run_impl.rs index bf3218659d..6c43493cc7 100644 --- a/src/cli/run_impl.rs +++ b/src/cli/run_impl.rs @@ -106,6 +106,7 @@ pub fn run(setup: Setup) { rpc_server.close(); info!(target: "main", "Jsonrpc shutdown"); + // FIXME: should gracefully shutdown network network.close(); info!(target: "main", "Network shutdown"); } From 3aa0bc878dbc39f752b3a3d26d2c049068b64115 Mon Sep 17 00:00:00 2001 From: Linfeng Qian Date: Thu, 21 Mar 2019 15:52:36 +0800 Subject: [PATCH 52/99] fix: Fix dial address with peer_id --- network/src/network.rs | 5 +++-- network/src/service/discovery_service.rs | 9 ++++++++- network/src/service/identify_service.rs | 8 +++++--- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/network/src/network.rs b/network/src/network.rs index 29fd8b93ec..5a503017e4 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -328,14 +328,15 @@ impl Network { } } - pub fn dial(&self, expected_peer_id: &PeerId, addr: Multiaddr) { + pub fn dial(&self, expected_peer_id: &PeerId, mut addr: Multiaddr) { if expected_peer_id == self.local_peer_id() { debug!(target: "network", "ignore dial to self"); return; } debug!(target: "network", "dial to peer {:?} address {:?}", expected_peer_id, addr); match Multihash::from_bytes(expected_peer_id.as_bytes().to_vec()) { - Ok(_peer_id_hash) => { + Ok(peer_id_hash) => { + addr.append(multiaddr::Protocol::P2p(peer_id_hash)); self.dial_addr(addr); } Err(err) => { diff --git a/network/src/service/discovery_service.rs b/network/src/service/discovery_service.rs index fbcef16c40..1e17b52e44 100644 --- a/network/src/service/discovery_service.rs +++ b/network/src/service/discovery_service.rs @@ -7,7 +7,7 @@ use std::sync::Arc; use p2p::{ context::{ServiceContext, SessionContext}, - multiaddr::Multiaddr, + multiaddr::{Multiaddr, Protocol}, secio::PeerId, traits::ServiceProtocol, utils::extract_peer_id, @@ -212,6 +212,13 @@ impl Stream for DiscoveryService { // TODO: wait for peer store update for addr in addrs.into_iter() { if let Some(peer_id) = extract_peer_id(&addr) { + let addr = addr + .into_iter() + .filter(|proto| match proto { + Protocol::P2p(_) => false, + _ => true, + }) + .collect::(); let _ = self .network .peer_store() diff --git a/network/src/service/identify_service.rs b/network/src/service/identify_service.rs index 04bde659d1..60505af9da 100644 --- a/network/src/service/identify_service.rs +++ b/network/src/service/identify_service.rs @@ -119,9 +119,11 @@ impl Stream for IdentifyService { .map(|socket_addr| socket_addr.port()) .map(move |port| { addr.into_iter() - .map(|proto| match proto { - Protocol::Tcp(_) => Protocol::Tcp(port), - value => value, + .filter_map(|proto| match proto { + Protocol::Tcp(_) => Some(Protocol::Tcp(port)), + // Remove p2p part + Protocol::P2p(_) => None, + value => Some(value), }) .collect() }) From 1981533209321a66c1470c2d1ad90337192baab7 Mon Sep 17 00:00:00 2001 From: Linfeng Qian Date: Thu, 21 Mar 2019 16:06:38 +0800 Subject: [PATCH 53/99] fix: Fix parse supported versions --- network/src/protocol.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/network/src/protocol.rs b/network/src/protocol.rs index b2c730a83c..95186ac14f 100644 --- a/network/src/protocol.rs +++ b/network/src/protocol.rs @@ -55,8 +55,14 @@ impl CKBProtocol { pub fn build(&self) -> ProtocolMeta { let event_sender = self.event_sender.clone(); + let supported_versions = self + .supported_versions + .iter() + .map(|v| v.to_string()) + .collect::>(); MetaBuilder::default() .id(self.id) + .support_versions(supported_versions) .service_handle(move || { ProtocolHandle::Callback(Box::new(CKBHandler { id: self.id, @@ -92,8 +98,8 @@ impl ServiceProtocol for CKBHandler { fn init(&mut self, _control: &mut ServiceContext) {} fn connected(&mut self, control: &mut ServiceContext, session: &SessionContext, version: &str) { let (peer_id, version) = { - // FIXME: version number should be discussed. - let parsed_version = version.split('.').last().and_then(|v| v.parse::().ok()); + // TODO: version number should be discussed. + let parsed_version = version.parse::().ok(); if session.remote_pubkey.is_none() || parsed_version.is_none() { error!(target: "network", "ckb protocol connected error, addr: {}, protocol:{}, version: {}", session.address, self.id, version); control.disconnect(session.id); From 5461d5e752695fbe1b49bde24ff59a9538ae9095 Mon Sep 17 00:00:00 2001 From: Linfeng Qian Date: Thu, 21 Mar 2019 16:19:25 +0800 Subject: [PATCH 54/99] chore: Remove unnecessary clone --- network/src/network.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/network/src/network.rs b/network/src/network.rs index 5a503017e4..c6f574e434 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -399,7 +399,7 @@ impl Network { .service_handle(move || { ProtocolHandle::Callback(Box::new(DiscoveryProtocol::new( DISCOVERY_PROTOCOL_ID, - disc_sender.clone(), + disc_sender, ))) }) .build(); From 4a068b61e4c5b134ef44dd385b03e2bc5d852bc7 Mon Sep 17 00:00:00 2001 From: Linfeng Qian Date: Thu, 21 Mar 2019 17:14:36 +0800 Subject: [PATCH 55/99] chore: Remove AddNewAddr event --- network/src/service/discovery_service.rs | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/network/src/service/discovery_service.rs b/network/src/service/discovery_service.rs index 1e17b52e44..d2dcc56453 100644 --- a/network/src/service/discovery_service.rs +++ b/network/src/service/discovery_service.rs @@ -150,10 +150,6 @@ pub enum DiscoveryEvent { peer_id: Option, }, Disconnected(SessionId), - AddNewAddr { - session_id: SessionId, - addr: Multiaddr, - }, AddNewAddrs { session_id: SessionId, addrs: Vec, @@ -204,9 +200,6 @@ impl Stream for DiscoveryService { Some(DiscoveryEvent::Disconnected(session_id)) => { self.sessions.remove(&session_id); } - Some(DiscoveryEvent::AddNewAddr { .. }) => { - // NOTE: ignore add new addr message, handle this in identify protocol - } Some(DiscoveryEvent::AddNewAddrs { session_id, addrs }) => { if let Some(_peer_id) = self.sessions.get(&session_id) { // TODO: wait for peer store update @@ -262,12 +255,7 @@ pub struct DiscoveryAddressManager { } impl AddressManager for DiscoveryAddressManager { - fn add_new_addr(&mut self, session_id: SessionId, addr: Multiaddr) { - let event = DiscoveryEvent::AddNewAddr { session_id, addr }; - if self.event_sender.unbounded_send(event).is_err() { - warn!(target: "network", "receiver maybe dropped!"); - } - } + fn add_new_addr(&mut self, _session_id: SessionId, _addr: Multiaddr) {} fn add_new_addrs(&mut self, session_id: SessionId, addrs: Vec) { let event = DiscoveryEvent::AddNewAddrs { session_id, addrs }; From 3999aee5d5b2bb333c2d4a25af160430900a8666 Mon Sep 17 00:00:00 2001 From: Linfeng Qian Date: Thu, 21 Mar 2019 17:16:01 +0800 Subject: [PATCH 56/99] revert: Revert out bounded service logic --- network/src/service/outbound_peer_service.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/network/src/service/outbound_peer_service.rs b/network/src/service/outbound_peer_service.rs index b068cfc4a6..c9cb5ae247 100644 --- a/network/src/service/outbound_peer_service.rs +++ b/network/src/service/outbound_peer_service.rs @@ -37,14 +37,11 @@ impl Stream for OutboundPeerService { .network .peer_store() .read() - .random_peers(new_outbound as u32) + .peers_to_attempt(new_outbound as u32); + for (peer_id, addr) in attempt_peers .into_iter() - .filter(|(peer_id, _addr)| { - self.network.get_peer_index(peer_id).is_none() - && self.network.local_peer_id() != peer_id - }) - .collect::>(); - for (peer_id, addr) in attempt_peers.into_iter() { + .filter(|(peer_id, _addr)| self.network.local_peer_id() != peer_id) + { self.network.dial(&peer_id, addr); } } From e88567c75aed615a2ea3a9032fe7c71fa9d96d04 Mon Sep 17 00:00:00 2001 From: quake wang Date: Tue, 19 Mar 2019 17:31:23 +0900 Subject: [PATCH 57/99] refactor: move some code to CellSetDiff --- chain/src/chain.rs | 52 ++++++------------------------- shared/src/cell_set.rs | 71 ++++++++++++++++++++++++++---------------- shared/src/shared.rs | 3 +- 3 files changed, 55 insertions(+), 71 deletions(-) diff --git a/chain/src/chain.rs b/chain/src/chain.rs index 1d9d00eb7e..28fb79cbcc 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -426,23 +426,7 @@ impl ChainService { chain_state: &mut ChainState, ) -> Result { let skip_verify = !self.verification; - - let mut old_inputs = FnvHashSet::default(); - let mut old_outputs = FnvHashSet::default(); - let mut new_inputs = FnvHashSet::default(); - let mut new_outputs = FnvHashMap::default(); - - let push_new = |b: &Block, - new_inputs: &mut FnvHashSet, - new_outputs: &mut FnvHashMap| { - for tx in b.commit_transactions() { - let input_pts = tx.input_pts(); - let tx_hash = tx.hash(); - let output_len = tx.outputs().len(); - new_inputs.extend(input_pts); - new_outputs.insert(tx_hash, output_len); - } - }; + let mut cell_set_diff = CellSetDiff::default(); let attached_blocks_iter = fork.attached_blocks().iter().rev(); let detached_blocks_iter = fork.detached_blocks().iter().rev(); @@ -451,17 +435,11 @@ impl ChainService { let verified_len = attached_blocks_len - fork.dirty_exts.len(); for b in detached_blocks_iter { - for tx in b.commit_transactions() { - let input_pts = tx.input_pts(); - let tx_hash = tx.hash(); - - old_inputs.extend(input_pts); - old_outputs.insert(tx_hash); - } + cell_set_diff.push_old(b); } - for b in attached_blocks_iter.clone().take(verified_len) { - push_new(b, &mut new_inputs, &mut new_outputs); + for b in attached_blocks_iter.take(verified_len) { + cell_set_diff.push_new(b); } // The verify function @@ -477,20 +455,20 @@ impl ChainService { { let cell_resolver = |op: &OutPoint| { self.shared.cell_at(op, |op| { - if new_inputs.contains(op) { + if cell_set_diff.new_inputs.contains(op) { Some(true) - } else if let Some(x) = new_outputs.get(&op.hash) { + } else if let Some(x) = cell_set_diff.new_outputs.get(&op.hash) { if op.index < (*x as u32) { Some(false) } else { Some(true) } - } else if old_outputs.contains(&op.hash) { + } else if cell_set_diff.old_outputs.contains(&op.hash) { None } else { chain_state .is_dead(op) - .map(|x| x && !old_inputs.contains(op)) + .map(|x| x && !cell_set_diff.old_inputs.contains(op)) } }) }; @@ -527,7 +505,7 @@ impl ChainService { .verify(chain_state.mut_txs_verify_cache(), &resolved) .is_ok() { - push_new(b, &mut new_inputs, &mut new_outputs); + cell_set_diff.push_new(b); ext.txs_verified = Some(true); } else { found_error = true; @@ -551,17 +529,7 @@ impl ChainService { Err(SharedError::InvalidTransaction)?; } - let old_inputs: Vec = old_inputs.into_iter().collect(); - let old_outputs: Vec = old_outputs.into_iter().collect(); - let new_inputs: Vec = new_inputs.into_iter().collect(); - let new_outputs: Vec<(H256, usize)> = new_outputs.into_iter().collect(); - - Ok(CellSetDiff { - old_inputs, - old_outputs, - new_inputs, - new_outputs, - }) + Ok(cell_set_diff) } // TODO: beatify diff --git a/shared/src/cell_set.rs b/shared/src/cell_set.rs index 3b1eab26a3..4c4cfe9f3a 100644 --- a/shared/src/cell_set.rs +++ b/shared/src/cell_set.rs @@ -1,15 +1,38 @@ +use ckb_core::block::Block; use ckb_core::transaction::OutPoint; use ckb_core::transaction_meta::TransactionMeta; -use fnv::FnvHashMap; +use fnv::{FnvHashMap, FnvHashSet}; use numext_fixed_hash::H256; use serde_derive::{Deserialize, Serialize}; #[derive(Default, Debug, Clone)] pub struct CellSetDiff { - pub old_inputs: Vec, - pub old_outputs: Vec, - pub new_inputs: Vec, - pub new_outputs: Vec<(H256, usize)>, + pub old_inputs: FnvHashSet, + pub old_outputs: FnvHashSet, + pub new_inputs: FnvHashSet, + pub new_outputs: FnvHashMap, +} + +impl CellSetDiff { + pub fn push_new(&mut self, block: &Block) { + for tx in block.commit_transactions() { + let input_pts = tx.input_pts(); + let tx_hash = tx.hash(); + let output_len = tx.outputs().len(); + self.new_inputs.extend(input_pts); + self.new_outputs.insert(tx_hash, output_len); + } + } + + pub fn push_old(&mut self, block: &Block) { + for tx in block.commit_transactions() { + let input_pts = tx.input_pts(); + let tx_hash = tx.hash(); + + self.old_inputs.extend(input_pts); + self.old_outputs.insert(tx_hash); + } + } } #[derive(Default, Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] @@ -32,8 +55,9 @@ impl CellSet { self.inner.get(h) } - pub fn insert(&mut self, hash: H256, outputs_len: usize) { - self.inner.insert(hash, TransactionMeta::new(outputs_len)); + pub fn insert(&mut self, hash: &H256, outputs_len: usize) { + self.inner + .insert(hash.clone(), TransactionMeta::new(outputs_len)); } pub fn remove(&mut self, hash: &H256) -> Option { @@ -52,28 +76,21 @@ impl CellSet { } } - fn rollback(&mut self, inputs: Vec, outputs: Vec) { - for h in outputs { - self.remove(&h); - } - - for o in inputs { - self.mark_live(&o); - } - } + pub fn update(&mut self, diff: CellSetDiff) { + diff.old_outputs.iter().for_each(|h| { + self.remove(h); + }); - fn forward(&mut self, inputs: Vec, outputs: Vec<(H256, usize)>) { - for (hash, len) in outputs { - self.insert(hash, len); - } + diff.old_inputs.iter().for_each(|o| { + self.mark_live(o); + }); - for o in inputs { - self.mark_dead(&o); - } - } + diff.new_outputs.iter().for_each(|(hash, len)| { + self.insert(hash, *len); + }); - pub fn update(&mut self, diff: CellSetDiff) { - self.rollback(diff.old_inputs, diff.old_outputs); - self.forward(diff.new_inputs, diff.new_outputs); + diff.new_inputs.iter().for_each(|o| { + self.mark_dead(o); + }); } } diff --git a/shared/src/shared.rs b/shared/src/shared.rs index 263b85c729..8633eb78f9 100644 --- a/shared/src/shared.rs +++ b/shared/src/shared.rs @@ -135,14 +135,13 @@ impl Shared { let hash = store.get_block_hash(n).unwrap(); for tx in store.get_block_body(&hash).unwrap() { let inputs = tx.input_pts(); - let tx_hash = tx.hash(); let output_len = tx.outputs().len(); for o in inputs { cell_set.mark_dead(&o); } - cell_set.insert(tx_hash, output_len); + cell_set.insert(&tx.hash(), output_len); } } From 87b56a77dc5b461f377f4a4aa77d4210a32e016b Mon Sep 17 00:00:00 2001 From: quake wang Date: Tue, 19 Mar 2019 17:49:43 +0900 Subject: [PATCH 58/99] chore: remove unused resolve_transaction in CellProvider trait --- core/src/cell.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/core/src/cell.rs b/core/src/cell.rs index ba18000dad..5d942f2fe7 100644 --- a/core/src/cell.rs +++ b/core/src/cell.rs @@ -62,11 +62,6 @@ pub trait CellProvider { ) -> CellStatus { unreachable!() } - - fn resolve_transaction(&self, transaction: &Transaction) -> ResolvedTransaction { - let mut seen_inputs = FnvHashSet::default(); - resolve_transaction(transaction, &mut seen_inputs, |x| self.cell(x)) - } } pub fn resolve_transaction CellStatus>( From 08395262865e6ea42e1b5e0b3f4b55c46e3caa6c Mon Sep 17 00:00:00 2001 From: quake wang Date: Wed, 20 Mar 2019 09:08:43 +0900 Subject: [PATCH 59/99] refactor: add OverlayCellProvider and refactor code --- chain/src/chain.rs | 43 ++++----------- chain/src/tests/basic.rs | 2 +- core/src/cell.rs | 86 +++++++++++++++++++++++------- rpc/src/module/chain.rs | 7 ++- shared/src/cell_set.rs | 26 +++++++++ shared/src/chain_state.rs | 76 +++++++++----------------- shared/src/shared.rs | 30 +---------- verification/src/block_verifier.rs | 6 +-- verification/src/tests/dummy.rs | 4 -- 9 files changed, 138 insertions(+), 142 deletions(-) diff --git a/chain/src/chain.rs b/chain/src/chain.rs index 28fb79cbcc..fba62693ac 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -1,8 +1,10 @@ use ckb_core::block::Block; -use ckb_core::cell::{resolve_transaction, CellProvider, CellStatus, ResolvedTransaction}; +use ckb_core::cell::{ + resolve_transaction, BlockCellProvider, OverlayCellProvider, ResolvedTransaction, +}; use ckb_core::extras::BlockExt; use ckb_core::service::{Request, DEFAULT_CHANNEL_SIZE, SIGNAL_CHANNEL_SIZE}; -use ckb_core::transaction::{OutPoint, ProposalShortId}; +use ckb_core::transaction::ProposalShortId; use ckb_core::BlockNumber; use ckb_db::batch::Batch; use ckb_notify::NotifyController; @@ -453,26 +455,6 @@ impl ChainService { .zip(fork.attached_blocks.iter()) .rev() { - let cell_resolver = |op: &OutPoint| { - self.shared.cell_at(op, |op| { - if cell_set_diff.new_inputs.contains(op) { - Some(true) - } else if let Some(x) = cell_set_diff.new_outputs.get(&op.hash) { - if op.index < (*x as u32) { - Some(false) - } else { - Some(true) - } - } else if cell_set_diff.old_outputs.contains(&op.hash) { - None - } else { - chain_state - .is_dead(op) - .map(|x| x && !cell_set_diff.old_inputs.contains(op)) - } - }) - }; - let mut output_indexs = FnvHashMap::default(); let mut seen_inputs = FnvHashSet::default(); @@ -480,23 +462,16 @@ impl ChainService { output_indexs.insert(tx.hash(), i); } + let cell_set_diff_cp = OverlayCellProvider::new(&cell_set_diff, chain_state.cell_set()); + let block_cp = BlockCellProvider::new(b); + let cell_provider = OverlayCellProvider::new(&block_cp, &cell_set_diff_cp); + // cellbase verified let resolved: Vec = b .commit_transactions() .iter() .skip(1) - .map(|x| { - resolve_transaction(x, &mut seen_inputs, |o| { - if let Some(i) = output_indexs.get(&o.hash) { - match b.commit_transactions()[*i].outputs().get(o.index as usize) { - Some(x) => CellStatus::Live(x.clone()), - None => CellStatus::Unknown, - } - } else { - cell_resolver(o) - } - }) - }) + .map(|x| resolve_transaction(x, &mut seen_inputs, &cell_provider)) .collect(); if !found_error diff --git a/chain/src/tests/basic.rs b/chain/src/tests/basic.rs index 4f59999211..af2867e66f 100644 --- a/chain/src/tests/basic.rs +++ b/chain/src/tests/basic.rs @@ -79,7 +79,7 @@ fn test_genesis_transaction_fetch() { let (_chain_controller, shared) = start_chain(Some(consensus)); let out_point = OutPoint::new(root_hash, 0); - let state = shared.cell(&out_point); + let state = shared.chain_state().lock().cell(&out_point); assert!(state.is_live()); } diff --git a/core/src/cell.rs b/core/src/cell.rs index 5d942f2fe7..06f567c111 100644 --- a/core/src/cell.rs +++ b/core/src/cell.rs @@ -1,5 +1,7 @@ +use crate::block::Block; use crate::transaction::{CellOutput, OutPoint, Transaction}; -use fnv::FnvHashSet; +use fnv::{FnvHashMap, FnvHashSet}; +use numext_fixed_hash::H256; use std::iter::Chain; use std::slice; @@ -54,27 +56,81 @@ pub struct ResolvedTransaction { pub trait CellProvider { fn cell(&self, out_point: &OutPoint) -> CellStatus; +} + +pub struct OverlayCellProvider<'a, O, CP> { + overlay: &'a O, + cell_provider: &'a CP, +} + +impl<'a, O, CP> OverlayCellProvider<'a, O, CP> { + pub fn new(overlay: &'a O, cell_provider: &'a CP) -> Self { + OverlayCellProvider { + overlay, + cell_provider, + } + } +} + +impl<'a, O, CP> CellProvider for OverlayCellProvider<'a, O, CP> +where + O: CellProvider, + CP: CellProvider, +{ + fn cell(&self, out_point: &OutPoint) -> CellStatus { + match self.overlay.cell(out_point) { + CellStatus::Live(co) => CellStatus::Live(co), + CellStatus::Dead => CellStatus::Dead, + CellStatus::Unknown => self.cell_provider.cell(out_point), + } + } +} + +pub struct BlockCellProvider<'a> { + output_indexs: FnvHashMap, + block: &'a Block, +} + +impl<'a> BlockCellProvider<'a> { + pub fn new(block: &'a Block) -> Self { + let mut output_indexs = FnvHashMap::default(); + for (i, tx) in block.commit_transactions().iter().enumerate() { + output_indexs.insert(tx.hash(), i); + } + Self { + output_indexs, + block, + } + } +} - fn cell_at Option>( - &self, - _out_point: &OutPoint, - _is_dead: F, - ) -> CellStatus { - unreachable!() +impl<'a> CellProvider for BlockCellProvider<'a> { + fn cell(&self, out_point: &OutPoint) -> CellStatus { + if let Some(i) = self.output_indexs.get(&out_point.hash) { + match self.block.commit_transactions()[*i] + .outputs() + .get(out_point.index as usize) + { + Some(x) => CellStatus::Live(x.clone()), + None => CellStatus::Unknown, + } + } else { + CellStatus::Unknown + } } } -pub fn resolve_transaction CellStatus>( +pub fn resolve_transaction( transaction: &Transaction, seen_inputs: &mut FnvHashSet, - cell: F, + cell_provider: &CP, ) -> ResolvedTransaction { let input_cells = transaction .input_pts() .iter() .map(|input| { if seen_inputs.insert(input.clone()) { - cell(input) + cell_provider.cell(input) } else { CellStatus::Dead } @@ -86,7 +142,7 @@ pub fn resolve_transaction CellStatus>( .iter() .map(|dep| { if seen_inputs.insert(dep.clone()) { - cell(dep) + cell_provider.cell(dep) } else { CellStatus::Dead } @@ -141,14 +197,6 @@ mod tests { None => CellStatus::Unknown, } } - - fn cell_at Option>(&self, o: &OutPoint, _: F) -> CellStatus { - match self.cells.get(o) { - Some(&Some(ref cell_output)) => CellStatus::Live(cell_output.clone()), - Some(&None) => CellStatus::Dead, - None => CellStatus::Unknown, - } - } } #[test] diff --git a/rpc/src/module/chain.rs b/rpc/src/module/chain.rs index fe18d3663e..b64c87f046 100644 --- a/rpc/src/module/chain.rs +++ b/rpc/src/module/chain.rs @@ -96,7 +96,12 @@ impl ChainRpc for ChainRpcImpl { } fn get_live_cell(&self, out_point: OutPoint) -> Result { - Ok(self.shared.cell(&(out_point.into())).into()) + Ok(self + .shared + .chain_state() + .lock() + .cell(&(out_point.into())) + .into()) } fn get_tip_block_number(&self) -> Result { diff --git a/shared/src/cell_set.rs b/shared/src/cell_set.rs index 4c4cfe9f3a..f280fe329a 100644 --- a/shared/src/cell_set.rs +++ b/shared/src/cell_set.rs @@ -1,4 +1,5 @@ use ckb_core::block::Block; +use ckb_core::cell::{CellProvider, CellStatus}; use ckb_core::transaction::OutPoint; use ckb_core::transaction_meta::TransactionMeta; use fnv::{FnvHashMap, FnvHashSet}; @@ -35,6 +36,16 @@ impl CellSetDiff { } } +impl CellProvider for CellSetDiff { + fn cell(&self, out_point: &OutPoint) -> CellStatus { + if self.new_inputs.contains(out_point) { + CellStatus::Dead + } else { + CellStatus::Unknown + } + } +} + #[derive(Default, Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] pub struct CellSet { pub inner: FnvHashMap, @@ -94,3 +105,18 @@ impl CellSet { }); } } + +impl CellProvider for CellSet { + fn cell(&self, out_point: &OutPoint) -> CellStatus { + match self.get(&out_point.hash) { + Some(meta) => { + if meta.is_dead(out_point.index as usize) { + CellStatus::Dead + } else { + CellStatus::Unknown + } + } + None => CellStatus::Unknown, + } + } +} diff --git a/shared/src/chain_state.rs b/shared/src/chain_state.rs index b351ea94bd..366cde5b07 100644 --- a/shared/src/chain_state.rs +++ b/shared/src/chain_state.rs @@ -4,7 +4,9 @@ use crate::index::ChainIndex; use crate::tx_pool::{PoolEntry, PoolError, StagingTxResult, TxPool}; use crate::tx_proposal_table::TxProposalTable; use ckb_core::block::Block; -use ckb_core::cell::{CellProvider, CellStatus, ResolvedTransaction}; +use ckb_core::cell::{ + resolve_transaction, CellProvider, CellStatus, OverlayCellProvider, ResolvedTransaction, +}; use ckb_core::header::{BlockNumber, Header}; use ckb_core::transaction::{OutPoint, ProposalShortId, Transaction}; use ckb_core::Cycle; @@ -124,58 +126,10 @@ impl ChainState { } } - fn get_cell_status_from_store(&self, out_point: &OutPoint) -> CellStatus { - let index = out_point.index as usize; - if let Some(f) = self.is_dead(out_point) { - if f { - CellStatus::Dead - } else { - let transaction = self - .store - .get_transaction(&out_point.hash) - .expect("transaction must exist"); - CellStatus::Live(transaction.outputs()[index].clone()) - } - } else { - CellStatus::Unknown - } - } - pub fn resolve_tx_from_pool(&self, tx: &Transaction, tx_pool: &TxPool) -> ResolvedTransaction { - let fetch_cell = |op| match tx_pool.staging.cell(op) { - CellStatus::Unknown => self.get_cell_status_from_store(op), - cs => cs, - }; + let cell_provider = OverlayCellProvider::new(&tx_pool.staging, self); let mut seen_inputs = FnvHashSet::default(); - let inputs = tx.input_pts(); - let input_cells = inputs - .iter() - .map(|input| { - if seen_inputs.insert(input.clone()) { - fetch_cell(input) - } else { - CellStatus::Dead - } - }) - .collect(); - - let dep_cells = tx - .dep_pts() - .iter() - .map(|dep| { - if seen_inputs.insert(dep.clone()) { - fetch_cell(dep) - } else { - CellStatus::Dead - } - }) - .collect(); - - ResolvedTransaction { - transaction: tx.clone(), - input_cells, - dep_cells, - } + resolve_transaction(tx, &mut seen_inputs, &cell_provider) } pub fn verify_rtx( @@ -370,3 +324,23 @@ impl ChainState { self.tx_pool.get_mut() } } + +impl CellProvider for ChainState { + fn cell(&self, out_point: &OutPoint) -> CellStatus { + if let Some(f) = self.is_dead(out_point) { + if f { + CellStatus::Dead + } else { + match self.store.get_transaction(&out_point.hash) { + Some(tx) => match tx.outputs().get(out_point.index as usize) { + Some(output) => CellStatus::Live(output.clone()), + None => CellStatus::Unknown, + }, + None => CellStatus::Unknown, + } + } + } else { + CellStatus::Unknown + } + } +} diff --git a/shared/src/shared.rs b/shared/src/shared.rs index 8633eb78f9..e530c8f888 100644 --- a/shared/src/shared.rs +++ b/shared/src/shared.rs @@ -9,10 +9,9 @@ use crate::tx_proposal_table::TxProposalTable; use crate::{COLUMNS, COLUMN_BLOCK_HEADER}; use ckb_chain_spec::consensus::{Consensus, ProposalWindow}; use ckb_core::block::Block; -use ckb_core::cell::{CellProvider, CellStatus}; use ckb_core::extras::BlockExt; use ckb_core::header::{BlockNumber, Header}; -use ckb_core::transaction::{Capacity, OutPoint, ProposalShortId, Transaction}; +use ckb_core::transaction::{Capacity, ProposalShortId, Transaction}; use ckb_core::uncle::UncleBlock; use ckb_db::{DBConfig, KeyValueDB, MemoryKeyValueDB, RocksDB}; use ckb_traits::{BlockMedianTimeContext, ChainProvider}; @@ -149,33 +148,6 @@ impl Shared { } } -impl CellProvider for Shared { - fn cell(&self, out_point: &OutPoint) -> CellStatus { - self.cell_at(out_point, |op| self.chain_state.lock().is_dead(op)) - } - - fn cell_at Option>( - &self, - out_point: &OutPoint, - is_dead: F, - ) -> CellStatus { - let index = out_point.index as usize; - if let Some(f) = is_dead(out_point) { - if f { - CellStatus::Dead - } else { - let transaction = self - .store - .get_transaction(&out_point.hash) - .expect("transaction must exist"); - CellStatus::Live(transaction.outputs()[index].clone()) - } - } else { - CellStatus::Unknown - } - } -} - impl ChainProvider for Shared { fn block(&self, hash: &H256) -> Option { self.store.get_block(hash) diff --git a/verification/src/block_verifier.rs b/verification/src/block_verifier.rs index 3f1fd78020..54754add0b 100644 --- a/verification/src/block_verifier.rs +++ b/verification/src/block_verifier.rs @@ -2,7 +2,7 @@ use crate::error::{CellbaseError, CommitError, Error, UnclesError}; use crate::header_verifier::HeaderResolver; use crate::{InputVerifier, TransactionVerifier, Verifier}; use ckb_core::block::Block; -use ckb_core::cell::{CellProvider, ResolvedTransaction}; +use ckb_core::cell::ResolvedTransaction; use ckb_core::header::Header; use ckb_core::transaction::{Capacity, CellInput}; use ckb_core::Cycle; @@ -32,7 +32,7 @@ pub struct BlockVerifier

{ impl

BlockVerifier

where - P: ChainProvider + CellProvider + Clone + 'static, + P: ChainProvider + Clone + 'static, { pub fn new(provider: P) -> Self { BlockVerifier { @@ -46,7 +46,7 @@ where } } -impl Verifier for BlockVerifier

{ +impl Verifier for BlockVerifier

{ type Target = Block; fn verify(&self, target: &Block) -> Result<(), Error> { diff --git a/verification/src/tests/dummy.rs b/verification/src/tests/dummy.rs index 07178b50d9..a2aae0d810 100644 --- a/verification/src/tests/dummy.rs +++ b/verification/src/tests/dummy.rs @@ -92,8 +92,4 @@ impl CellProvider for DummyChainProvider { fn cell(&self, _o: &OutPoint) -> CellStatus { panic!("Not implemented!"); } - - fn cell_at Option>(&self, _o: &OutPoint, _: F) -> CellStatus { - panic!("Not implemented!"); - } } From e3f531a5e0d5108045de433afa39e4a834e25bc2 Mon Sep 17 00:00:00 2001 From: quake wang Date: Thu, 21 Mar 2019 10:22:50 +0900 Subject: [PATCH 60/99] chore: tweak test code --- chain/src/tests/basic.rs | 52 ++++++++++++++++++------------------ chain/src/tests/find_fork.rs | 40 +++++++++++++-------------- chain/src/tests/util.rs | 19 ++++++------- 3 files changed, 56 insertions(+), 55 deletions(-) diff --git a/chain/src/tests/basic.rs b/chain/src/tests/basic.rs index af2867e66f..4e9f9b762c 100644 --- a/chain/src/tests/basic.rs +++ b/chain/src/tests/basic.rs @@ -32,17 +32,17 @@ fn test_genesis_transaction_spend() { .with_header_builder(HeaderBuilder::default().difficulty(U256::from(1000u64))); let consensus = Consensus::default().set_genesis_block(genesis_block); - let (chain_controller, shared) = start_chain(Some(consensus)); + let (chain_controller, shared) = start_chain(Some(consensus), false); let end = 21; let mut blocks1: Vec = vec![]; let mut parent = shared.block_header(&shared.block_hash(0).unwrap()).unwrap(); - for i in 1..end { + for _ in 1..end { let difficulty = parent.difficulty().clone(); let tx = create_transaction(root_hash); root_hash = tx.hash().clone(); - let new_block = gen_block(&parent, i, difficulty + U256::from(1u64), vec![tx], vec![]); + let new_block = gen_block(&parent, difficulty + U256::from(1u64), vec![tx], vec![], vec![]); blocks1.push(new_block.clone()); parent = new_block.header().clone(); } @@ -76,7 +76,7 @@ fn test_genesis_transaction_fetch() { .with_header_builder(HeaderBuilder::default().difficulty(U256::from(1000u64))); let consensus = Consensus::default().set_genesis_block(genesis_block); - let (_chain_controller, shared) = start_chain(Some(consensus)); + let (_chain_controller, shared) = start_chain(Some(consensus), false); let out_point = OutPoint::new(root_hash, 0); let state = shared.chain_state().lock().cell(&out_point); @@ -85,16 +85,16 @@ fn test_genesis_transaction_fetch() { #[test] fn test_chain_fork_by_total_difficulty() { - let (chain_controller, shared) = start_chain(None); + let (chain_controller, shared) = start_chain(None, false); let final_number = 20; let mut chain1: Vec = Vec::new(); let mut chain2: Vec = Vec::new(); let mut parent = shared.block_header(&shared.block_hash(0).unwrap()).unwrap(); - for i in 1..final_number { + for _ in 1..final_number { let difficulty = parent.difficulty().clone(); - let new_block = gen_block(&parent, i, difficulty + U256::from(100u64), vec![], vec![]); + let new_block = gen_block(&parent, difficulty + U256::from(100u64), vec![], vec![], vec![]); chain1.push(new_block.clone()); parent = new_block.header().clone(); } @@ -105,10 +105,10 @@ fn test_chain_fork_by_total_difficulty() { let j = if i > 10 { 110 } else { 99 }; let new_block = gen_block( &parent, - i + 1000, difficulty + U256::from(j as u32), vec![], vec![], + vec![], ); chain2.push(new_block.clone()); parent = new_block.header().clone(); @@ -133,29 +133,29 @@ fn test_chain_fork_by_total_difficulty() { #[test] fn test_chain_fork_by_hash() { - let (chain_controller, shared) = start_chain(None); + let (chain_controller, shared) = start_chain(None, false); let final_number = 20; let mut chain1: Vec = Vec::new(); let mut chain2: Vec = Vec::new(); let mut parent = shared.block_header(&shared.block_hash(0).unwrap()).unwrap(); - for i in 1..final_number { + for _ in 1..final_number { let difficulty = parent.difficulty().clone(); - let new_block = gen_block(&parent, i, difficulty + U256::from(100u64), vec![], vec![]); + let new_block = gen_block(&parent, difficulty + U256::from(100u64), vec![], vec![], vec![]); chain1.push(new_block.clone()); parent = new_block.header().clone(); } parent = shared.block_header(&shared.block_hash(0).unwrap()).unwrap(); - for i in 1..final_number { + for _ in 1..final_number { let difficulty = parent.difficulty().clone(); let new_block = gen_block( &parent, - i + 1000, difficulty + U256::from(100u64), vec![], vec![], + vec![], ); chain2.push(new_block.clone()); parent = new_block.header().clone(); @@ -195,29 +195,29 @@ fn test_chain_fork_by_hash() { #[test] fn test_chain_get_ancestor() { - let (chain_controller, shared) = start_chain(None); + let (chain_controller, shared) = start_chain(None, false); let final_number = 20; let mut chain1: Vec = Vec::new(); let mut chain2: Vec = Vec::new(); let mut parent = shared.block_header(&shared.block_hash(0).unwrap()).unwrap(); - for i in 1..final_number { + for _ in 1..final_number { let difficulty = parent.difficulty().clone(); - let new_block = gen_block(&parent, i, difficulty + U256::from(100u64), vec![], vec![]); + let new_block = gen_block(&parent, difficulty + U256::from(100u64), vec![], vec![], vec![]); chain1.push(new_block.clone()); parent = new_block.header().clone(); } parent = shared.block_header(&shared.block_hash(0).unwrap()).unwrap(); - for i in 1..final_number { + for _ in 1..final_number { let difficulty = parent.difficulty().clone(); let new_block = gen_block( &parent, - i + 1000, difficulty + U256::from(100u64), vec![], vec![], + vec![], ); chain2.push(new_block.clone()); parent = new_block.header().clone(); @@ -258,16 +258,16 @@ fn test_calculate_difficulty() { consensus.pow_time_span = 200; consensus.pow_spacing = 1; - let (chain_controller, shared) = start_chain(Some(consensus.clone())); + let (chain_controller, shared) = start_chain(Some(consensus.clone()), false); let final_number = shared.consensus().difficulty_adjustment_interval(); let mut chain1: Vec = Vec::new(); let mut chain2: Vec = Vec::new(); let mut parent = shared.block_header(&shared.block_hash(0).unwrap()).unwrap(); - for i in 1..final_number - 1 { + for _ in 1..final_number - 1 { let difficulty = shared.calculate_difficulty(&parent).unwrap(); - let new_block = gen_block(&parent, i, difficulty, vec![], vec![]); + let new_block = gen_block(&parent, difficulty, vec![], vec![], vec![]); chain_controller .process_block(Arc::new(new_block.clone())) .expect("process block ok"); @@ -282,7 +282,7 @@ fn test_calculate_difficulty() { if i < 26 { uncles.push(chain1[i as usize].clone().into()); } - let new_block = gen_block(&parent, i + 100, difficulty, vec![], uncles); + let new_block = gen_block(&parent, difficulty, vec![], vec![], uncles); chain_controller .process_block(Arc::new(new_block.clone())) .expect("process block ok"); @@ -297,7 +297,7 @@ fn test_calculate_difficulty() { // 25 * 10 * 1000 / 200 assert_eq!(difficulty, U256::from(1250u64)); - let (chain_controller, shared) = start_chain(Some(consensus.clone())); + let (chain_controller, shared) = start_chain(Some(consensus.clone()), false); let mut chain2: Vec = Vec::new(); for i in 1..final_number - 1 { chain_controller @@ -312,7 +312,7 @@ fn test_calculate_difficulty() { if i < 11 { uncles.push(chain1[i as usize].clone().into()); } - let new_block = gen_block(&parent, i + 100, difficulty, vec![], uncles); + let new_block = gen_block(&parent, difficulty, vec![], vec![], uncles); chain_controller .process_block(Arc::new(new_block.clone())) .expect("process block ok"); @@ -327,7 +327,7 @@ fn test_calculate_difficulty() { // min[10 * 10 * 1000 / 200, 1000] assert_eq!(difficulty, U256::from(1000u64)); - let (chain_controller, shared) = start_chain(Some(consensus.clone())); + let (chain_controller, shared) = start_chain(Some(consensus.clone()), false); let mut chain2: Vec = Vec::new(); for i in 1..final_number - 1 { chain_controller @@ -342,7 +342,7 @@ fn test_calculate_difficulty() { if i < 151 { uncles.push(chain1[i as usize].clone().into()); } - let new_block = gen_block(&parent, i + 100, difficulty, vec![], uncles); + let new_block = gen_block(&parent, difficulty, vec![], vec![], uncles); chain_controller .process_block(Arc::new(new_block.clone())) .expect("process block ok"); diff --git a/chain/src/tests/find_fork.rs b/chain/src/tests/find_fork.rs index 105c9defe6..4c06cfd62c 100644 --- a/chain/src/tests/find_fork.rs +++ b/chain/src/tests/find_fork.rs @@ -32,15 +32,15 @@ fn test_find_fork_case1() { let mut fork2: Vec = Vec::new(); let mut parent = genesis.clone(); - for i in 0..4 { - let new_block = gen_block(&parent, i, U256::from(100u64), vec![], vec![]); + for _ in 0..4 { + let new_block = gen_block(&parent, U256::from(100u64), vec![], vec![], vec![]); fork1.push(new_block.clone()); parent = new_block.header().clone(); } let mut parent = genesis.clone(); - for i in 0..3 { - let new_block = gen_block(&parent, i, U256::from(90u64), vec![], vec![]); + for _ in 0..3 { + let new_block = gen_block(&parent, U256::from(90u64), vec![], vec![], vec![]); fork2.push(new_block.clone()); parent = new_block.header().clone(); } @@ -58,7 +58,7 @@ fn test_find_fork_case1() { let tip_number = { shared.chain_state().lock().tip_number() }; // fork2 total_difficulty 470 - let new_block = gen_block(&parent, 100, U256::from(200u64), vec![], vec![]); + let new_block = gen_block(&parent, U256::from(200u64), vec![], vec![], vec![]); fork2.push(new_block.clone()); let ext = BlockExt { @@ -104,15 +104,15 @@ fn test_find_fork_case2() { let mut fork2: Vec = Vec::new(); let mut parent = genesis.clone(); - for i in 0..4 { - let new_block = gen_block(&parent, i, U256::from(100u64), vec![], vec![]); + for _ in 0..4 { + let new_block = gen_block(&parent, U256::from(100u64), vec![], vec![], vec![]); fork1.push(new_block.clone()); parent = new_block.header().clone(); } let mut parent = fork1[0].header().clone(); - for i in 0..2 { - let new_block = gen_block(&parent, i, U256::from(90u64), vec![], vec![]); + for _ in 0..2 { + let new_block = gen_block(&parent, U256::from(90u64), vec![], vec![], vec![]); fork2.push(new_block.clone()); parent = new_block.header().clone(); } @@ -132,10 +132,10 @@ fn test_find_fork_case2() { let difficulty = parent.difficulty().clone(); let new_block = gen_block( &parent, - 100, difficulty + U256::from(200u64), vec![], vec![], + vec![], ); fork2.push(new_block.clone()); @@ -182,15 +182,15 @@ fn test_find_fork_case3() { let mut fork2: Vec = Vec::new(); let mut parent = genesis.clone(); - for i in 0..3 { - let new_block = gen_block(&parent, i, U256::from(80u64), vec![], vec![]); + for _ in 0..3 { + let new_block = gen_block(&parent, U256::from(80u64), vec![], vec![], vec![]); fork1.push(new_block.clone()); parent = new_block.header().clone(); } let mut parent = genesis.clone(); - for i in 0..5 { - let new_block = gen_block(&parent, i, U256::from(40u64), vec![], vec![]); + for _ in 0..5 { + let new_block = gen_block(&parent, U256::from(40u64), vec![], vec![], vec![]); fork2.push(new_block.clone()); parent = new_block.header().clone(); } @@ -209,7 +209,7 @@ fn test_find_fork_case3() { println!("case3 tip{}", tip_number); - let new_block = gen_block(&parent, 100, U256::from(100u64), vec![], vec![]); + let new_block = gen_block(&parent, U256::from(100u64), vec![], vec![], vec![]); fork2.push(new_block.clone()); let ext = BlockExt { @@ -254,15 +254,15 @@ fn test_find_fork_case4() { let mut fork2: Vec = Vec::new(); let mut parent = genesis.clone(); - for i in 0..5 { - let new_block = gen_block(&parent, i, U256::from(40u64), vec![], vec![]); + for _ in 0..5 { + let new_block = gen_block(&parent, U256::from(40u64), vec![], vec![], vec![]); fork1.push(new_block.clone()); parent = new_block.header().clone(); } let mut parent = genesis.clone(); - for i in 0..2 { - let new_block = gen_block(&parent, i, U256::from(80u64), vec![], vec![]); + for _ in 0..2 { + let new_block = gen_block(&parent, U256::from(80u64), vec![], vec![], vec![]); fork2.push(new_block.clone()); parent = new_block.header().clone(); } @@ -281,7 +281,7 @@ fn test_find_fork_case4() { println!("case3 tip{}", tip_number); - let new_block = gen_block(&parent, 100, U256::from(100u64), vec![], vec![]); + let new_block = gen_block(&parent, U256::from(100u64), vec![], vec![], vec![]); fork2.push(new_block.clone()); let ext = BlockExt { diff --git a/chain/src/tests/util.rs b/chain/src/tests/util.rs index c14c93d8d8..3a81ab7310 100644 --- a/chain/src/tests/util.rs +++ b/chain/src/tests/util.rs @@ -4,7 +4,7 @@ use ckb_core::block::Block; use ckb_core::block::BlockBuilder; use ckb_core::header::{Header, HeaderBuilder}; use ckb_core::transaction::{ - CellInput, CellOutput, OutPoint, ProposalShortId, Transaction, TransactionBuilder, + CellInput, CellOutput, OutPoint, Transaction, TransactionBuilder, }; use ckb_core::uncle::UncleBlock; use ckb_core::BlockNumber; @@ -16,9 +16,11 @@ use ckb_shared::store::ChainKVStore; use faketime::unix_time_as_millis; use numext_fixed_hash::H256; use numext_fixed_uint::U256; +use rand; pub(crate) fn start_chain( consensus: Option, + verification: bool, ) -> (ChainController, Shared>) { let builder = SharedBuilder::::new(); let shared = builder @@ -27,7 +29,7 @@ pub(crate) fn start_chain( let notify = NotifyService::default().start::<&str>(None); let chain_service = ChainBuilder::new(shared.clone(), notify) - .verification(false) + .verification(verification) .build(); let chain_controller = chain_service.start::<&str>(None); (chain_controller, shared) @@ -42,28 +44,27 @@ fn create_cellbase(number: BlockNumber) -> Transaction { pub(crate) fn gen_block( parent_header: &Header, - nonce: u64, difficulty: U256, commit_transactions: Vec, + proposal_transactions: Vec, uncles: Vec, ) -> Block { let number = parent_header.number() + 1; let cellbase = create_cellbase(number); - let header = HeaderBuilder::default() + let header_builder = HeaderBuilder::default() .parent_hash(parent_header.hash().clone()) .timestamp(unix_time_as_millis()) .number(number) .difficulty(difficulty) - .nonce(nonce) - .build(); + .cellbase_id(cellbase.hash()) + .nonce(rand::random()); BlockBuilder::default() - .header(header) .commit_transaction(cellbase) .commit_transactions(commit_transactions) .uncles(uncles) - .proposal_transaction(ProposalShortId::from_slice(&[1; 10]).unwrap()) - .build() + .proposal_transactions(proposal_transactions.iter().map(|tx| tx.proposal_short_id()).collect()) + .with_header_builder(header_builder) } pub(crate) fn create_transaction(parent: H256) -> Transaction { From 2913cca3275aa752256e2eededa7a0450b7cddcb Mon Sep 17 00:00:00 2001 From: quake wang Date: Fri, 22 Mar 2019 11:05:25 +0900 Subject: [PATCH 61/99] fix: resolve transactions verifier bug and add some test --- chain/src/chain.rs | 75 ++++--- chain/src/tests/basic.rs | 265 ++++++++++++++++++++++- chain/src/tests/util.rs | 48 +++- core/src/cell.rs | 19 ++ core/src/transaction.rs | 4 + shared/src/chain_state.rs | 20 +- shared/src/error.rs | 4 +- verification/src/block_verifier.rs | 10 + verification/src/transaction_verifier.rs | 3 +- 9 files changed, 379 insertions(+), 69 deletions(-) diff --git a/chain/src/chain.rs b/chain/src/chain.rs index fba62693ac..2fb65c351a 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -18,7 +18,7 @@ use ckb_verification::{BlockVerifier, TransactionsVerifier, Verifier}; use crossbeam_channel::{self, select, Receiver, Sender}; use failure::Error as FailureError; use faketime::unix_time_as_millis; -use fnv::{FnvHashMap, FnvHashSet}; +use fnv::FnvHashSet; use log::{self, debug, error, log_enabled}; use numext_fixed_hash::H256; use numext_fixed_uint::U256; @@ -198,7 +198,7 @@ impl ChainService { ); if parent_ext.txs_verified == Some(false) { - Err(SharedError::InvalidTransaction)?; + Err(SharedError::InvalidParentBlock)?; } let ext = BlockExt { @@ -427,7 +427,6 @@ impl ChainService { fork: &mut ForkChanges, chain_state: &mut ChainState, ) -> Result { - let skip_verify = !self.verification; let mut cell_set_diff = CellSetDiff::default(); let attached_blocks_iter = fork.attached_blocks().iter().rev(); @@ -447,7 +446,7 @@ impl ChainService { // The verify function let txs_verifier = TransactionsVerifier::new(self.shared.consensus().max_block_cycles()); - let mut found_error = false; + let mut found_error = None; // verify transaction for (ext, b) in fork .dirty_exts @@ -455,36 +454,42 @@ impl ChainService { .zip(fork.attached_blocks.iter()) .rev() { - let mut output_indexs = FnvHashMap::default(); - let mut seen_inputs = FnvHashSet::default(); - - for (i, tx) in b.commit_transactions().iter().enumerate() { - output_indexs.insert(tx.hash(), i); - } - - let cell_set_diff_cp = OverlayCellProvider::new(&cell_set_diff, chain_state.cell_set()); - let block_cp = BlockCellProvider::new(b); - let cell_provider = OverlayCellProvider::new(&block_cp, &cell_set_diff_cp); - - // cellbase verified - let resolved: Vec = b - .commit_transactions() - .iter() - .skip(1) - .map(|x| resolve_transaction(x, &mut seen_inputs, &cell_provider)) - .collect(); - - if !found_error - || skip_verify - || txs_verifier - .verify(chain_state.mut_txs_verify_cache(), &resolved) - .is_ok() - { + if self.verification { + if found_error.is_none() { + let mut seen_inputs = FnvHashSet::default(); + + // TODO Q impl CellProvider for store, use chain_state as CellProvider here directly. + let cell_set_cp = OverlayCellProvider::new(chain_state.cell_set(), chain_state); + let cell_set_diff_cp = OverlayCellProvider::new(&cell_set_diff, &cell_set_cp); + let block_cp = BlockCellProvider::new(b); + let cell_provider = OverlayCellProvider::new(&block_cp, &cell_set_diff_cp); + + let resolved: Vec = b + .commit_transactions() + .iter() + .map(|x| resolve_transaction(x, &mut seen_inputs, &cell_provider)) + .collect(); + + match txs_verifier.verify( + chain_state.mut_txs_verify_cache(), + &resolved, + self.shared.block_reward(b.header().number()), + ) { + Ok(_) => { + cell_set_diff.push_new(b); + ext.txs_verified = Some(true); + } + Err(err) => { + found_error = Some(err); + ext.txs_verified = Some(false); + } + } + } else { + ext.txs_verified = Some(false); + } + } else { cell_set_diff.push_new(b); ext.txs_verified = Some(true); - } else { - found_error = true; - ext.txs_verified = Some(false); } } @@ -500,8 +505,10 @@ impl ChainService { .insert_block_ext(batch, &b.header().hash(), ext); } - if found_error { - Err(SharedError::InvalidTransaction)?; + if found_error.is_some() { + Err(SharedError::InvalidTransaction( + found_error.unwrap().to_string(), + ))?; } Ok(cell_set_diff) diff --git a/chain/src/tests/basic.rs b/chain/src/tests/basic.rs index 4e9f9b762c..94ae6ec3ee 100644 --- a/chain/src/tests/basic.rs +++ b/chain/src/tests/basic.rs @@ -38,11 +38,17 @@ fn test_genesis_transaction_spend() { let mut blocks1: Vec = vec![]; let mut parent = shared.block_header(&shared.block_hash(0).unwrap()).unwrap(); - for _ in 1..end { + for i in 1..end { let difficulty = parent.difficulty().clone(); - let tx = create_transaction(root_hash); + let tx = create_transaction(root_hash, i as u8); root_hash = tx.hash().clone(); - let new_block = gen_block(&parent, difficulty + U256::from(1u64), vec![tx], vec![], vec![]); + let new_block = gen_block( + &parent, + difficulty + U256::from(1u64), + vec![tx], + vec![], + vec![], + ); blocks1.push(new_block.clone()); parent = new_block.header().clone(); } @@ -54,6 +60,235 @@ fn test_genesis_transaction_spend() { } } +#[test] +fn test_transaction_spend_in_same_block() { + let (chain_controller, shared) = start_chain(None, true); + let mut chain: Vec = Vec::new(); + let mut parent = shared.block_header(&shared.block_hash(0).unwrap()).unwrap(); + { + let difficulty = parent.difficulty().clone(); + let new_block = gen_block( + &parent, + difficulty + U256::from(100u64), + vec![], + vec![], + vec![], + ); + parent = new_block.header().clone(); + chain.push(new_block); + } + + let last_cell_base = &chain.last().unwrap().commit_transactions()[0]; + let tx1 = create_transaction(last_cell_base.hash(), 1); + let tx2 = create_transaction(tx1.hash(), 2); + let txs = vec![tx1, tx2]; + // proposal txs + { + let difficulty = parent.difficulty().clone(); + let new_block = gen_block( + &parent, + difficulty + U256::from(100u64), + vec![], + txs.clone(), + vec![], + ); + parent = new_block.header().clone(); + chain.push(new_block); + } + // empty N+1 block + { + let difficulty = parent.difficulty().clone(); + let new_block = gen_block( + &parent, + difficulty + U256::from(100u64), + vec![], + vec![], + vec![], + ); + parent = new_block.header().clone(); + chain.push(new_block); + } + // commit txs in N+2 block + { + let difficulty = parent.difficulty().clone(); + let new_block = gen_block( + &parent, + difficulty + U256::from(100u64), + txs.clone(), + vec![], + vec![], + ); + chain.push(new_block); + } + for block in &chain { + chain_controller + .process_block(Arc::new(block.clone())) + .expect("process block ok"); + } +} + +#[test] +fn test_transaction_conflict_in_same_block() { + let (chain_controller, shared) = start_chain(None, true); + let mut chain: Vec = Vec::new(); + let mut parent = shared.block_header(&shared.block_hash(0).unwrap()).unwrap(); + { + let difficulty = parent.difficulty().clone(); + let new_block = gen_block( + &parent, + difficulty + U256::from(100u64), + vec![], + vec![], + vec![], + ); + parent = new_block.header().clone(); + chain.push(new_block); + } + + let last_cell_base = &chain.last().unwrap().commit_transactions()[0]; + let tx1 = create_transaction(last_cell_base.hash(), 1); + let tx2 = create_transaction(tx1.hash(), 2); + let tx3 = create_transaction(tx1.hash(), 3); + let txs = vec![tx1, tx2, tx3]; + // proposal txs + { + let difficulty = parent.difficulty().clone(); + let new_block = gen_block( + &parent, + difficulty + U256::from(100u64), + vec![], + txs.clone(), + vec![], + ); + parent = new_block.header().clone(); + chain.push(new_block); + } + // empty N+1 block + { + let difficulty = parent.difficulty().clone(); + let new_block = gen_block( + &parent, + difficulty + U256::from(100u64), + vec![], + vec![], + vec![], + ); + parent = new_block.header().clone(); + chain.push(new_block); + } + // commit txs in N+2 block + { + let difficulty = parent.difficulty().clone(); + let new_block = gen_block( + &parent, + difficulty + U256::from(100u64), + txs.clone(), + vec![], + vec![], + ); + chain.push(new_block); + } + for i in 0..3 { + chain_controller + .process_block(Arc::new(chain[i].clone())) + .expect("process block ok"); + } + assert_eq!( + "Err(InvalidTransaction(\"Transactions((2, Conflict))\"))", + format!( + "{:?}", + chain_controller.process_block(Arc::new(chain[3].clone())) + ) + ); +} + +#[test] +fn test_transaction_conflict_in_different_blocks() { + let (chain_controller, shared) = start_chain(None, true); + let mut chain: Vec = Vec::new(); + let mut parent = shared.block_header(&shared.block_hash(0).unwrap()).unwrap(); + { + let difficulty = parent.difficulty().clone(); + let new_block = gen_block( + &parent, + difficulty + U256::from(100u64), + vec![], + vec![], + vec![], + ); + parent = new_block.header().clone(); + chain.push(new_block); + } + + let last_cell_base = &chain.last().unwrap().commit_transactions()[0]; + let tx1 = create_transaction(last_cell_base.hash(), 1); + let tx2 = create_transaction(tx1.hash(), 2); + let tx3 = create_transaction(tx1.hash(), 3); + // proposal txs + { + let difficulty = parent.difficulty().clone(); + let new_block = gen_block( + &parent, + difficulty + U256::from(100u64), + vec![], + vec![tx1.clone(), tx2.clone(), tx3.clone()], + vec![], + ); + parent = new_block.header().clone(); + chain.push(new_block); + } + // empty N+1 block + { + let difficulty = parent.difficulty().clone(); + let new_block = gen_block( + &parent, + difficulty + U256::from(100u64), + vec![], + vec![], + vec![], + ); + parent = new_block.header().clone(); + chain.push(new_block); + } + // commit tx1 and tx2 in N+2 block + { + let difficulty = parent.difficulty().clone(); + let new_block = gen_block( + &parent, + difficulty + U256::from(100u64), + vec![tx1.clone(), tx2.clone()], + vec![], + vec![], + ); + parent = new_block.header().clone(); + chain.push(new_block); + } + // commit tx3 in N+3 block + { + let difficulty = parent.difficulty().clone(); + let new_block = gen_block( + &parent, + difficulty + U256::from(100u64), + vec![tx3.clone()], + vec![], + vec![], + ); + chain.push(new_block); + } + for i in 0..4 { + chain_controller + .process_block(Arc::new(chain[i].clone())) + .expect("process block ok"); + } + assert_eq!( + "Err(InvalidTransaction(\"Transactions((0, Conflict))\"))", + format!( + "{:?}", + chain_controller.process_block(Arc::new(chain[4].clone())) + ) + ); +} + #[test] fn test_genesis_transaction_fetch() { let tx = TransactionBuilder::default() @@ -94,7 +329,13 @@ fn test_chain_fork_by_total_difficulty() { let mut parent = shared.block_header(&shared.block_hash(0).unwrap()).unwrap(); for _ in 1..final_number { let difficulty = parent.difficulty().clone(); - let new_block = gen_block(&parent, difficulty + U256::from(100u64), vec![], vec![], vec![]); + let new_block = gen_block( + &parent, + difficulty + U256::from(100u64), + vec![], + vec![], + vec![], + ); chain1.push(new_block.clone()); parent = new_block.header().clone(); } @@ -142,7 +383,13 @@ fn test_chain_fork_by_hash() { let mut parent = shared.block_header(&shared.block_hash(0).unwrap()).unwrap(); for _ in 1..final_number { let difficulty = parent.difficulty().clone(); - let new_block = gen_block(&parent, difficulty + U256::from(100u64), vec![], vec![], vec![]); + let new_block = gen_block( + &parent, + difficulty + U256::from(100u64), + vec![], + vec![], + vec![], + ); chain1.push(new_block.clone()); parent = new_block.header().clone(); } @@ -204,7 +451,13 @@ fn test_chain_get_ancestor() { let mut parent = shared.block_header(&shared.block_hash(0).unwrap()).unwrap(); for _ in 1..final_number { let difficulty = parent.difficulty().clone(); - let new_block = gen_block(&parent, difficulty + U256::from(100u64), vec![], vec![], vec![]); + let new_block = gen_block( + &parent, + difficulty + U256::from(100u64), + vec![], + vec![], + vec![], + ); chain1.push(new_block.clone()); parent = new_block.header().clone(); } diff --git a/chain/src/tests/util.rs b/chain/src/tests/util.rs index 3a81ab7310..8fc7b4ac5b 100644 --- a/chain/src/tests/util.rs +++ b/chain/src/tests/util.rs @@ -3,9 +3,8 @@ use ckb_chain_spec::consensus::Consensus; use ckb_core::block::Block; use ckb_core::block::BlockBuilder; use ckb_core::header::{Header, HeaderBuilder}; -use ckb_core::transaction::{ - CellInput, CellOutput, OutPoint, Transaction, TransactionBuilder, -}; +use ckb_core::script::Script; +use ckb_core::transaction::{CellInput, CellOutput, OutPoint, Transaction, TransactionBuilder}; use ckb_core::uncle::UncleBlock; use ckb_core::BlockNumber; use ckb_db::memorydb::MemoryKeyValueDB; @@ -17,6 +16,9 @@ use faketime::unix_time_as_millis; use numext_fixed_hash::H256; use numext_fixed_uint::U256; use rand; +use std::fs::File; +use std::io::Read; +use std::path::Path; pub(crate) fn start_chain( consensus: Option, @@ -38,7 +40,12 @@ pub(crate) fn start_chain( fn create_cellbase(number: BlockNumber) -> Transaction { TransactionBuilder::default() .input(CellInput::new_cellbase_input(number)) - .output(CellOutput::new(0, vec![], H256::zero(), None)) + .output(CellOutput::new( + 5000, + vec![], + create_script().type_hash(), + None, + )) .build() } @@ -63,17 +70,34 @@ pub(crate) fn gen_block( .commit_transaction(cellbase) .commit_transactions(commit_transactions) .uncles(uncles) - .proposal_transactions(proposal_transactions.iter().map(|tx| tx.proposal_short_id()).collect()) + .proposal_transactions( + proposal_transactions + .iter() + .map(|tx| tx.proposal_short_id()) + .collect(), + ) .with_header_builder(header_builder) } -pub(crate) fn create_transaction(parent: H256) -> Transaction { - let mut output = CellOutput::default(); - output.capacity = 100_000_000 / 100 as u64; - let outputs: Vec = vec![output.clone(); 100]; - +pub(crate) fn create_transaction(parent: H256, unique_data: u8) -> Transaction { + let script = create_script(); TransactionBuilder::default() - .input(CellInput::new(OutPoint::new(parent, 0), Default::default())) - .outputs(outputs) + .output(CellOutput::new( + 5000, + vec![unique_data], + script.type_hash(), + None, + )) + .input(CellInput::new(OutPoint::new(parent, 0), script)) .build() } + +fn create_script() -> Script { + let mut file = File::open( + Path::new(env!("CARGO_MANIFEST_DIR")).join("../nodes_template/spec/cells/always_success"), + ) + .unwrap(); + let mut buffer = Vec::new(); + file.read_to_end(&mut buffer).unwrap(); + Script::new(0, Vec::new(), None, Some(buffer), Vec::new()) +} diff --git a/core/src/cell.rs b/core/src/cell.rs index 06f567c111..40b11ce4b0 100644 --- a/core/src/cell.rs +++ b/core/src/cell.rs @@ -1,5 +1,6 @@ use crate::block::Block; use crate::transaction::{CellOutput, OutPoint, Transaction}; +use crate::Capacity; use fnv::{FnvHashMap, FnvHashSet}; use numext_fixed_hash::H256; use std::iter::Chain; @@ -178,6 +179,24 @@ impl ResolvedTransaction { pub fn is_fully_resolved(&self) -> bool { self.cells_iter().all(|state| state.is_live()) } + + pub fn fee(&self) -> Capacity { + self.inputs_capacity() + .saturating_sub(self.transaction.outputs_capacity()) + } + + pub fn inputs_capacity(&self) -> Capacity { + self.input_cells + .iter() + .filter_map(|cell_status| { + if let CellStatus::Live(cell_output) = cell_status { + Some(cell_output.capacity) + } else { + None + } + }) + .sum() + } } #[cfg(test)] diff --git a/core/src/transaction.rs b/core/src/transaction.rs index d35d1d861c..c0b7b89d05 100644 --- a/core/src/transaction.rs +++ b/core/src/transaction.rs @@ -297,6 +297,10 @@ impl Transaction { pub fn get_output(&self, i: usize) -> Option { self.outputs.get(i).cloned() } + + pub fn outputs_capacity(&self) -> Capacity { + self.outputs.iter().map(|output| output.capacity).sum() + } } #[derive(Default)] diff --git a/shared/src/chain_state.rs b/shared/src/chain_state.rs index 366cde5b07..153bf17ed5 100644 --- a/shared/src/chain_state.rs +++ b/shared/src/chain_state.rs @@ -327,20 +327,12 @@ impl ChainState { impl CellProvider for ChainState { fn cell(&self, out_point: &OutPoint) -> CellStatus { - if let Some(f) = self.is_dead(out_point) { - if f { - CellStatus::Dead - } else { - match self.store.get_transaction(&out_point.hash) { - Some(tx) => match tx.outputs().get(out_point.index as usize) { - Some(output) => CellStatus::Live(output.clone()), - None => CellStatus::Unknown, - }, - None => CellStatus::Unknown, - } - } - } else { - CellStatus::Unknown + match self.store.get_transaction(&out_point.hash) { + Some(tx) => match tx.outputs().get(out_point.index as usize) { + Some(output) => CellStatus::Live(output.clone()), + None => CellStatus::Unknown, + }, + None => CellStatus::Unknown, } } } diff --git a/shared/src/error.rs b/shared/src/error.rs index 9d8c32a0f0..1f73d9002c 100644 --- a/shared/src/error.rs +++ b/shared/src/error.rs @@ -8,7 +8,9 @@ pub enum SharedError { #[fail(display = "InvalidOutput")] InvalidOutput, #[fail(display = "InvalidTransaction")] - InvalidTransaction, + InvalidTransaction(String), + #[fail(display = "InvalidParentBlock")] + InvalidParentBlock, #[fail(display = "DB error: {}", _0)] DB(DBError), } diff --git a/verification/src/block_verifier.rs b/verification/src/block_verifier.rs index 54754add0b..d1f40bba98 100644 --- a/verification/src/block_verifier.rs +++ b/verification/src/block_verifier.rs @@ -389,10 +389,20 @@ impl TransactionsVerifier { &self, txs_verify_cache: &mut LruCache, resolved: &[ResolvedTransaction], + block_reward: Capacity, ) -> Result<(), Error> { + // verify cellbase reward + let cellbase = &resolved[0]; + let fee: Capacity = resolved.iter().skip(1).map(|rt| rt.fee()).sum(); + if cellbase.transaction.outputs_capacity() > block_reward + fee { + return Err(Error::Cellbase(CellbaseError::InvalidReward)); + } + // TODO use TransactionScriptsVerifier to verify cellbase script + // make verifiers orthogonal let cycles_set = resolved .par_iter() + .skip(1) .enumerate() .map(|(index, tx)| { if let Some(cycles) = txs_verify_cache.get(&tx.transaction.hash()) { diff --git a/verification/src/transaction_verifier.rs b/verification/src/transaction_verifier.rs index 845b66b7b0..4205874a5f 100644 --- a/verification/src/transaction_verifier.rs +++ b/verification/src/transaction_verifier.rs @@ -32,10 +32,9 @@ impl<'a> TransactionVerifier<'a> { self.version.verify()?; self.empty.verify()?; self.null.verify()?; + self.inputs.verify()?; self.capacity.verify()?; self.duplicate_inputs.verify()?; - // InputVerifier should be executed before ScriptVerifier - self.inputs.verify()?; let cycles = self.script.verify(max_cycles)?; Ok(cycles) } From cd853373dac86395c3310f8c92e5230b4d89f877 Mon Sep 17 00:00:00 2001 From: quake wang Date: Fri, 22 Mar 2019 11:52:03 +0900 Subject: [PATCH 62/99] chore: remove some unused test case and fix clippy --- chain/src/tests/basic.rs | 8 +-- verification/src/tests/block_verifier.rs | 81 ------------------------ 2 files changed, 4 insertions(+), 85 deletions(-) diff --git a/chain/src/tests/basic.rs b/chain/src/tests/basic.rs index 94ae6ec3ee..64151db291 100644 --- a/chain/src/tests/basic.rs +++ b/chain/src/tests/basic.rs @@ -188,9 +188,9 @@ fn test_transaction_conflict_in_same_block() { ); chain.push(new_block); } - for i in 0..3 { + for block in chain.iter().take(3) { chain_controller - .process_block(Arc::new(chain[i].clone())) + .process_block(Arc::new(block.clone())) .expect("process block ok"); } assert_eq!( @@ -275,9 +275,9 @@ fn test_transaction_conflict_in_different_blocks() { ); chain.push(new_block); } - for i in 0..4 { + for block in chain.iter().take(4) { chain_controller - .process_block(Arc::new(chain[i].clone())) + .process_block(Arc::new(block.clone())) .expect("process block ok"); } assert_eq!( diff --git a/verification/src/tests/block_verifier.rs b/verification/src/tests/block_verifier.rs index 5d7cd96100..46b7e258f8 100644 --- a/verification/src/tests/block_verifier.rs +++ b/verification/src/tests/block_verifier.rs @@ -153,87 +153,6 @@ pub fn test_cellbase_with_more_reward_than_available() { ); } -#[test] -pub fn test_cellbase_with_invalid_transaction() { - let mut transaction_fees = HashMap::>::new(); - let transaction = create_normal_transaction(); - transaction_fees.insert(transaction.hash().clone(), Err(SharedError::InvalidOutput)); - - let block = BlockBuilder::default() - .commit_transaction(create_cellbase_transaction_with_capacity(100)) - .commit_transaction(transaction) - .build(); - - let provider = DummyChainProvider { - block_reward: 100, - transaction_fees, - }; - - let verifier = CellbaseVerifier::new(provider); - assert_eq!( - verifier.verify(&block), - Err(VerifyError::Chain(format!( - "{}", - SharedError::InvalidOutput - ))) - ); -} - -#[test] -pub fn test_cellbase_with_two_outputs() { - let mut transaction_fees = HashMap::>::new(); - let transaction = create_normal_transaction(); - transaction_fees.insert(transaction.hash().clone(), Ok(0)); - - let cellbase_transaction = TransactionBuilder::default() - .input(CellInput::new_cellbase_input(0)) - .output(CellOutput::new(100, Vec::new(), H256::default(), None)) - .output(CellOutput::new(50, Vec::new(), H256::default(), None)) - .build(); - - let block = BlockBuilder::default() - .commit_transaction(cellbase_transaction) - .commit_transaction(transaction) - .build(); - - let provider = DummyChainProvider { - block_reward: 150, - transaction_fees, - }; - - let verifier = CellbaseVerifier::new(provider); - assert!(verifier.verify(&block).is_ok()); -} - -#[test] -pub fn test_cellbase_with_two_outputs_and_more_rewards_than_maximum() { - let mut transaction_fees = HashMap::>::new(); - let transaction = create_normal_transaction(); - transaction_fees.insert(transaction.hash().clone(), Ok(0)); - - let cellbase_transaction = TransactionBuilder::default() - .input(CellInput::new_cellbase_input(0)) - .output(CellOutput::new(100, Vec::new(), H256::default(), None)) - .output(CellOutput::new(50, Vec::new(), H256::default(), None)) - .build(); - - let block = BlockBuilder::default() - .commit_transaction(cellbase_transaction) - .commit_transaction(transaction) - .build(); - - let provider = DummyChainProvider { - block_reward: 100, - transaction_fees, - }; - - let verifier = CellbaseVerifier::new(provider); - assert_eq!( - verifier.verify(&block), - Err(VerifyError::Cellbase(CellbaseError::InvalidReward)) - ); -} - #[test] pub fn test_empty_transactions() { let block = BlockBuilder::default().build(); From 948bb60ad6d0c79b80cc128fa2cba5e7ea95f93a Mon Sep 17 00:00:00 2001 From: quake wang Date: Fri, 22 Mar 2019 12:06:34 +0900 Subject: [PATCH 63/99] test: add delay verify test --- chain/src/tests/delay_verify.rs | 200 ++++++++++++++++++++++++++++++++ chain/src/tests/mod.rs | 1 + 2 files changed, 201 insertions(+) create mode 100644 chain/src/tests/delay_verify.rs diff --git a/chain/src/tests/delay_verify.rs b/chain/src/tests/delay_verify.rs new file mode 100644 index 0000000000..d6963c04d1 --- /dev/null +++ b/chain/src/tests/delay_verify.rs @@ -0,0 +1,200 @@ +use crate::tests::util::{create_transaction, gen_block, start_chain}; +use ckb_core::block::Block; +use ckb_traits::ChainProvider; +use numext_fixed_uint::U256; +use std::sync::Arc; + +#[test] +fn test_dead_cell_in_same_block() { + let (chain_controller, shared) = start_chain(None, true); + let final_number = 20; + let switch_fork_number = 10; + + let mut chain1: Vec = Vec::new(); + let mut chain2: Vec = Vec::new(); + + let mut parent = shared.block_header(&shared.block_hash(0).unwrap()).unwrap(); + for _ in 1..final_number { + let difficulty = parent.difficulty().clone(); + let new_block = gen_block( + &parent, + difficulty + U256::from(100u64), + vec![], + vec![], + vec![], + ); + chain1.push(new_block.clone()); + parent = new_block.header().clone(); + } + + parent = shared.block_header(&shared.block_hash(0).unwrap()).unwrap(); + for _ in 1..switch_fork_number { + let difficulty = parent.difficulty().clone(); + let new_block = gen_block( + &parent, + difficulty + U256::from(99u64), + vec![], + vec![], + vec![], + ); + chain2.push(new_block.clone()); + parent = new_block.header().clone(); + } + + let last_cell_base = &chain2.last().unwrap().commit_transactions()[0]; + let tx1 = create_transaction(last_cell_base.hash(), 1); + let tx2 = create_transaction(tx1.hash(), 2); + let tx3 = create_transaction(tx1.hash(), 3); + let txs = vec![tx1, tx2, tx3]; + for i in switch_fork_number..final_number { + let difficulty = parent.difficulty().clone(); + let new_block = if i == switch_fork_number { + gen_block( + &parent, + difficulty + U256::from(20000u64), + vec![], + txs.clone(), + vec![], + ) + } else if i == switch_fork_number + 2 { + gen_block( + &parent, + difficulty + U256::from(20000u64), + txs.clone(), + vec![], + vec![], + ) + } else { + gen_block( + &parent, + difficulty + U256::from(20000u64), + vec![], + vec![], + vec![], + ) + }; + chain2.push(new_block.clone()); + parent = new_block.header().clone(); + } + + for block in &chain1 { + chain_controller + .process_block(Arc::new(block.clone())) + .expect("process block ok"); + } + + for block in chain2.iter().take(switch_fork_number + 1) { + chain_controller + .process_block(Arc::new(block.clone())) + .expect("process block ok"); + } + + assert_eq!( + "Err(InvalidTransaction(\"Transactions((2, Conflict))\"))", + format!( + "{:?}", + chain_controller.process_block(Arc::new(chain2[switch_fork_number + 1].clone())) + ) + ); +} + +#[test] +fn test_dead_cell_in_different_block() { + let (chain_controller, shared) = start_chain(None, true); + let final_number = 20; + let switch_fork_number = 10; + + let mut chain1: Vec = Vec::new(); + let mut chain2: Vec = Vec::new(); + + let mut parent = shared.block_header(&shared.block_hash(0).unwrap()).unwrap(); + for _ in 1..final_number { + let difficulty = parent.difficulty().clone(); + let new_block = gen_block( + &parent, + difficulty + U256::from(100u64), + vec![], + vec![], + vec![], + ); + chain1.push(new_block.clone()); + parent = new_block.header().clone(); + } + + parent = shared.block_header(&shared.block_hash(0).unwrap()).unwrap(); + for _ in 1..switch_fork_number { + let difficulty = parent.difficulty().clone(); + let new_block = gen_block( + &parent, + difficulty + U256::from(99u64), + vec![], + vec![], + vec![], + ); + chain2.push(new_block.clone()); + parent = new_block.header().clone(); + } + + let last_cell_base = &chain2.last().unwrap().commit_transactions()[0]; + let tx1 = create_transaction(last_cell_base.hash(), 1); + let tx2 = create_transaction(tx1.hash(), 2); + let tx3 = create_transaction(tx1.hash(), 3); + for i in switch_fork_number..final_number { + let difficulty = parent.difficulty().clone(); + let new_block = if i == switch_fork_number { + gen_block( + &parent, + difficulty + U256::from(20000u64), + vec![], + vec![tx1.clone(), tx2.clone(), tx3.clone()], + vec![], + ) + } else if i == switch_fork_number + 2 { + gen_block( + &parent, + difficulty + U256::from(20000u64), + vec![tx1.clone(), tx2.clone()], + vec![], + vec![], + ) + } else if i == switch_fork_number + 3 { + gen_block( + &parent, + difficulty + U256::from(20000u64), + vec![tx3.clone()], + vec![], + vec![], + ) + } else { + gen_block( + &parent, + difficulty + U256::from(20000u64), + vec![], + vec![], + vec![], + ) + }; + chain2.push(new_block.clone()); + parent = new_block.header().clone(); + } + + for block in &chain1 { + chain_controller + .process_block(Arc::new(block.clone())) + .expect("process block ok"); + } + + for block in chain2.iter().take(switch_fork_number + 2) { + chain_controller + .process_block(Arc::new(block.clone())) + .expect("process block ok"); + } + + assert_eq!( + "Err(InvalidTransaction(\"Transactions((0, Conflict))\"))", + format!( + "{:?}", + chain_controller.process_block(Arc::new(chain2[switch_fork_number + 2].clone())) + ) + ); +} diff --git a/chain/src/tests/mod.rs b/chain/src/tests/mod.rs index 757d06d3ca..969c0a2f57 100644 --- a/chain/src/tests/mod.rs +++ b/chain/src/tests/mod.rs @@ -1,3 +1,4 @@ mod basic; +mod delay_verify; mod find_fork; mod util; From c3735207f25caa4e33a8115b9677c370dd7ecb67 Mon Sep 17 00:00:00 2001 From: quake wang Date: Fri, 22 Mar 2019 12:24:05 +0900 Subject: [PATCH 64/99] chore: remove unused test case --- verification/src/tests/block_verifier.rs | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/verification/src/tests/block_verifier.rs b/verification/src/tests/block_verifier.rs index 46b7e258f8..7020930f4c 100644 --- a/verification/src/tests/block_verifier.rs +++ b/verification/src/tests/block_verifier.rs @@ -130,29 +130,6 @@ pub fn test_cellbase_with_fee() { assert!(verifier.verify(&block).is_ok()); } -#[test] -pub fn test_cellbase_with_more_reward_than_available() { - let mut transaction_fees = HashMap::>::new(); - let transaction = create_normal_transaction(); - transaction_fees.insert(transaction.hash().clone(), Ok(10)); - - let block = BlockBuilder::default() - .commit_transaction(create_cellbase_transaction_with_capacity(130)) - .commit_transaction(transaction) - .build(); - - let provider = DummyChainProvider { - block_reward: 100, - transaction_fees, - }; - - let verifier = CellbaseVerifier::new(provider); - assert_eq!( - verifier.verify(&block), - Err(VerifyError::Cellbase(CellbaseError::InvalidReward)) - ); -} - #[test] pub fn test_empty_transactions() { let block = BlockBuilder::default().build(); From 6ee407f1e80ed9211be4d9e7b8da075394cfbd6b Mon Sep 17 00:00:00 2001 From: quake wang Date: Fri, 22 Mar 2019 12:24:34 +0900 Subject: [PATCH 65/99] chore: resolve review issue --- core/src/cell.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/core/src/cell.rs b/core/src/cell.rs index 40b11ce4b0..4640f406e6 100644 --- a/core/src/cell.rs +++ b/core/src/cell.rs @@ -88,18 +88,20 @@ where } pub struct BlockCellProvider<'a> { - output_indexs: FnvHashMap, + output_indices: FnvHashMap, block: &'a Block, } impl<'a> BlockCellProvider<'a> { pub fn new(block: &'a Block) -> Self { - let mut output_indexs = FnvHashMap::default(); - for (i, tx) in block.commit_transactions().iter().enumerate() { - output_indexs.insert(tx.hash(), i); - } + let output_indices = block + .commit_transactions() + .iter() + .enumerate() + .map(|(idx, tx)| (tx.hash(), idx)) + .collect(); Self { - output_indexs, + output_indices, block, } } @@ -107,7 +109,7 @@ impl<'a> BlockCellProvider<'a> { impl<'a> CellProvider for BlockCellProvider<'a> { fn cell(&self, out_point: &OutPoint) -> CellStatus { - if let Some(i) = self.output_indexs.get(&out_point.hash) { + if let Some(i) = self.output_indices.get(&out_point.hash) { match self.block.commit_transactions()[*i] .outputs() .get(out_point.index as usize) From 6ccbf0a5c137f9492604253ccd1bd7c4ae722617 Mon Sep 17 00:00:00 2001 From: jjy Date: Thu, 21 Mar 2019 10:14:58 +0800 Subject: [PATCH 66/99] refactor: rename connected_time to last_connected_at --- network/src/peer_store/db.rs | 26 ++++++++++----------- network/src/peer_store/sqlite_peer_store.rs | 10 ++++---- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/network/src/peer_store/db.rs b/network/src/peer_store/db.rs index b2284bf628..5ee389f3b3 100644 --- a/network/src/peer_store/db.rs +++ b/network/src/peer_store/db.rs @@ -21,7 +21,7 @@ pub fn create_tables(conn: &Connection) -> DBResult<()> { status INTEGER NOT NULL, endpoint INTEGER NOT NULL, ban_time INTEGER NOT NULL, - connected_time INTEGER NOT NULL + last_connected_at INTEGER NOT NULL ); "#; conn.execute_batch(sql)?; @@ -53,7 +53,7 @@ pub struct PeerInfo { pub status: Status, pub endpoint: SessionType, pub ban_time: Duration, - pub connected_time: Duration, + pub last_connected_at: Duration, } impl PeerInfo { @@ -63,18 +63,18 @@ impl PeerInfo { connected_addr: &Multiaddr, endpoint: SessionType, score: Score, - connected_time: Duration, + last_connected_at: Duration, ) -> DBResult { let network_group = connected_addr.network_group(); - let mut stmt = conn.prepare("INSERT INTO peer_info (peer_id, connected_addr, score, status, endpoint, connected_time, network_group, ban_time) - VALUES(:peer_id, :connected_addr, :score, :status, :endpoint, :connected_time, :network_group, 0)").expect("prepare"); + let mut stmt = conn.prepare("INSERT INTO peer_info (peer_id, connected_addr, score, status, endpoint, last_connected_at, network_group, ban_time) + VALUES(:peer_id, :connected_addr, :score, :status, :endpoint, :last_connected_at, :network_group, 0)").expect("prepare"); stmt.execute_named(&[ (":peer_id", &peer_id.as_bytes()), (":connected_addr", &connected_addr.to_bytes()), (":score", &score), (":status", &status_to_u8(Status::Unknown)), (":endpoint", &endpoint_to_bool(endpoint)), - (":connected_time", &duration_to_secs(connected_time)), + (":last_connected_at", &duration_to_secs(last_connected_at)), (":network_group", &network_group_to_bytes(&network_group)), ]) .map_err(Into::into) @@ -84,17 +84,17 @@ impl PeerInfo { id: u32, connected_addr: &Multiaddr, endpoint: SessionType, - connected_time: Duration, + last_connected_at: Duration, ) -> DBResult { let mut stmt = conn .prepare( - "UPDATE peer_info SET connected_addr=:connected_addr, endpoint=:endpoint, connected_time=:connected_time WHERE id=:id", + "UPDATE peer_info SET connected_addr=:connected_addr, endpoint=:endpoint, last_connected_at=:last_connected_at WHERE id=:id", ) .expect("prepare"); stmt.execute_named(&[ (":connected_addr", &connected_addr.to_bytes()), (":endpoint", &endpoint_to_bool(endpoint)), - (":connected_time", &duration_to_secs(connected_time)), + (":last_connected_at", &duration_to_secs(last_connected_at)), (":id", &id), ]) .map_err(Into::into) @@ -106,7 +106,7 @@ impl PeerInfo { } pub fn get_by_peer_id(conn: &Connection, peer_id: &PeerId) -> DBResult> { - conn.query_row("SELECT id, peer_id, connected_addr, score, status, endpoint, ban_time, connected_time FROM peer_info WHERE peer_id=?1 LIMIT 1", &[&peer_id.as_bytes()], |row| PeerInfo { + conn.query_row("SELECT id, peer_id, connected_addr, score, status, endpoint, ban_time, last_connected_at FROM peer_info WHERE peer_id=?1 LIMIT 1", &[&peer_id.as_bytes()], |row| PeerInfo { id: row.get(0), peer_id: PeerId::from_bytes(row.get(1)).expect("parse peer_id"), connected_addr: Multiaddr::from_bytes(row.get(2)).expect("parse multiaddr"), @@ -114,7 +114,7 @@ impl PeerInfo { status: u8_to_status(row.get::<_, u8>(4)), endpoint: bool_to_endpoint(row.get::<_, bool>(5)), ban_time: secs_to_duration(row.get(6)), - connected_time: secs_to_duration(row.get(7)), + last_connected_at: secs_to_duration(row.get(7)), }).optional().map_err(Into::into) } @@ -135,7 +135,7 @@ impl PeerInfo { .query_row::<(Vec, u32), _, _>("SELECT network_group, COUNT(network_group) AS network_group_count FROM peer_info GROUP BY network_group ORDER BY network_group_count DESC LIMIT 1", NO_PARAMS, |r| (r.get(0), r.get(1)))?; - let mut stmt = conn.prepare("SELECT id, peer_id, connected_addr, score, status, endpoint, ban_time, connected_time FROM peer_info + let mut stmt = conn.prepare("SELECT id, peer_id, connected_addr, score, status, endpoint, ban_time, last_connected_at FROM peer_info WHERE network_group=:network_group")?; let rows = stmt.query_map_named(&[(":network_group", &network_group)], |row| PeerInfo { id: row.get(0), @@ -145,7 +145,7 @@ impl PeerInfo { status: u8_to_status(row.get::<_, u8>(4)), endpoint: bool_to_endpoint(row.get::<_, bool>(5)), ban_time: secs_to_duration(row.get(6)), - connected_time: secs_to_duration(row.get(7)), + last_connected_at: secs_to_duration(row.get(7)), })?; Result::from_iter(rows).map_err(Into::into) } diff --git a/network/src/peer_store/sqlite_peer_store.rs b/network/src/peer_store/sqlite_peer_store.rs index eebfb3338c..1f4c819b85 100644 --- a/network/src/peer_store/sqlite_peer_store.rs +++ b/network/src/peer_store/sqlite_peer_store.rs @@ -136,7 +136,7 @@ impl SqlitePeerStore { unix_time() - Duration::from_secs(PEER_NOT_SEEN_TIMEOUT_SECS.into()); peers .into_iter() - .filter(move |peer| peer.connected_time < not_seen_timeout) + .filter(move |peer| peer.last_connected_at < not_seen_timeout) }; let candidate_peer = match candidate_peers.min_by_key(|peer| peer.score) { Some(peer) => peer, @@ -162,17 +162,17 @@ impl SqlitePeerStore { peer_id: &PeerId, addr: &Multiaddr, endpoint: SessionType, - connected_time: Duration, + last_connected_at: Duration, ) -> db::PeerInfo { self.pool .fetch(|conn| { db::PeerInfo::get_by_peer_id(conn, peer_id).and_then(|peer| match peer { Some(mut peer) => { - db::PeerInfo::update(conn, peer.id, &addr, endpoint, connected_time) + db::PeerInfo::update(conn, peer.id, &addr, endpoint, last_connected_at) .expect("update peer info"); peer.connected_addr = addr.to_owned(); peer.endpoint = endpoint; - peer.connected_time = connected_time; + peer.last_connected_at = last_connected_at; Ok(Some(peer)) } None => { @@ -182,7 +182,7 @@ impl SqlitePeerStore { &addr, endpoint, self.scoring_schema().peer_init_score(), - connected_time, + last_connected_at, )?; db::PeerInfo::get_by_peer_id(conn, &peer_id) } From 5a786291faf9984eb64b7e2f9cd0bb513030f25a Mon Sep 17 00:00:00 2001 From: jjy Date: Thu, 21 Mar 2019 11:12:20 +0800 Subject: [PATCH 67/99] refactor: Remove unnecessary interface from PeerStore --- network/src/benches/sqlite_peer_store.rs | 14 +-- network/src/network.rs | 2 +- network/src/peer_store.rs | 127 ++++++-------------- network/src/peer_store/score.rs | 64 ++++++++++ network/src/peer_store/sqlite_peer_store.rs | 55 +++------ network/src/peers_registry.rs | 2 +- network/src/service/ckb_service.rs | 2 +- network/src/tests/sqlite_peer_store.rs | 45 ++----- 8 files changed, 143 insertions(+), 168 deletions(-) create mode 100644 network/src/peer_store/score.rs diff --git a/network/src/benches/sqlite_peer_store.rs b/network/src/benches/sqlite_peer_store.rs index 0e74b08ff7..1497f4d0a2 100644 --- a/network/src/benches/sqlite_peer_store.rs +++ b/network/src/benches/sqlite_peer_store.rs @@ -20,7 +20,7 @@ fn insert_peer_info_benchmark(c: &mut Criterion) { let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); move || { for peer_id in peer_ids.clone() { - peer_store.new_connected_peer(&peer_id, addr.clone(), SessionType::Client); + peer_store.add_connected_peer(&peer_id, addr.clone(), SessionType::Client); } } }) @@ -32,7 +32,7 @@ fn insert_peer_info_benchmark(c: &mut Criterion) { let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); move || { for peer_id in peer_ids.clone() { - peer_store.new_connected_peer(&peer_id, addr.clone(), SessionType::Client); + peer_store.add_connected_peer(&peer_id, addr.clone(), SessionType::Client); } } }) @@ -46,7 +46,7 @@ fn insert_peer_info_benchmark(c: &mut Criterion) { let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); move || { for peer_id in peer_ids.clone() { - peer_store.new_connected_peer(&peer_id, addr.clone(), SessionType::Client); + peer_store.add_connected_peer(&peer_id, addr.clone(), SessionType::Client); } } }) @@ -61,8 +61,8 @@ fn random_order_benchmark(c: &mut Criterion) { let mut peer_store = peer_store.lock(); for _ in 0..8000 { let peer_id = random_peer_id(); - peer_store.new_connected_peer(&peer_id, addr.clone(), SessionType::Client); - let _ = peer_store.add_discovered_address(&peer_id, addr.clone()); + peer_store.add_connected_peer(&peer_id, addr.clone(), SessionType::Client); + let _ = peer_store.add_discovered_addr(&peer_id, addr.clone()); } } c.bench_function("random order 1000 / 8000 peer_info", { @@ -108,8 +108,8 @@ fn random_order_benchmark(c: &mut Criterion) { let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); for _ in 0..8000 { let peer_id = random_peer_id(); - peer_store.new_connected_peer(&peer_id, addr.clone(), SessionType::Client); - let _ = peer_store.add_discovered_address(&peer_id, addr.clone()); + peer_store.add_connected_peer(&peer_id, addr.clone(), SessionType::Client); + let _ = peer_store.add_discovered_addr(&peer_id, addr.clone()); } move || { let count = 1000; diff --git a/network/src/network.rs b/network/src/network.rs index c6f574e434..82ffc0708c 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -242,7 +242,7 @@ impl Network { let _ = self .peer_store() .write() - .add_discovered_address(peer_id, address); + .add_discovered_addr(peer_id, address); } fn to_external_url(&self, addr: &Multiaddr) -> String { diff --git a/network/src/peer_store.rs b/network/src/peer_store.rs index 6b3c0bd268..f30c9285c5 100644 --- a/network/src/peer_store.rs +++ b/network/src/peer_store.rs @@ -3,22 +3,50 @@ pub mod sqlite; pub use crate::{peer_store::sqlite_peer_store::SqlitePeerStore, SessionType}; #[cfg(db_trace)] pub mod db_trace; +mod score; pub(crate) mod sqlite_peer_store; pub(crate) use crate::PeerId; -use fnv::FnvHashMap; use p2p::multiaddr::Multiaddr; +pub use score::{Behaviour, Score, ScoringSchema}; use std::time::Duration; -#[allow(dead_code)] -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] -pub enum Behaviour { - FailedToConnect, - FailedToPing, - Ping, - Connect, - UnexpectedDisconnect, +/// PeerStore +/// See [rfc0007](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0007-scoring-system-and-network-security/0007-scoring-system-and-network-security.md) for details. +pub trait PeerStore: Send + Sync { + /// Add a peer and address into peer_store + /// this method will assume peer is connected, which implies address is "verified". + fn add_connected_peer(&mut self, peer_id: &PeerId, address: Multiaddr, endpoint: SessionType); + /// Add discovered peer addresses + /// this method will assume peer and addr is untrust since we have not connected to it. + fn add_discovered_addr(&mut self, peer_id: &PeerId, address: Multiaddr) -> bool; + /// Report peer behaviours + fn report(&mut self, peer_id: &PeerId, behaviour: Behaviour) -> ReportResult; + /// Update peer status + fn update_status(&mut self, peer_id: &PeerId, status: Status); + fn peer_status(&self, peer_id: &PeerId) -> Status; + fn peer_score(&self, peer_id: &PeerId) -> Option; + /// Add bootnode + fn add_bootnode(&mut self, peer_id: PeerId, addr: Multiaddr); + /// This method randomly return peers, it return bootnodes if no other peers in PeerStore. + fn bootnodes(&self, count: u32) -> Vec<(PeerId, Multiaddr)>; + /// Get addrs of a peer, note a peer may have multiple addrs + fn peer_addrs(&self, peer_id: &PeerId, count: u32) -> Option>; + /// Get peers for outbound connection, this method randomly return non-connected peer addrs + fn peers_to_attempt(&self, count: u32) -> Vec<(PeerId, Multiaddr)>; + /// Randomly get peers + fn random_peers(&self, count: u32) -> Vec<(PeerId, Multiaddr)>; + /// Ban a peer + fn ban_peer(&mut self, peer_id: &PeerId, timeout: Duration); + fn is_banned(&self, peer_id: &PeerId) -> bool; + fn scoring_schema(&self) -> &ScoringSchema; + fn peer_score_or_default(&self, peer_id: &PeerId) -> Score { + self.peer_score(peer_id) + .unwrap_or_else(|| self.scoring_schema().peer_init_score()) + } } + +/// Peer Status #[derive(Debug, Copy, Clone, Eq, PartialEq)] pub enum Status { Connected = 0, @@ -53,84 +81,3 @@ impl ReportResult { self == ReportResult::Ok } } - -pub type Score = i32; - -pub struct ScoringSchema { - schema: FnvHashMap, - peer_init_score: Score, - ban_score: Score, - default_ban_timeout: Duration, -} - -impl ScoringSchema { - pub fn new_default() -> Self { - let schema = [ - (Behaviour::FailedToConnect, -20), - (Behaviour::FailedToPing, -10), - (Behaviour::Ping, 5), - (Behaviour::Connect, 10), - (Behaviour::UnexpectedDisconnect, -20), - ] - .iter() - .cloned() - .collect(); - ScoringSchema { - schema, - peer_init_score: 100, - ban_score: 40, - default_ban_timeout: Duration::from_secs(24 * 3600), - } - } - - pub fn peer_init_score(&self) -> Score { - self.peer_init_score - } - - pub fn ban_score(&self) -> Score { - self.ban_score - } - - pub fn get_score(&self, behaviour: Behaviour) -> Option { - self.schema.get(&behaviour).cloned() - } - - pub fn default_ban_timeout(&self) -> Duration { - self.default_ban_timeout - } -} - -impl Default for ScoringSchema { - fn default() -> Self { - ScoringSchema::new_default() - } -} - -pub trait PeerStore: Send + Sync { - // initial or update peer_info in peer_store - fn new_connected_peer(&mut self, peer_id: &PeerId, address: Multiaddr, endpoint: SessionType); - // add peer discovered addresses, return numbers of new inserted line, return Err if peer not exists - fn add_discovered_address(&mut self, peer_id: &PeerId, address: Multiaddr) -> Result<(), ()>; - fn add_discovered_addresses( - &mut self, - peer_id: &PeerId, - address: Vec, - ) -> Result; - fn report(&mut self, peer_id: &PeerId, behaviour: Behaviour) -> ReportResult; - fn update_status(&mut self, peer_id: &PeerId, status: Status); - fn peer_status(&self, peer_id: &PeerId) -> Status; - fn peer_score(&self, peer_id: &PeerId) -> Option; - fn add_bootnode(&mut self, peer_id: PeerId, addr: Multiaddr); - // should return high scored nodes if possible, otherwise, return boostrap nodes - fn bootnodes(&self, count: u32) -> Vec<(PeerId, Multiaddr)>; - fn peer_addrs(&self, peer_id: &PeerId, count: u32) -> Option>; - fn peers_to_attempt(&self, count: u32) -> Vec<(PeerId, Multiaddr)>; - fn random_peers(&self, count: u32) -> Vec<(PeerId, Multiaddr)>; - fn ban_peer(&mut self, peer_id: &PeerId, timeout: Duration); - fn is_banned(&self, peer_id: &PeerId) -> bool; - fn scoring_schema(&self) -> &ScoringSchema; - fn peer_score_or_default(&self, peer_id: &PeerId) -> Score { - self.peer_score(peer_id) - .unwrap_or_else(|| self.scoring_schema().peer_init_score()) - } -} diff --git a/network/src/peer_store/score.rs b/network/src/peer_store/score.rs new file mode 100644 index 0000000000..987585d0d7 --- /dev/null +++ b/network/src/peer_store/score.rs @@ -0,0 +1,64 @@ +use fnv::FnvHashMap; +use std::time::Duration; +/// Peer Behaviours +#[allow(dead_code)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +pub enum Behaviour { + FailedToConnect, + FailedToPing, + Ping, + Connect, + UnexpectedDisconnect, +} + +pub type Score = i32; + +pub struct ScoringSchema { + schema: FnvHashMap, + peer_init_score: Score, + ban_score: Score, + default_ban_timeout: Duration, +} + +impl ScoringSchema { + pub fn new_default() -> Self { + let schema = [ + (Behaviour::FailedToConnect, -20), + (Behaviour::FailedToPing, -10), + (Behaviour::Ping, 5), + (Behaviour::Connect, 10), + (Behaviour::UnexpectedDisconnect, -20), + ] + .iter() + .cloned() + .collect(); + ScoringSchema { + schema, + peer_init_score: 100, + ban_score: 40, + default_ban_timeout: Duration::from_secs(24 * 3600), + } + } + + pub fn peer_init_score(&self) -> Score { + self.peer_init_score + } + + pub fn ban_score(&self) -> Score { + self.ban_score + } + + pub fn get_score(&self, behaviour: Behaviour) -> Option { + self.schema.get(&behaviour).cloned() + } + + pub fn default_ban_timeout(&self) -> Duration { + self.default_ban_timeout + } +} + +impl Default for ScoringSchema { + fn default() -> Self { + ScoringSchema::new_default() + } +} diff --git a/network/src/peer_store/sqlite_peer_store.rs b/network/src/peer_store/sqlite_peer_store.rs index 1f4c819b85..7ea32baa09 100644 --- a/network/src/peer_store/sqlite_peer_store.rs +++ b/network/src/peer_store/sqlite_peer_store.rs @@ -1,3 +1,14 @@ +/// SqlitePeerStore +/// Principles: +/// 1. PeerId is easy to be generated, should never use a PeerId as an identity. +/// 2. Peer's connected addr should be use as an identify to ban a peer, it is based on our +/// assumption that IP is a limited resource. +/// Solution: +/// 1. Through PeerId to ban or score a peer. +/// 2. When a peer get banned we also ban peer's connected addr. +/// 3. A bad peer can always avoid punishment by change it's PeerId, but it can't get high +/// score. +/// 4. Good peers can get higher score than bad peers. use super::{Behaviour, Multiaddr, PeerId, PeerStore, ReportResult, Score, ScoringSchema, Status}; use crate::network_group::MultiaddrExt; use crate::peer_store::db; @@ -14,13 +25,6 @@ pub(crate) const PEER_NOT_SEEN_TIMEOUT_SECS: u32 = 14 * 24 * 3600; const BAN_LIST_CLEAR_EXPIRES_SIZE: usize = 255; const DEFAULT_POOL_SIZE: u32 = 16; -// Scoring and ban: -// Because peer_id is easy to forge, we should consider to identify a peer by it's connected_addr -// instead of peer_id -// Howerver connected_addr maybe same for multiple inbound peers, these peers may in the same sub network or our node may behind a reverse proxy, so we can't just reject them. -// A solution is to identify and score a peer by it's peer_id, but ban a peer through it connected_addr, it's -// mean when a peer got banned, we're no longer accept new peers from the same connected_addr. - pub struct SqlitePeerStore { bootnodes: Vec<(PeerId, Multiaddr)>, schema: ScoringSchema, @@ -118,7 +122,7 @@ impl SqlitePeerStore { Ok(()) } - // check and try to delete peer_info if peer_infos reach limit + /// check and try delete peer_info if peer_infos reach limit fn check_store_limit(&mut self) -> Result<(), ()> { let peer_info_count = self .pool @@ -224,7 +228,7 @@ impl SqlitePeerStore { } impl PeerStore for SqlitePeerStore { - fn new_connected_peer(&mut self, peer_id: &PeerId, addr: Multiaddr, endpoint: SessionType) { + fn add_connected_peer(&mut self, peer_id: &PeerId, addr: Multiaddr, endpoint: SessionType) { if self.check_store_limit().is_err() { return; } @@ -233,38 +237,17 @@ impl PeerStore for SqlitePeerStore { self.get_and_upsert_peer_info_with(peer_id, &addr, endpoint, now); } - fn add_discovered_address(&mut self, peer_id: &PeerId, addr: Multiaddr) -> Result<(), ()> { - self.check_store_limit()?; + fn add_discovered_addr(&mut self, peer_id: &PeerId, addr: Multiaddr) -> bool { + // peer store is full + if self.check_store_limit().is_err() { + return false; + } let id = self.get_or_insert_peer_info(peer_id).id; let inserted = self .pool .fetch(|conn| db::PeerAddr::insert(&conn, id, &addr)) .expect("insert addr"); - if inserted > 0 { - Ok(()) - } else { - Err(()) - } - } - - fn add_discovered_addresses( - &mut self, - peer_id: &PeerId, - addrs: Vec, - ) -> Result { - self.check_store_limit()?; - let id = self.get_or_insert_peer_info(peer_id).id; - let count = self - .pool - .fetch(|conn| { - let mut count = 0; - for addr in &addrs { - count += db::PeerAddr::insert(&conn, id, &addr)?; - } - Ok(count) - }) - .expect("insert addrs"); - Ok(count) + inserted > 0 } fn report(&mut self, peer_id: &PeerId, behaviour: Behaviour) -> ReportResult { diff --git a/network/src/peers_registry.rs b/network/src/peers_registry.rs index 7324f6c92e..2128b78570 100644 --- a/network/src/peers_registry.rs +++ b/network/src/peers_registry.rs @@ -390,7 +390,7 @@ impl PeersRegistry { connected_addr: Multiaddr, session: Session, ) -> RegisterResult { - self.peer_store.write().new_connected_peer( + self.peer_store.write().add_connected_peer( &peer_id, connected_addr.clone(), session.session_type, diff --git a/network/src/service/ckb_service.rs b/network/src/service/ckb_service.rs index f9bff7f7ee..12b596523a 100644 --- a/network/src/service/ckb_service.rs +++ b/network/src/service/ckb_service.rs @@ -51,7 +51,7 @@ impl Stream for CKBService { let mut peer_store = network.peer_store().write(); peer_store.report(&peer_id, Behaviour::Connect); peer_store.update_status(&peer_id, Status::Connected); - let _ = peer_store.add_discovered_address(&peer_id, addr); + let _ = peer_store.add_discovered_addr(&peer_id, addr); } // call handler match self.find_handler(&peer_id, protocol_id) { diff --git a/network/src/tests/sqlite_peer_store.rs b/network/src/tests/sqlite_peer_store.rs index 5611467388..bbc844bb1c 100644 --- a/network/src/tests/sqlite_peer_store.rs +++ b/network/src/tests/sqlite_peer_store.rs @@ -9,11 +9,11 @@ use crate::{ use std::time::Duration; #[test] -fn test_new_connected_peer() { +fn test_add_connected_peer() { let mut peer_store: Box = Box::new(SqlitePeerStore::temp()); let peer_id = random_peer_id(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); - peer_store.new_connected_peer(&peer_id, addr, SessionType::Client); + peer_store.add_connected_peer(&peer_id, addr, SessionType::Client); assert_eq!( peer_store.peer_score(&peer_id).unwrap(), peer_store.scoring_schema().peer_init_score() @@ -22,24 +22,11 @@ fn test_new_connected_peer() { } #[test] -fn test_add_discovered_address() { +fn test_add_discovered_addr() { let mut peer_store: Box = Box::new(SqlitePeerStore::temp()); let peer_id = random_peer_id(); - peer_store - .add_discovered_address(&peer_id, "/ip4/127.0.0.1".to_multiaddr().unwrap()) - .expect("add discovered address"); + peer_store.add_discovered_addr(&peer_id, "/ip4/127.0.0.1".to_multiaddr().unwrap()); assert_eq!(peer_store.peer_addrs(&peer_id, 2).unwrap().len(), 1); - peer_store - .add_discovered_addresses( - &peer_id, - vec![ - "/ip4/127.0.0.1".to_multiaddr().unwrap(), - "/ip4/192.168.2.2".to_multiaddr().unwrap(), - "/ip4/192.168.2.3".to_multiaddr().unwrap(), - ], - ) - .expect("add discovered address"); - assert_eq!(peer_store.peer_addrs(&peer_id, 4).unwrap().len(), 3); } #[test] @@ -59,7 +46,7 @@ fn test_update_status() { peer_store.update_status(&peer_id, Status::Connected); assert_eq!(peer_store.peer_status(&peer_id), Status::Unknown); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); - peer_store.new_connected_peer(&peer_id, addr, SessionType::Server); + peer_store.add_connected_peer(&peer_id, addr, SessionType::Server); peer_store.update_status(&peer_id, Status::Connected); assert_eq!(peer_store.peer_status(&peer_id), Status::Connected); } @@ -71,7 +58,7 @@ fn test_ban_peer() { peer_store.ban_peer(&peer_id, Duration::from_secs(10)); assert!(!peer_store.is_banned(&peer_id)); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); - peer_store.new_connected_peer(&peer_id, addr, SessionType::Server); + peer_store.add_connected_peer(&peer_id, addr, SessionType::Server); peer_store.ban_peer(&peer_id, Duration::from_secs(10)); assert!(peer_store.is_banned(&peer_id)); } @@ -85,9 +72,7 @@ fn test_bootnodes() { peer_store.add_bootnode(peer_id.clone(), addr.clone()); assert_eq!(peer_store.bootnodes(2).len(), 1); let peer_id2 = random_peer_id(); - peer_store - .add_discovered_address(&peer_id2, addr.clone()) - .expect("add discovered address"); + peer_store.add_discovered_addr(&peer_id2, addr.clone()); assert_eq!( peer_store.bootnodes(3), vec![(peer_id2, addr.clone()), (peer_id, addr)] @@ -103,9 +88,7 @@ fn test_peers_to_attempt() { peer_store.add_bootnode(peer_id.clone(), addr.clone()); assert!(peer_store.peers_to_attempt(1).is_empty()); let peer_id2 = random_peer_id(); - peer_store - .add_discovered_address(&peer_id2, addr.clone()) - .expect("add discovered address"); + peer_store.add_discovered_addr(&peer_id2, addr.clone()); assert_eq!(peer_store.peers_to_attempt(2).len(), 1); peer_store.update_status(&peer_id2, Status::Connected); assert!(peer_store.peers_to_attempt(1).is_empty()); @@ -120,9 +103,7 @@ fn test_random_peers() { peer_store.add_bootnode(peer_id.clone(), addr.clone()); assert!(peer_store.random_peers(1).is_empty()); let peer_id2 = random_peer_id(); - peer_store - .add_discovered_address(&peer_id2, addr.clone()) - .expect("add discovered address"); + peer_store.add_discovered_addr(&peer_id2, addr.clone()); assert_eq!(peer_store.random_peers(2).len(), 1); peer_store.update_status(&peer_id2, Status::Connected); assert_eq!(peer_store.random_peers(1).len(), 1); @@ -134,7 +115,7 @@ fn test_delete_peer_info() { let addr1 = "/ip4/127.0.0.1".to_multiaddr().unwrap(); let addr2 = "/ip4/192.163.1.1".to_multiaddr().unwrap(); for _ in 0..(PEER_STORE_LIMIT - 2) { - peer_store.new_connected_peer(&random_peer_id(), addr1.clone(), SessionType::Server); + peer_store.add_connected_peer(&random_peer_id(), addr1.clone(), SessionType::Server); } let evict_target = random_peer_id(); let fake_target = random_peer_id(); @@ -145,8 +126,8 @@ fn test_delete_peer_info() { let faketime_file = faketime::millis_tempfile(recent_not_seen_time.as_secs() * 1000) .expect("create faketime file"); faketime::enable(&faketime_file); - peer_store.new_connected_peer(&evict_target, addr1.clone(), SessionType::Server); - peer_store.new_connected_peer(&fake_target, addr2, SessionType::Server); + peer_store.add_connected_peer(&evict_target, addr1.clone(), SessionType::Server); + peer_store.add_connected_peer(&fake_target, addr2, SessionType::Server); } peer_store.report(&evict_target, Behaviour::FailedToPing); peer_store.report(&fake_target, Behaviour::FailedToPing); @@ -157,7 +138,7 @@ fn test_delete_peer_info() { < peer_store.scoring_schema().peer_init_score() ); // should evict evict_target and accept this - peer_store.new_connected_peer(&random_peer_id(), addr1, SessionType::Server); + peer_store.add_connected_peer(&random_peer_id(), addr1, SessionType::Server); // evict_target is evicted in previous step assert_eq!( peer_store.peer_score_or_default(&evict_target), From cf2873f1f350271933ba820c00f7eb3a0a801e33 Mon Sep 17 00:00:00 2001 From: jjy Date: Thu, 21 Mar 2019 12:28:52 +0800 Subject: [PATCH 68/99] refactor: move SqlitePeerStore to correct modules --- network/src/benches/sqlite_peer_store.rs | 10 ++-- network/src/errors.rs | 35 ++++++----- network/src/network.rs | 12 ++-- network/src/peer_store.rs | 8 +-- network/src/peer_store/sqlite.rs | 60 +++++++++++-------- network/src/peer_store/{ => sqlite}/db.rs | 6 +- .../src/peer_store/{ => sqlite}/db_trace.rs | 0 .../peer_store.rs} | 35 +++++------ network/src/tests/peers_registry.rs | 2 +- network/src/tests/sqlite_peer_store.rs | 20 +++---- 10 files changed, 99 insertions(+), 89 deletions(-) rename network/src/peer_store/{ => sqlite}/db.rs (98%) rename network/src/peer_store/{ => sqlite}/db_trace.rs (100%) rename network/src/peer_store/{sqlite_peer_store.rs => sqlite/peer_store.rs} (94%) diff --git a/network/src/benches/sqlite_peer_store.rs b/network/src/benches/sqlite_peer_store.rs index 1497f4d0a2..429ed04341 100644 --- a/network/src/benches/sqlite_peer_store.rs +++ b/network/src/benches/sqlite_peer_store.rs @@ -15,7 +15,7 @@ use std::rc::Rc; fn insert_peer_info_benchmark(c: &mut Criterion) { c.bench_function("insert 100 peer_info", |b| { b.iter({ - let mut peer_store = SqlitePeerStore::default(); + let mut peer_store = SqlitePeerStore::memory().expect("memory"); let peer_ids = (0..100).map(|_| random_peer_id()).collect::>(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); move || { @@ -27,7 +27,7 @@ fn insert_peer_info_benchmark(c: &mut Criterion) { }); c.bench_function("insert 1000 peer_info", |b| { b.iter({ - let mut peer_store = SqlitePeerStore::default(); + let mut peer_store = SqlitePeerStore::memory().expect("memory"); let peer_ids = (0..1000).map(|_| random_peer_id()).collect::>(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); move || { @@ -41,7 +41,7 @@ fn insert_peer_info_benchmark(c: &mut Criterion) { // filesystem benchmark c.bench_function("insert 100 peer_info on filesystem", move |b| { b.iter({ - let mut peer_store = SqlitePeerStore::temp(); + let mut peer_store = SqlitePeerStore::temp().expect("temp"); let peer_ids = (0..100).map(|_| random_peer_id()).collect::>(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); move || { @@ -55,7 +55,7 @@ fn insert_peer_info_benchmark(c: &mut Criterion) { fn random_order_benchmark(c: &mut Criterion) { { - let peer_store = Rc::new(Mutex::new(SqlitePeerStore::default())); + let peer_store = Rc::new(Mutex::new(SqlitePeerStore::memory().expect("memory"))); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); { let mut peer_store = peer_store.lock(); @@ -104,7 +104,7 @@ fn random_order_benchmark(c: &mut Criterion) { "random order 1000 / 8000 peer_info on filesystem", move |b| { b.iter({ - let mut peer_store = SqlitePeerStore::temp(); + let mut peer_store = SqlitePeerStore::temp().expect("temp"); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); for _ in 0..8000 { let peer_id = random_peer_id(); diff --git a/network/src/errors.rs b/network/src/errors.rs index 5b9fa1f801..242eb01cb7 100644 --- a/network/src/errors.rs +++ b/network/src/errors.rs @@ -1,6 +1,5 @@ -use crate::{PeerIndex, ProtocolId}; -use p2p::{error::Error as P2PError, secio::PeerId, service::ServiceTask}; -use std::error; +use crate::{peer_store::sqlite::DBError, PeerIndex, ProtocolId}; +use p2p::secio::PeerId; use std::fmt; use std::fmt::Display; use std::io::Error as IoError; @@ -11,7 +10,8 @@ pub enum Error { Config(ConfigError), Protocol(ProtocolError), Io(IoError), - P2P(P2PError), + P2P(String), + DB(DBError), Shutdown, } @@ -63,25 +63,32 @@ impl From for Error { } } -impl From> for Error { - fn from(err: P2PError) -> Error { - Error::P2P(err) +impl From for Error { + fn from(err: DBError) -> Error { + Error::DB(err) } } impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self) + write!(f, "{:?}", self) } } -impl error::Error for Error { - fn description(&self) -> &str { - "ckb network error" +impl Display for ConfigError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:?}", self) } +} - fn cause(&self) -> Option<&error::Error> { - // Generic error, underlying cause isn't tracked. - None +impl Display for PeerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:?}", self) + } +} + +impl Display for ProtocolError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:?}", self) } } diff --git a/network/src/network.rs b/network/src/network.rs index 82ffc0708c..164ce37d85 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -1,5 +1,5 @@ use crate::errors::{ConfigError, Error, PeerError, ProtocolError}; -use crate::peer_store::{Behaviour, PeerStore, SqlitePeerStore}; +use crate::peer_store::{sqlite::SqlitePeerStore, Behaviour, PeerStore}; use crate::peers_registry::{ ConnectionStatus, Peer, PeerIdentifyInfo, PeersRegistry, RegisterResult, Session, }; @@ -263,7 +263,12 @@ impl Network { .p2p_control .write() .send_message(peer.session.id, protocol_id, data.to_vec()) - .map_err(Into::into), + .map_err(|_| { + Error::P2P(format!( + "error send to peer {:?} protocol {}", + peer_id, protocol_id + )) + }), None => Err(PeerError::ProtocolNotFound(peer_id.to_owned(), protocol_id).into()), }) .unwrap_or_else(|| Err(PeerError::NotFound(peer_id.to_owned()).into())) @@ -360,7 +365,7 @@ impl Network { .map(|addr| (addr.to_owned(), std::u8::MAX)) .collect(); let peer_store: Arc> = { - let mut peer_store = SqlitePeerStore::default(); + let mut peer_store = SqlitePeerStore::memory()?; let bootnodes = config.bootnodes()?; for (peer_id, addr) in bootnodes { peer_store.add_bootnode(peer_id, addr); @@ -470,7 +475,6 @@ impl Network { addr.clone(), err ); - //return Err(ErrorKind::Other(format!("listen address error: {:?}", err)).into()); return Err(Error::Io(err)); } }; diff --git a/network/src/peer_store.rs b/network/src/peer_store.rs index f30c9285c5..54656dfad4 100644 --- a/network/src/peer_store.rs +++ b/network/src/peer_store.rs @@ -1,12 +1,8 @@ -mod db; -pub mod sqlite; -pub use crate::{peer_store::sqlite_peer_store::SqlitePeerStore, SessionType}; -#[cfg(db_trace)] -pub mod db_trace; mod score; -pub(crate) mod sqlite_peer_store; +pub mod sqlite; pub(crate) use crate::PeerId; +pub use crate::{peer_store::sqlite::SqlitePeerStore, SessionType}; use p2p::multiaddr::Multiaddr; pub use score::{Behaviour, Score, ScoringSchema}; use std::time::Duration; diff --git a/network/src/peer_store/sqlite.rs b/network/src/peer_store/sqlite.rs index 264b987816..cc726e5ab8 100644 --- a/network/src/peer_store/sqlite.rs +++ b/network/src/peer_store/sqlite.rs @@ -1,10 +1,36 @@ #![allow(dead_code)] + +mod db; +#[cfg(db_trace)] +pub mod db_trace; +pub(crate) mod peer_store; + use lazy_static::lazy_static; -use r2d2::{Error as PoolError, Pool}; +pub use peer_store::SqlitePeerStore; +pub use r2d2::Error as PoolError; +use r2d2::Pool; use r2d2_sqlite::SqliteConnectionManager; use rusqlite::OpenFlags; pub use rusqlite::{Connection, Error as SqliteError}; +#[derive(Debug)] +pub enum DBError { + Pool(PoolError), + Sqlite(SqliteError), +} + +impl From for DBError { + fn from(err: PoolError) -> Self { + DBError::Pool(err) + } +} + +impl From for DBError { + fn from(err: SqliteError) -> Self { + DBError::Sqlite(err) + } +} + pub type ConnectionPool = Pool; pub type PooledConnection = r2d2::PooledConnection; @@ -25,7 +51,7 @@ pub enum StorePath { File(String), } -pub fn open_pool(store_path: StorePath, max_size: u32) -> ConnectionPool { +pub fn open_pool(store_path: StorePath, max_size: u32) -> Result { let manager = match store_path { StorePath::Memory => { let manager = SqliteConnectionManager::file(SHARED_MEMORY_PATH); @@ -39,10 +65,10 @@ pub fn open_pool(store_path: StorePath, max_size: u32) -> ConnectionPool { Pool::builder() .max_size(max_size) .build(manager) - .expect("build connection pool") + .map_err(Into::into) } -pub fn open(store_path: StorePath) -> Result { +pub fn open(store_path: StorePath) -> Result { match store_path { StorePath::Memory => { Connection::open_with_flags(SHARED_MEMORY_PATH, *MEMORY_OPEN_FLAGS).map_err(Into::into) @@ -53,36 +79,18 @@ pub fn open(store_path: StorePath) -> Result { } } -#[derive(Debug)] -pub enum Error { - Pool(PoolError), - Sqlite(SqliteError), -} - -impl From for Error { - fn from(err: PoolError) -> Self { - Error::Pool(err) - } -} - -impl From for Error { - fn from(err: SqliteError) -> Self { - Error::Sqlite(err) - } -} - pub trait ConnectionPoolExt { - fn fetch Result>( + fn fetch Result>( &self, f: F, - ) -> Result; + ) -> Result; } impl ConnectionPoolExt for ConnectionPool { - fn fetch Result>( + fn fetch Result>( &self, mut f: F, - ) -> Result { + ) -> Result { let mut connection = self.get()?; f(&mut connection) } diff --git a/network/src/peer_store/db.rs b/network/src/peer_store/sqlite/db.rs similarity index 98% rename from network/src/peer_store/db.rs rename to network/src/peer_store/sqlite/db.rs index 5ee389f3b3..af4933f416 100644 --- a/network/src/peer_store/db.rs +++ b/network/src/peer_store/sqlite/db.rs @@ -1,6 +1,6 @@ -use super::{Multiaddr, PeerId, Score, Status}; use crate::network_group::{Group, NetworkGroup}; -use crate::peer_store::sqlite::Error as SqliteError; +use crate::peer_store::sqlite::DBError; +use crate::peer_store::{Multiaddr, PeerId, Score, Status}; use crate::SessionType; use rusqlite::types::ToSql; use rusqlite::OptionalExtension; @@ -8,7 +8,7 @@ use rusqlite::{Connection, NO_PARAMS}; use std::iter::FromIterator; use std::time::Duration; -type DBResult = Result; +type DBResult = Result; pub fn create_tables(conn: &Connection) -> DBResult<()> { let sql = r#" diff --git a/network/src/peer_store/db_trace.rs b/network/src/peer_store/sqlite/db_trace.rs similarity index 100% rename from network/src/peer_store/db_trace.rs rename to network/src/peer_store/sqlite/db_trace.rs diff --git a/network/src/peer_store/sqlite_peer_store.rs b/network/src/peer_store/sqlite/peer_store.rs similarity index 94% rename from network/src/peer_store/sqlite_peer_store.rs rename to network/src/peer_store/sqlite/peer_store.rs index 7ea32baa09..b6cff03b5b 100644 --- a/network/src/peer_store/sqlite_peer_store.rs +++ b/network/src/peer_store/sqlite/peer_store.rs @@ -1,3 +1,5 @@ +use crate::network_group::MultiaddrExt; +use crate::peer_store::sqlite::{self, db, ConnectionPool, ConnectionPoolExt, DBError}; /// SqlitePeerStore /// Principles: /// 1. PeerId is easy to be generated, should never use a PeerId as an identity. @@ -9,10 +11,9 @@ /// 3. A bad peer can always avoid punishment by change it's PeerId, but it can't get high /// score. /// 4. Good peers can get higher score than bad peers. -use super::{Behaviour, Multiaddr, PeerId, PeerStore, ReportResult, Score, ScoringSchema, Status}; -use crate::network_group::MultiaddrExt; -use crate::peer_store::db; -use crate::peer_store::sqlite::{self, ConnectionPool, ConnectionPoolExt}; +use crate::peer_store::{ + Behaviour, Multiaddr, PeerId, PeerStore, ReportResult, Score, ScoringSchema, Status, +}; use crate::SessionType; use faketime::unix_time; use fnv::FnvHashMap; @@ -32,12 +33,6 @@ pub struct SqlitePeerStore { pool: ConnectionPool, } -impl Default for SqlitePeerStore { - fn default() -> Self { - SqlitePeerStore::memory() - } -} - impl SqlitePeerStore { pub fn new(connection_pool: ConnectionPool) -> Self { let mut peer_store = SqlitePeerStore { @@ -50,27 +45,27 @@ impl SqlitePeerStore { peer_store } - fn memory() -> Self { - let pool = sqlite::open_pool(sqlite::StorePath::Memory, DEFAULT_POOL_SIZE); - SqlitePeerStore::new(pool) + pub fn memory() -> Result { + let pool = sqlite::open_pool(sqlite::StorePath::Memory, DEFAULT_POOL_SIZE)?; + Ok(SqlitePeerStore::new(pool)) } #[allow(dead_code)] - pub fn temp() -> Self { - let pool = sqlite::open_pool(sqlite::StorePath::File("".into()), DEFAULT_POOL_SIZE); - SqlitePeerStore::new(pool) + pub fn temp() -> Result { + let pool = sqlite::open_pool(sqlite::StorePath::File("".into()), DEFAULT_POOL_SIZE)?; + Ok(SqlitePeerStore::new(pool)) } - fn prepare(&mut self) -> Result<(), sqlite::Error> { + fn prepare(&mut self) -> Result<(), DBError> { self.create_tables()?; self.load_banlist() } - fn create_tables(&mut self) -> Result<(), sqlite::Error> { + fn create_tables(&mut self) -> Result<(), DBError> { self.pool.fetch(|conn| db::create_tables(conn)) } - fn load_banlist(&mut self) -> Result<(), sqlite::Error> { + fn load_banlist(&mut self) -> Result<(), DBError> { self.clear_expires_banned_ip()?; let now = unix_time(); let ban_records = self.pool.fetch(|conn| db::get_ban_records(conn, now))?; @@ -111,7 +106,7 @@ impl SqlitePeerStore { } } - fn clear_expires_banned_ip(&mut self) -> Result<(), sqlite::Error> { + fn clear_expires_banned_ip(&mut self) -> Result<(), DBError> { let now = unix_time(); let ips = self .pool diff --git a/network/src/tests/peers_registry.rs b/network/src/tests/peers_registry.rs index 4a20df8665..60fb512b6c 100644 --- a/network/src/tests/peers_registry.rs +++ b/network/src/tests/peers_registry.rs @@ -9,7 +9,7 @@ use std::sync::Arc; use std::time::{Duration, Instant}; fn new_peer_store() -> Arc> { - Arc::new(RwLock::new(SqlitePeerStore::temp())) + Arc::new(RwLock::new(SqlitePeerStore::temp().expect("temp"))) } #[test] diff --git a/network/src/tests/sqlite_peer_store.rs b/network/src/tests/sqlite_peer_store.rs index bbc844bb1c..5581817c48 100644 --- a/network/src/tests/sqlite_peer_store.rs +++ b/network/src/tests/sqlite_peer_store.rs @@ -1,7 +1,7 @@ use crate::{ multiaddr::ToMultiaddr, peer_store::{ - sqlite_peer_store::{PEER_NOT_SEEN_TIMEOUT_SECS, PEER_STORE_LIMIT}, + sqlite::peer_store::{PEER_NOT_SEEN_TIMEOUT_SECS, PEER_STORE_LIMIT}, Behaviour, PeerStore, SqlitePeerStore, Status, }, random_peer_id, SessionType, @@ -10,7 +10,7 @@ use std::time::Duration; #[test] fn test_add_connected_peer() { - let mut peer_store: Box = Box::new(SqlitePeerStore::temp()); + let mut peer_store: Box = Box::new(SqlitePeerStore::temp().expect("temp")); let peer_id = random_peer_id(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); peer_store.add_connected_peer(&peer_id, addr, SessionType::Client); @@ -23,7 +23,7 @@ fn test_add_connected_peer() { #[test] fn test_add_discovered_addr() { - let mut peer_store: Box = Box::new(SqlitePeerStore::temp()); + let mut peer_store: Box = Box::new(SqlitePeerStore::temp().expect("temp")); let peer_id = random_peer_id(); peer_store.add_discovered_addr(&peer_id, "/ip4/127.0.0.1".to_multiaddr().unwrap()); assert_eq!(peer_store.peer_addrs(&peer_id, 2).unwrap().len(), 1); @@ -31,7 +31,7 @@ fn test_add_discovered_addr() { #[test] fn test_report() { - let mut peer_store: Box = Box::new(SqlitePeerStore::temp()); + let mut peer_store: Box = Box::new(SqlitePeerStore::temp().expect("temp")); let peer_id = random_peer_id(); assert!(peer_store.report(&peer_id, Behaviour::Ping).is_ok()); assert!( @@ -41,7 +41,7 @@ fn test_report() { #[test] fn test_update_status() { - let mut peer_store: Box = Box::new(SqlitePeerStore::temp()); + let mut peer_store: Box = Box::new(SqlitePeerStore::temp().expect("temp")); let peer_id = random_peer_id(); peer_store.update_status(&peer_id, Status::Connected); assert_eq!(peer_store.peer_status(&peer_id), Status::Unknown); @@ -53,7 +53,7 @@ fn test_update_status() { #[test] fn test_ban_peer() { - let mut peer_store: Box = Box::new(SqlitePeerStore::temp()); + let mut peer_store: Box = Box::new(SqlitePeerStore::temp().expect("temp")); let peer_id = random_peer_id(); peer_store.ban_peer(&peer_id, Duration::from_secs(10)); assert!(!peer_store.is_banned(&peer_id)); @@ -65,7 +65,7 @@ fn test_ban_peer() { #[test] fn test_bootnodes() { - let mut peer_store: Box = Box::new(SqlitePeerStore::temp()); + let mut peer_store: Box = Box::new(SqlitePeerStore::temp().expect("temp")); assert!(peer_store.bootnodes(1).is_empty()); let peer_id = random_peer_id(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); @@ -81,7 +81,7 @@ fn test_bootnodes() { #[test] fn test_peers_to_attempt() { - let mut peer_store: Box = Box::new(SqlitePeerStore::temp()); + let mut peer_store: Box = Box::new(SqlitePeerStore::temp().expect("temp")); assert!(peer_store.peers_to_attempt(1).is_empty()); let peer_id = random_peer_id(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); @@ -96,7 +96,7 @@ fn test_peers_to_attempt() { #[test] fn test_random_peers() { - let mut peer_store: Box = Box::new(SqlitePeerStore::temp()); + let mut peer_store: Box = Box::new(SqlitePeerStore::temp().expect("temp")); assert!(peer_store.random_peers(1).is_empty()); let peer_id = random_peer_id(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); @@ -111,7 +111,7 @@ fn test_random_peers() { #[test] fn test_delete_peer_info() { - let mut peer_store = SqlitePeerStore::temp(); + let mut peer_store = SqlitePeerStore::temp().expect("temp"); let addr1 = "/ip4/127.0.0.1".to_multiaddr().unwrap(); let addr2 = "/ip4/192.163.1.1".to_multiaddr().unwrap(); for _ in 0..(PEER_STORE_LIMIT - 2) { From b6a6cc802366893933ef78fa7903a16c1e56d0d8 Mon Sep 17 00:00:00 2001 From: jjy Date: Thu, 21 Mar 2019 14:59:25 +0800 Subject: [PATCH 69/99] feat: enable SqlitePeerStore file mode --- network/src/lib.rs | 9 +++++---- network/src/network.rs | 5 ++++- network/src/network_config.rs | 3 ++- network/src/peer_store/sqlite/peer_store.rs | 8 ++++++-- nodes_template/default.toml | 3 ++- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/network/src/lib.rs b/network/src/lib.rs index 493d835114..62fcd226a4 100644 --- a/network/src/lib.rs +++ b/network/src/lib.rs @@ -37,7 +37,8 @@ pub type PeerIndex = usize; #[derive(Clone, Debug, PartialEq, Deserialize)] pub struct Config { pub listen_addresses: Vec, - pub secret_file: Option, + pub secret_file: String, + pub peer_store_path: String, pub try_outbound_connect_secs: Option, /// List of initial node addresses pub bootnodes: Vec, @@ -81,9 +82,9 @@ impl From for NetworkConfig { _ => false, }; } - if let Some(dir_path) = config.config_dir_path { - cfg.config_dir_path = Some(dir_path.clone()); - cfg.secret_key_path = Some(format!("{}/secret_key", dir_path)) + if let Some(dir_path) = &config.config_dir_path { + cfg.secret_key_path = Some(format!("{}/{}", dir_path, config.secret_file)); + cfg.peer_store_path = Some(format!("{}/{}", dir_path, config.peer_store_path)); } cfg.client_version = "ckb network".to_string(); match cfg.read_secret_key() { diff --git a/network/src/network.rs b/network/src/network.rs index 164ce37d85..3c82efeb06 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -365,7 +365,10 @@ impl Network { .map(|addr| (addr.to_owned(), std::u8::MAX)) .collect(); let peer_store: Arc> = { - let mut peer_store = SqlitePeerStore::memory()?; + let mut peer_store = match &config.peer_store_path { + Some(path) => SqlitePeerStore::file(path.to_string())?, + None => SqlitePeerStore::memory()?, + }; let bootnodes = config.bootnodes()?; for (peer_id, addr) in bootnodes { peer_store.add_bootnode(peer_id, addr); diff --git a/network/src/network_config.rs b/network/src/network_config.rs index 5990ccdeb2..f0c234acc7 100644 --- a/network/src/network_config.rs +++ b/network/src/network_config.rs @@ -26,7 +26,7 @@ pub struct NetworkConfig { pub reserved_peers: Vec, pub secret_key: Option, pub secret_key_path: Option, - // peer_store path + pub peer_store_path: Option, pub config_dir_path: Option, pub bootnodes: Vec, pub ping_interval: Duration, @@ -150,6 +150,7 @@ impl Default for NetworkConfig { discovery_response_count: 20, discovery_interval: Duration::from_secs(15), try_outbound_connect_interval: Duration::from_secs(15), + peer_store_path: None, } } } diff --git a/network/src/peer_store/sqlite/peer_store.rs b/network/src/peer_store/sqlite/peer_store.rs index b6cff03b5b..9e3f55f57d 100644 --- a/network/src/peer_store/sqlite/peer_store.rs +++ b/network/src/peer_store/sqlite/peer_store.rs @@ -45,6 +45,11 @@ impl SqlitePeerStore { peer_store } + pub fn file(path: String) -> Result { + let pool = sqlite::open_pool(sqlite::StorePath::File(path), DEFAULT_POOL_SIZE)?; + Ok(SqlitePeerStore::new(pool)) + } + pub fn memory() -> Result { let pool = sqlite::open_pool(sqlite::StorePath::Memory, DEFAULT_POOL_SIZE)?; Ok(SqlitePeerStore::new(pool)) @@ -52,8 +57,7 @@ impl SqlitePeerStore { #[allow(dead_code)] pub fn temp() -> Result { - let pool = sqlite::open_pool(sqlite::StorePath::File("".into()), DEFAULT_POOL_SIZE)?; - Ok(SqlitePeerStore::new(pool)) + Self::file("".into()) } fn prepare(&mut self) -> Result<(), DBError> { diff --git a/nodes_template/default.toml b/nodes_template/default.toml index 9af7b7c55d..4274b55df6 100644 --- a/nodes_template/default.toml +++ b/nodes_template/default.toml @@ -18,7 +18,8 @@ reserved_nodes = [] only_reserved_peers = false min_peers = 4 max_peers = 8 -secret_file = "secret" +secret_file = "secret_key" +peer_store_path = "peer_store.db" [rpc] listen_address = "0.0.0.0:8114" From 422495124c0a7f942c00a75cc9fe290404725b60 Mon Sep 17 00:00:00 2001 From: jjy Date: Fri, 22 Mar 2019 10:48:08 +0800 Subject: [PATCH 70/99] refactor: use vector to dynamic allocation --- network/src/peer_store/score.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/network/src/peer_store/score.rs b/network/src/peer_store/score.rs index 987585d0d7..f2779f7667 100644 --- a/network/src/peer_store/score.rs +++ b/network/src/peer_store/score.rs @@ -22,15 +22,14 @@ pub struct ScoringSchema { impl ScoringSchema { pub fn new_default() -> Self { - let schema = [ + let schema = vec![ (Behaviour::FailedToConnect, -20), (Behaviour::FailedToPing, -10), (Behaviour::Ping, 5), (Behaviour::Connect, 10), (Behaviour::UnexpectedDisconnect, -20), ] - .iter() - .cloned() + .into_iter() .collect(); ScoringSchema { schema, From 581c0ecdedcea029d6de21f99fb502d34a6fa27f Mon Sep 17 00:00:00 2001 From: quake wang Date: Fri, 22 Mar 2019 12:40:54 +0900 Subject: [PATCH 71/99] rebase --- verification/src/block_verifier.rs | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/verification/src/block_verifier.rs b/verification/src/block_verifier.rs index d1f40bba98..287254767f 100644 --- a/verification/src/block_verifier.rs +++ b/verification/src/block_verifier.rs @@ -100,25 +100,7 @@ impl CellbaseVerifier { return Err(Error::Cellbase(CellbaseError::InvalidOutput)); } - let block_reward = self.provider.block_reward(block.header().number()); - let mut fee = 0; - for transaction in block.commit_transactions().iter().skip(1) { - fee += self - .provider - .calculate_transaction_fee(transaction) - .map_err(|e| Error::Chain(format!("{}", e)))?; - } - let total_reward = block_reward + fee; - let output_capacity: Capacity = cellbase_transaction - .outputs() - .iter() - .map(|output| output.capacity) - .sum(); - if output_capacity > total_reward { - Err(Error::Cellbase(CellbaseError::InvalidReward)) - } else { - Ok(()) - } + Ok(()) } } From 5f7505a8b837b813f868e370be146d3a710c1423 Mon Sep 17 00:00:00 2001 From: jjy Date: Fri, 22 Mar 2019 11:42:30 +0800 Subject: [PATCH 72/99] fix: rebase --- network/src/service/discovery_service.rs | 2 +- network/src/service/identify_service.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/network/src/service/discovery_service.rs b/network/src/service/discovery_service.rs index d2dcc56453..bd464fb0cb 100644 --- a/network/src/service/discovery_service.rs +++ b/network/src/service/discovery_service.rs @@ -216,7 +216,7 @@ impl Stream for DiscoveryService { .network .peer_store() .write() - .add_discovered_address(&peer_id, addr); + .add_discovered_addr(&peer_id, addr); } } } diff --git a/network/src/service/identify_service.rs b/network/src/service/identify_service.rs index 60505af9da..89c39228e5 100644 --- a/network/src/service/identify_service.rs +++ b/network/src/service/identify_service.rs @@ -132,7 +132,7 @@ impl Stream for IdentifyService { .network .peer_store() .write() - .add_discovered_address(&peer_id, addr); + .add_discovered_addr(&peer_id, addr); } } Some(IdentifyEvent::Misbehave { result, .. }) => { From 4ea613bd855f251e81ae610e00bb7cc33619272a Mon Sep 17 00:00:00 2001 From: jjy Date: Fri, 22 Mar 2019 12:18:31 +0800 Subject: [PATCH 73/99] refactor: use memory mode to run SqlitePeerStore tests --- network/src/network.rs | 2 +- network/src/peer_store/sqlite.rs | 17 +++++++------- network/src/peer_store/sqlite/peer_store.rs | 4 ++-- network/src/tests/sqlite_peer_store.rs | 25 +++++++++++++-------- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/network/src/network.rs b/network/src/network.rs index 3c82efeb06..de7863204f 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -367,7 +367,7 @@ impl Network { let peer_store: Arc> = { let mut peer_store = match &config.peer_store_path { Some(path) => SqlitePeerStore::file(path.to_string())?, - None => SqlitePeerStore::memory()?, + None => SqlitePeerStore::memory("default".to_string())?, }; let bootnodes = config.bootnodes()?; for (peer_id, addr) in bootnodes { diff --git a/network/src/peer_store/sqlite.rs b/network/src/peer_store/sqlite.rs index cc726e5ab8..2845e5248f 100644 --- a/network/src/peer_store/sqlite.rs +++ b/network/src/peer_store/sqlite.rs @@ -44,17 +44,16 @@ lazy_static! { | OpenFlags::SQLITE_OPEN_NO_MUTEX; } -const SHARED_MEMORY_PATH: &str = "file::memory:?cache=shared"; - pub enum StorePath { - Memory, + Memory(String), File(String), } pub fn open_pool(store_path: StorePath, max_size: u32) -> Result { let manager = match store_path { - StorePath::Memory => { - let manager = SqliteConnectionManager::file(SHARED_MEMORY_PATH); + StorePath::Memory(db) => { + let manager = + SqliteConnectionManager::file(format!("file:{}?mode=memory&cache=shared", db)); manager.with_flags(*MEMORY_OPEN_FLAGS) } StorePath::File(file_path) => { @@ -70,9 +69,11 @@ pub fn open_pool(store_path: StorePath, max_size: u32) -> Result Result { match store_path { - StorePath::Memory => { - Connection::open_with_flags(SHARED_MEMORY_PATH, *MEMORY_OPEN_FLAGS).map_err(Into::into) - } + StorePath::Memory(db) => Connection::open_with_flags( + format!("file:{}?mode=memory&cache=shared", db), + *MEMORY_OPEN_FLAGS, + ) + .map_err(Into::into), StorePath::File(file_path) => { Connection::open_with_flags(file_path, *FILE_OPEN_FLAGS).map_err(Into::into) } diff --git a/network/src/peer_store/sqlite/peer_store.rs b/network/src/peer_store/sqlite/peer_store.rs index 9e3f55f57d..246b5f1c74 100644 --- a/network/src/peer_store/sqlite/peer_store.rs +++ b/network/src/peer_store/sqlite/peer_store.rs @@ -50,8 +50,8 @@ impl SqlitePeerStore { Ok(SqlitePeerStore::new(pool)) } - pub fn memory() -> Result { - let pool = sqlite::open_pool(sqlite::StorePath::Memory, DEFAULT_POOL_SIZE)?; + pub fn memory(db: String) -> Result { + let pool = sqlite::open_pool(sqlite::StorePath::Memory(db), DEFAULT_POOL_SIZE)?; Ok(SqlitePeerStore::new(pool)) } diff --git a/network/src/tests/sqlite_peer_store.rs b/network/src/tests/sqlite_peer_store.rs index 5581817c48..5aa42f2fb3 100644 --- a/network/src/tests/sqlite_peer_store.rs +++ b/network/src/tests/sqlite_peer_store.rs @@ -6,11 +6,18 @@ use crate::{ }, random_peer_id, SessionType, }; +use rand::Rng; use std::time::Duration; +fn new_peer_store() -> Box { + let mut rng = rand::thread_rng(); + let db_number: u32 = rng.gen(); + Box::new(SqlitePeerStore::memory(format!("db{}", db_number).to_string()).expect("memory")) +} + #[test] fn test_add_connected_peer() { - let mut peer_store: Box = Box::new(SqlitePeerStore::temp().expect("temp")); + let mut peer_store: Box = new_peer_store(); let peer_id = random_peer_id(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); peer_store.add_connected_peer(&peer_id, addr, SessionType::Client); @@ -23,7 +30,7 @@ fn test_add_connected_peer() { #[test] fn test_add_discovered_addr() { - let mut peer_store: Box = Box::new(SqlitePeerStore::temp().expect("temp")); + let mut peer_store: Box = new_peer_store(); let peer_id = random_peer_id(); peer_store.add_discovered_addr(&peer_id, "/ip4/127.0.0.1".to_multiaddr().unwrap()); assert_eq!(peer_store.peer_addrs(&peer_id, 2).unwrap().len(), 1); @@ -31,7 +38,7 @@ fn test_add_discovered_addr() { #[test] fn test_report() { - let mut peer_store: Box = Box::new(SqlitePeerStore::temp().expect("temp")); + let mut peer_store: Box = new_peer_store(); let peer_id = random_peer_id(); assert!(peer_store.report(&peer_id, Behaviour::Ping).is_ok()); assert!( @@ -41,7 +48,7 @@ fn test_report() { #[test] fn test_update_status() { - let mut peer_store: Box = Box::new(SqlitePeerStore::temp().expect("temp")); + let mut peer_store: Box = new_peer_store(); let peer_id = random_peer_id(); peer_store.update_status(&peer_id, Status::Connected); assert_eq!(peer_store.peer_status(&peer_id), Status::Unknown); @@ -53,7 +60,7 @@ fn test_update_status() { #[test] fn test_ban_peer() { - let mut peer_store: Box = Box::new(SqlitePeerStore::temp().expect("temp")); + let mut peer_store: Box = new_peer_store(); let peer_id = random_peer_id(); peer_store.ban_peer(&peer_id, Duration::from_secs(10)); assert!(!peer_store.is_banned(&peer_id)); @@ -65,7 +72,7 @@ fn test_ban_peer() { #[test] fn test_bootnodes() { - let mut peer_store: Box = Box::new(SqlitePeerStore::temp().expect("temp")); + let mut peer_store: Box = new_peer_store(); assert!(peer_store.bootnodes(1).is_empty()); let peer_id = random_peer_id(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); @@ -81,7 +88,7 @@ fn test_bootnodes() { #[test] fn test_peers_to_attempt() { - let mut peer_store: Box = Box::new(SqlitePeerStore::temp().expect("temp")); + let mut peer_store: Box = new_peer_store(); assert!(peer_store.peers_to_attempt(1).is_empty()); let peer_id = random_peer_id(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); @@ -96,7 +103,7 @@ fn test_peers_to_attempt() { #[test] fn test_random_peers() { - let mut peer_store: Box = Box::new(SqlitePeerStore::temp().expect("temp")); + let mut peer_store: Box = new_peer_store(); assert!(peer_store.random_peers(1).is_empty()); let peer_id = random_peer_id(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); @@ -111,7 +118,7 @@ fn test_random_peers() { #[test] fn test_delete_peer_info() { - let mut peer_store = SqlitePeerStore::temp().expect("temp"); + let mut peer_store = new_peer_store(); let addr1 = "/ip4/127.0.0.1".to_multiaddr().unwrap(); let addr2 = "/ip4/192.163.1.1".to_multiaddr().unwrap(); for _ in 0..(PEER_STORE_LIMIT - 2) { From 4a8533377c85a1594460def46a822bfd12707e3c Mon Sep 17 00:00:00 2001 From: quake wang Date: Fri, 22 Mar 2019 14:14:32 +0900 Subject: [PATCH 74/99] test: resolve backtrace assert issue --- chain/src/tests/basic.rs | 23 +++++++++++++---------- chain/src/tests/delay_verify.rs | 23 +++++++++++++---------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/chain/src/tests/basic.rs b/chain/src/tests/basic.rs index 64151db291..e8c3ab3ab6 100644 --- a/chain/src/tests/basic.rs +++ b/chain/src/tests/basic.rs @@ -5,6 +5,7 @@ use ckb_core::block::BlockBuilder; use ckb_core::cell::CellProvider; use ckb_core::header::HeaderBuilder; use ckb_core::transaction::{CellInput, CellOutput, OutPoint, TransactionBuilder}; +use ckb_shared::error::SharedError; use ckb_traits::ChainProvider; use numext_fixed_hash::H256; use numext_fixed_uint::U256; @@ -194,11 +195,12 @@ fn test_transaction_conflict_in_same_block() { .expect("process block ok"); } assert_eq!( - "Err(InvalidTransaction(\"Transactions((2, Conflict))\"))", - format!( - "{:?}", - chain_controller.process_block(Arc::new(chain[3].clone())) - ) + SharedError::InvalidTransaction("Transactions((2, Conflict))".to_string()), + chain_controller + .process_block(Arc::new(chain[3].clone())) + .unwrap_err() + .downcast() + .unwrap() ); } @@ -281,11 +283,12 @@ fn test_transaction_conflict_in_different_blocks() { .expect("process block ok"); } assert_eq!( - "Err(InvalidTransaction(\"Transactions((0, Conflict))\"))", - format!( - "{:?}", - chain_controller.process_block(Arc::new(chain[4].clone())) - ) + SharedError::InvalidTransaction("Transactions((0, Conflict))".to_string()), + chain_controller + .process_block(Arc::new(chain[4].clone())) + .unwrap_err() + .downcast() + .unwrap() ); } diff --git a/chain/src/tests/delay_verify.rs b/chain/src/tests/delay_verify.rs index d6963c04d1..4395ae9fa9 100644 --- a/chain/src/tests/delay_verify.rs +++ b/chain/src/tests/delay_verify.rs @@ -1,5 +1,6 @@ use crate::tests::util::{create_transaction, gen_block, start_chain}; use ckb_core::block::Block; +use ckb_shared::error::SharedError; use ckb_traits::ChainProvider; use numext_fixed_uint::U256; use std::sync::Arc; @@ -90,11 +91,12 @@ fn test_dead_cell_in_same_block() { } assert_eq!( - "Err(InvalidTransaction(\"Transactions((2, Conflict))\"))", - format!( - "{:?}", - chain_controller.process_block(Arc::new(chain2[switch_fork_number + 1].clone())) - ) + SharedError::InvalidTransaction("Transactions((2, Conflict))".to_string()), + chain_controller + .process_block(Arc::new(chain2[switch_fork_number + 1].clone())) + .unwrap_err() + .downcast() + .unwrap() ); } @@ -191,10 +193,11 @@ fn test_dead_cell_in_different_block() { } assert_eq!( - "Err(InvalidTransaction(\"Transactions((0, Conflict))\"))", - format!( - "{:?}", - chain_controller.process_block(Arc::new(chain2[switch_fork_number + 2].clone())) - ) + SharedError::InvalidTransaction("Transactions((0, Conflict))".to_string()), + chain_controller + .process_block(Arc::new(chain2[switch_fork_number + 2].clone())) + .unwrap_err() + .downcast() + .unwrap() ); } From 2ef7fe1058bb7a979f245287f4e80dc571a00d04 Mon Sep 17 00:00:00 2001 From: jjy Date: Fri, 22 Mar 2019 13:16:53 +0800 Subject: [PATCH 75/99] fix: sqlite peer store benchmark --- network/src/benches/sqlite_peer_store.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/network/src/benches/sqlite_peer_store.rs b/network/src/benches/sqlite_peer_store.rs index 429ed04341..6b7787b417 100644 --- a/network/src/benches/sqlite_peer_store.rs +++ b/network/src/benches/sqlite_peer_store.rs @@ -15,7 +15,9 @@ use std::rc::Rc; fn insert_peer_info_benchmark(c: &mut Criterion) { c.bench_function("insert 100 peer_info", |b| { b.iter({ - let mut peer_store = SqlitePeerStore::memory().expect("memory"); + let mut peer_store = + SqlitePeerStore::memory("bench_db_insert_100_peer_info".to_string()) + .expect("memory"); let peer_ids = (0..100).map(|_| random_peer_id()).collect::>(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); move || { @@ -27,7 +29,9 @@ fn insert_peer_info_benchmark(c: &mut Criterion) { }); c.bench_function("insert 1000 peer_info", |b| { b.iter({ - let mut peer_store = SqlitePeerStore::memory().expect("memory"); + let mut peer_store = + SqlitePeerStore::memory("bench_db_insert_1000_peer_info".to_string()) + .expect("memory"); let peer_ids = (0..1000).map(|_| random_peer_id()).collect::>(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); move || { @@ -55,7 +59,9 @@ fn insert_peer_info_benchmark(c: &mut Criterion) { fn random_order_benchmark(c: &mut Criterion) { { - let peer_store = Rc::new(Mutex::new(SqlitePeerStore::memory().expect("memory"))); + let peer_store = Rc::new(Mutex::new( + SqlitePeerStore::memory("bench_db_random_order".to_string()).expect("memory"), + )); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); { let mut peer_store = peer_store.lock(); From f69bde85deb65d9d8b3342d27e84cc820200bd2f Mon Sep 17 00:00:00 2001 From: jjy Date: Fri, 22 Mar 2019 14:02:25 +0800 Subject: [PATCH 76/99] refactor: speed up sqlite peer store tests --- Cargo.lock | 1 + network/Cargo.toml | 1 + network/src/lib.rs | 28 ++++++++++++-- network/src/peer_store/sqlite.rs | 2 +- network/src/peer_store/sqlite/peer_store.rs | 2 +- network/src/tests/sqlite_peer_store.rs | 43 ++++++++++++++------- 6 files changed, 59 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d6878be97a..4ce9ab908a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -481,6 +481,7 @@ dependencies = [ "rusqlite 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", + "sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "snap 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)", "tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", diff --git a/network/Cargo.toml b/network/Cargo.toml index 1abab69d32..1ff1ed934d 100644 --- a/network/Cargo.toml +++ b/network/Cargo.toml @@ -31,6 +31,7 @@ lazy_static = "1.3.0" r2d2 = "0.8.3" r2d2_sqlite = "0.8.0" generic-channel = { version = "0.2.0", features = ["all"] } +sha2 = "0.8.0" [dev-dependencies] criterion = "0.2" diff --git a/network/src/lib.rs b/network/src/lib.rs index 62fcd226a4..ac28b3bfbc 100644 --- a/network/src/lib.rs +++ b/network/src/lib.rs @@ -98,8 +98,30 @@ impl From for NetworkConfig { } } +//TODO remove after tentacle-secio support PeerId::random pub fn random_peer_id() -> PeerId { - use p2p::secio::SecioKeyPair; - let pubkey = SecioKeyPair::secp256k1_generated().to_public_key(); - PeerId::from_public_key(&pubkey) + use rand::{thread_rng, Rng}; + use sha2::digest::Digest; + use unsigned_varint::encode; + + let data = { + let mut arr = [0u8; 20]; + thread_rng().fill(&mut arr[..]); + arr + }; + let mut buf = encode::u16_buffer(); + let code = encode::u16(0x12, &mut buf); + + let header_len = code.len() + 1; + + let mut inner = Vec::new(); + inner.resize(header_len + 32 as usize, 0); + inner[..code.len()].copy_from_slice(code); + inner[code.len()] = 32; + + let mut hasher = sha2::Sha256::default(); + hasher.input(data); + inner[header_len..].copy_from_slice(hasher.result().as_ref()); + + PeerId::from_bytes(inner).expect("random peer_id") } diff --git a/network/src/peer_store/sqlite.rs b/network/src/peer_store/sqlite.rs index 2845e5248f..d00e5c9508 100644 --- a/network/src/peer_store/sqlite.rs +++ b/network/src/peer_store/sqlite.rs @@ -1,6 +1,6 @@ #![allow(dead_code)] -mod db; +pub(crate) mod db; #[cfg(db_trace)] pub mod db_trace; pub(crate) mod peer_store; diff --git a/network/src/peer_store/sqlite/peer_store.rs b/network/src/peer_store/sqlite/peer_store.rs index 246b5f1c74..210028daba 100644 --- a/network/src/peer_store/sqlite/peer_store.rs +++ b/network/src/peer_store/sqlite/peer_store.rs @@ -30,7 +30,7 @@ pub struct SqlitePeerStore { bootnodes: Vec<(PeerId, Multiaddr)>, schema: ScoringSchema, ban_list: FnvHashMap, Duration>, - pool: ConnectionPool, + pub(crate) pool: ConnectionPool, } impl SqlitePeerStore { diff --git a/network/src/tests/sqlite_peer_store.rs b/network/src/tests/sqlite_peer_store.rs index 5aa42f2fb3..70a0e6a396 100644 --- a/network/src/tests/sqlite_peer_store.rs +++ b/network/src/tests/sqlite_peer_store.rs @@ -1,6 +1,8 @@ +use crate::peer_store::sqlite::ConnectionPoolExt; use crate::{ multiaddr::ToMultiaddr, peer_store::{ + sqlite::db, sqlite::peer_store::{PEER_NOT_SEEN_TIMEOUT_SECS, PEER_STORE_LIMIT}, Behaviour, PeerStore, SqlitePeerStore, Status, }, @@ -9,15 +11,15 @@ use crate::{ use rand::Rng; use std::time::Duration; -fn new_peer_store() -> Box { +fn new_peer_store() -> SqlitePeerStore { let mut rng = rand::thread_rng(); let db_number: u32 = rng.gen(); - Box::new(SqlitePeerStore::memory(format!("db{}", db_number).to_string()).expect("memory")) + SqlitePeerStore::memory(format!("db{}", db_number).to_string()).expect("memory") } #[test] fn test_add_connected_peer() { - let mut peer_store: Box = new_peer_store(); + let mut peer_store: Box = Box::new(new_peer_store()); let peer_id = random_peer_id(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); peer_store.add_connected_peer(&peer_id, addr, SessionType::Client); @@ -30,7 +32,7 @@ fn test_add_connected_peer() { #[test] fn test_add_discovered_addr() { - let mut peer_store: Box = new_peer_store(); + let mut peer_store: Box = Box::new(new_peer_store()); let peer_id = random_peer_id(); peer_store.add_discovered_addr(&peer_id, "/ip4/127.0.0.1".to_multiaddr().unwrap()); assert_eq!(peer_store.peer_addrs(&peer_id, 2).unwrap().len(), 1); @@ -38,7 +40,7 @@ fn test_add_discovered_addr() { #[test] fn test_report() { - let mut peer_store: Box = new_peer_store(); + let mut peer_store: Box = Box::new(new_peer_store()); let peer_id = random_peer_id(); assert!(peer_store.report(&peer_id, Behaviour::Ping).is_ok()); assert!( @@ -48,7 +50,7 @@ fn test_report() { #[test] fn test_update_status() { - let mut peer_store: Box = new_peer_store(); + let mut peer_store: Box = Box::new(new_peer_store()); let peer_id = random_peer_id(); peer_store.update_status(&peer_id, Status::Connected); assert_eq!(peer_store.peer_status(&peer_id), Status::Unknown); @@ -60,7 +62,7 @@ fn test_update_status() { #[test] fn test_ban_peer() { - let mut peer_store: Box = new_peer_store(); + let mut peer_store: Box = Box::new(new_peer_store()); let peer_id = random_peer_id(); peer_store.ban_peer(&peer_id, Duration::from_secs(10)); assert!(!peer_store.is_banned(&peer_id)); @@ -72,7 +74,7 @@ fn test_ban_peer() { #[test] fn test_bootnodes() { - let mut peer_store: Box = new_peer_store(); + let mut peer_store: Box = Box::new(new_peer_store()); assert!(peer_store.bootnodes(1).is_empty()); let peer_id = random_peer_id(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); @@ -88,7 +90,7 @@ fn test_bootnodes() { #[test] fn test_peers_to_attempt() { - let mut peer_store: Box = new_peer_store(); + let mut peer_store: Box = Box::new(new_peer_store()); assert!(peer_store.peers_to_attempt(1).is_empty()); let peer_id = random_peer_id(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); @@ -103,7 +105,7 @@ fn test_peers_to_attempt() { #[test] fn test_random_peers() { - let mut peer_store: Box = new_peer_store(); + let mut peer_store: Box = Box::new(new_peer_store()); assert!(peer_store.random_peers(1).is_empty()); let peer_id = random_peer_id(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); @@ -121,9 +123,24 @@ fn test_delete_peer_info() { let mut peer_store = new_peer_store(); let addr1 = "/ip4/127.0.0.1".to_multiaddr().unwrap(); let addr2 = "/ip4/192.163.1.1".to_multiaddr().unwrap(); - for _ in 0..(PEER_STORE_LIMIT - 2) { - peer_store.add_connected_peer(&random_peer_id(), addr1.clone(), SessionType::Server); - } + let now = faketime::unix_time(); + // prepare peer_info records + peer_store + .pool + .fetch(|conn| { + for _ in 0..(PEER_STORE_LIMIT - 2) { + db::PeerInfo::insert( + conn, + &random_peer_id(), + &addr1, + SessionType::Server, + peer_store.scoring_schema().peer_init_score(), + now, + )?; + } + Ok(()) + }) + .expect("insert peer infos"); let evict_target = random_peer_id(); let fake_target = random_peer_id(); { From 017d67498d1dbda31424d9ae7f282ae1c673d219 Mon Sep 17 00:00:00 2001 From: jjy Date: Fri, 22 Mar 2019 15:17:15 +0800 Subject: [PATCH 77/99] chore: update tentacle --- Cargo.lock | 44 ++++++++++++------------ network/Cargo.toml | 10 +++--- network/src/benches/sqlite_peer_store.rs | 12 +++---- network/src/lib.rs | 28 --------------- network/src/tests/peers_registry.rs | 30 ++++++++-------- network/src/tests/sqlite_peer_store.rs | 32 ++++++++--------- sync/src/synchronizer/mod.rs | 4 +-- 7 files changed, 66 insertions(+), 94 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4ce9ab908a..d7cbd1c7cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -484,11 +484,11 @@ dependencies = [ "sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "snap 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)", - "tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", - "tentacle-discovery 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", - "tentacle-identify 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", - "tentacle-ping 0.2.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", - "tentacle-secio 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", + "tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)", + "tentacle-discovery 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)", + "tentacle-identify 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)", + "tentacle-ping 0.2.0 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)", + "tentacle-secio 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)", "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "unsigned-varint 0.2.2 (git+https://github.com/paritytech/unsigned-varint)", ] @@ -2558,23 +2558,23 @@ dependencies = [ [[package]] name = "tentacle" version = "0.2.0-alpha.1" -source = "git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb#9c42be05724c3e2decb73716d0f515c36a7af1cb" +source = "git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe#6c1f778914dae864d13c1d9110a08b46fb4d38fe" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "parity-multiaddr 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tentacle-secio 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", + "tentacle-secio 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)", "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-threadpool 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-yamux 0.1.4 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", + "tokio-yamux 0.1.4 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)", ] [[package]] name = "tentacle-discovery" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb#9c42be05724c3e2decb73716d0f515c36a7af1cb" +source = "git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe#6c1f778914dae864d13c1d9110a08b46fb4d38fe" dependencies = [ "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2585,7 +2585,7 @@ dependencies = [ "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", - "tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", + "tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)", "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "trust-dns 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2593,32 +2593,32 @@ dependencies = [ [[package]] name = "tentacle-identify" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb#9c42be05724c3e2decb73716d0f515c36a7af1cb" +source = "git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe#6c1f778914dae864d13c1d9110a08b46fb4d38fe" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", + "tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)", "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "tentacle-ping" version = "0.2.0" -source = "git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb#9c42be05724c3e2decb73716d0f515c36a7af1cb" +source = "git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe#6c1f778914dae864d13c1d9110a08b46fb4d38fe" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "generic-channel 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)", + "tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)", ] [[package]] name = "tentacle-secio" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb#9c42be05724c3e2decb73716d0f515c36a7af1cb" +source = "git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe#6c1f778914dae864d13c1d9110a08b46fb4d38fe" dependencies = [ "aes-ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "bs58 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2931,7 +2931,7 @@ dependencies = [ [[package]] name = "tokio-yamux" version = "0.1.4" -source = "git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb#9c42be05724c3e2decb73716d0f515c36a7af1cb" +source = "git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe#6c1f778914dae864d13c1d9110a08b46fb4d38fe" dependencies = [ "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3477,11 +3477,11 @@ dependencies = [ "checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015" "checksum take 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b157868d8ac1f56b64604539990685fa7611d8fa9e5476cf0c02cf34d32917c5" "checksum tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "b86c784c88d98c801132806dadd3819ed29d8600836c4088e855cdf3e178ed8a" -"checksum tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)" = "" -"checksum tentacle-discovery 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)" = "" -"checksum tentacle-identify 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)" = "" -"checksum tentacle-ping 0.2.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)" = "" -"checksum tentacle-secio 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)" = "" +"checksum tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)" = "" +"checksum tentacle-discovery 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)" = "" +"checksum tentacle-identify 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)" = "" +"checksum tentacle-ping 0.2.0 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)" = "" +"checksum tentacle-secio 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)" = "" "checksum termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4096add70612622289f2fdcdbd5086dc81c1e2675e6ae58d6c4f62a16c6d7f2f" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum termios 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "72b620c5ea021d75a735c943269bb07d30c9b77d6ac6b236bc8b5c496ef05625" @@ -3507,7 +3507,7 @@ dependencies = [ "checksum tokio-trace-core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "350c9edade9830dc185ae48ba45667a445ab59f6167ef6d0254ec9d2430d9dd3" "checksum tokio-udp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "66268575b80f4a4a710ef83d087fdfeeabdce9b74c797535fbac18a2cb906e92" "checksum tokio-uds 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "037ffc3ba0e12a0ab4aca92e5234e0dedeb48fddf6ccd260f1f150a36a9f2445" -"checksum tokio-yamux 0.1.4 (git+https://github.com/nervosnetwork/p2p?rev=9c42be05724c3e2decb73716d0f515c36a7af1cb)" = "" +"checksum tokio-yamux 0.1.4 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)" = "" "checksum toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "758664fc71a3a69038656bee8b6be6477d2a6c315a6b81f7081f591bffa4111f" "checksum toml 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "87c5890a989fa47ecdc7bcb4c63a77a82c18f306714104b1decfd722db17b39e" "checksum trust-dns 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)" = "65096825b064877da37eeeb9a83390bd23433eabfc503a6476dc5b1949034aa7" diff --git a/network/Cargo.toml b/network/Cargo.toml index 1ff1ed934d..37e124f0e8 100644 --- a/network/Cargo.toml +++ b/network/Cargo.toml @@ -20,11 +20,11 @@ bytes = "0.4.12" tokio = "0.1.17" futures = { version = "0.1.19", features = ["use_std"] } snap = "0.2" -p2p = { git = "https://github.com/nervosnetwork/p2p", rev="9c42be05724c3e2decb73716d0f515c36a7af1cb", package="tentacle" } -secio = { git = "https://github.com/nervosnetwork/p2p", rev="9c42be05724c3e2decb73716d0f515c36a7af1cb", package="tentacle-secio" } -p2p-ping = { git = "https://github.com/nervosnetwork/p2p", rev="9c42be05724c3e2decb73716d0f515c36a7af1cb", package="tentacle-ping" } -p2p-discovery = { git = "https://github.com/nervosnetwork/p2p", rev="9c42be05724c3e2decb73716d0f515c36a7af1cb", package="tentacle-discovery" } -p2p-identify = { git = "https://github.com/nervosnetwork/p2p", rev="9c42be05724c3e2decb73716d0f515c36a7af1cb", package="tentacle-identify" } +p2p = { git = "https://github.com/nervosnetwork/p2p", rev="6c1f778914dae864d13c1d9110a08b46fb4d38fe", package="tentacle" } +secio = { git = "https://github.com/nervosnetwork/p2p", rev="6c1f778914dae864d13c1d9110a08b46fb4d38fe", package="tentacle-secio" } +p2p-ping = { git = "https://github.com/nervosnetwork/p2p", rev="6c1f778914dae864d13c1d9110a08b46fb4d38fe", package="tentacle-ping" } +p2p-discovery = { git = "https://github.com/nervosnetwork/p2p", rev="6c1f778914dae864d13c1d9110a08b46fb4d38fe", package="tentacle-discovery" } +p2p-identify = { git = "https://github.com/nervosnetwork/p2p", rev="6c1f778914dae864d13c1d9110a08b46fb4d38fe", package="tentacle-identify" } faketime = "0.2.0" rusqlite = {version = "0.16.0", features = ["bundled"]} lazy_static = "1.3.0" diff --git a/network/src/benches/sqlite_peer_store.rs b/network/src/benches/sqlite_peer_store.rs index 6b7787b417..ea2e50af74 100644 --- a/network/src/benches/sqlite_peer_store.rs +++ b/network/src/benches/sqlite_peer_store.rs @@ -6,7 +6,7 @@ extern crate ckb_util; use ckb_network::{ multiaddr::ToMultiaddr, peer_store::{PeerStore, SqlitePeerStore}, - random_peer_id, SessionType, + PeerId, SessionType, }; use ckb_util::Mutex; use criterion::Criterion; @@ -18,7 +18,7 @@ fn insert_peer_info_benchmark(c: &mut Criterion) { let mut peer_store = SqlitePeerStore::memory("bench_db_insert_100_peer_info".to_string()) .expect("memory"); - let peer_ids = (0..100).map(|_| random_peer_id()).collect::>(); + let peer_ids = (0..100).map(|_| PeerId::random()).collect::>(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); move || { for peer_id in peer_ids.clone() { @@ -32,7 +32,7 @@ fn insert_peer_info_benchmark(c: &mut Criterion) { let mut peer_store = SqlitePeerStore::memory("bench_db_insert_1000_peer_info".to_string()) .expect("memory"); - let peer_ids = (0..1000).map(|_| random_peer_id()).collect::>(); + let peer_ids = (0..1000).map(|_| PeerId::random()).collect::>(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); move || { for peer_id in peer_ids.clone() { @@ -46,7 +46,7 @@ fn insert_peer_info_benchmark(c: &mut Criterion) { c.bench_function("insert 100 peer_info on filesystem", move |b| { b.iter({ let mut peer_store = SqlitePeerStore::temp().expect("temp"); - let peer_ids = (0..100).map(|_| random_peer_id()).collect::>(); + let peer_ids = (0..100).map(|_| PeerId::random()).collect::>(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); move || { for peer_id in peer_ids.clone() { @@ -66,7 +66,7 @@ fn random_order_benchmark(c: &mut Criterion) { { let mut peer_store = peer_store.lock(); for _ in 0..8000 { - let peer_id = random_peer_id(); + let peer_id = PeerId::random(); peer_store.add_connected_peer(&peer_id, addr.clone(), SessionType::Client); let _ = peer_store.add_discovered_addr(&peer_id, addr.clone()); } @@ -113,7 +113,7 @@ fn random_order_benchmark(c: &mut Criterion) { let mut peer_store = SqlitePeerStore::temp().expect("temp"); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); for _ in 0..8000 { - let peer_id = random_peer_id(); + let peer_id = PeerId::random(); peer_store.add_connected_peer(&peer_id, addr.clone(), SessionType::Client); let _ = peer_store.add_discovered_addr(&peer_id, addr.clone()); } diff --git a/network/src/lib.rs b/network/src/lib.rs index ac28b3bfbc..c35702eb82 100644 --- a/network/src/lib.rs +++ b/network/src/lib.rs @@ -97,31 +97,3 @@ impl From for NetworkConfig { cfg } } - -//TODO remove after tentacle-secio support PeerId::random -pub fn random_peer_id() -> PeerId { - use rand::{thread_rng, Rng}; - use sha2::digest::Digest; - use unsigned_varint::encode; - - let data = { - let mut arr = [0u8; 20]; - thread_rng().fill(&mut arr[..]); - arr - }; - let mut buf = encode::u16_buffer(); - let code = encode::u16(0x12, &mut buf); - - let header_len = code.len() + 1; - - let mut inner = Vec::new(); - inner.resize(header_len + 32 as usize, 0); - inner[..code.len()].copy_from_slice(code); - inner[code.len()] = 32; - - let mut hasher = sha2::Sha256::default(); - hasher.input(data); - inner[header_len..].copy_from_slice(hasher.result().as_ref()); - - PeerId::from_bytes(inner).expect("random peer_id") -} diff --git a/network/src/tests/peers_registry.rs b/network/src/tests/peers_registry.rs index 60fb512b6c..402a9a49b6 100644 --- a/network/src/tests/peers_registry.rs +++ b/network/src/tests/peers_registry.rs @@ -2,7 +2,7 @@ use crate::{ multiaddr::ToMultiaddr, peer_store::{Behaviour, PeerStore, SqlitePeerStore}, peers_registry::{PeersRegistry, Session, EVICTION_PROTECT_PEERS}, - random_peer_id, SessionType, + PeerId, SessionType, }; use ckb_util::RwLock; use std::sync::Arc; @@ -15,7 +15,7 @@ fn new_peer_store() -> Arc> { #[test] fn test_accept_inbound_peer_in_reserve_only_mode() { let peer_store = new_peer_store(); - let reserved_peer = random_peer_id(); + let reserved_peer = PeerId::random(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); let session = Session { id: 1, @@ -31,7 +31,7 @@ fn test_accept_inbound_peer_in_reserve_only_mode() { vec![reserved_peer.clone()], ); assert!(peers - .accept_inbound_peer(random_peer_id(), addr.clone(), session) + .accept_inbound_peer(PeerId::random(), addr.clone(), session) .is_err()); peers .accept_inbound_peer(reserved_peer.clone(), addr.clone(), session) @@ -41,7 +41,7 @@ fn test_accept_inbound_peer_in_reserve_only_mode() { #[test] fn test_accept_inbound_peer_until_full() { let peer_store = new_peer_store(); - let reserved_peer = random_peer_id(); + let reserved_peer = PeerId::random(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); let session = Session { id: 1, @@ -56,17 +56,17 @@ fn test_accept_inbound_peer_until_full() { vec![reserved_peer.clone()], ); peers - .accept_inbound_peer(random_peer_id(), addr.clone(), session) + .accept_inbound_peer(PeerId::random(), addr.clone(), session) .expect("accept"); peers - .accept_inbound_peer(random_peer_id(), addr.clone(), session) + .accept_inbound_peer(PeerId::random(), addr.clone(), session) .expect("accept"); peers - .accept_inbound_peer(random_peer_id(), addr.clone(), session) + .accept_inbound_peer(PeerId::random(), addr.clone(), session) .expect("accept"); println!("{:?}", peers.connection_status()); assert!(peers - .accept_inbound_peer(random_peer_id(), addr.clone(), session) + .accept_inbound_peer(PeerId::random(), addr.clone(), session) .is_err(),); // should still accept reserved peer peers @@ -74,7 +74,7 @@ fn test_accept_inbound_peer_until_full() { .expect("accept"); // should refuse accept low score peer assert!(peers - .accept_inbound_peer(random_peer_id(), addr.clone(), session) + .accept_inbound_peer(PeerId::random(), addr.clone(), session) .is_err()); } @@ -85,9 +85,9 @@ fn test_accept_inbound_peer_eviction() { // 2. should never evict reserved peer // 3. should evict lowest scored peer let peer_store = new_peer_store(); - let reserved_peer = random_peer_id(); - let evict_target = random_peer_id(); - let lowest_score_peer = random_peer_id(); + let reserved_peer = PeerId::random(); + let evict_target = PeerId::random(); + let lowest_score_peer = PeerId::random(); let addr1 = "/ip4/127.0.0.1".to_multiaddr().unwrap(); let addr2 = "/ip4/192.168.0.1".to_multiaddr().unwrap(); let session = Session { @@ -106,7 +106,7 @@ fn test_accept_inbound_peer_eviction() { ); for _ in 0..protected_peers_count { assert!(peers_registry - .accept_inbound_peer(random_peer_id(), addr2.clone(), session) + .accept_inbound_peer(PeerId::random(), addr2.clone(), session) .is_ok()); } let mut peers_iter = peers_registry @@ -144,7 +144,7 @@ fn test_accept_inbound_peer_eviction() { let mut peer = peers_registry.get_mut(&peer_id).unwrap(); peer.connected_time = now - Duration::from_secs(10); } - let mut new_peer_ids = (0..3).map(|_| random_peer_id()).collect::>(); + let mut new_peer_ids = (0..3).map(|_| PeerId::random()).collect::>(); // setup 3 node and 1 reserved node from addr1 peers_registry .accept_inbound_peer(reserved_peer.clone(), addr1.clone(), session) @@ -189,7 +189,7 @@ fn test_accept_inbound_peer_eviction() { // should evict evict target assert!(peers_registry.get(&evict_target).is_some()); peers_registry - .accept_inbound_peer(random_peer_id(), addr1.clone(), session) + .accept_inbound_peer(PeerId::random(), addr1.clone(), session) .expect("accept"); assert!(peers_registry.get(&evict_target).is_none()); } diff --git a/network/src/tests/sqlite_peer_store.rs b/network/src/tests/sqlite_peer_store.rs index 70a0e6a396..857c0955ec 100644 --- a/network/src/tests/sqlite_peer_store.rs +++ b/network/src/tests/sqlite_peer_store.rs @@ -6,7 +6,7 @@ use crate::{ sqlite::peer_store::{PEER_NOT_SEEN_TIMEOUT_SECS, PEER_STORE_LIMIT}, Behaviour, PeerStore, SqlitePeerStore, Status, }, - random_peer_id, SessionType, + PeerId, SessionType, }; use rand::Rng; use std::time::Duration; @@ -20,7 +20,7 @@ fn new_peer_store() -> SqlitePeerStore { #[test] fn test_add_connected_peer() { let mut peer_store: Box = Box::new(new_peer_store()); - let peer_id = random_peer_id(); + let peer_id = PeerId::random(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); peer_store.add_connected_peer(&peer_id, addr, SessionType::Client); assert_eq!( @@ -33,7 +33,7 @@ fn test_add_connected_peer() { #[test] fn test_add_discovered_addr() { let mut peer_store: Box = Box::new(new_peer_store()); - let peer_id = random_peer_id(); + let peer_id = PeerId::random(); peer_store.add_discovered_addr(&peer_id, "/ip4/127.0.0.1".to_multiaddr().unwrap()); assert_eq!(peer_store.peer_addrs(&peer_id, 2).unwrap().len(), 1); } @@ -41,7 +41,7 @@ fn test_add_discovered_addr() { #[test] fn test_report() { let mut peer_store: Box = Box::new(new_peer_store()); - let peer_id = random_peer_id(); + let peer_id = PeerId::random(); assert!(peer_store.report(&peer_id, Behaviour::Ping).is_ok()); assert!( peer_store.peer_score_or_default(&peer_id) > peer_store.scoring_schema().peer_init_score() @@ -51,7 +51,7 @@ fn test_report() { #[test] fn test_update_status() { let mut peer_store: Box = Box::new(new_peer_store()); - let peer_id = random_peer_id(); + let peer_id = PeerId::random(); peer_store.update_status(&peer_id, Status::Connected); assert_eq!(peer_store.peer_status(&peer_id), Status::Unknown); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); @@ -63,7 +63,7 @@ fn test_update_status() { #[test] fn test_ban_peer() { let mut peer_store: Box = Box::new(new_peer_store()); - let peer_id = random_peer_id(); + let peer_id = PeerId::random(); peer_store.ban_peer(&peer_id, Duration::from_secs(10)); assert!(!peer_store.is_banned(&peer_id)); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); @@ -76,11 +76,11 @@ fn test_ban_peer() { fn test_bootnodes() { let mut peer_store: Box = Box::new(new_peer_store()); assert!(peer_store.bootnodes(1).is_empty()); - let peer_id = random_peer_id(); + let peer_id = PeerId::random(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); peer_store.add_bootnode(peer_id.clone(), addr.clone()); assert_eq!(peer_store.bootnodes(2).len(), 1); - let peer_id2 = random_peer_id(); + let peer_id2 = PeerId::random(); peer_store.add_discovered_addr(&peer_id2, addr.clone()); assert_eq!( peer_store.bootnodes(3), @@ -92,11 +92,11 @@ fn test_bootnodes() { fn test_peers_to_attempt() { let mut peer_store: Box = Box::new(new_peer_store()); assert!(peer_store.peers_to_attempt(1).is_empty()); - let peer_id = random_peer_id(); + let peer_id = PeerId::random(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); peer_store.add_bootnode(peer_id.clone(), addr.clone()); assert!(peer_store.peers_to_attempt(1).is_empty()); - let peer_id2 = random_peer_id(); + let peer_id2 = PeerId::random(); peer_store.add_discovered_addr(&peer_id2, addr.clone()); assert_eq!(peer_store.peers_to_attempt(2).len(), 1); peer_store.update_status(&peer_id2, Status::Connected); @@ -107,11 +107,11 @@ fn test_peers_to_attempt() { fn test_random_peers() { let mut peer_store: Box = Box::new(new_peer_store()); assert!(peer_store.random_peers(1).is_empty()); - let peer_id = random_peer_id(); + let peer_id = PeerId::random(); let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); peer_store.add_bootnode(peer_id.clone(), addr.clone()); assert!(peer_store.random_peers(1).is_empty()); - let peer_id2 = random_peer_id(); + let peer_id2 = PeerId::random(); peer_store.add_discovered_addr(&peer_id2, addr.clone()); assert_eq!(peer_store.random_peers(2).len(), 1); peer_store.update_status(&peer_id2, Status::Connected); @@ -131,7 +131,7 @@ fn test_delete_peer_info() { for _ in 0..(PEER_STORE_LIMIT - 2) { db::PeerInfo::insert( conn, - &random_peer_id(), + &PeerId::random(), &addr1, SessionType::Server, peer_store.scoring_schema().peer_init_score(), @@ -141,8 +141,8 @@ fn test_delete_peer_info() { Ok(()) }) .expect("insert peer infos"); - let evict_target = random_peer_id(); - let fake_target = random_peer_id(); + let evict_target = PeerId::random(); + let fake_target = PeerId::random(); { // make sure these 2 peers become candidate in eviction let recent_not_seen_time = @@ -162,7 +162,7 @@ fn test_delete_peer_info() { < peer_store.scoring_schema().peer_init_score() ); // should evict evict_target and accept this - peer_store.add_connected_peer(&random_peer_id(), addr1, SessionType::Server); + peer_store.add_connected_peer(&PeerId::random(), addr1, SessionType::Server); // evict_target is evicted in previous step assert_eq!( peer_store.peer_score_or_default(&evict_target), diff --git a/sync/src/synchronizer/mod.rs b/sync/src/synchronizer/mod.rs index 886ac968be..2792091028 100644 --- a/sync/src/synchronizer/mod.rs +++ b/sync/src/synchronizer/mod.rs @@ -741,7 +741,7 @@ mod tests { use ckb_core::transaction::{CellInput, CellOutput, Transaction, TransactionBuilder}; use ckb_db::memorydb::MemoryKeyValueDB; use ckb_network::{ - errors::Error as NetworkError, multiaddr::ToMultiaddr, random_peer_id, CKBProtocolContext, + errors::Error as NetworkError, multiaddr::ToMultiaddr, CKBProtocolContext, PeerId, PeerIndex, PeerInfo, ProtocolId, SessionInfo, SessionType, Severity, TimerToken, }; use ckb_notify::{NotifyController, NotifyService}; @@ -1062,7 +1062,7 @@ mod tests { fn mock_session_info() -> SessionInfo { SessionInfo { peer: PeerInfo { - peer_id: random_peer_id(), + peer_id: PeerId::random(), session_type: SessionType::Client, last_ping_time: None, connected_addr: "/ip4/127.0.0.1".to_multiaddr().expect("parse multiaddr"), From d4b7a22e9216622a1f8370250e389550f120c86f Mon Sep 17 00:00:00 2001 From: jjy Date: Fri, 22 Mar 2019 17:29:25 +0800 Subject: [PATCH 78/99] fix: integration tests config file --- test/fixtures/nodes_template/default.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/fixtures/nodes_template/default.toml b/test/fixtures/nodes_template/default.toml index 10cfb71fdc..06761c2344 100644 --- a/test/fixtures/nodes_template/default.toml +++ b/test/fixtures/nodes_template/default.toml @@ -18,7 +18,8 @@ reserved_nodes = [] only_reserved_peers = false min_peers = 4 max_peers = 8 -secret_file = "secret" +secret_file = "secret_key" +peer_store_path = "peer_store.db" try_outbound_connect_secs = 1 [rpc] From dd5f8b6ca0d13435404caacd1e7af4bf6ae0b00d Mon Sep 17 00:00:00 2001 From: zhangsoledad <787953403@qq.com> Date: Thu, 21 Mar 2019 20:34:29 +0800 Subject: [PATCH 79/99] feat: use TryFrom convert protocol --- Cargo.lock | 1 + Makefile | 2 +- network/src/service/discovery_service.rs | 2 +- protocol/Cargo.toml | 1 + protocol/src/builder.rs | 5 +- protocol/src/convert.rs | 282 +++++++++++------- protocol/src/error.rs | 7 + protocol/src/lib.rs | 8 + sync/src/relayer/block_proposal_process.rs | 14 +- .../src/relayer/block_transactions_process.rs | 20 +- sync/src/relayer/compact_block.rs | 61 ++-- sync/src/relayer/compact_block_process.rs | 7 +- .../src/relayer/get_block_proposal_process.rs | 19 +- .../relayer/get_block_transactions_process.rs | 17 +- sync/src/relayer/mod.rs | 130 ++++---- sync/src/relayer/transaction_process.rs | 8 +- sync/src/synchronizer/block_process.rs | 7 +- sync/src/synchronizer/filter_process.rs | 18 +- sync/src/synchronizer/get_blocks_process.rs | 37 ++- sync/src/synchronizer/get_headers_process.rs | 74 ++--- sync/src/synchronizer/headers_process.rs | 42 ++- sync/src/synchronizer/mod.rs | 71 +++-- util/src/lib.rs | 1 + util/src/unstable/mod.rs | 2 + util/src/unstable/try_convert.rs | 28 ++ 25 files changed, 544 insertions(+), 320 deletions(-) create mode 100644 protocol/src/error.rs create mode 100644 util/src/unstable/try_convert.rs diff --git a/Cargo.lock b/Cargo.lock index d6878be97a..191ef7643e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -526,6 +526,7 @@ dependencies = [ "ckb-core 0.7.0-pre", "ckb-merkle-tree 0.7.0-pre", "ckb-util 0.7.0-pre", + "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "hash 0.7.0-pre", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Makefile b/Makefile index 59220abd26..6357134c39 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ fmt: cargo fmt ${VERBOSE} --all -- --check clippy: - cargo clippy ${VERBOSE} --all --all-targets --all-features -- -D warnings -D clippy::clone_on_ref_ptr -D clippy::enum_glob_use + cargo clippy ${VERBOSE} --all --all-targets --all-features -- -D warnings -D clippy::clone_on_ref_ptr -D clippy::enum_glob_use -D clippy::fallible_impl_from ci: fmt clippy test diff --git a/network/src/service/discovery_service.rs b/network/src/service/discovery_service.rs index d2dcc56453..c065c16d3d 100644 --- a/network/src/service/discovery_service.rs +++ b/network/src/service/discovery_service.rs @@ -65,7 +65,7 @@ impl ServiceProtocol for DiscoveryProtocol { Ok(()) }) }) - .unwrap(); + .expect("Discovery init only once"); control.future_task(discovery_task); } diff --git a/protocol/Cargo.toml b/protocol/Cargo.toml index ae05962996..7847c04e01 100644 --- a/protocol/Cargo.toml +++ b/protocol/Cargo.toml @@ -15,3 +15,4 @@ siphasher = "0.3.0" rand = "0.6" ckb-util = { path = "../util" } ckb-merkle-tree = { path = "../util/merkle-tree"} +failure = "0.1.5" diff --git a/protocol/src/builder.rs b/protocol/src/builder.rs index 9e22015332..c2f6fc23f7 100644 --- a/protocol/src/builder.rs +++ b/protocol/src/builder.rs @@ -622,6 +622,7 @@ mod tests { use ckb_core::block::BlockBuilder; use ckb_core::header::HeaderBuilder; use ckb_core::transaction::TransactionBuilder; + use ckb_util::TryInto; use flatbuffers::get_root; #[test] @@ -632,7 +633,7 @@ mod tests { builder.finish(b, None); let fbs_header = get_root::(builder.finished_data()); - assert_eq!(header, fbs_header.into()); + assert_eq!(header, fbs_header.try_into().unwrap()); } #[test] @@ -645,7 +646,7 @@ mod tests { builder.finish(b, None); let fbs_block = get_root::(builder.finished_data()); - assert_eq!(block, fbs_block.into()); + assert_eq!(block, fbs_block.try_into().unwrap()); } #[test] diff --git a/protocol/src/convert.rs b/protocol/src/convert.rs index 764747d95e..95ed1e37dc 100644 --- a/protocol/src/convert.rs +++ b/protocol/src/convert.rs @@ -1,6 +1,9 @@ +use crate::cast; use crate::protocol_generated::ckb::protocol as ckb_protocol; use crate::FlatbuffersVectorIterator; use ckb_core; +use ckb_util::{TryFrom, TryInto}; +use failure::Error as FailureError; use numext_fixed_hash::H256; use numext_fixed_uint::U256; @@ -16,9 +19,11 @@ impl From<&H256> for ckb_protocol::H256 { } } -impl From<&ckb_protocol::H256> for H256 { - fn from(h256: &ckb_protocol::H256) -> Self { - H256::from_slice(&[ +impl TryFrom<&ckb_protocol::H256> for H256 { + type Error = FailureError; + + fn try_from(h256: &ckb_protocol::H256) -> Result { + let ret = H256::from_slice(&[ h256.u0(), h256.u1(), h256.u2(), @@ -51,8 +56,8 @@ impl From<&ckb_protocol::H256> for H256 { h256.u29(), h256.u30(), h256.u31(), - ]) - .unwrap() + ])?; + Ok(ret) } } @@ -66,9 +71,11 @@ impl From<&ckb_core::transaction::ProposalShortId> for ckb_protocol::ProposalSho } } -impl From<&ckb_protocol::ProposalShortId> for ckb_core::transaction::ProposalShortId { - fn from(short_id: &ckb_protocol::ProposalShortId) -> Self { - Self::from_slice(&[ +impl TryFrom<&ckb_protocol::ProposalShortId> for ckb_core::transaction::ProposalShortId { + type Error = FailureError; + + fn try_from(short_id: &ckb_protocol::ProposalShortId) -> Result { + let ret = cast!(Self::from_slice(&[ short_id.u0(), short_id.u1(), short_id.u2(), @@ -79,164 +86,221 @@ impl From<&ckb_protocol::ProposalShortId> for ckb_core::transaction::ProposalSho short_id.u7(), short_id.u8_(), short_id.u9(), - ]) - .unwrap() + ]))?; + Ok(ret) } } -impl<'a> From> for ckb_core::block::Block { - fn from(block: ckb_protocol::Block<'a>) -> Self { - let commit_transactions = - FlatbuffersVectorIterator::new(block.commit_transactions().unwrap()) - .map(Into::into) +impl<'a> TryFrom> for ckb_core::block::Block { + type Error = FailureError; + + fn try_from(block: ckb_protocol::Block<'a>) -> Result { + let commit_transactions: Result, FailureError> = + FlatbuffersVectorIterator::new(cast!(block.commit_transactions())?) + .map(TryInto::try_into) .collect(); - let uncles = FlatbuffersVectorIterator::new(block.uncles().unwrap()) - .map(Into::into) - .collect(); + let uncles: Result, FailureError> = + FlatbuffersVectorIterator::new(cast!(block.uncles())?) + .map(TryInto::try_into) + .collect(); - let proposal_transactions = block - .proposal_transactions() - .unwrap() + let proposal_transactions: Result< + Vec, + FailureError, + > = cast!(block.proposal_transactions())? .iter() - .map(Into::into) + .map(TryInto::try_into) .collect(); - ckb_core::block::BlockBuilder::default() - .header(block.header().unwrap().into()) - .uncles(uncles) - .commit_transactions(commit_transactions) - .proposal_transactions(proposal_transactions) - .build() + let header = cast!(block.header())?; + + Ok(ckb_core::block::BlockBuilder::default() + .header(TryInto::try_into(header)?) + .uncles(uncles?) + .commit_transactions(commit_transactions?) + .proposal_transactions(proposal_transactions?) + .build()) } } -impl<'a> From> for ckb_core::uncle::UncleBlock { - fn from(uncle_block: ckb_protocol::UncleBlock<'a>) -> Self { - ckb_core::uncle::UncleBlock { - header: uncle_block.header().unwrap().into(), - cellbase: uncle_block.cellbase().unwrap().into(), - proposal_transactions: uncle_block - .proposal_transactions() - .unwrap() - .iter() - .map(Into::into) - .collect(), - } +impl<'a> TryFrom> for ckb_core::uncle::UncleBlock { + type Error = FailureError; + + fn try_from(uncle_block: ckb_protocol::UncleBlock<'a>) -> Result { + let proposal_transactions: Result< + Vec, + FailureError, + > = cast!(uncle_block.proposal_transactions())? + .iter() + .map(TryInto::try_into) + .collect(); + let header = cast!(uncle_block.header())?; + let cellbase = cast!(uncle_block.cellbase())?; + + Ok(ckb_core::uncle::UncleBlock { + header: TryInto::try_into(header)?, + cellbase: TryInto::try_into(cellbase)?, + proposal_transactions: proposal_transactions?, + }) } } -impl<'a> From> for ckb_core::header::Header { - fn from(header: ckb_protocol::Header<'a>) -> Self { - ckb_core::header::HeaderBuilder::default() +impl<'a> TryFrom> for ckb_core::header::Header { + type Error = FailureError; + + fn try_from(header: ckb_protocol::Header<'a>) -> Result { + let parent_hash = cast!(header.parent_hash())?; + let txs_commit = cast!(header.txs_commit())?; + let txs_proposal = cast!(header.txs_proposal())?; + let cellbase_id = cast!(header.cellbase_id())?; + let uncles_hash = cast!(header.uncles_hash())?; + + Ok(ckb_core::header::HeaderBuilder::default() .version(header.version()) - .parent_hash(header.parent_hash().unwrap().into()) + .parent_hash(TryInto::try_into(parent_hash)?) .timestamp(header.timestamp()) .number(header.number()) - .txs_commit(header.txs_commit().unwrap().into()) - .txs_proposal(header.txs_proposal().unwrap().into()) - .difficulty( - U256::from_little_endian(header.difficulty().and_then(|b| b.seq()).unwrap()) - .unwrap(), - ) - .cellbase_id(header.cellbase_id().unwrap().into()) - .uncles_hash(header.uncles_hash().unwrap().into()) + .txs_commit(TryInto::try_into(txs_commit)?) + .txs_proposal(TryInto::try_into(txs_proposal)?) + .difficulty(U256::from_little_endian(cast!(header + .difficulty() + .and_then(|d| d.seq()))?)?) + .cellbase_id(TryInto::try_into(cellbase_id)?) + .uncles_hash(TryInto::try_into(uncles_hash)?) .nonce(header.nonce()) - .proof(header.proof().and_then(|b| b.seq()).unwrap().to_vec()) + .proof(cast!(header + .proof() + .and_then(|p| p.seq()) + .map(|p| p.to_vec()))?) .uncles_count(header.uncles_count()) - .build() + .build()) } } -impl<'a> From> for ckb_core::transaction::Transaction { - fn from(transaction: ckb_protocol::Transaction<'a>) -> Self { - let deps = FlatbuffersVectorIterator::new(transaction.deps().unwrap()) - .map(Into::into) - .collect(); +impl<'a> TryFrom> for ckb_core::transaction::Transaction { + type Error = FailureError; - let inputs = FlatbuffersVectorIterator::new(transaction.inputs().unwrap()) - .map(Into::into) - .collect(); + fn try_from(transaction: ckb_protocol::Transaction<'a>) -> Result { + let deps: Result, FailureError> = + FlatbuffersVectorIterator::new(cast!(transaction.deps())?) + .map(TryInto::try_into) + .collect(); - let outputs = FlatbuffersVectorIterator::new(transaction.outputs().unwrap()) - .map(Into::into) - .collect(); + let inputs: Result, FailureError> = + FlatbuffersVectorIterator::new(cast!(transaction.inputs())?) + .map(TryInto::try_into) + .collect(); + + let outputs: Result, FailureError> = + FlatbuffersVectorIterator::new(cast!(transaction.outputs())?) + .map(TryInto::try_into) + .collect(); - ckb_core::transaction::TransactionBuilder::default() + Ok(ckb_core::transaction::TransactionBuilder::default() .version(transaction.version()) - .deps(deps) - .inputs(inputs) - .outputs(outputs) - .build() + .deps(deps?) + .inputs(inputs?) + .outputs(outputs?) + .build()) } } -impl<'a> From> +impl<'a> TryFrom> for (ckb_core::transaction::Transaction, ckb_core::Cycle) { - fn from(tx: ckb_protocol::ValidTransaction<'a>) -> Self { - let cycles = tx.cycles(); - (tx.transaction().unwrap().into(), cycles) + type Error = FailureError; + + fn try_from(vtx: ckb_protocol::ValidTransaction<'a>) -> Result { + let tx = cast!(vtx.transaction())?; + let cycles = vtx.cycles(); + Ok((TryInto::try_into(tx)?, cycles)) } } -impl<'a> From> for ckb_core::transaction::OutPoint { - fn from(out_point: ckb_protocol::OutPoint<'a>) -> Self { - ckb_core::transaction::OutPoint { - hash: out_point.hash().unwrap().into(), +impl<'a> TryFrom> for ckb_core::transaction::OutPoint { + type Error = FailureError; + + fn try_from(out_point: ckb_protocol::OutPoint<'a>) -> Result { + let hash = cast!(out_point.hash())?; + Ok(ckb_core::transaction::OutPoint { + hash: TryInto::try_into(hash)?, index: out_point.index(), - } + }) } } -impl<'a> From> for ckb_core::script::Script { - fn from(script: ckb_protocol::Script<'a>) -> Self { - let args = FlatbuffersVectorIterator::new(script.args().unwrap()) - .map(|argument| argument.seq().unwrap().to_vec()) - .collect(); +impl<'a> TryFrom> for ckb_core::script::Script { + type Error = FailureError; - let signed_args = FlatbuffersVectorIterator::new(script.signed_args().unwrap()) - .map(|argument| argument.seq().unwrap().to_vec()) + fn try_from(script: ckb_protocol::Script<'a>) -> Result { + let args: Option>> = FlatbuffersVectorIterator::new(cast!(script.args())?) + .map(|argument| argument.seq().map(|s| s.to_vec())) .collect(); - ckb_core::script::Script { + let signed_args: Option>> = + FlatbuffersVectorIterator::new(cast!(script.signed_args())?) + .map(|argument| argument.seq().map(|s| s.to_vec())) + .collect(); + + let reference = match script.reference() { + Some(reference) => Some(TryInto::try_into(reference)?), + None => None, + }; + + Ok(ckb_core::script::Script { version: script.version(), - args, + args: cast!(args)?, binary: script.binary().and_then(|s| s.seq()).map(|s| s.to_vec()), - signed_args, - reference: script.reference().map(Into::into), - } + signed_args: cast!(signed_args)?, + reference, + }) } } -impl<'a> From> for ckb_core::transaction::CellInput { - fn from(cell_input: ckb_protocol::CellInput<'a>) -> Self { - ckb_core::transaction::CellInput { +impl<'a> TryFrom> for ckb_core::transaction::CellInput { + type Error = FailureError; + + fn try_from(cell_input: ckb_protocol::CellInput<'a>) -> Result { + let hash = cast!(cell_input.hash())?; + let unlock = cast!(cell_input.unlock())?; + Ok(ckb_core::transaction::CellInput { previous_output: ckb_core::transaction::OutPoint { - hash: cell_input.hash().unwrap().into(), + hash: TryInto::try_into(hash)?, index: cell_input.index(), }, - unlock: cell_input.unlock().unwrap().into(), - } + unlock: TryInto::try_into(unlock)?, + }) } } -impl<'a> From> for ckb_core::transaction::CellOutput { - fn from(cell_output: ckb_protocol::CellOutput<'a>) -> Self { - ckb_core::transaction::CellOutput { +impl<'a> TryFrom> for ckb_core::transaction::CellOutput { + type Error = FailureError; + + fn try_from(cell_output: ckb_protocol::CellOutput<'a>) -> Result { + let lock = cast!(cell_output.lock())?; + let type_ = match cell_output.type_() { + Some(type_) => Some(TryInto::try_into(type_)?), + None => None, + }; + + Ok(ckb_core::transaction::CellOutput { capacity: cell_output.capacity(), - data: cell_output.data().and_then(|b| b.seq()).unwrap().to_vec(), - lock: cell_output.lock().unwrap().into(), - type_: cell_output.type_().map(Into::into), - } + data: cast!(cell_output.data().and_then(|s| s.seq()))?.to_vec(), + lock: TryInto::try_into(lock)?, + type_, + }) } } -impl<'a> From> for ckb_core::transaction::IndexTransaction { - fn from(it: ckb_protocol::IndexTransaction<'a>) -> Self { - ckb_core::transaction::IndexTransaction { +impl<'a> TryFrom> for ckb_core::transaction::IndexTransaction { + type Error = FailureError; + + fn try_from(it: ckb_protocol::IndexTransaction<'a>) -> Result { + let transaction = cast!(it.transaction())?; + Ok(ckb_core::transaction::IndexTransaction { index: it.index() as usize, - transaction: it.transaction().unwrap().into(), - } + transaction: TryInto::try_into(transaction)?, + }) } } diff --git a/protocol/src/error.rs b/protocol/src/error.rs new file mode 100644 index 0000000000..0121d653ad --- /dev/null +++ b/protocol/src/error.rs @@ -0,0 +1,7 @@ +use failure::Fail; + +#[derive(Debug, Fail, PartialEq)] +pub enum Error { + #[fail(display = "Malformed message")] + Malformed, +} diff --git a/protocol/src/lib.rs b/protocol/src/lib.rs index 83f4b24529..1c4402ae51 100644 --- a/protocol/src/lib.rs +++ b/protocol/src/lib.rs @@ -1,5 +1,6 @@ mod builder; mod convert; +pub mod error; #[rustfmt::skip] #[allow(clippy::all)] mod protocol_generated; @@ -69,3 +70,10 @@ pub fn short_transaction_id(key0: u64, key1: u64, transaction_hash: &H256) -> Sh short_transaction_id } + +#[macro_export] +macro_rules! cast { + ($expr:expr) => { + $expr.ok_or_else(|| $crate::error::Error::Malformed) + }; +} diff --git a/sync/src/relayer/block_proposal_process.rs b/sync/src/relayer/block_proposal_process.rs index c829557a8d..4f0c294c13 100644 --- a/sync/src/relayer/block_proposal_process.rs +++ b/sync/src/relayer/block_proposal_process.rs @@ -1,7 +1,9 @@ use crate::relayer::Relayer; -use ckb_protocol::{BlockProposal, FlatbuffersVectorIterator}; +use ckb_protocol::{cast, BlockProposal, FlatbuffersVectorIterator}; use ckb_shared::index::ChainIndex; use ckb_traits::chain_provider::ChainProvider; +use ckb_util::TryInto; +use failure::Error as FailureError; pub struct BlockProposalProcess<'a, CI: ChainIndex + 'a> { message: &'a BlockProposal<'a>, @@ -16,13 +18,15 @@ where BlockProposalProcess { message, relayer } } - pub fn execute(self) { + pub fn execute(self) -> Result<(), FailureError> { let chain_state = self.relayer.shared.chain_state().lock(); - FlatbuffersVectorIterator::new(self.message.transactions().unwrap()).for_each(|tx| { + let txs = FlatbuffersVectorIterator::new(cast!(self.message.transactions())?); + for tx in txs { let _ = chain_state.add_tx_to_pool( - tx.into(), + TryInto::try_into(tx)?, self.relayer.shared.consensus().max_block_cycles(), ); - }) + } + Ok(()) } } diff --git a/sync/src/relayer/block_transactions_process.rs b/sync/src/relayer/block_transactions_process.rs index 18388a27dd..36e9ed5515 100644 --- a/sync/src/relayer/block_transactions_process.rs +++ b/sync/src/relayer/block_transactions_process.rs @@ -1,9 +1,10 @@ use crate::relayer::Relayer; use ckb_core::transaction::Transaction; -use ckb_network::CKBProtocolContext; -use ckb_network::PeerIndex; -use ckb_protocol::{BlockTransactions, FlatbuffersVectorIterator}; +use ckb_network::{CKBProtocolContext, PeerIndex}; +use ckb_protocol::{cast, BlockTransactions, FlatbuffersVectorIterator}; use ckb_shared::index::ChainIndex; +use ckb_util::TryInto; +use failure::Error as FailureError; use std::sync::Arc; pub struct BlockTransactionsProcess<'a, CI: ChainIndex + 'a> { @@ -31,8 +32,8 @@ where } } - pub fn execute(self) { - let hash = self.message.hash().unwrap().into(); + pub fn execute(self) -> Result<(), FailureError> { + let hash = cast!(self.message.hash())?.try_into()?; if let Some(compact_block) = self .relayer .state @@ -40,15 +41,15 @@ where .lock() .remove(&hash) { - let transactions: Vec = - FlatbuffersVectorIterator::new(self.message.transactions().unwrap()) - .map(Into::into) + let transactions: Result, FailureError> = + FlatbuffersVectorIterator::new(cast!(self.message.transactions())?) + .map(TryInto::try_into) .collect(); let ret = { let chain_state = self.relayer.shared.chain_state().lock(); self.relayer - .reconstruct_block(&chain_state, &compact_block, transactions) + .reconstruct_block(&chain_state, &compact_block, transactions?) }; if let Ok(block) = ret { @@ -56,5 +57,6 @@ where .accept_block(self.nc, self.peer, &Arc::new(block)); } } + Ok(()) } } diff --git a/sync/src/relayer/compact_block.rs b/sync/src/relayer/compact_block.rs index 47d000045e..be0bb428bd 100644 --- a/sync/src/relayer/compact_block.rs +++ b/sync/src/relayer/compact_block.rs @@ -1,7 +1,9 @@ use ckb_core::header::Header; use ckb_core::transaction::{IndexTransaction, ProposalShortId}; use ckb_core::uncle::UncleBlock; -use ckb_protocol::{self, FlatbuffersVectorIterator}; +use ckb_protocol::{self, cast, FlatbuffersVectorIterator}; +use ckb_util::{TryFrom, TryInto}; +use failure::Error as FailureError; pub type ShortTransactionID = [u8; 6]; @@ -15,34 +17,39 @@ pub struct CompactBlock { pub proposal_transactions: Vec, } -impl<'a> From> for CompactBlock { - fn from(b: ckb_protocol::CompactBlock<'a>) -> Self { - CompactBlock { - header: b.header().unwrap().into(), +impl<'a> TryFrom> for CompactBlock { + type Error = FailureError; + + fn try_from(b: ckb_protocol::CompactBlock<'a>) -> Result { + let header = cast!(b.header())?; + let short_ids = cast!(b.short_ids())?; + let prefilled_transactions: Result, FailureError> = + FlatbuffersVectorIterator::new(cast!(b.prefilled_transactions())?) + .map(TryInto::try_into) + .collect(); + + let uncles: Result, FailureError> = + FlatbuffersVectorIterator::new(cast!(b.uncles())?) + .map(TryInto::try_into) + .collect(); + let proposal_transactions: Result, FailureError> = cast!(b.proposal_transactions())? + .iter() + .map(TryInto::try_into) + .collect(); + + Ok(CompactBlock { + header: header.try_into()?, nonce: b.nonce(), - short_ids: FlatbuffersVectorIterator::new(b.short_ids().unwrap()) - .map(|bytes| { + short_ids: cast!(FlatbuffersVectorIterator::new(short_ids) + .map(|bytes| bytes.seq().map(|seq| { let mut short_id = [0u8; 6]; - short_id.copy_from_slice(bytes.seq().unwrap()); + short_id.copy_from_slice(seq); short_id - }) - .collect(), - prefilled_transactions: FlatbuffersVectorIterator::new( - b.prefilled_transactions().unwrap(), - ) - .map(Into::into) - .collect(), - - uncles: FlatbuffersVectorIterator::new(b.uncles().unwrap()) - .map(Into::into) - .collect(), - - proposal_transactions: b - .proposal_transactions() - .unwrap() - .iter() - .map(Into::into) - .collect(), - } + })) + .collect::>>())?, + prefilled_transactions: prefilled_transactions?, + uncles: uncles?, + proposal_transactions: proposal_transactions?, + }) } } diff --git a/sync/src/relayer/compact_block_process.rs b/sync/src/relayer/compact_block_process.rs index c5fd3212c1..94ba5f3f5c 100644 --- a/sync/src/relayer/compact_block_process.rs +++ b/sync/src/relayer/compact_block_process.rs @@ -5,7 +5,9 @@ use ckb_protocol::{CompactBlock as FbsCompactBlock, RelayMessage}; use ckb_shared::index::ChainIndex; use ckb_shared::shared::Shared; use ckb_traits::{BlockMedianTimeContext, ChainProvider}; +use ckb_util::TryInto; use ckb_verification::{HeaderResolverWrapper, HeaderVerifier, Verifier}; +use failure::Error as FailureError; use flatbuffers::FlatBufferBuilder; use fnv::FnvHashMap; use numext_fixed_hash::H256; @@ -36,8 +38,8 @@ where } } - pub fn execute(self) { - let compact_block: CompactBlock = (*self.message).into(); + pub fn execute(self) -> Result<(), FailureError> { + let compact_block: CompactBlock = (*self.message).try_into()?; let block_hash = compact_block.header.hash(); let mut missing_indexes: Vec = Vec::new(); { @@ -94,6 +96,7 @@ where fbb.finish(message, None); let _ = self.nc.send(self.peer, fbb.finished_data().to_vec()); } + Ok(()) } } diff --git a/sync/src/relayer/get_block_proposal_process.rs b/sync/src/relayer/get_block_proposal_process.rs index ba41995d31..2b98d3d4d1 100644 --- a/sync/src/relayer/get_block_proposal_process.rs +++ b/sync/src/relayer/get_block_proposal_process.rs @@ -1,7 +1,9 @@ use crate::relayer::Relayer; use ckb_network::{CKBProtocolContext, PeerIndex}; -use ckb_protocol::{GetBlockProposal, RelayMessage}; +use ckb_protocol::{cast, GetBlockProposal, RelayMessage}; use ckb_shared::index::ChainIndex; +use ckb_util::TryInto; +use failure::Error as FailureError; use flatbuffers::FlatBufferBuilder; pub struct GetBlockProposalProcess<'a, CI: ChainIndex + 'a> { @@ -29,17 +31,21 @@ where } } - pub fn execute(self) { + pub fn execute(self) -> Result<(), FailureError> { let mut pending_proposals_request = self.relayer.state.pending_proposals_request.lock(); + let proposal_transactions = cast!(self.message.proposal_transactions())?; let transactions = { let chain_state = self.relayer.shared.chain_state().lock(); let tx_pool = chain_state.tx_pool(); - self.message - .proposal_transactions() - .unwrap() + + let proposals = proposal_transactions .iter() - .map(Into::into) + .map(TryInto::try_into) + .collect::, FailureError>>(); + + proposals? + .into_iter() .filter_map(|short_id| { tx_pool.get_tx(&short_id).or({ pending_proposals_request @@ -57,5 +63,6 @@ where fbb.finish(message, None); let _ = self.nc.send(self.peer, fbb.finished_data().to_vec()); + Ok(()) } } diff --git a/sync/src/relayer/get_block_transactions_process.rs b/sync/src/relayer/get_block_transactions_process.rs index 1e2a96e732..0c7031b03f 100644 --- a/sync/src/relayer/get_block_transactions_process.rs +++ b/sync/src/relayer/get_block_transactions_process.rs @@ -1,7 +1,9 @@ use crate::relayer::Relayer; use ckb_network::{CKBProtocolContext, PeerIndex}; -use ckb_protocol::{GetBlockTransactions, RelayMessage}; +use ckb_protocol::{cast, GetBlockTransactions, RelayMessage}; use ckb_shared::index::ChainIndex; +use ckb_util::TryInto; +use failure::Error as FailureError; use flatbuffers::FlatBufferBuilder; use log::debug; @@ -30,15 +32,14 @@ where } } - pub fn execute(self) { - let hash = self.message.hash().unwrap().into(); + pub fn execute(self) -> Result<(), FailureError> { + let hash = cast!(self.message.hash())?.try_into()?; debug!(target: "relay", "get_block_transactions {:?}", hash); + let indexes = cast!(self.message.indexes())?; + if let Some(block) = self.relayer.get_block(&hash) { - let transactions = self - .message - .indexes() - .unwrap() + let transactions = indexes .safe_slice() .iter() .filter_map(|i| block.commit_transactions().get(*i as usize).cloned()) @@ -50,5 +51,7 @@ where let _ = self.nc.send(self.peer, fbb.finished_data().to_vec()); } + + Ok(()) } } diff --git a/sync/src/relayer/mod.rs b/sync/src/relayer/mod.rs index 2c2f28f8f9..f2baf4d459 100644 --- a/sync/src/relayer/mod.rs +++ b/sync/src/relayer/mod.rs @@ -21,13 +21,16 @@ use bytes::Bytes; use ckb_chain::chain::ChainController; use ckb_core::block::{Block, BlockBuilder}; use ckb_core::transaction::{ProposalShortId, Transaction}; -use ckb_network::{CKBProtocolContext, CKBProtocolHandler, PeerIndex, TimerToken}; -use ckb_protocol::{short_transaction_id, short_transaction_id_keys, RelayMessage, RelayPayload}; +use ckb_network::{CKBProtocolContext, CKBProtocolHandler, PeerIndex, Severity, TimerToken}; +use ckb_protocol::{ + cast, short_transaction_id, short_transaction_id_keys, RelayMessage, RelayPayload, +}; use ckb_shared::chain_state::ChainState; use ckb_shared::index::ChainIndex; use ckb_shared::shared::Shared; use ckb_traits::ChainProvider; use ckb_util::Mutex; +use failure::Error as FailureError; use flatbuffers::{get_root, FlatBufferBuilder}; use fnv::{FnvHashMap, FnvHashSet}; use log::{debug, info}; @@ -60,48 +63,72 @@ where } } - fn process(&self, nc: &CKBProtocolContext, peer: PeerIndex, message: RelayMessage) { + fn try_process( + &self, + nc: &CKBProtocolContext, + peer: PeerIndex, + message: RelayMessage, + ) -> Result<(), FailureError> { match message.payload_type() { - RelayPayload::CompactBlock => CompactBlockProcess::new( - &message.payload_as_compact_block().unwrap(), - self, - peer, - nc, - ) - .execute(), - RelayPayload::ValidTransaction => TransactionProcess::new( - &message.payload_as_valid_transaction().unwrap(), - self, - peer, - nc, - ) - .execute(), - RelayPayload::GetBlockTransactions => GetBlockTransactionsProcess::new( - &message.payload_as_get_block_transactions().unwrap(), - self, - peer, - nc, - ) - .execute(), - RelayPayload::BlockTransactions => BlockTransactionsProcess::new( - &message.payload_as_block_transactions().unwrap(), - self, - peer, - nc, - ) - .execute(), - RelayPayload::GetBlockProposal => GetBlockProposalProcess::new( - &message.payload_as_get_block_proposal().unwrap(), - self, - peer, - nc, - ) - .execute(), + RelayPayload::CompactBlock => { + CompactBlockProcess::new( + &cast!(message.payload_as_compact_block())?, + self, + peer, + nc, + ) + .execute()?; + } + RelayPayload::ValidTransaction => { + TransactionProcess::new( + &cast!(message.payload_as_valid_transaction())?, + self, + peer, + nc, + ) + .execute()?; + } + RelayPayload::GetBlockTransactions => { + GetBlockTransactionsProcess::new( + &cast!(message.payload_as_get_block_transactions())?, + self, + peer, + nc, + ) + .execute()?; + } + RelayPayload::BlockTransactions => { + BlockTransactionsProcess::new( + &cast!(message.payload_as_block_transactions())?, + self, + peer, + nc, + ) + .execute()?; + } + RelayPayload::GetBlockProposal => { + GetBlockProposalProcess::new( + &cast!(message.payload_as_get_block_proposal())?, + self, + peer, + nc, + ) + .execute()?; + } RelayPayload::BlockProposal => { - BlockProposalProcess::new(&message.payload_as_block_proposal().unwrap(), self) - .execute() + BlockProposalProcess::new(&cast!(message.payload_as_block_proposal())?, self) + .execute()?; + } + RelayPayload::NONE => { + cast!(None)?; } - RelayPayload::NONE => {} + } + Ok(()) + } + + fn process(&self, nc: &CKBProtocolContext, peer: PeerIndex, message: RelayMessage) { + if self.try_process(nc, peer, message).is_err() { + nc.report_peer(peer, Severity::Bad("Malformed RelayMessage")); } } @@ -199,24 +226,27 @@ where // append remain transactions short_ids_iter.for_each(|short_id| block_transactions.push(txs_map.remove(short_id))); - let mut missing_indexes = Vec::new(); - - for (i, t) in block_transactions.iter().enumerate() { - if t.is_none() { - missing_indexes.push(i); - } - } + let missing = block_transactions.iter().any(|tx| tx.is_none()); - if missing_indexes.is_empty() { + if !missing { + let txs = block_transactions + .into_iter() + .collect::>>() + .expect("missing checked, should not fail"); let block = BlockBuilder::default() .header(compact_block.header.clone()) .uncles(compact_block.uncles.clone()) - .commit_transactions(block_transactions.into_iter().map(|t| t.unwrap()).collect()) + .commit_transactions(txs) .proposal_transactions(compact_block.proposal_transactions.clone()) .build(); Ok(block) } else { + let missing_indexes = block_transactions + .iter() + .enumerate() + .filter_map(|(i, t)| if t.is_none() { Some(i) } else { None }) + .collect(); Err(missing_indexes) } } diff --git a/sync/src/relayer/transaction_process.rs b/sync/src/relayer/transaction_process.rs index 750850ebca..3b3d3f71b8 100644 --- a/sync/src/relayer/transaction_process.rs +++ b/sync/src/relayer/transaction_process.rs @@ -5,7 +5,9 @@ use ckb_protocol::{RelayMessage, ValidTransaction as FbsValidTransaction}; use ckb_shared::index::ChainIndex; use ckb_shared::tx_pool::types::PoolError; use ckb_traits::chain_provider::ChainProvider; +use ckb_util::TryInto; use ckb_verification::TransactionError; +use failure::Error as FailureError; use flatbuffers::FlatBufferBuilder; use log::debug; use std::time::Duration; @@ -37,8 +39,8 @@ where } } - pub fn execute(self) { - let (tx, relay_cycles): (Transaction, Cycle) = (*self.message).into(); + pub fn execute(self) -> Result<(), FailureError> { + let (tx, relay_cycles): (Transaction, Cycle) = (*self.message).try_into()?; let tx_result = { let chain_state = self.relayer.shared.chain_state().lock(); let max_block_cycles = self.relayer.shared.consensus().max_block_cycles(); @@ -83,5 +85,7 @@ where self.nc.ban_peer(self.peer, DEFAULT_BAN_TIME); } } + + Ok(()) } } diff --git a/sync/src/synchronizer/block_process.rs b/sync/src/synchronizer/block_process.rs index 40c350ca0d..2a1a722082 100644 --- a/sync/src/synchronizer/block_process.rs +++ b/sync/src/synchronizer/block_process.rs @@ -3,6 +3,8 @@ use ckb_core::block::Block; use ckb_network::{CKBProtocolContext, PeerIndex}; use ckb_protocol::Block as PBlock; use ckb_shared::index::ChainIndex; +use ckb_util::TryInto; +use failure::Error as FailureError; use log::debug; pub struct BlockProcess<'a, CI: ChainIndex + 'a> { @@ -28,11 +30,12 @@ where } } - pub fn execute(self) { - let block: Block = (*self.message).into(); + pub fn execute(self) -> Result<(), FailureError> { + let block: Block = (*self.message).try_into()?; debug!(target: "sync", "BlockProcess received block {} {:x}", block.header().number(), block.header().hash()); self.synchronizer.peers.block_received(self.peer, &block); self.synchronizer.process_new_block(self.peer, block); + Ok(()) } } diff --git a/sync/src/synchronizer/filter_process.rs b/sync/src/synchronizer/filter_process.rs index 0e8b6cc1b5..4a775999e5 100644 --- a/sync/src/synchronizer/filter_process.rs +++ b/sync/src/synchronizer/filter_process.rs @@ -1,8 +1,9 @@ use crate::synchronizer::Synchronizer; use crate::types::TransactionFilter; use ckb_network::PeerIndex; -use ckb_protocol::{AddFilter, SetFilter}; +use ckb_protocol::{cast, AddFilter, SetFilter}; use ckb_shared::index::ChainIndex; +use failure::Error as FailureError; pub struct SetFilterProcess<'a, CI: ChainIndex + 'a> { message: &'a SetFilter<'a>, @@ -26,16 +27,18 @@ where } } - pub fn execute(self) { + pub fn execute(self) -> Result<(), FailureError> { // TODO add filter size and num_hashes max value checking let mut filters = self.synchronizer.peers.transaction_filters.write(); + let msg = cast!(self.message.filter())?; filters.entry(self.peer).or_insert_with(|| { TransactionFilter::new( - self.message.filter().unwrap(), + msg, self.message.num_hashes() as usize, self.message.hash_seed() as usize, ) }); + Ok(()) } } @@ -61,11 +64,13 @@ where } } - pub fn execute(self) { + pub fn execute(self) -> Result<(), FailureError> { let mut filters = self.synchronizer.peers.transaction_filters.write(); + let msg = cast!(self.message.filter())?; filters .entry(self.peer) - .and_modify(|filter| filter.update(self.message.filter().unwrap())); + .and_modify(|filter| filter.update(msg)); + Ok(()) } } @@ -82,8 +87,9 @@ where Self { peer, synchronizer } } - pub fn execute(self) { + pub fn execute(self) -> Result<(), FailureError> { let mut filters = self.synchronizer.peers.transaction_filters.write(); filters.remove(&self.peer); + Ok(()) } } diff --git a/sync/src/synchronizer/get_blocks_process.rs b/sync/src/synchronizer/get_blocks_process.rs index 023f28ae73..475f3b328c 100644 --- a/sync/src/synchronizer/get_blocks_process.rs +++ b/sync/src/synchronizer/get_blocks_process.rs @@ -1,7 +1,9 @@ use crate::synchronizer::Synchronizer; use ckb_network::{CKBProtocolContext, PeerIndex}; -use ckb_protocol::{GetBlocks, SyncMessage}; +use ckb_protocol::{cast, GetBlocks, SyncMessage}; use ckb_shared::index::ChainIndex; +use ckb_util::TryInto; +use failure::Error as FailureError; use flatbuffers::FlatBufferBuilder; use log::debug; @@ -30,19 +32,32 @@ where } } - pub fn execute(self) { - self.message.block_hashes().unwrap().iter().for_each(|fbs_h256| { - let block_hash = fbs_h256.into(); + pub fn execute(self) -> Result<(), FailureError> { + let block_hashes = cast!(self.message.block_hashes())?; + + for fbs_h256 in block_hashes { + let block_hash = fbs_h256.try_into()?; debug!(target: "sync", "get_blocks {:x}", block_hash); if let Some(block) = self.synchronizer.get_block(&block_hash) { debug!(target: "sync", "respond_block {} {:x}", block.header().number(), block.header().hash()); - if let Some(filter) = self.synchronizer.peers.transaction_filters.read().get(&self.peer) { - let transactions_index = block.commit_transactions().iter().enumerate().filter(|(_index, tx)| - filter.contains(tx) - ).map(|ti| ti.0).collect::>(); + if let Some(filter) = self + .synchronizer + .peers + .transaction_filters + .read() + .get(&self.peer) + { + let transactions_index = block + .commit_transactions() + .iter() + .enumerate() + .filter(|(_index, tx)| filter.contains(tx)) + .map(|ti| ti.0) + .collect::>(); let fbb = &mut FlatBufferBuilder::new(); - let message = SyncMessage::build_filtered_block(fbb, &block, &transactions_index); + let message = + SyncMessage::build_filtered_block(fbb, &block, &transactions_index); fbb.finish(message, None); let _ = self.nc.send(self.peer, fbb.finished_data().to_vec()); } else { @@ -55,6 +70,8 @@ where // TODO response not found // TODO add timeout check in synchronizer } - }) + } + + Ok(()) } } diff --git a/sync/src/synchronizer/get_headers_process.rs b/sync/src/synchronizer/get_headers_process.rs index 6a1f16d82c..e6af618e53 100644 --- a/sync/src/synchronizer/get_headers_process.rs +++ b/sync/src/synchronizer/get_headers_process.rs @@ -2,8 +2,10 @@ use crate::synchronizer::Synchronizer; use crate::MAX_LOCATOR_SIZE; use ckb_core::header::Header; use ckb_network::{CKBProtocolContext, PeerIndex, Severity}; -use ckb_protocol::{GetHeaders, SyncMessage}; +use ckb_protocol::{cast, GetHeaders, SyncMessage}; use ckb_shared::index::ChainIndex; +use ckb_util::TryInto; +use failure::Error as FailureError; use flatbuffers::FlatBufferBuilder; use log::{debug, info, warn}; use numext_fixed_hash::H256; @@ -33,48 +35,50 @@ where } } - pub fn execute(self) { + pub fn execute(self) -> Result<(), FailureError> { if self.synchronizer.is_initial_block_download() { info!(target: "sync", "Ignoring getheaders from peer={} because node is in initial block download", self.peer); - return; + return Ok(()); } - if let Some(locator) = self.message.block_locator_hashes() { - let locator_size = locator.len(); - if locator_size > MAX_LOCATOR_SIZE { - warn!(target: "sync", " getheaders locator size {} from peer={}", locator_size, self.peer); - self.nc - .report_peer(self.peer, Severity::Bad("over maximum locator size")); - return; - } - let hash_stop = H256::zero(); // TODO PENDING self.message.hash_stop().unwrap().into(); - let block_locator_hashes = locator.iter().map(Into::into).collect::>(); + let locator = cast!(self.message.block_locator_hashes())?; + let locator_size = locator.len(); + if locator_size > MAX_LOCATOR_SIZE { + warn!(target: "sync", " getheaders locator size {} from peer={}", locator_size, self.peer); + cast!(None)?; + } - if let Some(block_number) = self - .synchronizer - .locate_latest_common_block(&hash_stop, &block_locator_hashes[..]) - { - debug!(target: "sync", "\n\nheaders latest_common={} tip={} begin\n\n", block_number, {self.synchronizer.tip_header().number()}); + let hash_stop = H256::zero(); // TODO PENDING self.message.hash_stop().into(); + let block_locator_hashes = locator + .iter() + .map(TryInto::try_into) + .collect::, FailureError>>()?; - self.synchronizer.peers.getheaders_received(self.peer); - let headers: Vec

= self - .synchronizer - .get_locator_response(block_number, &hash_stop); - // response headers + if let Some(block_number) = self + .synchronizer + .locate_latest_common_block(&hash_stop, &block_locator_hashes[..]) + { + debug!(target: "sync", "\n\nheaders latest_common={} tip={} begin\n\n", block_number, {self.synchronizer.tip_header().number()}); + + self.synchronizer.peers.getheaders_received(self.peer); + let headers: Vec
= self + .synchronizer + .get_locator_response(block_number, &hash_stop); + // response headers - debug!(target: "sync", "\nheaders len={}\n", headers.len()); + debug!(target: "sync", "\nheaders len={}\n", headers.len()); - let fbb = &mut FlatBufferBuilder::new(); - let message = SyncMessage::build_headers(fbb, &headers); - fbb.finish(message, None); - let _ = self.nc.send(self.peer, fbb.finished_data().to_vec()); - } else { - warn!(target: "sync", "\n\nunknown block headers from peer {} {:#?}\n\n", self.peer, block_locator_hashes); - // Got 'headers' message without known blocks - // ban or close peers - self.nc - .report_peer(self.peer, Severity::Bad("without common headers")); - } + let fbb = &mut FlatBufferBuilder::new(); + let message = SyncMessage::build_headers(fbb, &headers); + fbb.finish(message, None); + let _ = self.nc.send(self.peer, fbb.finished_data().to_vec()); + } else { + warn!(target: "sync", "\n\nunknown block headers from peer {} {:#?}\n\n", self.peer, block_locator_hashes); + // Got 'headers' message without known blocks + // ban or close peers + self.nc + .report_peer(self.peer, Severity::Bad("without common headers")); } + Ok(()) } } diff --git a/sync/src/synchronizer/headers_process.rs b/sync/src/synchronizer/headers_process.rs index 3e07f46a23..845b8994a8 100644 --- a/sync/src/synchronizer/headers_process.rs +++ b/sync/src/synchronizer/headers_process.rs @@ -2,12 +2,13 @@ use crate::synchronizer::{BlockStatus, Synchronizer}; use crate::MAX_HEADERS_LEN; use ckb_core::header::Header; use ckb_network::{CKBProtocolContext, PeerIndex}; -use ckb_protocol::{FlatbuffersVectorIterator, Headers}; +use ckb_protocol::{cast, FlatbuffersVectorIterator, Headers}; use ckb_shared::index::ChainIndex; use ckb_traits::{BlockMedianTimeContext, ChainProvider}; +use ckb_util::TryInto; use ckb_verification::{Error as VerifyError, HeaderResolver, HeaderVerifier, Verifier}; -use log; -use log::{debug, log_enabled}; +use failure::Error as FailureError; +use log::{self, debug, log_enabled, warn}; use numext_fixed_hash::H256; use numext_fixed_uint::U256; use std::sync::Arc; @@ -163,14 +164,6 @@ where } } - fn is_empty(&self) -> bool { - self.message.headers().unwrap().len() == 0 - } - - fn is_oversize(&self) -> bool { - self.message.headers().unwrap().len() > MAX_HEADERS_LEN - } - fn is_continuous(&self, headers: &[Header]) -> bool { for window in headers.windows(2) { if let [parent, header] = &window { @@ -205,28 +198,30 @@ where acceptor.accept() } - pub fn execute(self) { + pub fn execute(self) -> Result<(), FailureError> { debug!(target: "sync", "HeadersProcess begin"); - if self.is_oversize() { + let headers = cast!(self.message.headers())?; + + if headers.len() > MAX_HEADERS_LEN { self.synchronizer.peers.misbehavior(self.peer, 20); - debug!(target: "sync", "HeadersProcess is_oversize"); - return; + warn!(target: "sync", "HeadersProcess is_oversize"); + return Ok(()); } - if self.is_empty() { + if headers.len() == 0 { debug!(target: "sync", "HeadersProcess is_empty"); - return; + return Ok(()); } - let headers = FlatbuffersVectorIterator::new(self.message.headers().unwrap()) - .map(Into::into) - .collect::>(); + let headers = FlatbuffersVectorIterator::new(headers) + .map(TryInto::try_into) + .collect::, FailureError>>()?; if !self.is_continuous(&headers) { self.synchronizer.peers.misbehavior(self.peer, 20); debug!(target: "sync", "HeadersProcess is not continuous"); - return; + return Ok(()); } let result = self.accept_first(&headers[0]); @@ -237,7 +232,7 @@ where .misbehavior(self.peer, result.misbehavior); } debug!(target: "sync", "\n\nHeadersProcess accept_first is_valid {:?} headers = {:?}\n\n", result, headers[0]); - return; + return Ok(()); } for window in headers.windows(2) { @@ -258,7 +253,7 @@ where .misbehavior(self.peer, result.misbehavior); } debug!(target: "sync", "HeadersProcess accept is invalid {:?}", result); - return; + return Ok(()); } } } @@ -300,6 +295,7 @@ where self.synchronizer .send_getheaders_to_peer(self.nc, self.peer, start); } + Ok(()) } } diff --git a/sync/src/synchronizer/mod.rs b/sync/src/synchronizer/mod.rs index 886ac968be..ec0503b974 100644 --- a/sync/src/synchronizer/mod.rs +++ b/sync/src/synchronizer/mod.rs @@ -27,7 +27,7 @@ use ckb_chain_spec::consensus::Consensus; use ckb_core::block::Block; use ckb_core::header::{BlockNumber, Header}; use ckb_network::{CKBProtocolContext, CKBProtocolHandler, PeerIndex, Severity, TimerToken}; -use ckb_protocol::{SyncMessage, SyncPayload}; +use ckb_protocol::{cast, SyncMessage, SyncPayload}; use ckb_shared::index::ChainIndex; use ckb_shared::shared::Shared; use ckb_traits::ChainProvider; @@ -136,35 +136,53 @@ impl Synchronizer { outbound_peers_with_protect: Arc::new(AtomicUsize::new(0)), } } - - fn process(&self, nc: &CKBProtocolContext, peer: PeerIndex, message: SyncMessage) { + fn try_process( + &self, + nc: &CKBProtocolContext, + peer: PeerIndex, + message: SyncMessage, + ) -> Result<(), FailureError> { match message.payload_type() { SyncPayload::GetHeaders => { - GetHeadersProcess::new(&message.payload_as_get_headers().unwrap(), self, peer, nc) - .execute() + GetHeadersProcess::new(&cast!(message.payload_as_get_headers())?, self, peer, nc) + .execute()?; } SyncPayload::Headers => { - HeadersProcess::new(&message.payload_as_headers().unwrap(), self, peer, nc) - .execute() + HeadersProcess::new(&cast!(message.payload_as_headers())?, self, peer, nc) + .execute()?; } SyncPayload::GetBlocks => { - GetBlocksProcess::new(&message.payload_as_get_blocks().unwrap(), self, peer, nc) - .execute() + GetBlocksProcess::new(&cast!(message.payload_as_get_blocks())?, self, peer, nc) + .execute()?; } SyncPayload::Block => { - BlockProcess::new(&message.payload_as_block().unwrap(), self, peer, nc).execute() + BlockProcess::new(&cast!(message.payload_as_block())?, self, peer, nc).execute()?; } SyncPayload::SetFilter => { - SetFilterProcess::new(&message.payload_as_set_filter().unwrap(), self, peer) - .execute() + SetFilterProcess::new(&cast!(message.payload_as_set_filter())?, self, peer) + .execute()?; } SyncPayload::AddFilter => { - AddFilterProcess::new(&message.payload_as_add_filter().unwrap(), self, peer) - .execute() + AddFilterProcess::new(&cast!(message.payload_as_add_filter())?, self, peer) + .execute()?; + } + SyncPayload::ClearFilter => { + ClearFilterProcess::new(self, peer).execute()?; + } + SyncPayload::FilteredBlock => { + // ignore, should not receive FilteredBlock in full node mode + cast!(None)?; + } + SyncPayload::NONE => { + cast!(None)?; } - SyncPayload::ClearFilter => ClearFilterProcess::new(self, peer).execute(), - SyncPayload::FilteredBlock => {} // ignore, should not receive FilteredBlock in full node mode - SyncPayload::NONE => {} + } + Ok(()) + } + + fn process(&self, nc: &CKBProtocolContext, peer: PeerIndex, message: SyncMessage) { + if self.try_process(nc, peer, message).is_err() { + nc.report_peer(peer, Severity::Bad("Malformed SyncMessage")); } } @@ -453,12 +471,11 @@ impl Synchronizer { .block_header(&block.header().parent_hash()) .is_some() { - let ret = self.accept_block(peer, &block); - if ret.is_err() { + if let Err(e) = self.accept_block(peer, &block) { debug!( target: "sync", "[Synchronizer] accept_block {:?} error {:?}", block, - ret.unwrap_err() + e ); } } else { @@ -592,7 +609,11 @@ impl Synchronizer { self.send_getheaders_to_peer( nc, *peer, - &state.chain_sync.work_header.clone().unwrap(), + &state + .chain_sync + .work_header + .clone() + .expect("work_header be assigned"), ); } } @@ -1196,7 +1217,9 @@ mod tests { let fbs_headers = get_root::(fbb.finished_data()); let peer = 1usize; - HeadersProcess::new(&fbs_headers, &synchronizer1, peer, &mock_network_context(0)).execute(); + HeadersProcess::new(&fbs_headers, &synchronizer1, peer, &mock_network_context(0)) + .execute() + .unwrap(); let best_known_header = synchronizer1.peers.best_known_header(peer); @@ -1224,7 +1247,9 @@ mod tests { fbb.finish(fbs_block, None); let fbs_block = get_root::(fbb.finished_data()); - BlockProcess::new(&fbs_block, &synchronizer1, peer, &mock_network_context(0)).execute(); + BlockProcess::new(&fbs_block, &synchronizer1, peer, &mock_network_context(0)) + .execute() + .unwrap(); } assert_eq!( diff --git a/util/src/lib.rs b/util/src/lib.rs index fdaa9c30c3..7774644263 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -1,5 +1,6 @@ mod unstable; +pub use crate::unstable::{TryFrom, TryInto}; pub use parking_lot::{Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard}; /// Helper macro for reducing boilerplate code for matching `Option` together diff --git a/util/src/unstable/mod.rs b/util/src/unstable/mod.rs index 8b13789179..30abac43e2 100644 --- a/util/src/unstable/mod.rs +++ b/util/src/unstable/mod.rs @@ -1 +1,3 @@ +pub mod try_convert; +pub use try_convert::{TryFrom, TryInto}; diff --git a/util/src/unstable/try_convert.rs b/util/src/unstable/try_convert.rs new file mode 100644 index 0000000000..cfe8a76dfd --- /dev/null +++ b/util/src/unstable/try_convert.rs @@ -0,0 +1,28 @@ +pub trait TryInto: Sized { + /// The type returned in the event of a conversion error. + type Error; + + /// Performs the conversion. + fn try_into(self) -> Result; +} + +/// Attempt to construct `Self` via a conversion. +pub trait TryFrom: Sized { + /// The type returned in the event of a conversion error. + type Error; + + /// Performs the conversion. + fn try_from(value: T) -> Result; +} + +// TryFrom implies TryInto +impl TryInto for T +where + U: TryFrom, +{ + type Error = U::Error; + + fn try_into(self) -> Result { + U::try_from(self) + } +} From 3b958767926a1152cecd618c743aa7a643f128fa Mon Sep 17 00:00:00 2001 From: quake wang Date: Fri, 22 Mar 2019 22:23:47 +0900 Subject: [PATCH 80/99] chore: resolve review comment --- shared/src/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/src/error.rs b/shared/src/error.rs index 1f73d9002c..e374b686c7 100644 --- a/shared/src/error.rs +++ b/shared/src/error.rs @@ -7,7 +7,7 @@ pub enum SharedError { InvalidInput, #[fail(display = "InvalidOutput")] InvalidOutput, - #[fail(display = "InvalidTransaction")] + #[fail(display = "InvalidTransaction: {}", _0)] InvalidTransaction(String), #[fail(display = "InvalidParentBlock")] InvalidParentBlock, From ebe24da29ee19674cf5fbfaab0b95b56476684e0 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" Date: Sun, 24 Mar 2019 21:15:20 +0000 Subject: [PATCH 81/99] chore(deps): bump tokio from 0.1.17 to 0.1.18 Bumps [tokio](https://github.com/tokio-rs/tokio) from 0.1.17 to 0.1.18. - [Release notes](https://github.com/tokio-rs/tokio/releases) - [Commits](https://github.com/tokio-rs/tokio/compare/tokio-0.1.17...tokio-0.1.18) Signed-off-by: dependabot[bot] --- Cargo.lock | 64 +++++++++++++++++++++++----------------------- network/Cargo.toml | 2 +- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 01dfa409e7..0352439b52 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -489,7 +489,7 @@ dependencies = [ "tentacle-identify 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)", "tentacle-ping 0.2.0 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)", "tentacle-secio 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)", - "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", "unsigned-varint 0.2.2 (git+https://github.com/paritytech/unsigned-varint)", ] @@ -1298,12 +1298,12 @@ dependencies = [ "net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-executor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-executor 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-reactor 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-tcp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-threadpool 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-threadpool 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-timer 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "want 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1427,7 +1427,7 @@ dependencies = [ "jsonrpc-server-utils 10.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", "unicase 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1442,7 +1442,7 @@ dependencies = [ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "unicase 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2567,8 +2567,8 @@ dependencies = [ "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "parity-multiaddr 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "tentacle-secio 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)", - "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-threadpool 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-threadpool 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-yamux 0.1.4 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)", ] @@ -2587,7 +2587,7 @@ dependencies = [ "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)", - "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", "trust-dns 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2600,7 +2600,7 @@ dependencies = [ "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)", - "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2633,7 +2633,7 @@ dependencies = [ "ring 0.14.6 (registry+https://github.com/rust-lang/crates.io-index)", "secp256k1 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)", "sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", "twofish 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "unsigned-varint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "untrusted 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2710,7 +2710,7 @@ dependencies = [ [[package]] name = "tokio" -version = "0.1.17" +version = "0.1.18" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2718,14 +2718,14 @@ dependencies = [ "mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-current-thread 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-executor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-current-thread 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-executor 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-fs 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-reactor 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-sync 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-tcp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-threadpool 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-threadpool 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-timer 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-trace-core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-udp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2753,8 +2753,8 @@ dependencies = [ "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)", "scoped-tls 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-executor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-executor 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-reactor 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-timer 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2762,16 +2762,16 @@ dependencies = [ [[package]] name = "tokio-current-thread" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-executor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-executor 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "tokio-executor" -version = "0.1.6" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2785,7 +2785,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-threadpool 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-threadpool 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2828,7 +2828,7 @@ dependencies = [ "num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-executor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-executor 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-sync 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2865,7 +2865,7 @@ dependencies = [ [[package]] name = "tokio-threadpool" -version = "0.1.12" +version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "crossbeam-deque 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2876,7 +2876,7 @@ dependencies = [ "num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-executor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-executor 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2887,7 +2887,7 @@ dependencies = [ "crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-executor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-executor 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2939,7 +2939,7 @@ dependencies = [ "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2972,7 +2972,7 @@ dependencies = [ "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "radix_trie 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-tcp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "trust-dns-proto 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2992,7 +2992,7 @@ dependencies = [ "rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "smallvec 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", "socket2 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-executor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-executor 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-reactor 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-tcp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3491,11 +3491,11 @@ dependencies = [ "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" "checksum tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e9175261fbdb60781fcd388a4d6cc7e14764a2b629a7ad94abb439aed223a44f" "checksum tinytemplate 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7655088894274afb52b807bd3c87072daa1fedd155068b8705cabfd628956115" -"checksum tokio 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)" = "1021bb1f4150435ab8f222eb7ed37c60b2d57037def63ba43085a79f387512d7" +"checksum tokio 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "65641e515a437b308ab131a82ce3042ff9795bef5d6c5a9be4eb24195c417fd9" "checksum tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5c501eceaf96f0e1793cf26beb63da3d11c738c4a943fdf3746d81d64684c39f" "checksum tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)" = "aeeffbbb94209023feaef3c196a41cbcdafa06b4a6f893f68779bb5e53796f71" -"checksum tokio-current-thread 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "c756b04680eea21902a46fca4e9f410a2332c04995af590e07ff262e2193a9a3" -"checksum tokio-executor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "30c6dbf2d1ad1de300b393910e8a3aa272b724a400b6531da03eed99e329fbf0" +"checksum tokio-current-thread 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "d16217cad7f1b840c5a97dfb3c43b0c871fef423a6e8d2118c604e843662a443" +"checksum tokio-executor 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "83ea44c6c0773cc034771693711c35c677b4b5a4b21b9e7071704c54de7d555e" "checksum tokio-fs 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "3fe6dc22b08d6993916647d108a1a7d15b9cd29c4f4496c62b92c45b5041b7af" "checksum tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "5090db468dad16e1a7a54c8c67280c5e4b544f3d3e018f0b913b400261f85926" "checksum tokio-proto 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8fbb47ae81353c63c487030659494b295f6cb6576242f907f203473b191b0389" @@ -3503,7 +3503,7 @@ dependencies = [ "checksum tokio-service 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "24da22d077e0f15f55162bdbdc661228c1581892f52074fb242678d015b45162" "checksum tokio-sync 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1bf2b9dac2a0509b5cfd1df5aa25eafacb616a42a491a13604d6bbeab4486363" "checksum tokio-tcp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1d14b10654be682ac43efee27401d792507e30fd8d26389e1da3b185de2e4119" -"checksum tokio-threadpool 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "742e511f6ce2298aeb86fc9ea0d8df81c2388c6ebae3dc8a7316e8c9df0df801" +"checksum tokio-threadpool 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "ec5759cf26cf9659555f36c431b515e3d05f66831741c85b4b5d5dfb9cf1323c" "checksum tokio-timer 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "2910970404ba6fa78c5539126a9ae2045d62e3713041e447f695f41405a120c6" "checksum tokio-trace-core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "350c9edade9830dc185ae48ba45667a445ab59f6167ef6d0254ec9d2430d9dd3" "checksum tokio-udp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "66268575b80f4a4a710ef83d087fdfeeabdce9b74c797535fbac18a2cb906e92" diff --git a/network/Cargo.toml b/network/Cargo.toml index 37e124f0e8..166b03324d 100644 --- a/network/Cargo.toml +++ b/network/Cargo.toml @@ -17,7 +17,7 @@ ckb-util = { path = "../util" } unsigned-varint = {git = "https://github.com/paritytech/unsigned-varint", features = ["codec"]} log = "0.4.5" bytes = "0.4.12" -tokio = "0.1.17" +tokio = "0.1.18" futures = { version = "0.1.19", features = ["use_std"] } snap = "0.2" p2p = { git = "https://github.com/nervosnetwork/p2p", rev="6c1f778914dae864d13c1d9110a08b46fb4d38fe", package="tentacle" } From 90cb6f40460f70193567167f28d78bfcd4699593 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" Date: Sun, 24 Mar 2019 21:15:22 +0000 Subject: [PATCH 82/99] chore(deps): bump bit-vec from 0.5.0 to 0.5.1 Bumps [bit-vec](https://github.com/contain-rs/bit-vec) from 0.5.0 to 0.5.1. - [Release notes](https://github.com/contain-rs/bit-vec/releases) - [Commits](https://github.com/contain-rs/bit-vec/commits) Signed-off-by: dependabot[bot] --- Cargo.lock | 8 ++++---- core/Cargo.toml | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 01dfa409e7..3cf1746467 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -145,12 +145,12 @@ name = "bit-set" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "bit-vec 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bit-vec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "bit-vec" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -377,7 +377,7 @@ name = "ckb-core" version = "0.7.0-pre" dependencies = [ "bincode 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "bit-vec 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bit-vec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "ckb-merkle-tree 0.7.0-pre", "ckb-util 0.7.0-pre", "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3259,7 +3259,7 @@ dependencies = [ "checksum bincode 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3efe0b4c8eaeed8600549c29f538a6a11bf422858d0ed435b1d70ec4ab101190" "checksum bindgen 0.47.1 (registry+https://github.com/rust-lang/crates.io-index)" = "91f5aa1a54c5997396b59cfac31af9c1cbcaa988958c4ce84364257b2cadfad0" "checksum bit-set 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6f1efcc46c18245a69c38fcc5cc650f16d3a59d034f3106e9ed63748f695730a" -"checksum bit-vec 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4440d5cb623bb7390ae27fec0bb6c61111969860f8e3ae198bfa0663645e67cf" +"checksum bit-vec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f59bbe95d4e52a6398ec21238d31577f2b28a9d86807f06ca59d191d8440d0bb" "checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" "checksum blake2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "91721a6330935673395a0607df4d49a9cb90ae12d259f1b3e0a3f6e1d486872e" "checksum blake2b-rs 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "895ae726056ce2ab1b19854be889ca43a4a2c0b02c355bee90aa9e8ee6d0379b" diff --git a/core/Cargo.toml b/core/Cargo.toml index d5a7735dcb..825a386e7f 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -14,7 +14,7 @@ numext-fixed-uint = { version = "0.1", features = ["support_rand", "support_heap hash = {path = "../util/hash"} crypto = {path = "../util/crypto"} occupied-capacity = {path = "../util/occupied-capacity"} -bit-vec = "0.5.0" +bit-vec = "0.5.1" crossbeam-channel = "0.3" ckb-util = { path = "../util" } fnv = "1.0.3" From 7018ec2b62871123ad01ecf6ab24650623e2883a Mon Sep 17 00:00:00 2001 From: quake wang Date: Mon, 25 Mar 2019 13:59:43 +0900 Subject: [PATCH 83/99] refactor: resolve review comment issue --- Cargo.lock | 1 - chain/Cargo.toml | 1 - chain/src/chain.rs | 10 ++++------ chain/src/tests/util.rs | 4 +--- shared/src/cell_set.rs | 22 ++++++++++++++-------- shared/src/shared.rs | 2 +- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d6878be97a..1c21024d4c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -351,7 +351,6 @@ dependencies = [ "lru-cache 0.1.0 (git+https://github.com/nervosnetwork/lru-cache)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "stop-handler 0.7.0-pre", "tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/chain/Cargo.toml b/chain/Cargo.toml index f6ab75bd31..8126ab43f4 100644 --- a/chain/Cargo.toml +++ b/chain/Cargo.toml @@ -27,4 +27,3 @@ failure = "0.1.5" [dev-dependencies] env_logger = "0.6" tempfile = "3.0" -rand = "0.6" diff --git a/chain/src/chain.rs b/chain/src/chain.rs index 2fb65c351a..d91be481dc 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -505,13 +505,11 @@ impl ChainService { .insert_block_ext(batch, &b.header().hash(), ext); } - if found_error.is_some() { - Err(SharedError::InvalidTransaction( - found_error.unwrap().to_string(), - ))?; + if let Some(err) = found_error { + Err(SharedError::InvalidTransaction(err.to_string()))? + } else { + Ok(cell_set_diff) } - - Ok(cell_set_diff) } // TODO: beatify diff --git a/chain/src/tests/util.rs b/chain/src/tests/util.rs index 8fc7b4ac5b..14517b5134 100644 --- a/chain/src/tests/util.rs +++ b/chain/src/tests/util.rs @@ -15,7 +15,6 @@ use ckb_shared::store::ChainKVStore; use faketime::unix_time_as_millis; use numext_fixed_hash::H256; use numext_fixed_uint::U256; -use rand; use std::fs::File; use std::io::Read; use std::path::Path; @@ -63,8 +62,7 @@ pub(crate) fn gen_block( .timestamp(unix_time_as_millis()) .number(number) .difficulty(difficulty) - .cellbase_id(cellbase.hash()) - .nonce(rand::random()); + .cellbase_id(cellbase.hash()); BlockBuilder::default() .commit_transaction(cellbase) diff --git a/shared/src/cell_set.rs b/shared/src/cell_set.rs index f280fe329a..d42d1cf972 100644 --- a/shared/src/cell_set.rs +++ b/shared/src/cell_set.rs @@ -66,9 +66,8 @@ impl CellSet { self.inner.get(h) } - pub fn insert(&mut self, hash: &H256, outputs_len: usize) { - self.inner - .insert(hash.clone(), TransactionMeta::new(outputs_len)); + pub fn insert(&mut self, hash: H256, outputs_len: usize) { + self.inner.insert(hash, TransactionMeta::new(outputs_len)); } pub fn remove(&mut self, hash: &H256) -> Option { @@ -88,19 +87,26 @@ impl CellSet { } pub fn update(&mut self, diff: CellSetDiff) { - diff.old_outputs.iter().for_each(|h| { + let CellSetDiff { + old_inputs, + old_outputs, + new_inputs, + new_outputs, + } = diff; + + old_outputs.iter().for_each(|h| { self.remove(h); }); - diff.old_inputs.iter().for_each(|o| { + old_inputs.iter().for_each(|o| { self.mark_live(o); }); - diff.new_outputs.iter().for_each(|(hash, len)| { - self.insert(hash, *len); + new_outputs.into_iter().for_each(|(hash, len)| { + self.insert(hash, len); }); - diff.new_inputs.iter().for_each(|o| { + new_inputs.iter().for_each(|o| { self.mark_dead(o); }); } diff --git a/shared/src/shared.rs b/shared/src/shared.rs index e530c8f888..7942986dec 100644 --- a/shared/src/shared.rs +++ b/shared/src/shared.rs @@ -140,7 +140,7 @@ impl Shared { cell_set.mark_dead(&o); } - cell_set.insert(&tx.hash(), output_len); + cell_set.insert(tx.hash(), output_len); } } From 0b1beaae93e758bccfa51963db90d2b5ed5346cc Mon Sep 17 00:00:00 2001 From: jjy Date: Mon, 25 Mar 2019 14:45:41 +0800 Subject: [PATCH 84/99] refactor: unify network peer scoring --- network/src/behaviour.rs | 10 ++++ network/src/lib.rs | 4 ++ network/src/network.rs | 6 +- network/src/peer_store.rs | 32 +++++++--- network/src/peer_store/score.rs | 63 -------------------- network/src/peer_store/sqlite/peer_store.rs | 40 +++++-------- network/src/protocol_handler.rs | 26 +++++--- network/src/service/ckb_service.rs | 6 +- network/src/service/ping_service.rs | 8 +-- network/src/tests/peers_registry.rs | 19 +++--- network/src/tests/sqlite_peer_store.rs | 29 +++++---- sync/src/relayer/mod.rs | 4 +- sync/src/synchronizer/get_headers_process.rs | 7 ++- sync/src/synchronizer/mod.rs | 13 ++-- sync/src/tests/mod.rs | 8 ++- 15 files changed, 124 insertions(+), 151 deletions(-) create mode 100644 network/src/behaviour.rs delete mode 100644 network/src/peer_store/score.rs diff --git a/network/src/behaviour.rs b/network/src/behaviour.rs new file mode 100644 index 0000000000..0ad670fe5d --- /dev/null +++ b/network/src/behaviour.rs @@ -0,0 +1,10 @@ +use crate::Score; +pub type Behaviour = (Score, &'static str); + +pub const CONNECT: Behaviour = (10, "peer connected"); +pub const UNEXPECTED_DISCONNECT: Behaviour = (-10, "peer unexpected disconnected"); +pub const PING: Behaviour = (10, "peer ping"); +pub const FAILED_TO_PING: Behaviour = (-20, "failed to ping"); +pub const SYNC_USELESS: Behaviour = (-50, "sync useless"); +pub const UNEXPECTED_NETWORK_MESSAGE: Behaviour = (-50, "unexpected network message"); +pub const NETWORK_TIMEOUT: Behaviour = (-20, "network timeout"); diff --git a/network/src/lib.rs b/network/src/lib.rs index c35702eb82..fb9d06f605 100644 --- a/network/src/lib.rs +++ b/network/src/lib.rs @@ -1,5 +1,6 @@ #[macro_use] pub extern crate futures; +pub mod behaviour; pub mod errors; mod network; mod network_config; @@ -13,13 +14,16 @@ mod service; #[cfg(test)] mod tests; +pub use crate::behaviour::Behaviour; pub use crate::network::{Network, PeerInfo, SessionInfo}; pub use crate::network_config::NetworkConfig; pub use crate::network_service::NetworkService; +pub use crate::peer_store::Score; pub use crate::peers_registry::RegisterResult; pub use crate::protocol::{CKBProtocol, Event as CKBEvent, Version as ProtocolVersion}; pub use crate::protocol_handler::{CKBProtocolContext, CKBProtocolHandler, Severity}; pub use crate::service::timer_service::{Timer, TimerRegistry, TimerToken}; +pub use errors::Error; pub use p2p::{multiaddr, secio::PeerId, yamux::session::SessionType, ProtocolId}; // p2p internal expose pub(crate) use p2p::{ diff --git a/network/src/network.rs b/network/src/network.rs index de7863204f..8352d17fbe 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -1,5 +1,5 @@ use crate::errors::{ConfigError, Error, PeerError, ProtocolError}; -use crate::peer_store::{sqlite::SqlitePeerStore, Behaviour, PeerStore}; +use crate::peer_store::{sqlite::SqlitePeerStore, PeerStore}; use crate::peers_registry::{ ConnectionStatus, Peer, PeerIdentifyInfo, PeersRegistry, RegisterResult, Session, }; @@ -14,8 +14,8 @@ use crate::service::{ timer_service::{TimerRegistry, TimerService}, }; use crate::{ - CKBEvent, CKBProtocol, NetworkConfig, PeerIndex, ProtocolId, ServiceContext, ServiceControl, - SessionType, + Behaviour, CKBEvent, CKBProtocol, NetworkConfig, PeerIndex, ProtocolId, ServiceContext, + ServiceControl, SessionType, }; use bytes::Bytes; use ckb_util::{Mutex, RwLock}; diff --git a/network/src/peer_store.rs b/network/src/peer_store.rs index 54656dfad4..1175820a65 100644 --- a/network/src/peer_store.rs +++ b/network/src/peer_store.rs @@ -1,12 +1,30 @@ -mod score; pub mod sqlite; pub(crate) use crate::PeerId; pub use crate::{peer_store::sqlite::SqlitePeerStore, SessionType}; use p2p::multiaddr::Multiaddr; -pub use score::{Behaviour, Score, ScoringSchema}; use std::time::Duration; +pub type Score = i32; + +/// PeerStore Scoring configuration +#[derive(Copy, Clone, Debug)] +pub struct PeerScoreConfig { + pub default_score: Score, + pub ban_score: Score, + pub ban_timeout: Duration, +} + +impl Default for PeerScoreConfig { + fn default() -> Self { + PeerScoreConfig { + default_score: 100, + ban_score: 40, + ban_timeout: Duration::from_secs(24 * 3600), + } + } +} + /// PeerStore /// See [rfc0007](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0007-scoring-system-and-network-security/0007-scoring-system-and-network-security.md) for details. pub trait PeerStore: Send + Sync { @@ -17,7 +35,7 @@ pub trait PeerStore: Send + Sync { /// this method will assume peer and addr is untrust since we have not connected to it. fn add_discovered_addr(&mut self, peer_id: &PeerId, address: Multiaddr) -> bool; /// Report peer behaviours - fn report(&mut self, peer_id: &PeerId, behaviour: Behaviour) -> ReportResult; + fn report(&mut self, peer_id: &PeerId, behaviour: (Score, &str)) -> ReportResult; /// Update peer status fn update_status(&mut self, peer_id: &PeerId, status: Status); fn peer_status(&self, peer_id: &PeerId) -> Status; @@ -34,12 +52,10 @@ pub trait PeerStore: Send + Sync { fn random_peers(&self, count: u32) -> Vec<(PeerId, Multiaddr)>; /// Ban a peer fn ban_peer(&mut self, peer_id: &PeerId, timeout: Duration); + /// Check peer ban status fn is_banned(&self, peer_id: &PeerId) -> bool; - fn scoring_schema(&self) -> &ScoringSchema; - fn peer_score_or_default(&self, peer_id: &PeerId) -> Score { - self.peer_score(peer_id) - .unwrap_or_else(|| self.scoring_schema().peer_init_score()) - } + /// peer score config + fn peer_score_config(&self) -> PeerScoreConfig; } /// Peer Status diff --git a/network/src/peer_store/score.rs b/network/src/peer_store/score.rs deleted file mode 100644 index f2779f7667..0000000000 --- a/network/src/peer_store/score.rs +++ /dev/null @@ -1,63 +0,0 @@ -use fnv::FnvHashMap; -use std::time::Duration; -/// Peer Behaviours -#[allow(dead_code)] -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] -pub enum Behaviour { - FailedToConnect, - FailedToPing, - Ping, - Connect, - UnexpectedDisconnect, -} - -pub type Score = i32; - -pub struct ScoringSchema { - schema: FnvHashMap, - peer_init_score: Score, - ban_score: Score, - default_ban_timeout: Duration, -} - -impl ScoringSchema { - pub fn new_default() -> Self { - let schema = vec![ - (Behaviour::FailedToConnect, -20), - (Behaviour::FailedToPing, -10), - (Behaviour::Ping, 5), - (Behaviour::Connect, 10), - (Behaviour::UnexpectedDisconnect, -20), - ] - .into_iter() - .collect(); - ScoringSchema { - schema, - peer_init_score: 100, - ban_score: 40, - default_ban_timeout: Duration::from_secs(24 * 3600), - } - } - - pub fn peer_init_score(&self) -> Score { - self.peer_init_score - } - - pub fn ban_score(&self) -> Score { - self.ban_score - } - - pub fn get_score(&self, behaviour: Behaviour) -> Option { - self.schema.get(&behaviour).cloned() - } - - pub fn default_ban_timeout(&self) -> Duration { - self.default_ban_timeout - } -} - -impl Default for ScoringSchema { - fn default() -> Self { - ScoringSchema::new_default() - } -} diff --git a/network/src/peer_store/sqlite/peer_store.rs b/network/src/peer_store/sqlite/peer_store.rs index 210028daba..3138fd6388 100644 --- a/network/src/peer_store/sqlite/peer_store.rs +++ b/network/src/peer_store/sqlite/peer_store.rs @@ -12,12 +12,11 @@ use crate::peer_store::sqlite::{self, db, ConnectionPool, ConnectionPoolExt, DBE /// score. /// 4. Good peers can get higher score than bad peers. use crate::peer_store::{ - Behaviour, Multiaddr, PeerId, PeerStore, ReportResult, Score, ScoringSchema, Status, + Multiaddr, PeerId, PeerScoreConfig, PeerStore, ReportResult, Score, Status, }; use crate::SessionType; use faketime::unix_time; use fnv::FnvHashMap; -use log::debug; use std::net::IpAddr; use std::time::Duration; @@ -28,18 +27,18 @@ const DEFAULT_POOL_SIZE: u32 = 16; pub struct SqlitePeerStore { bootnodes: Vec<(PeerId, Multiaddr)>, - schema: ScoringSchema, + peer_score_config: PeerScoreConfig, ban_list: FnvHashMap, Duration>, pub(crate) pool: ConnectionPool, } impl SqlitePeerStore { - pub fn new(connection_pool: ConnectionPool) -> Self { + pub fn new(connection_pool: ConnectionPool, peer_score_config: PeerScoreConfig) -> Self { let mut peer_store = SqlitePeerStore { bootnodes: Vec::new(), - schema: Default::default(), ban_list: Default::default(), pool: connection_pool, + peer_score_config, }; peer_store.prepare().expect("prepare tables"); peer_store @@ -47,12 +46,12 @@ impl SqlitePeerStore { pub fn file(path: String) -> Result { let pool = sqlite::open_pool(sqlite::StorePath::File(path), DEFAULT_POOL_SIZE)?; - Ok(SqlitePeerStore::new(pool)) + Ok(SqlitePeerStore::new(pool, PeerScoreConfig::default())) } pub fn memory(db: String) -> Result { let pool = sqlite::open_pool(sqlite::StorePath::Memory(db), DEFAULT_POOL_SIZE)?; - Ok(SqlitePeerStore::new(pool)) + Ok(SqlitePeerStore::new(pool, PeerScoreConfig::default())) } #[allow(dead_code)] @@ -146,7 +145,7 @@ impl SqlitePeerStore { None => return Err(()), }; - if candidate_peer.score >= self.schema.peer_init_score() { + if candidate_peer.score >= self.peer_score_config.default_score { return Err(()); } self.pool @@ -184,7 +183,7 @@ impl SqlitePeerStore { peer_id, &addr, endpoint, - self.scoring_schema().peer_init_score(), + self.peer_score_config.default_score, last_connected_at, )?; db::PeerInfo::get_by_peer_id(conn, &peer_id) @@ -208,7 +207,7 @@ impl SqlitePeerStore { peer_id, &addr, SessionType::Server, - self.scoring_schema().peer_init_score(), + self.peer_score_config.default_score, now, )?; db::PeerInfo::get_by_peer_id(conn, &peer_id) @@ -249,22 +248,14 @@ impl PeerStore for SqlitePeerStore { inserted > 0 } - fn report(&mut self, peer_id: &PeerId, behaviour: Behaviour) -> ReportResult { + fn report(&mut self, peer_id: &PeerId, (score_offset, _reason): (Score, &str)) -> ReportResult { if self.is_banned(peer_id) { return ReportResult::Banned; } - let behaviour_score = match self.schema.get_score(behaviour) { - Some(score) => score, - None => { - debug!(target: "network", "behaviour {:?} is undefined", behaviour); - return ReportResult::Ok; - } - }; let peer = self.get_or_insert_peer_info(peer_id); - let score = peer.score.saturating_add(behaviour_score); - if score < self.schema.ban_score() { - let ban_timeout = self.schema.default_ban_timeout(); - self.ban_peer(peer_id, ban_timeout); + let score = peer.score.saturating_add(score_offset); + if score < self.peer_score_config.ban_score { + self.ban_peer(peer_id, self.peer_score_config.ban_timeout); return ReportResult::Banned; } self.pool @@ -343,8 +334,7 @@ impl PeerStore for SqlitePeerStore { } false } - - fn scoring_schema(&self) -> &ScoringSchema { - &self.schema + fn peer_score_config(&self) -> PeerScoreConfig { + self.peer_score_config } } diff --git a/network/src/protocol_handler.rs b/network/src/protocol_handler.rs index 61ca5aca1b..6a5a5f65df 100644 --- a/network/src/protocol_handler.rs +++ b/network/src/protocol_handler.rs @@ -1,9 +1,8 @@ use crate::errors::{Error, PeerError, ProtocolError}; -use crate::{Network, PeerIndex, ProtocolId, SessionInfo, TimerRegistry, TimerToken}; +use crate::{Behaviour, Network, PeerIndex, ProtocolId, SessionInfo, TimerRegistry, TimerToken}; use bytes::Bytes; use ckb_util::Mutex; use log::debug; -use log::info; use std::sync::Arc; use std::time::Duration; @@ -23,7 +22,7 @@ pub trait CKBProtocolContext: Send { data: Vec, ) -> Result<(), Error>; // TODO combinate this interface with peer score - fn report_peer(&self, peer_index: PeerIndex, reason: Severity); + fn report_peer(&self, peer_index: PeerIndex, behaviour: Behaviour) -> Result<(), Error>; fn ban_peer(&self, peer_index: PeerIndex, timeout: Duration); fn disconnect(&self, peer_index: PeerIndex); fn register_timer(&self, token: TimerToken, delay: Duration) -> Result<(), Error>; @@ -83,11 +82,24 @@ impl CKBProtocolContext for DefaultCKBProtocolContext { } } // report peer behaviour - fn report_peer(&self, peer_index: PeerIndex, reason: Severity) { - // TODO combinate this interface with peer score - info!(target: "network", "report peer {} reason: {:?}", peer_index, reason); - self.disconnect(peer_index); + fn report_peer(&self, peer_index: PeerIndex, behaviour: Behaviour) -> Result<(), Error> { + debug!(target: "network", "report peer {} behaviour: {:?}", peer_index, behaviour); + if let Some(peer_id) = self.network.get_peer_id(peer_index) { + if self + .network + .peer_store() + .write() + .report(&peer_id, behaviour) + .is_banned() + { + self.disconnect(peer_index); + } + Ok(()) + } else { + Err(Error::Peer(PeerError::IndexNotFound(peer_index))) + } } + // ban peer fn ban_peer(&self, peer_index: PeerIndex, timeout: Duration) { if let Some(peer_id) = self.network.get_peer_id(peer_index) { diff --git a/network/src/service/ckb_service.rs b/network/src/service/ckb_service.rs index 12b596523a..e97df2219f 100644 --- a/network/src/service/ckb_service.rs +++ b/network/src/service/ckb_service.rs @@ -1,5 +1,5 @@ -use crate::peer_store::{Behaviour, Status}; use crate::protocol_handler::DefaultCKBProtocolContext; +use crate::{behaviour, peer_store::Status}; use crate::{peers_registry::RegisterResult, CKBEvent, CKBProtocolHandler, Network, PeerId}; use futures::{sync::mpsc::Receiver, Async, Stream}; use log::{debug, error, info}; @@ -49,7 +49,7 @@ impl Stream for CKBService { // update status in peer_store if let RegisterResult::New(_) = register_result { let mut peer_store = network.peer_store().write(); - peer_store.report(&peer_id, Behaviour::Connect); + peer_store.report(&peer_id, behaviour::CONNECT); peer_store.update_status(&peer_id, Status::Connected); let _ = peer_store.add_discovered_addr(&peer_id, addr); } @@ -78,7 +78,7 @@ impl Stream for CKBService { // update disconnect in peer_store { let mut peer_store = network.peer_store().write(); - peer_store.report(&peer_id, Behaviour::UnexpectedDisconnect); + peer_store.report(&peer_id, behaviour::UNEXPECTED_DISCONNECT); peer_store.update_status(&peer_id, Status::Disconnected); } if let Some(peer_index) = network.get_peer_index(&peer_id) { diff --git a/network/src/service/ping_service.rs b/network/src/service/ping_service.rs index 663b3df390..4acee85181 100644 --- a/network/src/service/ping_service.rs +++ b/network/src/service/ping_service.rs @@ -1,4 +1,4 @@ -use crate::peer_store::Behaviour; +use crate::behaviour; use crate::Network; use futures::{sync::mpsc::Receiver, Async, Stream}; use log::{debug, trace}; @@ -27,16 +27,16 @@ impl Stream for PingService { peer.ping = Some(duration); peer.last_ping_time = Some(Instant::now()); }); - self.network.report(&peer_id, Behaviour::Ping); + self.network.report(&peer_id, behaviour::PING); } Some(Timeout(peer_id)) => { debug!(target: "network", "timeout to ping {:?}", peer_id); - self.network.report(&peer_id, Behaviour::FailedToPing); + self.network.report(&peer_id, behaviour::FAILED_TO_PING); self.network.drop_peer(&peer_id); } Some(UnexpectedError(peer_id)) => { debug!(target: "network", "failed to ping {:?}", peer_id); - self.network.report(&peer_id, Behaviour::FailedToPing); + self.network.report(&peer_id, behaviour::FAILED_TO_PING); self.network.drop_peer(&peer_id); } None => { diff --git a/network/src/tests/peers_registry.rs b/network/src/tests/peers_registry.rs index 402a9a49b6..91e18a0c4b 100644 --- a/network/src/tests/peers_registry.rs +++ b/network/src/tests/peers_registry.rs @@ -1,6 +1,7 @@ use crate::{ + behaviour, multiaddr::ToMultiaddr, - peer_store::{Behaviour, PeerStore, SqlitePeerStore}, + peer_store::{PeerStore, SqlitePeerStore}, peers_registry::{PeersRegistry, Session, EVICTION_PROTECT_PEERS}, PeerId, SessionType, }; @@ -119,8 +120,8 @@ fn test_accept_inbound_peer_eviction() { let mut peer_store = peer_store.write(); for _ in 0..EVICTION_PROTECT_PEERS { let peer_id = peers_iter.next().unwrap(); - peer_store.report(&peer_id, Behaviour::Ping); - peer_store.report(&peer_id, Behaviour::Ping); + peer_store.report(&peer_id, behaviour::PING); + peer_store.report(&peer_id, behaviour::PING); } } // lowest ping peers @@ -168,12 +169,12 @@ fn test_accept_inbound_peer_eviction() { // setup score { let mut peer_store = peer_store.write(); - peer_store.report(&lowest_score_peer, Behaviour::FailedToPing); - peer_store.report(&lowest_score_peer, Behaviour::FailedToPing); - peer_store.report(&lowest_score_peer, Behaviour::FailedToPing); - peer_store.report(&reserved_peer, Behaviour::FailedToPing); - peer_store.report(&reserved_peer, Behaviour::FailedToPing); - peer_store.report(&evict_target, Behaviour::FailedToPing); + peer_store.report(&lowest_score_peer, behaviour::FAILED_TO_PING); + peer_store.report(&lowest_score_peer, behaviour::FAILED_TO_PING); + peer_store.report(&lowest_score_peer, behaviour::FAILED_TO_PING); + peer_store.report(&reserved_peer, behaviour::FAILED_TO_PING); + peer_store.report(&reserved_peer, behaviour::FAILED_TO_PING); + peer_store.report(&evict_target, behaviour::FAILED_TO_PING); } // make sure other peers should not protected by longest connection time rule new_peer_ids.extend_from_slice(&[ diff --git a/network/src/tests/sqlite_peer_store.rs b/network/src/tests/sqlite_peer_store.rs index 857c0955ec..0a8087b9d1 100644 --- a/network/src/tests/sqlite_peer_store.rs +++ b/network/src/tests/sqlite_peer_store.rs @@ -1,10 +1,11 @@ use crate::peer_store::sqlite::ConnectionPoolExt; use crate::{ + behaviour, multiaddr::ToMultiaddr, peer_store::{ sqlite::db, sqlite::peer_store::{PEER_NOT_SEEN_TIMEOUT_SECS, PEER_STORE_LIMIT}, - Behaviour, PeerStore, SqlitePeerStore, Status, + PeerStore, SqlitePeerStore, Status, }, PeerId, SessionType, }; @@ -24,8 +25,8 @@ fn test_add_connected_peer() { let addr = "/ip4/127.0.0.1".to_multiaddr().unwrap(); peer_store.add_connected_peer(&peer_id, addr, SessionType::Client); assert_eq!( - peer_store.peer_score(&peer_id).unwrap(), - peer_store.scoring_schema().peer_init_score() + peer_store.peer_score(&peer_id), + Some(peer_store.peer_score_config().default_score) ); assert_eq!(peer_store.peer_addrs(&peer_id, 1).unwrap().len(), 0); } @@ -42,9 +43,10 @@ fn test_add_discovered_addr() { fn test_report() { let mut peer_store: Box = Box::new(new_peer_store()); let peer_id = PeerId::random(); - assert!(peer_store.report(&peer_id, Behaviour::Ping).is_ok()); + assert!(peer_store.report(&peer_id, behaviour::PING).is_ok()); assert!( - peer_store.peer_score_or_default(&peer_id) > peer_store.scoring_schema().peer_init_score() + peer_store.peer_score(&peer_id).expect("peer score") + > peer_store.peer_score_config().default_score ); } @@ -134,7 +136,7 @@ fn test_delete_peer_info() { &PeerId::random(), &addr1, SessionType::Server, - peer_store.scoring_schema().peer_init_score(), + peer_store.peer_score_config().default_score, now, )?; } @@ -153,19 +155,16 @@ fn test_delete_peer_info() { peer_store.add_connected_peer(&evict_target, addr1.clone(), SessionType::Server); peer_store.add_connected_peer(&fake_target, addr2, SessionType::Server); } - peer_store.report(&evict_target, Behaviour::FailedToPing); - peer_store.report(&fake_target, Behaviour::FailedToPing); - peer_store.report(&fake_target, Behaviour::FailedToPing); + peer_store.report(&evict_target, behaviour::FAILED_TO_PING); + peer_store.report(&fake_target, behaviour::FAILED_TO_PING); + peer_store.report(&fake_target, behaviour::FAILED_TO_PING); // evict_target has lower score than init score assert!( - peer_store.peer_score_or_default(&evict_target) - < peer_store.scoring_schema().peer_init_score() + peer_store.peer_score(&evict_target).expect("peer store") + < peer_store.peer_score_config().default_score ); // should evict evict_target and accept this peer_store.add_connected_peer(&PeerId::random(), addr1, SessionType::Server); // evict_target is evicted in previous step - assert_eq!( - peer_store.peer_score_or_default(&evict_target), - peer_store.scoring_schema().peer_init_score() - ); + assert_eq!(peer_store.peer_score(&evict_target), None); } diff --git a/sync/src/relayer/mod.rs b/sync/src/relayer/mod.rs index f2baf4d459..bc5c108e9f 100644 --- a/sync/src/relayer/mod.rs +++ b/sync/src/relayer/mod.rs @@ -21,7 +21,7 @@ use bytes::Bytes; use ckb_chain::chain::ChainController; use ckb_core::block::{Block, BlockBuilder}; use ckb_core::transaction::{ProposalShortId, Transaction}; -use ckb_network::{CKBProtocolContext, CKBProtocolHandler, PeerIndex, Severity, TimerToken}; +use ckb_network::{behaviour, CKBProtocolContext, CKBProtocolHandler, PeerIndex, TimerToken}; use ckb_protocol::{ cast, short_transaction_id, short_transaction_id_keys, RelayMessage, RelayPayload, }; @@ -128,7 +128,7 @@ where fn process(&self, nc: &CKBProtocolContext, peer: PeerIndex, message: RelayMessage) { if self.try_process(nc, peer, message).is_err() { - nc.report_peer(peer, Severity::Bad("Malformed RelayMessage")); + let _ = nc.report_peer(peer, behaviour::UNEXPECTED_NETWORK_MESSAGE); } } diff --git a/sync/src/synchronizer/get_headers_process.rs b/sync/src/synchronizer/get_headers_process.rs index e6af618e53..ae0e548a85 100644 --- a/sync/src/synchronizer/get_headers_process.rs +++ b/sync/src/synchronizer/get_headers_process.rs @@ -1,7 +1,7 @@ use crate::synchronizer::Synchronizer; use crate::MAX_LOCATOR_SIZE; use ckb_core::header::Header; -use ckb_network::{CKBProtocolContext, PeerIndex, Severity}; +use ckb_network::{behaviour, CKBProtocolContext, PeerIndex}; use ckb_protocol::{cast, GetHeaders, SyncMessage}; use ckb_shared::index::ChainIndex; use ckb_util::TryInto; @@ -76,8 +76,9 @@ where warn!(target: "sync", "\n\nunknown block headers from peer {} {:#?}\n\n", self.peer, block_locator_hashes); // Got 'headers' message without known blocks // ban or close peers - self.nc - .report_peer(self.peer, Severity::Bad("without common headers")); + let _ = self.nc.report_peer(self.peer, behaviour::SYNC_USELESS); + // disconnect peer anyway + self.nc.disconnect(self.peer); } Ok(()) } diff --git a/sync/src/synchronizer/mod.rs b/sync/src/synchronizer/mod.rs index 497bc2c9ee..8f28aa2081 100644 --- a/sync/src/synchronizer/mod.rs +++ b/sync/src/synchronizer/mod.rs @@ -26,7 +26,7 @@ use ckb_chain::chain::ChainController; use ckb_chain_spec::consensus::Consensus; use ckb_core::block::Block; use ckb_core::header::{BlockNumber, Header}; -use ckb_network::{CKBProtocolContext, CKBProtocolHandler, PeerIndex, Severity, TimerToken}; +use ckb_network::{behaviour, CKBProtocolContext, CKBProtocolHandler, PeerIndex, TimerToken}; use ckb_protocol::{cast, SyncMessage, SyncPayload}; use ckb_shared::index::ChainIndex; use ckb_shared::shared::Shared; @@ -182,7 +182,7 @@ impl Synchronizer { fn process(&self, nc: &CKBProtocolContext, peer: PeerIndex, message: SyncMessage) { if self.try_process(nc, peer, message).is_err() { - nc.report_peer(peer, Severity::Bad("Malformed SyncMessage")); + let _ = nc.report_peer(peer, behaviour::UNEXPECTED_NETWORK_MESSAGE); } } @@ -622,7 +622,7 @@ impl Synchronizer { } for peer in eviction { warn!(target: "sync", "timeout eviction peer={}", peer); - nc.report_peer(peer, Severity::Timeout); + let _ = nc.report_peer(peer, behaviour::NETWORK_TIMEOUT); } } @@ -762,8 +762,8 @@ mod tests { use ckb_core::transaction::{CellInput, CellOutput, Transaction, TransactionBuilder}; use ckb_db::memorydb::MemoryKeyValueDB; use ckb_network::{ - errors::Error as NetworkError, multiaddr::ToMultiaddr, CKBProtocolContext, PeerId, - PeerIndex, PeerInfo, ProtocolId, SessionInfo, SessionType, Severity, TimerToken, + multiaddr::ToMultiaddr, Behaviour, CKBProtocolContext, Error as NetworkError, PeerId, + PeerIndex, PeerInfo, ProtocolId, SessionInfo, SessionType, TimerToken, }; use ckb_notify::{NotifyController, NotifyService}; use ckb_protocol::{Block as FbsBlock, Headers as FbsHeaders}; @@ -1117,8 +1117,9 @@ mod tests { Ok(()) } /// Report peer. Depending on the report, peer may be disconnected and possibly banned. - fn report_peer(&self, peer: PeerIndex, _reason: Severity) { + fn report_peer(&self, peer: PeerIndex, _behaviour: Behaviour) -> Result<(), NetworkError> { self.disconnected.lock().insert(peer); + Ok(()) } fn ban_peer(&self, _peer: PeerIndex, _duration: Duration) {} diff --git a/sync/src/tests/mod.rs b/sync/src/tests/mod.rs index 32a2c8ff2e..7c830d4a2e 100644 --- a/sync/src/tests/mod.rs +++ b/sync/src/tests/mod.rs @@ -1,7 +1,7 @@ use bytes::Bytes; use ckb_network::{ - errors::Error as NetworkError, CKBProtocolContext, CKBProtocolHandler, PeerIndex, ProtocolId, - SessionInfo, Severity, TimerToken, + errors::Error as NetworkError, Behaviour, CKBProtocolContext, CKBProtocolHandler, PeerIndex, + ProtocolId, SessionInfo, TimerToken, }; use std::collections::HashMap; use std::sync::mpsc::{channel, Receiver, Sender}; @@ -163,7 +163,9 @@ impl CKBProtocolContext for TestNetworkContext { Ok(()) } - fn report_peer(&self, _peer: PeerIndex, _reason: Severity) {} + fn report_peer(&self, _peer: PeerIndex, _behaviour: Behaviour) -> Result<(), NetworkError> { + Ok(()) + } fn register_timer(&self, token: TimerToken, delay: Duration) -> Result<(), NetworkError> { if let Some(sender) = self.timer_senders.get(&(self.protocol, token)) { From 9fbd1839f00f4d27790d15886e153c627f32bd81 Mon Sep 17 00:00:00 2001 From: zhangsoledad <787953403@qq.com> Date: Mon, 25 Mar 2019 15:05:54 +0800 Subject: [PATCH 85/99] chore: bump several deps rocksdb 0.11.0 -> 0.12.0 tokio-sync 0.1.3 -> 0.1.4 proptest 0.9.1 -> 0.9.2 --- Cargo.lock | 320 +++++++++++++++++++++++--------------------------- db/Cargo.toml | 2 +- 2 files changed, 150 insertions(+), 172 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1bb8b25fef..cd2dccbe48 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,7 +5,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "aes-soft 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "aesni 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", - "ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ctr 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "stream-cipher 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -31,10 +31,10 @@ dependencies = [ [[package]] name = "aho-corasick" -version = "0.6.9" +version = "0.6.10" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "memchr 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -68,7 +68,7 @@ name = "atty" version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -80,13 +80,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "backtrace" -version = "0.3.13" +version = "0.3.14" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "backtrace-sys 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)", - "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-demangle 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -97,7 +97,7 @@ version = "0.1.28" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cc 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -121,12 +121,12 @@ dependencies = [ [[package]] name = "bindgen" -version = "0.47.1" +version = "0.47.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", - "cexpr 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", - "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "cexpr 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "clang-sys 0.26.4 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -142,7 +142,7 @@ dependencies = [ [[package]] name = "bit-set" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bit-vec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -255,15 +255,15 @@ dependencies = [ [[package]] name = "cexpr" -version = "0.3.4" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "nom 4.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "cfg-if" -version = "0.1.6" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -401,7 +401,7 @@ dependencies = [ "ckb-util 0.7.0-pre", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", - "rocksdb 0.11.0 (git+https://github.com/rust-rocksdb/rust-rocksdb?rev=1038c71df85a58b7f48894ca4cbf7c9228cb1cdf)", + "rocksdb 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", @@ -454,7 +454,7 @@ dependencies = [ "lru-cache 0.1.0 (git+https://github.com/nervosnetwork/lru-cache)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "proptest 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "proptest 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", @@ -513,7 +513,7 @@ dependencies = [ "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "hash 0.7.0-pre", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "proptest 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "proptest 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", @@ -584,7 +584,7 @@ dependencies = [ "hash 0.7.0-pre", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "proptest 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "proptest 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -721,7 +721,7 @@ version = "0.26.4" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "libloading 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -746,7 +746,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -764,13 +764,13 @@ version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "nom 4.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "rust-ini 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde-hjson 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", - "yaml-rust 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "yaml-rust 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -782,7 +782,7 @@ dependencies = [ "clicolors-control 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "encode_unicode 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "termios 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -804,7 +804,7 @@ dependencies = [ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_os 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "rand_xoshiro 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", @@ -830,7 +830,7 @@ version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", - "smallvec 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -857,7 +857,7 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "arrayvec 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", - "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "crossbeam-utils 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -871,7 +871,7 @@ version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "arrayvec 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", - "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -891,7 +891,7 @@ name = "crossbeam-utils" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -899,7 +899,7 @@ name = "crossbeam-utils" version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -949,12 +949,12 @@ name = "csv-core" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "memchr 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "ctr" -version = "0.3.0" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "block-cipher-trait 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1009,7 +1009,7 @@ version = "0.7.0-pre" [[package]] name = "either" -version = "1.5.0" +version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -1039,7 +1039,7 @@ name = "error-chain" version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "backtrace 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)", + "backtrace 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1047,7 +1047,7 @@ name = "failure" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "backtrace 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)", + "backtrace 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1058,7 +1058,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)", "synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1090,7 +1090,7 @@ name = "flatbuffers" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "smallvec 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1163,10 +1163,10 @@ name = "globset" version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", + "aho-corasick 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "memchr 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1182,14 +1182,14 @@ dependencies = [ [[package]] name = "h2" -version = "0.1.16" +version = "0.1.17" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "http 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", + "http 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", "indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1234,7 +1234,7 @@ dependencies = [ [[package]] name = "http" -version = "0.1.15" +version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1277,7 +1277,7 @@ dependencies = [ "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-proto 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-service 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "unicase 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unicase 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "want 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1289,8 +1289,8 @@ dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "futures-cpupool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", - "h2 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", - "http 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", + "h2 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", + "http 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", "httparse 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1340,7 +1340,7 @@ name = "iovec" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1349,7 +1349,7 @@ name = "itertools" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "either 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1414,7 +1414,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1428,7 +1428,7 @@ dependencies = [ "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", - "unicase 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unicase 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1444,7 +1444,7 @@ dependencies = [ "num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "unicase 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unicase 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1456,7 +1456,7 @@ dependencies = [ "jsonrpc-core 10.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "proptest 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "proptest 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1485,9 +1485,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "lazy_static" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "spin 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", -] [[package]] name = "lazycell" @@ -1496,7 +1493,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "libc" -version = "0.2.48" +version = "0.2.50" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -1511,12 +1508,12 @@ dependencies = [ [[package]] name = "librocksdb-sys" version = "5.17.2" -source = "git+https://github.com/rust-rocksdb/rust-rocksdb?rev=1038c71df85a58b7f48894ca4cbf7c9228cb1cdf#1038c71df85a58b7f48894ca4cbf7c9228cb1cdf" +source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "bindgen 0.47.1 (registry+https://github.com/rust-lang/crates.io-index)", + "bindgen 0.47.3 (registry+https://github.com/rust-lang/crates.io-index)", "cc 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1538,11 +1535,6 @@ dependencies = [ "serde_test 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "linked-hash-map" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "linked-hash-map" version = "0.5.1" @@ -1550,7 +1542,7 @@ source = "git+https://github.com/nervosnetwork/linked-hash-map?rev=df27f21#df27f [[package]] name = "linked-hash-map" -version = "0.5.1" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -1575,7 +1567,7 @@ name = "log" version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1600,15 +1592,15 @@ version = "0.1.0" source = "git+https://github.com/nervosnetwork/lru-cache#6e852fc0ac52523979630d5b35dea25bb158564b" dependencies = [ "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", - "linked-hash-map 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "lru-cache" -version = "0.1.1" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "linked-hash-map 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1618,11 +1610,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "memchr" -version = "2.1.3" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1640,7 +1631,7 @@ name = "mime" version = "0.3.13" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "unicase 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unicase 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1653,7 +1644,7 @@ dependencies = [ "iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazycell 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1667,7 +1658,7 @@ version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1687,8 +1678,8 @@ name = "net2" version = "0.2.33" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1704,8 +1695,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "cc 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", - "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1716,10 +1707,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "nom" -version = "4.2.0" +version = "4.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "memchr 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1749,7 +1740,7 @@ name = "num_cpus" version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1767,7 +1758,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1810,7 +1801,7 @@ version = "0.7.0-pre" dependencies = [ "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1867,10 +1858,10 @@ name = "parking_lot_core" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", - "smallvec 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1917,10 +1908,10 @@ dependencies = [ [[package]] name = "proptest" -version = "0.9.1" +version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "bit-set 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bit-set 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1985,7 +1976,7 @@ name = "rand" version = "0.3.23" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1995,7 +1986,7 @@ version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2008,7 +1999,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2019,14 +2010,14 @@ version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "rand_chacha 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand_hc 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand_isaac 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand_jitter 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_os 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_pcg 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_pcg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2074,19 +2065,19 @@ name = "rand_jitter" version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "rand_os" -version = "0.1.2" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2094,11 +2085,11 @@ dependencies = [ [[package]] name = "rand_pcg" -version = "0.1.1" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2124,7 +2115,7 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "crossbeam-deque 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "either 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "rayon-core 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2135,7 +2126,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "crossbeam-deque 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2165,8 +2156,8 @@ name = "regex" version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", - "memchr 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "aho-corasick 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex-syntax 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2203,7 +2194,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cc 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "spin 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "untrusted 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2211,11 +2202,11 @@ dependencies = [ [[package]] name = "rocksdb" -version = "0.11.0" -source = "git+https://github.com/rust-rocksdb/rust-rocksdb?rev=1038c71df85a58b7f48894ca4cbf7c9228cb1cdf#1038c71df85a58b7f48894ca4cbf7c9228cb1cdf" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", - "librocksdb-sys 5.17.2 (git+https://github.com/rust-rocksdb/rust-rocksdb?rev=1038c71df85a58b7f48894ca4cbf7c9228cb1cdf)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", + "librocksdb-sys 5.17.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2225,7 +2216,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "libsqlite3-sys 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)", - "lru-cache 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "lru-cache 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2310,7 +2301,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2371,7 +2362,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2430,11 +2421,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "smallvec" -version = "0.6.8" +version = "0.6.9" source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", -] [[package]] name = "snap" @@ -2450,8 +2438,8 @@ name = "socket2" version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2511,7 +2499,7 @@ dependencies = [ [[package]] name = "syn" -version = "0.15.26" +version = "0.15.29" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2534,7 +2522,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2548,8 +2536,8 @@ name = "tempfile" version = "3.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)", "remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2624,7 +2612,7 @@ dependencies = [ "aes-ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "bs58 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ctr 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "hmac 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2652,7 +2640,7 @@ name = "termion" version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)", "redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2662,7 +2650,7 @@ name = "termios" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2686,7 +2674,7 @@ name = "time" version = "0.1.42" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2723,7 +2711,7 @@ dependencies = [ "tokio-fs 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-reactor 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-sync 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-sync 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-tcp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-threadpool 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-timer 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2830,7 +2818,7 @@ dependencies = [ "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-executor 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-sync 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-sync 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2843,7 +2831,7 @@ dependencies = [ [[package]] name = "tokio-sync" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2920,7 +2908,7 @@ dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)", "mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2990,7 +2978,7 @@ dependencies = [ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "smallvec 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", "socket2 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-executor 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3033,7 +3021,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "unicase" -version = "2.2.0" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3052,7 +3040,7 @@ name = "unicode-normalization" version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "smallvec 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -3070,14 +3058,6 @@ name = "unicode-xid" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "unreachable" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "unsigned-varint" version = "0.2.2" @@ -3137,7 +3117,7 @@ name = "wait-timeout" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -3176,7 +3156,7 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -3236,29 +3216,29 @@ dependencies = [ [[package]] name = "yaml-rust" -version = "0.4.2" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "linked-hash-map 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [metadata] "checksum aes-ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d2e5b0458ea3beae0d1d8c0f3946564f8e10f90646cf78c06b4351052058d1ee" "checksum aes-soft 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cfd7e7ae3f9a1fb5c03b389fc6bb9a51400d0c13053f0dca698c832bfd893a0d" "checksum aesni 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2f70a6b5f971e473091ab7cfb5ffac6cde81666c4556751d8d5620ead8abf100" -"checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e" +"checksum aho-corasick 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "81ce3d38065e618af2d7b77e10c5ad9a069859b4be3c2250f674af3840d9c8a5" "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" "checksum antidote 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "34fde25430d87a9388dadbe6e34d7f72a462c8b43ac8d309b42b0a8505d7e2a5" "checksum arrayref 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0d382e583f07208808f6b1249e60848879ba3543f57c32277bf52d69c2f0f0ee" "checksum arrayvec 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "92c7fb76bc8826a8b33b4ee5bb07a247a81e76764ab4d55e8f73e3a4d8808c71" "checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652" "checksum autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a6d640bee2da49f60a4068a7fae53acde8982514ab7bae8b8cea9e88cbcfd799" -"checksum backtrace 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)" = "b5b493b66e03090ebc4343eb02f94ff944e0cbc9ac6571491d170ba026741eb5" +"checksum backtrace 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "cd5a90e2b463010cd0e0ce9a11d4a9d5d58d9f41d4a6ba3dcaf9e68b466e88b4" "checksum backtrace-sys 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)" = "797c830ac25ccc92a7f8a7b9862bde440715531514594a6154e3d4a54dd769b6" "checksum base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "489d6c0ed21b11d038c31b6ceccca973e65d73ba3bd8ecb9a2babf5546164643" "checksum bincode 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3efe0b4c8eaeed8600549c29f538a6a11bf422858d0ed435b1d70ec4ab101190" -"checksum bindgen 0.47.1 (registry+https://github.com/rust-lang/crates.io-index)" = "91f5aa1a54c5997396b59cfac31af9c1cbcaa988958c4ce84364257b2cadfad0" -"checksum bit-set 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6f1efcc46c18245a69c38fcc5cc650f16d3a59d034f3106e9ed63748f695730a" +"checksum bindgen 0.47.3 (registry+https://github.com/rust-lang/crates.io-index)" = "df683a55b54b41d5ea8ebfaebb5aa7e6b84e3f3006a78f010dadc9ca88469260" +"checksum bit-set 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e84c238982c4b1e1ee668d136c510c67a13465279c0cb367ea6baf6310620a80" "checksum bit-vec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f59bbe95d4e52a6398ec21238d31577f2b28a9d86807f06ca59d191d8440d0bb" "checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" "checksum blake2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "91721a6330935673395a0607df4d49a9cb90ae12d259f1b3e0a3f6e1d486872e" @@ -3273,8 +3253,8 @@ dependencies = [ "checksum bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c" "checksum cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "926013f2860c46252efceabb19f4a6b308197505082c609025aa6706c011d427" "checksum cc 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)" = "389803e36973d242e7fecb092b2de44a3d35ac62524b3b9339e51d577d668e02" -"checksum cexpr 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "644d693ecfa91955ed32dcc7eda4914e1be97a641fb6f0645a37348e20b230da" -"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" +"checksum cexpr 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a7fa24eb00d5ffab90eaeaf1092ac85c04c64aaf358ea6f84505b8116d24c6af" +"checksum cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "11d43355396e872eefb45ce6342e4374ed7bc2b3a502d1b28e36d6e23c05d1f4" "checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878" "checksum ckb-vm 0.1.0 (git+https://github.com/nervosnetwork/ckb-vm?rev=dd90ed1)" = "" "checksum clang-sys 0.26.4 (registry+https://github.com/rust-lang/crates.io-index)" = "6ef0c1bcf2e99c649104bd7a7012d8f8802684400e03db0ec0af48583c6fa0e4" @@ -3298,13 +3278,13 @@ dependencies = [ "checksum crypto-mac 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4434400df11d95d556bac068ddfedd482915eb18fe8bea89bc80b6e4b1c179e5" "checksum csv 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "9fd1c44c58078cfbeaf11fbb3eac9ae5534c23004ed770cc4bfb48e658ae4f04" "checksum csv-core 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fa5cdef62f37e6ffe7d1f07a381bc0db32b7a3ff1cac0de56cb0d81e71f53d65" -"checksum ctr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "044f882973b245404e90c90e7e42e8ee8d7a64edfd7adf83d684fb97e8e2c1b6" +"checksum ctr 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "022cd691704491df67d25d006fe8eca083098253c4d43516c2206479c58c6736" "checksum ctrlc 3.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "630391922b1b893692c6334369ff528dcc3a9d8061ccf4c803aa8f83cb13db5e" "checksum data-encoding 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f4f47ca1860a761136924ddd2422ba77b2ea54fe8cc75b9040804a0d9d32ad97" "checksum data-encoding-macro 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "9b87bf377fc964606c3679c1de6822a9a9d8b69aae2651ca4af28cb2d1550b37" "checksum data-encoding-macro-internal 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "22b318a8d6d56c45df45c61fcc7d2dbf98ea014d4987e7c74ef1f86c9b87e503" "checksum digest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "05f47366984d3ad862010e22c7ce81a7dbcaebbdfb37241a620f8b6596ee135c" -"checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0" +"checksum either 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c67353c641dc847124ea1902d69bd753dee9bb3beff9aa3662ecf86c971d1fac" "checksum encode_unicode 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "90b2c9496c001e8cb61827acdefad780795c42264c137744cae6f7d9e3450abd" "checksum endian-type 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" "checksum env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b61fa891024a945da30a9581546e8cfaf5602c7b3f4c137a2805cf388f92075a" @@ -3328,11 +3308,11 @@ dependencies = [ "checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" "checksum globset 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4743617a7464bbda3c8aec8558ff2f9429047e025771037df561d383337ff865" "checksum goblin 0.0.19 (registry+https://github.com/rust-lang/crates.io-index)" = "c65cd533b33e3d04c6e393225fa8919ddfcf5862ca8919c7f9a167c312ef41c2" -"checksum h2 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "ddb2b25a33e231484694267af28fec74ac63b5ccf51ee2065a5e313b834d836e" +"checksum h2 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)" = "910a5e7be6283a9c91b3982fa5188368c8719cce2a3cf3b86048673bf9d9c36b" "checksum hashbrown 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3bae29b6653b3412c2e71e9d486db9f9df5d701941d86683005efb9f2d28e3da" "checksum heapsize 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1679e6ea370dee694f91f1dc469bf94cf8f52051d147aec3e1f9497c6fc22461" "checksum hmac 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f127a908633569f208325f86f71255d3363c79721d7f9fe31cd5569908819771" -"checksum http 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "1a10e5b573b9a0146545010f50772b9e8b1dd0a256564cc4307694c68832a2f5" +"checksum http 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "fe67e3678f2827030e89cc4b9e7ecd16d52f132c0b940ab5005f88e821500f6a" "checksum httparse 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e8734b0cfd3bc3e101ec59100e101c2eecd19282202e87808b3037b442777a83" "checksum humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ca7e5f2e110db35f93b837c81797f3714500b81d517bf20c431b16d3ca4f114" "checksum hyper 0.11.27 (registry+https://github.com/rust-lang/crates.io-index)" = "34a590ca09d341e94cddf8e5af0bbccde205d5fbc2fa3c09dd67c7f85cea59d7" @@ -3355,21 +3335,20 @@ dependencies = [ "checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73" "checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" "checksum lazycell 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f" -"checksum libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)" = "e962c7641008ac010fa60a7dfdc1712449f29c44ef2d4702394aea943ee75047" +"checksum libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)" = "aab692d7759f5cd8c859e169db98ae5b52c924add2af5fbbca11d12fefb567c1" "checksum libloading 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9c3ad660d7cb8c5822cd83d10897b0f1f1526792737a179e73896152f85b88c2" -"checksum librocksdb-sys 5.17.2 (git+https://github.com/rust-rocksdb/rust-rocksdb?rev=1038c71df85a58b7f48894ca4cbf7c9228cb1cdf)" = "" +"checksum librocksdb-sys 5.17.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7dfb546562f9b450237bb8df7a31961849ee9fb1186d9e356db1d7a6b7609ff2" "checksum libsqlite3-sys 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3567bc1a0c84e2c0d71eeb4a1f08451babf7843babd733158777d9c686dad9f3" "checksum linked-hash-map 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6d262045c5b87c0861b3f004610afd0e2c851e2908d08b6c870cbb9d5f494ecd" -"checksum linked-hash-map 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7860ec297f7008ff7a1e3382d7f7e1dcd69efc94751a2284bafc3d013c2aa939" "checksum linked-hash-map 0.5.1 (git+https://github.com/nervosnetwork/linked-hash-map?rev=df27f21)" = "" -"checksum linked-hash-map 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "70fb39025bc7cdd76305867c4eccf2f2dcf6e9a57f5b21a93e1c2d86cd03ec9e" +"checksum linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ae91b68aebc4ddb91978b11a1b02ddd8602a05ec19002801c5666000e05e0f83" "checksum lock_api 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "62ebf1391f6acad60e5c8b43706dde4582df75c06698ab44511d15016bc2442c" "checksum log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b" "checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6" "checksum lru-cache 0.1.0 (git+https://github.com/nervosnetwork/lru-cache)" = "" -"checksum lru-cache 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4d06ff7ff06f729ce5f4e227876cb88d10bc59cd4ae1e09fbb2bde15c850dc21" +"checksum lru-cache 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "31e24f1ad8321ca0e8a1e0ac13f23cb668e6f5466c2c57319f6a5cf1cc8e3b1c" "checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" -"checksum memchr 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e1dd4eaac298c32ce07eb6ed9242eda7d82955b9170b7d6db59b2e02cc63fcb8" +"checksum memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2efc7bc57c883d4a4d6e3246905283d8dae951bb3bd32f49d6ef297f546e1c39" "checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3" "checksum merkle-cbt 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d589b5a7ca642540e7ccfbca3bcd0aa18693eb9287e2a6b17c79b1d062d52863" "checksum mime 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)" = "3e27ca21f40a310bd06d9031785f4801710d566c184a6e15bad4f1d9b65f9425" @@ -3380,7 +3359,7 @@ dependencies = [ "checksum nibble_vec 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "c8d77f3db4bce033f4d04db08079b2ef1c3d02b44e86f25d08886fafa7756ffa" "checksum nix 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d37e713a259ff641624b6cb20e3b12b2952313ba36b6823c0f16e6cfd9e5de17" "checksum nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9667ddcc6cc8a43afc9b7917599d7216aa09c463919ea32c59ed6cac8bc945" -"checksum nom 4.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b30adc557058ce00c9d0d7cb3c6e0b5bc6f36e2e2eabe74b0ba726d194abd588" +"checksum nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2ad2a91a8e869eeb30b9cb3119ae87773a8f4ae617f41b1eb9c154b2905f7bd6" "checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea" "checksum num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31" "checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1" @@ -3402,7 +3381,7 @@ dependencies = [ "checksum proc-macro-hack 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2c725b36c99df7af7bf9324e9c999b9e37d92c8f8caf106d82e1d7953218d2d8" "checksum proc-macro-hack-impl 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2b753ad9ed99dd8efeaa7d2fb8453c8f6bc3e54b97966d35f1bc77ca6865254a" "checksum proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)" = "4d317f9caece796be1980837fd5cb3dfec5613ebdb04ad0956deea83ce168915" -"checksum proptest 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea66c78d75f2c6e9f304269eaef90899798daecc69f1a625d5a3dd793ff3522" +"checksum proptest 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "24f5844db2f839e97e3021980975f6ebf8691d9b9b2ca67ed3feb38dc3edb52c" "checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0" "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" "checksum quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "cdd8e04bd9c52e0342b406469d494fcb033be4bdbe5c606016defbb1681411e1" @@ -3419,8 +3398,8 @@ dependencies = [ "checksum rand_hc 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7b40677c7be09ae76218dc623efbf7b18e34bced3f38883af07bb75630a21bc4" "checksum rand_isaac 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ded997c9d5f13925be2a6fd7e66bf1872597f759fd9dd93513dd7e92e5a5ee08" "checksum rand_jitter 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7b9ea758282efe12823e0d952ddb269d2e1897227e464919a554f2a03ef1b832" -"checksum rand_os 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b7c690732391ae0abafced5015ffb53656abfaec61b342290e5eb56b286a679d" -"checksum rand_pcg 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "086bd09a33c7044e56bb44d5bdde5a60e7f119a9e95b0775f545de759a32fe05" +"checksum rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071" +"checksum rand_pcg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44" "checksum rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c" "checksum rand_xoshiro 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "03b418169fb9c46533f326efd6eed2576699c44ca92d3052a066214a8d828929" "checksum rayon 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "373814f27745b2686b350dd261bfd24576a6fb0e2c5919b3a2b6005f820b0473" @@ -3433,7 +3412,7 @@ dependencies = [ "checksum relay 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1576e382688d7e9deecea24417e350d3062d97e32e45d70b1cde65994ff1489a" "checksum remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3488ba1b9a2084d38645c4c08276a1752dcbf2c7130d74f1569681ad5d2799c5" "checksum ring 0.14.6 (registry+https://github.com/rust-lang/crates.io-index)" = "426bc186e3e95cac1e4a4be125a4aca7e84c2d616ffc02244eef36e2a60a093c" -"checksum rocksdb 0.11.0 (git+https://github.com/rust-rocksdb/rust-rocksdb?rev=1038c71df85a58b7f48894ca4cbf7c9228cb1cdf)" = "" +"checksum rocksdb 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "231ceea69907012c24a63315418c40da12d02ea7aecad9360fe18441976d0592" "checksum rusqlite 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6381ddfe91dbb659b4b132168da15985bc84162378cf4fcdc4eb99c857d063e2" "checksum rust-ini 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3e52c148ef37f8c375d49d5a73aa70713125b7f19095948a923f80afdeb22ec2" "checksum rustc-demangle 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "adacaae16d02b6ec37fdc7acfcddf365978de76d1983d3ee22afc260e1ca9619" @@ -3463,7 +3442,7 @@ dependencies = [ "checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23" "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" "checksum smallvec 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4c8cbcd6df1e117c2210e13ab5109635ad68a929fcbb8964dc965b76cb5ee013" -"checksum smallvec 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)" = "88aea073965ab29f6edb5493faf96ad662fb18aa9eeb186a3b7057951605ed15" +"checksum smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c4488ae950c49d403731982257768f48fada354a5203fe81f9bb6f43ca9002be" "checksum snap 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "95d697d63d44ad8b78b8d235bf85b34022a78af292c8918527c5f0cffdde7f43" "checksum socket2 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "c4d11a52082057d87cb5caa31ad812f4504b97ab44732cd8359df2e9ff9f48e7" "checksum spin 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "44363f6f51401c34e7be73db0db371c04705d35efbe9f7d6082e03a921a32c55" @@ -3473,7 +3452,7 @@ dependencies = [ "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" "checksum subtle 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2d67a5a62ba6e01cb2192ff309324cb4875d0c451d55fe2319433abe7a05a8ee" "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" -"checksum syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)" = "f92e629aa1d9c827b2bb8297046c1ccffc57c99b947a680d3ccff1f136a3bee9" +"checksum syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)" = "1825685f977249735d510a242a6727b46efe914bb67e38d30c071b1b72b1d5c2" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015" "checksum take 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b157868d8ac1f56b64604539990685fa7611d8fa9e5476cf0c02cf34d32917c5" @@ -3501,7 +3480,7 @@ dependencies = [ "checksum tokio-proto 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8fbb47ae81353c63c487030659494b295f6cb6576242f907f203473b191b0389" "checksum tokio-reactor 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "6af16bfac7e112bea8b0442542161bfc41cbfa4466b580bdda7d18cb88b911ce" "checksum tokio-service 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "24da22d077e0f15f55162bdbdc661228c1581892f52074fb242678d015b45162" -"checksum tokio-sync 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1bf2b9dac2a0509b5cfd1df5aa25eafacb616a42a491a13604d6bbeab4486363" +"checksum tokio-sync 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "fda385df506bf7546e70872767f71e81640f1f251bdf2fd8eb81a0eaec5fe022" "checksum tokio-tcp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1d14b10654be682ac43efee27401d792507e30fd8d26389e1da3b185de2e4119" "checksum tokio-threadpool 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "ec5759cf26cf9659555f36c431b515e3d05f66831741c85b4b5d5dfb9cf1323c" "checksum tokio-timer 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "2910970404ba6fa78c5539126a9ae2045d62e3713041e447f695f41405a120c6" @@ -3518,13 +3497,12 @@ dependencies = [ "checksum twofish 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712d261e83e727c8e2dbb75dacac67c36e35db36a958ee504f2164fc052434e1" "checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169" "checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86" -"checksum unicase 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9d3218ea14b4edcaccfa0df0a64a3792a2c32cc706f1b336e48867f9d3147f90" +"checksum unicase 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "41d17211f887da8e4a70a45b9536f26fc5de166b81e2d5d80de4a17fd22553bd" "checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" "checksum unicode-normalization 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "141339a08b982d942be2ca06ff8b076563cbe223d1befd5450716790d44e2426" "checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" "checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" -"checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" "checksum unsigned-varint 0.2.2 (git+https://github.com/paritytech/unsigned-varint)" = "" "checksum unsigned-varint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2c64cdf40b4a9645534a943668681bcb219faf51874d4b65d2e0abda1b10a2ab" "checksum untrusted 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "55cd1f4b4e96b46aeb8d4855db4a7a9bd96eeeb5c6a1ab54593328761642ce2f" @@ -3547,4 +3525,4 @@ dependencies = [ "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" "checksum wincolor 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "561ed901ae465d6185fa7864d63fbd5720d0ef718366c9a4dc83cf6170d7e9ba" "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" -"checksum yaml-rust 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "95acf0db5515d07da9965ec0e0ba6cc2d825e2caeb7303b66ca441729801254e" +"checksum yaml-rust 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "65923dd1784f44da1d2c3dbbc5e822045628c590ba72123e1c73d3c230c4434d" diff --git a/db/Cargo.toml b/db/Cargo.toml index 538fb36833..4fa647f5e6 100644 --- a/db/Cargo.toml +++ b/db/Cargo.toml @@ -8,7 +8,7 @@ edition = "2018" [dependencies] bincode = "1.1" ckb-util = { path = "../util" } -rocksdb = { git = "https://github.com/rust-rocksdb/rust-rocksdb", rev = "1038c71df85a58b7f48894ca4cbf7c9228cb1cdf" } +rocksdb = "0.12.0" fnv = "1.0.3" serde = "1.0" serde_derive = "1.0" From 37e7cb7d0223ae627a8ffca9ccfc598bd04bd6fc Mon Sep 17 00:00:00 2001 From: u2 Date: Fri, 15 Mar 2019 22:30:11 +0800 Subject: [PATCH 86/99] feature: index whether a tx is a cellbase in the chain, prepare for the cellbase outputs maturity checking --- core/src/transaction_meta.rs | 30 +++++++++++++++++++++++------- shared/src/cell_set.rs | 25 ++++++++++++++++++------- shared/src/shared.rs | 2 +- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/core/src/transaction_meta.rs b/core/src/transaction_meta.rs index 8926f783fd..5484d02eca 100644 --- a/core/src/transaction_meta.rs +++ b/core/src/transaction_meta.rs @@ -16,17 +16,33 @@ impl From for BitVec { #[derive(Default, Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] pub struct TransactionMeta { + block_number: u64, + /// first bit indicate if transaction is a cellbase transaction + /// next bits indicate if transaction has deal cells #[serde(with = "BitVecSerde")] - pub dead_cell: BitVec, + dead_cell: BitVec, } impl TransactionMeta { - pub fn new(outputs_count: usize) -> TransactionMeta { + pub fn new(block_number: u64, outputs_count: usize) -> TransactionMeta { TransactionMeta { - dead_cell: BitVec::from_elem(outputs_count, false), + block_number, + dead_cell: BitVec::from_elem(outputs_count + 1, false), } } + /// New cellbase transaction + pub fn new_cellbase(block_number: u64, outputs: usize) -> Self { + let mut result = Self::new(block_number, outputs); + result.dead_cell.set(0, true); + result + } + + /// Returns true if it is a cellbase transaction + pub fn is_cellbase(&self) -> bool { + self.dead_cell.get(0).expect("One bit should always exists") + } + pub fn len(&self) -> usize { self.dead_cell.len() } @@ -44,15 +60,15 @@ impl TransactionMeta { } pub fn is_dead(&self, index: usize) -> bool { - self.dead_cell.get(index).unwrap_or(true) + self.dead_cell.get(index + 1).unwrap_or(true) } pub fn set_dead(&mut self, index: usize) { - self.dead_cell.set(index, true); + self.dead_cell.set(index + 1, true); } pub fn unset_dead(&mut self, index: usize) { - self.dead_cell.set(index, false); + self.dead_cell.set(index + 1, false); } } @@ -63,7 +79,7 @@ mod tests { #[test] fn transaction_meta_serde() { - let mut original = TransactionMeta::new(4); + let mut original = TransactionMeta::new(0, 4); original.set_dead(1); original.set_dead(3); diff --git a/shared/src/cell_set.rs b/shared/src/cell_set.rs index d42d1cf972..88fc71729a 100644 --- a/shared/src/cell_set.rs +++ b/shared/src/cell_set.rs @@ -11,7 +11,7 @@ pub struct CellSetDiff { pub old_inputs: FnvHashSet, pub old_outputs: FnvHashSet, pub new_inputs: FnvHashSet, - pub new_outputs: FnvHashMap, + pub new_outputs: FnvHashMap, } impl CellSetDiff { @@ -21,7 +21,10 @@ impl CellSetDiff { let tx_hash = tx.hash(); let output_len = tx.outputs().len(); self.new_inputs.extend(input_pts); - self.new_outputs.insert(tx_hash, output_len); + self.new_outputs.insert( + tx_hash, + (block.header().number(), tx.is_cellbase(), output_len), + ); } } @@ -66,8 +69,14 @@ impl CellSet { self.inner.get(h) } - pub fn insert(&mut self, hash: H256, outputs_len: usize) { - self.inner.insert(hash, TransactionMeta::new(outputs_len)); + pub fn insert(&mut self, hash: H256, number: u64, cellbase: bool, outputs_len: usize) { + if cellbase { + self.inner + .insert(hash, TransactionMeta::new_cellbase(number, outputs_len)); + } else { + self.inner + .insert(hash, TransactionMeta::new(number, outputs_len)); + } } pub fn remove(&mut self, hash: &H256) -> Option { @@ -102,9 +111,11 @@ impl CellSet { self.mark_live(o); }); - new_outputs.into_iter().for_each(|(hash, len)| { - self.insert(hash, len); - }); + new_outputs + .into_iter() + .for_each(|(hash, (number, cellbase, len))| { + self.insert(hash, number, cellbase, len); + }); new_inputs.iter().for_each(|o| { self.mark_dead(o); diff --git a/shared/src/shared.rs b/shared/src/shared.rs index 7942986dec..1c9b522bac 100644 --- a/shared/src/shared.rs +++ b/shared/src/shared.rs @@ -140,7 +140,7 @@ impl Shared { cell_set.mark_dead(&o); } - cell_set.insert(tx.hash(), output_len); + cell_set.insert(tx.hash(), n, tx.is_cellbase(), output_len); } } From 477b5683ebdbaf1c7d66c7b3385e6a1fef962f8d Mon Sep 17 00:00:00 2001 From: jjy Date: Mon, 25 Mar 2019 16:25:36 +0800 Subject: [PATCH 87/99] refactor: use DSL macro to define network behaviours --- network/src/behaviour.rs | 25 ++++++++++++++++++------- sync/src/relayer/mod.rs | 2 +- sync/src/synchronizer/mod.rs | 4 ++-- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/network/src/behaviour.rs b/network/src/behaviour.rs index 0ad670fe5d..0bd639a35e 100644 --- a/network/src/behaviour.rs +++ b/network/src/behaviour.rs @@ -1,10 +1,21 @@ use crate::Score; pub type Behaviour = (Score, &'static str); -pub const CONNECT: Behaviour = (10, "peer connected"); -pub const UNEXPECTED_DISCONNECT: Behaviour = (-10, "peer unexpected disconnected"); -pub const PING: Behaviour = (10, "peer ping"); -pub const FAILED_TO_PING: Behaviour = (-20, "failed to ping"); -pub const SYNC_USELESS: Behaviour = (-50, "sync useless"); -pub const UNEXPECTED_NETWORK_MESSAGE: Behaviour = (-50, "unexpected network message"); -pub const NETWORK_TIMEOUT: Behaviour = (-20, "network timeout"); +macro_rules! define_behaviour { + ( $( $name:ident => $score:expr ),* ) => { + $( + pub const $name: Behaviour = ($score,"$name"); + )* + }; +} + +// Define behaviours and scores +define_behaviour! { + CONNECT => 10, + PING => 10, + FAILED_TO_PING => -20, + TIMEOUT => -20, + SYNC_USELESS => -50, + UNEXPECTED_MESSAGE => -50, + UNEXPECTED_DISCONNECT => -10 +} diff --git a/sync/src/relayer/mod.rs b/sync/src/relayer/mod.rs index bc5c108e9f..dcf9052c1f 100644 --- a/sync/src/relayer/mod.rs +++ b/sync/src/relayer/mod.rs @@ -128,7 +128,7 @@ where fn process(&self, nc: &CKBProtocolContext, peer: PeerIndex, message: RelayMessage) { if self.try_process(nc, peer, message).is_err() { - let _ = nc.report_peer(peer, behaviour::UNEXPECTED_NETWORK_MESSAGE); + let _ = nc.report_peer(peer, behaviour::UNEXPECTED_MESSAGE); } } diff --git a/sync/src/synchronizer/mod.rs b/sync/src/synchronizer/mod.rs index 8f28aa2081..c261e9a372 100644 --- a/sync/src/synchronizer/mod.rs +++ b/sync/src/synchronizer/mod.rs @@ -182,7 +182,7 @@ impl Synchronizer { fn process(&self, nc: &CKBProtocolContext, peer: PeerIndex, message: SyncMessage) { if self.try_process(nc, peer, message).is_err() { - let _ = nc.report_peer(peer, behaviour::UNEXPECTED_NETWORK_MESSAGE); + let _ = nc.report_peer(peer, behaviour::UNEXPECTED_MESSAGE); } } @@ -622,7 +622,7 @@ impl Synchronizer { } for peer in eviction { warn!(target: "sync", "timeout eviction peer={}", peer); - let _ = nc.report_peer(peer, behaviour::NETWORK_TIMEOUT); + let _ = nc.report_peer(peer, behaviour::TIMEOUT); } } From b5d78df252188a5e3675f8202432bd620c000876 Mon Sep 17 00:00:00 2001 From: ian Date: Mon, 25 Mar 2019 21:17:55 +0800 Subject: [PATCH 88/99] chore: bump to v0.8.0-pre --- Cargo.lock | 322 +++++++++++------------ Cargo.toml | 2 +- benches/Cargo.toml | 2 +- chain/Cargo.toml | 2 +- core/Cargo.toml | 2 +- db/Cargo.toml | 2 +- miner/Cargo.toml | 2 +- network/Cargo.toml | 2 +- notify/Cargo.toml | 2 +- pow/Cargo.toml | 2 +- protocol/Cargo.toml | 2 +- rpc/Cargo.toml | 2 +- script/Cargo.toml | 2 +- shared/Cargo.toml | 2 +- spec/Cargo.toml | 2 +- sync/Cargo.toml | 2 +- test/Cargo.toml | 2 +- traits/Cargo.toml | 2 +- util/Cargo.toml | 2 +- util/build-info/Cargo.toml | 2 +- util/crypto/Cargo.toml | 2 +- util/dir/Cargo.toml | 2 +- util/hash/Cargo.toml | 2 +- util/instrument/Cargo.toml | 2 +- util/jsonrpc-types/Cargo.toml | 2 +- util/logger/Cargo.toml | 2 +- util/merkle-tree/Cargo.toml | 2 +- util/occupied-capacity-derive/Cargo.toml | 2 +- util/occupied-capacity/Cargo.toml | 2 +- util/stop-handler/Cargo.toml | 2 +- verification/Cargo.toml | 2 +- 31 files changed, 191 insertions(+), 191 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fb55f48fcf..fd9ea8b8d0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -219,7 +219,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "build-info" -version = "0.7.0" +version = "0.8.0-pre" [[package]] name = "byte-tools" @@ -278,32 +278,32 @@ dependencies = [ [[package]] name = "ckb" -version = "0.7.0" -dependencies = [ - "build-info 0.7.0", - "ckb-chain 0.7.0", - "ckb-chain-spec 0.7.0", - "ckb-core 0.7.0", - "ckb-db 0.7.0", - "ckb-instrument 0.7.0", - "ckb-miner 0.7.0", - "ckb-network 0.7.0", - "ckb-notify 0.7.0", - "ckb-pow 0.7.0", - "ckb-rpc 0.7.0", - "ckb-shared 0.7.0", - "ckb-sync 0.7.0", - "ckb-traits 0.7.0", - "ckb-util 0.7.0", +version = "0.8.0-pre" +dependencies = [ + "build-info 0.8.0-pre", + "ckb-chain 0.8.0-pre", + "ckb-chain-spec 0.8.0-pre", + "ckb-core 0.8.0-pre", + "ckb-db 0.8.0-pre", + "ckb-instrument 0.8.0-pre", + "ckb-miner 0.8.0-pre", + "ckb-network 0.8.0-pre", + "ckb-notify 0.8.0-pre", + "ckb-pow 0.8.0-pre", + "ckb-rpc 0.8.0-pre", + "ckb-shared 0.8.0-pre", + "ckb-sync 0.8.0-pre", + "ckb-traits 0.8.0-pre", + "ckb-util 0.8.0-pre", "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", "config 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", - "crypto 0.7.0", + "crypto 0.8.0-pre", "ctrlc 3.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "dir 0.7.0", - "hash 0.7.0", + "dir 0.8.0-pre", + "hash 0.8.0-pre", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "logger 0.7.0", + "logger 0.8.0-pre", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", @@ -314,16 +314,16 @@ dependencies = [ [[package]] name = "ckb-benches" -version = "0.7.0" -dependencies = [ - "ckb-chain 0.7.0", - "ckb-chain-spec 0.7.0", - "ckb-core 0.7.0", - "ckb-db 0.7.0", - "ckb-notify 0.7.0", - "ckb-pow 0.7.0", - "ckb-shared 0.7.0", - "ckb-traits 0.7.0", +version = "0.8.0-pre" +dependencies = [ + "ckb-chain 0.8.0-pre", + "ckb-chain-spec 0.8.0-pre", + "ckb-core 0.8.0-pre", + "ckb-db 0.8.0-pre", + "ckb-notify 0.8.0-pre", + "ckb-pow 0.8.0-pre", + "ckb-shared 0.8.0-pre", + "ckb-traits 0.8.0-pre", "criterion 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -333,15 +333,15 @@ dependencies = [ [[package]] name = "ckb-chain" -version = "0.7.0" -dependencies = [ - "ckb-chain-spec 0.7.0", - "ckb-core 0.7.0", - "ckb-db 0.7.0", - "ckb-notify 0.7.0", - "ckb-shared 0.7.0", - "ckb-traits 0.7.0", - "ckb-verification 0.7.0", +version = "0.8.0-pre" +dependencies = [ + "ckb-chain-spec 0.8.0-pre", + "ckb-core 0.8.0-pre", + "ckb-db 0.8.0-pre", + "ckb-notify 0.8.0-pre", + "ckb-shared 0.8.0-pre", + "ckb-traits 0.8.0-pre", + "ckb-verification 0.8.0-pre", "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -352,17 +352,17 @@ dependencies = [ "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", - "stop-handler 0.7.0", + "stop-handler 0.8.0-pre", "tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "ckb-chain-spec" -version = "0.7.0" +version = "0.8.0-pre" dependencies = [ - "ckb-core 0.7.0", - "ckb-pow 0.7.0", - "ckb-protocol 0.7.0", + "ckb-core 0.8.0-pre", + "ckb-pow 0.8.0-pre", + "ckb-protocol 0.8.0-pre", "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -373,20 +373,20 @@ dependencies = [ [[package]] name = "ckb-core" -version = "0.7.0" +version = "0.8.0-pre" dependencies = [ "bincode 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "bit-vec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "ckb-merkle-tree 0.7.0", - "ckb-util 0.7.0", + "ckb-merkle-tree 0.8.0-pre", + "ckb-util 0.8.0-pre", "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", - "crypto 0.7.0", + "crypto 0.8.0-pre", "faster-hex 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", - "hash 0.7.0", + "hash 0.8.0-pre", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "occupied-capacity 0.7.0", + "occupied-capacity 0.8.0-pre", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_bytes 0.10.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", @@ -394,10 +394,10 @@ dependencies = [ [[package]] name = "ckb-db" -version = "0.7.0" +version = "0.8.0-pre" dependencies = [ "bincode 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "ckb-util 0.7.0", + "ckb-util 0.8.0-pre", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "rocksdb 0.11.0 (git+https://github.com/rust-rocksdb/rust-rocksdb?rev=1038c71df85a58b7f48894ca4cbf7c9228cb1cdf)", @@ -409,46 +409,46 @@ dependencies = [ [[package]] name = "ckb-instrument" -version = "0.7.0" +version = "0.8.0-pre" dependencies = [ - "ckb-chain 0.7.0", - "ckb-core 0.7.0", - "ckb-shared 0.7.0", - "ckb-traits 0.7.0", + "ckb-chain 0.8.0-pre", + "ckb-core 0.8.0-pre", + "ckb-shared 0.8.0-pre", + "ckb-traits 0.8.0-pre", "indicatif 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "ckb-merkle-tree" -version = "0.7.0" +version = "0.8.0-pre" dependencies = [ - "hash 0.7.0", + "hash 0.8.0-pre", "merkle-cbt 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "ckb-miner" -version = "0.7.0" -dependencies = [ - "ckb-chain 0.7.0", - "ckb-chain-spec 0.7.0", - "ckb-core 0.7.0", - "ckb-db 0.7.0", - "ckb-notify 0.7.0", - "ckb-pow 0.7.0", - "ckb-shared 0.7.0", - "ckb-traits 0.7.0", - "ckb-util 0.7.0", - "ckb-verification 0.7.0", +version = "0.8.0-pre" +dependencies = [ + "ckb-chain 0.8.0-pre", + "ckb-chain-spec 0.8.0-pre", + "ckb-core 0.8.0-pre", + "ckb-db 0.8.0-pre", + "ckb-notify 0.8.0-pre", + "ckb-pow 0.8.0-pre", + "ckb-shared 0.8.0-pre", + "ckb-traits 0.8.0-pre", + "ckb-util 0.8.0-pre", + "ckb-verification 0.8.0-pre", "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "faketime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.12.25 (registry+https://github.com/rust-lang/crates.io-index)", - "jsonrpc-types 0.7.0", + "jsonrpc-types 0.8.0-pre", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "lru-cache 0.1.0 (git+https://github.com/nervosnetwork/lru-cache)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -458,15 +458,15 @@ dependencies = [ "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", - "stop-handler 0.7.0", + "stop-handler 0.8.0-pre", ] [[package]] name = "ckb-network" -version = "0.7.0" +version = "0.8.0-pre" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "ckb-util 0.7.0", + "ckb-util 0.8.0-pre", "criterion 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "faketime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -494,23 +494,23 @@ dependencies = [ [[package]] name = "ckb-notify" -version = "0.7.0" +version = "0.8.0-pre" dependencies = [ - "ckb-core 0.7.0", + "ckb-core 0.8.0-pre", "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "stop-handler 0.7.0", + "stop-handler 0.8.0-pre", ] [[package]] name = "ckb-pow" -version = "0.7.0" +version = "0.8.0-pre" dependencies = [ "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "ckb-core 0.7.0", + "ckb-core 0.8.0-pre", "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", - "hash 0.7.0", + "hash 0.8.0-pre", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "proptest 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -520,15 +520,15 @@ dependencies = [ [[package]] name = "ckb-protocol" -version = "0.7.0" +version = "0.8.0-pre" dependencies = [ "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "ckb-core 0.7.0", - "ckb-merkle-tree 0.7.0", - "ckb-util 0.7.0", + "ckb-core 0.8.0-pre", + "ckb-merkle-tree 0.8.0-pre", + "ckb-util 0.8.0-pre", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", - "hash 0.7.0", + "hash 0.8.0-pre", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -537,20 +537,20 @@ dependencies = [ [[package]] name = "ckb-rpc" -version = "0.7.0" -dependencies = [ - "build-info 0.7.0", - "ckb-chain 0.7.0", - "ckb-core 0.7.0", - "ckb-db 0.7.0", - "ckb-miner 0.7.0", - "ckb-network 0.7.0", - "ckb-pow 0.7.0", - "ckb-protocol 0.7.0", - "ckb-shared 0.7.0", - "ckb-sync 0.7.0", - "ckb-traits 0.7.0", - "ckb-verification 0.7.0", +version = "0.8.0-pre" +dependencies = [ + "build-info 0.8.0-pre", + "ckb-chain 0.8.0-pre", + "ckb-core 0.8.0-pre", + "ckb-db 0.8.0-pre", + "ckb-miner 0.8.0-pre", + "ckb-network 0.8.0-pre", + "ckb-pow 0.8.0-pre", + "ckb-protocol 0.8.0-pre", + "ckb-shared 0.8.0-pre", + "ckb-sync 0.8.0-pre", + "ckb-traits 0.8.0-pre", + "ckb-verification 0.8.0-pre", "faster-hex 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", @@ -558,7 +558,7 @@ dependencies = [ "jsonrpc-derive 10.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "jsonrpc-http-server 10.0.1 (git+https://github.com/nervosnetwork/jsonrpc?branch=http_remake)", "jsonrpc-server-utils 10.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "jsonrpc-types 0.7.0", + "jsonrpc-types 0.8.0-pre", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -570,17 +570,17 @@ dependencies = [ [[package]] name = "ckb-script" -version = "0.7.0" +version = "0.8.0-pre" dependencies = [ "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "ckb-core 0.7.0", - "ckb-protocol 0.7.0", + "ckb-core 0.8.0-pre", + "ckb-protocol 0.8.0-pre", "ckb-vm 0.1.0 (git+https://github.com/nervosnetwork/ckb-vm?rev=dd90ed1)", - "crypto 0.7.0", + "crypto 0.8.0-pre", "faster-hex 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", - "hash 0.7.0", + "hash 0.8.0-pre", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "proptest 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -588,15 +588,15 @@ dependencies = [ [[package]] name = "ckb-shared" -version = "0.7.0" +version = "0.8.0-pre" dependencies = [ "bincode 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "ckb-chain-spec 0.7.0", - "ckb-core 0.7.0", - "ckb-db 0.7.0", - "ckb-traits 0.7.0", - "ckb-util 0.7.0", - "ckb-verification 0.7.0", + "ckb-chain-spec 0.8.0-pre", + "ckb-core 0.8.0-pre", + "ckb-db 0.8.0-pre", + "ckb-traits 0.8.0-pre", + "ckb-util 0.8.0-pre", + "ckb-verification 0.8.0-pre", "env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "faketime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -606,7 +606,7 @@ dependencies = [ "lru-cache 0.1.0 (git+https://github.com/nervosnetwork/lru-cache)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "occupied-capacity 0.7.0", + "occupied-capacity 0.8.0-pre", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", @@ -615,22 +615,22 @@ dependencies = [ [[package]] name = "ckb-sync" -version = "0.7.0" +version = "0.8.0-pre" dependencies = [ "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "bloom-filters 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "ckb-chain 0.7.0", - "ckb-chain-spec 0.7.0", - "ckb-core 0.7.0", - "ckb-db 0.7.0", - "ckb-network 0.7.0", - "ckb-notify 0.7.0", - "ckb-protocol 0.7.0", - "ckb-shared 0.7.0", - "ckb-traits 0.7.0", - "ckb-util 0.7.0", - "ckb-verification 0.7.0", + "ckb-chain 0.8.0-pre", + "ckb-chain-spec 0.8.0-pre", + "ckb-core 0.8.0-pre", + "ckb-db 0.8.0-pre", + "ckb-network 0.8.0-pre", + "ckb-notify 0.8.0-pre", + "ckb-protocol 0.8.0-pre", + "ckb-shared 0.8.0-pre", + "ckb-traits 0.8.0-pre", + "ckb-util 0.8.0-pre", + "ckb-verification 0.8.0-pre", "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -646,16 +646,16 @@ dependencies = [ [[package]] name = "ckb-test" -version = "0.7.0" +version = "0.8.0-pre" dependencies = [ - "ckb-core 0.7.0", - "ckb-network 0.7.0", + "ckb-core 0.8.0-pre", + "ckb-network 0.8.0-pre", "fs_extra 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "jsonrpc-client-core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "jsonrpc-client-http 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", - "jsonrpc-types 0.7.0", + "jsonrpc-types 0.8.0-pre", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "logger 0.7.0", + "logger 0.8.0-pre", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)", @@ -663,10 +663,10 @@ dependencies = [ [[package]] name = "ckb-traits" -version = "0.7.0" +version = "0.8.0-pre" dependencies = [ - "ckb-chain-spec 0.7.0", - "ckb-core 0.7.0", + "ckb-chain-spec 0.8.0-pre", + "ckb-core 0.8.0-pre", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -674,33 +674,33 @@ dependencies = [ [[package]] name = "ckb-util" -version = "0.7.0" +version = "0.8.0-pre" dependencies = [ "parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "ckb-verification" -version = "0.7.0" -dependencies = [ - "ckb-chain 0.7.0", - "ckb-chain-spec 0.7.0", - "ckb-core 0.7.0", - "ckb-db 0.7.0", - "ckb-merkle-tree 0.7.0", - "ckb-notify 0.7.0", - "ckb-pow 0.7.0", - "ckb-script 0.7.0", - "ckb-shared 0.7.0", - "ckb-traits 0.7.0", +version = "0.8.0-pre" +dependencies = [ + "ckb-chain 0.8.0-pre", + "ckb-chain-spec 0.8.0-pre", + "ckb-core 0.8.0-pre", + "ckb-db 0.8.0-pre", + "ckb-merkle-tree 0.8.0-pre", + "ckb-notify 0.8.0-pre", + "ckb-pow 0.8.0-pre", + "ckb-script 0.8.0-pre", + "ckb-shared 0.8.0-pre", + "ckb-traits 0.8.0-pre", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "faketime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", - "hash 0.7.0", + "hash 0.8.0-pre", "lru-cache 0.1.0 (git+https://github.com/nervosnetwork/lru-cache)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "occupied-capacity 0.7.0", + "occupied-capacity 0.8.0-pre", "rayon 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -914,7 +914,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "crypto" -version = "0.7.0" +version = "0.8.0-pre" dependencies = [ "crunchy 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1004,7 +1004,7 @@ dependencies = [ [[package]] name = "dir" -version = "0.7.0" +version = "0.8.0-pre" [[package]] name = "either" @@ -1198,7 +1198,7 @@ dependencies = [ [[package]] name = "hash" -version = "0.7.0" +version = "0.8.0-pre" dependencies = [ "blake2b-rs 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "faster-hex 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1448,9 +1448,9 @@ dependencies = [ [[package]] name = "jsonrpc-types" -version = "0.7.0" +version = "0.8.0-pre" dependencies = [ - "ckb-core 0.7.0", + "ckb-core 0.8.0-pre", "faster-hex 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "jsonrpc-core 10.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1579,7 +1579,7 @@ dependencies = [ [[package]] name = "logger" -version = "0.7.0" +version = "0.8.0-pre" dependencies = [ "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1797,15 +1797,15 @@ dependencies = [ [[package]] name = "occupied-capacity" -version = "0.7.0" +version = "0.8.0-pre" dependencies = [ "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "occupied-capacity-derive 0.7.0", + "occupied-capacity-derive 0.8.0-pre", ] [[package]] name = "occupied-capacity-derive" -version = "0.7.0" +version = "0.8.0-pre" dependencies = [ "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2467,7 +2467,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "stop-handler" -version = "0.7.0" +version = "0.8.0-pre" dependencies = [ "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 3c7e90c5ec..11a7fe13cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/benches/Cargo.toml b/benches/Cargo.toml index cf70df9ba7..a4870b2a06 100644 --- a/benches/Cargo.toml +++ b/benches/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-benches" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/chain/Cargo.toml b/chain/Cargo.toml index b50a4995b9..17875ccb98 100644 --- a/chain/Cargo.toml +++ b/chain/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-chain" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/core/Cargo.toml b/core/Cargo.toml index 6959e556f7..bcb521213f 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-core" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/db/Cargo.toml b/db/Cargo.toml index 758ff8bf9d..1525514732 100644 --- a/db/Cargo.toml +++ b/db/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-db" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/miner/Cargo.toml b/miner/Cargo.toml index 247b540d1a..51f53c5aba 100644 --- a/miner/Cargo.toml +++ b/miner/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-miner" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/network/Cargo.toml b/network/Cargo.toml index 1d5428ea59..27d14ceb2b 100644 --- a/network/Cargo.toml +++ b/network/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-network" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/notify/Cargo.toml b/notify/Cargo.toml index 6c97f3e8d5..bf36ac365d 100644 --- a/notify/Cargo.toml +++ b/notify/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-notify" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/pow/Cargo.toml b/pow/Cargo.toml index 7622838b9e..940703a052 100644 --- a/pow/Cargo.toml +++ b/pow/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-pow" -version = "0.7.0" +version = "0.8.0-pre" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/protocol/Cargo.toml b/protocol/Cargo.toml index eb807fc1ad..8e4f8964b2 100644 --- a/protocol/Cargo.toml +++ b/protocol/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-protocol" -version = "0.7.0" +version = "0.8.0-pre" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index 8d4987ae5c..e0086deb1c 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-rpc" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/script/Cargo.toml b/script/Cargo.toml index e8b6a5e49b..c5013e45e7 100644 --- a/script/Cargo.toml +++ b/script/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-script" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/shared/Cargo.toml b/shared/Cargo.toml index 2bbf219e1b..4671f28085 100644 --- a/shared/Cargo.toml +++ b/shared/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-shared" -version = "0.7.0" +version = "0.8.0-pre" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/spec/Cargo.toml b/spec/Cargo.toml index 15ffefa9e5..d9791baf94 100644 --- a/spec/Cargo.toml +++ b/spec/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-chain-spec" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/sync/Cargo.toml b/sync/Cargo.toml index 41bcb25da5..98dcc44c95 100644 --- a/sync/Cargo.toml +++ b/sync/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-sync" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/test/Cargo.toml b/test/Cargo.toml index 243ad37bce..71ec3cd13f 100644 --- a/test/Cargo.toml +++ b/test/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-test" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/traits/Cargo.toml b/traits/Cargo.toml index f295b8244d..78f8688359 100644 --- a/traits/Cargo.toml +++ b/traits/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-traits" -version = "0.7.0" +version = "0.8.0-pre" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/util/Cargo.toml b/util/Cargo.toml index 8bc181df42..e0758d8353 100644 --- a/util/Cargo.toml +++ b/util/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-util" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/util/build-info/Cargo.toml b/util/build-info/Cargo.toml index 35afaec0a7..e8958021b3 100644 --- a/util/build-info/Cargo.toml +++ b/util/build-info/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "build-info" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/util/crypto/Cargo.toml b/util/crypto/Cargo.toml index 5ccf2d53ae..bab1f7a4f8 100644 --- a/util/crypto/Cargo.toml +++ b/util/crypto/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "crypto" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/util/dir/Cargo.toml b/util/dir/Cargo.toml index 693b3edb45..6649a7dc75 100644 --- a/util/dir/Cargo.toml +++ b/util/dir/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dir" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/util/hash/Cargo.toml b/util/hash/Cargo.toml index f7b69c59ce..da53a3eaba 100644 --- a/util/hash/Cargo.toml +++ b/util/hash/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hash" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/util/instrument/Cargo.toml b/util/instrument/Cargo.toml index 5cb5fd35a7..214f29029f 100644 --- a/util/instrument/Cargo.toml +++ b/util/instrument/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-instrument" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/util/jsonrpc-types/Cargo.toml b/util/jsonrpc-types/Cargo.toml index 081d2ee37e..b42b283518 100644 --- a/util/jsonrpc-types/Cargo.toml +++ b/util/jsonrpc-types/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "jsonrpc-types" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/util/logger/Cargo.toml b/util/logger/Cargo.toml index d0a0571e70..d2862f9da0 100644 --- a/util/logger/Cargo.toml +++ b/util/logger/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "logger" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos "] edition = "2018" diff --git a/util/merkle-tree/Cargo.toml b/util/merkle-tree/Cargo.toml index 12619efbff..882cf1d9df 100644 --- a/util/merkle-tree/Cargo.toml +++ b/util/merkle-tree/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-merkle-tree" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/util/occupied-capacity-derive/Cargo.toml b/util/occupied-capacity-derive/Cargo.toml index cd99d24066..c2ae347434 100644 --- a/util/occupied-capacity-derive/Cargo.toml +++ b/util/occupied-capacity-derive/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "occupied-capacity-derive" -version = "0.7.0" +version = "0.8.0-pre" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/util/occupied-capacity/Cargo.toml b/util/occupied-capacity/Cargo.toml index 944ecf8953..410c51b105 100644 --- a/util/occupied-capacity/Cargo.toml +++ b/util/occupied-capacity/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "occupied-capacity" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/util/stop-handler/Cargo.toml b/util/stop-handler/Cargo.toml index 3c64fa4e6e..989766b4c5 100644 --- a/util/stop-handler/Cargo.toml +++ b/util/stop-handler/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "stop-handler" -version = "0.7.0" +version = "0.8.0-pre" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/verification/Cargo.toml b/verification/Cargo.toml index 3967d63dd1..ee550b07a2 100644 --- a/verification/Cargo.toml +++ b/verification/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-verification" -version = "0.7.0" +version = "0.8.0-pre" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" From f9abb11cd0fc2407648fcb9e26d3d843181b3a8d Mon Sep 17 00:00:00 2001 From: jjy Date: Tue, 26 Mar 2019 10:24:29 +0800 Subject: [PATCH 89/99] refactor: change Behaviour to an enum --- network/src/behaviour.rs | 38 +++++++++++--------- network/src/lib.rs | 2 +- network/src/peer_store.rs | 4 +-- network/src/peer_store/sqlite/peer_store.rs | 6 ++-- network/src/service/ckb_service.rs | 6 ++-- network/src/service/ping_service.rs | 8 ++--- network/src/tests/peers_registry.rs | 19 +++++----- network/src/tests/sqlite_peer_store.rs | 11 +++--- sync/src/relayer/mod.rs | 4 +-- sync/src/synchronizer/get_headers_process.rs | 4 +-- sync/src/synchronizer/mod.rs | 6 ++-- 11 files changed, 56 insertions(+), 52 deletions(-) diff --git a/network/src/behaviour.rs b/network/src/behaviour.rs index 0bd639a35e..d9349a25f2 100644 --- a/network/src/behaviour.rs +++ b/network/src/behaviour.rs @@ -1,21 +1,27 @@ use crate::Score; -pub type Behaviour = (Score, &'static str); -macro_rules! define_behaviour { - ( $( $name:ident => $score:expr ),* ) => { - $( - pub const $name: Behaviour = ($score,"$name"); - )* - }; +#[derive(Debug, Clone, Copy, Eq, PartialEq)] +pub enum Behaviour { + Connect, + Ping, + FailedToPing, + Timeout, + SyncUseless, + UnexpectedMessage, + UnexpectedDisconnect, } -// Define behaviours and scores -define_behaviour! { - CONNECT => 10, - PING => 10, - FAILED_TO_PING => -20, - TIMEOUT => -20, - SYNC_USELESS => -50, - UNEXPECTED_MESSAGE => -50, - UNEXPECTED_DISCONNECT => -10 +impl Behaviour { + pub fn score(self) -> Score { + use Behaviour::*; + match self { + Connect => 10, + Ping => 10, + FailedToPing => -20, + Timeout => -20, + SyncUseless => -50, + UnexpectedMessage => -50, + UnexpectedDisconnect => -10, + } + } } diff --git a/network/src/lib.rs b/network/src/lib.rs index fb9d06f605..063f2c59c8 100644 --- a/network/src/lib.rs +++ b/network/src/lib.rs @@ -1,6 +1,6 @@ #[macro_use] pub extern crate futures; -pub mod behaviour; +mod behaviour; pub mod errors; mod network; mod network_config; diff --git a/network/src/peer_store.rs b/network/src/peer_store.rs index 1175820a65..108bced7ee 100644 --- a/network/src/peer_store.rs +++ b/network/src/peer_store.rs @@ -1,7 +1,7 @@ pub mod sqlite; -pub(crate) use crate::PeerId; pub use crate::{peer_store::sqlite::SqlitePeerStore, SessionType}; +pub(crate) use crate::{Behaviour, PeerId}; use p2p::multiaddr::Multiaddr; use std::time::Duration; @@ -35,7 +35,7 @@ pub trait PeerStore: Send + Sync { /// this method will assume peer and addr is untrust since we have not connected to it. fn add_discovered_addr(&mut self, peer_id: &PeerId, address: Multiaddr) -> bool; /// Report peer behaviours - fn report(&mut self, peer_id: &PeerId, behaviour: (Score, &str)) -> ReportResult; + fn report(&mut self, peer_id: &PeerId, behaviour: Behaviour) -> ReportResult; /// Update peer status fn update_status(&mut self, peer_id: &PeerId, status: Status); fn peer_status(&self, peer_id: &PeerId) -> Status; diff --git a/network/src/peer_store/sqlite/peer_store.rs b/network/src/peer_store/sqlite/peer_store.rs index 3138fd6388..685e0db82b 100644 --- a/network/src/peer_store/sqlite/peer_store.rs +++ b/network/src/peer_store/sqlite/peer_store.rs @@ -12,7 +12,7 @@ use crate::peer_store::sqlite::{self, db, ConnectionPool, ConnectionPoolExt, DBE /// score. /// 4. Good peers can get higher score than bad peers. use crate::peer_store::{ - Multiaddr, PeerId, PeerScoreConfig, PeerStore, ReportResult, Score, Status, + Behaviour, Multiaddr, PeerId, PeerScoreConfig, PeerStore, ReportResult, Score, Status, }; use crate::SessionType; use faketime::unix_time; @@ -248,12 +248,12 @@ impl PeerStore for SqlitePeerStore { inserted > 0 } - fn report(&mut self, peer_id: &PeerId, (score_offset, _reason): (Score, &str)) -> ReportResult { + fn report(&mut self, peer_id: &PeerId, behaviour: Behaviour) -> ReportResult { if self.is_banned(peer_id) { return ReportResult::Banned; } let peer = self.get_or_insert_peer_info(peer_id); - let score = peer.score.saturating_add(score_offset); + let score = peer.score.saturating_add(behaviour.score()); if score < self.peer_score_config.ban_score { self.ban_peer(peer_id, self.peer_score_config.ban_timeout); return ReportResult::Banned; diff --git a/network/src/service/ckb_service.rs b/network/src/service/ckb_service.rs index e97df2219f..5e9b22342b 100644 --- a/network/src/service/ckb_service.rs +++ b/network/src/service/ckb_service.rs @@ -1,5 +1,5 @@ use crate::protocol_handler::DefaultCKBProtocolContext; -use crate::{behaviour, peer_store::Status}; +use crate::{peer_store::Status, Behaviour}; use crate::{peers_registry::RegisterResult, CKBEvent, CKBProtocolHandler, Network, PeerId}; use futures::{sync::mpsc::Receiver, Async, Stream}; use log::{debug, error, info}; @@ -49,7 +49,7 @@ impl Stream for CKBService { // update status in peer_store if let RegisterResult::New(_) = register_result { let mut peer_store = network.peer_store().write(); - peer_store.report(&peer_id, behaviour::CONNECT); + peer_store.report(&peer_id, Behaviour::Connect); peer_store.update_status(&peer_id, Status::Connected); let _ = peer_store.add_discovered_addr(&peer_id, addr); } @@ -78,7 +78,7 @@ impl Stream for CKBService { // update disconnect in peer_store { let mut peer_store = network.peer_store().write(); - peer_store.report(&peer_id, behaviour::UNEXPECTED_DISCONNECT); + peer_store.report(&peer_id, Behaviour::UnexpectedDisconnect); peer_store.update_status(&peer_id, Status::Disconnected); } if let Some(peer_index) = network.get_peer_index(&peer_id) { diff --git a/network/src/service/ping_service.rs b/network/src/service/ping_service.rs index 4acee85181..c2933c8baa 100644 --- a/network/src/service/ping_service.rs +++ b/network/src/service/ping_service.rs @@ -1,4 +1,4 @@ -use crate::behaviour; +use crate::Behaviour; use crate::Network; use futures::{sync::mpsc::Receiver, Async, Stream}; use log::{debug, trace}; @@ -27,16 +27,16 @@ impl Stream for PingService { peer.ping = Some(duration); peer.last_ping_time = Some(Instant::now()); }); - self.network.report(&peer_id, behaviour::PING); + self.network.report(&peer_id, Behaviour::Ping); } Some(Timeout(peer_id)) => { debug!(target: "network", "timeout to ping {:?}", peer_id); - self.network.report(&peer_id, behaviour::FAILED_TO_PING); + self.network.report(&peer_id, Behaviour::FailedToPing); self.network.drop_peer(&peer_id); } Some(UnexpectedError(peer_id)) => { debug!(target: "network", "failed to ping {:?}", peer_id); - self.network.report(&peer_id, behaviour::FAILED_TO_PING); + self.network.report(&peer_id, Behaviour::FailedToPing); self.network.drop_peer(&peer_id); } None => { diff --git a/network/src/tests/peers_registry.rs b/network/src/tests/peers_registry.rs index 91e18a0c4b..06ce1c42c2 100644 --- a/network/src/tests/peers_registry.rs +++ b/network/src/tests/peers_registry.rs @@ -1,9 +1,8 @@ use crate::{ - behaviour, multiaddr::ToMultiaddr, peer_store::{PeerStore, SqlitePeerStore}, peers_registry::{PeersRegistry, Session, EVICTION_PROTECT_PEERS}, - PeerId, SessionType, + Behaviour, PeerId, SessionType, }; use ckb_util::RwLock; use std::sync::Arc; @@ -120,8 +119,8 @@ fn test_accept_inbound_peer_eviction() { let mut peer_store = peer_store.write(); for _ in 0..EVICTION_PROTECT_PEERS { let peer_id = peers_iter.next().unwrap(); - peer_store.report(&peer_id, behaviour::PING); - peer_store.report(&peer_id, behaviour::PING); + peer_store.report(&peer_id, Behaviour::Ping); + peer_store.report(&peer_id, Behaviour::Ping); } } // lowest ping peers @@ -169,12 +168,12 @@ fn test_accept_inbound_peer_eviction() { // setup score { let mut peer_store = peer_store.write(); - peer_store.report(&lowest_score_peer, behaviour::FAILED_TO_PING); - peer_store.report(&lowest_score_peer, behaviour::FAILED_TO_PING); - peer_store.report(&lowest_score_peer, behaviour::FAILED_TO_PING); - peer_store.report(&reserved_peer, behaviour::FAILED_TO_PING); - peer_store.report(&reserved_peer, behaviour::FAILED_TO_PING); - peer_store.report(&evict_target, behaviour::FAILED_TO_PING); + peer_store.report(&lowest_score_peer, Behaviour::FailedToPing); + peer_store.report(&lowest_score_peer, Behaviour::FailedToPing); + peer_store.report(&lowest_score_peer, Behaviour::FailedToPing); + peer_store.report(&reserved_peer, Behaviour::FailedToPing); + peer_store.report(&reserved_peer, Behaviour::FailedToPing); + peer_store.report(&evict_target, Behaviour::FailedToPing); } // make sure other peers should not protected by longest connection time rule new_peer_ids.extend_from_slice(&[ diff --git a/network/src/tests/sqlite_peer_store.rs b/network/src/tests/sqlite_peer_store.rs index 0a8087b9d1..554bbc34cd 100644 --- a/network/src/tests/sqlite_peer_store.rs +++ b/network/src/tests/sqlite_peer_store.rs @@ -1,13 +1,12 @@ use crate::peer_store::sqlite::ConnectionPoolExt; use crate::{ - behaviour, multiaddr::ToMultiaddr, peer_store::{ sqlite::db, sqlite::peer_store::{PEER_NOT_SEEN_TIMEOUT_SECS, PEER_STORE_LIMIT}, PeerStore, SqlitePeerStore, Status, }, - PeerId, SessionType, + Behaviour, PeerId, SessionType, }; use rand::Rng; use std::time::Duration; @@ -43,7 +42,7 @@ fn test_add_discovered_addr() { fn test_report() { let mut peer_store: Box = Box::new(new_peer_store()); let peer_id = PeerId::random(); - assert!(peer_store.report(&peer_id, behaviour::PING).is_ok()); + assert!(peer_store.report(&peer_id, Behaviour::Ping).is_ok()); assert!( peer_store.peer_score(&peer_id).expect("peer score") > peer_store.peer_score_config().default_score @@ -155,9 +154,9 @@ fn test_delete_peer_info() { peer_store.add_connected_peer(&evict_target, addr1.clone(), SessionType::Server); peer_store.add_connected_peer(&fake_target, addr2, SessionType::Server); } - peer_store.report(&evict_target, behaviour::FAILED_TO_PING); - peer_store.report(&fake_target, behaviour::FAILED_TO_PING); - peer_store.report(&fake_target, behaviour::FAILED_TO_PING); + peer_store.report(&evict_target, Behaviour::FailedToPing); + peer_store.report(&fake_target, Behaviour::FailedToPing); + peer_store.report(&fake_target, Behaviour::FailedToPing); // evict_target has lower score than init score assert!( peer_store.peer_score(&evict_target).expect("peer store") diff --git a/sync/src/relayer/mod.rs b/sync/src/relayer/mod.rs index dcf9052c1f..fe5d2e25ef 100644 --- a/sync/src/relayer/mod.rs +++ b/sync/src/relayer/mod.rs @@ -21,7 +21,7 @@ use bytes::Bytes; use ckb_chain::chain::ChainController; use ckb_core::block::{Block, BlockBuilder}; use ckb_core::transaction::{ProposalShortId, Transaction}; -use ckb_network::{behaviour, CKBProtocolContext, CKBProtocolHandler, PeerIndex, TimerToken}; +use ckb_network::{Behaviour, CKBProtocolContext, CKBProtocolHandler, PeerIndex, TimerToken}; use ckb_protocol::{ cast, short_transaction_id, short_transaction_id_keys, RelayMessage, RelayPayload, }; @@ -128,7 +128,7 @@ where fn process(&self, nc: &CKBProtocolContext, peer: PeerIndex, message: RelayMessage) { if self.try_process(nc, peer, message).is_err() { - let _ = nc.report_peer(peer, behaviour::UNEXPECTED_MESSAGE); + let _ = nc.report_peer(peer, Behaviour::UnexpectedMessage); } } diff --git a/sync/src/synchronizer/get_headers_process.rs b/sync/src/synchronizer/get_headers_process.rs index ae0e548a85..60509fb9ae 100644 --- a/sync/src/synchronizer/get_headers_process.rs +++ b/sync/src/synchronizer/get_headers_process.rs @@ -1,7 +1,7 @@ use crate::synchronizer::Synchronizer; use crate::MAX_LOCATOR_SIZE; use ckb_core::header::Header; -use ckb_network::{behaviour, CKBProtocolContext, PeerIndex}; +use ckb_network::{Behaviour, CKBProtocolContext, PeerIndex}; use ckb_protocol::{cast, GetHeaders, SyncMessage}; use ckb_shared::index::ChainIndex; use ckb_util::TryInto; @@ -76,7 +76,7 @@ where warn!(target: "sync", "\n\nunknown block headers from peer {} {:#?}\n\n", self.peer, block_locator_hashes); // Got 'headers' message without known blocks // ban or close peers - let _ = self.nc.report_peer(self.peer, behaviour::SYNC_USELESS); + let _ = self.nc.report_peer(self.peer, Behaviour::SyncUseless); // disconnect peer anyway self.nc.disconnect(self.peer); } diff --git a/sync/src/synchronizer/mod.rs b/sync/src/synchronizer/mod.rs index c261e9a372..a922e9451b 100644 --- a/sync/src/synchronizer/mod.rs +++ b/sync/src/synchronizer/mod.rs @@ -26,7 +26,7 @@ use ckb_chain::chain::ChainController; use ckb_chain_spec::consensus::Consensus; use ckb_core::block::Block; use ckb_core::header::{BlockNumber, Header}; -use ckb_network::{behaviour, CKBProtocolContext, CKBProtocolHandler, PeerIndex, TimerToken}; +use ckb_network::{Behaviour, CKBProtocolContext, CKBProtocolHandler, PeerIndex, TimerToken}; use ckb_protocol::{cast, SyncMessage, SyncPayload}; use ckb_shared::index::ChainIndex; use ckb_shared::shared::Shared; @@ -182,7 +182,7 @@ impl Synchronizer { fn process(&self, nc: &CKBProtocolContext, peer: PeerIndex, message: SyncMessage) { if self.try_process(nc, peer, message).is_err() { - let _ = nc.report_peer(peer, behaviour::UNEXPECTED_MESSAGE); + let _ = nc.report_peer(peer, Behaviour::UnexpectedMessage); } } @@ -622,7 +622,7 @@ impl Synchronizer { } for peer in eviction { warn!(target: "sync", "timeout eviction peer={}", peer); - let _ = nc.report_peer(peer, behaviour::TIMEOUT); + let _ = nc.report_peer(peer, Behaviour::Timeout); } } From 94414115e75424dcff1975cdf92c5eca636c6899 Mon Sep 17 00:00:00 2001 From: zhangsoledad <787953403@qq.com> Date: Tue, 26 Mar 2019 10:26:01 +0800 Subject: [PATCH 90/99] ci: bors longer timeout --- bors.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/bors.toml b/bors.toml index 4ee923aedd..f1de09d545 100644 --- a/bors.toml +++ b/bors.toml @@ -2,3 +2,4 @@ status = [ "continuous-integration/travis-ci/push" ] required_approvals = 1 +timeout_sec = 14400 From 89104f5c2f41cbc3e2da1a457502b79a44a3666e Mon Sep 17 00:00:00 2001 From: zhangsoledad <787953403@qq.com> Date: Tue, 26 Mar 2019 11:35:51 +0800 Subject: [PATCH 91/99] refactor: merge cell_set chain_state cell_provider --- chain/src/chain.rs | 4 +-- chain/src/tests/basic.rs | 55 ++++++++++++++++++++++++++++++++++++--- shared/src/chain_state.rs | 14 ++++++---- 3 files changed, 62 insertions(+), 11 deletions(-) diff --git a/chain/src/chain.rs b/chain/src/chain.rs index d91be481dc..90044694ac 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -458,9 +458,7 @@ impl ChainService { if found_error.is_none() { let mut seen_inputs = FnvHashSet::default(); - // TODO Q impl CellProvider for store, use chain_state as CellProvider here directly. - let cell_set_cp = OverlayCellProvider::new(chain_state.cell_set(), chain_state); - let cell_set_diff_cp = OverlayCellProvider::new(&cell_set_diff, &cell_set_cp); + let cell_set_diff_cp = OverlayCellProvider::new(&cell_set_diff, chain_state); let block_cp = BlockCellProvider::new(b); let cell_provider = OverlayCellProvider::new(&block_cp, &cell_set_diff_cp); diff --git a/chain/src/tests/basic.rs b/chain/src/tests/basic.rs index e8c3ab3ab6..3a0c9dac00 100644 --- a/chain/src/tests/basic.rs +++ b/chain/src/tests/basic.rs @@ -2,7 +2,7 @@ use crate::tests::util::{create_transaction, gen_block, start_chain}; use ckb_chain_spec::consensus::Consensus; use ckb_core::block::Block; use ckb_core::block::BlockBuilder; -use ckb_core::cell::CellProvider; +use ckb_core::cell::{CellProvider, CellStatus}; use ckb_core::header::HeaderBuilder; use ckb_core::transaction::{CellInput, CellOutput, OutPoint, TransactionBuilder}; use ckb_shared::error::SharedError; @@ -28,6 +28,8 @@ fn test_genesis_transaction_spend() { let mut root_hash = tx.hash().clone(); + let genesis_tx_hash = root_hash.clone(); + let genesis_block = BlockBuilder::default() .commit_transaction(tx) .with_header_builder(HeaderBuilder::default().difficulty(U256::from(1000u64))); @@ -59,6 +61,14 @@ fn test_genesis_transaction_spend() { .process_block(Arc::new(block.clone())) .is_ok()); } + + assert_eq!( + shared + .chain_state() + .lock() + .cell(&OutPoint::new(genesis_tx_hash, 0)), + CellStatus::Dead + ); } #[test] @@ -80,9 +90,30 @@ fn test_transaction_spend_in_same_block() { } let last_cell_base = &chain.last().unwrap().commit_transactions()[0]; - let tx1 = create_transaction(last_cell_base.hash(), 1); - let tx2 = create_transaction(tx1.hash(), 2); + let last_cell_base_hash = last_cell_base.hash().clone(); + let tx1 = create_transaction(last_cell_base_hash.clone(), 1); + let tx1_hash = tx1.hash().clone(); + let tx2 = create_transaction(tx1_hash.clone(), 2); + let tx2_hash = tx2.hash().clone(); + let tx2_output = tx2.outputs()[0].clone(); + let txs = vec![tx1, tx2]; + + for hash in [ + last_cell_base_hash.clone(), + tx1_hash.clone(), + tx2_hash.clone(), + ] + .iter() + { + assert_eq!( + shared + .chain_state() + .lock() + .cell(&OutPoint::new(hash.clone(), 0)), + CellStatus::Unknown + ); + } // proposal txs { let difficulty = parent.difficulty().clone(); @@ -126,6 +157,24 @@ fn test_transaction_spend_in_same_block() { .process_block(Arc::new(block.clone())) .expect("process block ok"); } + + for hash in [last_cell_base_hash, tx1_hash].iter() { + assert_eq!( + shared + .chain_state() + .lock() + .cell(&OutPoint::new(hash.clone(), 0)), + CellStatus::Dead + ); + } + + assert_eq!( + shared + .chain_state() + .lock() + .cell(&OutPoint::new(tx2_hash, 0)), + CellStatus::Live(tx2_output) + ); } #[test] diff --git a/shared/src/chain_state.rs b/shared/src/chain_state.rs index 153bf17ed5..e1c1065254 100644 --- a/shared/src/chain_state.rs +++ b/shared/src/chain_state.rs @@ -327,11 +327,15 @@ impl ChainState { impl CellProvider for ChainState { fn cell(&self, out_point: &OutPoint) -> CellStatus { - match self.store.get_transaction(&out_point.hash) { - Some(tx) => match tx.outputs().get(out_point.index as usize) { - Some(output) => CellStatus::Live(output.clone()), - None => CellStatus::Unknown, - }, + match self.is_dead(out_point) { + Some(true) => CellStatus::Dead, + Some(false) => { + let tx = self + .store + .get_transaction(&out_point.hash) + .expect("store should be consistent with cell_set"); + CellStatus::Live(tx.outputs()[out_point.index as usize].clone()) + } None => CellStatus::Unknown, } } From 5bbb35ff33e385a1b45eed60506096f3ea5517cb Mon Sep 17 00:00:00 2001 From: jjy Date: Tue, 26 Mar 2019 18:40:16 +0800 Subject: [PATCH 92/99] refactor: network config --- network/src/lib.rs | 68 --------- network/src/network.rs | 28 ++-- network/src/network_config.rs | 160 ++++++++++------------ nodes_template/default.toml | 15 +- src/cli/run_impl.rs | 4 +- src/setup.rs | 7 +- test/fixtures/nodes_template/default.toml | 16 ++- 7 files changed, 103 insertions(+), 195 deletions(-) diff --git a/network/src/lib.rs b/network/src/lib.rs index 063f2c59c8..f3c6f49429 100644 --- a/network/src/lib.rs +++ b/network/src/lib.rs @@ -30,74 +30,6 @@ pub(crate) use p2p::{ context::{ServiceContext, SessionContext}, service::ServiceControl, }; -use serde_derive::Deserialize; -use std::time::Duration; - -const DEFAULT_OUTGOING_PEERS_RATIO: u32 = 3; // used in CKBProtocolContext pub type PeerIndex = usize; - -#[derive(Clone, Debug, PartialEq, Deserialize)] -pub struct Config { - pub listen_addresses: Vec, - pub secret_file: String, - pub peer_store_path: String, - pub try_outbound_connect_secs: Option, - /// List of initial node addresses - pub bootnodes: Vec, - /// List of reserved node addresses. - pub reserved_nodes: Vec, - /// The non-reserved peer mode. - pub non_reserved_mode: Option, - /// Minimum number of connected peers to maintain - pub max_peers: u32, - pub outbound_peers_ratio: Option, - pub config_dir_path: Option, -} - -impl Config { - fn max_outbound_peers(&self) -> u32 { - self.max_peers - / self - .outbound_peers_ratio - .unwrap_or_else(|| DEFAULT_OUTGOING_PEERS_RATIO) - } - fn max_inbound_peers(&self) -> u32 { - self.max_peers - self.max_outbound_peers() - } -} - -impl From for NetworkConfig { - fn from(config: Config) -> Self { - let mut cfg = NetworkConfig::default(); - cfg.max_outbound_peers = config.max_outbound_peers(); - cfg.max_inbound_peers = config.max_inbound_peers(); - cfg.listen_addresses = config.listen_addresses; - cfg.bootnodes = config.bootnodes; - cfg.reserved_peers = config.reserved_nodes; - if let Some(try_outbound_connect_secs) = config.try_outbound_connect_secs { - cfg.try_outbound_connect_interval = Duration::from_secs(try_outbound_connect_secs); - } - if let Some(value) = config.non_reserved_mode { - cfg.reserved_only = match value.as_str() { - "Accept" => false, - "Deny" => true, - _ => false, - }; - } - if let Some(dir_path) = &config.config_dir_path { - cfg.secret_key_path = Some(format!("{}/{}", dir_path, config.secret_file)); - cfg.peer_store_path = Some(format!("{}/{}", dir_path, config.peer_store_path)); - } - cfg.client_version = "ckb network".to_string(); - match cfg.read_secret_key() { - Some(raw_key) => cfg.secret_key = Some(raw_key), - None => { - cfg.generate_random_key().expect("generate random key"); - cfg.write_secret_key_to_file().expect("write random key"); - } - } - cfg - } -} diff --git a/network/src/network.rs b/network/src/network.rs index 8352d17fbe..43208b4560 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -1,4 +1,4 @@ -use crate::errors::{ConfigError, Error, PeerError, ProtocolError}; +use crate::errors::{Error, PeerError, ProtocolError}; use crate::peer_store::{sqlite::SqlitePeerStore, PeerStore}; use crate::peers_registry::{ ConnectionStatus, Peer, PeerIdentifyInfo, PeersRegistry, RegisterResult, Session, @@ -354,10 +354,8 @@ impl Network { config: &NetworkConfig, ckb_protocols: CKBProtocols, ) -> Result<(Arc, P2PService, TimerRegistry, EventReceivers), Error> { - let local_private_key = match config.fetch_private_key() { - Some(private_key) => private_key?, - None => return Err(ConfigError::InvalidKey.into()), - }; + config.create_dir_if_not_exists()?; + let local_private_key = config.fetch_private_key()?; // set max score to public addresses let listened_addresses: FnvHashMap = config .public_addresses @@ -365,10 +363,8 @@ impl Network { .map(|addr| (addr.to_owned(), std::u8::MAX)) .collect(); let peer_store: Arc> = { - let mut peer_store = match &config.peer_store_path { - Some(path) => SqlitePeerStore::file(path.to_string())?, - None => SqlitePeerStore::memory("default".to_string())?, - }; + let mut peer_store = + SqlitePeerStore::file(config.peer_store_path().to_string_lossy().to_string())?; let bootnodes = config.bootnodes()?; for (peer_id, addr) in bootnodes { peer_store.add_bootnode(peer_id, addr); @@ -382,8 +378,8 @@ impl Network { .collect::>(); let peers_registry = PeersRegistry::new( Arc::clone(&peer_store), - config.max_inbound_peers, - config.max_outbound_peers, + config.max_inbound_peers(), + config.max_outbound_peers(), config.reserved_only, reserved_peers, ); @@ -394,8 +390,8 @@ impl Network { .service_handle(move || { ProtocolHandle::Callback(Box::new(PingHandler::new( PING_PROTOCOL_ID, - config.ping_interval, - config.ping_timeout, + Duration::from_secs(config.ping_interval_secs), + Duration::from_secs(config.ping_timeout_secs), ping_sender, ))) }) @@ -539,8 +535,10 @@ impl Network { event_receiver: ckb_event_receiver, }; let timer_service = TimerService::new(timer_registry, Arc::clone(&network)); - let outbound_peer_service = - OutboundPeerService::new(Arc::clone(&network), config.try_outbound_connect_interval); + let outbound_peer_service = OutboundPeerService::new( + Arc::clone(&network), + Duration::from_secs(config.connect_outbound_interval_secs), + ); // prepare services futures let futures: Vec + Send>> = vec![ Box::new( diff --git a/network/src/network_config.rs b/network/src/network_config.rs index f0c234acc7..8b370cfbac 100644 --- a/network/src/network_config.rs +++ b/network/src/network_config.rs @@ -1,93 +1,102 @@ use crate::errors::{ConfigError, Error}; use crate::PeerId; -use bytes::Bytes; use log::info; use p2p::multiaddr::{Multiaddr, Protocol, ToMultiaddr}; use rand; use rand::Rng; use secio; +use serde_derive::Deserialize; use std::fs; -use std::io::Error as IoError; use std::io::Read; use std::io::Write; -use std::iter; -use std::net::Ipv4Addr; -use std::time::Duration; +use std::path::PathBuf; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Deserialize)] pub struct NetworkConfig { pub listen_addresses: Vec, pub public_addresses: Vec, - pub client_version: String, - pub protocol_version: String, + pub bootnodes: Vec, + pub reserved_peers: Vec, pub reserved_only: bool, - pub max_inbound_peers: u32, + pub max_peers: u32, pub max_outbound_peers: u32, - pub reserved_peers: Vec, - pub secret_key: Option, - pub secret_key_path: Option, - pub peer_store_path: Option, - pub config_dir_path: Option, - pub bootnodes: Vec, - pub ping_interval: Duration, - pub ping_timeout: Duration, - pub discovery_timeout: Duration, - pub discovery_response_count: usize, - pub discovery_interval: Duration, - pub try_outbound_connect_interval: Duration, + pub config_dir_path: PathBuf, + pub ping_interval_secs: u64, + pub ping_timeout_secs: u64, + pub connect_outbound_interval_secs: u64, } -impl NetworkConfig { - pub(crate) fn read_secret_key(&self) -> Option { - if self.secret_key.is_some() { - self.secret_key.clone() - } else if let Some(ref path) = self.secret_key_path { - match fs::File::open(path).and_then(|mut file| { - let mut buf = Vec::new(); - file.read_to_end(&mut buf).map(|_| buf) - }) { - Ok(secret) => Some(secret.into()), - Err(_err) => None, - } - } else { - None +fn generate_random_key() -> [u8; 32] { + loop { + let mut key: [u8; 32] = [0; 32]; + rand::thread_rng().fill(&mut key); + if secio::SecioKeyPair::secp256k1_raw_key(&key).is_ok() { + return key; } } +} - pub fn new() -> Self { - Self::default() +impl NetworkConfig { + pub fn secret_key_path(&self) -> PathBuf { + let mut path = self.config_dir_path.clone(); + path.set_file_name("secret_key"); + path } - pub fn generate_random_key(&mut self) -> Result { - info!(target: "network", "Generate random key"); - let mut key: [u8; 32] = [0; 32]; - rand::thread_rng().fill(&mut key); - self.secret_key = Some(Bytes::from(key.to_vec())); - secio::SecioKeyPair::secp256k1_raw_key(&key).map_err(|_err| ConfigError::InvalidKey.into()) + pub fn peer_store_path(&self) -> PathBuf { + let mut path = self.config_dir_path.clone(); + path.set_file_name("peer_store.db"); + path } - pub fn write_secret_key_to_file(&mut self) -> Result<(), IoError> { - if let Some(ref secret_key_path) = self.secret_key_path { - if let Some(secret_key) = self.secret_key.clone() { - info!(target: "network", "write random secret key to {}", secret_key_path); - return fs::OpenOptions::new() - .create(true) - .write(true) - .open(secret_key_path) - .and_then(|mut file| file.write_all(&secret_key)); - } + pub fn create_dir_if_not_exists(&self) -> Result<(), Error> { + if !self.config_dir_path.exists() { + fs::create_dir(&self.config_dir_path)?; } Ok(()) } - pub fn fetch_private_key(&self) -> Option> { - if let Some(secret) = self.read_secret_key() { - Some( - secio::SecioKeyPair::secp256k1_raw_key(&secret) - .map_err(|_err| ConfigError::InvalidKey.into()), - ) - } else { - None + pub fn max_inbound_peers(&self) -> u32 { + self.max_peers - self.max_outbound_peers + } + + pub fn max_outbound_peers(&self) -> u32 { + self.max_outbound_peers + } + + fn read_secret_key(&self) -> Result, Error> { + let path = self.secret_key_path(); + let mut file = match fs::File::open(path) { + Ok(file) => file, + Err(_) => return Ok(None), + }; + let mut buf = Vec::new(); + file.read_to_end(&mut buf)?; + Ok(Some(secio::SecioKeyPair::secp256k1_raw_key(&buf).map_err( + |_err: secio::error::SecioError| ConfigError::InvalidKey, + )?)) + } + + fn write_secret_key_to_file(&self) -> Result<(), Error> { + let path = self.secret_key_path(); + info!(target: "network", "Generate random key"); + let random_key_pair = generate_random_key(); + info!(target: "network", "write random secret key to {:?}", path); + fs::OpenOptions::new() + .create(true) + .write(true) + .open(path) + .and_then(|mut file| file.write_all(&random_key_pair)) + .map_err(Into::into) + } + + pub fn fetch_private_key(&self) -> Result { + match self.read_secret_key()? { + Some(key) => Ok(key), + None => { + self.write_secret_key_to_file()?; + Ok(self.read_secret_key()?.expect("key must exists")) + } } } @@ -125,32 +134,3 @@ impl NetworkConfig { Ok(peers) } } - -impl Default for NetworkConfig { - fn default() -> Self { - NetworkConfig { - listen_addresses: vec![iter::once(Protocol::Ip4(Ipv4Addr::new(0, 0, 0, 0))) - .chain(iter::once(Protocol::Tcp(30333))) - .collect()], - public_addresses: Vec::new(), - client_version: "ckb".to_owned(), - protocol_version: "ckb".to_owned(), - reserved_only: false, - max_outbound_peers: 15, - max_inbound_peers: 10, - reserved_peers: vec![], - secret_key: None, - secret_key_path: None, - bootnodes: vec![], - config_dir_path: None, - // protocol services config - ping_interval: Duration::from_secs(15), - ping_timeout: Duration::from_secs(20), - discovery_timeout: Duration::from_secs(20), - discovery_response_count: 20, - discovery_interval: Duration::from_secs(15), - try_outbound_connect_interval: Duration::from_secs(15), - peer_store_path: None, - } - } -} diff --git a/nodes_template/default.toml b/nodes_template/default.toml index 4274b55df6..0935ae0455 100644 --- a/nodes_template/default.toml +++ b/nodes_template/default.toml @@ -13,13 +13,16 @@ color = true [network] listen_addresses = ["/ip4/0.0.0.0/tcp/8115"] +public_addresses = [] bootnodes = [] -reserved_nodes = [] -only_reserved_peers = false -min_peers = 4 -max_peers = 8 -secret_file = "secret_key" -peer_store_path = "peer_store.db" +reserved_peers = [] +reserved_only = false +max_peers = 125 +max_outbound_peers = 30 +config_dir_path = "default/network" +ping_interval_secs = 15 +ping_timeout_secs = 20 +connect_outbound_interval_secs = 15 [rpc] listen_address = "0.0.0.0:8114" diff --git a/src/cli/run_impl.rs b/src/cli/run_impl.rs index 6c43493cc7..48fca324e7 100644 --- a/src/cli/run_impl.rs +++ b/src/cli/run_impl.rs @@ -6,7 +6,6 @@ use ckb_db::diskdb::RocksDB; use ckb_miner::BlockAssembler; use ckb_network::futures::sync::mpsc::channel; use ckb_network::CKBProtocol; -use ckb_network::NetworkConfig; use ckb_network::NetworkService; use ckb_network::ProtocolId; use ckb_notify::{NotifyController, NotifyService}; @@ -55,7 +54,6 @@ pub fn run(setup: Setup) { let net_time_checker = Arc::new(NetTimeProtocol::default()); - let network_config = NetworkConfig::from(setup.configs.network); let (sender, receiver) = channel(std::u8::MAX as usize); let protocols = vec![ ( @@ -87,7 +85,7 @@ pub fn run(setup: Setup) { ), ]; let network = Arc::new( - NetworkService::run_in_thread(&network_config, protocols, receiver) + NetworkService::run_in_thread(&setup.configs.network, protocols, receiver) .expect("Create and start network"), ); diff --git a/src/setup.rs b/src/setup.rs index acb1e4ed22..937b2162ac 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -2,7 +2,7 @@ use crate::helper::{require_path_exists, to_absolute_path}; use ckb_chain_spec::ChainSpec; use ckb_db::DBConfig; use ckb_miner::BlockAssemblerConfig; -use ckb_network::Config as NetworkConfig; +use ckb_network::NetworkConfig; use ckb_rpc::Config as RpcConfig; use ckb_shared::tx_pool::TxPoolConfig; use ckb_sync::Config as SyncConfig; @@ -71,11 +71,6 @@ impl Setup { configs.logger.file = Some(path.join(file)); } - if configs.network.config_dir_path.is_none() { - configs.network.config_dir_path = - Some(dirs.join("network").to_string_lossy().to_string()); - } - let chain_spec = ChainSpec::read_from_file(&configs.chain.spec).map_err(|e| { Box::new(ConfigError::Message(format!( "invalid chain spec {}, {}", diff --git a/test/fixtures/nodes_template/default.toml b/test/fixtures/nodes_template/default.toml index 06761c2344..687174da61 100644 --- a/test/fixtures/nodes_template/default.toml +++ b/test/fixtures/nodes_template/default.toml @@ -13,14 +13,16 @@ color = true [network] listen_addresses = ["/ip4/0.0.0.0/tcp/P2P_PORT"] +public_addresses = [] bootnodes = [] -reserved_nodes = [] -only_reserved_peers = false -min_peers = 4 -max_peers = 8 -secret_file = "secret_key" -peer_store_path = "peer_store.db" -try_outbound_connect_secs = 1 +reserved_peers = [] +reserved_only = false +max_peers = 125 +max_outbound_peers = 30 +config_dir_path = "default/network" +ping_interval_secs = 15 +ping_timeout_secs = 20 +connect_outbound_interval_secs = 1 [rpc] listen_address = "0.0.0.0:RPC_PORT" From 09d79fc139bf4775b932c69b46aa7da29daeca4f Mon Sep 17 00:00:00 2001 From: jjy Date: Wed, 27 Mar 2019 11:20:42 +0800 Subject: [PATCH 93/99] fix: network path dir --- network/src/network_config.rs | 10 +++++----- nodes_template/default.toml | 2 +- src/setup.rs | 3 +++ test/fixtures/nodes_template/default.toml | 2 +- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/network/src/network_config.rs b/network/src/network_config.rs index 8b370cfbac..d4a315af31 100644 --- a/network/src/network_config.rs +++ b/network/src/network_config.rs @@ -20,7 +20,7 @@ pub struct NetworkConfig { pub reserved_only: bool, pub max_peers: u32, pub max_outbound_peers: u32, - pub config_dir_path: PathBuf, + pub path: PathBuf, pub ping_interval_secs: u64, pub ping_timeout_secs: u64, pub connect_outbound_interval_secs: u64, @@ -38,20 +38,20 @@ fn generate_random_key() -> [u8; 32] { impl NetworkConfig { pub fn secret_key_path(&self) -> PathBuf { - let mut path = self.config_dir_path.clone(); + let mut path = self.path.clone(); path.set_file_name("secret_key"); path } pub fn peer_store_path(&self) -> PathBuf { - let mut path = self.config_dir_path.clone(); + let mut path = self.path.clone(); path.set_file_name("peer_store.db"); path } pub fn create_dir_if_not_exists(&self) -> Result<(), Error> { - if !self.config_dir_path.exists() { - fs::create_dir(&self.config_dir_path)?; + if !self.path.exists() { + fs::create_dir(&self.path)?; } Ok(()) } diff --git a/nodes_template/default.toml b/nodes_template/default.toml index 0935ae0455..629216d2a4 100644 --- a/nodes_template/default.toml +++ b/nodes_template/default.toml @@ -12,6 +12,7 @@ filter = "info" color = true [network] +path = "default/network" listen_addresses = ["/ip4/0.0.0.0/tcp/8115"] public_addresses = [] bootnodes = [] @@ -19,7 +20,6 @@ reserved_peers = [] reserved_only = false max_peers = 125 max_outbound_peers = 30 -config_dir_path = "default/network" ping_interval_secs = 15 ping_timeout_secs = 20 connect_outbound_interval_secs = 15 diff --git a/src/setup.rs b/src/setup.rs index 937b2162ac..035343912d 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -109,6 +109,9 @@ impl Configs { if self.db.path.is_relative() { self.db.path = base.join(&self.db.path); } + if self.network.path.is_relative() { + self.network.path = base.join(&self.network.path); + } } } diff --git a/test/fixtures/nodes_template/default.toml b/test/fixtures/nodes_template/default.toml index 687174da61..39a6356416 100644 --- a/test/fixtures/nodes_template/default.toml +++ b/test/fixtures/nodes_template/default.toml @@ -12,6 +12,7 @@ filter = "info,network=trace,rpc=debug,sync=debug,relay=debug" color = true [network] +path = "default/network" listen_addresses = ["/ip4/0.0.0.0/tcp/P2P_PORT"] public_addresses = [] bootnodes = [] @@ -19,7 +20,6 @@ reserved_peers = [] reserved_only = false max_peers = 125 max_outbound_peers = 30 -config_dir_path = "default/network" ping_interval_secs = 15 ping_timeout_secs = 20 connect_outbound_interval_secs = 1 From 4a1ecc7891ead519883bc4121db2a28c18586e47 Mon Sep 17 00:00:00 2001 From: jjy Date: Wed, 27 Mar 2019 12:56:23 +0800 Subject: [PATCH 94/99] fix: network path --- network/src/network_config.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/network/src/network_config.rs b/network/src/network_config.rs index d4a315af31..981aa5056c 100644 --- a/network/src/network_config.rs +++ b/network/src/network_config.rs @@ -39,13 +39,13 @@ fn generate_random_key() -> [u8; 32] { impl NetworkConfig { pub fn secret_key_path(&self) -> PathBuf { let mut path = self.path.clone(); - path.set_file_name("secret_key"); + path.push("secret_key"); path } pub fn peer_store_path(&self) -> PathBuf { let mut path = self.path.clone(); - path.set_file_name("peer_store.db"); + path.push("peer_store.db"); path } From c0992b15894a301ec040cb442745ace39db4ae1a Mon Sep 17 00:00:00 2001 From: zhangsoledad <787953403@qq.com> Date: Wed, 27 Mar 2019 17:52:50 +0800 Subject: [PATCH 95/99] fix: trace rpc addition: * remove integration tests from root workspace * fix integration tests logger panic at flush --- Cargo.lock | 259 ---------------------- Cargo.toml | 3 +- devtools/ci/script.sh | 2 +- docker/hub/Dockerfile | 2 +- rpc/src/module/trace.rs | 2 +- shared/src/tx_pool/trace.rs | 12 +- src/cli/run_impl.rs | 10 - test/Cargo.toml | 5 +- test/fixtures/nodes_template/default.toml | 2 +- test/src/main.rs | 24 +- test/src/node.rs | 22 ++ test/src/rpc.rs | 3 + test/src/specs/block_relay.rs | 2 +- test/src/specs/block_sync.rs | 2 +- test/src/specs/mining.rs | 2 +- test/src/specs/mod.rs | 2 +- test/src/specs/pool.rs | 45 +++- test/src/specs/transaction_relay.rs | 2 +- 18 files changed, 105 insertions(+), 296 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3bf6492de7..e7b08b6137 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -100,15 +100,6 @@ dependencies = [ "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "base64" -version = "0.9.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "safemem 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "bincode" version = "1.1.2" @@ -644,23 +635,6 @@ dependencies = [ "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "ckb-test" -version = "0.8.0-pre" -dependencies = [ - "ckb-core 0.8.0-pre", - "ckb-network 0.8.0-pre", - "fs_extra 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "jsonrpc-client-core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", - "jsonrpc-client-http 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", - "jsonrpc-types 0.8.0-pre", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "logger 0.8.0-pre", - "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", - "tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "ckb-traits" version = "0.8.0-pre" @@ -1033,14 +1007,6 @@ dependencies = [ "termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "error-chain" -version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "backtrace 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "failure" version = "0.1.5" @@ -1097,11 +1063,6 @@ name = "fnv" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "fs_extra" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "fuchsia-cprng" version = "0.1.1" @@ -1254,32 +1215,6 @@ dependencies = [ "quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "hyper" -version = "0.11.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", - "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "futures-cpupool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", - "httparse 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)", - "iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "mime 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)", - "net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", - "percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "relay 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-proto 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-service 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "unicase 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "want 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "hyper" version = "0.12.25" @@ -1356,44 +1291,6 @@ name = "itoa" version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "jsonrpc-client-core" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "jsonrpc-core 8.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "jsonrpc-client-http" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "hyper 0.11.27 (registry+https://github.com/rust-lang/crates.io-index)", - "jsonrpc-client-core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "jsonrpc-core" -version = "8.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "jsonrpc-core" version = "10.1.0" @@ -1470,11 +1367,6 @@ dependencies = [ "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "language-tags" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "lazy_static" version = "0.2.11" @@ -1553,14 +1445,6 @@ dependencies = [ "scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "log" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "log" version = "0.4.6" @@ -1625,14 +1509,6 @@ name = "merkle-cbt" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "mime" -version = "0.3.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "unicase 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "mio" version = "0.6.16" @@ -1970,27 +1846,6 @@ dependencies = [ "nibble_vec 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "rand" -version = "0.3.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "rand" -version = "0.4.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "rand" version = "0.5.6" @@ -2170,14 +2025,6 @@ dependencies = [ "ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "relay" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "remove_dir_all" version = "0.5.1" @@ -2253,11 +2100,6 @@ name = "ryu" version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "safemem" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "same-file" version = "1.0.4" @@ -2274,11 +2116,6 @@ dependencies = [ "antidote 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "scoped-tls" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "scopeguard" version = "0.3.3" @@ -2403,21 +2240,11 @@ name = "siphasher" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "slab" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "slab" version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "smallvec" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "smallvec" version = "0.6.9" @@ -2525,11 +2352,6 @@ dependencies = [ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "take" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "tempfile" version = "3.0.7" @@ -2729,24 +2551,6 @@ dependencies = [ "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "tokio-core" -version = "0.1.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)", - "scoped-tls 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-executor 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-reactor 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-timer 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "tokio-current-thread" version = "0.1.6" @@ -2785,23 +2589,6 @@ dependencies = [ "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "tokio-proto" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", - "net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.3.23 (registry+https://github.com/rust-lang/crates.io-index)", - "slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "smallvec 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "take 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-service 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "tokio-reactor" version = "0.1.9" @@ -2820,14 +2607,6 @@ dependencies = [ "tokio-sync 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "tokio-service" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "tokio-sync" version = "0.1.4" @@ -2988,11 +2767,6 @@ dependencies = [ "url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "try-lock" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "try-lock" version = "0.2.2" @@ -3129,16 +2903,6 @@ dependencies = [ "winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "want" -version = "0.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "try-lock 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "want" version = "0.0.6" @@ -3234,7 +2998,6 @@ dependencies = [ "checksum autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a6d640bee2da49f60a4068a7fae53acde8982514ab7bae8b8cea9e88cbcfd799" "checksum backtrace 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "cd5a90e2b463010cd0e0ce9a11d4a9d5d58d9f41d4a6ba3dcaf9e68b466e88b4" "checksum backtrace-sys 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)" = "797c830ac25ccc92a7f8a7b9862bde440715531514594a6154e3d4a54dd769b6" -"checksum base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "489d6c0ed21b11d038c31b6ceccca973e65d73ba3bd8ecb9a2babf5546164643" "checksum bincode 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3efe0b4c8eaeed8600549c29f538a6a11bf422858d0ed435b1d70ec4ab101190" "checksum bindgen 0.47.3 (registry+https://github.com/rust-lang/crates.io-index)" = "df683a55b54b41d5ea8ebfaebb5aa7e6b84e3f3006a78f010dadc9ca88469260" "checksum bit-set 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e84c238982c4b1e1ee668d136c510c67a13465279c0cb367ea6baf6310620a80" @@ -3287,7 +3050,6 @@ dependencies = [ "checksum encode_unicode 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "90b2c9496c001e8cb61827acdefad780795c42264c137744cae6f7d9e3450abd" "checksum endian-type 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" "checksum env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b61fa891024a945da30a9581546e8cfaf5602c7b3f4c137a2805cf388f92075a" -"checksum error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "07e791d3be96241c77c43846b665ef1384606da2cd2a48730abe606a12906e02" "checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2" "checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1" "checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" @@ -3296,7 +3058,6 @@ dependencies = [ "checksum faster-hex 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a5b8cccaafb5aae8c282692e5590f341925edea6c696e8715ff0d973320b2646" "checksum flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ea0c34f669be9911826facafe996adfda978aeee67285a13556869e2d8b8331f" "checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3" -"checksum fs_extra 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5f2a4a2034423744d2cc7ca2068453168dcdb82c438419e639a26bd87839c674" "checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" @@ -3314,7 +3075,6 @@ dependencies = [ "checksum http 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "fe67e3678f2827030e89cc4b9e7ecd16d52f132c0b940ab5005f88e821500f6a" "checksum httparse 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e8734b0cfd3bc3e101ec59100e101c2eecd19282202e87808b3037b442777a83" "checksum humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ca7e5f2e110db35f93b837c81797f3714500b81d517bf20c431b16d3ca4f114" -"checksum hyper 0.11.27 (registry+https://github.com/rust-lang/crates.io-index)" = "34a590ca09d341e94cddf8e5af0bbccde205d5fbc2fa3c09dd67c7f85cea59d7" "checksum hyper 0.12.25 (registry+https://github.com/rust-lang/crates.io-index)" = "7d5b6658b016965ae301fa995306db965c93677880ea70765a84235a96eae896" "checksum idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e" "checksum indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7e81a7c05f79578dbc15793d8b619db9ba32b4577003ef3af1a91c416798c58d" @@ -3322,15 +3082,11 @@ dependencies = [ "checksum iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dbe6e417e7d0975db6512b90796e8ce223145ac4e33c377e4a42882a0e88bb08" "checksum itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5b8467d9c1cebe26feb08c640139247fac215782d35371ade9a2136ed6085358" "checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b" -"checksum jsonrpc-client-core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f29cb249837420fb0cee7fb0fbf1d22679e121b160e71bb5e0d90b9df241c23e" -"checksum jsonrpc-client-http 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2e642eb74423b9dfcb4512fda167148746b76f788a823cd712fadf409f31d302" "checksum jsonrpc-core 10.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "dc15eef5f8b6bef5ac5f7440a957ff95d036e2f98706947741bfc93d1976db4c" -"checksum jsonrpc-core 8.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ddf83704f4e79979a424d1082dd2c1e52683058056c9280efa19ac5f6bc9033c" "checksum jsonrpc-derive 10.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c2dae61ca8a3b047fb11309b00661bc56837085bd07e46f907b9c562c0b03e68" "checksum jsonrpc-http-server 10.0.1 (git+https://github.com/nervosnetwork/jsonrpc?branch=http_remake)" = "" "checksum jsonrpc-server-utils 10.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c9527f01ef25f251d64082cbefc0c6d6f367349afe6848ef908a674e06b2bdd3" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" -"checksum language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a" "checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73" "checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" "checksum lazycell 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f" @@ -3342,7 +3098,6 @@ dependencies = [ "checksum linked-hash-map 0.5.1 (git+https://github.com/nervosnetwork/linked-hash-map?rev=df27f21)" = "" "checksum linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ae91b68aebc4ddb91978b11a1b02ddd8602a05ec19002801c5666000e05e0f83" "checksum lock_api 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "62ebf1391f6acad60e5c8b43706dde4582df75c06698ab44511d15016bc2442c" -"checksum log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b" "checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6" "checksum lru-cache 0.1.0 (git+https://github.com/nervosnetwork/lru-cache)" = "" "checksum lru-cache 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "31e24f1ad8321ca0e8a1e0ac13f23cb668e6f5466c2c57319f6a5cf1cc8e3b1c" @@ -3350,7 +3105,6 @@ dependencies = [ "checksum memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2efc7bc57c883d4a4d6e3246905283d8dae951bb3bd32f49d6ef297f546e1c39" "checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3" "checksum merkle-cbt 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d589b5a7ca642540e7ccfbca3bcd0aa18693eb9287e2a6b17c79b1d062d52863" -"checksum mime 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)" = "3e27ca21f40a310bd06d9031785f4801710d566c184a6e15bad4f1d9b65f9425" "checksum mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)" = "71646331f2619b1026cc302f87a2b8b648d5c6dd6937846a16cc8ce0f347f432" "checksum mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "966257a94e196b11bb43aca423754d87429960a768de9414f3691d6957abf125" "checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" @@ -3387,8 +3141,6 @@ dependencies = [ "checksum r2d2 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)" = "5d746fc8a0dab19ccea7ff73ad535854e90ddb3b4b8cdce953dd5cd0b2e7bd22" "checksum r2d2_sqlite 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a7cba990b29ae565b1a765ef45f6b84a89a77736b91582e0243c12f613653857" "checksum radix_trie 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ebcf72e767017c1aa4b63d4dd0b0b836a243b648fd81d41c6bf6e850ef7a95c7" -"checksum rand 0.3.23 (registry+https://github.com/rust-lang/crates.io-index)" = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c" -"checksum rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" "checksum rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c618c47cd3ebd209790115ab837de41425723956ad3ce2e6a7f09890947cacb9" "checksum rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca" "checksum rand_chacha 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef" @@ -3408,7 +3160,6 @@ dependencies = [ "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" "checksum regex 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "53ee8cfdddb2e0291adfb9f13d31d3bbe0a03c9a402c01b1e24188d86c35b24f" "checksum regex-syntax 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8c2f35eedad5295fdf00a63d7d4b238135723f92b434ec06774dad15c7ab0861" -"checksum relay 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1576e382688d7e9deecea24417e350d3062d97e32e45d70b1cde65994ff1489a" "checksum remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3488ba1b9a2084d38645c4c08276a1752dcbf2c7130d74f1569681ad5d2799c5" "checksum ring 0.14.6 (registry+https://github.com/rust-lang/crates.io-index)" = "426bc186e3e95cac1e4a4be125a4aca7e84c2d616ffc02244eef36e2a60a093c" "checksum rocksdb 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "231ceea69907012c24a63315418c40da12d02ea7aecad9360fe18441976d0592" @@ -3418,10 +3169,8 @@ dependencies = [ "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" "checksum rusty-fork 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9591f190d2852720b679c21f66ad929f9f1d7bb09d1193c26167586029d8489c" "checksum ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9e9b8cde282a9fe6a42dd4681319bfb63f121b8a8ee9439c6f4107e58a46f7" -"checksum safemem 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8dca453248a96cb0749e36ccdfe2b0b4e54a61bfef89fb97ec621eb8e0a93dd9" "checksum same-file 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8f20c4be53a8a1ff4c1f1b2bd14570d2f634628709752f0702ecdd2b3f9a5267" "checksum scheduled-thread-pool 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1a2ff3fc5223829be817806c6441279c676e454cc7da608faf03b0ccc09d3889" -"checksum scoped-tls 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "332ffa32bf586782a3efaeb58f127980944bbc8c4d6913a86107ac2a5ab24b28" "checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" "checksum scroll 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2f84d114ef17fd144153d608fba7c446b0145d038985e7a8cc5d08bb0ce20383" "checksum scroll_derive 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8f1aa96c45e7f5a91cb7fabe7b279f02fea7126239fc40b732316e8b6a2d0fcb" @@ -3438,9 +3187,7 @@ dependencies = [ "checksum sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" "checksum sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7b4d8bfd0e469f417657573d8451fb33d16cfe0989359b93baf3a1ffc639543d" "checksum siphasher 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9913c75df657d84a03fa689c016b0bb2863ff0b497b26a8d6e9703f8d5df03a8" -"checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23" "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" -"checksum smallvec 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4c8cbcd6df1e117c2210e13ab5109635ad68a929fcbb8964dc965b76cb5ee013" "checksum smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c4488ae950c49d403731982257768f48fada354a5203fe81f9bb6f43ca9002be" "checksum snap 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "95d697d63d44ad8b78b8d235bf85b34022a78af292c8918527c5f0cffdde7f43" "checksum socket2 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "c4d11a52082057d87cb5caa31ad812f4504b97ab44732cd8359df2e9ff9f48e7" @@ -3454,7 +3201,6 @@ dependencies = [ "checksum syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)" = "1825685f977249735d510a242a6727b46efe914bb67e38d30c071b1b72b1d5c2" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015" -"checksum take 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b157868d8ac1f56b64604539990685fa7611d8fa9e5476cf0c02cf34d32917c5" "checksum tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "b86c784c88d98c801132806dadd3819ed29d8600836c4088e855cdf3e178ed8a" "checksum tentacle 0.2.0-alpha.1 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)" = "" "checksum tentacle-discovery 0.1.0 (git+https://github.com/nervosnetwork/p2p?rev=6c1f778914dae864d13c1d9110a08b46fb4d38fe)" = "" @@ -3471,14 +3217,11 @@ dependencies = [ "checksum tinytemplate 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7655088894274afb52b807bd3c87072daa1fedd155068b8705cabfd628956115" "checksum tokio 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "65641e515a437b308ab131a82ce3042ff9795bef5d6c5a9be4eb24195c417fd9" "checksum tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5c501eceaf96f0e1793cf26beb63da3d11c738c4a943fdf3746d81d64684c39f" -"checksum tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)" = "aeeffbbb94209023feaef3c196a41cbcdafa06b4a6f893f68779bb5e53796f71" "checksum tokio-current-thread 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "d16217cad7f1b840c5a97dfb3c43b0c871fef423a6e8d2118c604e843662a443" "checksum tokio-executor 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "83ea44c6c0773cc034771693711c35c677b4b5a4b21b9e7071704c54de7d555e" "checksum tokio-fs 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "3fe6dc22b08d6993916647d108a1a7d15b9cd29c4f4496c62b92c45b5041b7af" "checksum tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "5090db468dad16e1a7a54c8c67280c5e4b544f3d3e018f0b913b400261f85926" -"checksum tokio-proto 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8fbb47ae81353c63c487030659494b295f6cb6576242f907f203473b191b0389" "checksum tokio-reactor 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "6af16bfac7e112bea8b0442542161bfc41cbfa4466b580bdda7d18cb88b911ce" -"checksum tokio-service 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "24da22d077e0f15f55162bdbdc661228c1581892f52074fb242678d015b45162" "checksum tokio-sync 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "fda385df506bf7546e70872767f71e81640f1f251bdf2fd8eb81a0eaec5fe022" "checksum tokio-tcp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1d14b10654be682ac43efee27401d792507e30fd8d26389e1da3b185de2e4119" "checksum tokio-threadpool 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "ec5759cf26cf9659555f36c431b515e3d05f66831741c85b4b5d5dfb9cf1323c" @@ -3491,7 +3234,6 @@ dependencies = [ "checksum toml 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "87c5890a989fa47ecdc7bcb4c63a77a82c18f306714104b1decfd722db17b39e" "checksum trust-dns 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)" = "65096825b064877da37eeeb9a83390bd23433eabfc503a6476dc5b1949034aa7" "checksum trust-dns-proto 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "09144f0992b0870fa8d2972cc069cbf1e3c0fda64d1f3d45c4d68d0e0b52ad4e" -"checksum try-lock 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee2aa4715743892880f70885373966c83d73ef1b0838a664ef0c76fffd35e7c2" "checksum try-lock 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" "checksum twofish 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712d261e83e727c8e2dbb75dacac67c36e35db36a958ee504f2164fc052434e1" "checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169" @@ -3513,7 +3255,6 @@ dependencies = [ "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" "checksum wait-timeout 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "b9f3bf741a801531993db6478b95682117471f76916f5e690dd8d45395b09349" "checksum walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9d9d7ed3431229a144296213105a390676cc49c9b6a72bd19f3176c98e129fa1" -"checksum want 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a05d9d966753fa4b5c8db73fcab5eed4549cfe0e1e4e66911e5564a0085c35d1" "checksum want 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "797464475f30ddb8830cc529aaaae648d581f99e2036a928877dfde027ddf6b3" "checksum which 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b57acb10231b9493c8472b20cb57317d0679a49e0bdbee44b3b803a6473af164" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" diff --git a/Cargo.toml b/Cargo.toml index 11a7fe13cb..278a02d004 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -70,6 +70,5 @@ members = [ "verification", "script", "pow", - "benches", - "test" + "benches" ] diff --git a/devtools/ci/script.sh b/devtools/ci/script.sh index 91f61bcb0f..ea731302d2 100755 --- a/devtools/ci/script.sh +++ b/devtools/ci/script.sh @@ -20,7 +20,7 @@ git diff --exit-code Cargo.lock if [ "$TRAVIS_BRANCH" = master -o "$TRAVIS_BRANCH" = staging -o "$TRAVIS_BRANCH" = trying ]; then cargo build - cargo run -p ckb-test target/debug/ckb + cd test && cargo run ../target/debug/ckb # Switch to release mode when the running time is much longer than the build time. # cargo build --release diff --git a/docker/hub/Dockerfile b/docker/hub/Dockerfile index 8e1dc98125..fe41e6d67b 100644 --- a/docker/hub/Dockerfile +++ b/docker/hub/Dockerfile @@ -41,7 +41,7 @@ RUN ./rustup-init -y --no-modify-path --default-toolchain $RUST_VERSION; \ WORKDIR /ckb COPY ./ . -RUN cargo build --release +RUN make prod FROM ubuntu:bionic diff --git a/rpc/src/module/trace.rs b/rpc/src/module/trace.rs index 0268d1b91d..2d47a086e9 100644 --- a/rpc/src/module/trace.rs +++ b/rpc/src/module/trace.rs @@ -46,7 +46,7 @@ impl TraceRpc for TraceRpcImpl { Ok(cycles) => Some(cycles), }; let entry = PoolEntry::new(tx.clone(), 0, cycles); - chain_state.mut_tx_pool().enqueue_tx(entry); + chain_state.mut_tx_pool().trace_tx(entry); cycles }; diff --git a/shared/src/tx_pool/trace.rs b/shared/src/tx_pool/trace.rs index ca9c51d44c..7519b2d109 100644 --- a/shared/src/tx_pool/trace.rs +++ b/shared/src/tx_pool/trace.rs @@ -1,11 +1,11 @@ use faketime::unix_time_as_millis; use lru_cache::LruCache; use numext_fixed_hash::H256; -use serde_derive::Serialize; +use serde_derive::{Deserialize, Serialize}; use std::fmt; -#[derive(Debug, Clone, Eq, PartialEq, Serialize, Hash)] -pub(crate) enum Action { +#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, Hash)] +pub enum Action { AddPending, Proposed, Staged, @@ -14,7 +14,7 @@ pub(crate) enum Action { Committed, } -#[derive(Clone, Eq, PartialEq, Serialize, Hash)] +#[derive(Clone, Eq, PartialEq, Serialize, Deserialize, Hash)] pub struct TxTrace { pub(crate) action: Action, pub(crate) info: String, @@ -25,6 +25,10 @@ impl TxTrace { pub(crate) fn new(action: Action, info: String, time: u64) -> TxTrace { TxTrace { action, info, time } } + + pub fn action(&self) -> &Action { + &self.action + } } impl fmt::Debug for TxTrace { diff --git a/src/cli/run_impl.rs b/src/cli/run_impl.rs index 6c43493cc7..7cc19f8465 100644 --- a/src/cli/run_impl.rs +++ b/src/cli/run_impl.rs @@ -35,7 +35,6 @@ pub fn run(setup: Setup) { let chain_controller = setup_chain(shared.clone(), notify.clone()); info!(target: "main", "chain genesis hash: {:#x}", shared.genesis_hash()); - // let tx_pool_controller = setup_tx_pool(setup.configs.pool, shared.clone(), notify.clone()); let block_assembler = BlockAssembler::new(shared.clone(), setup.configs.block_assembler.type_hash); @@ -119,15 +118,6 @@ fn setup_chain( chain_service.start(Some("ChainService")) } -// fn setup_tx_pool( -// config: PoolConfig, -// shared: Shared, -// notify: NotifyController, -// ) -> TransactionPoolController { -// let tx_pool_service = TransactionPoolService::new(config, shared, notify); -// tx_pool_service.start(Some("TransactionPoolService")) -// } - pub fn type_hash(setup: &Setup) { let consensus = setup.chain_spec.to_consensus().unwrap(); let system_cell_tx = &consensus.genesis_block().commit_transactions()[0]; diff --git a/test/Cargo.toml b/test/Cargo.toml index 71ec3cd13f..688f6de15b 100644 --- a/test/Cargo.toml +++ b/test/Cargo.toml @@ -10,6 +10,7 @@ jsonrpc-types = { path = "../util/jsonrpc-types" } logger = { path = "../util/logger" } ckb-core= { path = "../core" } ckb-network = { path = "../network" } +ckb-shared = { path = "../shared" } numext-fixed-hash = { version = "0.1", features = ["support_rand", "support_heapsize", "support_serde"] } fs_extra = "1.1" tempfile = "3.0" @@ -18,4 +19,6 @@ jsonrpc-client-http = "0.5.0" rand = "0.6" log = "0.4" -[dev-dependencies] +# Prevent this from interfering with workspaces +[workspace] +members = ["."] diff --git a/test/fixtures/nodes_template/default.toml b/test/fixtures/nodes_template/default.toml index 06761c2344..3ad8fecf2a 100644 --- a/test/fixtures/nodes_template/default.toml +++ b/test/fixtures/nodes_template/default.toml @@ -27,7 +27,7 @@ listen_address = "0.0.0.0:RPC_PORT" max_request_body_size = 10485760 -modules = ["Net", "Pool", "Miner", "Chain", "IntegrationTest"] +modules = ["Net", "Pool", "Miner", "Chain", "IntegrationTest", "Trace"] [sync] verification_level = "Full" diff --git a/test/src/main.rs b/test/src/main.rs index a6da4468f8..13cb2bc501 100644 --- a/test/src/main.rs +++ b/test/src/main.rs @@ -20,22 +20,24 @@ fn main() { .expect("invalid port number"); if let Some(spec_name) = env::args().nth(3) { let spec: Box = match &spec_name[..] { - "block_relay_basic" => Box::new(BlockRelayBasic {}), - "block_sync_basic" => Box::new(BlockSyncBasic {}), - "mining_basic" => Box::new(MiningBasic {}), - "pool_reconcile" => Box::new(PoolReconcile {}), - "transaction_relay_basic" => Box::new(TransactionRelayBasic {}), + "block_relay_basic" => Box::new(BlockRelayBasic), + "block_sync_basic" => Box::new(BlockSyncBasic), + "mining_basic" => Box::new(MiningBasic), + "pool_reconcile" => Box::new(PoolReconcile), + "pool_trace" => Box::new(PoolTrace), + "transaction_relay_basic" => Box::new(TransactionRelayBasic), _ => panic!("invalid spec"), }; let net = spec.setup_net(&binary, start_port); spec.run(&net); } else { let specs: Vec> = vec![ - Box::new(BlockRelayBasic {}), - Box::new(BlockSyncBasic {}), - Box::new(MiningBasic {}), - Box::new(PoolReconcile {}), - Box::new(TransactionRelayBasic {}), + Box::new(BlockRelayBasic), + Box::new(BlockSyncBasic), + Box::new(MiningBasic), + Box::new(PoolReconcile), + Box::new(PoolTrace), + Box::new(TransactionRelayBasic), ]; specs.iter().for_each(|spec| { @@ -43,4 +45,6 @@ fn main() { spec.run(&net); }) } + + logger::flush(); } diff --git a/test/src/node.rs b/test/src/node.rs index 587da88da8..fef9080043 100644 --- a/test/src/node.rs +++ b/test/src/node.rs @@ -120,6 +120,28 @@ impl Node { .expect("rpc call send_transaction failed") } + pub fn send_traced_transaction(&self) -> H256 { + let mut rpc = self.rpc_client(); + let tip_number = rpc + .get_tip_block_number() + .call() + .expect("rpc call get_tip_block_number failed"); + let block_hash = rpc + .get_block_hash(tip_number) + .call() + .expect("rpc call get_block_hash failed") + .expect("get_block_hash result none"); + let block = rpc + .get_block(block_hash) + .call() + .expect("rpc call get_block failed") + .expect("get_block result none"); + let cellbase: Transaction = block.commit_transactions[0].clone().into(); + rpc.trace_transaction((&self.new_transaction(cellbase.hash())).into()) + .call() + .expect("rpc call send_transaction failed") + } + pub fn wait_for_rpc_connection(&self) { let mut client = self.rpc_client(); diff --git a/test/src/rpc.rs b/test/src/rpc.rs index fb927d05f6..aa8755bf80 100644 --- a/test/src/rpc.rs +++ b/test/src/rpc.rs @@ -1,4 +1,5 @@ use ckb_core::BlockNumber; +use ckb_shared::tx_pool::TxTrace; use jsonrpc_client_core::{expand_params, jsonrpc_client}; use jsonrpc_types::{Block, BlockTemplate, Header, LocalNode, Transaction}; use numext_fixed_hash::H256; @@ -12,6 +13,8 @@ jsonrpc_client!(pub struct RpcClient { pub fn submit_block(&mut self, work_id: String, data: Block) -> RpcRequest>; pub fn send_transaction(&mut self, tx: Transaction) -> RpcRequest; + pub fn trace_transaction(&mut self, tx: Transaction) -> RpcRequest; + pub fn get_transaction_trace(&mut self, hash: H256) -> RpcRequest>>; pub fn get_pool_transaction(&mut self, hash: H256) -> RpcRequest>; pub fn get_block(&mut self, hash: H256) -> RpcRequest>; diff --git a/test/src/specs/block_relay.rs b/test/src/specs/block_relay.rs index 420ab73760..3ace720391 100644 --- a/test/src/specs/block_relay.rs +++ b/test/src/specs/block_relay.rs @@ -1,7 +1,7 @@ use crate::{sleep, Net, Spec}; use log::info; -pub struct BlockRelayBasic {} +pub struct BlockRelayBasic; impl Spec for BlockRelayBasic { fn run(&self, net: &Net) { diff --git a/test/src/specs/block_sync.rs b/test/src/specs/block_sync.rs index c4ad3105e4..df0e17404b 100644 --- a/test/src/specs/block_sync.rs +++ b/test/src/specs/block_sync.rs @@ -1,7 +1,7 @@ use crate::{sleep, Net, Spec}; use log::info; -pub struct BlockSyncBasic {} +pub struct BlockSyncBasic; impl Spec for BlockSyncBasic { fn run(&self, net: &Net) { diff --git a/test/src/specs/mining.rs b/test/src/specs/mining.rs index 31e081fb37..96333c6da4 100644 --- a/test/src/specs/mining.rs +++ b/test/src/specs/mining.rs @@ -3,7 +3,7 @@ use ckb_core::block::Block; use ckb_core::transaction::ProposalShortId; use log::info; -pub struct MiningBasic {} +pub struct MiningBasic; impl Spec for MiningBasic { fn run(&self, net: &Net) { diff --git a/test/src/specs/mod.rs b/test/src/specs/mod.rs index 3c3ac06505..1a16a69e9f 100644 --- a/test/src/specs/mod.rs +++ b/test/src/specs/mod.rs @@ -7,7 +7,7 @@ mod transaction_relay; pub use block_relay::BlockRelayBasic; pub use block_sync::BlockSyncBasic; pub use mining::MiningBasic; -pub use pool::PoolReconcile; +pub use pool::{PoolReconcile, PoolTrace}; pub use transaction_relay::TransactionRelayBasic; use crate::{sleep, Net}; diff --git a/test/src/specs/pool.rs b/test/src/specs/pool.rs index 0c5bf0b8d8..b52884d407 100644 --- a/test/src/specs/pool.rs +++ b/test/src/specs/pool.rs @@ -1,7 +1,8 @@ use crate::{sleep, Net, Spec}; +use ckb_shared::tx_pool::trace::Action; use log::info; -pub struct PoolReconcile {} +pub struct PoolReconcile; impl Spec for PoolReconcile { fn run(&self, net: &Net) { @@ -56,3 +57,45 @@ impl Spec for PoolReconcile { false } } + +pub struct PoolTrace; + +impl Spec for PoolTrace { + fn run(&self, net: &Net) { + info!("Running PoolTrace"); + let node0 = &net.nodes[0]; + + info!("Generate 1 block on node0"); + node0.generate_block(); + + info!("Use generated block's cellbase as tx input"); + let hash = node0.send_traced_transaction(); + + info!("Generate 3 more blocks on node0"); + node0.generate_block(); + node0.generate_block(); + node0.generate_block(); + + let actions: Vec<_> = node0 + .rpc_client() + .get_transaction_trace(hash) + .call() + .unwrap() + .unwrap() + .iter() + .map(|trace| trace.action()) + .cloned() + .collect(); + + info!("TxTrace actions should contains AddPending and Committed"); + assert!(actions.contains(&Action::AddPending) && actions.contains(&Action::Committed)); + } + + fn num_nodes(&self) -> usize { + 1 + } + + fn connect_all(&self) -> bool { + false + } +} diff --git a/test/src/specs/transaction_relay.rs b/test/src/specs/transaction_relay.rs index ccde57146f..f7e371c6d7 100644 --- a/test/src/specs/transaction_relay.rs +++ b/test/src/specs/transaction_relay.rs @@ -1,7 +1,7 @@ use crate::{sleep, Net, Spec}; use log::info; -pub struct TransactionRelayBasic {} +pub struct TransactionRelayBasic; impl Spec for TransactionRelayBasic { fn run(&self, net: &Net) { From d27bcbfc0ed5c3871b98e5faa2239269061710b7 Mon Sep 17 00:00:00 2001 From: ian Date: Thu, 28 Mar 2019 09:48:22 +0800 Subject: [PATCH 96/99] docs: rm docs/data-structures.md [skip ci] Moved to https://github.com/nervosnetwork/rfcs/pull/91 --- docs/data-structures.md | 257 ---------------------------------------- 1 file changed, 257 deletions(-) delete mode 100644 docs/data-structures.md diff --git a/docs/data-structures.md b/docs/data-structures.md deleted file mode 100644 index 27fb53195b..0000000000 --- a/docs/data-structures.md +++ /dev/null @@ -1,257 +0,0 @@ -# Data Structures of Cell/Script/Transaction/Block - -This documents list all the basic structures one may need to know in order to develop on CKB, or mine CKB coins. - -* [Cell](#Cell) -* [Script](#Script) -* [Transaction](#Transaction) -* [Block](#Block) - - - -## Cell - -### Example - -```json -{ - "capacity": 5000000, - "data": "0x", - "lock": "0xa58a960b28d6e283546e38740e80142da94f88e88d5114d8dc91312b8da4765a", - "type": null -} -``` - -## Description - -| Name | Type | Description | -| :--------- | :--------- | :----------------------------------------------------------- | -| `capacity` | uint64 | **The size of the cell.** When a new cell is generated (via transaction), one of the verification rule is `capacity ≥ len(capacity)+len(data)+len(type)+len(lock)`. This value also represents the balance of CKB coin, just like the `balance` field in the Bitcoin's UTXO. (E.g. Alice owns 100 CKB coins means she can unlock a group of cells that has 100 amount of `capacity` in total.) | -| `data` | Bytes | **Arbitrary data.** This part is for storing states or scripts. In order to make this cell valid on-chain, the data filled in this field should comply with the logics and rules defined by `type` or `lock`. | -| `type` | `Script` | **A Script that defines the type of the cell.** In a transaction, if an input cell and an output cell has the same `type` field, then the `data` part of these two cells is limited by the `type` script upon the transaction verification. (I.e. `type` is a script that limits how the `data` field of the new cells can be changed from the old cells.) `type` is required to has a data structure of `script`. **This field is optional.** | -| `lock` | H256(hash) | **The hash of a Script that defines the ownership of the cell**, just like the `lock` field in the Bitcoin's UTXO. Whoever can provide an unlock script that has the same hash of a cell's `lock` hash can use this cell as input in an transaction (i.e. has the ownership of this cell). This is similar to the P2SH scheme in Bitcoin. | - - - -More information about Cell can be found in the [whitepaper](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0002-ckb/0002-ckb.md#42-cell). - - - -## Script - -### Example - -```json -{ - "version": 0, - "binary": null, - "reference": "0x12b464bcab8f55822501cdb91ea35ea707d72ec970363972388a0c49b94d377c", - "signed_args": [ - "024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01" - ], - "args": [ - "3044022038f282cffdd26e2a050d7779ddc29be81a7e2f8a73706d2b7a6fde8a78e950ee0220538657b4c01be3e77827a82e92d33a923e864c55b88fd18cd5e5b25597432e9b", - "1" - ] -} -``` - - - -### Description - -| Name | Type | Description | -| :------------ | :------ | :----------------------------------------------------------- | -| `version` | uint8 | **The version of the script.** It‘s used to distinguish transactions when there's a fork happened to the blockchain system. | -| `binary` | Bytes | **ELF formatted binary that contains an RISC-V based script.** This part of data is loaded into an CKB-VM instance when they are specified upon the transaction verification. | -| `reference` | Bytes | **The `type hash` of the script that is referred by this script.** It is possible to refer the script in another cell on-chain as the binary code in this script, instead of entering the binary directly into the script. **Notice:** When loading a script, CKB-VM will first try to load the `binary` data. If `binary` is `null`, then `reference` would be used. | -| `args` | [Bytes] | **An array of arguments as the script input.** The arguments here are imported into the CKB-VM instance as input arguments for the scripts. This part is NOT used when calculating the hash of the script. | -| `signed_args` | [Bytes] | **An array of arguments that belongs to the script for improving code reuse rate**. Please refer [this document](https://github.com/Mine77/ckb-demo-ruby-sdk/blob/docs/update-docs/docs/how-to-write-contracts.md#script-model) for more explanation about this field. | - - - -More information about Script can be [here](https://github.com/nervosnetwork/ckb-demo-ruby-sdk/blob/master/docs/how-to-write-contracts.md#script-model). - -Also you can find how the `Script` structure is implemented from [these codes](https://github.com/nervosnetwork/ckb/blob/17b48a0777753a91691146b3817758c02db6f415/core/src/script.rs#L13-L53) - - - -## Transaction - -### Example - -```json -{ - "deps": [], - "hash": "0x4707810253259258e3091934dc5b543403c10cc899859e077fe26067f8d52dc0", - "inputs": [ - { - "previous_output": { - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "index": 4294967295 - }, - "unlock": { - "args": [], - "binary": "0x0d00000000000000", - "reference": null, - "signed_args": [], - "version": 0 - } - } - ], - "outputs": [ - { - "capacity": 5000000, - "data": "0x", - "lock": "0xa58a960b28d6e283546e38740e80142da94f88e88d5114d8dc91312b8da4765a", - "type": null - } - ], - "version": 0 -} -``` - -### Description - -#### Transaction - -| Name | Type | Description | -| ----------------- | -------------------------------- | ------------------------------------------------------------ | -| `version` | uint32 | **The version of the transaction.** It‘s used to distinguish transactions when there's a fork happened to the blockchain system. | -| `hash` | H256(Hash) | **The hash of the transaction.** This also serve as the identifier of the transaction. | -| `deps` | [`outpoint`] | **An array of `outpoint` that point to the cells that are dependencies of this transaction.** Only live cells can be listed here. The cells listed are read-only. | -| `inputs` | [{`previsou_output` , `unlock`}] | **An array of {`previsou_output` , `unlock`}.** | -| `previous_output` | `outpoint` | **A cell outpoint that point to the cells used as inputs.** Input cells are in fact the output of previous transactions, hence they are noted as `previous_output` here. These cells are referred through `outpoint`, which contains the transaction `hash` of the previous transaction, as well as this cell's `index` in its transaction's output list. | -| `unlock` | `script` | **A script for unlocking the corresponding input cell** (i.e. `previous_output`). See [here](https://github.com/nervosnetwork/ckb-demo-ruby-sdk/blob/develop/docs/how-to-write-contracts.md) for how to program this part. | -| `outputs` | [`cell`] | **An array of cells that are used as outputs**, i.e. the newly generated cells. These are the cells may be used as inputs for other transactions. Each of the Cell has the same structure to [the Cell section](#cell) above. | - - - -#### OutPoint - - - -| Name | Type | Description | -| ---------------- | ------------------ | ------------------------------------------------------------ | -| `outpoint` | {`hash` , `index`} | **An outpoint is pointer to a specific cell.** This is used in a transaction to refer a cell that is generated in a previous transaction. | -| `outpoint.hash` | H256(hash) | **The hash of the transaction that this cell belongs to.** | -| `outpoint.index` | uint32 | **The index of the cell in its transaction's output list.** | - - - - - -More information about the Transaction of Nervos CKB can be found in [whitepaper](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0002-ckb/0002-ckb.md#44-transaction). - - - -## Block - -### Example - -```json -{ -"commit_transactions": [ - { - "deps": [], - "hash": "0xabeb06aea75b59ec316db9d21243ee3f0b0ad0723e50f57761cef7e07974b9b5", - "inputs": [ - { - "previous_output": { - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "index": 4294967295 - }, - "unlock": { - "args": [], - "binary": "0x0b00000000000000", - "reference": null, - "signed_args": [], - "version": 0 - } - } - ], - "outputs": [ - { - "capacity": 5000000, - "data": "0x", - "lock": "0xa58a960b28d6e283546e38740e80142da94f88e88d5114d8dc91312b8da4765a", - "type": null - } - ], - "version": 0 - } -], -"header": { - "cellbase_id": "0xabeb06aea75b59ec316db9d21243ee3f0b0ad0723e50f57761cef7e07974b9b5", - "difficulty": "0x100", - "hash": "0xcddd882eff5edd2f7db25074cbbdc1d21cd698f60d6fb39412ef91d19eb900e8", - "number": 11, - "parent_hash": "0x255f65bf9dc00bcd9f9b8be8624be222cba16b51366208a8267f1925eb40e7e4", - "seal": { - "nonce": 503529102265201399, - "proof": "0x" - }, - "timestamp": 1551155125985, - "txs_commit": "0xabeb06aea75b59ec316db9d21243ee3f0b0ad0723e50f57761cef7e07974b9b5", - "txs_proposal": "0x0000000000000000000000000000000000000000000000000000000000000000", - "uncles_count": 1, - "uncles_hash": "0x99cf8710e59303bfac236b57256fcea2c58192f2c9c39d1ea4c19cbcf88b4952", - "version": 0 -}, -"proposal_transactions": [], -"uncles": [ - { - "cellbase": { - ... - }, - "header": { - ... - }, - "proposal_transactions": [] - } -] -} -``` - -### Description - -#### Block - -| Name | Type | Description | -| ----------------------- | --------------- | ------------------------------------------------------------ | -| `header` | `Header` | **The block header of the block.** This part contains some metadata of the block. See [the Header section](#header) below for the details of this part. | -| `commit_trasactions` | [`Transaction`] | **An array of committed transactions contained in the block.** Each element of this array has the same structure as [the Transaction structure](#transaction) above. | -| `proposal_transactions` | [string] | **An array of hex-encoded short transaction ID of the proposed transactions.** | -| `uncles` | [`UncleBlock`] | **An array of uncle blocks of the block.** See [the UncleBlock section](#uncleblock) below for the details of this part. | - -#### Header - -(`header` is a sub-structure of `block` and `UncleBlock`.) - -| Name | Type | Description | -| -------------- | ------------------- | ------------------------------------------------------------ | -| `cellbase_id` | H256(hash) | **The hash of the Cellbase transaction.** Cellbase transaction is just like the coinbase transaction in Bitcoin. It's the transaction added by the miner who mined this block, by which the miner receives block reward for successfully mined the block. | -| `difficulty` | Bytes | **The difficulty of the PoW puzzle.** | -| `hash` | H256(hash) | **The block hash.** Note that although this value is returned by the JSON-RPC API, it is in fact NOT a part of the header. This value is actually calculated from the `Header` data. | -| `number` | uint64 | **The block height.** | -| `parent_hash` | H256(hash) | **The hash of the parent block.** | -| `seal` | `nonce` and `proof` | **The seal of a block.** After finished the block assembling, the miner can start to do the calculation for finding the solution of the PoW puzzle. The "solution" here is called `seal`. | -| `seal.nonce` | uint64 | **The nonce.** Similar to [the nonce in Bitcoin](https://en.bitcoin.it/wiki/Nonce). | -| `seal.proof` | Bytes | **The solution of the PoW puzzle.** | -| `timestamp` | uint64 | **A [Unix time](http://en.wikipedia.org/wiki/Unix_time) timestamp.** | -| `txs_commit` | H256(hash) | **The Merkle Root of the Merkle trie with the hash of transactions as leaves.** | -| `txs_proposal` | H256(hash) | **The Merkle Root of the Merkle trie with the hash of short transaction IDs as leaves.** | -| `uncles_count` | uint32 | **The number of uncle blocks.** | -| `uncles_hash` | H256(hash) | **The hash of the serialized uncle blocks data.** This will later be changed to using [CFB Encoding](https://github.com/nervosnetwork/cfb). | -| `version` | uint32 | **The version of the block**. This is for solving the compatibility issues might be occurred after a fork. | - -#### UncleBlock - -(`UncleBlock` is a sub-structure of `Block`.) - -| Name | Type | Description | -| ----------------------- | ------------- | ------------------------------------------------------------ | -| `cellbase` | `Transaction` | **The cellbase transaction of the uncle block.** The inner structure of this part is same as [the Transaction structure](#transaction) above. | -| `header` | `Header` | **The block header of the uncle block.** The inner structure of this part is same as [the Header structure](#header) above. | -| `proposal_transactions` | [`string`] | **An array of short transaction IDs of the proposed transactions in the uncle block.** | - From eaaa2a365a46194dbe29a7799dc49a6886e4e8ef Mon Sep 17 00:00:00 2001 From: zhangsoledad <787953403@qq.com> Date: Thu, 28 Mar 2019 10:24:34 +0800 Subject: [PATCH 97/99] ci: increase ulimit on travis OSX --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index ca74416754..a312748972 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,6 +33,7 @@ addons: - texinfo - libtool +before_install: if [ "$TRAVIS_OS_NAME" = "osx" ]; then ulimit -n 8192; fi install: ./devtools/ci/install.sh script: ./devtools/ci/script.sh From 6c5ac8ac3294ce8c88efbbaab5cda09bb89dc329 Mon Sep 17 00:00:00 2001 From: ian Date: Mon, 8 Apr 2019 10:23:04 +0800 Subject: [PATCH 98/99] chore: CHANGELOG for v0.8.0 --- CHANGELOG.md | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 932bace288..a98d396368 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,78 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +# [v0.8.0](https://github.com/nervosnetwork/ckb/compare/v0.7.0...v0.8.0) (2019-04-08) + +### Features + +* #336: index whether a tx is a cellbase in the chain r=quake a=u2 + + Index whether a tx is a cellbase in the chain, prepare for the cellbase outputs maturity checking. + + Now saving the cellbase index and block number in the `TransactionMeta`, there is another implementation which creates a `HashMap`. The second one may be a little memory saving, but this one is more simple. I think both are ok. + + https://github.com/nervosnetwork/ckb/issues/54 + +* #350: use TryFrom convert protocol r=doitian a=zhangsoledad +* #340: Integrate discovery and identify protocol r=jjyr a=TheWaWaR + + Known issues: + + - Shutdown network not very graceful. + +* #345: Add `random_peers` function to PeerStore r=jjyr a=jjyr +* #335: Enforce `type` field of a cellbase output cell must be absent r=doitian a=zhangsoledad +* #334: Version verification r=doitian a=zhangsoledad +* #295: Replace P2P library r=quake a=jjyr + +### Bug Fixes + +* #365: trace rpc r=zhangsoledad a=zhangsoledad + + addition: + * remove integration tests from root workspace + * fix integration tests logger panic at flush + +* #341: verify tx cycles in relay protocol r=zhangsoledad a=jjyr + +### Improvements + +* #359: merge `cell_set` `chain_state` cell provider r=quake a=zhangsoledad + +* #343: use CellProvider r=zhangsoledad a=quake + + This refactoring is intended to remove closure in ChainService and duplicate code in ChainState. And fix bugs in block processing and add some test cases. + +* #346: replace unwrap with expect r=doitian a=zhangsoledad +* #342: shrink lock-acquisition r=quake a=zhangsoledad +* #361: refactor network config r=jjyr a=jjyr +* #356: Unify network peer scoring r=jjyr a=jjyr +* #349: Refactor peer store r=jjyr a=jjyr + +### BREAKING CHANGES + +* #361: network config + +```diff +[network] +- reserved_nodes = [] +-only_reserved_peers = false +-max_peers = 8 +-min_peers = 4 +-secret_file = "secret_key" +-peer_store_path = "peer_store.db" + ++reserved_peers = [] ++reserved_only = false ++max_peers = 125 ++max_outbound_peers = 30 ++config_dir_path = "default/network" ++ping_interval_secs = 15 ++ping_timeout_secs = 20 ++connect_outbound_interval_secs = 15 +``` + + # [v0.7.0](https://github.com/nervosnetwork/ckb/compare/v0.6.0...v0.7.0) (2019-03-25) From 0f1e112703c2668c84a8c05eeba2b0d086a9db4d Mon Sep 17 00:00:00 2001 From: ian Date: Mon, 8 Apr 2019 10:24:28 +0800 Subject: [PATCH 99/99] chore: bump to v0.8.0 --- Cargo.lock | 312 +++++++++++------------ Cargo.toml | 2 +- benches/Cargo.toml | 2 +- chain/Cargo.toml | 2 +- core/Cargo.toml | 2 +- db/Cargo.toml | 2 +- miner/Cargo.toml | 2 +- network/Cargo.toml | 2 +- notify/Cargo.toml | 2 +- pow/Cargo.toml | 2 +- protocol/Cargo.toml | 2 +- rpc/Cargo.toml | 2 +- script/Cargo.toml | 2 +- shared/Cargo.toml | 2 +- spec/Cargo.toml | 2 +- sync/Cargo.toml | 2 +- test/Cargo.toml | 2 +- traits/Cargo.toml | 2 +- util/Cargo.toml | 2 +- util/build-info/Cargo.toml | 2 +- util/crypto/Cargo.toml | 2 +- util/dir/Cargo.toml | 2 +- util/hash/Cargo.toml | 2 +- util/instrument/Cargo.toml | 2 +- util/jsonrpc-types/Cargo.toml | 2 +- util/logger/Cargo.toml | 2 +- util/merkle-tree/Cargo.toml | 2 +- util/occupied-capacity-derive/Cargo.toml | 2 +- util/occupied-capacity/Cargo.toml | 2 +- util/stop-handler/Cargo.toml | 2 +- verification/Cargo.toml | 2 +- 31 files changed, 186 insertions(+), 186 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e7b08b6137..3bd240744a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -210,7 +210,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "build-info" -version = "0.8.0-pre" +version = "0.8.0" [[package]] name = "byte-tools" @@ -269,32 +269,32 @@ dependencies = [ [[package]] name = "ckb" -version = "0.8.0-pre" -dependencies = [ - "build-info 0.8.0-pre", - "ckb-chain 0.8.0-pre", - "ckb-chain-spec 0.8.0-pre", - "ckb-core 0.8.0-pre", - "ckb-db 0.8.0-pre", - "ckb-instrument 0.8.0-pre", - "ckb-miner 0.8.0-pre", - "ckb-network 0.8.0-pre", - "ckb-notify 0.8.0-pre", - "ckb-pow 0.8.0-pre", - "ckb-rpc 0.8.0-pre", - "ckb-shared 0.8.0-pre", - "ckb-sync 0.8.0-pre", - "ckb-traits 0.8.0-pre", - "ckb-util 0.8.0-pre", +version = "0.8.0" +dependencies = [ + "build-info 0.8.0", + "ckb-chain 0.8.0", + "ckb-chain-spec 0.8.0", + "ckb-core 0.8.0", + "ckb-db 0.8.0", + "ckb-instrument 0.8.0", + "ckb-miner 0.8.0", + "ckb-network 0.8.0", + "ckb-notify 0.8.0", + "ckb-pow 0.8.0", + "ckb-rpc 0.8.0", + "ckb-shared 0.8.0", + "ckb-sync 0.8.0", + "ckb-traits 0.8.0", + "ckb-util 0.8.0", "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", "config 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", - "crypto 0.8.0-pre", + "crypto 0.8.0", "ctrlc 3.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "dir 0.8.0-pre", - "hash 0.8.0-pre", + "dir 0.8.0", + "hash 0.8.0", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "logger 0.8.0-pre", + "logger 0.8.0", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", @@ -305,16 +305,16 @@ dependencies = [ [[package]] name = "ckb-benches" -version = "0.8.0-pre" -dependencies = [ - "ckb-chain 0.8.0-pre", - "ckb-chain-spec 0.8.0-pre", - "ckb-core 0.8.0-pre", - "ckb-db 0.8.0-pre", - "ckb-notify 0.8.0-pre", - "ckb-pow 0.8.0-pre", - "ckb-shared 0.8.0-pre", - "ckb-traits 0.8.0-pre", +version = "0.8.0" +dependencies = [ + "ckb-chain 0.8.0", + "ckb-chain-spec 0.8.0", + "ckb-core 0.8.0", + "ckb-db 0.8.0", + "ckb-notify 0.8.0", + "ckb-pow 0.8.0", + "ckb-shared 0.8.0", + "ckb-traits 0.8.0", "criterion 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -324,15 +324,15 @@ dependencies = [ [[package]] name = "ckb-chain" -version = "0.8.0-pre" -dependencies = [ - "ckb-chain-spec 0.8.0-pre", - "ckb-core 0.8.0-pre", - "ckb-db 0.8.0-pre", - "ckb-notify 0.8.0-pre", - "ckb-shared 0.8.0-pre", - "ckb-traits 0.8.0-pre", - "ckb-verification 0.8.0-pre", +version = "0.8.0" +dependencies = [ + "ckb-chain-spec 0.8.0", + "ckb-core 0.8.0", + "ckb-db 0.8.0", + "ckb-notify 0.8.0", + "ckb-shared 0.8.0", + "ckb-traits 0.8.0", + "ckb-verification 0.8.0", "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -343,17 +343,17 @@ dependencies = [ "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", - "stop-handler 0.8.0-pre", + "stop-handler 0.8.0", "tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "ckb-chain-spec" -version = "0.8.0-pre" +version = "0.8.0" dependencies = [ - "ckb-core 0.8.0-pre", - "ckb-pow 0.8.0-pre", - "ckb-protocol 0.8.0-pre", + "ckb-core 0.8.0", + "ckb-pow 0.8.0", + "ckb-protocol 0.8.0", "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -364,20 +364,20 @@ dependencies = [ [[package]] name = "ckb-core" -version = "0.8.0-pre" +version = "0.8.0" dependencies = [ "bincode 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "bit-vec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "ckb-merkle-tree 0.8.0-pre", - "ckb-util 0.8.0-pre", + "ckb-merkle-tree 0.8.0", + "ckb-util 0.8.0", "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", - "crypto 0.8.0-pre", + "crypto 0.8.0", "faster-hex 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", - "hash 0.8.0-pre", + "hash 0.8.0", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "occupied-capacity 0.8.0-pre", + "occupied-capacity 0.8.0", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_bytes 0.10.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", @@ -385,10 +385,10 @@ dependencies = [ [[package]] name = "ckb-db" -version = "0.8.0-pre" +version = "0.8.0" dependencies = [ "bincode 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "ckb-util 0.8.0-pre", + "ckb-util 0.8.0", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "rocksdb 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -400,46 +400,46 @@ dependencies = [ [[package]] name = "ckb-instrument" -version = "0.8.0-pre" +version = "0.8.0" dependencies = [ - "ckb-chain 0.8.0-pre", - "ckb-core 0.8.0-pre", - "ckb-shared 0.8.0-pre", - "ckb-traits 0.8.0-pre", + "ckb-chain 0.8.0", + "ckb-core 0.8.0", + "ckb-shared 0.8.0", + "ckb-traits 0.8.0", "indicatif 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "ckb-merkle-tree" -version = "0.8.0-pre" +version = "0.8.0" dependencies = [ - "hash 0.8.0-pre", + "hash 0.8.0", "merkle-cbt 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "ckb-miner" -version = "0.8.0-pre" -dependencies = [ - "ckb-chain 0.8.0-pre", - "ckb-chain-spec 0.8.0-pre", - "ckb-core 0.8.0-pre", - "ckb-db 0.8.0-pre", - "ckb-notify 0.8.0-pre", - "ckb-pow 0.8.0-pre", - "ckb-shared 0.8.0-pre", - "ckb-traits 0.8.0-pre", - "ckb-util 0.8.0-pre", - "ckb-verification 0.8.0-pre", +version = "0.8.0" +dependencies = [ + "ckb-chain 0.8.0", + "ckb-chain-spec 0.8.0", + "ckb-core 0.8.0", + "ckb-db 0.8.0", + "ckb-notify 0.8.0", + "ckb-pow 0.8.0", + "ckb-shared 0.8.0", + "ckb-traits 0.8.0", + "ckb-util 0.8.0", + "ckb-verification 0.8.0", "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "faketime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.12.25 (registry+https://github.com/rust-lang/crates.io-index)", - "jsonrpc-types 0.8.0-pre", + "jsonrpc-types 0.8.0", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "lru-cache 0.1.0 (git+https://github.com/nervosnetwork/lru-cache)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -449,15 +449,15 @@ dependencies = [ "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", - "stop-handler 0.8.0-pre", + "stop-handler 0.8.0", ] [[package]] name = "ckb-network" -version = "0.8.0-pre" +version = "0.8.0" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "ckb-util 0.8.0-pre", + "ckb-util 0.8.0", "criterion 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "faketime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -485,23 +485,23 @@ dependencies = [ [[package]] name = "ckb-notify" -version = "0.8.0-pre" +version = "0.8.0" dependencies = [ - "ckb-core 0.8.0-pre", + "ckb-core 0.8.0", "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "stop-handler 0.8.0-pre", + "stop-handler 0.8.0", ] [[package]] name = "ckb-pow" -version = "0.8.0-pre" +version = "0.8.0" dependencies = [ "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "ckb-core 0.8.0-pre", + "ckb-core 0.8.0", "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", - "hash 0.8.0-pre", + "hash 0.8.0", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "proptest 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -511,15 +511,15 @@ dependencies = [ [[package]] name = "ckb-protocol" -version = "0.8.0-pre" +version = "0.8.0" dependencies = [ "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "ckb-core 0.8.0-pre", - "ckb-merkle-tree 0.8.0-pre", - "ckb-util 0.8.0-pre", + "ckb-core 0.8.0", + "ckb-merkle-tree 0.8.0", + "ckb-util 0.8.0", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", - "hash 0.8.0-pre", + "hash 0.8.0", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -528,20 +528,20 @@ dependencies = [ [[package]] name = "ckb-rpc" -version = "0.8.0-pre" -dependencies = [ - "build-info 0.8.0-pre", - "ckb-chain 0.8.0-pre", - "ckb-core 0.8.0-pre", - "ckb-db 0.8.0-pre", - "ckb-miner 0.8.0-pre", - "ckb-network 0.8.0-pre", - "ckb-pow 0.8.0-pre", - "ckb-protocol 0.8.0-pre", - "ckb-shared 0.8.0-pre", - "ckb-sync 0.8.0-pre", - "ckb-traits 0.8.0-pre", - "ckb-verification 0.8.0-pre", +version = "0.8.0" +dependencies = [ + "build-info 0.8.0", + "ckb-chain 0.8.0", + "ckb-core 0.8.0", + "ckb-db 0.8.0", + "ckb-miner 0.8.0", + "ckb-network 0.8.0", + "ckb-pow 0.8.0", + "ckb-protocol 0.8.0", + "ckb-shared 0.8.0", + "ckb-sync 0.8.0", + "ckb-traits 0.8.0", + "ckb-verification 0.8.0", "faster-hex 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", @@ -549,7 +549,7 @@ dependencies = [ "jsonrpc-derive 10.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "jsonrpc-http-server 10.0.1 (git+https://github.com/nervosnetwork/jsonrpc?branch=http_remake)", "jsonrpc-server-utils 10.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "jsonrpc-types 0.8.0-pre", + "jsonrpc-types 0.8.0", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -561,17 +561,17 @@ dependencies = [ [[package]] name = "ckb-script" -version = "0.8.0-pre" +version = "0.8.0" dependencies = [ "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "ckb-core 0.8.0-pre", - "ckb-protocol 0.8.0-pre", + "ckb-core 0.8.0", + "ckb-protocol 0.8.0", "ckb-vm 0.1.0 (git+https://github.com/nervosnetwork/ckb-vm?rev=dd90ed1)", - "crypto 0.8.0-pre", + "crypto 0.8.0", "faster-hex 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", - "hash 0.8.0-pre", + "hash 0.8.0", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "proptest 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -579,15 +579,15 @@ dependencies = [ [[package]] name = "ckb-shared" -version = "0.8.0-pre" +version = "0.8.0" dependencies = [ "bincode 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "ckb-chain-spec 0.8.0-pre", - "ckb-core 0.8.0-pre", - "ckb-db 0.8.0-pre", - "ckb-traits 0.8.0-pre", - "ckb-util 0.8.0-pre", - "ckb-verification 0.8.0-pre", + "ckb-chain-spec 0.8.0", + "ckb-core 0.8.0", + "ckb-db 0.8.0", + "ckb-traits 0.8.0", + "ckb-util 0.8.0", + "ckb-verification 0.8.0", "env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "faketime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -597,7 +597,7 @@ dependencies = [ "lru-cache 0.1.0 (git+https://github.com/nervosnetwork/lru-cache)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "occupied-capacity 0.8.0-pre", + "occupied-capacity 0.8.0", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", @@ -606,22 +606,22 @@ dependencies = [ [[package]] name = "ckb-sync" -version = "0.8.0-pre" +version = "0.8.0" dependencies = [ "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "bloom-filters 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", - "ckb-chain 0.8.0-pre", - "ckb-chain-spec 0.8.0-pre", - "ckb-core 0.8.0-pre", - "ckb-db 0.8.0-pre", - "ckb-network 0.8.0-pre", - "ckb-notify 0.8.0-pre", - "ckb-protocol 0.8.0-pre", - "ckb-shared 0.8.0-pre", - "ckb-traits 0.8.0-pre", - "ckb-util 0.8.0-pre", - "ckb-verification 0.8.0-pre", + "ckb-chain 0.8.0", + "ckb-chain-spec 0.8.0", + "ckb-core 0.8.0", + "ckb-db 0.8.0", + "ckb-network 0.8.0", + "ckb-notify 0.8.0", + "ckb-protocol 0.8.0", + "ckb-shared 0.8.0", + "ckb-traits 0.8.0", + "ckb-util 0.8.0", + "ckb-verification 0.8.0", "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -637,10 +637,10 @@ dependencies = [ [[package]] name = "ckb-traits" -version = "0.8.0-pre" +version = "0.8.0" dependencies = [ - "ckb-chain-spec 0.8.0-pre", - "ckb-core 0.8.0-pre", + "ckb-chain-spec 0.8.0", + "ckb-core 0.8.0", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -648,33 +648,33 @@ dependencies = [ [[package]] name = "ckb-util" -version = "0.8.0-pre" +version = "0.8.0" dependencies = [ "parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "ckb-verification" -version = "0.8.0-pre" -dependencies = [ - "ckb-chain 0.8.0-pre", - "ckb-chain-spec 0.8.0-pre", - "ckb-core 0.8.0-pre", - "ckb-db 0.8.0-pre", - "ckb-merkle-tree 0.8.0-pre", - "ckb-notify 0.8.0-pre", - "ckb-pow 0.8.0-pre", - "ckb-script 0.8.0-pre", - "ckb-shared 0.8.0-pre", - "ckb-traits 0.8.0-pre", +version = "0.8.0" +dependencies = [ + "ckb-chain 0.8.0", + "ckb-chain-spec 0.8.0", + "ckb-core 0.8.0", + "ckb-db 0.8.0", + "ckb-merkle-tree 0.8.0", + "ckb-notify 0.8.0", + "ckb-pow 0.8.0", + "ckb-script 0.8.0", + "ckb-shared 0.8.0", + "ckb-traits 0.8.0", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "faketime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", - "hash 0.8.0-pre", + "hash 0.8.0", "lru-cache 0.1.0 (git+https://github.com/nervosnetwork/lru-cache)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-uint 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "occupied-capacity 0.8.0-pre", + "occupied-capacity 0.8.0", "rayon 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -888,7 +888,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "crypto" -version = "0.8.0-pre" +version = "0.8.0" dependencies = [ "crunchy 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -978,7 +978,7 @@ dependencies = [ [[package]] name = "dir" -version = "0.8.0-pre" +version = "0.8.0" [[package]] name = "either" @@ -1159,7 +1159,7 @@ dependencies = [ [[package]] name = "hash" -version = "0.8.0-pre" +version = "0.8.0" dependencies = [ "blake2b-rs 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "faster-hex 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1345,9 +1345,9 @@ dependencies = [ [[package]] name = "jsonrpc-types" -version = "0.8.0-pre" +version = "0.8.0" dependencies = [ - "ckb-core 0.8.0-pre", + "ckb-core 0.8.0", "faster-hex 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "jsonrpc-core 10.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1455,7 +1455,7 @@ dependencies = [ [[package]] name = "logger" -version = "0.8.0-pre" +version = "0.8.0" dependencies = [ "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1664,15 +1664,15 @@ dependencies = [ [[package]] name = "occupied-capacity" -version = "0.8.0-pre" +version = "0.8.0" dependencies = [ "numext-fixed-hash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "occupied-capacity-derive 0.8.0-pre", + "occupied-capacity-derive 0.8.0", ] [[package]] name = "occupied-capacity-derive" -version = "0.8.0-pre" +version = "0.8.0" dependencies = [ "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2282,7 +2282,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "stop-handler" -version = "0.8.0-pre" +version = "0.8.0" dependencies = [ "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 278a02d004..983a4abc88 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/benches/Cargo.toml b/benches/Cargo.toml index a4870b2a06..991cacbeaa 100644 --- a/benches/Cargo.toml +++ b/benches/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-benches" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/chain/Cargo.toml b/chain/Cargo.toml index 17875ccb98..895929424f 100644 --- a/chain/Cargo.toml +++ b/chain/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-chain" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/core/Cargo.toml b/core/Cargo.toml index bcb521213f..eb2d99cc75 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-core" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/db/Cargo.toml b/db/Cargo.toml index 95a4d80d74..4253b03241 100644 --- a/db/Cargo.toml +++ b/db/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-db" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/miner/Cargo.toml b/miner/Cargo.toml index 51f53c5aba..c58264f2ed 100644 --- a/miner/Cargo.toml +++ b/miner/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-miner" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/network/Cargo.toml b/network/Cargo.toml index 27d14ceb2b..3deb8dc81f 100644 --- a/network/Cargo.toml +++ b/network/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-network" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/notify/Cargo.toml b/notify/Cargo.toml index bf36ac365d..2b14888a3e 100644 --- a/notify/Cargo.toml +++ b/notify/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-notify" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/pow/Cargo.toml b/pow/Cargo.toml index 940703a052..b9309b5cb1 100644 --- a/pow/Cargo.toml +++ b/pow/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-pow" -version = "0.8.0-pre" +version = "0.8.0" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/protocol/Cargo.toml b/protocol/Cargo.toml index 8e4f8964b2..277ffc5f05 100644 --- a/protocol/Cargo.toml +++ b/protocol/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-protocol" -version = "0.8.0-pre" +version = "0.8.0" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index e0086deb1c..fe0b9a5c69 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-rpc" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/script/Cargo.toml b/script/Cargo.toml index c5013e45e7..f1a634d40f 100644 --- a/script/Cargo.toml +++ b/script/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-script" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/shared/Cargo.toml b/shared/Cargo.toml index 4671f28085..35360dc383 100644 --- a/shared/Cargo.toml +++ b/shared/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-shared" -version = "0.8.0-pre" +version = "0.8.0" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/spec/Cargo.toml b/spec/Cargo.toml index d9791baf94..71a2ab3268 100644 --- a/spec/Cargo.toml +++ b/spec/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-chain-spec" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/sync/Cargo.toml b/sync/Cargo.toml index 98dcc44c95..ce2aa71ede 100644 --- a/sync/Cargo.toml +++ b/sync/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-sync" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/test/Cargo.toml b/test/Cargo.toml index 688f6de15b..c26256296c 100644 --- a/test/Cargo.toml +++ b/test/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-test" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/traits/Cargo.toml b/traits/Cargo.toml index 78f8688359..a37ef926b1 100644 --- a/traits/Cargo.toml +++ b/traits/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-traits" -version = "0.8.0-pre" +version = "0.8.0" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/util/Cargo.toml b/util/Cargo.toml index e0758d8353..2dc52d1e9a 100644 --- a/util/Cargo.toml +++ b/util/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-util" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/util/build-info/Cargo.toml b/util/build-info/Cargo.toml index e8958021b3..00f8569aa3 100644 --- a/util/build-info/Cargo.toml +++ b/util/build-info/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "build-info" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/util/crypto/Cargo.toml b/util/crypto/Cargo.toml index bab1f7a4f8..ce1cd5beef 100644 --- a/util/crypto/Cargo.toml +++ b/util/crypto/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "crypto" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/util/dir/Cargo.toml b/util/dir/Cargo.toml index 6649a7dc75..fdd93a763e 100644 --- a/util/dir/Cargo.toml +++ b/util/dir/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dir" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/util/hash/Cargo.toml b/util/hash/Cargo.toml index da53a3eaba..f514067796 100644 --- a/util/hash/Cargo.toml +++ b/util/hash/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hash" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/util/instrument/Cargo.toml b/util/instrument/Cargo.toml index 214f29029f..68461e1b47 100644 --- a/util/instrument/Cargo.toml +++ b/util/instrument/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-instrument" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/util/jsonrpc-types/Cargo.toml b/util/jsonrpc-types/Cargo.toml index b42b283518..69c5138d7c 100644 --- a/util/jsonrpc-types/Cargo.toml +++ b/util/jsonrpc-types/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "jsonrpc-types" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/util/logger/Cargo.toml b/util/logger/Cargo.toml index d2862f9da0..6d9161fdd9 100644 --- a/util/logger/Cargo.toml +++ b/util/logger/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "logger" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos "] edition = "2018" diff --git a/util/merkle-tree/Cargo.toml b/util/merkle-tree/Cargo.toml index 882cf1d9df..4d50eb7f8d 100644 --- a/util/merkle-tree/Cargo.toml +++ b/util/merkle-tree/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-merkle-tree" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/util/occupied-capacity-derive/Cargo.toml b/util/occupied-capacity-derive/Cargo.toml index c2ae347434..82e113a31e 100644 --- a/util/occupied-capacity-derive/Cargo.toml +++ b/util/occupied-capacity-derive/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "occupied-capacity-derive" -version = "0.8.0-pre" +version = "0.8.0" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/util/occupied-capacity/Cargo.toml b/util/occupied-capacity/Cargo.toml index 410c51b105..1472a6301b 100644 --- a/util/occupied-capacity/Cargo.toml +++ b/util/occupied-capacity/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "occupied-capacity" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/util/stop-handler/Cargo.toml b/util/stop-handler/Cargo.toml index 989766b4c5..faca0f6938 100644 --- a/util/stop-handler/Cargo.toml +++ b/util/stop-handler/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "stop-handler" -version = "0.8.0-pre" +version = "0.8.0" authors = ["Nervos Core Dev "] edition = "2018" diff --git a/verification/Cargo.toml b/verification/Cargo.toml index ee550b07a2..c1af6b4443 100644 --- a/verification/Cargo.toml +++ b/verification/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-verification" -version = "0.8.0-pre" +version = "0.8.0" license = "MIT" authors = ["Nervos Core Dev "] edition = "2018"