Skip to content

Commit

Permalink
protobuf for TransactionBody (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilblackdragon authored and azban committed Jan 29, 2019
1 parent a8435a8 commit c027acf
Show file tree
Hide file tree
Showing 57 changed files with 6,322 additions and 612 deletions.
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Cargo.lock linguist-generated=true -diff
core/protos/src/autogenerated/* linguist-generated=true -diff
nearlib/protos.js linguist-generated=true -diff
scripts/protos/* linguist-generated=true -diff
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ core/wasm/**/Cargo.lock

# Python env
.env
*.pyc

# Integration tests
tmp/
Expand Down
9 changes: 9 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ variables:
mkdir -p "${CACHE_ROOT}/target" &&
ln -s "${CACHE_ROOT}/target" "${CI_PROJECT_DIR}/target"

.setup_deps: &setup_deps
sudo apt-get install -y unzip &&
wget -O /tmp/protoc.zip https://github.com/protocolbuffers/protobuf/releases/download/v3.6.1/protoc-3.6.1-linux-x86_64.zip &&
unzip -p /tmp/protoc.zip bin/protoc | sudo tee /usr/local/bin/protoc 1> /dev/null &&
sudo chmod 755 /usr/local/bin/protoc &&
sudo pip install protobuf

.cleanup_obsolete_cache: &cleanup_obsolete_cache
find "${CACHE_ROOT}/target" -atime +2 -delete

Expand All @@ -22,6 +29,7 @@ test_cargo:
stage: test
before_script:
- *setup_cache
- *setup_deps
- rustup component add clippy
script:
- rustc --version && cargo --version
Expand All @@ -35,6 +43,7 @@ test_nearlib:
stage: test
before_script:
- *setup_cache
- *setup_deps
script:
- rustc --version && cargo --version
- cargo build --release --package=devnet
Expand Down
72 changes: 68 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ path = "node/src/main.rs"
[dependencies]
testnet = { path = "./node/testnet" }

[build-dependencies]
protos-autogen = { path = "./protos/builder" }

[workspace]
members = [
"core/chain",
Expand All @@ -29,6 +32,7 @@ members = [
"node/devnet",
"node/http",
"node/network",
"protos/builder",
"node/runtime",
"node/shard",
"node/testnet",
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ $ rustup component add clippy-preview

You may need to activate the environment via `. ~/.cargo/env` to use `cargo`.


### Install dependencies

Mac OS:
```bash
brew install protobuf
```

Ubuntu:
```bash
apt-get install protobuf-compiler
```

### Build & Run from source code

```bash
Expand All @@ -46,6 +59,15 @@ It will build the first time and then run:

```bash
cargo run
```

### Testing

In order to run tests currently, you must setup the following:

```bash
# sudo may be required if you are not testing with a python virtual environment
pip install protobuf
```

### Logging
Expand Down
5 changes: 5 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use protos_autogen;

fn main() {
protos_autogen::autogenerate();
}
1 change: 1 addition & 0 deletions core/chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ serde_derive = "1.0"
parking_lot = "0.7.1"
log = "0.4"

near-protos = { path = "../protos" }
primitives = { path = "../primitives" }
storage = { path = "../storage" }
9 changes: 5 additions & 4 deletions core/chain/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#[macro_use]
extern crate log;
extern crate parking_lot;
extern crate primitives;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate storage;
#[macro_use]
extern crate log;

use std::cmp;
use std::collections::HashMap;
Expand All @@ -19,12 +19,13 @@ use primitives::hash::CryptoHash;
use primitives::traits::Signer;
use primitives::types::{BlockId, PartialSignature};
use primitives::utils::index_to_bytes;
use primitives::serialize::{Encode, Decode};
use storage::{read_with_cache, write_with_cache, Storage};

const BLOCKCHAIN_BEST_BLOCK: &[u8] = b"best";

/// Trait that abstracts ``Header"
pub trait SignedHeader: Debug + Clone + Send + Sync + Serialize + DeserializeOwned + Eq + 'static
pub trait SignedHeader: Debug + Clone + Encode + Decode + Send + Sync + Eq + Serialize + DeserializeOwned + 'static
{
/// Returns hash of the block body.
fn block_hash(&self) -> CryptoHash;
Expand All @@ -38,7 +39,7 @@ pub trait SignedHeader: Debug + Clone + Send + Sync + Serialize + DeserializeOwn

/// Trait that abstracts a ``Block", Is used for both beacon-chain blocks
/// and shard-chain blocks.
pub trait SignedBlock: Debug + Clone + Send + Sync + Serialize + DeserializeOwned + Eq + 'static {
pub trait SignedBlock: Debug + Clone + Encode + Decode + Send + Sync + Eq + Serialize + DeserializeOwned + 'static {
type SignedHeader: SignedHeader;

/// Returns signed header for given block.
Expand Down
4 changes: 4 additions & 0 deletions core/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2018"
regex = "1"
bincode = { version = "1.0", features = ["i128"] }
bs58 = "0.2.0"
base64 = "0.10.0"
byteorder = "1.2"
exonum_sodiumoxide = "0.0.20"
futures = "0.1"
Expand All @@ -18,6 +19,9 @@ sha2 = "0.8.0"
serde_json = "1.0"
pairing = { git = "https://github.com/mmaker/pairing.git", rev = "a3bbecefe6c5d2f15c7126ea8b84930053929f20" }
rand = "0.4"
protobuf = "2.2.4"

near-protos = { path = "../protos" }

[dev-dependencies]
serde_json = "1.0"
25 changes: 13 additions & 12 deletions core/primitives/src/serialize.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
use std::io;

use serde::{de::DeserializeOwned, Serialize};

pub type EncodeType = Result<Vec<u8>, String>;
pub type DecodeType<T> = Result<T, String>;
pub type EncodeResult = Result<Vec<u8>, io::Error>;
pub type DecodeResult<T> = Result<T, io::Error>;

// encode a type to byte array
pub trait Encode {
fn encode(&self) -> EncodeType;
fn encode(&self) -> EncodeResult;
}

// decode from byte array
pub trait Decode: Sized {
fn decode(data: &[u8]) -> DecodeType<Self>;
fn decode(data: &[u8]) -> DecodeResult<Self>;
}

impl<T> Encode for T
where
T: Serialize,
{
fn encode(&self) -> EncodeType {
bincode::serialize(&self).map_err(|_| "Failed to serialize".to_string())
impl<T: Serialize> Encode for T {
fn encode(&self) -> EncodeResult {
bincode::serialize(&self)
.map_err(|_| io::Error::new(io::ErrorKind::Other, "Failed to serialize"))
}
}

impl<T> Decode for T
where
T: DeserializeOwned,
{
fn decode(data: &[u8]) -> DecodeType<Self> {
bincode::deserialize(data).map_err(|_| "Failed to deserialize".to_string())
fn decode(data: &[u8]) -> DecodeResult<Self> {
bincode::deserialize(data)
.map_err(|_| io::Error::new(io::ErrorKind::Other, "Failed to deserialize"))
}
}
Loading

0 comments on commit c027acf

Please sign in to comment.