From b731f8092a111d238835b8efd5265dfd43ba7149 Mon Sep 17 00:00:00 2001 From: daehyun Date: Wed, 11 Jul 2018 20:45:04 +0900 Subject: [PATCH] Compress snapshot files Fix: #220 --- Cargo.lock | 11 + core/res/blake_pow.json | 18 +- core/res/cuckoo.json | 18 +- core/res/null.json | 18 +- core/res/solo.json | 18 +- core/res/solo_authority.json | 18 +- core/res/tendermint.json | 18 +- core/src/consensus/null_engine/mod.rs | 8 +- core/src/header.rs | 9 + core/src/machine.rs | 9 + core/src/parcel.rs | 17 +- core/src/transaction.rs | 20 +- docs/basic-usage.rst | 2 +- spec/JSON-RPC.md | 440 +++++++++++++++++--------- sync/Cargo.toml | 1 + sync/src/lib.rs | 1 + sync/src/snapshot/service.rs | 9 +- 17 files changed, 408 insertions(+), 227 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cf5210c504..ba485dff40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -444,6 +444,7 @@ dependencies = [ "patricia-trie 0.1.0", "rand 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", "rlp 0.2.1", + "snap 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", "triehash 0.1.0", ] @@ -1702,6 +1703,15 @@ name = "smallvec" version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "snap" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "snappy-sys" version = "0.1.0" @@ -2390,6 +2400,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum smallvec 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4c8cbcd6df1e117c2210e13ab5109635ad68a929fcbb8964dc965b76cb5ee013" "checksum smallvec 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ee4f357e8cd37bf8822e1b964e96fd39e2cb5a0424f8aaa284ccaccc2162411c" "checksum smallvec 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "03dab98ab5ded3a8b43b2c80751194608d0b2aa0f1d46cf95d1c35e192844aa7" +"checksum snap 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "174451758f7045084ae92070f18e5d8e5c53a716f4172a9c6b17ce03e7b82573" "checksum snappy-sys 0.1.0 (git+https://github.com/paritytech/rust-snappy)" = "" "checksum socket2 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "06dc9f86ee48652b7c80f3d254e3b9accb67a928c562c64d10d7b016d3d98dab" "checksum spmc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "cd1f11d1fb5fd41834e55ce0b85a186efbf2f2afd9fdb09e2c8d72f9bff1ad1a" diff --git a/core/res/blake_pow.json b/core/res/blake_pow.json index ac492d7936..7e2d4f9483 100644 --- a/core/res/blake_pow.json +++ b/core/res/blake_pow.json @@ -25,15 +25,15 @@ "extraData": "0x" }, "accounts": { - "0000000000000000000000000000000000000001": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000002": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000003": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000004": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000005": { "balance": "100" }, - "0000000000000000000000000000000000000006": { "balance": "100" }, - "0000000000000000000000000000000000000007": { "balance": "100" }, - "0000000000000000000000000000000000000008": { "balance": "100" }, - "a6594b7196808d161b6fb137e781abbc251385d9": { "balance": "100" }, + "0000000000000000000000000000000000000001": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000002": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000003": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000004": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000005": { "balance": "1000000" }, + "0000000000000000000000000000000000000006": { "balance": "1000000" }, + "0000000000000000000000000000000000000007": { "balance": "1000000" }, + "0000000000000000000000000000000000000008": { "balance": "1000000" }, + "a6594b7196808d161b6fb137e781abbc251385d9": { "balance": "1000000" }, "9cce34f7ab185c7aba1b7c8140d620b4bda941d6": { "balance": "1606938044258990275541962092341162602522202993782792835301376", "nonce": "0" } }, "shards": { diff --git a/core/res/cuckoo.json b/core/res/cuckoo.json index a172891070..ca965f0ced 100644 --- a/core/res/cuckoo.json +++ b/core/res/cuckoo.json @@ -28,15 +28,15 @@ "extraData": "0x" }, "accounts": { - "0000000000000000000000000000000000000001": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000002": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000003": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000004": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000005": { "balance": "100" }, - "0000000000000000000000000000000000000006": { "balance": "100" }, - "0000000000000000000000000000000000000007": { "balance": "100" }, - "0000000000000000000000000000000000000008": { "balance": "100" }, - "a6594b7196808d161b6fb137e781abbc251385d9": { "balance": "100" }, + "0000000000000000000000000000000000000001": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000002": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000003": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000004": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000005": { "balance": "1000000" }, + "0000000000000000000000000000000000000006": { "balance": "1000000" }, + "0000000000000000000000000000000000000007": { "balance": "1000000" }, + "0000000000000000000000000000000000000008": { "balance": "1000000" }, + "a6594b7196808d161b6fb137e781abbc251385d9": { "balance": "1000000" }, "9cce34f7ab185c7aba1b7c8140d620b4bda941d6": { "balance": "1606938044258990275541962092341162602522202993782792835301376", "nonce": "0" } }, "shards": { diff --git a/core/res/null.json b/core/res/null.json index 70b48bb9a2..0efcf7fd0b 100644 --- a/core/res/null.json +++ b/core/res/null.json @@ -22,15 +22,15 @@ "extraData": "0x" }, "accounts": { - "0000000000000000000000000000000000000001": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000002": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000003": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000004": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000005": { "balance": "100" }, - "0000000000000000000000000000000000000006": { "balance": "100" }, - "0000000000000000000000000000000000000007": { "balance": "100" }, - "0000000000000000000000000000000000000008": { "balance": "100" }, - "a6594b7196808d161b6fb137e781abbc251385d9": { "balance": "100" }, + "0000000000000000000000000000000000000001": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000002": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000003": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000004": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000005": { "balance": "1000000" }, + "0000000000000000000000000000000000000006": { "balance": "1000000" }, + "0000000000000000000000000000000000000007": { "balance": "1000000" }, + "0000000000000000000000000000000000000008": { "balance": "1000000" }, + "a6594b7196808d161b6fb137e781abbc251385d9": { "balance": "1000000" }, "9cce34f7ab185c7aba1b7c8140d620b4bda941d6": { "balance": "1606938044258990275541962092341162602522202993782792835301376", "nonce": "0" } }, "shards": { diff --git a/core/res/solo.json b/core/res/solo.json index 7c2696146c..149405dbe5 100644 --- a/core/res/solo.json +++ b/core/res/solo.json @@ -23,15 +23,15 @@ "extraData": "0x" }, "accounts": { - "0000000000000000000000000000000000000001": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000002": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000003": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000004": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000005": { "balance": "100" }, - "0000000000000000000000000000000000000006": { "balance": "100" }, - "0000000000000000000000000000000000000007": { "balance": "100" }, - "0000000000000000000000000000000000000008": { "balance": "100" }, - "a6594b7196808d161b6fb137e781abbc251385d9": { "balance": "100" }, + "0000000000000000000000000000000000000001": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000002": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000003": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000004": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000005": { "balance": "1000000" }, + "0000000000000000000000000000000000000006": { "balance": "1000000" }, + "0000000000000000000000000000000000000007": { "balance": "1000000" }, + "0000000000000000000000000000000000000008": { "balance": "1000000" }, + "a6594b7196808d161b6fb137e781abbc251385d9": { "balance": "1000000" }, "9cce34f7ab185c7aba1b7c8140d620b4bda941d6": { "balance": "1606938044258990275541962092341162602522202993782792835301376", "nonce": "0" } }, "shards": { diff --git a/core/res/solo_authority.json b/core/res/solo_authority.json index 441541fd79..0e57293f86 100644 --- a/core/res/solo_authority.json +++ b/core/res/solo_authority.json @@ -27,15 +27,15 @@ "extraData": "0x" }, "accounts": { - "0000000000000000000000000000000000000001": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000002": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000003": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000004": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000005": { "balance": "100" }, - "0000000000000000000000000000000000000006": { "balance": "100" }, - "0000000000000000000000000000000000000007": { "balance": "100" }, - "0000000000000000000000000000000000000008": { "balance": "100" }, - "a6594b7196808d161b6fb137e781abbc251385d9": { "balance": "100" }, + "0000000000000000000000000000000000000001": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000002": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000003": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000004": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000005": { "balance": "1000000" }, + "0000000000000000000000000000000000000006": { "balance": "1000000" }, + "0000000000000000000000000000000000000007": { "balance": "1000000" }, + "0000000000000000000000000000000000000008": { "balance": "1000000" }, + "a6594b7196808d161b6fb137e781abbc251385d9": { "balance": "1000000" }, "9cce34f7ab185c7aba1b7c8140d620b4bda941d6": { "balance": "1606938044258990275541962092341162602522202993782792835301376", "nonce": "0" } }, "shards": { diff --git a/core/res/tendermint.json b/core/res/tendermint.json index 184b4a15df..23e9a08e85 100644 --- a/core/res/tendermint.json +++ b/core/res/tendermint.json @@ -39,15 +39,15 @@ "extraData": "0x" }, "accounts": { - "0000000000000000000000000000000000000001": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000002": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000003": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000004": { "balance": "100", "nonce": "0" }, - "0000000000000000000000000000000000000005": { "balance": "100" }, - "0000000000000000000000000000000000000006": { "balance": "100" }, - "0000000000000000000000000000000000000007": { "balance": "100" }, - "0000000000000000000000000000000000000008": { "balance": "100" }, - "a6594b7196808d161b6fb137e781abbc251385d9": { "balance": "100" }, + "0000000000000000000000000000000000000001": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000002": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000003": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000004": { "balance": "1000000", "nonce": "0" }, + "0000000000000000000000000000000000000005": { "balance": "1000000" }, + "0000000000000000000000000000000000000006": { "balance": "1000000" }, + "0000000000000000000000000000000000000007": { "balance": "1000000" }, + "0000000000000000000000000000000000000008": { "balance": "1000000" }, + "a6594b7196808d161b6fb137e781abbc251385d9": { "balance": "1000000" }, "9cce34f7ab185c7aba1b7c8140d620b4bda941d6": { "balance": "1606938044258990275541962092341162602522202993782792835301376", "nonce": "0" } }, "shards": { diff --git a/core/src/consensus/null_engine/mod.rs b/core/src/consensus/null_engine/mod.rs index c0bbba7d9a..e8cd0d7336 100644 --- a/core/src/consensus/null_engine/mod.rs +++ b/core/src/consensus/null_engine/mod.rs @@ -51,12 +51,12 @@ impl ConsensusEngine for NullEngine { &self.machine } + fn verify_local_seal(&self, _header: &M::Header) -> Result<(), M::Error> { + Ok(()) + } + fn on_close_block(&self, block: &mut M::LiveBlock) -> Result<(), M::Error> { let author = *LiveBlock::header(&*block).author(); self.machine.add_balance(block, &author, &self.params.block_reward) } - - fn verify_local_seal(&self, _header: &M::Header) -> Result<(), M::Error> { - Ok(()) - } } diff --git a/core/src/header.rs b/core/src/header.rs index debb6bebc8..f1214bf33d 100644 --- a/core/src/header.rs +++ b/core/src/header.rs @@ -332,3 +332,12 @@ impl ::machine::Header for Header { Header::number(self) } } + +impl ::machine::ScoredHeader for Header { + fn score(&self) -> &U256 { + self.score() + } + fn set_score(&mut self, score: U256) { + self.set_score(score) + } +} diff --git a/core/src/machine.rs b/core/src/machine.rs index 9a120f1936..fabb106d84 100644 --- a/core/src/machine.rs +++ b/core/src/machine.rs @@ -35,6 +35,15 @@ pub trait Header { fn number(&self) -> u64; } +/// A header with an associated score (difficulty in PoW terms) +pub trait ScoredHeader: Header { + /// Get the score of this header. + fn score(&self) -> &U256; + + /// Set the score of this header. + fn set_score(&mut self, score: U256); +} + /// A "live" block is one which is in the process of the transition. /// The state of this block can be mutated by arbitrary rules of the /// state transition function. diff --git a/core/src/parcel.rs b/core/src/parcel.rs index f4d5f9b38d..1d87a0e9b6 100644 --- a/core/src/parcel.rs +++ b/core/src/parcel.rs @@ -77,8 +77,8 @@ pub fn parcel_error_message(error: &ParcelError) -> String { match error { AlreadyImported => "Already imported".into(), Old => "No longer valid".into(), - TooCheapToReplace => "Gas price too low to replace".into(), - InvalidNetworkId => "Parcel of this network ID is not allowed on this chain.".into(), + TooCheapToReplace => "Fee too low to replace".into(), + InvalidNetworkId => "This network ID is not allowed on this chain".into(), MetadataTooBig => "Metadata size is too big.".into(), LimitReached => "Parcel limit reached".into(), InsufficientFee { @@ -375,14 +375,25 @@ impl UnverifiedParcel { for t in transactions { match &t { Transaction::AssetMint { + network_id, metadata, .. } => { if metadata.len() > params.max_metadata_size { return Err(ParcelError::MetadataTooBig) } + if network_id != &self.network_id { + return Err(ParcelError::InvalidNetworkId) + } + } + Transaction::AssetTransfer { + network_id, + .. + } => { + if network_id != &self.network_id { + return Err(ParcelError::InvalidNetworkId) + } } - _ => {} } } } diff --git a/core/src/transaction.rs b/core/src/transaction.rs index b71fc7db0c..ca55df18db 100644 --- a/core/src/transaction.rs +++ b/core/src/transaction.rs @@ -29,6 +29,7 @@ use super::parcel::{AssetTransferInput, AssetTransferOutput}; pub enum Transaction { #[serde(rename_all = "camelCase")] AssetMint { + network_id: u64, metadata: String, lock_script_hash: H256, parameters: Vec, @@ -101,16 +102,17 @@ impl Decodable for Transaction { fn decode(d: &UntrustedRlp) -> Result { match d.val_at(0)? { ASSET_MINT_ID => { - if d.item_count()? != 7 { + if d.item_count()? != 8 { return Err(DecoderError::RlpIncorrectListLen) } Ok(Transaction::AssetMint { - metadata: d.val_at(1)?, - lock_script_hash: d.val_at(2)?, - parameters: d.val_at(3)?, - amount: d.val_at(4)?, - registrar: d.val_at(5)?, - nonce: d.val_at(6)?, + network_id: d.val_at(1)?, + metadata: d.val_at(2)?, + lock_script_hash: d.val_at(3)?, + parameters: d.val_at(4)?, + amount: d.val_at(5)?, + registrar: d.val_at(6)?, + nonce: d.val_at(7)?, }) } ASSET_TRANSFER_ID => { @@ -134,14 +136,16 @@ impl Encodable for Transaction { fn rlp_append(&self, s: &mut RlpStream) { match self { Transaction::AssetMint { + network_id, metadata, lock_script_hash, parameters, amount, registrar, nonce, - } => s.begin_list(7) + } => s.begin_list(8) .append(&ASSET_MINT_ID) + .append(network_id) .append(metadata) .append(lock_script_hash) .append(parameters) diff --git a/docs/basic-usage.rst b/docs/basic-usage.rst index ea2bb031df..d2c1d11f23 100644 --- a/docs/basic-usage.rst +++ b/docs/basic-usage.rst @@ -46,7 +46,7 @@ The way each node is connected does not matter, as long as each node is connecte the node with a secret key of 1, use this command: :: - codechain --db-path db/db0 --port 3485 --jsonrpc-port 8080 --secret-key 0000000000000000000000000000000000000000000000000000000000000001 -c tendermint --bootstrap-addresses 127.0.0.1:8080 + codechain --db-path db/db1 --port 3486 --jsonrpc-port 8081 --secret-key 0000000000000000000000000000000000000000000000000000000000000002 -c tendermint --bootstrap-addresses 127.0.0.1:3485 Checking if CodeChain is Configured Properly ============================================ diff --git a/spec/JSON-RPC.md b/spec/JSON-RPC.md index 30976187fa..6262437c32 100644 --- a/spec/JSON-RPC.md +++ b/spec/JSON-RPC.md @@ -11,68 +11,76 @@ In the current version, it's only supported through HTTP. # List of types -## block object +## H160, H256, H512, ... - - author: `160 bit hexadecimal string` - - extraData: Array of any - - hash: `256 bit hexadecimal string` - - invoicesRoot: `string` +A XXX-bit hexadecimal string. (e.g. H160: 160-bit hexadecimal string) + +## U128, U256, U512, ... + +A hexadecimal string for XXX-bit unsigned integer + +## BlockObject + + - author: `H160` + - extraData: `any[]` + - hash: `H256` + - invoicesRoot: `H256` - number: `number` - - parcels: Array of `parcel object` - - parcelsRoot: `256 bit hexadecimal string` - - parentHash: `256 bit hexadecimal string` + - parcels: `ParcelObject[]` + - parcelsRoot: `H256` + - parentHash: `H256` - score: `number` - - seal: Array of `string` - - stateRoot: `256 bit hexadecimal string` + - seal: `string[]` + - stateRoot: `H256` - timestamp: `number` -## parcel object +## ParcelObject - - blockHash: `256 bit hexadecimal string` + - blockHash: `H256` - blockNumber: `number` - - fee: `hexadecimal string of 256 bit unsigned integer` - - hash: `256 bit hexadecimal string` + - fee: `U256` + - hash: `H256` - networkId: `number` - - nonce: `hexadecimal string of 256 bit unsigned integer` + - nonce: `U256` - parcelIndex: `number` - - sig: `520 bit hexadecimal string` for ECDSA signature or `512 bit hexadecimal string` for Schnorr signature - - action: `action object` + - sig: `H520` for ECDSA signature | `H512` for Schnorr signature + - action: `ActionObject` -## action objects +## ActionObjects -### ChangeShardState action object +### ChangeShardState ActionObject - action: "changeShardState" - - transactions: Array of `transaction object` + - transactions: `TransactionObject[]` -### Payment action object +### Payment ActionObject - action: "payment" - - receiver: `160 bit hexadecimal string` - - amount: `hexadecimal string of 256 bit unsigned integer` + - receiver: `H160` + - amount: `U256` -### SetRegularKey action object +### SetRegularKey ActionObject - action: "setRegularKey" - - key: `512 bit hexadecimal string` + - key: `H512` -## transaction object +## TransactionObject - - type: `string` - "assetMint" | "assetTransfer" - - data: `asset mint object` or `asset transfer object` + - type: "assetMint" | "assetTransfer" + - data: `AssetMintObject` | `AssetTransferObject` -## asset scheme object +## AssetSchemeObject - amount: `number` - metadata: `string` - - registrar: `160 bit hexadecimal string` or `null` + - registrar: `H160` | `null` -## asset object +## AssetObject - amount: `number` - - asset_type: `256 bit hexadecimal string` - - lock_script_hash: `256 bit hexadecimal string` - - parameters: Array of `hexadecimal string` + - asset_type: `H256` + - lock_script_hash: `H256` + - parameters: `hexadecimal string[]` # List of methods @@ -128,7 +136,11 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":"pong","id":null} +{ + "jsonrpc":"2.0", + "result":"pong", + "id":null +} ``` ## version @@ -148,7 +160,11 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":"0.1.0","id":null} +{ + "jsonrpc":"2.0", + "result":"0.1.0", + "id":null +} ``` ## chain_getBestBlockNumber @@ -168,7 +184,11 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":1,"id":null} +{ + "jsonrpc":"2.0", + "result":1, + "id":null +} ``` ## chain_getBestBlockId @@ -176,7 +196,7 @@ Gets the number and the hash of the best block. Params: No parameters -Return Type: { number: `number`, hash: `256 bit hexadecimal string` } +Return Type: { number: `number`, hash: `H256` } Request Example ``` @@ -188,7 +208,14 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":{"hash":"0x56642f04d519ae3262c7ba6facf1c5b11450ebaeb7955337cfbc45420d573077","number":1},"id":null} +{ + "jsonrpc":"2.0", + "result":{ + "hash":"0x56642f04d519ae3262c7ba6facf1c5b11450ebaeb7955337cfbc45420d573077", + "number":1 + }, + "id":null +} ``` ## chain_getBlockHash @@ -197,7 +224,7 @@ Gets the hash of the block with given number. Params: 1. n - `number` -Return Type: `null` or `256 bit hexadecimal string` +Return Type: `null` | `H256` Request Example: ``` @@ -209,16 +236,20 @@ Request Example: Response Example ``` -{"jsonrpc":"2.0","result":"0x56642f04d519ae3262c7ba6facf1c5b11450ebaeb7955337cfbc45420d573077","id":null} +{ + "jsonrpc":"2.0", + "result":"0x56642f04d519ae3262c7ba6facf1c5b11450ebaeb7955337cfbc45420d573077", + "id":null +} ``` ## chain_getBlockByHash Gets block with given hash. Params: - 1. hash: `256 bit hexadecimal string` + 1. hash: `H256` -Return Type: `null` or `block object` +Return Type: `null` | `BlockObject` Request Example: ``` @@ -231,39 +262,45 @@ Request Example: Response Example ``` { - "id": null, - "jsonrpc": "2.0", - "result": { - "author": "0x84137e7a75043bed32e4458a45da7549a8169b4d", - "extraData": [], - "hash": "0x49b5fda89dbfa92e9a744d3019790107757d189608e2cfe15e796825f4561959", - "invoicesRoot": "0x45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0", - "number": 1, - "parcels": [ - { - "action": { - "action": "changeShardState", - "transactions": [] - }, - "blockHash": "0x49b5fda89dbfa92e9a744d3019790107757d189608e2cfe15e796825f4561959", - "blockNumber": 1, - "fee": "0xa", - "hash": "0x20dced7a95e82cf165bbb7ef111bfda24b664e3c3ffd5a255e970300eea5ec56", - "networkId": 17, - "nonce": "0x0", - "parcelIndex": 0, - "r": "0xab2f74e74344b0b24932c85e29a4039150ae0b9fab17398b7e138a70022fd09c", - "s": "0x364dd6aeee95f45cbd6773c3edc6507d07505f7fbfb5d85ce128d19fa104d2a6", - "v": 1 - } - ], - "parcelsRoot": "0x934b77fa1ff7f405127de3c63efd44b92dad7ee4ff923c9b77f06abebd4844a4", - "parentHash": "0xc2338c8fd5a9b4ca5dd5dd12fc548e796bbb953ee6043afa14377037d0387e25", - "score": "0x20000", - "seal": [], - "stateRoot": "0x223ac1b388a6f3a2e001482d328c7f6f3b8f0b8686d3988224870a8fed99c8b1", - "timestamp": 1530694371 - } + "id":null, + "jsonrpc":"2.0", + "result":{ + "author":"0x84137e7a75043bed32e4458a45da7549a8169b4d", + "extraData":[ + + ], + "hash":"0x49b5fda89dbfa92e9a744d3019790107757d189608e2cfe15e796825f4561959", + "invoicesRoot":"0x45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0", + "number":1, + "parcels":[ + { + "action":{ + "action":"changeShardState", + "transactions":[ + + ] + }, + "blockHash":"0x49b5fda89dbfa92e9a744d3019790107757d189608e2cfe15e796825f4561959", + "blockNumber":1, + "fee":"0xa", + "hash":"0x20dced7a95e82cf165bbb7ef111bfda24b664e3c3ffd5a255e970300eea5ec56", + "networkId":17, + "nonce":"0x0", + "parcelIndex":0, + "r":"0xab2f74e74344b0b24932c85e29a4039150ae0b9fab17398b7e138a70022fd09c", + "s":"0x364dd6aeee95f45cbd6773c3edc6507d07505f7fbfb5d85ce128d19fa104d2a6", + "v":1 + } + ], + "parcelsRoot":"0x934b77fa1ff7f405127de3c63efd44b92dad7ee4ff923c9b77f06abebd4844a4", + "parentHash":"0xc2338c8fd5a9b4ca5dd5dd12fc548e796bbb953ee6043afa14377037d0387e25", + "score":"0x20000", + "seal":[ + + ], + "stateRoot":"0x223ac1b388a6f3a2e001482d328c7f6f3b8f0b8686d3988224870a8fed99c8b1", + "timestamp":1530694371 + } } ``` @@ -273,7 +310,7 @@ Sends signed parcel, returning its hash. Params: 1. bytes: `hexadecimal string` - RLP encoded hex string of SignedParcel -Return Type: `256 bit hexadecimal string` - parcel hash +Return Type: `H256` - parcel hash Request Example: ``` @@ -285,16 +322,20 @@ Request Example: Response Example ``` -{"jsonrpc":"2.0","result":"0x20dced7a95e82cf165bbb7ef111bfda24b664e3c3ffd5a255e970300eea5ec56","id":null} +{ + "jsonrpc":"2.0", + "result":"0x20dced7a95e82cf165bbb7ef111bfda24b664e3c3ffd5a255e970300eea5ec56", + "id":null +} ``` ## chain_getParcel Gets parcel with given hash. Params: - 1. parcel hash - `256 bit hexadecimal string` + 1. parcel hash - `H256` -Return Type: `null` or `parcel object` +Return Type: `null` or `ParcelObject` Request Example ``` @@ -332,9 +373,9 @@ Response Example Gets a parcel invoice with given hash. Params: - 1. parcel hash - `256 bit hexadecimal string` + 1. parcel hash - `H256` -Return Type: `null` or Array of string. The string either `Success` or `Failed` +Return Type: `null` | string[]. The string either "Success" or "Failed" Request Example ``` @@ -346,16 +387,22 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":["Success"],"id":null} +{ + "jsonrpc":"2.0", + "result":[ + "Success" + ], + "id":null +} ``` ## chain_getTransactionInvoice Gets transaction invoice with given hash Params: - 1. transaction hash - `256 bit hexadecimal string` + 1. transaction hash - `H256` -Return Type: `null` or string `Success` or `Failed` +Return Type: `null` | "Success" | "Failed" Request Example ``` @@ -367,16 +414,20 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":"Success","id":null} +{ + "jsonrpc":"2.0", + "result":"Success", + "id":null +} ``` ## chain_getAssetScheme Gets asset scheme with given asset type. Params: - 1. transaction hash of AssetMintTransaction - `256 bit hexadecimal string` + 1. transaction hash of AssetMintTransaction - `H256` -Return Type: `null` or `asset scheme object` +Return Type: `null` | `AssetSchemeObject` Request Example ``` @@ -388,21 +439,25 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":{ - "amount":100, - "metadata":"", - "registrar":null -},"id":null} +{ + "jsonrpc":"2.0", + "result":{ + "amount":100, + "metadata":"", + "registrar":null + }, + "id":null +} ``` ## chain_getAsset Gets asset with given asset type. Params: - 1. transaction hash of AssetMintTransaction or AssetTransferTransaction - `256 bit hexadecimal string` + 1. transaction hash - `H256` 2. index - `number` -Return Type: `null` or `asset object` +Return Type: `null` | `AssetObject` Request Example ``` @@ -414,22 +469,28 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":{ - "amount":100, - "asset_type":"0x53000000000000002ec1193ecd52e2833ffc10b45bea1fda49f857e34db67c68", - "lock_script_hash":"0x0000000000000000000000000000000000000000000000000000000000000000", - "parameters":[] -},"id":null} +{ + "jsonrpc":"2.0", + "result":{ + "amount":100, + "asset_type":"0x53000000000000002ec1193ecd52e2833ffc10b45bea1fda49f857e34db67c68", + "lock_script_hash":"0x0000000000000000000000000000000000000000000000000000000000000000", + "parameters":[ + + ] + }, + "id":null +} ``` ## chain_getNonce Gets nonce of an account of given address, at state of given blockNumber. Params: - 1. address: `160 bit hexadecimal string` - 2. block number: `number` or `null` + 1. address: `H160` + 2. block number: `number` | `null` -Return Type: `hexadecimal string for 256 bit unsigned integer` +Return Type: `U256` Request Example ``` @@ -441,17 +502,21 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":"0x54","id":null} +{ + "jsonrpc":"2.0", + "result":"0x54", + "id":null +} ``` ## chain_getBalance Gets balance of an account of given address, at state of given blockNumber. Params: - 1. address: `160 bit hexadecimal string` - 2. block number: `number` or `null` + 1. address: `H160` + 2. block number: `number` | `null` -Return Type: `hexadecimal string for 256 bit unsigned integer` +Return Type: `U256` Request Example ``` @@ -463,17 +528,21 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":"0xe8d4a50dd0","id":null} +{ + "jsonrpc":"2.0", + "result":"0xe8d4a50dd0", + "id":null +} ``` ## chain_getRegularKey Gets the regular key of an account of given address, at state of given blockNumber. Params: - 1. address: `160 bit hexadecimal string` - 2. block number: `number` or `null` + 1. address: `H160` + 2. block number: `number` | `null` -Return Type: `512 bit hexadecimal string` - 512-bit public key +Return Type: `H512` - 512-bit public key Request Example ``` @@ -485,14 +554,18 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","id":null} +{ + "jsonrpc":"2.0", + "result":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "id":null +} ``` ## chain_getNumberOfShards Gets the number of shards, at state of given blockNumber. Param: -1. block number: `number` or `null` +1. block number: `number` | `null` Return Type: `number` - the number of shards @@ -506,7 +579,11 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":3,"id":null} +{ + "jsonrpc":"2.0", + "result":3, + "id":null +} ``` ## chain_getShardRoot @@ -514,9 +591,9 @@ Gets the root of shard, at state of given blockNumber. Param: 1. shard id: `number` -1. block number: `number` or `null` +1. block number: `number` | `null` -Return Type: `null` or `256 bit hexadecimal string` - the root of shard +Return Type: `null` | `H256` - the root of shard Request Example ``` @@ -528,7 +605,11 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":"0xf3841adc1615bfeabb801dda23585c1722b80d810df084a5f2198e92285d4bfd","id":null} +{ + "jsonrpc":"2.0", + "result":"0xf3841adc1615bfeabb801dda23585c1722b80d810df084a5f2198e92285d4bfd", + "id":null +} ``` @@ -537,7 +618,7 @@ Gets parcels in the current parcel queue. Params: No parameters -Return Type: Array of `parcel object` +Return Type: `ParcelObject[]` Request Example ``` @@ -549,26 +630,34 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":[{ - "blockHash":null, - "blockNumber":null, - "fee":"0xa", - "hash":"0x8ae3363ccdcc02d8d662d384deee34fb89d1202124e8065f0d6c84ab31e68d8a", - "networkId":17, - "nonce":"0x0", - "parcelIndex":null, - "r":"0x22605d6b9fb713d3a415e02eeed8b4a630e0d867c91bf7d9b7721f94159c0fe1", - "s":"0x772f19f1c27f1db8b28289caa9e99ad756878fd56b2415c25cd47cc737f7e0c2", - "transactions":[{ - "payment":{ - "nonce":"0x1", - "receiver":"0xa6594b7196808d161b6fb137e781abbc251385d9", - "sender":"0xa6594b7196808d161b6fb137e781abbc251385d9", - "value":"0x0" +{ + "jsonrpc":"2.0", + "result":[ + { + "blockHash":null, + "blockNumber":null, + "fee":"0xa", + "hash":"0x8ae3363ccdcc02d8d662d384deee34fb89d1202124e8065f0d6c84ab31e68d8a", + "networkId":17, + "nonce":"0x0", + "parcelIndex":null, + "r":"0x22605d6b9fb713d3a415e02eeed8b4a630e0d867c91bf7d9b7721f94159c0fe1", + "s":"0x772f19f1c27f1db8b28289caa9e99ad756878fd56b2415c25cd47cc737f7e0c2", + "transactions":[ + { + "payment":{ + "nonce":"0x1", + "receiver":"0xa6594b7196808d161b6fb137e781abbc251385d9", + "sender":"0xa6594b7196808d161b6fb137e781abbc251385d9", + "value":"0x0" + } + } + ], + "v":0 } - }], - "v":0 -}],"id":null} + ], + "id":null +} ``` ## chain_getCoinbase @@ -576,7 +665,7 @@ Response Example Params: No parameters -Return Type: `160 bit hexadecimal string` or `null` +Return Type: `H160` | `null` Request Example ``` @@ -588,7 +677,11 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":"0xa6594b7196808d161b6fb137e781abbc251385d9","id":null} +{ + "jsonrpc":"2.0", + "result":"0xa6594b7196808d161b6fb137e781abbc251385d9", + "id":null +} ``` ## miner_getWork @@ -596,7 +689,7 @@ Returns the hash of the current block, the score and the block number. Params: No parameters -Return Type: `work object` +Return Type: `WorkObject` Request Example ``` @@ -608,10 +701,14 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":{ - "powHash": "0x56642f04d519ae3262c7ba6facf1c5b11450ebaeb7955337cfbc45420d573077", - "target": 100 -},"id":null} +{ + "jsonrpc":"2.0", + "result":{ + "powHash":"0x56642f04d519ae3262c7ba6facf1c5b11450ebaeb7955337cfbc45420d573077", + "target":100 + }, + "id":null +} ``` ## miner_submitWork @@ -619,7 +716,7 @@ Used for submitting a proof-of-work solution. Params: 1. powHash: `string` - 1. seal: Array of `string` + 1. seal: `string[]` Return Type: `bool` @@ -633,7 +730,11 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":true,"id":6} +{ + "jsonrpc":"2.0", + "result":true, + "id":6 +} ``` ## net_shareSecret @@ -656,7 +757,11 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":null,"id":5} +{ + "jsonrpc":"2.0", + "result":null, + "id":5 +} ``` ## net_connect @@ -678,7 +783,11 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":null,"id":5} +{ + "jsonrpc":"2.0", + "result":null, + "id":5 +} ``` ## net_isConnected @@ -700,7 +809,11 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":true,"id":6} +{ + "jsonrpc":"2.0", + "result":true, + "id":6 +} ``` ## net_disconnect @@ -722,7 +835,11 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":true,"id":6} +{ + "jsonrpc":"2.0", + "result":true, + "id":6 +} ``` ## devel_getStateTrieKeys @@ -732,7 +849,7 @@ Params: 1. offset: `number` 2. limit: `number` -Return Type: Array of `string` with maximum length _limit_ +Return Type: `string[]` with maximum length _limit_ Request Example ``` @@ -744,7 +861,13 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":["0x00acf5cba5c53e11f1512b8b480521cb546e7a17a96235a9282f6253b90de043"],"id":null} +{ + "jsonrpc":"2.0", + "result":[ + "0x00acf5cba5c53e11f1512b8b480521cb546e7a17a96235a9282f6253b90de043" + ], + "id":null +} ``` ## devel_getStateTrieValue @@ -753,7 +876,7 @@ Gets the value of the state trie with given key. Params: 1. key: `string` -Return Type: Array of `string` - each string is RLP encoded +Return Type: `string[]` - each string is RLP encoded Request Example ``` @@ -765,5 +888,12 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":["0x20d560025f3a1c6675cb32384355ae05b224a3473ae17d3d15b6aa164af7d717","0xf84541a053000000000000002ab33f741ba153ff1ffdf1107845828637c864d5360e4932a00000000000000000000000000000000000000000000000000000000000000000c06f"],"id":null} +{ + "jsonrpc":"2.0", + "result":[ + "0x20d560025f3a1c6675cb32384355ae05b224a3473ae17d3d15b6aa164af7d717", + "0xf84541a053000000000000002ab33f741ba153ff1ffdf1107845828637c864d5360e4932a00000000000000000000000000000000000000000000000000000000000000000c06f" + ], + "id":null +} ``` diff --git a/sync/Cargo.toml b/sync/Cargo.toml index ef0abf2e43..efc4ff4efb 100644 --- a/sync/Cargo.toml +++ b/sync/Cargo.toml @@ -18,6 +18,7 @@ parking_lot = "0.5" patricia-trie = { path = "../util/patricia_trie" } rand = "0.5.3" rlp = { path = "../util/rlp" } +snap = "0.2" time = "0.1" triehash = { path = "../util/triehash" } diff --git a/sync/src/lib.rs b/sync/src/lib.rs index 035d49ad82..6309faeb22 100644 --- a/sync/src/lib.rs +++ b/sync/src/lib.rs @@ -30,6 +30,7 @@ extern crate patricia_trie as trie; extern crate rand; #[cfg_attr(test, macro_use)] extern crate rlp; +extern crate snap; extern crate time; extern crate triehash; diff --git a/sync/src/snapshot/service.rs b/sync/src/snapshot/service.rs index 9f0d5c0a14..e7a20b9359 100644 --- a/sync/src/snapshot/service.rs +++ b/sync/src/snapshot/service.rs @@ -25,6 +25,7 @@ use ctypes::H256; use kvdb::KeyValueDB; use rlp::{decode as rlp_decode, RlpStream}; +use snap; use trie::{Node, OwnedNode}; use super::error::Error; @@ -91,6 +92,7 @@ fn write_snapshot(db: Arc, path: PathBuf, root: &H256) -> Result<(), { let mut file = File::create(path.join("head"))?; + let mut snappy = snap::Writer::new(Write::by_ref(&mut file)); let mut stream = RlpStream::new(); stream.begin_unbounded_list(); @@ -101,12 +103,14 @@ fn write_snapshot(db: Arc, path: PathBuf, root: &H256) -> Result<(), } stream.complete_unbounded_list(); - file.write(&stream.drain())?; + snappy.write(&stream.drain())?; } for (grandchild, _) in &grandchildren { let nodes = enumerate_subtree(&db, grandchild)?; let mut file = File::create(path.join(format!("{:x}", grandchild)))?; + let mut snappy = snap::Writer::new(Write::by_ref(&mut file)); + let mut stream = RlpStream::new(); stream.begin_unbounded_list(); for (key, value) in nodes { @@ -115,7 +119,8 @@ fn write_snapshot(db: Arc, path: PathBuf, root: &H256) -> Result<(), stream.append(&value); } stream.complete_unbounded_list(); - file.write(&stream.drain())?; + + snappy.write(&stream.drain())?; } Ok(())