Move a bunch of stuff around (#10101)
* Move devtools. * Merge stop_guard & rename memzero * Move price-info to miner. * Group account management * Clean up workspace members. * Move local store closer to miner. * Move clib examples. * Move registrar and hash-fetch * Move rpc_cli/rpc_client * Move stratum closer to miner. * Fix naming convention of crates. * Update Cpp examples path. * Fix paths for clib-example. * Fix removing build.
This commit is contained in:
42
util/rlp-compress/src/common.rs
Normal file
42
util/rlp-compress/src/common.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
// Copyright 2015-2017 Parity Technologies
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
//! Contains RLPs used for compression.
|
||||
|
||||
use Swapper;
|
||||
|
||||
lazy_static! {
|
||||
/// Swapper for snapshot compression.
|
||||
pub static ref SNAPSHOT_SWAPPER: Swapper<'static> = Swapper::new(EMPTY_RLPS, INVALID_RLPS);
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
/// Swapper with common long RLPs, up to 127 can be added.
|
||||
pub static ref BLOCKS_SWAPPER: Swapper<'static> = Swapper::new(COMMON_RLPS, INVALID_RLPS);
|
||||
}
|
||||
|
||||
static EMPTY_RLPS: &'static [&'static [u8]] = &[
|
||||
// RLP of KECCAK_NULL_RLP
|
||||
&[160, 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, 110, 91, 72, 224, 27, 153, 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33],
|
||||
// RLP of KECCAK_EMPTY
|
||||
&[160, 197, 210, 70, 1, 134, 247, 35, 60, 146, 126, 125, 178, 220, 199, 3, 192, 229, 0, 182, 83, 202, 130, 39, 59, 123, 250, 216, 4, 93, 133, 164, 112]
|
||||
];
|
||||
|
||||
static COMMON_RLPS: &'static [&'static [u8]] = &[
|
||||
// RLP of KECCAK_NULL_RLP
|
||||
&[160, 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, 110, 91, 72, 224, 27, 153, 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33],
|
||||
// RLP of KECCAK_EMPTY
|
||||
&[160, 197, 210, 70, 1, 134, 247, 35, 60, 146, 126, 125, 178, 220, 199, 3, 192, 229, 0, 182, 83, 202, 130, 39, 59, 123, 250, 216, 4, 93, 133, 164, 112],
|
||||
// Other RLPs found in blocks DB using the test below.
|
||||
&[160, 29, 204, 77, 232, 222, 199, 93, 122, 171, 133, 181, 103, 182, 204, 212, 26, 211, 18, 69, 27, 148, 138, 116, 19, 240, 161, 66, 253, 64, 212, 147, 71],
|
||||
&[148, 50, 190, 52, 59, 148, 248, 96, 18, 77, 196, 254, 226, 120, 253, 203, 211, 140, 16, 45, 136],
|
||||
&[148, 82, 188, 68, 213, 55, 131, 9, 238, 42, 191, 21, 57, 191, 113, 222, 27, 125, 123, 227, 181],
|
||||
&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||
];
|
||||
|
||||
static INVALID_RLPS: &'static [&'static [u8]] = &[&[0x81, 0x0], &[0x81, 0x1], &[0x81, 0x2], &[0x81, 0x3], &[0x81, 0x4], &[0x81, 0x5], &[0x81, 0x6], &[0x81, 0x7], &[0x81, 0x8], &[0x81, 0x9], &[0x81, 0xa], &[0x81, 0xb], &[0x81, 0xc], &[0x81, 0xd], &[0x81, 0xe], &[0x81, 0xf], &[0x81, 0x10], &[0x81, 0x11], &[0x81, 0x12], &[0x81, 0x13], &[0x81, 0x14], &[0x81, 0x15], &[0x81, 0x16], &[0x81, 0x17], &[0x81, 0x18], &[0x81, 0x19], &[0x81, 0x1a], &[0x81, 0x1b], &[0x81, 0x1c], &[0x81, 0x1d], &[0x81, 0x1e], &[0x81, 0x1f], &[0x81, 0x20], &[0x81, 0x21], &[0x81, 0x22], &[0x81, 0x23], &[0x81, 0x24], &[0x81, 0x25], &[0x81, 0x26], &[0x81, 0x27], &[0x81, 0x28], &[0x81, 0x29], &[0x81, 0x2a], &[0x81, 0x2b], &[0x81, 0x2c], &[0x81, 0x2d], &[0x81, 0x2e], &[0x81, 0x2f], &[0x81, 0x30], &[0x81, 0x31], &[0x81, 0x32], &[0x81, 0x33], &[0x81, 0x34], &[0x81, 0x35], &[0x81, 0x36], &[0x81, 0x37], &[0x81, 0x38], &[0x81, 0x39], &[0x81, 0x3a], &[0x81, 0x3b], &[0x81, 0x3c], &[0x81, 0x3d], &[0x81, 0x3e], &[0x81, 0x3f], &[0x81, 0x40], &[0x81, 0x41], &[0x81, 0x42], &[0x81, 0x43], &[0x81, 0x44], &[0x81, 0x45], &[0x81, 0x46], &[0x81, 0x47], &[0x81, 0x48], &[0x81, 0x49], &[0x81, 0x4a], &[0x81, 0x4b], &[0x81, 0x4c], &[0x81, 0x4d], &[0x81, 0x4e], &[0x81, 0x4f], &[0x81, 0x50], &[0x81, 0x51], &[0x81, 0x52], &[0x81, 0x53], &[0x81, 0x54], &[0x81, 0x55], &[0x81, 0x56], &[0x81, 0x57], &[0x81, 0x58], &[0x81, 0x59], &[0x81, 0x5a], &[0x81, 0x5b], &[0x81, 0x5c], &[0x81, 0x5d], &[0x81, 0x5e], &[0x81, 0x5f], &[0x81, 0x60], &[0x81, 0x61], &[0x81, 0x62], &[0x81, 0x63], &[0x81, 0x64], &[0x81, 0x65], &[0x81, 0x66], &[0x81, 0x67], &[0x81, 0x68], &[0x81, 0x69], &[0x81, 0x6a], &[0x81, 0x6b], &[0x81, 0x6c], &[0x81, 0x6d], &[0x81, 0x6e], &[0x81, 0x6f], &[0x81, 0x70], &[0x81, 0x71], &[0x81, 0x72], &[0x81, 0x73], &[0x81, 0x74], &[0x81, 0x75], &[0x81, 0x76], &[0x81, 0x77], &[0x81, 0x78], &[0x81, 0x79], &[0x81, 0x7a], &[0x81, 0x7b], &[0x81, 0x7c], &[0x81, 0x7d], &[0x81, 0x7e]];
|
||||
109
util/rlp-compress/src/lib.rs
Normal file
109
util/rlp-compress/src/lib.rs
Normal file
@@ -0,0 +1,109 @@
|
||||
// Copyright 2015-2018 Parity Technologies
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
extern crate elastic_array;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
extern crate rlp;
|
||||
|
||||
mod common;
|
||||
|
||||
use std::cmp;
|
||||
use std::collections::HashMap;
|
||||
use elastic_array::ElasticArray1024;
|
||||
use rlp::{Rlp, RlpStream};
|
||||
use common::{SNAPSHOT_SWAPPER, BLOCKS_SWAPPER};
|
||||
|
||||
pub fn snapshot_swapper() -> &'static Swapper<'static> {
|
||||
&SNAPSHOT_SWAPPER as &Swapper
|
||||
}
|
||||
|
||||
pub fn blocks_swapper() -> &'static Swapper<'static> {
|
||||
&BLOCKS_SWAPPER as &Swapper
|
||||
}
|
||||
|
||||
/// A trait used to compress rlp.
|
||||
pub trait Compressor {
|
||||
/// Get compressed version of given rlp.
|
||||
fn compressed(&self, rlp: &[u8]) -> Option<&[u8]>;
|
||||
}
|
||||
|
||||
/// A trait used to convert compressed rlp into it's original version.
|
||||
pub trait Decompressor {
|
||||
/// Get decompressed rlp.
|
||||
fn decompressed(&self, compressed: &[u8]) -> Option<&[u8]>;
|
||||
}
|
||||
|
||||
/// Call this function to compress rlp.
|
||||
pub fn compress(c: &[u8], swapper: &Compressor) -> ElasticArray1024<u8> {
|
||||
let rlp = Rlp::new(c);
|
||||
if rlp.is_data() {
|
||||
ElasticArray1024::from_slice(swapper.compressed(rlp.as_raw()).unwrap_or_else(|| rlp.as_raw()))
|
||||
} else {
|
||||
map_rlp(&rlp, |r| compress(r.as_raw(), swapper))
|
||||
}
|
||||
}
|
||||
|
||||
/// Call this function to decompress rlp.
|
||||
pub fn decompress(c: &[u8], swapper: &Decompressor) -> ElasticArray1024<u8> {
|
||||
let rlp = Rlp::new(c);
|
||||
if rlp.is_data() {
|
||||
ElasticArray1024::from_slice(swapper.decompressed(rlp.as_raw()).unwrap_or_else(|| rlp.as_raw()))
|
||||
} else {
|
||||
map_rlp(&rlp, |r| decompress(r.as_raw(), swapper))
|
||||
}
|
||||
}
|
||||
|
||||
fn map_rlp<F: Fn(&Rlp) -> ElasticArray1024<u8>>(rlp: &Rlp, f: F) -> ElasticArray1024<u8> {
|
||||
let mut stream = RlpStream::new_list(rlp.item_count().unwrap_or_default());
|
||||
for subrlp in rlp.iter() {
|
||||
stream.append_raw(&f(&subrlp), 1);
|
||||
}
|
||||
stream.drain().as_slice().into()
|
||||
}
|
||||
|
||||
/// Stores RLPs used for compression
|
||||
pub struct Swapper<'a> {
|
||||
compressed_to_rlp: HashMap<&'a [u8], &'a [u8]>,
|
||||
rlp_to_compressed: HashMap<&'a [u8], &'a [u8]>,
|
||||
}
|
||||
|
||||
impl<'a> Swapper<'a> {
|
||||
/// Construct a swapper from a list of common RLPs
|
||||
pub fn new(rlps_to_swap: &[&'a [u8]], compressed: &[&'a [u8]]) -> Self {
|
||||
if rlps_to_swap.len() > 0x7e {
|
||||
panic!("Invalid usage, only 127 RLPs can be swappable.");
|
||||
}
|
||||
|
||||
let items = cmp::min(rlps_to_swap.len(), compressed.len());
|
||||
let mut compressed_to_rlp = HashMap::with_capacity(items);
|
||||
let mut rlp_to_compressed = HashMap::with_capacity(items);
|
||||
|
||||
for (&rlp, &compressed) in rlps_to_swap.iter().zip(compressed.iter()) {
|
||||
compressed_to_rlp.insert(compressed, rlp);
|
||||
rlp_to_compressed.insert(rlp, compressed);
|
||||
}
|
||||
|
||||
Swapper {
|
||||
compressed_to_rlp,
|
||||
rlp_to_compressed,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Decompressor for Swapper<'a> {
|
||||
fn decompressed(&self, compressed: &[u8]) -> Option<&[u8]> {
|
||||
self.compressed_to_rlp.get(compressed).cloned()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Compressor for Swapper<'a> {
|
||||
fn compressed(&self, rlp: &[u8]) -> Option<&[u8]> {
|
||||
self.rlp_to_compressed.get(rlp).cloned()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user