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

Fix safe uint256 sub #824

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions solidity_contracts/src/PlainOpcodes/Counter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,24 @@ contract Counter {
count += 1;
}

function decUnchecked() public {
unchecked {
count -= 1;
}
function dec() public {
count -= 1;
}

function decInPlace() public greaterThanZero {
function decInPlace() public {
count--;
}

function dec() public greaterThanZero {
function decWithModifier() public greaterThanZero {
count -= 1;
}

function decUnchecked() public {
unchecked {
count -= 1;
}
}

function reset() public {
count = 0;
}
Expand Down
60 changes: 31 additions & 29 deletions tests/end_to_end/PlainOpcodes/test_counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,49 @@ async def test_should_return_0_after_deployment(
assert await counter.count() == 0

class TestInc:
async def test_should_increase_count(self, counter, owner):
await counter.reset(caller_eoa=owner.starknet_contract)
await counter.inc(caller_eoa=owner.starknet_contract)
async def test_should_increase_count(self, counter):
await counter.reset()
await counter.inc()
assert await counter.count() == 1

class TestDec:
async def test_should_raise_from_modifier_when_count_is_0(self, counter, owner):
await counter.reset(caller_eoa=owner.starknet_contract)
async def test_should_raise_from_modifier_when_count_is_0(self, counter):
await counter.reset()
with evm_error("count should be strictly greater than 0"):
await counter.dec(caller_eoa=owner.starknet_contract)
await counter.decWithModifier()

@pytest.mark.xfail(
reason="https://github.com/kkrt-labs/kakarot/issues/683",
)
async def test_should_raise_from_vm_when_count_is_0(self, counter, owner):
await counter.reset(caller_eoa=owner.starknet_contract)
async def test_should_return_uint256_max(self, counter):
await counter.reset()
await counter.decUnchecked()
assert await counter.count() == 2**256 - 1

async def test_should_revert_when_count_zero(self, counter):
await counter.reset()
with evm_error():
await counter.decUnchecked(caller_eoa=owner.starknet_contract)
await counter.dec()

async def test_should_decrease_count(self, counter, owner):
await counter.reset(caller_eoa=owner.starknet_contract)
await counter.inc(caller_eoa=owner.starknet_contract)
await counter.dec(caller_eoa=owner.starknet_contract)
async def test_should_decrease_count(self, counter):
await counter.reset()
await counter.inc()
await counter.dec()
assert await counter.count() == 0

async def test_should_decrease_count_unchecked(self, counter, owner):
await counter.reset(caller_eoa=owner.starknet_contract)
await counter.inc(caller_eoa=owner.starknet_contract)
await counter.decUnchecked(caller_eoa=owner.starknet_contract)
async def test_should_decrease_count_unchecked(self, counter):
await counter.reset()
await counter.inc()
await counter.decUnchecked()
assert await counter.count() == 0

async def test_should_decrease_count_in_place(self, counter, owner):
await counter.reset(caller_eoa=owner.starknet_contract)
await counter.inc(caller_eoa=owner.starknet_contract)
await counter.decInPlace(caller_eoa=owner.starknet_contract)
async def test_should_decrease_count_in_place(self, counter):
await counter.reset()
await counter.inc()
await counter.decInPlace()
assert await counter.count() == 0

class TestReset:
async def test_should_set_count_to_0(self, counter, owner):
await counter.inc(caller_eoa=owner.starknet_contract)
await counter.reset(caller_eoa=owner.starknet_contract)
async def test_should_set_count_to_0(self, counter):
await counter.inc()
await counter.reset()
assert await counter.count() == 0

class TestDeploymentWithValue:
Expand All @@ -73,12 +75,12 @@ class TestLoops:
async def test_should_set_counter_to_iterations_with_for_loop(
self, counter, owner, iterations
):
await counter.incForLoop(iterations, caller_eoa=owner.starknet_contract)
await counter.incForLoop(iterations)
assert await counter.count() == iterations

@pytest.mark.parametrize("iterations", [0, 50, 200])
async def test_should_set_counter_to_iterations_with_while_loop(
self, counter, owner, iterations
):
await counter.incWhileLoop(iterations, caller_eoa=owner.starknet_contract)
await counter.incWhileLoop(iterations)
assert await counter.count() == iterations
Loading