Fix 16bit func_id (#11985)

This commit is contained in:
Kevin Wang
2022-08-08 17:46:57 +08:00
committed by GitHub
parent 20c49b20a7
commit 266fa8516a
2 changed files with 17 additions and 16 deletions
@@ -38,7 +38,7 @@
//! //!
//! However, only extensions implementing [`RegisteredChainExtension`] can be put into a tuple. //! However, only extensions implementing [`RegisteredChainExtension`] can be put into a tuple.
//! This is because the [`RegisteredChainExtension::ID`] is used to decide which of those extensions //! This is because the [`RegisteredChainExtension::ID`] is used to decide which of those extensions
//! should should be used when the contract calls a chain extensions. Extensions which are generally //! should be used when the contract calls a chain extensions. Extensions which are generally
//! useful should claim their `ID` with [the registry](https://github.com/paritytech/chainextension-registry) //! useful should claim their `ID` with [the registry](https://github.com/paritytech/chainextension-registry)
//! so that no collisions with other vendors will occur. //! so that no collisions with other vendors will occur.
//! //!
@@ -215,7 +215,7 @@ where
/// It returns the two least significant bytes of the `id` passed by a contract as the other /// It returns the two least significant bytes of the `id` passed by a contract as the other
/// two bytes represent the chain extension itself (the code which is calling this function). /// two bytes represent the chain extension itself (the code which is calling this function).
pub fn func_id(&self) -> u16 { pub fn func_id(&self) -> u16 {
(self.inner.id & 0x00FF) as u16 (self.inner.id & 0x0000FFFF) as u16
} }
/// The chain extension id within the `id` passed by a contract. /// The chain extension id within the `id` passed by a contract.
+15 -14
View File
@@ -158,14 +158,14 @@ impl ChainExtension<Test> for TestExtension {
let func_id = env.func_id(); let func_id = env.func_id();
let id = env.ext_id() as u32 | func_id as u32; let id = env.ext_id() as u32 | func_id as u32;
match func_id { match func_id {
0 => { 0x8000 => {
let mut env = env.buf_in_buf_out(); let mut env = env.buf_in_buf_out();
let input = env.read(8)?; let input = env.read(8)?;
env.write(&input, false, None)?; env.write(&input, false, None)?;
TEST_EXTENSION.with(|e| e.borrow_mut().last_seen_buffer = input); TEST_EXTENSION.with(|e| e.borrow_mut().last_seen_buffer = input);
Ok(RetVal::Converging(id)) Ok(RetVal::Converging(id))
}, },
1 => { 0x8001 => {
let env = env.only_in(); let env = env.only_in();
TEST_EXTENSION.with(|e| { TEST_EXTENSION.with(|e| {
e.borrow_mut().last_seen_inputs = e.borrow_mut().last_seen_inputs =
@@ -173,13 +173,13 @@ impl ChainExtension<Test> for TestExtension {
}); });
Ok(RetVal::Converging(id)) Ok(RetVal::Converging(id))
}, },
2 => { 0x8002 => {
let mut env = env.buf_in_buf_out(); let mut env = env.buf_in_buf_out();
let weight = env.read(5)?[4].into(); let weight = env.read(5)?[4].into();
env.charge_weight(weight)?; env.charge_weight(weight)?;
Ok(RetVal::Converging(id)) Ok(RetVal::Converging(id))
}, },
3 => Ok(RetVal::Diverging { flags: ReturnFlags::REVERT, data: vec![42, 99] }), 0x8003 => Ok(RetVal::Diverging { flags: ReturnFlags::REVERT, data: vec![42, 99] }),
_ => { _ => {
panic!("Passed unknown id to test chain extension: {}", func_id); panic!("Passed unknown id to test chain extension: {}", func_id);
}, },
@@ -1646,21 +1646,22 @@ fn chain_extension_works() {
),); ),);
let addr = Contracts::contract_address(&ALICE, &hash, &[]); let addr = Contracts::contract_address(&ALICE, &hash, &[]);
// 0 = read input buffer and pass it through as output // 0x8000 = read input buffer and pass it through as output
let input: Vec<u8> = ExtensionInput { extension_id: 0, func_id: 0, extra: &[99] }.into(); let input: Vec<u8> =
ExtensionInput { extension_id: 0, func_id: 0x8000, extra: &[99] }.into();
let result = let result =
Contracts::bare_call(ALICE, addr.clone(), 0, GAS_LIMIT, None, input.clone(), false); Contracts::bare_call(ALICE, addr.clone(), 0, GAS_LIMIT, None, input.clone(), false);
assert_eq!(TestExtension::last_seen_buffer(), input); assert_eq!(TestExtension::last_seen_buffer(), input);
assert_eq!(result.result.unwrap().data, Bytes(input)); assert_eq!(result.result.unwrap().data, Bytes(input));
// 1 = treat inputs as integer primitives and store the supplied integers // 0x8001 = treat inputs as integer primitives and store the supplied integers
Contracts::bare_call( Contracts::bare_call(
ALICE, ALICE,
addr.clone(), addr.clone(),
0, 0,
GAS_LIMIT, GAS_LIMIT,
None, None,
ExtensionInput { extension_id: 0, func_id: 1, extra: &[] }.into(), ExtensionInput { extension_id: 0, func_id: 0x8001, extra: &[] }.into(),
false, false,
) )
.result .result
@@ -1668,14 +1669,14 @@ fn chain_extension_works() {
// those values passed in the fixture // those values passed in the fixture
assert_eq!(TestExtension::last_seen_inputs(), (4, 4, 16, 12)); assert_eq!(TestExtension::last_seen_inputs(), (4, 4, 16, 12));
// 2 = charge some extra weight (amount supplied in the fifth byte) // 0x8002 = charge some extra weight (amount supplied in the fifth byte)
let result = Contracts::bare_call( let result = Contracts::bare_call(
ALICE, ALICE,
addr.clone(), addr.clone(),
0, 0,
GAS_LIMIT, GAS_LIMIT,
None, None,
ExtensionInput { extension_id: 0, func_id: 2, extra: &[0] }.into(), ExtensionInput { extension_id: 0, func_id: 0x8002, extra: &[0] }.into(),
false, false,
); );
assert_ok!(result.result); assert_ok!(result.result);
@@ -1686,7 +1687,7 @@ fn chain_extension_works() {
0, 0,
GAS_LIMIT, GAS_LIMIT,
None, None,
ExtensionInput { extension_id: 0, func_id: 2, extra: &[42] }.into(), ExtensionInput { extension_id: 0, func_id: 0x8002, extra: &[42] }.into(),
false, false,
); );
assert_ok!(result.result); assert_ok!(result.result);
@@ -1697,20 +1698,20 @@ fn chain_extension_works() {
0, 0,
GAS_LIMIT, GAS_LIMIT,
None, None,
ExtensionInput { extension_id: 0, func_id: 2, extra: &[95] }.into(), ExtensionInput { extension_id: 0, func_id: 0x8002, extra: &[95] }.into(),
false, false,
); );
assert_ok!(result.result); assert_ok!(result.result);
assert_eq!(result.gas_consumed, gas_consumed + 95); assert_eq!(result.gas_consumed, gas_consumed + 95);
// 3 = diverging chain extension call that sets flags to 0x1 and returns a fixed buffer // 0x8003 = diverging chain extension call that sets flags to 0x1 and returns a fixed buffer
let result = Contracts::bare_call( let result = Contracts::bare_call(
ALICE, ALICE,
addr.clone(), addr.clone(),
0, 0,
GAS_LIMIT, GAS_LIMIT,
None, None,
ExtensionInput { extension_id: 0, func_id: 3, extra: &[] }.into(), ExtensionInput { extension_id: 0, func_id: 0x8003, extra: &[] }.into(),
false, false,
) )
.result .result