Skip to content

Commit

Permalink
[WIP] Working PR (matter-labs#31)
Browse files Browse the repository at this point in the history
* Store tx result and block ops in db + function to get  block ops from db

* Fix formatting

* ETH watcher upgrade done

* Create dummy prover

* Store tokens to db

* Update deps

* Revive eth sender

* Deploy scripts

* No exit queue address

* Make franklin init work

* Improve wallet

* txs fix

* Add withdraw functions

* Integration test
  • Loading branch information
dvush authored Aug 15, 2019
1 parent 879e2b0 commit e0b1041
Show file tree
Hide file tree
Showing 41 changed files with 1,876 additions and 1,332 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Cargo.lock
!/etc/env/dev.env.example
/contracts/yarn-error.log
/deploy.log
/genesis.log
/keys
/tmp
/volumes
Expand Down
1,003 changes: 525 additions & 478 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ init:
@bin/init

yarn:
@cd js/franklin && yarn
@cd js/franklin_lib && yarn
@cd js/client && yarn
@cd js/loadtest && yarn
@cd js/explorer && yarn
Expand Down Expand Up @@ -60,6 +60,9 @@ db-drop: confirm_action
db-wait:
@bin/db-wait

genesis: confirm_action db-reset
@bin/genesis.sh

# Frontend clients

dist-config:
Expand Down Expand Up @@ -176,7 +179,7 @@ deposit: confirm_action
# Devops: main

# (Re)deploy contracts and database
redeploy: confirm_action stop deploy-contracts db-reset
redeploy: confirm_action stop deploy-contracts db-insert-contract

dev-ready = docker ps | grep -q "$(GETH_DOCKER_IMAGE)"

Expand Down
23 changes: 6 additions & 17 deletions bin/deploy-contracts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,15 @@ cd contracts
yarn deploy | tee ../deploy.log
cd ..

export LABEL=$FRANKLIN_ENV-`date +%Y-%m-%d-%H%M%S`

export NEW_CONTRACT=`cat deploy.log | grep "Franklin address" | grep -oE '0x(.+)' | sed -n "s/0x//p"`


if [[ ! -z "$NEW_CONTRACT" ]]
CONTRACT_ADDR_NEW_VALUE=`grep "CONTRACT_ADDR" deploy.log`
if [[ ! -z "$CONTRACT_ADDR_NEW_VALUE" ]]
then
echo New contract at $NEW_CONTRACT

OLD_CONTRACT=`grep "^CONTRACT_ADDR" ./$ENV_FILE | grep -oE '=(.+)' | sed -n "s/=//p"`
echo Old contract at $OLD_CONTRACT

export LABEL=$FRANKLIN_ENV-Contract_deploy-`date +%Y-%m-%d-%H%M%S`
mkdir -p logs/$LABEL/
cp deploy.log logs/$LABEL/deploy.log
cp ./$ENV_FILE logs/$LABEL/$FRANKLIN_ENV.bak

sed -i".bak" "s/^CONTRACT_ADDR=$OLD_CONTRACT/CONTRACT_ADDR=$NEW_CONTRACT/g" ./$ENV_FILE

echo successfully deployed contracts

cp deploy.log logs/$LABEL/
echo $CONTRACT_ADDR_NEW_VALUE
python3 bin/replace-env-variable.py ./$ENV_FILE $CONTRACT_ADDR_NEW_VALUE
else
echo "Contract deployment failed"
exit 1
Expand Down
16 changes: 16 additions & 0 deletions bin/genesis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

. .setup_env

set -e

cargo run --bin server -- --genesis | tee genesis.log

GENESIS_ROOT_NEW_VALUE=`grep GENESIS_ROOT genesis.log`

export LABEL=$FRANKLIN_ENV-Genesis_gen-`date +%Y-%m-%d-%H%M%S`
mkdir -p logs/$LABEL/
cp ./$ENV_FILE logs/$LABEL/$FRANKLIN_ENV.bak
cp genesis.log logs/$LABEL/
echo $GENESIS_ROOT_NEW_VALUE
python3 bin/replace-env-variable.py ./$ENV_FILE $GENESIS_ROOT_NEW_VALUE
1 change: 1 addition & 0 deletions bin/init
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ franklin env
franklin yarn || true # It can fail.
franklin db-wait
franklin db-setup
franklin genesis
franklin redeploy
19 changes: 19 additions & 0 deletions bin/replace-env-variable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/python3

import fileinput
import sys

def process_line(line, env_name, env_val):
line_env_name = line.split("=")[0]
if line_env_name == env_name:
print("%s=%s" % (env_name, env_val))
else:
print(line, end = '')

if __name__ == "__main__":
file = sys.argv[1]
env = sys.argv[2].split("=")
env_name = env[0]
env_val = env[1]
for line in fileinput.input(file, inplace=True):
process_line(line, env_name, env_val)
2 changes: 1 addition & 1 deletion contracts/contracts/Franklin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ contract Franklin is DummyVerifier, VerificationKeys {
emit OnchainDeposit(
msg.sender,
_tokenId,
_amount,
balances[msg.sender][_tokenId].balance,
lockedUntilBlock,
_franklin_addr
);
Expand Down
1 change: 1 addition & 0 deletions contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"scripts": {
"test": "waffle && mocha -r ts-node/register test/**/*.ts",
"deploy": "waffle && ts-node scripts/testnet-deploy.ts",
"print-operator-wallet": "ts-node scripts/init-operator-acc.ts",
"lint:solidity": "solhint contracts/**/*.sol",
"lint:ts": "tslint -c tslint.json {scripts,test}/**/*.ts",
"lint:ts-fix": "tslint -c tslint.json --fix {scripts,test}/**/*.ts",
Expand Down
19 changes: 19 additions & 0 deletions contracts/scripts/exampleDeposits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {franklinContractCode} from "../src.ts/deploy";
import {parseEther} from "ethers/utils";
import {Contract, ethers} from "ethers";

const provider = new ethers.providers.JsonRpcProvider(process.env.WEB3_URL);
const wallet = ethers.Wallet.fromMnemonic(process.env.MNEMONIC, "m/44'/60'/0'/0/1").connect(provider);
const franklinAddress = "010203040506070809101112131415161718192021222334252627";
const franklinAddressBinary = Buffer.from(franklinAddress, "hex");

async function main() {
const franklinDeployedContract = new Contract(process.env.CONTRACT_ADDR, franklinContractCode.interface, wallet);
const depositValue = parseEther("0.3");
const depositFee = parseEther("0.01");
const tx = await franklinDeployedContract.depositETH(franklinAddressBinary, {value: depositValue});
const receipt = await tx.wait();
console.log(receipt);
}

main();
14 changes: 14 additions & 0 deletions contracts/scripts/init-operator-acc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'ethers'
import {ethers} from "ethers";
import {Wallet} from "../../js/franklin_lib/src/wallet";

async function main() {
const provider = new ethers.providers.JsonRpcProvider(process.env.WEB3_URL);
const operatorAccountPrivateKey = process.env.OPERATOR_PRIVATE_KEY;
let ethWallet = new ethers.Wallet(operatorAccountPrivateKey);
let franklinWallet = await Wallet.fromEthWallet(ethWallet);
console.log(`OPERATOR_ETH_ADDRESS=${ethWallet.address}`);
console.log(`OPERATOR_FRANKLIN_ADDRESS=${franklinWallet.address}`);
}

main();
3 changes: 2 additions & 1 deletion contracts/scripts/testnet-deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {addTestERC20Token, deployFranklin} from "../src.ts/deploy";
async function main() {
const provider = new ethers.providers.JsonRpcProvider(process.env.WEB3_URL);
const wallet = ethers.Wallet.fromMnemonic(process.env.MNEMONIC, "m/44'/60'/0'/0/1").connect(provider);
const franklin = await deployFranklin(wallet);
const franklin = await deployFranklin(wallet, process.env.GENESIS_ROOT);
await franklin.setValidator(process.env.OPERATOR_ETH_ADDRESS, true);
await addTestERC20Token(wallet, franklin);
}

Expand Down
6 changes: 3 additions & 3 deletions contracts/src.ts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export const ERC20MintableContract = function () {
}();
export const franklinContractCode = require('../build/Franklin');

export async function deployFranklin(wallet, franklinCode = franklinContractCode) {
export async function deployFranklin(wallet, genesisRoot = ethers.constants.HashZero, franklinCode = franklinContractCode) {
try {
let contract = await deployContract(wallet, franklinCode, [ethers.constants.HashZero, wallet.address, wallet.address], {
let contract = await deployContract(wallet, franklinCode, [genesisRoot, ethers.constants.AddressZero, wallet.address], {
gasLimit: 8000000,
});
console.log("Franklin address:" + contract.address);
console.log(`CONTRACT_ADDR=${contract.address}`);

return contract
} catch (err) {
Expand Down
18 changes: 10 additions & 8 deletions core/eth_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,25 @@ pub struct ETHClient {
/// ETH client for Plasma contract
/// All methods are blocking for now
impl ETHClient {
pub fn new(contract_abi: ABI) -> Self {
pub fn new(contract_abi: String) -> Self {
Self {
web3_url: env::var("WEB3_URL").unwrap_or_else(|_| "http://localhost:8545".to_string()),
private_key: H256::from_str(&env::var("PRIVATE_KEY").unwrap_or_else(|_| {
"aa8564af9bef22f581e99125d1829b76c45d08e4f6f0b74d586911f4318b6776".to_string()
}))
private_key: H256::from_str(
&env::var("OPERATOR_PRIVATE_KEY").expect("OPERATOR_PRIVATE_KEY"),
)
.expect("private key must be correct"),
contract_addr: H160::from_str(
&env::var("CONTRACT_ADDR")
.unwrap_or_else(|_| "616e08c733fe20e99bf70c5088635694d5e25c54".to_string()),
.map(|s| s[2..].to_string())
.expect("CONTRACT_ADDR"),
)
.expect("contract address must be correct"),
sender_account: env::var("SENDER_ACCOUNT")
.unwrap_or_else(|_| "e5d0efb4756bd5cdd4b5140d3d2e08ca7e6cf644".to_string()),
sender_account: env::var("OPERATOR_ETH_ADDRESS")
.map(|s| s[2..].to_string())
.expect("OPERATOR_ETH_ADDRESS"),
chain_id: u8::from_str(&env::var("CHAIN_ID").unwrap_or_else(|_| "4".to_string()))
.expect("chain id must be correct"),
contract: ethabi::Contract::load(contract_abi.0)
contract: ethabi::Contract::load(contract_abi.as_bytes())
.expect("contract must be loaded correctly"),
reqwest_client: reqwest::Client::new(),
gas_price_factor: usize::from_str(
Expand Down
1 change: 0 additions & 1 deletion core/franklinmodels/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ pub struct Operation {
}

pub enum StateKeeperRequest {
AddTx(Box<FranklinTx>, oneshot::Sender<Result<(), String>>),
GetAccount(AccountAddress, Sender<Option<Account>>),
GetNetworkStatus(Sender<NetworkStatus>),
TimerTick,
Expand Down
17 changes: 15 additions & 2 deletions core/franklinmodels/src/node/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ use franklin_crypto::alt_babyjubjub::JubjubEngine;
use franklin_crypto::jubjub::{edwards, Unknown};
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use super::{AccountId, AccountUpdates, Nonce, TokenAmount, TokenId};
use super::{AccountId, AccountUpdates, Nonce, TokenId};
use super::{Engine, Fr};
use crate::circuit::account::{Balance, CircuitAccount};

#[derive(Debug, Clone, PartialEq, Default, Eq, Hash)]
#[derive(Clone, PartialEq, Default, Eq, Hash)]
pub struct AccountAddress {
pub data: [u8; 27],
}

impl std::fmt::Debug for AccountAddress {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.to_hex())
}
}

impl AccountAddress {
pub fn to_hex(&self) -> String {
format!("0x{}", hex::encode(&self.data))
Expand All @@ -32,6 +38,13 @@ impl AccountAddress {
data: bytes.as_slice().try_into().unwrap(),
})
}

pub fn from_bytes(bytes: &[u8]) -> Result<Self, failure::Error> {
ensure!(bytes.len() == 27, "Size mismatch");
Ok(AccountAddress {
data: bytes.try_into().unwrap(),
})
}
}

impl Serialize for AccountAddress {
Expand Down
16 changes: 14 additions & 2 deletions core/franklinmodels/src/node/block.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::operations::FranklinOp;
use super::tx::FranklinTx;
use super::{AccountId, BlockNumber, Fr};
use crate::params::BLOCK_SIZE_CHUNKS;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ExecutedTx {
Expand All @@ -20,7 +21,18 @@ pub struct Block {

impl Block {
pub fn get_eth_public_data(&self) -> Vec<u8> {
// TODO unimplemented
Vec::new()
let mut executed_tx_pub_data = self
.block_transactions
.iter()
.filter_map(|tx| tx.op.clone().map(|op| op.public_data()))
.fold(Vec::new(), |mut acc, pub_data| {
acc.extend(pub_data.into_iter());
acc
});

// Pad block with noops.
executed_tx_pub_data.resize(BLOCK_SIZE_CHUNKS * 8, 0x00);

executed_tx_pub_data
}
}
12 changes: 9 additions & 3 deletions core/franklinmodels/src/node/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::merkle_tree::{PedersenHasher, SparseMerkleTree};
use super::primitives::pack_as_float;
use bigdecimal::BigDecimal;
use franklin_crypto::eddsa;
use pairing::bn256;
Expand Down Expand Up @@ -43,6 +44,11 @@ pub type TokenId = u16;
pub type AccountId = u32;
pub type BlockNumber = u32;
pub type Nonce = u32;
/// 3 bytes used.
pub type TokenAmount = u32;
pub type FeeAmount = u8;

pub fn pack_token_amount(amount: &BigDecimal) -> Vec<u8> {
pack_as_float(amount, 5, 19)
}

pub fn pack_fee_amount(amount: &BigDecimal) -> Vec<u8> {
pack_as_float(amount, 4, 4)
}
Loading

0 comments on commit e0b1041

Please sign in to comment.