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

Support higher tx versions in Account #858

Merged
merged 25 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
004dafd
support higher tx versions in account
andrew-fleming Dec 18, 2023
70be4eb
update changelog
andrew-fleming Dec 18, 2023
558ee94
add utils
andrew-fleming Dec 22, 2023
7818652
add utils
andrew-fleming Dec 22, 2023
9400021
change version to u256
andrew-fleming Dec 22, 2023
0bdde42
update version import and cast to felt
andrew-fleming Dec 22, 2023
785982a
update spdx
andrew-fleming Dec 22, 2023
dacf07d
update scarb and cairo to 2.4.0
andrew-fleming Dec 22, 2023
94591b2
bump to v2.4.1
andrew-fleming Dec 24, 2023
2ec0b2c
add helper fns for testing versions
andrew-fleming Dec 28, 2023
7f3c1c7
add scarb bump to changelog
andrew-fleming Dec 29, 2023
2cba294
move account consts back to accountcomponent
andrew-fleming Dec 29, 2023
39562bb
change const to MIN_TRANSACTION_VERSION
andrew-fleming Dec 29, 2023
294eaa9
add version consts for tests
andrew-fleming Dec 29, 2023
591ef05
change version helper fns to consts
andrew-fleming Dec 29, 2023
f074be1
cast tx version to u64 in __execute__
andrew-fleming Dec 29, 2023
3ac828e
add query version comment
andrew-fleming Dec 29, 2023
ac5ea8c
add future query version test
andrew-fleming Dec 29, 2023
09f94fa
cast version to u256
andrew-fleming Dec 29, 2023
34caef2
add query tx version check
andrew-fleming Jan 19, 2024
228708b
add requirement in __execute__ comment
andrew-fleming Jan 19, 2024
6c2ba9e
fix comment
andrew-fleming Jan 19, 2024
9bb87ce
add comment
andrew-fleming Jan 19, 2024
7e599ff
fix comment
andrew-fleming Jan 19, 2024
07e5679
Update src/account/account.cairo
martriay Jan 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- uses: actions/checkout@v3
- uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.3.1"
scarb-version: "2.4.1"
martriay marked this conversation as resolved.
Show resolved Hide resolved
- name: Markdown lint
uses: DavidAnson/markdownlint-cli2-action@5b7c9f74fec47e6b15667b2cc23c63dff11e449e # v9
with:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Use ComponentState in tests (#836)
- Docsite navbar (#838)
- Support higher tx versions in Account (#858)
4 changes: 2 additions & 2 deletions Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "openzeppelin"
version = "0.8.0"
cairo-version = "2.3.1"
cairo-version = "2.4.1"
authors = ["OpenZeppelin Community <maintainers@openzeppelin.org>"]
description = "OpenZeppelin Contracts written in Cairo for StarkNet, a decentralized ZK Rollup"
documentation = "https://docs.openzeppelin.com/contracts-cairo"
Expand All @@ -11,7 +11,7 @@ license-file = "LICENSE"
keywords = ["openzeppelin", "starknet", "cairo", "contracts", "security", "standards"]

[dependencies]
starknet = "2.3.1"
starknet = "2.4.1"

[lib]

Expand Down
1 change: 1 addition & 0 deletions src/account.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod account;
mod dual_account;
mod interface;
mod utils;

use account::AccountComponent;
use interface::AccountABIDispatcher;
Expand Down
16 changes: 5 additions & 11 deletions src/account/account.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts for Cairo v0.8.0 (account/account.cairo)
// OpenZeppelin Contracts for Cairo vX.Y.Z (account/account.cairo)
martriay marked this conversation as resolved.
Show resolved Hide resolved

/// # Account Component
///
Expand All @@ -8,17 +8,14 @@
mod AccountComponent {
use ecdsa::check_ecdsa_signature;
use openzeppelin::account::interface;
use openzeppelin::account::utils::TRANSACTION_VERSION;
use openzeppelin::introspection::src5::SRC5Component::InternalTrait as SRC5InternalTrait;
use openzeppelin::introspection::src5::SRC5Component;
use starknet::account::Call;
use starknet::get_caller_address;
use starknet::get_contract_address;
use starknet::get_tx_info;

const TRANSACTION_VERSION: felt252 = 1;
// 2**128 + TRANSACTION_VERSION
const QUERY_VERSION: felt252 = 0x100000000000000000000000000000001;

#[storage]
struct Storage {
Account_public_key: felt252
Expand Down Expand Up @@ -59,8 +56,7 @@ mod AccountComponent {
///
/// Requirements:
///
/// - The transaction version must be `TRANSACTION_VERSION` for actual transactions.
/// For simulations, the version must be `QUERY_VERSION`.
/// - The transaction version must be greater than or equal to `TRANSACTION_VERSION`.
fn __execute__(
self: @ComponentState<TContractState>, mut calls: Array<Call>
) -> Array<Span<felt252>> {
Expand All @@ -71,10 +67,8 @@ mod AccountComponent {

// Check tx version
let tx_info = get_tx_info().unbox();
let version = tx_info.version;
if version != TRANSACTION_VERSION {
assert(version == QUERY_VERSION, Errors::INVALID_TX_VERSION);
}
let tx_version: u256 = tx_info.version.into();
martriay marked this conversation as resolved.
Show resolved Hide resolved
assert(TRANSACTION_VERSION <= tx_version, Errors::INVALID_TX_VERSION);
martriay marked this conversation as resolved.
Show resolved Hide resolved

_execute_calls(calls)
}
Expand Down
6 changes: 6 additions & 0 deletions src/account/utils.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts for Cairo vX.Y.Z (account/utils.cairo)

const TRANSACTION_VERSION: u256 = 1;
// 2**128 + TRANSACTION_VERSION
const QUERY_VERSION: u256 = 0x100000000000000000000000000000001;
martriay marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 11 additions & 5 deletions src/tests/account/test_account.cairo
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use openzeppelin::account::AccountComponent::{InternalTrait, SRC6CamelOnlyImpl};
use openzeppelin::account::AccountComponent::{OwnerAdded, OwnerRemoved};
use openzeppelin::account::AccountComponent::{PublicKeyCamelImpl, PublicKeyImpl};
use openzeppelin::account::AccountComponent::{TRANSACTION_VERSION, QUERY_VERSION};
use openzeppelin::account::AccountComponent;
use openzeppelin::account::interface::{ISRC6, ISRC6_ID};
use openzeppelin::account::{AccountABIDispatcherTrait, AccountABIDispatcher};
use openzeppelin::introspection::interface::{ISRC5, ISRC5_ID};
use openzeppelin::tests::mocks::account_mocks::DualCaseAccountMock;
use openzeppelin::tests::mocks::erc20_mocks::DualCaseERC20Mock;
use openzeppelin::tests::utils::constants::{PUBKEY, NEW_PUBKEY, SALT, ZERO};
use openzeppelin::tests::utils::constants::{
PUBKEY, NEW_PUBKEY, SALT, ZERO, QUERY_VERSION, TRANSACTION_VERSION
};
use openzeppelin::tests::utils;
use openzeppelin::token::erc20::interface::{IERC20DispatcherTrait, IERC20Dispatcher};
use openzeppelin::utils::selectors;
Expand Down Expand Up @@ -71,7 +72,7 @@ fn setup() -> ComponentState {
}

martriay marked this conversation as resolved.
Show resolved Hide resolved
fn setup_dispatcher(data: Option<@SignedTransactionData>) -> AccountABIDispatcher {
testing::set_version(TRANSACTION_VERSION);
testing::set_version(TRANSACTION_VERSION());

let mut calldata = array![];
if data.is_some() {
Expand Down Expand Up @@ -287,17 +288,22 @@ fn test_execute() {
test_execute_with_version(Option::None(()));
}

#[test]
fn test_execute_future_version() {
test_execute_with_version(Option::Some(TRANSACTION_VERSION() + 1));
}

#[test]
#[available_gas(2000000)]
fn test_execute_query_version() {
test_execute_with_version(Option::Some(QUERY_VERSION));
test_execute_with_version(Option::Some(QUERY_VERSION()));
}

#[test]
#[available_gas(2000000)]
#[should_panic(expected: ('Account: invalid tx version', 'ENTRYPOINT_FAILED'))]
fn test_execute_invalid_version() {
test_execute_with_version(Option::Some(TRANSACTION_VERSION - 1));
test_execute_with_version(Option::Some(TRANSACTION_VERSION() - 1));
}

#[test]
Expand Down
16 changes: 11 additions & 5 deletions src/tests/presets/test_account.cairo
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use openzeppelin::account::AccountComponent::{OwnerAdded, OwnerRemoved};
use openzeppelin::account::AccountComponent::{TRANSACTION_VERSION, QUERY_VERSION};
use openzeppelin::account::interface::ISRC6_ID;
use openzeppelin::account::{AccountABIDispatcherTrait, AccountABIDispatcher};
use openzeppelin::introspection::interface::ISRC5_ID;
use openzeppelin::presets::Account;
use openzeppelin::tests::account::test_account::{
deploy_erc20, SIGNED_TX_DATA, SignedTransactionData
};
use openzeppelin::tests::utils::constants::{PUBKEY, NEW_PUBKEY, SALT, ZERO, RECIPIENT};
use openzeppelin::tests::utils::constants::{
PUBKEY, NEW_PUBKEY, SALT, ZERO, RECIPIENT, QUERY_VERSION, TRANSACTION_VERSION
};
use openzeppelin::tests::utils;
use openzeppelin::token::erc20::interface::{IERC20DispatcherTrait, IERC20Dispatcher};
use openzeppelin::utils::selectors;
Expand All @@ -34,7 +35,7 @@ fn setup_dispatcher() -> AccountABIDispatcher {
}

fn setup_dispatcher_with_data(data: Option<@SignedTransactionData>) -> AccountABIDispatcher {
testing::set_version(TRANSACTION_VERSION);
testing::set_version(TRANSACTION_VERSION());

let mut calldata = array![];
if data.is_some() {
Expand Down Expand Up @@ -328,17 +329,22 @@ fn test_execute() {
test_execute_with_version(Option::None(()));
}

#[test]
fn test_execute_future_version() {
test_execute_with_version(Option::Some(TRANSACTION_VERSION() + 1));
}

#[test]
#[available_gas(2000000)]
fn test_execute_query_version() {
test_execute_with_version(Option::Some(QUERY_VERSION));
test_execute_with_version(Option::Some(QUERY_VERSION()));
}

#[test]
#[available_gas(2000000)]
#[should_panic(expected: ('Account: invalid tx version', 'ENTRYPOINT_FAILED'))]
fn test_execute_invalid_version() {
test_execute_with_version(Option::Some(TRANSACTION_VERSION - 1));
test_execute_with_version(Option::Some(TRANSACTION_VERSION() - 1));
}

#[test]
Expand Down
9 changes: 9 additions & 0 deletions src/tests/utils/constants.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use openzeppelin::account::utils;
use starknet::ClassHash;
use starknet::ContractAddress;
use starknet::class_hash_const;
Expand Down Expand Up @@ -75,3 +76,11 @@ fn DATA(success: bool) -> Span<felt252> {
}
data.span()
}

fn TRANSACTION_VERSION() -> felt252 {
utils::TRANSACTION_VERSION.try_into().unwrap()
}
martriay marked this conversation as resolved.
Show resolved Hide resolved

fn QUERY_VERSION() -> felt252 {
utils::QUERY_VERSION.try_into().unwrap()
}