@@ -443,26 +443,17 @@ impl PartitionProvider {
443443mod blockchain_reader_tests {
444444 use crate :: * ;
445445 use alloy:: {
446+ hex:: FromHex ,
446447 network:: EthereumWallet ,
447448 node_bindings:: { Anvil , AnvilInstance } ,
448449 primitives:: Address ,
449450 providers:: ProviderBuilder ,
450- signers:: local:: PrivateKeySigner ,
451- signers:: Signer ,
451+ signers:: { local:: PrivateKeySigner , Signer } ,
452452 sol_types:: { SolCall , SolValue } ,
453453 transports:: http:: { Client , Http } ,
454454 } ;
455455 use cartesi_dave_contracts:: daveconsensus:: DaveConsensus :: { self , EpochSealed } ;
456456 use cartesi_dave_merkle:: Digest ;
457- use cartesi_prt_contracts:: {
458- bottomtournamentfactory:: BottomTournamentFactory ,
459- middletournamentfactory:: MiddleTournamentFactory ,
460- multileveltournamentfactory:: MultiLevelTournamentFactory :: {
461- self , CommitmentStructure , DisputeParameters , MultiLevelTournamentFactoryInstance ,
462- TimeConstants ,
463- } ,
464- toptournamentfactory:: TopTournamentFactory ,
465- } ;
466457 use cartesi_prt_core:: arena:: SenderFiller ;
467458 use cartesi_rollups_contracts:: {
468459 inputbox:: InputBox :: { self , InputAdded } ,
@@ -479,13 +470,24 @@ mod blockchain_reader_tests {
479470
480471 type Result < T > = std:: result:: Result < T , Box < dyn std:: error:: Error > > ;
481472 const APP_ADDRESS : Address = Address :: ZERO ;
482- const INITIAL_STATE : Digest = Digest :: ZERO ;
473+ // $ xxd -p -c32 test/programs/echo/machine-image/hash
474+ const INITIAL_STATE : & str =
475+ "0x84c8181abd120e0281f5032d22422b890f79880ae90d9a1416be1afccb8182a0" ;
483476 const INPUT_PAYLOAD : & str = "Hello!" ;
484477 const INPUT_PAYLOAD2 : & str = "Hello Two!" ;
485478
486- fn spawn_anvil_and_provider ( ) -> ( AnvilInstance , Arc < SenderFiller > ) {
487- let args = vec ! [ "--slots-in-an-epoch" , "1" ] ;
488- let anvil = Anvil :: default ( ) . block_time ( 1 ) . args ( args) . spawn ( ) ;
479+ fn spawn_anvil_and_provider ( ) -> ( AnvilInstance , SenderFiller , Address , Address ) {
480+ let anvil = Anvil :: default ( )
481+ . block_time ( 1 )
482+ . args ( [
483+ "--disable-code-size-limit" ,
484+ "--preserve-historical-states" ,
485+ "--slots-in-an-epoch" ,
486+ "1" ,
487+ "--load-state" ,
488+ "../../../test/programs/echo/anvil_state.json" ,
489+ ] )
490+ . spawn ( ) ;
489491
490492 let mut signer: PrivateKeySigner = anvil. keys ( ) [ 0 ] . clone ( ) . into ( ) ;
491493
@@ -497,7 +499,12 @@ mod blockchain_reader_tests {
497499 . wallet ( wallet)
498500 . on_http ( anvil. endpoint_url ( ) ) ;
499501
500- ( anvil, Arc :: new ( provider) )
502+ (
503+ anvil,
504+ provider,
505+ Address :: from_hex ( "0x5fbdb2315678afecb367f032d93f642f64180aa3" ) . unwrap ( ) ,
506+ Address :: from_hex ( "0x0165878a594ca255338adfa4d48449f69242eb8f" ) . unwrap ( ) ,
507+ )
501508 }
502509
503510 fn create_partition_rovider ( url : & str ) -> Result < PartitionProvider > {
@@ -513,70 +520,8 @@ mod blockchain_reader_tests {
513520 EventReader :: < InputAdded > :: new ( )
514521 }
515522
516- async fn deploy_inputbox < ' a > (
517- provider : & ' a Arc < SenderFiller > ,
518- ) -> Result < Arc < InputBox :: InputBoxInstance < Http < Client > , & ' a Arc < SenderFiller > > > > {
519- let inputbox = InputBox :: deploy ( provider) . await ?;
520- Ok ( Arc :: new ( inputbox) )
521- }
522-
523- async fn deploy_daveconsensus < ' a > (
524- provider : & ' a Arc < SenderFiller > ,
525- inputbox : & Address ,
526- tournament_factory : & Address ,
527- ) -> Result < Arc < DaveConsensus :: DaveConsensusInstance < Http < Client > , & ' a Arc < SenderFiller > > > >
528- {
529- let daveconsensus = DaveConsensus :: deploy (
530- provider,
531- * inputbox,
532- APP_ADDRESS ,
533- * tournament_factory,
534- INITIAL_STATE . into ( ) ,
535- )
536- . await ?;
537- Ok ( Arc :: new ( daveconsensus) )
538- }
539-
540- async fn deploy_tournamentfactories < ' a > (
541- provider : & ' a Arc < SenderFiller > ,
542- ) -> Result < Arc < MultiLevelTournamentFactoryInstance < Http < Client > , & ' a Arc < SenderFiller > > > > {
543- let dispute_parameters = DisputeParameters {
544- timeConstants : TimeConstants {
545- matchEffort : 60 * 2 ,
546- maxAllowance : 60 * ( 60 + 5 + 2 ) ,
547- } ,
548- commitmentStructures : vec ! [
549- CommitmentStructure {
550- log2step: 44 ,
551- height: 48 ,
552- } ,
553- CommitmentStructure {
554- log2step: 28 ,
555- height: 16 ,
556- } ,
557- CommitmentStructure {
558- log2step: 0 ,
559- height: 28 ,
560- } ,
561- ] ,
562- } ;
563-
564- let top_tournamentfactory = TopTournamentFactory :: deploy ( provider) . await ?;
565- let mid_tournamentfactory = MiddleTournamentFactory :: deploy ( provider) . await ?;
566- let bottom_tournamentfactory = BottomTournamentFactory :: deploy ( provider) . await ?;
567- let multi_tournamentfactory = MultiLevelTournamentFactory :: deploy (
568- provider,
569- * top_tournamentfactory. address ( ) ,
570- * mid_tournamentfactory. address ( ) ,
571- * bottom_tournamentfactory. address ( ) ,
572- dispute_parameters,
573- )
574- . await ?;
575- Ok ( Arc :: new ( multi_tournamentfactory) )
576- }
577-
578- async fn add_input < ' a > (
579- inputbox : & ' a Arc < InputBox :: InputBoxInstance < Http < Client > , & ' a Arc < SenderFiller > > > ,
523+ async fn add_input (
524+ inputbox : & InputBox :: InputBoxInstance < Http < Client > , impl Provider < Http < Client > > > ,
580525 input_payload : & ' static str ,
581526 count : usize ,
582527 ) -> Result < ( ) > {
@@ -669,40 +614,40 @@ mod blockchain_reader_tests {
669614
670615 #[ tokio:: test]
671616 async fn test_input_reader ( ) -> Result < ( ) > {
672- let ( anvil, provider) = spawn_anvil_and_provider ( ) ;
673-
674- let inputbox = deploy_inputbox ( & provider) . await ?;
617+ let ( anvil, provider, input_box_address, _) = spawn_anvil_and_provider ( ) ;
618+ let inputbox = InputBox :: new ( input_box_address, & provider) ;
675619
676- let mut input_count = 2 ;
677- add_input ( & inputbox, & INPUT_PAYLOAD , input_count) . await ?;
620+ let input_count_1 = 2 ;
621+ // Inputbox is deployed with 1 input already
622+ add_input ( & inputbox, INPUT_PAYLOAD , input_count_1) . await ?;
678623
679624 let input_reader = create_input_reader ( ) ;
680625 let mut read_inputs = read_inputs_until_count (
681626 & anvil. endpoint ( ) ,
682627 inputbox. address ( ) ,
683628 & input_reader,
684- input_count ,
629+ 1 + input_count_1 ,
685630 )
686631 . await ?;
687- assert_eq ! ( read_inputs. len( ) , input_count ) ;
632+ assert_eq ! ( read_inputs. len( ) , 1 + input_count_1 ) ;
688633
689634 let received_payload =
690- EvmAdvanceCall :: abi_decode ( read_inputs[ input_count - 1 ] . input . as_ref ( ) , true ) ?;
635+ EvmAdvanceCall :: abi_decode ( read_inputs. last ( ) . unwrap ( ) . input . as_ref ( ) , true ) ?;
691636 assert_eq ! ( received_payload. payload. as_ref( ) , INPUT_PAYLOAD . as_bytes( ) ) ;
692637
693- input_count = 3 ;
694- add_input ( & inputbox, & INPUT_PAYLOAD2 , input_count ) . await ?;
638+ let input_count_2 = 3 ;
639+ add_input ( & inputbox, INPUT_PAYLOAD2 , input_count_2 ) . await ?;
695640 read_inputs = read_inputs_until_count (
696641 & anvil. endpoint ( ) ,
697642 inputbox. address ( ) ,
698643 & input_reader,
699- input_count ,
644+ 1 + input_count_1 + input_count_2 ,
700645 )
701646 . await ?;
702- assert_eq ! ( read_inputs. len( ) , input_count ) ;
647+ assert_eq ! ( read_inputs. len( ) , 1 + input_count_1 + input_count_2 ) ;
703648
704649 let received_payload =
705- EvmAdvanceCall :: abi_decode ( read_inputs[ input_count - 1 ] . input . as_ref ( ) , true ) ?;
650+ EvmAdvanceCall :: abi_decode ( read_inputs. last ( ) . unwrap ( ) . input . as_ref ( ) , true ) ?;
706651 assert_eq ! ( received_payload. payload. as_ref( ) , INPUT_PAYLOAD2 . as_bytes( ) ) ;
707652
708653 drop ( anvil) ;
@@ -711,16 +656,8 @@ mod blockchain_reader_tests {
711656
712657 #[ tokio:: test]
713658 async fn test_epoch_reader ( ) -> Result < ( ) > {
714- let ( anvil, provider) = spawn_anvil_and_provider ( ) ;
715-
716- let inputbox = deploy_inputbox ( & provider) . await ?;
717- let multi_tournamentfactory = deploy_tournamentfactories ( & provider) . await ?;
718- let daveconsensus = deploy_daveconsensus (
719- & provider,
720- inputbox. address ( ) ,
721- multi_tournamentfactory. address ( ) ,
722- )
723- . await ?;
659+ let ( anvil, provider, _, consensus_address) = spawn_anvil_and_provider ( ) ;
660+ let daveconsensus = DaveConsensus :: new ( consensus_address, & provider) ;
724661
725662 let epoch_reader = create_epoch_reader ( ) ;
726663 let read_epochs =
@@ -729,33 +666,28 @@ mod blockchain_reader_tests {
729666 assert_eq ! ( read_epochs. len( ) , 1 ) ;
730667 assert_eq ! (
731668 & read_epochs[ 0 ] . initialMachineStateHash. abi_encode( ) ,
732- INITIAL_STATE . slice( )
669+ Digest :: from_digest_hex ( INITIAL_STATE ) . unwrap ( ) . slice( )
733670 ) ;
734671
735672 drop ( anvil) ;
736673 Ok ( ( ) )
737674 }
738675
739676 #[ tokio:: test]
740- async fn test_blockchain_reader ( ) -> Result < ( ) > {
741- let ( anvil, provider) = spawn_anvil_and_provider ( ) ;
677+ async fn test_blockchain_reader_aaa ( ) -> Result < ( ) > {
678+ let ( anvil, provider, input_box_address , consensus_address ) = spawn_anvil_and_provider ( ) ;
742679
743- let inputbox = deploy_inputbox ( & provider) . await ? ;
680+ let inputbox = InputBox :: new ( input_box_address , & provider) ;
744681 let state_manager = Arc :: new ( PersistentStateAccess :: new (
745682 Connection :: open_in_memory ( ) . unwrap ( ) ,
746683 ) ?) ;
747684
685+ // Note that inputbox is deployed with 1 input already
748686 // add inputs to epoch 0
749- let mut input_count = 2 ;
750- add_input ( & inputbox, & INPUT_PAYLOAD , input_count ) . await ?;
687+ let input_count_1 = 2 ;
688+ add_input ( & inputbox, INPUT_PAYLOAD , input_count_1 ) . await ?;
751689
752- let multi_tournamentfactory = deploy_tournamentfactories ( & provider) . await ?;
753- let daveconsensus = deploy_daveconsensus (
754- & provider,
755- inputbox. address ( ) ,
756- multi_tournamentfactory. address ( ) ,
757- )
758- . await ?;
690+ let daveconsensus = DaveConsensus :: new ( consensus_address, & provider) ;
759691 let mut blockchain_reader = BlockchainReader :: new (
760692 state_manager. clone ( ) ,
761693 AddressBook {
@@ -767,23 +699,30 @@ mod blockchain_reader_tests {
767699 1 ,
768700 ) ?;
769701
770- let _ = spawn ( async move {
702+ let r = spawn ( async move {
771703 blockchain_reader. start ( ) . await . unwrap ( ) ;
772704 } ) ;
773705
774- read_inputs_from_db_until_count ( & state_manager, 0 , input_count) . await ?;
706+ read_inputs_from_db_until_count ( & state_manager, 0 , 1 ) . await ?;
707+ read_inputs_from_db_until_count ( & state_manager, 1 , input_count_1) . await ?;
775708
776709 // add inputs to epoch 1
777- input_count = 3 ;
778- add_input ( & inputbox, & INPUT_PAYLOAD , input_count ) . await ?;
779- read_inputs_from_db_until_count ( & state_manager, 1 , input_count ) . await ?;
710+ let input_count_2 = 3 ;
711+ add_input ( & inputbox, INPUT_PAYLOAD , input_count_2 ) . await ?;
712+ read_inputs_from_db_until_count ( & state_manager, 1 , input_count_1 + input_count_2 ) . await ?;
780713
781714 // add more inputs to epoch 1
782- let more_input_count = 3 ;
783- add_input ( & inputbox, & INPUT_PAYLOAD , more_input_count) . await ?;
784- read_inputs_from_db_until_count ( & state_manager, 1 , input_count + more_input_count) . await ?;
715+ let input_count_3 = 3 ;
716+ add_input ( & inputbox, INPUT_PAYLOAD , input_count_3) . await ?;
717+ read_inputs_from_db_until_count (
718+ & state_manager,
719+ 1 ,
720+ input_count_1 + input_count_2 + input_count_3,
721+ )
722+ . await ?;
785723
786724 drop ( anvil) ;
725+ drop ( r) ;
787726 Ok ( ( ) )
788727 }
789728}
0 commit comments