Skip to content

Commit

Permalink
setup path to maintain contract class hashes and program hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
drspacemn committed Oct 3, 2023
1 parent 14984b3 commit 253d70d
Show file tree
Hide file tree
Showing 14 changed files with 545 additions and 114 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Rust Library for running the Starknet OS via the [Cairo VM](https://github.com/l
***Cairo [Env](https://docs.cairo-lang.org/0.12.0/quickstart.html)***

```bash
git submodule update --init
./scripts/setup-tests.sh
```

## Env
Expand Down
41 changes: 0 additions & 41 deletions build.rs

This file was deleted.

43 changes: 43 additions & 0 deletions scripts/setup-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

if ! command -v cairo-compile > /dev/null
then
echo "please start cairo dev environment"
exit 1
fi

if ! command -v starknet-compile-deprecated > /dev/null
then
echo "please start cairo dev environment"
exit 1
fi

git submodule update --init

# setup test_contract path
cp tests/dependencies/test_contract_interface.cairo cairo-lang/src/starkware/starknet/core/test_contract/
cp tests/dependencies/deprecated_syscalls.cairo cairo-lang/src/starkware/starknet/core/test_contract/

starknet-compile-deprecated --no_debug_info tests/contracts/test_contract.cairo --output build/test_contract.json --cairo_path cairo-lang/src

# setup token_for_testing path
mkdir -p cairo-lang/src/starkware/starknet/std_contracts/ERC20
cp tests/dependencies/ERC20.cairo cairo-lang/src/starkware/starknet/std_contracts/ERC20/
cp tests/dependencies/ERC20_base.cairo cairo-lang/src/starkware/starknet/std_contracts/ERC20/
cp tests/dependencies/permitted.cairo cairo-lang/src/starkware/starknet/std_contracts/ERC20/
mkdir -p cairo-lang/src/starkware/starknet/std_contracts/upgradability_proxy
cp tests/dependencies/initializable.cairo cairo-lang/src/starkware/starknet/std_contracts/upgradability_proxy

# compile starknet contract
starknet-compile-deprecated --no_debug_info tests/contracts/token_for_testing.cairo --output build/token_for_testing.json --cairo_path cairo-lang/src --account_contract
starknet-compile-deprecated --no_debug_info tests/contracts/dummy_account.cairo --output build/dummy_account.json --cairo_path cairo-lang/src --account_contract
starknet-compile-deprecated --no_debug_info tests/contracts/dummy_token.cairo --output build/dummy_token.json --cairo_path cairo-lang/src --account_contract

# compile os with debug info
cairo-compile cairo-lang/src/starkware/starknet/core/os/os.cairo --output build/os_debug.json --cairo_path cairo-lang/src

# compile cairo programs
cairo-compile tests/programs/different_output.cairo --output build/different_output.json
cairo-compile tests/programs/fact.cairo --output build/fact.json
cairo-compile tests/programs/hint.cairo --output build/hint.json

9 changes: 9 additions & 0 deletions scripts/teardown-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

shopt -s extglob

git submodule deinit -f .
git submodule update --init

# remove compiled contracts
rm -f build/!(os_latest.json)
69 changes: 2 additions & 67 deletions tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,74 +22,9 @@ use rstest::*;
use blockifier::execution::contract_class::ContractClassV0;
use blockifier::test_utils::*;

const BUILD_DIR: &str = "build/";
const CAIRO_COMPILE_CMD: &str = "cairo-compile";
const STARKNET_COMPILE_CMD: &str = "starknet-compile-deprecated";
const TEST_CONTRACTS_DIR: &str = "tests/contracts/";
const TEST_PROGRAMS_DIR: &str = "tests/programs/";

#[fixture]
#[once]
pub fn compile_contracts() {
let contracts = fs::read_dir(TEST_CONTRACTS_DIR).unwrap();

for contract in contracts {
let contract_path = contract.unwrap().path();
let stem = contract_path.file_stem().unwrap();

let contract_out_fmt = format!("{BUILD_DIR}{}.json", stem.to_str().unwrap());
let contract_out = path::PathBuf::from(&contract_out_fmt);

if !contract_out.exists() {
let cmd_check = process::Command::new(STARKNET_COMPILE_CMD).arg("-v").output();
assert!(cmd_check.is_ok());

let out = process::Command::new(STARKNET_COMPILE_CMD)
.args([
contract_path.to_str().unwrap(),
"--output",
contract_out.to_str().unwrap(),
"--no_debug_info",
"--cairo_path",
"cairo-lang/src",
])
.output();
assert!(out.is_ok());
}
}
}

#[fixture]
#[once]
pub fn compile_programs() {
let programs = fs::read_dir(TEST_PROGRAMS_DIR).unwrap();

for program in programs {
let program_path = program.unwrap().path();
let stem = program_path.file_stem().unwrap();

let program_out_fmt = format!("{BUILD_DIR}{}.json", stem.to_str().unwrap());
let program_out = path::PathBuf::from(&program_out_fmt);

if !program_out.exists() {
let cmd_check = process::Command::new(CAIRO_COMPILE_CMD).arg("-v").output();
assert!(cmd_check.is_ok());

let out = process::Command::new(CAIRO_COMPILE_CMD)
.args([
program_path.to_str().unwrap(),
"--output",
program_out.to_str().unwrap(),
"--no_debug_info",
])
.output();
assert!(out.is_ok());
}
}
}

#[fixture]
pub fn setup_runner(_compile_programs: ()) -> (CairoRunner, VirtualMachine) {
pub fn setup_runner() -> (CairoRunner, VirtualMachine) {
let program_content = fs::read("build/fact.json").unwrap();

let mut hint_processor = BuiltinHintProcessor::new_empty();
Expand Down Expand Up @@ -161,7 +96,7 @@ pub fn sw_compat_config() -> (StarknetGeneralConfig, BlockInfo) {

// StateDiff(address_to_class_hash={1320198688701432534719157742574249187606949535760401890964327160391813649815: 3091807693782130767501414083074324621934995166802976508502430836300051087875, 991941543224299354231458875645971891578962076399384993792970752200863471543: 2876089962371428491550960640645254719239874945228583157550348788822249547409}, nonces={<DataAvailabilityMode.L1: 0>: {1320198688701432534719157742574249187606949535760401890964327160391813649815: 2, 991941543224299354231458875645971891578962076399384993792970752200863471543: 1}}, storage_updates={<DataAvailabilityMode.L1: 0>: {1320198688701432534719157742574249187606949535760401890964327160391813649815: {322990191961554429053868449035526014412279677330895387449703561219527453810: 1464161352, 553060490499374521350519736808313349113904249869887409031913453630132670891: 1320198688701432534719157742574249187606949535760401890964327160391813649815, 27838543048034294391053713572600349634190921977214803311654412455831886488: 168811955464684315858783496655603761152, 482148859801725464274198147480840119334382080162606228723774290742111978842: 170141183460469231731687303715884105728, 877823913686921299048507465990220541161247202424540097559864758276037605753: 18, 1473120764136009396440970107973971969419251478021578277222806501183556393953: 6928249226643520745136808551794, 622991711977806760541268368343056323675924475443734630808231720566777350071: 1329227995784915872903807060280344576, 1267095271664963432194589802007389382906322551387806188627353037007263322258: 1}}}, declared_classes={}, block_info=BlockInfo(block_number=0, block_timestamp=1000, eth_l1_gas_price=100000000, strk_l1_gas_price=0, sequencer_address=443902168967810054148884074756742919510645257800272067493104417962415061304, starknet_version='0.12.3'))
#[rstest]
pub fn setup_snos_data(sw_compat_config: (StarknetGeneralConfig, BlockInfo), _compile_contracts: ()) {
pub fn setup_snos_data(sw_compat_config: (StarknetGeneralConfig, BlockInfo)) {

// let class_map = HashMap::from([
// (
Expand Down
4 changes: 2 additions & 2 deletions tests/contracts/test_contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ from starkware.starknet.common.syscalls import (
storage_write,
)
from starkware.starknet.core.os.constants import TRANSACTION_VERSION
from tests.contracts.deprecated_syscalls import delegate_call
from tests.contracts.test_contract_interface import StorageCell, TestContract
from starkware.starknet.core.test_contract.deprecated_syscalls import delegate_call
from starkware.starknet.core.test_contract.test_contract_interface import StorageCell, TestContract

@storage_var
func two_counters(index: felt) -> (res: (felt, felt)) {
Expand Down
39 changes: 39 additions & 0 deletions tests/contracts/token_for_testing.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// The fee token contract used in tests; allow easy initialization using a single `DeployAccount`
// transaction; also serves as a faucet account.

%lang starknet

from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.cairo_builtins import HashBuiltin
from starkware.cairo.common.uint256 import Uint256
from starkware.starknet.common.syscalls import get_contract_address
from starkware.starknet.core.test_contract.dummy_account import (
__execute__,
__validate__,
__validate_declare__,
__validate_deploy__,
deploy_contract,
)
from starkware.starknet.std_contracts.ERC20.ERC20 import initialize, permissionedMint, transfer
from starkware.starknet.std_contracts.ERC20.ERC20_base import ERC20_mint, balanceOf, name

const AMOUNT_TO_MINT = 2 ** 127;

@constructor
func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() {
let (self_address: felt) = get_contract_address();
let (init_vector: felt*) = alloc();

// Name.
assert init_vector[0] = 'Wrapped Ether';
// Symbol.
assert init_vector[1] = 'WETH';
// Decimals.
assert init_vector[2] = 18;
// Minter address.
assert init_vector[3] = self_address;

initialize(init_vector_len=4, init_vector=init_vector);
ERC20_mint(recipient=self_address, amount=Uint256(low=AMOUNT_TO_MINT, high=0));
return ();
}
Loading

0 comments on commit 253d70d

Please sign in to comment.