Skip to content

Commit

Permalink
initial, fallable built-ins
Browse files Browse the repository at this point in the history
  • Loading branch information
NikVolf committed Mar 22, 2017
1 parent 498d5c0 commit 5e34235
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 11 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions ethcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ ethcore-bloom-journal = { path = "../util/bloom" }
hardware-wallet = { path = "../hw" }
stats = { path = "../util/stats" }
num = "0.1"
bn = { git = "https://github.com/paritytech/bn" }

[dependencies.hyper]
git = "https://github.com/ethcore/hyper"
Expand Down
2 changes: 2 additions & 0 deletions ethcore/res/ethereum/foundation.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@
"0000000000000000000000000000000000000003": { "builtin": { "name": "ripemd160", "pricing": { "linear": { "base": 600, "word": 120 } } } },
"0000000000000000000000000000000000000004": { "builtin": { "name": "identity", "pricing": { "linear": { "base": 15, "word": 3 } } } },
"0000000000000000000000000000000000000005": { "builtin": { "name": "modexp", "activate_at": "0x7fffffffffffff", "pricing": { "modexp": { "divisor": 20 } } } },
"0000000000000000000000000000000000000006": { "builtin": { "name": "alt_bn128_add", "activate_at": "0x7fffffffffffff", "pricing": { "linear": { "base": 999999, "word": 0 } } } },
"0000000000000000000000000000000000000007": { "builtin": { "name": "alt_bn128_mul", "activate_at": "0x7fffffffffffff", "pricing": { "linear": { "base": 999999, "word": 0 } } } },
"3282791d6fd713f1e94f4bfd565eaa78b3a0599d": {
"balance": "1337000000000000000000"
},
Expand Down
77 changes: 68 additions & 9 deletions ethcore/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,19 @@ use util::{U256, H256, Uint, Hashable, BytesRef};
use ethkey::{Signature, recover as ec_recover};
use ethjson;

#[derive(Debug)]
pub struct Error(pub String);

impl From<&'static str> for Error {
fn from(val: &'static str) -> Self {
Error(val.to_owned())
}
}

/// Native implementation of a built-in contract.
pub trait Impl: Send + Sync {
/// execute this built-in on the given input, writing to the given output.
fn execute(&self, input: &[u8], output: &mut BytesRef);
fn execute(&self, input: &[u8], output: &mut BytesRef) -> Result<(), Error>;
}

/// A gas pricing scheme for built-in contracts.
Expand Down Expand Up @@ -102,7 +111,10 @@ impl Builtin {
pub fn cost(&self, input: &[u8]) -> U256 { self.pricer.cost(input) }

/// Simple forwarder for execute.
pub fn execute(&self, input: &[u8], output: &mut BytesRef) { self.native.execute(input, output) }
pub fn execute(&self, input: &[u8], output: &mut BytesRef) -> Result<(), Error> {
self.native.execute(input, output);
Ok(())
}

/// Whether the builtin is activated at the given block number.
pub fn is_active(&self, at: u64) -> bool { at >= self.activate_at }
Expand Down Expand Up @@ -172,14 +184,21 @@ struct Ripemd160;
#[derive(Debug)]
struct ModexpImpl;

#[derive(Debug)]
struct Bn128AddImpl;

#[derive(Debug)]
struct Bn128MulImpl;

impl Impl for Identity {
fn execute(&self, input: &[u8], output: &mut BytesRef) {
fn execute(&self, input: &[u8], output: &mut BytesRef) -> Result<(), Error> {
output.write(0, input);
Ok(())
}
}

impl Impl for EcRecover {
fn execute(&self, i: &[u8], output: &mut BytesRef) {
fn execute(&self, i: &[u8], output: &mut BytesRef) -> Result<(), Error> {
let len = min(i.len(), 128);

let mut input = [0; 128];
Expand All @@ -192,7 +211,7 @@ impl Impl for EcRecover {

let bit = match v[31] {
27 | 28 if &v.0[..31] == &[0; 31] => v[31] - 27,
_ => return,
_ => { return Ok(()); },
};

let s = Signature::from_rsv(&r, &s, bit);
Expand All @@ -203,35 +222,41 @@ impl Impl for EcRecover {
output.write(12, &r[12..r.len()]);
}
}

Ok(())
}
}

impl Impl for Sha256 {
fn execute(&self, input: &[u8], output: &mut BytesRef) {
fn execute(&self, input: &[u8], output: &mut BytesRef) -> Result<(), Error> {
let mut sha = Sha256Digest::new();
sha.input(input);

let mut out = [0; 32];
sha.result(&mut out);

output.write(0, &out);

Ok(())
}
}

impl Impl for Ripemd160 {
fn execute(&self, input: &[u8], output: &mut BytesRef) {
fn execute(&self, input: &[u8], output: &mut BytesRef) -> Result<(), Error> {
let mut sha = Ripemd160Digest::new();
sha.input(input);

let mut out = [0; 32];
sha.result(&mut out[12..32]);

output.write(0, &out);

Ok(())
}
}

impl Impl for ModexpImpl {
fn execute(&self, input: &[u8], output: &mut BytesRef) {
fn execute(&self, input: &[u8], output: &mut BytesRef) -> Result<(), Error> {
let mut reader = input.chain(io::repeat(0));
let mut buf = [0; 32];

Expand Down Expand Up @@ -294,9 +319,43 @@ impl Impl for ModexpImpl {
let res_start = mod_len - bytes.len();
output.write(res_start, &bytes);
}

Ok(())
}
}

impl Impl for Bn128AddImpl {
fn execute(&self, input: &[u8], output: &mut BytesRef) -> Result<(), Error> {
use bn::{Fq, AffineG1, G1};

let mut buf = [0u8; 32];
let mut next_coord = |reader: &mut io::Chain<&[u8], io::Repeat>| {
reader.read_exact(&mut buf[..]).expect("reading from zero-extended memory cannot fail; qed");
Fq::from_slice(&input[0..32])
};

let mut padded_input = input.chain(io::repeat(0));

let p1x = next_coord(&mut padded_input).map_err(|_| Error::from("Invalid p1 x coordinate"))?;
let p1y = next_coord(&mut padded_input).map_err(|_| Error::from("Invalid p1 y coordinate"))?;
let p1: G1 = AffineG1::new(p1x, p1y).into();

let p2x = next_coord(&mut padded_input).map_err(|_| Error::from("Invalid p2 x coordinate"))?;
let p2y = next_coord(&mut padded_input).map_err(|_| Error::from("Invalid p2 y coordinate"))?;
let p2: G1 = AffineG1::new(p2x, p2y).into();

let sum = AffineG1::from_jacobian(p1 + p2).expect("Sum of two valid points is a valid point also; qed");

Ok(())
}
}

impl Impl for Bn128MulImpl {
fn execute(&self, input: &[u8], output: &mut BytesRef) -> Result<(), Error> {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::{Builtin, Linear, ethereum_builtin, Pricer, Modexp};
Expand Down Expand Up @@ -574,4 +633,4 @@ mod tests {
b.execute(&i[..], &mut BytesRef::Fixed(&mut o[..]));
assert_eq!(i, o);
}
}
}
9 changes: 9 additions & 0 deletions ethcore/src/evm/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::{ops, cmp, fmt};
use util::{U128, U256, U512, Uint, trie};
use action_params::ActionParams;
use evm::Ext;
use builtin;

/// Evm errors.
#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -59,6 +60,7 @@ pub enum Error {
/// What was the stack limit
limit: usize
},
BuiltIn(String),
/// Returned on evm internal error. Should never be ignored during development.
/// Likely to cause consensus issues.
Internal(String),
Expand All @@ -70,6 +72,12 @@ impl From<Box<trie::TrieError>> for Error {
}
}

impl From<builtin::Error> for Error {
fn from(err: builtin::Error) -> Self {
Error::BuiltIn(err.0)
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::Error::*;
Expand All @@ -79,6 +87,7 @@ impl fmt::Display for Error {
BadInstruction { .. } => "Bad instruction",
StackUnderflow { .. } => "Stack underflow",
OutOfStack { .. } => "Out of stack",
BuiltIn { .. } => "Built-in failed",
Internal(ref msg) => msg,
};
message.fmt(f)
Expand Down
3 changes: 2 additions & 1 deletion ethcore/src/executive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ impl<'a, B: 'a + StateBackend> Executive<'a, B> {

let cost = builtin.cost(data);
if cost <= params.gas {
builtin.execute(data, &mut output);
builtin.execute(data, &mut output)?;
self.state.discard_checkpoint();

// trace only top level calls to builtins to avoid DDoS attacks
Expand Down Expand Up @@ -497,6 +497,7 @@ impl<'a, B: 'a + StateBackend> Executive<'a, B> {
| Err(evm::Error::BadJumpDestination {..})
| Err(evm::Error::BadInstruction {.. })
| Err(evm::Error::StackUnderflow {..})
| Err(evm::Error::BuiltIn {..})
| Err(evm::Error::OutOfStack {..}) => {
self.state.revert_to_checkpoint();
},
Expand Down
1 change: 1 addition & 0 deletions ethcore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ extern crate ethabi;
extern crate hardware_wallet;
extern crate stats;
extern crate num;
extern crate bn;

#[macro_use]
extern crate log;
Expand Down
7 changes: 6 additions & 1 deletion ethcore/src/types/trace_types/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ pub enum Error {
/// `StackUnderflow` when there is not enough stack elements to execute instruction
StackUnderflow,
/// When execution would exceed defined Stack Limit
OutOfStack,
OutOfStack,
/// When builtin contract failed on input data
BuiltIn,
/// Returned on evm internal error. Should never be ignored during development.
/// Likely to cause consensus issues.
Internal,
Expand All @@ -48,6 +50,7 @@ impl<'a> From<&'a EvmError> for Error {
EvmError::BadInstruction { .. } => Error::BadInstruction,
EvmError::StackUnderflow { .. } => Error::StackUnderflow,
EvmError::OutOfStack { .. } => Error::OutOfStack,
EvmError::BuiltIn { .. } => Error::OutOfStack,
EvmError::Internal(_) => Error::Internal,
}
}
Expand All @@ -68,6 +71,7 @@ impl fmt::Display for Error {
BadInstruction => "Bad instruction",
StackUnderflow => "Stack underflow",
OutOfStack => "Out of stack",
BuiltIn => "Built-in failed",
Internal => "Internal error",
};
message.fmt(f)
Expand All @@ -84,6 +88,7 @@ impl Encodable for Error {
StackUnderflow => 3,
OutOfStack => 4,
Internal => 5,
BuiltIn => 6,
};

s.append_internal(&value);
Expand Down

0 comments on commit 5e34235

Please sign in to comment.