-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adress simple comments use generic List strategy inline errors use patch ethereum specs
- Loading branch information
Showing
8 changed files
with
184 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from ethereum_types.bytes import BytesStruct | ||
|
||
struct StackUnderflowError { | ||
value: BytesStruct*, | ||
} | ||
|
||
struct StackOverflowError { | ||
value: BytesStruct*, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from ethereum_types.numeric import U256, U256Struct | ||
from ethereum_types.bytes import BytesStruct | ||
from starkware.cairo.common.dict import DictAccess, dict_read, dict_write | ||
from ethereum.cancun.vm.exceptions import StackOverflowError, StackUnderflowError | ||
|
||
struct Stack { | ||
value: StackStruct*, | ||
} | ||
|
||
struct StackStruct { | ||
dict_ptr_start: StackDictAccess*, | ||
dict_ptr: StackDictAccess*, | ||
len: felt, | ||
} | ||
|
||
struct StackDictAccess { | ||
key: felt, | ||
prev_value: U256, | ||
new_value: U256, | ||
} | ||
|
||
const STACK_MAX_SIZE = 1024; | ||
|
||
func pop{stack: Stack}() -> (U256, StackUnderflowError) { | ||
alloc_locals; | ||
let len = stack.value.len; | ||
if (len == 0) { | ||
tempvar err = StackUnderflowError(new BytesStruct(cast(0, felt*), 0)); | ||
let val = U256(cast(0, U256Struct*)); | ||
return (val, err); | ||
} | ||
|
||
let dict_ptr = cast(stack.value.dict_ptr, DictAccess*); | ||
with dict_ptr { | ||
let (pointer) = dict_read(len - 1); | ||
} | ||
let new_dict_ptr = cast(dict_ptr, StackDictAccess*); | ||
|
||
tempvar stack = Stack(new StackStruct(stack.value.dict_ptr_start, new_dict_ptr, len - 1)); | ||
tempvar value = U256(cast(pointer, U256Struct*)); | ||
|
||
let ok_ = StackUnderflowError(cast(0, BytesStruct*)); | ||
return (value, ok_); | ||
} | ||
|
||
func push{stack: Stack}(value: U256) -> StackOverflowError { | ||
alloc_locals; | ||
let len = stack.value.len; | ||
if (len == STACK_MAX_SIZE) { | ||
tempvar err = StackOverflowError(new BytesStruct(cast(0, felt*), 0)); | ||
return err; | ||
} | ||
|
||
let dict_ptr = cast(stack.value.dict_ptr, DictAccess*); | ||
with dict_ptr { | ||
dict_write(len, cast(value.value, felt)); | ||
} | ||
let new_dict_ptr = cast(dict_ptr, StackDictAccess*); | ||
|
||
tempvar stack = Stack(new StackStruct(stack.value.dict_ptr_start, new_dict_ptr, len + 1)); | ||
let ok_ = StackOverflowError(cast(0, BytesStruct*)); | ||
|
||
return ok_; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from typing import List | ||
|
||
import pytest | ||
from ethereum_types.numeric import U256 | ||
from hypothesis import assume, given | ||
|
||
from ethereum.cancun.vm.exceptions import StackOverflowError, StackUnderflowError | ||
from ethereum.cancun.vm.stack import pop, push | ||
|
||
|
||
class TestStack: | ||
def test_pop_underflow(self, cairo_run): | ||
stack = [] | ||
with pytest.raises(StackUnderflowError): | ||
cairo_run("pop", stack) | ||
with pytest.raises(StackUnderflowError): | ||
pop(stack) | ||
|
||
@given(stack=...) | ||
def test_pop_success(self, cairo_run, stack: List[U256]): | ||
assume(len(stack) > 0) | ||
|
||
(new_stack_cairo, popped_value_cairo) = cairo_run("pop", stack) | ||
popped_value_py = pop(stack) | ||
assert new_stack_cairo == stack | ||
assert popped_value_cairo == popped_value_py | ||
|
||
@given(value=...) | ||
def test_push_overflow(self, cairo_run, value: U256): | ||
stack = [U256(0)] * 1024 | ||
with pytest.raises(StackOverflowError): | ||
cairo_run("push", stack, value) | ||
with pytest.raises(StackOverflowError): | ||
push(stack, value) | ||
|
||
@given(stack=..., value=...) | ||
def test_push_success(self, cairo_run, stack: List[U256], value: U256): | ||
new_stack_cairo = cairo_run("push", stack, value) | ||
push(stack, value) | ||
assert new_stack_cairo == stack |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.