-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin' into eshaben/moonbeam-vs-eth
- Loading branch information
Showing
25 changed files
with
554 additions
and
62 deletions.
There are no files selected for viewing
This file contains 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
74 changes: 74 additions & 0 deletions
74
...s/code/builders/pallets-precompiles/precompiles/relay-data-verifier/RelayDataVerifier.sol
This file contains 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,74 @@ | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
pragma solidity >=0.8.3; | ||
|
||
/// @dev The RelayDataVerifier contract's address. | ||
address constant RELAY_DATA_VERIFIER_ADDRESS = 0x0000000000000000000000000000000000000819; | ||
|
||
/// @dev The RelayDataVerifier contract's instance. | ||
RelayDataVerifier constant RELAY_DATA_VERIFIER_CONTRACT = RelayDataVerifier( | ||
RELAY_DATA_VERIFIER_ADDRESS | ||
); | ||
|
||
/// @author The Moonbeam Team | ||
/// @title Relay Proof Verifier Interface | ||
/// @dev The interface that Solidity contracts use to interact with the Relay Proof Verifier | ||
/// precompile. | ||
/// A typical workflow to verify relay chain data is the following: | ||
/// 1. Moonbeam RPC Call: Call `latestRelayBlockNumber` function to get the latest relay | ||
/// block number tracked by the chain in `pallet-storage-root`. | ||
/// 2. Relay RPC Call: Call `chain_getBlockHash(blockNumber)` RPC method to get the relay block hash | ||
/// for the block number obtained in step 1. | ||
/// 3. Relay RPC Call: Call `state_getReadProof(keys, at)` RPC method where `at` | ||
/// is the relay block hash obtained in step 2 to get the 'ReadProof` of the entries. | ||
/// 4. Moonbeam RPC Call: Submit an ethereum transaction (directly or through a SC) to call the | ||
/// `verifyEntry` or `verifyEntries` function to verify the data against the relay block | ||
/// number. The call data contain the relay block number obtained in step 1, and the read | ||
/// proof generated in step 3, along with the key/s to verify. | ||
/// @custom:address 0x0000000000000000000000000000000000000819 | ||
interface RelayDataVerifier { | ||
/// @dev ReadProof struct returned by the `state_getReadProof` RPC method. | ||
struct ReadProof { | ||
// The block hash against which the proof is generated | ||
bytes32 at; | ||
/// The storage proof | ||
bytes[] proof; | ||
} | ||
|
||
/// @dev Verifies a storage entry in the Relay Chain using a relay block number and a storage | ||
/// proof. This function takes a relay block number, a storage proof, and the key of the storage | ||
/// entry to verify. It returns the value associated with the key if the verification is | ||
/// successful. | ||
/// @custom:selector 27001faa | ||
/// @param relayBlockNumber The relay block number against which the entry is being verified. | ||
/// @param readProof The storage proof used to verify the entry. | ||
/// @param key The key of the storage entry to verify. | ||
/// @return value The value associated with the key, returned as a bytes array. | ||
function verifyEntry( | ||
uint32 relayBlockNumber, | ||
ReadProof calldata readProof, | ||
bytes calldata key | ||
) external returns (bytes memory value); | ||
|
||
/// @dev Verifies a set of entries in the Relay Chain and returns the corresponding values. | ||
/// This function takes a relay block number, a storage proof, and an array of keys for the | ||
/// storage entries to verify. It returns an array of values associated with the keys, in the | ||
/// same order as the keys. | ||
/// @custom:selector 2da33a45 | ||
/// @param relayBlockNumber The relay block number for which the data is being verified. | ||
/// @param readProof The storage proof used to verify the data. | ||
/// @param keys The keys of the storage entries to verify. | ||
/// @return values The values associated with the keys, returned in the same order as the keys. | ||
function verifyEntries( | ||
uint32 relayBlockNumber, | ||
ReadProof calldata readProof, | ||
bytes[] calldata keys | ||
) external returns (bytes[] memory values); | ||
|
||
/// @dev Returns the latest relay block number that has a storage root stored on-chain. | ||
/// @custom:selector aed36869 | ||
/// @return relayBlockNumber the lastest relay block number | ||
function latestRelayBlockNumber() | ||
external | ||
view | ||
returns (uint32 relayBlockNumber); | ||
} |
97 changes: 97 additions & 0 deletions
97
.snippets/code/builders/pallets-precompiles/precompiles/relay-data-verifier/abi.js
This file contains 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,97 @@ | ||
[ | ||
{ | ||
inputs: [], | ||
name: 'latestRelayBlockNumber', | ||
outputs: [ | ||
{ | ||
internalType: 'uint32', | ||
name: 'relayBlockNumber', | ||
type: 'uint32', | ||
}, | ||
], | ||
stateMutability: 'view', | ||
type: 'function', | ||
}, | ||
{ | ||
inputs: [ | ||
{ | ||
internalType: 'uint32', | ||
name: 'relayBlockNumber', | ||
type: 'uint32', | ||
}, | ||
{ | ||
components: [ | ||
{ | ||
internalType: 'bytes32', | ||
name: 'at', | ||
type: 'bytes32', | ||
}, | ||
{ | ||
internalType: 'bytes[]', | ||
name: 'proof', | ||
type: 'bytes[]', | ||
}, | ||
], | ||
internalType: 'struct RelayDataVerifier.ReadProof', | ||
name: 'readProof', | ||
type: 'tuple', | ||
}, | ||
{ | ||
internalType: 'bytes[]', | ||
name: 'keys', | ||
type: 'bytes[]', | ||
}, | ||
], | ||
name: 'verifyEntries', | ||
outputs: [ | ||
{ | ||
internalType: 'bytes[]', | ||
name: 'values', | ||
type: 'bytes[]', | ||
}, | ||
], | ||
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
{ | ||
inputs: [ | ||
{ | ||
internalType: 'uint32', | ||
name: 'relayBlockNumber', | ||
type: 'uint32', | ||
}, | ||
{ | ||
components: [ | ||
{ | ||
internalType: 'bytes32', | ||
name: 'at', | ||
type: 'bytes32', | ||
}, | ||
{ | ||
internalType: 'bytes[]', | ||
name: 'proof', | ||
type: 'bytes[]', | ||
}, | ||
], | ||
internalType: 'struct RelayDataVerifier.ReadProof', | ||
name: 'readProof', | ||
type: 'tuple', | ||
}, | ||
{ | ||
internalType: 'bytes', | ||
name: 'key', | ||
type: 'bytes', | ||
}, | ||
], | ||
name: 'verifyEntry', | ||
outputs: [ | ||
{ | ||
internalType: 'bytes', | ||
name: 'value', | ||
type: 'bytes', | ||
}, | ||
], | ||
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
]; |
41 changes: 41 additions & 0 deletions
41
...uilders/pallets-precompiles/precompiles/relay-data-verifier/ethers-relay-data-verifier.js
This file contains 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 @@ | ||
// For reading local ABI file | ||
import * as fs from 'fs'; | ||
// Import Ethers library, to interact with Moonbeam networks | ||
import { ethers } from 'ethers'; | ||
// Import Polkadot library, to interact with relay chain | ||
import { ApiPromise, WsProvider } from '@polkadot/api'; | ||
|
||
const abi = JSON.parse(fs.readFileSync('./RelayChainDataVerifierABI.json')); | ||
const privateKey = 'INSERT_PRIVATE_KEY'; | ||
const precompileAddress = '0x0000000000000000000000000000000000000819'; | ||
const moonbeamURL = 'https://rpc.api.moonbase.moonbeam.network'; | ||
const relayURL = 'wss://frag-moonbase-relay-rpc-ws.g.moonbase.moonbeam.network'; | ||
|
||
// Create Ethers provider and signer | ||
const provider = new ethers.JsonRpcProvider(moonbeamURL); | ||
const signer = new ethers.Wallet(privateKey, provider); | ||
const precompileContract = new ethers.Contract(precompileAddress, abi, signer); | ||
|
||
async function run() { | ||
// Create provider for relay chain | ||
const wsProvider = new WsProvider(relayURL); | ||
const api = await ApiPromise.create({ provider: wsProvider }); | ||
|
||
// Get the storage key for a random account on relay chain | ||
const key = api.query.system.account.key( | ||
'5CBATpb3yvEM4mhX9Dw3tyuqiWKhq9YBG6ugSbodRUSbodoU' | ||
); | ||
// Find the latest available relay chain block number from Moonbeam | ||
const blockNum = await precompileContract.latestRelayBlockNumber(); | ||
|
||
// Get the block hash and storage proof from relay chain | ||
const blockHash = await api.rpc.chain.getBlockHash(blockNum); | ||
const proof = await api.rpc.state.getReadProof([key], blockHash); | ||
|
||
// This tx will be rejected if the verification failed | ||
const receipt = await precompileContract.verifyEntry(blockNum, proof, key); | ||
await receipt.wait(); | ||
console.log(receipt.hash); | ||
} | ||
|
||
await run(); |
59 changes: 59 additions & 0 deletions
59
...uilders/pallets-precompiles/precompiles/relay-data-verifier/web3js-relay-data-verifier.js
This file contains 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,59 @@ | ||
// For reading local ABI file | ||
import * as fs from 'fs'; | ||
// Import web3js library, to interact with Moonbeam networks | ||
import { Web3 } from 'web3'; | ||
// Import Polkadot library, to interact with relay chain | ||
import { ApiPromise, WsProvider } from '@polkadot/api'; | ||
|
||
const abi = JSON.parse(fs.readFileSync('./RelayChainDataVerifierABI.json')); | ||
const privateKey = 'INSERT_PRIVATE_KEY'; | ||
const precompileAddress = '0x0000000000000000000000000000000000000819'; | ||
const moonbeamURL = 'https://rpc.api.moonbase.moonbeam.network'; | ||
const relayURL = 'wss://frag-moonbase-relay-rpc-ws.g.moonbase.moonbeam.network'; | ||
|
||
// Create Web3js provider and signer | ||
const web3 = new Web3(moonbeamURL); | ||
const precompileContract = new web3.eth.Contract(abi, precompileAddress); | ||
|
||
const account = web3.eth.accounts.privateKeyToAccount(privateKey); | ||
|
||
async function run() { | ||
// Create provider for relay chain | ||
const wsProvider = new WsProvider(relayURL); | ||
const api = await ApiPromise.create({ provider: wsProvider }); | ||
|
||
// Get the storage key for a random account on relay chain | ||
const key = api.query.system.account.key( | ||
'5CBATpb3yvEM4mhX9Dw3tyuqiWKhq9YBG6ugSbodRUSbodoU' | ||
); | ||
// Find the latest available relay chain block number from Moonbeam | ||
const blockNum = await precompileContract.methods | ||
.latestRelayBlockNumber() | ||
.call(); | ||
|
||
// Get the block hash and storage proof from relay chain | ||
const blockHash = await api.rpc.chain.getBlockHash(blockNum); | ||
const proof = await api.rpc.state.getReadProof([key], blockHash); | ||
|
||
const callObject = { | ||
to: precompileAddress, | ||
data: precompileContract.methods | ||
.verifyEntry(blockNum, proof, key) | ||
.encodeABI(), | ||
gas: await precompileContract.methods | ||
.verifyEntry(blockNum, proof, key) | ||
.estimateGas(), | ||
gasPrice: await web3.eth.getGasPrice(), | ||
nonce: await web3.eth.getTransactionCount(account.address), | ||
}; | ||
|
||
// This tx will be rejected if the verification failed | ||
const tx = await web3.eth.accounts.signTransaction( | ||
callObject, | ||
account.privateKey | ||
); | ||
const receipt = await web3.eth.sendSignedTransaction(tx.rawTransaction); | ||
console.log(receipt.transactionHash); | ||
} | ||
|
||
await run(); |
50 changes: 50 additions & 0 deletions
50
...uilders/pallets-precompiles/precompiles/relay-data-verifier/web3py-relay-data-verifier.py
This file contains 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,50 @@ | ||
# Import packages | ||
from substrateinterface import SubstrateInterface | ||
from web3 import Web3 | ||
from eth_account import Account | ||
|
||
# Initialize variables | ||
abi = INSERT_ABI | ||
|
||
privateKey = "INSERT_PRIVATE_KEY" | ||
precompileAddress = "0x0000000000000000000000000000000000000819" | ||
moonbeamURL = "https://rpc.api.moonbase.moonbeam.network" | ||
relayURL = "wss://frag-moonbase-relay-rpc-ws.g.moonbase.moonbeam.network" | ||
|
||
# Create provider for Moonbeam network | ||
web3 = Web3(Web3.HTTPProvider(moonbeamURL)) | ||
account = Account.from_key(privateKey) | ||
precompileContract = web3.eth.contract(address=precompileAddress, abi=abi) | ||
|
||
# Create provider for relay chain | ||
substrate = SubstrateInterface(url=relayURL) | ||
|
||
# Get storage key | ||
key = substrate.generate_storage_hash( | ||
storage_module="System", | ||
storage_function="Account", | ||
params=["5CBATpb3yvEM4mhX9Dw3tyuqiWKhq9YBG6ugSbodRUSbodoU"], | ||
) | ||
|
||
# Find the latest available relay chain block number from Moonbeam | ||
blockNum = precompileContract.functions.latestRelayBlockNumber().call() | ||
|
||
# Get the block hash from relay chain | ||
blockHash = substrate.get_block_hash(blockNum) | ||
|
||
# Get the storage proof from relay chain | ||
response = substrate.rpc_request("state_getReadProof", [[key], blockHash]) | ||
proof = response["result"] | ||
|
||
# Call smart contract | ||
tx = precompileContract.functions.verifyEntry(blockNum, proof, key).build_transaction( | ||
{ | ||
"from": Web3.to_checksum_address(account.address), | ||
"nonce": web3.eth.get_transaction_count( | ||
Web3.to_checksum_address(account.address) | ||
), | ||
} | ||
) | ||
tx_create = web3.eth.account.sign_transaction(tx, privateKey) | ||
tx_hash = web3.eth.send_raw_transaction(tx_create.rawTransaction) | ||
tx_receipt = web3.eth.wait_for_transaction_receipt(tx_hash) |
Oops, something went wrong.