Skip to content

Commit 9993873

Browse files
authored
Solidity contract that accepts unverified substrate headers (#65)
* solidity contract * continue * upd * cargo update * fixes * ehtereum_headers -> headers * extracted some common stuff * ethereum_sync.rs -> sync.rs * make sync generic * continue extracting * continue * add eth-contract argument * continue * some fixes * contract v2 * continue * more fixes * more fixes * deal with duplicated params * removed multiple call_rpc variants * bail_on_error!() * fn submit_ethereum_transaction * more fixes * cargo fmt --all * fix * bail_on_arg_error!() * fix * fix * remove async_extra stuff * substrate-bridge.json -> substrate-bridge-abi.json * get rid of substrate transactions hashes * get rid of ethereum transactions hashes * extracted contract bytecode to separate file * cargo fmt --all * avoid duplicate import in contracts * removed Default::default() * swapped configurations for sub2eth && eth2sub * fix compilation * do not double gas limit when submitting Substrate headers * cargo fmt --all * solidity contract removed * consts * extracted solc compilation details to separate file * removed (obsolete in future Vec<u8> justification) * fixed cli option description * fix typos * fix grumble * extracted constants * log decoded header * cargo fmt --all * comment
1 parent 521c769 commit 9993873

21 files changed

+2643
-584
lines changed

Cargo.lock

+1,295-391
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ members = [
55
"bin/node/runtime",
66
"modules/substrate",
77
"modules/ethereum",
8+
"modules/ethereum-contract/builtin",
89
"relays/ethereum",
910
"relays/substrate",
1011
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "ethereum-contract-builtin"
3+
description = "Small crate that helps Solidity contract to verify finality proof."
4+
version = "0.1.0"
5+
authors = ["Parity Technologies <admin@parity.io>"]
6+
edition = "2018"
7+
8+
[dependencies]
9+
10+
# General dependencies
11+
12+
codec = { package = "parity-scale-codec", version = "1.0.0" }
13+
finality-grandpa = { version = "0.11.2", features = ["derive-codec"] }
14+
sp-blockchain = "2.0.0-alpha.5"
15+
sp-finality-grandpa = "2.0.0-alpha.5"
16+
sp-runtime = "2.0.0-alpha.5"
17+
18+
# Runtime/chain specific dependencies
19+
20+
bridge-node-runtime = { path = "../../../bin/node/runtime" }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
2+
// This file is part of Parity Bridges Common.
3+
4+
// Parity Bridges Common is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Parity Bridges Common is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
16+
17+
use bridge_node_runtime::{BlockNumber, Hash, Header as RuntimeHeader};
18+
use codec::Decode;
19+
use sp_blockchain::Error as ClientError;
20+
21+
/// Builtin errors.
22+
#[derive(Debug)]
23+
pub enum Error {
24+
/// Failed to decode Substrate header.
25+
HeaderDecode(codec::Error),
26+
/// Failed to decode best voters set.
27+
BestVotersDecode(codec::Error),
28+
/// Failed to decode finality proof.
29+
FinalityProofDecode(codec::Error),
30+
/// Failed to verify justification.
31+
JustificationVerify(ClientError),
32+
}
33+
34+
/// Substrate header.
35+
#[derive(Debug)]
36+
pub struct Header {
37+
/// Header hash.
38+
pub hash: Hash,
39+
/// Parent header hash.
40+
pub parent_hash: Hash,
41+
/// Header number.
42+
pub number: BlockNumber,
43+
/// GRANDPA validators change signal.
44+
pub signal: Option<ValidatorsSetSignal>,
45+
}
46+
47+
/// GRANDPA validators set change signal.
48+
#[derive(Debug)]
49+
pub struct ValidatorsSetSignal {
50+
/// Signal delay.
51+
pub delay: BlockNumber,
52+
/// New validators set.
53+
pub validators: Vec<u8>,
54+
}
55+
56+
/// Parse Substrate header.
57+
pub fn parse_substrate_header(raw_header: &[u8]) -> Result<Header, Error> {
58+
RuntimeHeader::decode(&mut &raw_header[..])
59+
.map(|header| Header {
60+
hash: header.hash(),
61+
parent_hash: header.parent_hash,
62+
number: header.number,
63+
signal: None, // TODO: parse me
64+
})
65+
.map_err(Error::HeaderDecode)
66+
}
67+
68+
/// Verify GRANDPA finality proof.
69+
pub fn verify_substrate_finality_proof(
70+
_best_set_id: u64,
71+
_raw_best_voters: &[u8],
72+
_raw_best_header: &[u8],
73+
_raw_headers: &[&[u8]],
74+
_raw_finality_proof: &[u8],
75+
) -> Result<(usize, usize), Error> {
76+
Err(Error::JustificationVerify(ClientError::Msg(
77+
"Not yet implemented".into(),
78+
))) // TODO: implement me
79+
}

relays/ethereum/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ async-stream = "0.2.0"
1111
clap = { version = "2.33.0", features = ["yaml"] }
1212
codec = { package = "parity-scale-codec", version = "1.0.0" }
1313
env_logger = "0.7.0"
14+
ethabi = "12.0"
15+
ethabi-contract = "11.0"
16+
ethabi-derive = "11.0"
17+
ethereum-tx-sign = { git = "https://github.com/svyatonik/ethereum-tx-sign.git", branch = "up-ethereum-types" }
1418
futures = "0.3.1"
19+
hex = "0.4"
1520
jsonrpsee = { git = "https://github.com/paritytech/jsonrpsee.git", default-features = false, features = ["http"] }
1621
linked-hash-map = "0.5.2"
1722
log = "0.4.8"
1823
num-traits = "0.2"
24+
parity-crypto = { version = "0.6", features = ["publickey"] }
1925
parking_lot = "0.10.2"
2026
rustc-hex = "2.0.1"
2127
serde = { version = "1.0.106", features = ["derive"] }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
[
2+
{
3+
"inputs": [
4+
{
5+
"internalType": "bytes",
6+
"name": "rawInitialHeader",
7+
"type": "bytes"
8+
},
9+
{
10+
"internalType": "uint64",
11+
"name": "initialValidatorsSetId",
12+
"type": "uint64"
13+
},
14+
{
15+
"internalType": "bytes",
16+
"name": "initialValidatorsSet",
17+
"type": "bytes"
18+
}
19+
],
20+
"stateMutability": "nonpayable",
21+
"type": "constructor"
22+
},
23+
{
24+
"stateMutability": "nonpayable",
25+
"type": "fallback"
26+
},
27+
{
28+
"inputs": [],
29+
"name": "bestKnownHeader",
30+
"outputs": [
31+
{
32+
"internalType": "uint256",
33+
"name": "",
34+
"type": "uint256"
35+
},
36+
{
37+
"internalType": "bytes32",
38+
"name": "",
39+
"type": "bytes32"
40+
}
41+
],
42+
"stateMutability": "view",
43+
"type": "function"
44+
},
45+
{
46+
"inputs": [
47+
{
48+
"internalType": "uint256",
49+
"name": "finalityTargetNumber",
50+
"type": "uint256"
51+
},
52+
{
53+
"internalType": "bytes32",
54+
"name": "finalityTargetHash",
55+
"type": "bytes32"
56+
},
57+
{
58+
"internalType": "bytes",
59+
"name": "rawFinalityProof",
60+
"type": "bytes"
61+
}
62+
],
63+
"name": "importFinalityProof",
64+
"outputs": [],
65+
"stateMutability": "nonpayable",
66+
"type": "function"
67+
},
68+
{
69+
"inputs": [
70+
{
71+
"internalType": "bytes",
72+
"name": "rawHeader",
73+
"type": "bytes"
74+
}
75+
],
76+
"name": "importHeader",
77+
"outputs": [],
78+
"stateMutability": "nonpayable",
79+
"type": "function"
80+
},
81+
{
82+
"inputs": [
83+
{
84+
"internalType": "bytes32",
85+
"name": "headerHash",
86+
"type": "bytes32"
87+
}
88+
],
89+
"name": "isFinalityProofRequired",
90+
"outputs": [
91+
{
92+
"internalType": "bool",
93+
"name": "",
94+
"type": "bool"
95+
}
96+
],
97+
"stateMutability": "view",
98+
"type": "function"
99+
},
100+
{
101+
"inputs": [
102+
{
103+
"internalType": "bytes32",
104+
"name": "headerHash",
105+
"type": "bytes32"
106+
}
107+
],
108+
"name": "isKnownHeader",
109+
"outputs": [
110+
{
111+
"internalType": "bool",
112+
"name": "",
113+
"type": "bool"
114+
}
115+
],
116+
"stateMutability": "view",
117+
"type": "function"
118+
}
119+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
60806040523480156200001157600080fd5b5060405162000e6738038062000e67833981810160405260608110156200003757600080fd5b81019080805160405193929190846401000000008211156200005857600080fd5b9083019060208201858111156200006e57600080fd5b82516401000000008111828201881017156200008957600080fd5b82525081516020918201929091019080838360005b83811015620000b85781810151838201526020016200009e565b50505050905090810190601f168015620000e65780820380516001836020036101000a031916815260200191505b506040818152602083015192018051929491939192846401000000008211156200010f57600080fd5b9083019060208201858111156200012557600080fd5b82516401000000008111828201881017156200014057600080fd5b82525081516020918201929091019080838360005b838110156200016f57818101518382015260200162000155565b50505050905090810190601f1680156200019d5780820380516001836020036101000a031916815260200191505b50604052505050620001ae620003d4565b620001c2846001600160e01b03620002dc16565b805160008181556002918255604080840180516001908155825160e08101845281815260208088015181830190815293518286019081526080808a0151606085019081526001600160401b038e169185019190915260a0840188905260c084018890528951885260058352959096208251815460ff191690151517815593519284019290925593519482019490945590518051949550919390926200026f92600385019291019062000409565b506080820151600482810180546001600160401b03199081166001600160401b039485161790915560a0850151600585015560c09094015160069093019290925560038054909316908616179091558251620002d19190602085019062000409565b5050505050620004ae565b620002e6620003d4565b600080600080600060608751602089016040516020810160208101602081016020810160a08588886010600019fa6200031e57600080fd5b93519251915190519351929b50909950975090955093505082159050620003a757816001600160401b03811180156200035657600080fd5b506040519080825280601f01601f19166020018201604052801562000382576020820181803683370190505b50905087516020890160208301848184846011600019fa620003a357600080fd5b5050505b6040805160a081018252968752602087019590955293850192909252606084015250608082015292915050565b6040518060a0016040528060008019168152602001600080191681526020016000815260200160008152602001606081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106200044c57805160ff19168380011785556200047c565b828001600101855582156200047c579182015b828111156200047c5782518255916020019190600101906200045f565b506200048a9291506200048e565b5090565b620004ab91905b808211156200048a576000815560010162000495565b90565b6109a980620004be6000396000f3fe608060405234801561001057600080fd5b50600436106100575760003560e01c8063871ebe181461005c578063a98bfaad1461008d578063d96a2deb146100aa578063e7af0779146100cb578063fae71ae814610173575b600080fd5b6100796004803603602081101561007257600080fd5b5035610225565b604080519115158252519081900360200190f35b610079600480360360208110156100a357600080fd5b503561023a565b6100b2610297565b6040805192835260208301919091528051918290030190f35b610171600480360360208110156100e157600080fd5b8101906020810181356401000000008111156100fc57600080fd5b82018360208201111561010e57600080fd5b8035906020019184600183028401116401000000008311171561013057600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295506102af945050505050565b005b6101716004803603606081101561018957600080fd5b8135916020810135918101906060810160408201356401000000008111156101b057600080fd5b8201836020820111156101c257600080fd5b803590602001918460018302840111640100000000831117156101e457600080fd5b91908080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525092955061058e945050505050565b60009081526005602052604090205460ff1690565b6000818152600560205260408120805460ff16801561025e57506001548160020154115b8015610271575080600601548160020154145b80156102905750600354600482015467ffffffffffffffff9081169116145b9392505050565b60008054808252600560205260409091206002015491565b6102b76107a8565b6102c0826106ac565b805160009081526005602052604090205490915060ff1615610329576040805162461bcd60e51b815260206004820152601760248201527f48656164657220697320616c7265616479206b6e6f776e000000000000000000604482015290519081900360640190fd5b6020808201516000908152600590915260409020805460ff168015610358575060018260400151038160020154145b6103935760405162461bcd60e51b81526004018080602001828103825260268152602001806108ee6026913960400191505060405180910390fd5b6006810154158015906103ad575080600201548160060154145b156103f6578160200151600254146103f65760405162461bcd60e51b81526004018080602001828103825260318152602001806109436031913960400191505060405180910390fd5b60068101546080830151511561046b578260400151811061045e576040805162461bcd60e51b815260206004820152601960248201527f4f7665726c617070696e67207369676e616c7320666f756e6400000000000000604482015290519081900360640190fd5b5060608201516040830151015b60048201546005830154600384015467ffffffffffffffff9092169160026000196001831615610100020190911604156104ac575060208401516001909101905b6040805160e0810182526001808252602088810151818401908152898501518486019081526080808c01516060870190815267ffffffffffffffff8a169187019190915260a0860188905260c086018a90528b51600090815260058552969096208551815460ff191690151517815591519382019390935591516002830155925180519293919261054392600385019201906107dd565b50608082015160048201805467ffffffffffffffff191667ffffffffffffffff90921691909117905560a0820151600582015560c09091015160069091015550509151600055505050565b60008281526005602052604090206002015483146105dd5760405162461bcd60e51b815260040180806020018281038252602f815260200180610914602f913960400191505060405180910390fd5b60025460006105ed85858561079e565b600081815260056020526040902060028281558101546001559091505b8282146106a457506000908152600560205260409020600181015460068201546002830154919291141561069f576005818101546000908152602091909152604090206003805467ffffffffffffffff198116600167ffffffffffffffff9283168101909216178255908201805461069892600492916002610100928216159290920260001901160461085b565b50506106a4565b61060a565b505050505050565b6106b46107a8565b600080600080600060608751602089016040516020810160208101602081016020810160a08588886010600019fa6106eb57600080fd5b93519251915190519351929b50909950975090955093505082159050610771578167ffffffffffffffff8111801561072257600080fd5b506040519080825280601f01601f19166020018201604052801561074d576020820181803683370190505b50905087516020890160208301848184846011600019fa61076d57600080fd5b5050505b6040805160a081018252968752602087019590955293850192909252606084015250608082015292915050565b6002549392505050565b6040518060a0016040528060008019168152602001600080191681526020016000815260200160008152602001606081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061081e57805160ff191683800117855561084b565b8280016001018555821561084b579182015b8281111561084b578251825591602001919060010190610830565b506108579291506108d0565b5090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10610894578054855561084b565b8280016001018555821561084b57600052602060002091601f016020900482015b8281111561084b5782548255916001019190600101906108b5565b6108ea91905b8082111561085757600081556001016108d6565b9056fe4d697373696e6720706172656e74206865616465722066726f6d207468652073746f726167654d697373696e672066696e616c69747920746172676574206865616465722066726f6d207468652073746f726167654d697373696e672072657175697265642066696e616c6974792070726f6f6620666f7220706172656e7420686561646572a26469706673582212206cc35a10288e85e37e14250b2d4af37dc4fbcd59635965d9ac0421cb8db1f66c64736f6c63430006060033
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Last Change Date: 2020-04-28
2+
Solc version: 0.6.6+commit.6c089d02
3+
Source hash (keccak256): 0xdc46aff04e37129265223e507d17f1407a70cb1ecea3230e1eaa77a17586724d
4+
Source gist: https://gist.github.com/svyatonik/876b388f9507a8de242cb2db9547c4f0
5+
Compiler flags used (command to produce the file): `docker run -i ethereum/solc:0.6.6 --optimize --bin - < substrate-bridge.sol`

relays/ethereum/src/cli.yml

+79
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,82 @@ subcommands:
4343
long: sub-signer-password
4444
value_name: SUB_SIGNER_PASSWORD
4545
help: The password for the SURI of secret key to use when transactions are submitted to the Substrate node.
46+
- sub-to-eth:
47+
about: Synchronize headers from Substrate node to Ethereum node.
48+
args:
49+
- eth-host:
50+
long: eth-host
51+
value_name: ETH_HOST
52+
help: Connect to Ethereum node at given host.
53+
takes_value: true
54+
- eth-port:
55+
long: eth-port
56+
value_name: ETH_PORT
57+
help: Connect to Ethereum node at given port.
58+
takes_value: true
59+
- eth-contract:
60+
long: eth-contract
61+
value_name: ETH_CONTRACT
62+
help: Address of deployed bridge contract.
63+
takes_value: true
64+
- eth-signer:
65+
long: eth-signer
66+
value_name: ETH_SIGNER
67+
help: Hex-encoded secret to use when transactions are submitted to the Ethereum node.
68+
- sub-host:
69+
long: sub-host
70+
value_name: SUB_HOST
71+
help: Connect to Substrate node at given host.
72+
takes_value: true
73+
- sub-port:
74+
long: sub-port
75+
value_name: SUB_PORT
76+
help: Connect to Substrate node at given port.
77+
takes_value: true
78+
- eth-deploy-contract:
79+
about: Deploy Bridge contract on Ethereum node.
80+
args:
81+
- eth-host:
82+
long: eth-host
83+
value_name: ETH_HOST
84+
help: Connect to Ethereum node at given host.
85+
takes_value: true
86+
- eth-port:
87+
long: eth-port
88+
value_name: ETH_PORT
89+
help: Connect to Ethereum node at given port.
90+
takes_value: true
91+
- eth-signer:
92+
long: eth-signer
93+
value_name: ETH_SIGNER
94+
help: Hex-encoded secret to use when transactions are submitted to the Ethereum node.
95+
- eth-contract-code:
96+
long: eth-contract-code
97+
value_name: ETH_CONTRACT_CODE
98+
help: Bytecode of bridge contract.
99+
takes_value: true
100+
- sub-host:
101+
long: sub-host
102+
value_name: SUB_HOST
103+
help: Connect to Substrate node at given host.
104+
takes_value: true
105+
- sub-port:
106+
long: sub-port
107+
value_name: SUB_PORT
108+
help: Connect to Substrate node at given port.
109+
takes_value: true
110+
- sub-authorities-set-id:
111+
long: sub-authorities-set-id
112+
value_name: SUB_AUTHORITIES_SET_ID
113+
help: ID of initial GRANDPA authorities set.
114+
takes_value: true
115+
- sub-authorities-set:
116+
long: sub-authorities-set
117+
value_name: SUB_AUTHORITIES_SET
118+
help: Encoded initial GRANDPA authorities set.
119+
takes_value: true
120+
- sub-initial-header:
121+
long: sub-initial-header
122+
value_name: SUB_INITIAL_HEADER
123+
help: Encoded initial Substrate header.
124+
takes_value: true

0 commit comments

Comments
 (0)