@@ -1055,3 +1055,95 @@ async fn test_mine_first_block_with_interval() {
1055
1055
let second_block = api. block_by_number ( 2 . into ( ) ) . await . unwrap ( ) . unwrap ( ) ;
1056
1056
assert_eq ! ( second_block. header. timestamp, init_timestamp + 120 ) ;
1057
1057
}
1058
+
1059
+ #[ tokio:: test( flavor = "multi_thread" ) ]
1060
+ async fn test_anvil_reset_non_fork ( ) {
1061
+ let ( api, handle) = spawn ( NodeConfig :: test ( ) ) . await ;
1062
+ let provider = handle. http_provider ( ) ;
1063
+
1064
+ // Get initial state
1065
+ let init_block = provider. get_block ( BlockId :: latest ( ) ) . await . unwrap ( ) . unwrap ( ) ;
1066
+ let init_accounts = api. accounts ( ) . unwrap ( ) ;
1067
+ let init_balance = provider. get_balance ( init_accounts[ 0 ] ) . await . unwrap ( ) ;
1068
+
1069
+ // Store the instance id before reset
1070
+ let instance_id_before = api. instance_id ( ) ;
1071
+
1072
+ // Mine some blocks and make transactions
1073
+ for _ in 0 ..5 {
1074
+ api. mine_one ( ) . await ;
1075
+ }
1076
+
1077
+ // Send a transaction
1078
+ let to = Address :: random ( ) ;
1079
+ let val = U256 :: from ( 1337 ) ;
1080
+ let tx = TransactionRequest :: default ( ) . with_from ( init_accounts[ 0 ] ) . with_to ( to) . with_value ( val) ;
1081
+ let tx = WithOtherFields :: new ( tx) ;
1082
+
1083
+ let _ = provider. send_transaction ( tx) . await . unwrap ( ) . get_receipt ( ) . await . unwrap ( ) ;
1084
+
1085
+ // Check state has changed
1086
+ let block_before_reset = provider. get_block ( BlockId :: latest ( ) ) . await . unwrap ( ) . unwrap ( ) ;
1087
+ assert ! ( block_before_reset. header. number > init_block. header. number) ;
1088
+
1089
+ let balance_before_reset = provider. get_balance ( init_accounts[ 0 ] ) . await . unwrap ( ) ;
1090
+ assert ! ( balance_before_reset < init_balance) ;
1091
+
1092
+ let to_balance_before_reset = provider. get_balance ( to) . await . unwrap ( ) ;
1093
+ assert_eq ! ( to_balance_before_reset, val) ;
1094
+
1095
+ // Reset to fresh in-memory state (non-fork)
1096
+ api. anvil_reset ( None ) . await . unwrap ( ) ;
1097
+
1098
+ // Check instance id has changed
1099
+ let instance_id_after = api. instance_id ( ) ;
1100
+ assert_ne ! ( instance_id_before, instance_id_after) ;
1101
+
1102
+ // Check we're back at genesis
1103
+ let block_after_reset = provider. get_block ( BlockId :: latest ( ) ) . await . unwrap ( ) . unwrap ( ) ;
1104
+ assert_eq ! ( block_after_reset. header. number, 0 ) ;
1105
+
1106
+ // Check accounts are restored to initial state
1107
+ let balance_after_reset = provider. get_balance ( init_accounts[ 0 ] ) . await . unwrap ( ) ;
1108
+ assert_eq ! ( balance_after_reset, init_balance) ;
1109
+
1110
+ // Check the recipient's balance is zero
1111
+ let to_balance_after_reset = provider. get_balance ( to) . await . unwrap ( ) ;
1112
+ assert_eq ! ( to_balance_after_reset, U256 :: ZERO ) ;
1113
+
1114
+ // Test we can continue mining after reset
1115
+ api. mine_one ( ) . await ;
1116
+ let new_block = provider. get_block ( BlockId :: latest ( ) ) . await . unwrap ( ) . unwrap ( ) ;
1117
+ assert_eq ! ( new_block. header. number, 1 ) ;
1118
+ }
1119
+
1120
+ #[ tokio:: test( flavor = "multi_thread" ) ]
1121
+ async fn test_anvil_reset_fork_to_non_fork ( ) {
1122
+ let ( api, handle) = spawn ( fork_config ( ) ) . await ;
1123
+ let provider = handle. http_provider ( ) ;
1124
+
1125
+ // Verify we're in fork mode
1126
+ let metadata = api. anvil_metadata ( ) . await . unwrap ( ) ;
1127
+ assert ! ( metadata. forked_network. is_some( ) ) ;
1128
+
1129
+ // Mine some blocks
1130
+ for _ in 0 ..3 {
1131
+ api. mine_one ( ) . await ;
1132
+ }
1133
+
1134
+ // Reset to non-fork mode
1135
+ api. anvil_reset ( None ) . await . unwrap ( ) ;
1136
+
1137
+ // Verify we're no longer in fork mode
1138
+ let metadata_after = api. anvil_metadata ( ) . await . unwrap ( ) ;
1139
+ assert ! ( metadata_after. forked_network. is_none( ) ) ;
1140
+
1141
+ // Check we're at block 0
1142
+ let block = provider. get_block ( BlockId :: latest ( ) ) . await . unwrap ( ) . unwrap ( ) ;
1143
+ assert_eq ! ( block. header. number, 0 ) ;
1144
+
1145
+ // Verify we can still mine blocks
1146
+ api. mine_one ( ) . await ;
1147
+ let new_block = provider. get_block ( BlockId :: latest ( ) ) . await . unwrap ( ) . unwrap ( ) ;
1148
+ assert_eq ! ( new_block. header. number, 1 ) ;
1149
+ }
0 commit comments