Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(docs): cheatcode docs #1585

Merged
merged 9 commits into from
Aug 18, 2023
Merged

feat(docs): cheatcode docs #1585

merged 9 commits into from
Aug 18, 2023

Conversation

rahul-kothari
Copy link
Contributor

fIX #1458

Copy link
Contributor

@LHerskind LHerskind left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nits and grits

docs/docs/dev_docs/testing/cheat_codes.md Outdated Show resolved Hide resolved

## Introduction

Most of the time, simply testing your smart contract isn't enough. To manipulate the state of the Aztec blockchain, as well as test for specific reverts and events, the sandbox is shipped with a set of cheatcodes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The start is a little weird. It is mainly to help you in the testing that we want to provide a bit of cheat-codes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When mentioning specific reverts and such, we should probably have examples with that 👀 .

docs/docs/dev_docs/testing/cheat_codes.md Outdated Show resolved Hide resolved
docs/docs/dev_docs/testing/cheat_codes.md Outdated Show resolved Hide resolved
const value = await cc.l2.loadPublic(contract, 1n) // current_value is stored in slot 1
```

### computeSlotInMap
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think it would be neat to flip the ordering of computeSlotInMap and loadPublic, mainly as we could then very easily show using the computed slot for reading balances or similar. Just to carve it in neon what people can use it for etc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separately, the example is made with private storage, which you cannot access from cheatcodes etc. Think using public storage would be better, and then it can be bundled with the loadPublic as mentioned.

docs/docs/dev_docs/testing/cheat_codes.md Outdated Show resolved Hide resolved
docs/docs/dev_docs/testing/cheat_codes.md Outdated Show resolved Hide resolved
docs/docs/dev_docs/testing/cheat_codes.md Outdated Show resolved Hide resolved
docs/docs/dev_docs/testing/cheat_codes.md Outdated Show resolved Hide resolved
docs/docs/dev_docs/testing/cheat_codes.md Outdated Show resolved Hide resolved
Copy link
Member

@Maddiaa0 Maddiaa0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

temp review before pushing changes


Most of the time, simply testing your smart contract isn't enough. To manipulate the state of the Aztec blockchain, as well as test for specific reverts and events, the sandbox is shipped with a set of cheatcodes.

Cheatcodes allow you to change the time of the L2 block, load certain state or more easily manipulate L1 instead of having to write dedicated RPC calls to anvil.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has just made me think, should we make the cheat codes agnostic of hardhat / anvil

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm aztec uses anvil within the sandbox though

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think anvil will also accept the hardhat naming and is just used as aliases, so if we use the hardhat ones, it might cover both more easily.

docs/docs/dev_docs/testing/cheat_codes.md Show resolved Hide resolved
docs/docs/dev_docs/testing/cheat_codes.md Outdated Show resolved Hide resolved
rahul-kothari added a commit that referenced this pull request Aug 18, 2023
In case we like the `.eth` and `.aztec` variation over `.l1` and `.l2`
in cheatcodes!

This would get merged into my docs pr #1585
rahul-kothari added a commit that referenced this pull request Aug 18, 2023
In case we like the `.eth` and `.aztec` variation over `.l1` and `.l2`
in cheatcodes!

This would get merged into my docs pr #1585

#### Description
Sets the timestamp (unix format in seconds) for the next mined block on Ethereum.
Remember that timestamp can only be set in the future and not in the past.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit its probably worth being explicit about the erroring

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably related to: #1585 (comment)

docs/docs/dev_docs/testing/cheat_codes.md Show resolved Hide resolved
- @aztec/aztec.js

### Initialisation
```js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should get better highlighting using `ts rather than js

These are cheatcodes exposed from anvil/hardhat conveniently wrapped for ease of use in the Sandbox.

### Interface
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

annotate `ts

These are cheatcodes specific to manipulating the state of Aztec rollup.

### Interface
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

annotate ts


#### Example
```
/// struct Storage {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noir snippets can be annotated rust for better results

Copy link
Member

@Maddiaa0 Maddiaa0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall great content, however noir snippets can be annotated with rust, the ones currently js should be ts to get better highlighting and solidity ones can be separated rather than inline commented and annotated with solidity

Comment on lines 229 to 243
```
/// contract LeetContract {
/// uint256 private leet = 1337; // slot 0
/// }

const leetContractAddress = EthAddress.fromString('0x1234...');
await cc.eth.store(leetContractAddress, BigInt(0), BigInt(1000));
const value = await cc.eth.load(leetContractAddress, BigInt(0));
console.log(value); // 1000
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contract LeetContract {
     uint256 private leet = 1337; // slot 0
 }
const leetContractAddress = EthAddress.fromString('0x1234...');
await cc.eth.store(leetContractAddress, BigInt(0), BigInt(1000));
const value = await cc.eth.load(leetContractAddress, BigInt(0));
console.log(value); // 1000

Solidity blocks can be annotated with solidity, they arent perfect in docusaurus but it does offer some highlighting.

rahul-kothari added a commit that referenced this pull request Aug 18, 2023
In case we like the `.eth` and `.aztec` variation over `.l1` and `.l2`
in cheatcodes!

This would get merged into my docs pr #1585
@Maddiaa0 Maddiaa0 changed the title add cheatcode docs feat(docs): cheatcode docs Aug 18, 2023
#### Example
```rust
struct Storage {
// highlight-next-line:PublicState
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this still required

#### Description
Loads the value stored at the given slot in the public storage of the given contract.

Note: One Field element occupies a storage slot. So structs with multiple field elements won't fit in a single slot. So using loadPublic would only load a part of the struct (depending on the size of the attributes within it).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Note: One Field element occupies a storage slot. So structs with multiple field elements won't fit in a single slot. So using loadPublic would only load a part of the struct (depending on the size of the attributes within it).
Note: One Field element occupies a storage slot. Hence, structs with multiple field elements will be spread over multiple slots, rather they will be spread over multiple sequential slots. Using loadPublic will only load a single field of the struct (depending on the size of the attributes within it).

Id reword this slightly


Keep up with the latest discussion and join the conversation in the [Aztec forum](https://discourse.aztec.network).

You can also use the above link to request for more cheatcodes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
You can also use the above link to request for more cheatcodes.
You can also use the above link to request more cheatcodes.

#### Description
Dumps the current Ethereum chain state to a file.
Stores a hex string representing the complete state of the chain in a file with the provided path. Can be re-imported into a fresh/restarted instance of Anvil to reattain the same state.
When combined with `loadChainState()` cheatcode, it can be let you easily import the current state of mainnet into the Anvil instance of the sandbox, to use Uniswap for example.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The uniswap example here is really specific to our testing case and probably confusing without context. Lets drop it, people looking for this feature likely have in mind a specific use case

@rahul-kothari rahul-kothari enabled auto-merge (squash) August 18, 2023 11:35
rahul-kothari and others added 7 commits August 18, 2023 11:37
Co-authored-by: Lasse Herskind <16536249+LHerskind@users.noreply.github.com>
In case we like the `.eth` and `.aztec` variation over `.l1` and `.l2`
in cheatcodes!

This would get merged into my docs pr #1585
#### Description
Dumps the current Ethereum chain state to a file.
Stores a hex string representing the complete state of the chain in a file with the provided path. Can be re-imported into a fresh/restarted instance of Anvil to reattain the same state.
When combined with `loadChainState()` cheatcode, it can be let you easily import the current state of mainnet into the Anvil instance of the sandbox.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wording is a little weird here, it can be let you and it might not be the current state. Maybe fine to point to the other cheatcode and say use in combination.

Suggested change
When combined with `loadChainState()` cheatcode, it can be let you easily import the current state of mainnet into the Anvil instance of the sandbox.
To be used in combination with `loadChainState()`

@rahul-kothari rahul-kothari merged commit b1a2f8f into master Aug 18, 2023
1 check passed
@rahul-kothari rahul-kothari deleted the rk/cc_docs branch August 18, 2023 11:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Document cheatcodes
3 participants