-
Notifications
You must be signed in to change notification settings - Fork 209
Expose all extrinsic and storage to EVM #1639
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
Merged
+181
−5
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
b041691
init solution
open-junius 4a6d525
call dispatch
open-junius ae85913
Add missing trait bounds
keithtensor fb9ca43
evm test part
open-junius da050d8
test is done
open-junius 33a0e80
Merge branch 'devnet-ready' into any-call-any-query
open-junius 8270040
clean up code
open-junius 1f9fbb1
clean up code
open-junius 919c9e0
remove contract definition
open-junius b855e7d
format code
open-junius 00d7ca7
cargo clippy
open-junius 7094c4f
not include the descriptos
open-junius 6b53c5f
revert lock file
open-junius 227220b
revert unnecessary change
open-junius a615482
revert unnecessary change
open-junius dd8d8ab
cargo fix
open-junius cf56ee2
use hash function to get the key
open-junius 7904b10
add generate call data process
open-junius 9948aa6
merge with taraget branch
open-junius e1cf60a
Merge branch 'devnet-ready' into any-call-any-query
open-junius 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,75 @@ | ||
import * as assert from "assert"; | ||
import { getAliceSigner, getDevnetApi } from "../src/substrate" | ||
import { generateRandomEthersWallet, getPublicClient } from "../src/utils"; | ||
import { IDISPATCH_ADDRESS, ISTORAGE_QUERY_ADDRESS, ETH_LOCAL_URL } from "../src/config"; | ||
import { devnet, MultiAddress } from "@polkadot-api/descriptors" | ||
import { hexToNumber, PublicClient } from "viem"; | ||
import { PolkadotSigner, TypedApi } from "polkadot-api"; | ||
import { convertPublicKeyToSs58 } from "../src/address-utils" | ||
import { forceSetBalanceToEthAddress, setMaxChildkeyTake } from "../src/subtensor"; | ||
import { xxhashAsU8a } from '@polkadot/util-crypto'; | ||
import { u8aToHex } from '@polkadot/util'; | ||
|
||
describe("Test the dispatch precompile", () => { | ||
let publicClient: PublicClient; | ||
const wallet1 = generateRandomEthersWallet(); | ||
let api: TypedApi<typeof devnet> | ||
let alice: PolkadotSigner; | ||
|
||
before(async () => { | ||
publicClient = await getPublicClient(ETH_LOCAL_URL) | ||
api = await getDevnetApi() | ||
alice = await getAliceSigner() | ||
await forceSetBalanceToEthAddress(api, wallet1.address) | ||
}) | ||
|
||
it("Dispatch transfer call via precompile contract works correctly", async () => { | ||
// call for transfer 1 token to alice | ||
const transferAmount = BigInt(1000000000); | ||
|
||
const unsignedTx = api.tx.Balances.transfer_keep_alive({ | ||
dest: MultiAddress.Id(convertPublicKeyToSs58(alice.publicKey)), | ||
value: transferAmount, | ||
}); | ||
const encodedCallDataBytes = await unsignedTx.getEncodedData(); | ||
|
||
// encoded call should be 0x050300d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d02286bee | ||
const transferCall = encodedCallDataBytes.asHex() | ||
|
||
const aliceBalance = (await api.query.System.Account.getValue( convertPublicKeyToSs58(alice.publicKey))).data.free | ||
const txResponse = await wallet1.sendTransaction({ | ||
to: IDISPATCH_ADDRESS, | ||
data: transferCall, | ||
}) | ||
await txResponse.wait() | ||
|
||
const aliceBalanceAfterTransfer = (await api.query.System.Account.getValue( convertPublicKeyToSs58(alice.publicKey))).data.free | ||
|
||
assert.equal(aliceBalance + transferAmount, aliceBalanceAfterTransfer) | ||
}) | ||
|
||
|
||
it("Storage query call via precompile contract works correctly", async () => { | ||
const palletPrefixBytes = xxhashAsU8a("SubtensorModule", 128); | ||
const storageItemPrefixBytes = xxhashAsU8a("MaxChildkeyTake", 128); | ||
const fullStorageKeyBytes = new Uint8Array([...palletPrefixBytes, ...storageItemPrefixBytes]); | ||
// 0x658faa385070e074c85bf6b568cf0555dba018859cab7e989f77669457b394be | ||
// key for max child key take | ||
const fullStorageKeyHex = u8aToHex(fullStorageKeyBytes); | ||
|
||
let maxChildkeyTake = 257; | ||
await setMaxChildkeyTake(api, maxChildkeyTake) | ||
|
||
api.query.SubtensorModule.MaxChildkeyTake.getValue(); | ||
const rawCallResponse = await publicClient.call({ | ||
to: ISTORAGE_QUERY_ADDRESS, | ||
data: fullStorageKeyHex, | ||
}) | ||
const rawResultData = rawCallResponse.data; | ||
if (rawResultData === undefined) { | ||
throw new Error("rawResultData is undefined"); | ||
} | ||
let value = hexToNumber(rawResultData); | ||
assert.equal(value, maxChildkeyTake, "value should be 257") | ||
}) | ||
}); |
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,56 @@ | ||
use core::marker::PhantomData; | ||
|
||
use frame_support::dispatch::{GetDispatchInfo, PostDispatchInfo}; | ||
use pallet_evm::{ExitSucceed, Precompile, PrecompileHandle, PrecompileOutput}; | ||
use sp_runtime::traits::{Dispatchable, StaticLookup}; | ||
use sp_std::vec::Vec; | ||
|
||
use crate::PrecompileExt; | ||
|
||
pub(crate) struct StorageQueryPrecompile<R>(PhantomData<R>); | ||
|
||
impl<R> PrecompileExt<R::AccountId> for StorageQueryPrecompile<R> | ||
where | ||
R: frame_system::Config + pallet_subtensor::Config + pallet_evm::Config, | ||
R::AccountId: From<[u8; 32]>, | ||
<R as frame_system::Config>::RuntimeCall: | ||
GetDispatchInfo + Dispatchable<PostInfo = PostDispatchInfo>, | ||
<R as frame_system::Config>::RuntimeCall: From<pallet_subtensor::Call<R>> | ||
+ GetDispatchInfo | ||
+ Dispatchable<PostInfo = PostDispatchInfo>, | ||
<<R as frame_system::Config>::Lookup as StaticLookup>::Source: From<R::AccountId>, | ||
{ | ||
const INDEX: u64 = 2055; | ||
} | ||
|
||
impl<R> Precompile for StorageQueryPrecompile<R> | ||
where | ||
R: frame_system::Config + pallet_subtensor::Config + pallet_evm::Config, | ||
R::AccountId: From<[u8; 32]>, | ||
<R as frame_system::Config>::RuntimeCall: | ||
GetDispatchInfo + Dispatchable<PostInfo = PostDispatchInfo>, | ||
<R as frame_system::Config>::RuntimeCall: From<pallet_subtensor::Call<R>> | ||
+ GetDispatchInfo | ||
+ Dispatchable<PostInfo = PostDispatchInfo>, | ||
<<R as frame_system::Config>::Lookup as StaticLookup>::Source: From<R::AccountId>, | ||
{ | ||
fn execute(handle: &mut impl PrecompileHandle) -> fp_evm::PrecompileResult { | ||
let input = handle.input(); | ||
let data = sp_io::storage::get(input); | ||
|
||
match data { | ||
Some(value) => { | ||
let result = value.to_vec(); | ||
|
||
Ok(PrecompileOutput { | ||
exit_status: ExitSucceed::Returned, | ||
output: result, | ||
}) | ||
} | ||
None => Ok(PrecompileOutput { | ||
exit_status: ExitSucceed::Returned, | ||
output: Vec::new(), | ||
}), | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this line needed given value is not stored in a variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just check the value can be got after setMaxChildkeyTake, without exception. maybe it is not necessary, will consider to remove it in the future.