1818
1919#![ cfg_attr( not( feature = "std" ) , no_std) ]
2020
21+ use astar_primitives:: xvm:: { CallError , Context , VmId , XvmCall } ;
2122use frame_support:: dispatch:: Encode ;
2223use pallet_contracts:: {
2324 chain_extension:: { ChainExtension , Environment , Ext , InitState , RetVal } ,
2425 Origin ,
2526} ;
26- use pallet_xvm:: XvmContext ;
2727use sp_runtime:: DispatchError ;
2828use sp_std:: marker:: PhantomData ;
2929use xvm_chain_extension_types:: { XvmCallArgs , XvmExecutionResult } ;
3030
3131enum XvmFuncId {
32- XvmCall ,
32+ Call ,
3333 // TODO: expand with other calls too
3434}
3535
@@ -38,7 +38,7 @@ impl TryFrom<u16> for XvmFuncId {
3838
3939 fn try_from ( value : u16 ) -> Result < Self , Self :: Error > {
4040 match value {
41- 1 => Ok ( XvmFuncId :: XvmCall ) ,
41+ 1 => Ok ( XvmFuncId :: Call ) ,
4242 _ => Err ( DispatchError :: Other (
4343 "Unsupported func id in Xvm chain extension" ,
4444 ) ) ,
@@ -47,17 +47,18 @@ impl TryFrom<u16> for XvmFuncId {
4747}
4848
4949/// XVM chain extension.
50- pub struct XvmExtension < T > ( PhantomData < T > ) ;
50+ pub struct XvmExtension < T , XC > ( PhantomData < ( T , XC ) > ) ;
5151
52- impl < T > Default for XvmExtension < T > {
52+ impl < T , XC > Default for XvmExtension < T , XC > {
5353 fn default ( ) -> Self {
5454 XvmExtension ( PhantomData )
5555 }
5656}
5757
58- impl < T > ChainExtension < T > for XvmExtension < T >
58+ impl < T , XC > ChainExtension < T > for XvmExtension < T , XC >
5959where
60- T : pallet_contracts:: Config + pallet_xvm:: Config ,
60+ T : pallet_contracts:: Config ,
61+ XC : XvmCall < T :: AccountId > ,
6162{
6263 fn call < E : Ext > ( & mut self , env : Environment < E , InitState > ) -> Result < RetVal , DispatchError >
6364 where
@@ -67,13 +68,13 @@ where
6768 let mut env = env. buf_in_buf_out ( ) ;
6869
6970 match func_id {
70- XvmFuncId :: XvmCall => {
71+ XvmFuncId :: Call => {
7172 // We need to immediately charge for the worst case scenario. Gas equals Weight in pallet-contracts context.
72- let remaining_weight = env. ext ( ) . gas_meter ( ) . gas_left ( ) ;
73+ let weight_limit = env. ext ( ) . gas_meter ( ) . gas_left ( ) ;
74+ // TODO: track proof size in align fees ticket
7375 // We don't track used proof size, so we can't refund after.
7476 // So we will charge a 32KB dummy value as a temporary replacement.
75- let charged_weight =
76- env. charge_weight ( remaining_weight. set_proof_size ( 32 * 1024 ) ) ?;
77+ let charged_weight = env. charge_weight ( weight_limit. set_proof_size ( 32 * 1024 ) ) ?;
7778
7879 let caller = match env. ext ( ) . caller ( ) . clone ( ) {
7980 Origin :: Signed ( address) => address,
@@ -82,47 +83,60 @@ where
8283 target: "xvm-extension::xvm_call" ,
8384 "root origin not supported"
8485 ) ;
85- // TODO: expand XvmErrors with BadOrigin
86- return Ok ( RetVal :: Converging ( XvmExecutionResult :: UnknownError as u32 ) ) ;
86+ return Ok ( RetVal :: Converging (
87+ XvmExecutionResult :: from ( CallError :: BadOrigin ) . into ( ) ,
88+ ) ) ;
8789 }
8890 } ;
8991
9092 let XvmCallArgs { vm_id, to, input } = env. read_as_unbounded ( env. in_len ( ) ) ?;
9193
9294 let _origin_address = env. ext ( ) . address ( ) . clone ( ) ;
9395 let _value = env. ext ( ) . value_transferred ( ) ;
94- let xvm_context = XvmContext {
95- id : vm_id,
96- max_weight : remaining_weight,
97- env : None ,
96+ let xvm_context = Context {
97+ source_vm_id : VmId :: Wasm ,
98+ weight_limit,
9899 } ;
99100
100- let call_result =
101- pallet_xvm:: Pallet :: < T > :: xvm_bare_call ( xvm_context, caller, to, input) ;
101+ let vm_id = {
102+ match TryInto :: < VmId > :: try_into ( vm_id) {
103+ Ok ( id) => id,
104+ Err ( err) => {
105+ // TODO: Propagate error
106+ let result = Into :: < XvmExecutionResult > :: into ( err) ;
107+ return Ok ( RetVal :: Converging ( result. into ( ) ) ) ;
108+ }
109+ }
110+ } ;
111+ let call_result = XC :: call ( xvm_context, vm_id, caller, to, input) ;
102112
103- let actual_weight = pallet_xvm:: consumed_weight ( & call_result) ;
113+ let actual_weight = match call_result {
114+ Ok ( ref info) => info. used_weight ,
115+ Err ( ref err) => err. used_weight ,
116+ } ;
104117 env. adjust_weight ( charged_weight, actual_weight) ;
105118
106119 match call_result {
107- Ok ( success ) => {
120+ Ok ( info ) => {
108121 log:: trace!(
109122 target: "xvm-extension::xvm_call" ,
110- "success : {:?}" , success
123+ "info : {:?}" , info
111124 ) ;
112125
113- let buffer: sp_std:: vec:: Vec < _ > = success . output ( ) . encode ( ) ;
126+ let buffer: sp_std:: vec:: Vec < _ > = info . output . encode ( ) ;
114127 env. write ( & buffer, false , None ) ?;
115- Ok ( RetVal :: Converging ( XvmExecutionResult :: Success as u32 ) )
128+ Ok ( RetVal :: Converging ( XvmExecutionResult :: Ok . into ( ) ) )
116129 }
117130
118- Err ( failure ) => {
131+ Err ( err ) => {
119132 log:: trace!(
120133 target: "xvm-extension::xvm_call" ,
121- "failure : {:?}" , failure
134+ "err : {:?}" , err
122135 ) ;
123136
124137 // TODO Propagate error
125- Ok ( RetVal :: Converging ( XvmExecutionResult :: UnknownError as u32 ) )
138+ let result = Into :: < XvmExecutionResult > :: into ( err. error ) ;
139+ Ok ( RetVal :: Converging ( result. into ( ) ) )
126140 }
127141 }
128142 }
0 commit comments