Starknet 백서

Starknet

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 73쪽 중 64쪽

10.3 (December 12, '22)"> <Note> The `deploy` transaction is no longer supported on this version. </Note> This version contains the following changes: Starknet * Performance - Separate the state commitment computation from the execution of the transactions * Add `starknet-class-hash` command to compute the class hash of a compiled Starknet contract Cairo: * Autoformatter: Automatically break lines inside expressions </Update> <Update label="Starknet v0.10.2 (November 29, '22)"> * This version introduces sequencer parallelization! This is the first step in our roadmap of performance upgrades. Details about the specific mechanism of parallelization and the roadmap in general are described in [this medium post](https://medium.com/starkware/starknet-performance-roadmap-bb7aae14c7de). * A new endpoint, `estimate_fee_bulk`, is added to the feeder gateway. This will allow estimating the fee of several transactions at once, where each transaction is executed relative to the resulting state from applying the previous one. * Sequencing performance improvements * Builtin ratio changes, which affects builtin costs * Add `estimate_fee_bulk` API that computes the fee of multiple transactions that will be executed consecutively As part of this version, we will also increase the finality of transactions in the pending block, by fixing the timestamp at the time of the block creation. This will solve the issue of transactions moving from pending to rejected on account of too old timestamp </Update> <Update label="Starknet v0.10.1 (October 25, '22)"> This version contains the following changes: Starknet: * Add `DeployAccount` transaction (which will replace the Deploy transaction for deploying account contracts). To use it, you should first add enough funds to your account address to pay the transaction fee, and then you can invoke DeployAccount * Split the `starknet deploy_account` CLI command into `starknet new_account` and `starknet deploy_account` * Account contracts that are expected to be deployed this way should implement the`__validate_deploy__()` entry point, which should check the signature of the `DeployAccount` transaction * Improve L1 fee computation: the fee is computed according to the diff of the storage state * API: Remove `entry_point_type` field from transaction information Cairo: * Add `uint256_mul_div_mod` to `uint256.cairo` </Update> <Update label="Starknet v0.10.0 (September 05, '22)"> This version introduces the next step in Starknet's account abstraction design, specifically the validate/execute separation. See [here](https://www.notion.so/starkware/Starknet-0-10-0-4ac978234c384a30a195ce4070461257) for more information. This version contains the following changes: Starknet: * Contract (breaking changes): @external and @view functions should be imported directly by the main compiled file. Otherwise, they will not be usable as external functions. Forbid using the same storage variable name in two modules * New transaction version (version 1) for `invoke` and `declare` transactions: Transactions of version 0 are deprecated and will not be supported in Starknet from the next version (v0.11.0). Please update your systems to use the new version <Note> In order to use transactions of version 1 you will need to upgrade your account contracts </Note> * Add nonce field to the transactions. Nonce validation is now part of the Starknet protocol and is enforced to be executed sequentially * `Invoke`: Split `__execute__` to two functions:` __validate__` (only validates the transaction) and `__execute__` (only executes the transaction). Remove the selector (which is now always `__execute__`) field, following the above change. * Declare: `declare` transaction should now be sent from an account (and is validated using `__validate_declare__` in the account contract) * Support fee for sending L1 messages. At this point, it's not mandatory and messages with no fee will still be handled. Starting from the next version it will become mandatory. Cairo: Syntax changes in Cairo (to make it more similar to rust and C++): You can use the cairo-migrate script to convert old code to the new syntax. Use the `-i` flag to apply the changes to the files. End statements with `;`. Use `{ … }` for code blocks (instead of `:` and `end`). Add `()` around the condition of if statements. Remove the member keyword in structs. Change comment to use `//` instead of `#`. Use `..., ap++` instead of `...; ap++` in low level Cairo code. Support return types that are not tuples. Return statement accepts expressions, rather than only tuples. Support using functions as expressions. Fix a bug in the secp signature verification code. Add Cairo code for the recursive STARK verifier. Technical changes: * Move from python3.7 to python3.9 </Update> <Update label="Starknet v0.9.1 (July 20, '22)"> This version contains the following changes: Starknet: API changes: * Add `get_block_traces` API - returns all the transaction traces of a given block * Add a list of declared contracts in `get_state_update` * Add a 0x prefix for class hash in the API * Add `starknet_version` field for blocks (only applies to new blocks) Starknet CLI: * Change the default block number to pending * Using a wallet is the default, `--no_wallet` must be specified explicitly to override this * Deploying contracts: Add `deploy_contract` function to the account contract created by `starknet deploy_account`. Use this function to deploy contract (unless using `--no_wallet`). In particular, `deploy` should be used after declaring the contract (it expects the contract class hash) * Support `--dry_run` to get the transaction information without signing or sending it * Support `deploy_from_zero` in the `deploy` syscall to deploy a contract to an address that does not depend on the deployer Cairo: * Support and in if statements (`if x == y and z == w`). <Note> At the moment other boolean combinations are not supported </Note> </Update> <Update label="Starknet v0.9.0 (June 06, '22)"> This version introduces the contract class/instance paradigm into Starknet. See [here](https://docs.starknet.io/documentation/architecture_and_learn/architecture/Contracts/contract-classes/) for more information. This version contains the following changes: Starknet: * Enforce fees - `max_fee` must not be set to zero, and selector must be `__execute__` * Split the concepts of contract class and contract instance. * Add `declare` transaction type * New API and CLI commands: `declare` - Declares a contract class. `get_class_by_hash` - Returns the contract class given its hash. `get_class_hash_at` - Returns the class hash for a given contract instance address * Rename `delegate_call` to `library_call`, and change the contract address argument to class hash. * Add a `deploy` system call. * Rename `ContractDefinition` to `ContractClass` * Reduce the compiled contract file's size by removing unnecessary identifiers (this optimization can be disabled using `--dont_filter_identifiers`) Cairo: * Initial support for the `EC-op` builtin (scalar multiplication over the STARK curve). Not supported in Starknet yet. * Add additional helper methods to `blake2s.cairo`, including big-endian support Technical changes: * Change function's `return` type from a struct to a named tuple. In particular, `foo.Return.SIZE` is no longer supported. </Update> # Accounts Source: https://docs.starknet.io/learn/protocol/accounts ## Overview Accounts represent users onchain, and enable them to interact with the blockchain. [Unlike Ethereum](/learn/protocol/accounts#ethereum's-account-structure), Starknet facilitates account abstraction natively, via its unique account [structure](/learn/protocol/accounts#starknet's-account-structure) and [nonce management](/learn/protocol/accounts#account-nonces). Accounts can be [easily deployed on Starknet](/learn/protocol/accounts#deploying-a-new-account), both with or without having a preexisting account. ## Ethereum's account structure Within Ethereum individual user accounts are known as Externally Owned Accounts (EOAs). EOAs differ from smart contracts in that EOAs are not controlled by code, but rather by a pair of private and public keys. The account's address is derived from those keys, and only by possessing the private key can you initiate transactions from an account. While simple, because the signature scheme is fixed, EOAs have some drawbacks, including the following: * Control over the private key gives complete control over the account, so you must keep your seed phrase secure yet accessible. * Wallet functionality has limited flexibility [ERC-4337](https://eips.ethereum.org/EIPS/eip-4337) is a design proposal for Ethereum that outlines *\_account abstraction*, whereby all accounts are managed via a dedicated smart contract on the Ethereum network, as a way to increase flexibility and usability. Using account abstraction you can program how your account functions, including: * Determine what it means for a signature to be valid, or what contracts your account is allowed to interact with, also known as *signature abstraction* * Pay transaction fees in different tokens, also known as *fee abstraction* * Design your own replay protection mechanism and allow sending multiple uncoupled transactions in parallel, also known as *nonce abstraction* <Note> Without account abstractions, EOAs cannot send two transactions in parallel, but rather must wait for confirmation of the first before sending the second, as otherwise the second transaction can be rejected due to an invalid nonce. With account abstraction, sequential nonces is not required. </Note> Two prominent examples of how account abstraction might be used to program account functionality include: * **Social recovery:** retrieving your wallet if in case its lost via a selected social network, vastly improving the typical experience of wallet recovery * **Operating your account via facial recognition:** Using your phone's native hardware to sign transactions, making it practically impossible to take control of another user's account, even if your phone is stolen ## Starknet's account structure Starknet's account structure is inspired by Ethereum's EIP-4337 where smart contract accounts with arbitrary verification logic are used instead of EOAs. A valid account contract in Starknet is simply a contract that includes the following functions: | Function name | When required | | - | - | | `__validate__` | Always required | | `__execute__` | Always required. The signatures of `__validate__` and `__execute__` must be identical. See critical validation requirements below. | | `__validate_declare__` | Required for the account to be able to send a `DECLARE` transaction. This function must receive exactly one argument, which is the class hash of the declared class. | | `__validate_deploy__` | Required to allow deploying an instance of the account contract with a `DEPLOY_ACCOUNT` transaction. The arguments of `__validate_deploy__` must be the class hash of the account to be deployed and the salt used for computing the account's contract address, followed by the constructor arguments. | | `constructor` | All contracts have a `constructor` function. It can be explicitly defined in the contract, or if not explicitly defined, the sequencer uses a default `constructor` function, which is empty. | <Danger> Two critical validations must happen in `__execute__`, and their absence can lead to draining of the account's funds: (1) `assert!(get_caller_address().is_zero())` This asserts that the account's `__execute__` is not called from another contract, thus skipping validations (in later versions we may disallow calling `__execute__` from another contract at the protocol level) (2) `assert!(get_tx_info().unbox().version.into() >= 1_u32)` This asserts that the transaction's version is at least 1, preventing the account from accepting INVOKE v0 transactions. It is critical to explicitly disallow the deprecated v0 transaction type, as v0 transactions assume that the signature verification happens in `__execute__`, and are thus skipping `__validate__` entirely. </Danger> <Note> You can only use the `__validate_deploy__` function in an account contract to validate the DEPLOY\_ACCOUNT transaction for that same contract. That is, this function runs at most once throughout the lifecycle of the account. </Note> ### Flow When the sequencer receives a transaction, it calls the corresponding function with the appropriate input from the transaction's data, as follows: * For a `DECLARE` transaction, the sequencer validates the transaction by calling the `__validate_declare__` function. * For an `INVOKE` transaction, the sequencer calls the `__validate__` function with the transaction's calldata as input, after being deserialized to the arguments in the `__validate__` function's signature. After successfully completing validation, the sequencer calls the `__execute__` function with the same arguments. * For a `DEPLOY_ACCOUNT` transaction, the sequencer calls the `constructor` function with the transaction's `constructor_calldata` as input, after being deserialized to the arguments in the constructor signature. After the successful execution of the constructor, the sequencer validates the transaction by calling the `__validate_deploy__` function. <Warning> For an `INVOKE` and `DEPLOY_ACCOUNT` transactions, it is up to the sender to make sure that the calldata is serializied correctly according to either the `__validate__` or `constructor` function's signature. </Warning> <Note> Separating the validation and execution stages guarantees payment to sequencers for work completed and protects them from DoS attacks. </Note> <Tip> For more information on transaction types and lifecycle, see [Transactions](/learn/protocol/transactions/). For more information on the each transaction's fields, see [Transaction fields and hash calculations reference](/learn/cheatsheets/transactions-reference/). </Tip> ### Limitations The logic of the `__validate__`, `__validate_deploy__`, `__validate_declare__`, and `constructor` functions can be mostly arbitrary, except for the following limitations: <Note> For the `constructor` function, limitations apply only when run in a `DEPLOY_ACCOUNT` transaction (in particular, **not** when an account is deployed from an existing class via the `deploy` syscall) </Note> * You cannot use more than 1,000,000 Cairo steps * You cannot use more than 100,000,000 gas * You cannot call the following syscalls: * `get_class_hash_at` * `get_sequencer_address` (this syscall is only supported for Cairo 0 contracts) <Warning> Starting from Starknet version 0.14.0, calling the `deploy` syscall from the `__validate__`, `__validate_deploy__`, `__validate_declare__`, and `constructor` functions will also not be possible. </Warning> * You cannot call functions in external contracts <Note> This restriction enforces a single storage update being able to invalidate only transactions from a single account. However, be aware that an account can always invalidate its own past transactions by e.g. changing its public key. This limitation implies that the fees you need to pay to invalidate transactions in the mempool are directly proportional to the number of unique accounts whose transactions you want to invalidate. </Note> * When calling the `get_execution_info` syscall: * `sequencer_address` is set to zero * `block_timestamp` returns the time (in UTC), rounded to the most recent hour * `block_number` returns the block number, rounded down to the nearest multiple of 100 These limitations are designed to prevent the following DoS attacks on the sequencer: * An attacker could cause the sequencer to perform a large amount of work before a transaction fails validation. Two examples of such attacks are: * Spamming `INVOKE` transactions whose `__validate__` requires many steps, but eventually fails * Spamming `DEPLOY_ACCOUNT` transactions that are invalid as a result of the constructor or `__validate_deploy__` failing. * The above attacks are solved by making sure that the validation step is not resource-intensive, e.g. by keeping the maximal number of steps low. However, even if the validation is simple, the following "mempool pollution" attack could still be possible: 1. An attacker fills the mempool with transactions that are valid at the time they are sent. 2. The sequencer is ready to execute them, thinking that by the time it includes them in a block, they will still be valid. 3. Shortly after the transactions are sent, the attacker sends one transaction that somehow invalidates all the previous ones and makes sure it's included in a block, e.g. by offering higher fees for this one transaction. An example of such an attack is having the implementation of `__validate__` checks that the value of a storage slot is `1`, and the attacker's transaction later sets it to `0`. Restricting validation functions from calling external contracts prevents this attack. ### Failures When the `__validate__`, `__validate_deploy__`, or `__validate_declare__` functions fail, the account in question does not pay any fee, and the transaction's status is `REJECTED`. When the `__execute__` function fails, the transaction's status is `REVERTED`. Similar to Ethereum, a reverted transaction is included in a block, and the sequencer is eligible to charge a fee for the work done up to the point of failure. <Tip> To learn more about transaction statuses, see [Transactions](/learn/protocol/transactions#transaction_statuses). </Tip> ### SNIP-6 While not mandatory at the protocol level, you can use a richer standard interface for accounts, defined in [Starknet Improvement Proposal 6 (SNIP-6)](https://github.com/starknet-io/SNIPs/blob/main/SNIPS/snip-6.md). SNIP-6 was developed by community members at [OpenZeppelin](https://www.openzeppelin.com/), in close collaboration with wallet teams and other core Starknet developers. ### Example Thanks to account abstraction, the logic of `__execute__` and the different validation functions is up to the party implementing the account. To review a concrete implementation, you can check out OpenZeppelin's [account component](https://github.com/OpenZeppelin/cairo-contracts/blob/v0.14.0/src/account/account.cairo#L72), which also adheres to [SNIP6](/learn/protocol/accounts#snip-6). ## Account nonces Similar to Ethereum, every contract in Starknet, including account contracts, has a nonce. The nonce of a transaction sent from an account must match the nonce of that account, which changes after the transaction is executed — even if it was reverted. Nonces serves two important roles: * They guarantee transaction hash uniqueness, which is important for a good user experience * They provide replay protection to the account, by binding the signature to a particular nonce and preventing a malicious party from replaying the transaction Also similarly to Ethereum, Starknet currently determines the nonce structure at the protocol level to be sequential (i.e., the nonce of a transaction sent from an account is incremented by one after the transaction is executed). In the future, Starknet will consider a more flexible design, extending account abstraction to nonce abstraction. However, unlike Ethereum, only the nonce of account contracts — that is, those adhering to [Starknet's account structure](/learn/protocol/accounts#starknet's-account-structure) — can be non-zero in Starknet. In contrast, in Ethereum, regular smart contracts can also increment their nonce by deploying smart contracts (i.e., executing the `CREATE` and `CREATE2` opcodes). ## Deploying a new account New accounts can be deployed in the following ways: * Sending a `DEPLOY_ACCOUNT` transaction, which does not require a preexisting account.
프로젝트로 돌아가기