-
Notifications
You must be signed in to change notification settings - Fork 15
Stateless MPT-based token script #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
s1na
wants to merge
16
commits into
ewasm:master
Choose a base branch
from
s1na:smpt
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b61cc66
Add stateless MPT-based token script
s1na 6183ae5
smpt: add missing .cargo dir
s1na 11715f5
ci: install clang for smpt
s1na 91aa5f2
ci: fix clang installation
s1na 300e2ff
ci: install clang-8
s1na 6ff1e46
ci: fix clang-8 installation
s1na 47a87b2
ci: smpt test finally passes
s1na fb6385f
smpt: process blocks with multiple txes
s1na 47c2bbb
smpt: add js relayer
s1na 03295c1
smpt: update default yaml
s1na 6598bec
smpt: fix default yaml
s1na 6892452
smpt: use ewasm_api v0.10.0
s1na 6ccf5e4
smpt: add smpt-specific gitignore
s1na d707e89
smpt: use wee_alloc instead of qimalloc (fixes bug)
s1na ab4b7d2
smpt: deduplicate proof nodes
s1na a1e747b
smpt: fix default yaml
s1na File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| [build] | ||
| target = "wasm32-unknown-unknown" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| [package] | ||
| name = "smpt" | ||
| version = "0.0.0" | ||
| license = "Apache-2.0" | ||
| repository = "https://github.com/ewasm/scout" | ||
| description = "Stateless merkle patricia balance tree" | ||
| publish = false | ||
| edition = "2018" | ||
|
|
||
| [dependencies] | ||
| patricia-trie-ethereum = "0.1.0" | ||
| hash-db = "0.12.4" | ||
| trie-db = "0.12.4" | ||
| keccak-hasher = "0.12.4" | ||
| kvdb = "0.1" | ||
| memory-db = "0.12.4" | ||
| rlp = "0.4.0" | ||
| elastic-array = "0.10" | ||
| ethereum-types = "0.6.0" | ||
| tiny-keccak = "1.4.2" | ||
| plain_hasher = "0.2" | ||
| libsecp256k1 = "0.2.2" | ||
| #eth-secp256k1 = { git = "https://github.com/paritytech/rust-secp256k1" } | ||
| #ethkey = "0.2.5" | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib"] | ||
|
|
||
| [dependencies.ewasm_api] | ||
| git = "https://github.com/ewasm/ewasm-rust-api" | ||
| rev = "1c01982" | ||
| default-features = false | ||
| features = ["std", "eth2", "qimalloc"] | ||
|
|
||
| [profile.release] | ||
| lto = true | ||
| debug = false | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| all: build test | ||
|
|
||
| build: | ||
| cargo build --release | ||
| chisel run --config chisel.toml; true | ||
|
|
||
| test: | ||
| node relayer/index.js | ||
| ../../target/release/phase2-scout smpt.yaml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| smpt: | ||
| file: "target/wasm32-unknown-unknown/release/smpt.wasm" | ||
| trimexports: | ||
| preset: "ewasm" | ||
| verifyexports: | ||
| preset: "ewasm" | ||
| repack: | ||
| preset: "ewasm" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| const assert = require('assert') | ||
| const { promisify } = require('util') | ||
| const BN = require('bn.js') | ||
| const Trie = require('merkle-patricia-tree') | ||
| const Account = require('ethereumjs-account').default | ||
| const StateManager = require('ethereumjs-vm/dist/state/stateManager').default | ||
| const PStateManager = require('ethereumjs-vm/dist/state/promisified').default | ||
| const { keccak256, ecsign, stripZeros } = require('ethereumjs-util') | ||
| const { encode } = require('rlp') | ||
| const Wallet = require('ethereumjs-wallet') | ||
| const yaml = require('js-yaml') | ||
| const fs = require('fs') | ||
|
|
||
| const prove = promisify(Trie.prove) | ||
| const verifyProof = promisify(Trie.verifyProof) | ||
|
|
||
| async function main () { | ||
| const testSuite = { | ||
| 'beacon_state': { | ||
| 'execution_scripts': [ | ||
| 'target/wasm32-unknown-unknown/release/smpt.wasm' | ||
| ], | ||
| }, | ||
| 'shard_pre_state': { | ||
| 'exec_env_states': [ | ||
| ] | ||
| }, | ||
| 'shard_blocks': [ | ||
| ], | ||
| 'shard_post_state': { | ||
| 'exec_env_states': [ | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| const rawState = new StateManager() | ||
| const state = new PStateManager(rawState) | ||
|
|
||
| // Generate random accounts | ||
| let accounts = await generateAccounts(state) | ||
|
|
||
| let root = await state.getStateRoot() | ||
| testSuite.shard_pre_state.exec_env_states.push(root.toString('hex')) | ||
|
|
||
| // Generate txes | ||
| let txes = await generateTxes(state, accounts) | ||
|
|
||
| // Serialize witnesses and tx data | ||
| const blockData = encode(txes) | ||
| console.log(`block data length: ${blockData.length}`) | ||
| testSuite.shard_blocks.push({ | ||
| 'env': 0, | ||
| 'data': blockData.toString('hex') | ||
| }) | ||
|
|
||
| root = await state.getStateRoot() | ||
| testSuite.shard_post_state.exec_env_states.push(root.toString('hex')) | ||
|
|
||
| const serializedTestSuite = yaml.safeDump(testSuite) | ||
| fs.writeFileSync('smpt.yaml', serializedTestSuite) | ||
| } | ||
|
|
||
| async function generateTxes (state, accounts) { | ||
| let txes = [] | ||
| for (let i = 0; i < 20; i++) { | ||
| const root = await state.getStateRoot() | ||
| const from = accounts[i].address | ||
| const to = accounts[i + 1].address | ||
| const value = new BN('00000000000000000000000000000000000000000000000000000000000000ff', 16) | ||
| const nonce = new BN('0000000000000000000000000000000000000000000000000000000000000000', 16) | ||
|
|
||
| const fromAccount = await state.getAccount(from) | ||
| const fromWitness = await prove(state._wrapped._trie, keccak256(from)) | ||
| let val = await verifyProof(root, keccak256(from), fromWitness) | ||
| assert(val.equals(fromAccount.serialize()), "valid from witness") | ||
|
|
||
| const toAccount = await state.getAccount(to) | ||
| const toWitness = await prove(state._wrapped._trie, keccak256(to)) | ||
| val = await verifyProof(root, keccak256(to), toWitness) | ||
| assert(val.equals(toAccount.serialize()), "valid to witness") | ||
|
|
||
| const txRlp = encode([to, stripZeros(value.toBuffer('be', 32)), stripZeros(nonce.toBuffer('be', 32))]) | ||
| const txHash = keccak256(txRlp) | ||
| const txSig = ecsign(txHash, accounts[i].privateKey) | ||
|
|
||
| txes.push([ | ||
| [to, stripZeros(value.toBuffer('be', 32)), stripZeros(nonce.toBuffer('be', 32)), [stripZeros(txSig.r), stripZeros(txSig.s), txSig.v]], | ||
| fromWitness, | ||
| toWitness | ||
| ]) | ||
|
|
||
| await transfer(state, { from, to, value, nonce }) | ||
| } | ||
| return txes | ||
| } | ||
|
|
||
| async function transfer (state, tx) { | ||
| let { from, to, value, nonce } = tx | ||
| assert(value.gten(0)) | ||
|
|
||
| const fromAcc = await state.getAccount(from) | ||
| const toAcc = await state.getAccount(to) | ||
|
|
||
| assert(new BN(fromAcc.balance).gte(value)) | ||
| assert(new BN(fromAcc.nonce).eq(nonce)) | ||
|
|
||
| const newFromBalance = new BN(fromAcc.balance).sub(value) | ||
| fromAcc.balance = newFromBalance.toBuffer() | ||
| fromAcc.nonce = nonce.addn(1).toBuffer() | ||
| const newToBalance = new BN(toAcc.balance).add(value) | ||
| toAcc.balance = newToBalance.toBuffer() | ||
|
|
||
| await state.putAccount(from, fromAcc) | ||
| await state.putAccount(to, toAcc) | ||
| } | ||
|
|
||
| async function generateAccounts (state) { | ||
| let accounts = [] | ||
| for (let i = 0; i < 500; i++) { | ||
| let wallet = Wallet.generate() | ||
| let address = wallet.getAddress() | ||
| let privateKey = wallet.getPrivateKey() | ||
| let account = new Account() | ||
| account.balance = new BN('ffffff', 16).toBuffer() | ||
| accounts.push({ | ||
| address, | ||
| privateKey, | ||
| account | ||
| }) | ||
| await state.putAccount(address, account) | ||
| } | ||
| return accounts | ||
| } | ||
|
|
||
| main().then(() => {}).catch((e) => console.log(e)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "name": "mpt", | ||
| "version": "1.0.0", | ||
| "description": "", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "author": "", | ||
| "license": "ISC", | ||
| "dependencies": { | ||
| "bn.js": "^4.11.8", | ||
| "ethereumjs-account": "^3.0.0", | ||
| "ethereumjs-util": "^6.1.0", | ||
| "ethereumjs-vm": "^4.0.0-beta.1", | ||
| "ethereumjs-wallet": "^0.6.3", | ||
| "js-yaml": "^3.13.1", | ||
| "merkle-patricia-tree": "^3.0.0", | ||
| "rlp": "^2.2.3" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| use ethereum_types::{H256, U256}; | ||
| use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream}; | ||
|
|
||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub struct BasicAccount { | ||
| pub nonce: U256, | ||
| pub balance: U256, | ||
| pub storage_root: H256, | ||
| pub code_hash: H256, | ||
| } | ||
|
|
||
| impl Decodable for BasicAccount { | ||
| fn decode(d: &Rlp) -> Result<Self, DecoderError> { | ||
| if d.item_count()? != 4 { | ||
| return Err(DecoderError::RlpIncorrectListLen); | ||
| } | ||
|
|
||
| Ok(BasicAccount { | ||
| nonce: d.val_at(0)?, | ||
| balance: d.val_at(1)?, | ||
| storage_root: d.val_at(2)?, | ||
| code_hash: d.val_at(3)?, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl Encodable for BasicAccount { | ||
| fn rlp_append(&self, s: &mut RlpStream) { | ||
| s.begin_list(4); | ||
| s.append(&self.nonce); | ||
| s.append(&self.balance); | ||
| s.append(&self.storage_root); | ||
| s.append(&self.code_hash); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Copyright 2015-2019 Parity Technologies (UK) Ltd. | ||
| // This file is part of Parity Ethereum. | ||
|
|
||
| // Parity Ethereum is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
|
|
||
| // Parity Ethereum is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| //! Hasher implementation for the Keccak-256 hash | ||
| //! Taken from: | ||
| //! https://github.com/paritytech/parity-ethereum/blob/6bb106a784678cc2cadabfd621981371f477c48d/util/keccak-hasher/src/lib.rs | ||
| extern crate ethereum_types; | ||
| extern crate hash_db; | ||
| extern crate plain_hasher; | ||
| extern crate tiny_keccak; | ||
|
|
||
| use ethereum_types::H256; | ||
| use hash_db::Hasher; | ||
| use plain_hasher::PlainHasher; | ||
| use tiny_keccak::Keccak; | ||
| /// Concrete `Hasher` impl for the Keccak-256 hash | ||
| #[derive(Default, Debug, Clone, PartialEq)] | ||
| pub struct KeccakHasher; | ||
| impl Hasher for KeccakHasher { | ||
| type Out = H256; | ||
| type StdHasher = PlainHasher; | ||
| const LENGTH: usize = 32; | ||
| fn hash(x: &[u8]) -> Self::Out { | ||
| let mut out = [0; 32]; | ||
| Keccak::keccak256(x, &mut out); | ||
| out.into() | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.