Skip to content
Permalink
Browse files

Merge pull request #210 from syuhei176/succeed-to-submit

Succeed to submit merkle root #204
  • Loading branch information...
syuhei176 committed Sep 15, 2019
2 parents d45de5f + f2e9e90 commit 8b4a63dd841ae999a3b0642d3935972a0e5516ef
@@ -46,12 +46,13 @@ impl Stream for Handle {

fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
try_ready!(self.interval.poll().map_err(|_| ()));
println!("start to submit merkle root");
println!("start to submit");
let agg = self.plasma_aggregator.lock().unwrap();
if agg.submit_next_block().is_ok() {
println!("succeeded to submit");
Ok(Async::Ready(Some(())))
} else {
Ok(Async::NotReady)
Ok(Async::Ready(Some(())))
}
}
}
@@ -60,7 +61,7 @@ impl ServerHandler for Handle {
fn handle_message(&mut self, msg: Message, sender: Sender) {
let mut agg = self.plasma_aggregator.lock().unwrap();
let ingest_result = agg.ingest_transaction(Transaction::from_abi(&msg.message).unwrap());
println!("{:?}", ingest_result);
println!("Recieving new transaction {:?}", ingest_result);

let state_updates = StateUpdateList::new(agg.get_all_state_updates());
println!("STATE_UPDATES: {:?}", state_updates);
@@ -73,10 +74,13 @@ impl ServerHandler for Handle {
}

fn main() {
let aggregator_address = hex::decode("627306090abab3a6e1400e9345bc60c78a8bef57").unwrap();
let commitment_contract_address =
hex::decode("30753E4A8aad7F8597332E813735Def5dD395028").unwrap();
let mut aggregator = PlasmaAggregator::new(
Address::from_slice(&aggregator_address),
Address::zero(),
Address::zero(),
Address::zero(),
Address::from_slice(&commitment_contract_address),
"c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3",
);
aggregator.insert_test_ranges();
@@ -56,18 +56,19 @@ impl<KVS: KeyValueStore + DatabaseTrait> BlockManager<KVS> {
let root = block.merkelize()?;

// send root hash to commitment contract
let f = File::open("CommitmentContract.json").unwrap();
let f = File::open("../contract-wrapper/CommitmentChain.json").unwrap();
let reader = BufReader::new(f);
let contract_abi = ContractABI::load(reader).unwrap();
let contract = CommitmentContractAdaptor::new(
"http://127.0.0.1:9545",
&self.commitment_contract_address.to_string(),
"http://127.0.0.1:8545",
self.commitment_contract_address,
contract_abi,
)
.unwrap();
let _ = contract.submit_block(self.aggregator_address, root);
let _ = contract.submit_block(self.aggregator_address, block.get_block_number(), root)?;

let _ = block_db.save_block(&block);
let _ = block_db.delete_all_queued_state_updates();
Ok(())
}

@@ -1,3 +1,4 @@
use contract_wrapper::error::Error as ContractError;
use ethabi::Error as AbiDecodeError;
use failure::{Backtrace, Context, Fail};
use plasma_db::error::Error as PlasmaDbError;
@@ -13,6 +14,8 @@ pub enum ErrorKind {
AbiDecode,
#[fail(display = "Db Error")]
PlasmaDbError,
#[fail(display = "Contract Error")]
ContractError,
#[fail(display = "Invalid Transaction")]
InvalidTransaction,
#[fail(display = "Merkelizing Error")]
@@ -81,9 +84,17 @@ impl From<AbiDecodeError> for Error {
}

impl From<PlasmaDbError> for Error {
fn from(_error: PlasmaDbError) -> Error {
fn from(error: PlasmaDbError) -> Error {
Error {
inner: Context::from(ErrorKind::PlasmaDbError),
inner: error.context(ErrorKind::PlasmaDbError),
}
}
}

impl From<ContractError> for Error {
fn from(error: ContractError) -> Error {
Error {
inner: error.context(ErrorKind::ContractError),
}
}
}
@@ -1,28 +1,41 @@
[
{
"anonymous": false,
"constant": false,
"inputs": [
{
"indexed": false,
"name": "root",
"internalType": "uint64",
"name": "blkNumber",
"type": "uint64"
},
{
"internalType": "bytes32",
"name": "_root",
"type": "bytes32"
}
],
"name": "BlockSubmitted",
"type": "event"
"name": "submit_root",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"anonymous": false,
"inputs": [
{
"name": "_root",
"indexed": false,
"internalType": "uint64",
"name": "blockNumber",
"type": "uint64"
},
{
"indexed": false,
"internalType": "bytes32",
"name": "root",
"type": "bytes32"
}
],
"name": "submit",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
"name": "BlockSubmitted",
"type": "event"
}
]
]
@@ -14,14 +14,11 @@ pub struct CommitmentContractAdaptor {
}

impl CommitmentContractAdaptor {
pub fn new(host: &str, address: &str, abi: ContractABI) -> Result<Self, Error> {
pub fn new(host: &str, address: Address, abi: ContractABI) -> Result<Self, Error> {
let (_eloop, http) = web3::transports::Http::new(host)
.map_err(|_| Error::from(ErrorKind::InvalidInputType))?;
.map_err(|_| Error::from(ErrorKind::FailedToConnect))?;
let web3 = web3::Web3::new(http);

let address: Address = address
.parse()
.map_err(|_| Error::from(ErrorKind::InvalidInputType))?;
let contract = Contract::new(web3.eth(), address, abi);

Ok(Self {
@@ -32,14 +29,25 @@ impl CommitmentContractAdaptor {
})
}

pub fn submit_block(&self, from: Address, root: Bytes) -> Result<H256, Error> {
let result = self
.inner
.call("submit", root.to_vec(), from, Options::default());
pub fn submit_block(
&self,
from: Address,
block_number: u64,
root: Bytes,
) -> Result<H256, Error> {
let result = self.inner.call(
"submit_root",
(block_number, H256::from_slice(root.to_vec().as_slice())),
from,
Options::default(),
);

match result.wait() {
Ok(r) => Ok(r),
Err(e) => Err(e.into()),
Err(e) => {
println!("{:?}", e);
Err(e.into())
}
}
}
}
@@ -9,6 +9,8 @@ use web3::contract::Error as Web3Error;
pub enum ErrorKind {
#[fail(display = "Invalid Input Type")]
InvalidInputType,
#[fail(display = "Failed to connect")]
FailedToConnect,
#[fail(display = "ABI error")]
Abi,
#[fail(display = "Web3 error")]

0 comments on commit 8b4a63d

Please sign in to comment.
You can’t perform that action at this time.