Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Add randomness contract support to Authority Round.
Browse files Browse the repository at this point in the history
Changes have been cherry-picked from poanetwork's aura-pos branch.
Most of the work has been done by @mbr.
  • Loading branch information
afck committed Aug 20, 2019
1 parent 0a654af commit 64ae88d
Show file tree
Hide file tree
Showing 26 changed files with 1,237 additions and 52 deletions.
1 change: 1 addition & 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 @@ -14,6 +14,7 @@ blooms-db = { path = "../util/blooms-db", optional = true }
client-traits = { path = "./client-traits" }
common-types = { path = "types" }
crossbeam-utils = "0.6"
derive_more = "0.15.0"
engine = { path = "./engine" }
env_logger = { version = "0.5", optional = true }
ethabi = "8.0"
Expand Down
24 changes: 22 additions & 2 deletions ethcore/client-traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use common_types::{
pruning_info::PruningInfo,
receipt::LocalizedReceipt,
trace_filter::Filter as TraceFilter,
transaction::{self, LocalizedTransaction, CallError, SignedTransaction},
transaction::{self, Action, LocalizedTransaction, CallError, SignedTransaction},
tree_route::TreeRoute,
verification::{VerificationQueueInfo, Unverified},
};
Expand Down Expand Up @@ -367,7 +367,27 @@ pub trait BlockChainClient : Sync + Send + AccountData + BlockChain + CallContra
fn pruning_info(&self) -> PruningInfo;

/// Schedule state-altering transaction to be executed on the next pending block.
fn transact_contract(&self, address: Address, data: Bytes) -> Result<(), transaction::Error>;
fn transact_contract(&self, address: Address, data: Bytes) -> Result<(), transaction::Error> {
self.transact(Action::Call(address), data, None, None, None)
}

/// Returns a signed transaction. If gas limit, gas price, or nonce are not
/// specified, the defaults are used.
fn create_transaction(
&self,
action: Action,
data: Bytes,
gas: Option<U256>,
gas_price: Option<U256>,
nonce: Option<U256>
) -> Result<SignedTransaction, transaction::Error>;

/// Schedule state-altering transaction to be executed on the next pending
/// block with the given gas and nonce parameters.
///
/// If they are `None`, sensible values are selected automatically.
fn transact(&self, action: Action, data: Bytes, gas: Option<U256>, gas_price: Option<U256>, nonce: Option<U256>)
-> Result<(), transaction::Error>;

/// Get the address of the registry itself.
fn registrar_address(&self) -> Option<Address>;
Expand Down
8 changes: 7 additions & 1 deletion ethcore/engine/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use common_types::{
machine::{AuxiliaryData, AuxiliaryRequest},
},
errors::{EthcoreError as Error, EngineError},
transaction::{self, UnverifiedTransaction},
transaction::{self, SignedTransaction, UnverifiedTransaction},
};
use client_traits::EngineClient;

Expand Down Expand Up @@ -187,6 +187,12 @@ pub trait Engine: Sync + Send {
/// Allow mutating the header during seal generation. Currently only used by Clique.
fn on_seal_block(&self, _block: &mut ExecutedBlock) -> Result<(), Error> { Ok(()) }

/// Returns a list of transactions for a new block if we are the author.
///
/// This is called when the miner prepares a new block that this node will author and seal. It returns a list of
/// transactions that will be added to the block before any other transactions from the queue.
fn on_prepare_block(&self, _block: &ExecutedBlock) -> Result<Vec<SignedTransaction>, Error> { Ok(Vec::new()) }

/// Returns the engine's current sealing state.
fn sealing_state(&self) -> SealingState { SealingState::External }

Expand Down
15 changes: 15 additions & 0 deletions ethcore/engine/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use ethereum_types::{H256, Address};
use ethkey::{self, Signature};
use ethkey::crypto::ecies;

/// Everything that an Engine needs to sign messages.
pub trait EngineSigner: Send + Sync {
Expand All @@ -26,6 +27,12 @@ pub trait EngineSigner: Send + Sync {

/// Signing address
fn address(&self) -> Address;

/// Decrypt a message that was encrypted to this signer's key.
fn decrypt(&self, auth_data: &[u8], cipher: &[u8]) -> Result<Vec<u8>, ethkey::crypto::Error>;

/// The signer's public key, if available.
fn public(&self) -> Option<ethkey::Public>;
}

/// Creates a new `EngineSigner` from given key pair.
Expand All @@ -40,7 +47,15 @@ impl EngineSigner for Signer {
ethkey::sign(self.0.secret(), &hash)
}

fn decrypt(&self, auth_data: &[u8], cipher: &[u8]) -> Result<Vec<u8>, ethkey::crypto::Error> {
ecies::decrypt(self.0.secret(), auth_data, cipher)
}

fn address(&self) -> Address {
self.0.address()
}

fn public(&self) -> Option<ethkey::Public> {
Some(*self.0.public())
}
}
11 changes: 11 additions & 0 deletions ethcore/engine/src/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,18 @@ impl EngineSigner for (Arc<AccountProvider>, Address, Password) {
}
}

fn decrypt(&self, auth_data: &[u8], cipher: &[u8]) -> Result<Vec<u8>, ethkey::crypto::Error> {
self.0.decrypt(self.1, None, auth_data, cipher).map_err(|e| {
warn!("Unable to decrypt message: {:?}", e);
ethkey::crypto::Error::InvalidMessage
})
}

fn address(&self) -> Address {
self.1
}

fn public(&self) -> Option<ethkey::Public> {
self.0.account_public(self.1, &self.2).ok()
}
}
60 changes: 60 additions & 0 deletions ethcore/res/authority_round_randomness_contract.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"name": "TestAuthorityRoundRandomnessContract",
"engine": {
"authorityRound": {
"params": {
"stepDuration": 1,
"startStep": 2,
"validators": {
"list": [
"0x7d577a597b2742b498cb5cf0c26cdcd726d39e6e"
]
},
"immediateTransitions": true,
"maximumEmptySteps": "2",
"randomnessContractAddress": "0x0000000000000000000000000000000000000042"
}
}
},
"params": {
"gasLimitBoundDivisor": "0x0400",
"accountStartNonce": "0x0",
"maximumExtraDataSize": "0x20",
"minGasLimit": "0x1388",
"networkID" : "0x69",
"eip140Transition": "0x0",
"eip211Transition": "0x0",
"eip214Transition": "0x0",
"eip658Transition": "0x0"
},
"genesis": {
"seal": {
"authorityRound": {
"step": "0x0",
"signature": "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
}
},
"difficulty": "0x20000",
"author": "0x0000000000000000000000000000000000000000",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x",
"gasLimit": "0x222222"
},
"accounts": {
"0x7d577a597b2742b498cb5cf0c26cdcd726d39e6e": { "balance": "100000000000" },
"0000000000000000000000000000000000000001": { "balance": "1", "nonce": "1048576", "builtin": { "name": "ecrecover", "pricing": { "linear": { "base": 3000, "word": 0 } } } },
"0000000000000000000000000000000000000002": { "balance": "1", "nonce": "1048576", "builtin": { "name": "sha256", "pricing": { "linear": { "base": 60, "word": 12 } } } },
"0000000000000000000000000000000000000003": { "balance": "1", "nonce": "1048576", "builtin": { "name": "ripemd160", "pricing": { "linear": { "base": 600, "word": 120 } } } },
"0000000000000000000000000000000000000004": { "balance": "1", "nonce": "1048576", "builtin": { "name": "identity", "pricing": { "linear": { "base": 15, "word": 3 } } } },
"0000000000000000000000000000000000000005": { "balance": "1", "builtin": { "name": "modexp", "activate_at": 0, "pricing": { "modexp": { "divisor": 20 } } } },
"0000000000000000000000000000000000000006": { "balance": "1", "builtin": { "name": "alt_bn128_add", "activate_at": 0, "pricing": { "linear": { "base": 500, "word": 0 } } } },
"0000000000000000000000000000000000000007": { "balance": "1", "builtin": { "name": "alt_bn128_mul", "activate_at": 0, "pricing": { "linear": { "base": 40000, "word": 0 } } } },
"0000000000000000000000000000000000000008": { "balance": "1", "builtin": { "name": "alt_bn128_pairing", "activate_at": 0, "pricing": { "alt_bn128_pairing": { "base": 100000, "pair": 80000 } } } },
"9cce34f7ab185c7aba1b7c8140d620b4bda941d6": { "balance": "1606938044258990275541962092341162602522202993782792835301376", "nonce": "1048576" },
"0000000000000000000000000000000000000042": {
"balance": "1",
"constructor": "608060405234801561001057600080fd5b506107dd806100206000396000f3fe608060405234801561001057600080fd5b5060043610610107576000357c01000000000000000000000000000000000000000000000000000000009004806363f160e6116100a957806398df67c61161008357806398df67c61461031c578063baf11cab14610339578063c358ced014610365578063f2f56ffe1461036d57610107565b806363f160e6146102cc57806374ce90671461030c5780637a3e286b1461031457610107565b806320965255116100e557806320965255146102525780632e8a8dd51461026c5780633fa4f245146102985780635580e58b146102a057610107565b806304fdb0161461010c578063096a113d146101ad5780630b61ba85146101d9575b600080fd5b6101386004803603604081101561012257600080fd5b5080359060200135600160a060020a0316610399565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561017257818101518382015260200161015a565b50505050905090810190601f16801561019f5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610138600480360360408110156101c357600080fd5b5080359060200135600160a060020a031661043d565b610250600480360360408110156101ef57600080fd5b8135919081019060408101602082013564010000000081111561021157600080fd5b82018360208201111561022357600080fd5b8035906020019184600183028401116401000000008311171561024557600080fd5b5090925090506104ef565b005b61025a610574565b60408051918252519081900360200190f35b61025a6004803603604081101561028257600080fd5b5080359060200135600160a060020a031661057b565b61025a610595565b61025a600480360360408110156102b657600080fd5b5080359060200135600160a060020a031661059b565b6102f8600480360360408110156102e257600080fd5b5080359060200135600160a060020a03166105b8565b604080519115158252519081900360200190f35b6102f86105e2565b61025a6105f2565b6102506004803603602081101561033257600080fd5b50356105fd565b6102f86004803603604081101561034f57600080fd5b5080359060200135600160a060020a03166106ab565b6102f86106c1565b61025a6004803603604081101561038357600080fd5b5080359060200135600160a060020a03166106cc565b600160208181526000938452604080852082529284529282902080548351600293821615610100026000190190911692909204601f810185900485028301850190935282825290929091908301828280156104355780601f1061040a57610100808354040283529160200191610435565b820191906000526020600020905b81548152906001019060200180831161041857829003601f168201915b505050505081565b6000828152600160208181526040808420600160a060020a038616855282529283902080548451600294821615610100026000190190911693909304601f810183900483028401830190945283835260609390918301828280156104e25780601f106104b7576101008083540402835291602001916104e2565b820191906000526020600020905b8154815290600101906020018083116104c557829003601f168201915b5050505050905092915050565b4133146104fb57600080fd5b610507600143036106f2565b61051057600080fd5b600061051e600143036106fd565b905061052a81336106ab565b1561053457600080fd5b6000818152602081815260408083203380855290835281842088905584845260018352818420908452909152902061056d908484610710565b5050505050565b6003545b90565b600060208181529281526040808220909352908152205481565b60035481565b600260209081526000928352604080842090915290825290205481565b6000918252600260209081526040808420600160a060020a03939093168452919052902054151590565b60006105ed436106f2565b905090565b60006105ed436106fd565b41331461060957600080fd5b61061560014303610704565b61061e57600080fd5b600061062c600143036106fd565b905061063881336105b8565b1561064257600080fd5b60408051602080820185905282518083038201815291830183528151918101919091206000848152808352838120338252909252919020541461068457600080fd5b60009081526002602090815260408083203384529091529020819055600380549091189055565b6000806106b884846106cc565b14159392505050565b60006105ed43610704565b600091825260208281526040808420600160a060020a0393909316845291905290205490565b600360069091061090565b6006900490565b60036006909106101590565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106107515782800160ff1982351617855561077e565b8280016001018555821561077e579182015b8281111561077e578235825591602001919060010190610763565b5061078a92915061078e565b5090565b61057891905b8082111561078a576000815560010161079456fea265627a7a72305820883fbcdb4e45da5d878f68564910eee8ea01c855ad862aee3f5162e50e06e9f564736f6c634300050a0032"
}
}
}
158 changes: 158 additions & 0 deletions ethcore/res/contracts/authority_round_random.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
[{
"constant": true,
"inputs": [],
"name": "currentRandom",
"outputs": [{
"name": "",
"type": "uint256[]"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [{
"name": "_secretHash",
"type": "bytes32"
},
{
"name": "_cipher",
"type": "bytes"
}
],
"name": "commitHash",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [{
"name": "_secret",
"type": "uint256"
}],
"name": "revealSecret",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "currentCollectRound",
"outputs": [{
"name": "",
"type": "uint256"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [{
"name": "_collectRound",
"type": "uint256"
},
{
"name": "_validator",
"type": "address"
}
],
"name": "getCipher",
"outputs": [{
"name": "",
"type": "bytes"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [{
"name": "_collectRound",
"type": "uint256"
},
{
"name": "_validator",
"type": "address"
}
],
"name": "getCommit",
"outputs": [{
"name": "",
"type": "bytes32"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [{
"name": "_collectRound",
"type": "uint256"
},
{
"name": "_validator",
"type": "address"
}
],
"name": "isCommitted",
"outputs": [{
"name": "",
"type": "bool"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "isCommitPhase",
"outputs": [{
"name": "",
"type": "bool"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "isRevealPhase",
"outputs": [{
"name": "",
"type": "bool"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [{
"name": "_collectRound",
"type": "uint256"
},
{
"name": "_validator",
"type": "address"
}
],
"name": "sentReveal",
"outputs": [{
"name": "",
"type": "bool"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]
Loading

0 comments on commit 64ae88d

Please sign in to comment.