Skip to content

Commit

Permalink
Merge pull request #101 from ergoplatform/i100-rust-api-guidelines-co…
Browse files Browse the repository at this point in the history
…nform

Conform to Rust API guidelines
  • Loading branch information
greenhat committed Oct 6, 2020
2 parents f9a9cd3 + 1a9589e commit a8518a5
Show file tree
Hide file tree
Showing 30 changed files with 95 additions and 96 deletions.
1 change: 1 addition & 0 deletions bindings/ergo-lib-android/Cargo.toml
Expand Up @@ -5,6 +5,7 @@ license = "CC0-1.0"
authors = ["Denys Zadorozhnyi <denys@zadorozhnyi.com>"]
edition = "2018"
description = "JNI bindings for ergo-lib"
repository = "https://github.com/ergoplatform/sigma-rust"

[lib]
name = "ergowalletlibjni"
Expand Down
1 change: 1 addition & 0 deletions bindings/ergo-lib-c-core/Cargo.toml
Expand Up @@ -5,6 +5,7 @@ license = "CC0-1.0"
authors = ["Denys Zadorozhnyi <denys@zadorozhnyi.com>"]
edition = "2018"
description = "Common code for ergo-lib C and JNI bindings"
repository = "https://github.com/ergoplatform/sigma-rust"

[lib]
crate-type = ["lib"]
Expand Down
1 change: 1 addition & 0 deletions bindings/ergo-lib-c/Cargo.toml
Expand Up @@ -5,6 +5,7 @@ license = "CC0-1.0"
authors = ["Denys Zadorozhnyi <denys@zadorozhnyi.com>"]
edition = "2018"
description = "C bindings for ergo-lib"
repository = "https://github.com/ergoplatform/sigma-rust"
build = "build.rs"

[lib]
Expand Down
1 change: 1 addition & 0 deletions bindings/ergo-lib-wasm/Cargo.toml
Expand Up @@ -5,6 +5,7 @@ license = "CC0-1.0"
authors = ["Denys Zadorozhnyi <denys@zadorozhnyi.com>"]
edition = "2018"
description = "WASM bindings for ergo-lib"
repository = "https://github.com/ergoplatform/sigma-rust"

[lib]
crate-type = ["cdylib", "rlib"]
Expand Down
6 changes: 3 additions & 3 deletions bindings/ergo-lib-wasm/src/ast.rs
Expand Up @@ -39,7 +39,7 @@ impl Constant {
}

/// Extract i32 value, returning error if wrong type
pub fn as_i32(&self) -> Result<i32, JsValue> {
pub fn to_i32(&self) -> Result<i32, JsValue> {
match self.0.v {
ergo_lib::ast::ConstantVal::Int(v) => Ok(v),
_ => Err(JsValue::from_str(&format!(
Expand All @@ -55,7 +55,7 @@ impl Constant {
}

/// Extract i64 value, returning error if wrong type
pub fn as_i64(&self) -> Result<I64, JsValue> {
pub fn to_i64(&self) -> Result<I64, JsValue> {
match self.0.v {
ergo_lib::ast::ConstantVal::Long(v) => Ok(v.into()),
_ => Err(JsValue::from_str(&format!(
Expand All @@ -71,7 +71,7 @@ impl Constant {
}

/// Extract byte array, returning error if wrong type
pub fn as_byte_array(&self) -> Result<Uint8Array, JsValue> {
pub fn to_byte_array(&self) -> Result<Uint8Array, JsValue> {
match self.0.v.clone() {
ergo_lib::ast::ConstantVal::Coll(ergo_lib::ast::ConstantColl::Primitive(
ergo_lib::ast::CollPrim::CollByte(coll_bytes),
Expand Down
2 changes: 1 addition & 1 deletion bindings/ergo-lib-wasm/src/ergo_box.rs
Expand Up @@ -66,7 +66,7 @@ impl ErgoBox {
let chain_contract: chain::contract::Contract = contract.clone().into();
let b = chain::ergo_box::ErgoBox::new(
value.0,
chain_contract.get_ergo_tree(),
chain_contract.ergo_tree(),
vec![],
NonMandatoryRegisters::empty(),
creation_height,
Expand Down
2 changes: 1 addition & 1 deletion bindings/ergo-lib-wasm/src/ergo_box/box_builder.rs
Expand Up @@ -23,7 +23,7 @@ impl ErgoBoxCandidateBuilder {
pub fn new(value: &BoxValue, contract: &Contract, creation_height: u32) -> Self {
ErgoBoxCandidateBuilder(chain::ergo_box::box_builder::ErgoBoxCandidateBuilder::new(
value.clone().into(),
chain::contract::Contract::from(contract.clone()).get_ergo_tree(),
chain::contract::Contract::from(contract.clone()).ergo_tree(),
creation_height,
))
}
Expand Down
8 changes: 4 additions & 4 deletions bindings/ergo-lib-wasm/tests/test_constant.js
Expand Up @@ -7,7 +7,7 @@ import {
it('decode Constant i32', async () => {
let enc_v = '048ce5d4e505';
let c = Constant.decode_from_base16(enc_v);
let c_value = c.as_i32();
let c_value = c.to_i32();
expect(c_value).equal(777689414);
});

Expand All @@ -16,7 +16,7 @@ it('roundtrip Constant i32', async () => {
let c = Constant.from_i32(value);
let encoded = c.encode_to_base16();
let decoded_c = Constant.decode_from_base16(encoded);
let decoded_c_value = decoded_c.as_i32();
let decoded_c_value = decoded_c.to_i32();
expect(decoded_c_value).equal(value);
});

Expand All @@ -25,7 +25,7 @@ it('roundtrip Constant i64', async () => {
let c = Constant.from_i64(I64.from_str(value_str));
let encoded = c.encode_to_base16();
let decoded_c = Constant.decode_from_base16(encoded);
let decoded_c_value = decoded_c.as_i64();
let decoded_c_value = decoded_c.to_i64();
let decoded_c_value_str = decoded_c_value.to_str();
expect(decoded_c_value_str).equal(value_str);
});
Expand All @@ -35,7 +35,7 @@ it('roundtrip Constant byte array', async () => {
let c = Constant.from_byte_array(value);
let encoded = c.encode_to_base16();
let decoded_c = Constant.decode_from_base16(encoded);
let decoded_c_value = decoded_c.as_byte_array();
let decoded_c_value = decoded_c.to_byte_array();
expect(decoded_c_value.toString()).equal(value.toString());
});

Expand Down
5 changes: 3 additions & 2 deletions ergo-lib/Cargo.toml
Expand Up @@ -5,6 +5,7 @@ license = "CC0-1.0"
authors = ["Denys Zadorozhnyi <denys@zadorozhnyi.com>"]
edition = "2018"
description = "ErgoTree interpreter and wallet-like features for Ergo"
repository = "https://github.com/ergoplatform/sigma-rust"

[lib]
crate-type = ["cdylib", "rlib"]
Expand All @@ -24,8 +25,8 @@ num-bigint = "0.3.0"
rand = "0.7"

[features]
default = ["with-serde"]
with-serde = ["serde"]
default = ["json"]
json = ["serde"]

[dev-dependencies]
wasm-bindgen-test = "0.3.10"
Expand Down
2 changes: 1 addition & 1 deletion ergo-lib/README.md
Expand Up @@ -6,7 +6,7 @@ Features:

# Usage
## Crate features
### `with-serde`
### `json`
JSON serialization for chain types using `serde`.


Expand Down
6 changes: 3 additions & 3 deletions ergo-lib/src/ast/constant.rs
Expand Up @@ -5,7 +5,7 @@ use crate::{
sigma_protocol::{dlog_group::EcPoint, sigma_boolean::SigmaProp},
types::{LiftIntoSType, SType},
};
#[cfg(feature = "with-serde")]
#[cfg(feature = "json")]
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;

Expand Down Expand Up @@ -86,9 +86,9 @@ impl ConstantVal {
}

#[derive(PartialEq, Eq, Debug, Clone)]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "with-serde",
feature = "json",
serde(into = "Base16EncodedBytes", try_from = "Base16DecodedBytes")
)]
/// Constant
Expand Down
2 changes: 1 addition & 1 deletion ergo-lib/src/chain.rs
@@ -1,6 +1,6 @@
//! Ergo chain types

#[cfg(feature = "with-serde")]
#[cfg(feature = "json")]
mod json;

mod base16_bytes;
Expand Down
10 changes: 5 additions & 5 deletions ergo-lib/src/chain/base16_bytes.rs
@@ -1,12 +1,12 @@
//! Transitioning type for Base16 encoded bytes in JSON serialization

#[cfg(feature = "with-serde")]
#[cfg(feature = "json")]
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;

/// Transitioning type for Base16 encoded bytes
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "with-serde", serde(into = "String"))]
#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "json", serde(into = "String"))]
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct Base16EncodedBytes(String);

Expand All @@ -24,8 +24,8 @@ impl Into<String> for Base16EncodedBytes {
}

/// Transitioning type for Base16 decoded bytes
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "with-serde", serde(try_from = "String"))]
#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "json", serde(try_from = "String"))]
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct Base16DecodedBytes(pub Vec<u8>);

Expand Down
4 changes: 2 additions & 2 deletions ergo-lib/src/chain/context_extension.rs
Expand Up @@ -7,15 +7,15 @@ use crate::{
},
};
use indexmap::IndexMap;
#[cfg(feature = "with-serde")]
#[cfg(feature = "json")]
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::{convert::TryFrom, io, num::ParseIntError};

/// User-defined variables to be put into context
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(
feature = "with-serde",
feature = "json",
derive(Serialize, Deserialize),
serde(
into = "HashMap<String, Constant>",
Expand Down
2 changes: 1 addition & 1 deletion ergo-lib/src/chain/contract.rs
Expand Up @@ -21,7 +21,7 @@ impl Contract {
}

/// get ErgoTree for this contract
pub fn get_ergo_tree(&self) -> ErgoTree {
pub fn ergo_tree(&self) -> ErgoTree {
self.ergo_tree.clone()
}
}
6 changes: 3 additions & 3 deletions ergo-lib/src/chain/data_input.rs
Expand Up @@ -11,16 +11,16 @@ use crate::serialization::{
use proptest::prelude::*;
#[cfg(test)]
use proptest_derive::Arbitrary;
#[cfg(feature = "with-serde")]
#[cfg(feature = "json")]
use serde::{Deserialize, Serialize};

/// Inputs, that are used to enrich script context, but won't be spent by the transaction
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(test, derive(Arbitrary))]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
pub struct DataInput {
/// id of the box to add into context (should be in UTXO)
#[cfg_attr(feature = "with-serde", serde(rename = "boxId"))]
#[cfg_attr(feature = "json", serde(rename = "boxId"))]
pub box_id: BoxId,
}

Expand Down
6 changes: 3 additions & 3 deletions ergo-lib/src/chain/digest32.rs
Expand Up @@ -6,7 +6,7 @@ use blake2::digest::{Update, VariableOutput};
use blake2::VarBlake2b;
#[cfg(test)]
use proptest_derive::Arbitrary;
#[cfg(feature = "with-serde")]
#[cfg(feature = "json")]
use serde::{Deserialize, Serialize};
use sigma_ser::vlq_encode;
use std::convert::TryFrom;
Expand All @@ -15,9 +15,9 @@ use std::io;
use thiserror::Error;

/// 32 byte array used in box, transaction ids (hash)
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "with-serde",
feature = "json",
serde(into = "Base16EncodedBytes", try_from = "Base16DecodedBytes")
)]
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
Expand Down
38 changes: 16 additions & 22 deletions ergo-lib/src/chain/ergo_box.rs
Expand Up @@ -5,7 +5,7 @@ pub mod box_id;
pub mod box_value;
pub mod register;

#[cfg(feature = "with-serde")]
#[cfg(feature = "json")]
use super::json;
use super::{
digest32::blake2b256_hash,
Expand All @@ -25,12 +25,12 @@ use box_id::BoxId;
use box_value::BoxValue;
use indexmap::IndexSet;
use register::NonMandatoryRegisters;
#[cfg(feature = "with-serde")]
#[cfg(feature = "json")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "with-serde")]
#[cfg(feature = "json")]
use std::convert::TryFrom;
use std::io;
#[cfg(feature = "with-serde")]
#[cfg(feature = "json")]
use thiserror::Error;

/// Box (aka coin, or an unspent output) is a basic concept of a UTXO-based cryptocurrency.
Expand All @@ -50,40 +50,34 @@ use thiserror::Error;
///
/// A transaction is unsealing a box. As a box can not be open twice, any further valid transaction
/// can not be linked to the same box.
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "with-serde",
serde(try_from = "json::ergo_box::ErgoBoxFromJson")
)]
#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "json", serde(try_from = "json::ergo_box::ErgoBoxFromJson"))]
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct ErgoBox {
#[cfg_attr(feature = "with-serde", serde(rename = "boxId"))]
#[cfg_attr(feature = "json", serde(rename = "boxId"))]
box_id: BoxId,
/// amount of money associated with the box
#[cfg_attr(feature = "with-serde", serde(rename = "value"))]
#[cfg_attr(feature = "json", serde(rename = "value"))]
pub value: BoxValue,
/// guarding script, which should be evaluated to true in order to open this box
#[cfg_attr(
feature = "with-serde",
serde(rename = "ergoTree", with = "json::ergo_tree")
)]
#[cfg_attr(feature = "json", serde(rename = "ergoTree", with = "json::ergo_tree"))]
pub ergo_tree: ErgoTree,
/// secondary tokens the box contains
#[cfg_attr(feature = "with-serde", serde(rename = "assets"))]
#[cfg_attr(feature = "json", serde(rename = "assets"))]
pub tokens: Vec<TokenAmount>,
/// additional registers the box can carry over
#[cfg_attr(feature = "with-serde", serde(rename = "additionalRegisters"))]
#[cfg_attr(feature = "json", serde(rename = "additionalRegisters"))]
pub additional_registers: NonMandatoryRegisters,
/// height when a transaction containing the box was created.
/// This height is declared by user and should not exceed height of the block,
/// containing the transaction with this box.
#[cfg_attr(feature = "with-serde", serde(rename = "creationHeight"))]
#[cfg_attr(feature = "json", serde(rename = "creationHeight"))]
pub creation_height: u32,
/// id of transaction which created the box
#[cfg_attr(feature = "with-serde", serde(rename = "transactionId"))]
#[cfg_attr(feature = "json", serde(rename = "transactionId"))]
pub transaction_id: TxId,
/// number of box (from 0 to total number of boxes the transaction with transactionId created - 1)
#[cfg_attr(feature = "with-serde", serde(rename = "index"))]
#[cfg_attr(feature = "json", serde(rename = "index"))]
pub index: u16,
}

Expand Down Expand Up @@ -210,15 +204,15 @@ impl ErgoBoxId for ErgoBox {
}

/// Errors on parsing ErgoBox from JSON
#[cfg(feature = "with-serde")]
#[cfg(feature = "json")]
#[derive(Error, PartialEq, Eq, Debug, Clone)]
pub enum ErgoBoxFromJsonError {
/// Box id parsed from JSON differs from calculated from box serialized bytes
#[error("Box id parsed from JSON differs from calculated from box serialized bytes")]
InvalidBoxId,
}

#[cfg(feature = "with-serde")]
#[cfg(feature = "json")]
impl TryFrom<json::ergo_box::ErgoBoxFromJson> for ErgoBox {
type Error = ErgoBoxFromJsonError;
fn try_from(box_json: json::ergo_box::ErgoBoxFromJson) -> Result<Self, Self::Error> {
Expand Down
6 changes: 3 additions & 3 deletions ergo-lib/src/chain/ergo_box/box_id.rs
@@ -1,7 +1,7 @@
//! Box id type
use std::io;

#[cfg(feature = "with-serde")]
#[cfg(feature = "json")]
use serde::{Deserialize, Serialize};

use super::super::digest32::Digest32;
Expand All @@ -14,7 +14,7 @@ use proptest_derive::Arbitrary;

/// newtype for box ids
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
#[cfg_attr(test, derive(Arbitrary))]
pub struct BoxId(pub Digest32);

Expand All @@ -34,7 +34,7 @@ impl From<Digest32> for BoxId {
}
}

#[cfg(feature = "with-serde")]
#[cfg(feature = "json")]
impl Into<String> for BoxId {
fn into(self) -> String {
self.0.into()
Expand Down

0 comments on commit a8518a5

Please sign in to comment.