@@ -28,25 +28,43 @@ impl TestEnvironment {
28
28
get_block_number ()
29
29
}
30
30
31
- // Returns the block number of the last committed block - this is the block number that gets used in production
32
- // to execute the private part of transactions when your PXE managed to successfully sync to the tip of the chain.
31
+ /// Returns the block number of the last committed block - this is the block number that gets used in production
32
+ /// to execute the private part of transactions when your PXE managed to successfully sync to the tip of the chain.
33
33
pub unconstrained fn committed_block_number (self : Self ) -> u32 {
34
34
self .pending_block_number () - 1
35
35
}
36
36
37
+ /// With TXe tests, every test is run in a mock "contract". This facilitates the ability to write to and read from storage,
38
+ /// emit and retrieve nullifiers (because they are hashed with a contract address), and formulate notes in a canonical way.
39
+ /// The contract_address also represents the "msg_sender" when we call a contract with a private or public context; and when
40
+ /// we call top-level unconstrained functions, we must set this contract address to the contract being called.
41
+ /// The contract address can be manipulated to do the above at any particular address, and not simply the one provided at
42
+ /// the instantiation of the test.
43
+ /// Returns the currently set contract address.
37
44
pub unconstrained fn contract_address (_self : Self ) -> AztecAddress {
38
45
get_contract_address ()
39
46
}
40
47
48
+ /// Modifies the currently set contract address. As per above, it sets the "msg_sender" address on our subsequent calls.
49
+ /// This is useful when we have multiple "accounts" that need to interact with an arbitrary contract. This also allows
50
+ /// us to change the "contract" we emit side effects from, and is required when we want to run a top-level unconstrained function on another contract.
41
51
pub unconstrained fn impersonate (_self : Self , address : AztecAddress ) {
42
52
cheatcodes:: set_contract_address (address )
43
53
}
44
54
55
+ /// Advances the internal TXe state to specified block number.
56
+ /// Note that this block number describes the block being built. i.e. If you advance the block to 5 from 1,
57
+ /// the pending block number will be 5, and your last committed block number will be 4.
45
58
pub unconstrained fn advance_block_to (&mut self , block_number : u32 ) {
46
- let difference = block_number - get_block_number ();
59
+ let pending_block_number = self .pending_block_number ();
60
+ assert (pending_block_number <= block_number , "Cannot advance block to a previous block." );
61
+
62
+ let difference = block_number - pending_block_number ;
47
63
self .advance_block_by (difference );
48
64
}
49
65
66
+ /// Advances the internal TXe state by the amount of blocks specified. The TXe produces a valid block for every
67
+ /// block advanced, and we are able to fetch historical data from warped over blocks.
50
68
pub unconstrained fn advance_block_by (_self : &mut Self , blocks : u32 ) {
51
69
cheatcodes:: advance_blocks_by (blocks );
52
70
}
0 commit comments