@@ -27,6 +27,7 @@ use pallet_revive::{
2727 create1,
2828 evm:: { Account , BlockTag , U256 } ,
2929} ;
30+ use static_init:: dynamic;
3031use std:: thread;
3132use substrate_cli_test_utils:: * ;
3233
@@ -60,6 +61,52 @@ fn get_contract(name: &str) -> anyhow::Result<(Vec<u8>, ethabi::Contract)> {
6061 Ok ( ( bytecode, contract) )
6162}
6263
64+ struct SharedResources {
65+ _node_handle : std:: thread:: JoinHandle < ( ) > ,
66+ _rpc_handle : std:: thread:: JoinHandle < ( ) > ,
67+ }
68+
69+ impl SharedResources {
70+ fn start ( ) -> Self {
71+ // Start the node.
72+ let _node_handle = thread:: spawn ( move || {
73+ if let Err ( e) = start_node_inline ( vec ! [
74+ "--dev" ,
75+ "--rpc-port=45789" ,
76+ "--no-telemetry" ,
77+ "--no-prometheus" ,
78+ "-lerror,evm=debug,sc_rpc_server=info,runtime::revive=trace" ,
79+ ] ) {
80+ panic ! ( "Node exited with error: {e:?}" ) ;
81+ }
82+ } ) ;
83+
84+ // Start the rpc server.
85+ let args = CliCommand :: parse_from ( [
86+ "--dev" ,
87+ "--rpc-port=45788" ,
88+ "--node-rpc-url=ws://localhost:45789" ,
89+ "--no-prometheus" ,
90+ "-linfo,eth-rpc=debug" ,
91+ ] ) ;
92+
93+ let _rpc_handle = thread:: spawn ( move || {
94+ if let Err ( e) = cli:: run ( args) {
95+ panic ! ( "eth-rpc exited with error: {e:?}" ) ;
96+ }
97+ } ) ;
98+
99+ Self { _node_handle, _rpc_handle }
100+ }
101+
102+ async fn client ( ) -> WsClient {
103+ ws_client_with_retry ( "ws://localhost:45788" ) . await
104+ }
105+ }
106+
107+ #[ dynamic( lazy) ]
108+ static mut SHARED_RESOURCES : SharedResources = SharedResources :: start ( ) ;
109+
63110macro_rules! unwrap_call_err(
64111 ( $err: expr) => {
65112 match $err. downcast_ref:: <jsonrpsee:: core:: client:: Error >( ) . unwrap( ) {
@@ -70,41 +117,42 @@ macro_rules! unwrap_call_err(
70117) ;
71118
72119#[ tokio:: test]
73- async fn test_jsonrpsee_server ( ) -> anyhow:: Result < ( ) > {
74- // Start the node.
75- let _ = thread:: spawn ( move || {
76- if let Err ( e) = start_node_inline ( vec ! [
77- "--dev" ,
78- "--rpc-port=45789" ,
79- "--no-telemetry" ,
80- "--no-prometheus" ,
81- "-lerror,evm=debug,sc_rpc_server=info,runtime::revive=trace" ,
82- ] ) {
83- panic ! ( "Node exited with error: {e:?}" ) ;
84- }
85- } ) ;
86-
87- // Start the rpc server.
88- let args = CliCommand :: parse_from ( [
89- "--dev" ,
90- "--rpc-port=45788" ,
91- "--node-rpc-url=ws://localhost:45789" ,
92- "--no-prometheus" ,
93- "-linfo,eth-rpc=debug" ,
94- ] ) ;
95- let _ = thread:: spawn ( move || {
96- if let Err ( e) = cli:: run ( args) {
97- panic ! ( "eth-rpc exited with error: {e:?}" ) ;
98- }
99- } ) ;
120+ async fn transfer ( ) -> anyhow:: Result < ( ) > {
121+ let _lock = SHARED_RESOURCES . write ( ) ;
122+ let client = SharedResources :: client ( ) . await ;
100123
101- let client = ws_client_with_retry ( "ws://localhost:45788" ) . await ;
124+ let ethan = Account :: from ( subxt_signer:: eth:: dev:: ethan ( ) ) ;
125+ let initial_balance = client. get_balance ( ethan. address ( ) , BlockTag :: Latest . into ( ) ) . await ?;
126+
127+ let value = 1_000_000_000_000_000_000_000u128 . into ( ) ;
128+ let hash = TransactionBuilder :: default ( )
129+ . value ( value)
130+ . to ( ethan. address ( ) )
131+ . send ( & client)
132+ . await ?;
133+
134+ let receipt = wait_for_successful_receipt ( & client, hash) . await ?;
135+ assert_eq ! (
136+ Some ( ethan. address( ) ) ,
137+ receipt. to,
138+ "Receipt should have the correct contract address."
139+ ) ;
140+
141+ let increase =
142+ client. get_balance ( ethan. address ( ) , BlockTag :: Latest . into ( ) ) . await ? - initial_balance;
143+ assert_eq ! ( value, increase) ;
144+ Ok ( ( ) )
145+ }
146+
147+ #[ tokio:: test]
148+ async fn deploy_and_call ( ) -> anyhow:: Result < ( ) > {
149+ let _lock = SHARED_RESOURCES . write ( ) ;
150+ let client = SharedResources :: client ( ) . await ;
102151 let account = Account :: default ( ) ;
103152
104153 // Balance transfer
105154 let ethan = Account :: from ( subxt_signer:: eth:: dev:: ethan ( ) ) ;
106- let ethan_balance = client. get_balance ( ethan. address ( ) , BlockTag :: Latest . into ( ) ) . await ?;
107- assert_eq ! ( U256 :: zero( ) , ethan_balance) ;
155+ let initial_balance = client. get_balance ( ethan. address ( ) , BlockTag :: Latest . into ( ) ) . await ?;
108156
109157 let value = 1_000_000_000_000_000_000_000u128 . into ( ) ;
110158 let hash = TransactionBuilder :: default ( )
@@ -120,8 +168,8 @@ async fn test_jsonrpsee_server() -> anyhow::Result<()> {
120168 "Receipt should have the correct contract address."
121169 ) ;
122170
123- let ethan_balance = client. get_balance ( ethan. address ( ) , BlockTag :: Latest . into ( ) ) . await ?;
124- assert_eq ! ( value, ethan_balance , "ethan's balance should be the same as the value sent." ) ;
171+ let updated_balance = client. get_balance ( ethan. address ( ) , BlockTag :: Latest . into ( ) ) . await ?;
172+ assert_eq ! ( value, updated_balance - initial_balance ) ;
125173
126174 // Deploy contract
127175 let data = b"hello world" . to_vec ( ) ;
@@ -169,15 +217,19 @@ async fn test_jsonrpsee_server() -> anyhow::Result<()> {
169217 wait_for_successful_receipt ( & client, hash) . await ?;
170218 let increase = client. get_balance ( contract_address, BlockTag :: Latest . into ( ) ) . await ? - balance;
171219 assert_eq ! ( value, increase, "contract's balance should have increased by the value sent." ) ;
220+ Ok ( ( ) )
221+ }
172222
173- // Deploy revert
223+ #[ tokio:: test]
224+ async fn revert_call ( ) -> anyhow:: Result < ( ) > {
225+ let _lock = SHARED_RESOURCES . write ( ) ;
226+ let client = SharedResources :: client ( ) . await ;
174227 let ( bytecode, contract) = get_contract ( "revert" ) ?;
175228 let receipt = TransactionBuilder :: default ( )
176229 . input ( contract. constructor . clone ( ) . unwrap ( ) . encode_input ( bytecode, & [ ] ) . unwrap ( ) )
177230 . send_and_wait_for_receipt ( & client)
178231 . await ?;
179232
180- // Call doRevert
181233 let err = TransactionBuilder :: default ( )
182234 . to ( receipt. contract_address . unwrap ( ) )
183235 . input ( contract. function ( "doRevert" ) ?. encode_input ( & [ ] ) ?. to_vec ( ) )
@@ -187,25 +239,36 @@ async fn test_jsonrpsee_server() -> anyhow::Result<()> {
187239
188240 let call_err = unwrap_call_err ! ( err. source( ) . unwrap( ) ) ;
189241 assert_eq ! ( call_err. message( ) , "Execution reverted: revert message" ) ;
242+ Ok ( ( ) )
243+ }
190244
191- // Deploy event
245+ #[ tokio:: test]
246+ async fn event_logs ( ) -> anyhow:: Result < ( ) > {
247+ let _lock = SHARED_RESOURCES . write ( ) ;
248+ let client = SharedResources :: client ( ) . await ;
192249 let ( bytecode, contract) = get_contract ( "event" ) ?;
193250 let receipt = TransactionBuilder :: default ( )
194251 . input ( bytecode)
195252 . send_and_wait_for_receipt ( & client)
196253 . await ?;
197254
198- // Call triggerEvent
199255 let receipt = TransactionBuilder :: default ( )
200256 . to ( receipt. contract_address . unwrap ( ) )
201257 . input ( contract. function ( "triggerEvent" ) ?. encode_input ( & [ ] ) ?. to_vec ( ) )
202258 . send_and_wait_for_receipt ( & client)
203259 . await ?;
204260 assert_eq ! ( receipt. logs. len( ) , 1 , "There should be one log." ) ;
261+ Ok ( ( ) )
262+ }
263+
264+ #[ tokio:: test]
265+ async fn invalid_transaction ( ) -> anyhow:: Result < ( ) > {
266+ let _lock = SHARED_RESOURCES . write ( ) ;
267+ let client = SharedResources :: client ( ) . await ;
268+ let ethan = Account :: from ( subxt_signer:: eth:: dev:: ethan ( ) ) ;
205269
206- // Invalid transaction
207270 let err = TransactionBuilder :: default ( )
208- . value ( value )
271+ . value ( U256 :: from ( 1_000_000_000_000u128 ) )
209272 . to ( ethan. address ( ) )
210273 . mutate ( |tx| tx. chain_id = Some ( 42u32 . into ( ) ) )
211274 . send ( & client)
0 commit comments