Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic functionality without JSON standard enforcement and no-std compilation #1

Merged
merged 5 commits into from Sep 12, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -0,0 +1,42 @@
{
This conversation was marked as resolved by fubuloubu

This comment has been minimized.

Copy link
@fubuloubu

fubuloubu Sep 8, 2019

Collaborator

Can this file be removed from the commit history? It's a local editor file I don't think we need to commit. (maybe add to .gitignore?)

This comment has been minimized.

Copy link
@fubuloubu

fubuloubu Sep 12, 2019

Collaborator

Tracked in #5

// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "cargo",
"subcommand": "build",
"problemMatcher": [
"$rustc"
],
"group": "build"
},
{
"type": "cargo",
"subcommand": "check",
"problemMatcher": [
"$rustc"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "cargo test",
"type": "shell",
"command": "RUST_BACKTRACE=1 cargo test",
"problemMatcher": [
"$rustc"
]
},
{
"label": "cargo build no-std",
"type": "shell",
"command": "cargo build --features no-std",
"problemMatcher": [
"$rustc"
]
}
]
}
@@ -1,16 +1,23 @@
[package]
name = "ethpm"
version = "0.1.0-alpha.2"
authors = ["Bryant Eisenbach <fubuloubu@gmail.com>"]
authors = ["Bryant Eisenbach <fubuloubu@gmail.com>","Sean Coughlin <sean@froth.com>"]
edition = "2018"
license = "MIT"
description = "Rust library for EthPM package manifest and registry operations"
homepage = "https://ethpm.github.io/ethpm-spec/"
repository = "https://github.com/ethpm/ethpm-rs"

[features]
default = ["serde", "serde_json"]
# enable support for the standard library
std = ["serde", "serde_json"]
no-std = ["serde-json-core"]

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde = { version = "1.0.80", features = ["derive"], optional = true }
serde_json = { version = "1.0", optional = true }
serde-json-core = { version = "0.0.1", optional = true }

[dev-dependencies]
glob = "0.3.0"
@@ -1,15 +1,220 @@
#[cfg(all(feature = "no-std", not(test)))]
#[no_std]

#[cfg(any(feature = "default", feature = "std", test))]
#[macro_use]
extern crate std;

#[cfg(all(feature = "no-std", not(test)))]
#[macro_use]
extern crate core as std;

#[cfg(all(feature = "no-std", not(test)))]
extern crate alloc;

#[cfg(all(feature = "no-std", not(test)))]
use alloc::vec::Vec;

#[cfg(all(feature = "no-std", not(test)))]
use alloc::collections::btree_map::BTreeMap;
#[cfg(any(feature = "default", feature = "std", test))]
use std::collections::BTreeMap;

extern crate serde;
#[cfg(any(feature = "default", feature = "std", test))]
extern crate serde_json;
#[cfg(all(feature = "no-std", not(test)))]
extern crate serde_json_core;

use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

//Rust-based transcription of Package standard as specified at https://github.com/ethpm/ethpm-spec/blob/master/spec/package.spec.json
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct Package<'a> {
manifest_version: &'a str, //TODO: Rustify
package_name: &'a str,
meta: Option<PackageMeta<'a>>,
version: &'a str, //TODO: Rustify
sources: Option<BTreeMap<&'a str, &'a str>>, //TODO: Rustify
contract_types: Option<BTreeMap<&'a str, ContractType<'a>>>, //TODO: Rustify
deployments: Option<BTreeMap<&'a str, BTreeMap<&'a str, ContractInstance<'a>>>>, //TODO: Rustify
build_dependencies: Option<BTreeMap<&'a str, &'a str>>, //TODO: Rustify
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct PackageMeta<'a> {
authors: Option<Vec<&'a str>>,
license: Option<&'a str>,
description: Option<&'a str>,
keywords: Option<Vec<&'a str>>,
links: Option<BTreeMap<&'a str, &'a str>>, //TODO: Rustify
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct ContractType<'a> {
contract_name: Option<&'a str>,
deployment_bytecode: Option<BytecodeObject<'a>>,
runtime_bytecode: Option<BytecodeObject<'a>>,
abi: Option<Vec<ABI<'a>>>,
natspec: Option<NatSpec<'a>>,
compiler: Option<CompilerInformation<'a>>,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct ContractInstance<'a> {
contract_type: &'a str,
address: &'a str,
transaction: Option<&'a str>,
block: Option<&'a str>,
deployment_bytecode: Option<BytecodeObject<'a>>,
runtime_bytecode: Option<BytecodeObject<'a>>,
compiler: Option<CompilerInformation<'a>>,
link_dependencies: Option<Vec<LinkValue<'a>>>,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct BytecodeObject<'a> {
bytecode: Option<&'a str>,
This conversation was marked as resolved by fubuloubu

This comment has been minimized.

Copy link
@fubuloubu

fubuloubu Sep 8, 2019

Collaborator

Store as raw bytes? (e.g. decode hexstr to bytes)

This comment has been minimized.

Copy link
@fubuloubu

fubuloubu Sep 12, 2019

Collaborator

Tracked in #4

link_references: Option<Vec<LinkReference<'a>>>,
link_dependencies: Option<Vec<LinkValue<'a>>>,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct ABI<'a> {
This conversation was marked as resolved by fubuloubu

This comment has been minimized.

Copy link
@fubuloubu

fubuloubu Sep 8, 2019

Collaborator

Might be helpful to leverage this library for Ethereum ABIs: https://docs.rs/ethabi/8.0.1/ethabi/

This comment has been minimized.

Copy link
@fubuloubu

fubuloubu Sep 12, 2019

Collaborator

Tracked in #2

constant: Option<bool>,
anonymous: Option<bool>,
inputs: Option<Vec<EthTypes<'a>>>,
name: Option<&'a str>,
outputs: Option<Vec<EthTypes<'a>>>,
payable: Option<bool>,
#[serde(rename = "stateMutability")]
state_mutability: Option<&'a str>, //TODO: Rustify
#[serde(rename = "type")]
type_property: Option<EthMethodType>,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub enum EthMethodType {
#[serde(rename = "constructor")]
Constructor,
#[serde(rename = "function")]
Function,
#[serde(rename = "fallback")]
Fallback,
#[serde(rename = "event")]
Event,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct EthTypes<'a> {
indexed: Option<bool>,
name: &'a str,
#[serde(rename = "type")]
type_property: EthValueType,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub enum EthValueType {
#[serde(rename = "address")]
Address,
#[serde(rename = "uint256")]
Uint256,
#[serde(rename = "bool")]
Bool,
//TODO: Add complete list of value types
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub enum StateMutability {
#[serde(rename = "nonpayable")]
NonPayable,
#[serde(rename = "view")]
View,
#[serde(rename = "pure")]
Pure,
#[serde(rename = "payable")]
Payable,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct LinkReference<'a> {
offsets: Vec<i64>,
length: u64,
name: &'a str,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
enum LinkValueType {
#[serde(rename = "literal")]
Literal,
#[serde(rename = "reference")]
Reference,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct LinkValue<'a> {
offsets: Vec<i64>,
#[serde(rename = "type")]
type_property: LinkValueType,
value: &'a str, //TODO: Rustify
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct CompilerInformation<'a> {
name: CompilerType,
version: &'a str, //TODO: Rustify
settings: Option<CompilerSettings>,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub enum CompilerType {
#[serde(rename = "solc")]
Solc,
This conversation was marked as resolved by fubuloubu

This comment has been minimized.

Copy link
@fubuloubu

fubuloubu Sep 8, 2019

Collaborator

Add Vyper (binary name is vyper)

This comment has been minimized.

Copy link
@fubuloubu

fubuloubu Sep 12, 2019

Collaborator

Tracked in #3

}

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct CompilerSettings {
optimize: bool,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct NatSpec<'a> {
compiler: Option<CompilerInformation<'a>>,
author: Option<&'a str>,
methods: Option<BTreeMap<&'a str, Method<'a>>>,
title: Option<&'a str>,
description: Option<&'a str>,
#[serde(rename = "type")]
type_property: Option<&'a str>, //TODO: Rustify
dev: Option<&'a str>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Package {
// Required Members
manifest_version: String,
package_name: String,
version: String,
// Other members here...
#[serde(deny_unknown_fields)]
pub struct Method<'a> {
details: Option<&'a str>,
params: Option<BTreeMap<&'a str, &'a str>>,
notice: Option<&'a str>,
#[serde(rename = "return")]
return_property: Option<EthValueType>,
dev: Option<&'a str>, //TODO: Rustify
}

#[cfg(test)]
@@ -4,6 +4,8 @@ use std::io::prelude::*;
extern crate glob;
use glob::glob;

use ::ethpm::Package;

fn get_examples() -> Vec<String> {
glob("./ethpm-spec/examples/**/*.json")
.expect("Make sure to load git submodules")
@@ -17,17 +19,18 @@ fn get_examples() -> Vec<String> {
.collect()
}

use ethpm;

// Try to parse packages located in
// ethpm-spec/examples/**/*.json
// TODO: Generate 1 case per example
#[test]
fn can_parse_examples() {
for example in get_examples() {
// let raw_object: serde_json::Value = serde_json::from_str(&example).unwrap();
// println!("JSON: {}", raw_object);

// Try to parse without failure
let package: ethpm::Package = serde_json::from_str(&example).unwrap();
let raw_object: serde_json::Value = serde_json::from_str(&example).unwrap();
// TODO iterate through `raw_object` and assert field match to `package`
let package: Package = serde_json::from_str(&example).unwrap();

println!("Package: {:?}", package);
This conversation was marked as resolved by fubuloubu

This comment has been minimized.

Copy link
@fubuloubu

fubuloubu Sep 8, 2019

Collaborator

Still feel like there needs to be some sort of deeper testing of this functionality, although simply parsing is probably a decent test of the package parser working

}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.