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

Update Struct public getter to return whole Struct #2064

Merged
merged 4 commits into from
Jun 28, 2020
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
74 changes: 37 additions & 37 deletions tests/examples/voting/test_ballot.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,33 @@ def test_initial_state(w3, c):
# Check chairperson is msg.sender
assert c.chairperson() == a0
# Check propsal names are correct
assert c.proposals__name(0)[:7] == b"Clinton"
assert c.proposals__name(1)[:5] == b"Trump"
assert c.proposals(0)[0][:7] == b"Clinton" # Proposal.name
assert c.proposals(1)[0][:5] == b"Trump" # Proposal.name
# Check proposal voteCount is 0
assert c.proposals__voteCount(0) == 0
assert c.proposals__voteCount(1) == 0
assert c.proposals(0)[1] == 0 # Proposal.voteCount
assert c.proposals(1)[1] == 0 # Proposal.voteCount
# Check voterCount is 0
assert c.voterCount() == 0
# Check voter starts empty
assert c.voters__delegate(z0) is None
assert c.voters__vote(z0) == 0
assert c.voters__voted(z0) is False
assert c.voters__weight(z0) == 0
assert c.voters(z0)[2] is None # Voter.delegate
assert c.voters(z0)[3] == 0 # Voter.vote
assert c.voters(z0)[1] is False # Voter.voted
assert c.voters(z0)[0] == 0 # Voter.weight


def test_give_the_right_to_vote(w3, c, assert_tx_failed):
a0, a1, a2, a3, a4, a5 = w3.eth.accounts[:6]
c.giveRightToVote(a1, transact={})
# Check voter given right has weight of 1
assert c.voters__weight(a1) == 1
assert c.voters(a1)[0] == 1 # Voter.weight
# Check no other voter attributes have changed
assert c.voters__delegate(a1) is None
assert c.voters__vote(a1) == 0
assert c.voters__voted(a1) is False
assert c.voters(a1)[2] is None # Voter.delegate
assert c.voters(a1)[3] == 0 # Voter.vote
assert c.voters(a1)[1] is False # Voter.voted
# Chairperson can give themselves the right to vote
c.giveRightToVote(a0, transact={})
# Check chairperson has weight of 1
assert c.voters__weight(a0) == 1
assert c.voters(a0)[0] == 1 # Voter.weight
# Check voter_acount is 2
assert c.voterCount() == 2
# Check several giving rights to vote
Expand All @@ -55,7 +55,7 @@ def test_give_the_right_to_vote(w3, c, assert_tx_failed):
# Check chairperson cannot give the right to vote twice to the same voter
assert_tx_failed(lambda: c.giveRightToVote(a5, transact={}))
# Check voters weight didn't change
assert c.voters__weight(a5) == 1
assert c.voters(a5)[0] == 1 # Voter.weight


def test_forward_weight(w3, c):
Expand All @@ -77,51 +77,51 @@ def test_forward_weight(w3, c):
# a1(0) -> a2(2) a3(1)
c.delegate(a3, transact={"from": a2})
# a1(0) -> a2(0) -> a3(3)
assert c.voters__weight(a1) == 0
assert c.voters__weight(a2) == 0
assert c.voters__weight(a3) == 3
assert c.voters(a1)[0] == 0 # Voter.weight
assert c.voters(a2)[0] == 0 # Voter.weight
assert c.voters(a3)[0] == 3 # Voter.weight

c.delegate(a9, transact={"from": a8})
# a7(1) a8(0) -> a9(2)
c.delegate(a8, transact={"from": a7})
# a7(0) -> a8(0) -> a9(3)
assert c.voters__weight(a7) == 0
assert c.voters__weight(a8) == 0
assert c.voters__weight(a9) == 3
assert c.voters(a7)[0] == 0 # Voter.weight
assert c.voters(a8)[0] == 0 # Voter.weight
assert c.voters(a9)[0] == 3 # Voter.weight
c.delegate(a7, transact={"from": a6})
c.delegate(a6, transact={"from": a5})
c.delegate(a5, transact={"from": a4})
# a4(0) -> a5(0) -> a6(0) -> a7(0) -> a8(0) -> a9(6)
assert c.voters__weight(a9) == 6
assert c.voters__weight(a8) == 0
assert c.voters(a9)[0] == 6 # Voter.weight
assert c.voters(a8)[0] == 0 # Voter.weight

# a3(3) a4(0) -> a5(0) -> a6(0) -> a7(0) -> a8(0) -> a9(6)
c.delegate(a4, transact={"from": a3})
# a3(0) -> a4(0) -> a5(0) -> a6(0) -> a7(0) -> a8(3) -> a9(6)
# a3's vote weight of 3 only makes it to a8 in the delegation chain:
assert c.voters__weight(a8) == 3
assert c.voters__weight(a9) == 6
assert c.voters(a8)[0] == 3 # Voter.weight
assert c.voters(a9)[0] == 6 # Voter.weight

# call forward_weight again to move the vote weight the
# rest of the way:
c.forwardWeight(a8, transact={})
# a3(0) -> a4(0) -> a5(0) -> a6(0) -> a7(0) -> a8(0) -> a9(9)
assert c.voters__weight(a8) == 0
assert c.voters__weight(a9) == 9
assert c.voters(a8)[0] == 0 # Voter.weight
assert c.voters(a9)[0] == 9 # Voter.weight

# a0(1) -> a1(0) -> a2(0) -> a3(0) -> a4(0) -> a5(0) -> a6(0) -> a7(0) -> a8(0) -> a9(9)
c.delegate(a1, transact={"from": a0})
# a0's vote weight of 1 only makes it to a5 in the delegation chain:
# a0(0) -> a1(0) -> a2(0) -> a3(0) -> a4(0) -> a5(1) -> a6(0) -> a7(0) -> a8(0) -> a9(9)
assert c.voters__weight(a5) == 1
assert c.voters__weight(a9) == 9
assert c.voters(a5)[0] == 1 # Voter.weight
assert c.voters(a9)[0] == 9 # Voter.weight

# once again call forward_weight to move the vote weight the
# rest of the way:
c.forwardWeight(a5, transact={})
# a0(0) -> a1(0) -> a2(0) -> a3(0) -> a4(0) -> a5(0) -> a6(0) -> a7(0) -> a8(0) -> a9(10)
assert c.voters__weight(a5) == 0
assert c.voters__weight(a9) == 10
assert c.voters(a5)[0] == 0 # Voter.weight
assert c.voters(a9)[0] == 10 # Voter.weight


def test_block_short_cycle(w3, c, assert_tx_failed):
Expand Down Expand Up @@ -154,15 +154,15 @@ def test_delegate(w3, c, assert_tx_failed):
c.giveRightToVote(a2, transact={})
c.giveRightToVote(a3, transact={})
# Voter's weight is 1
assert c.voters__weight(a1) == 1
assert c.voters(a1)[0] == 1 # Voter.weight
# Voter can delegate: a1 -> a0
c.delegate(a0, transact={"from": a1})
# Voter's weight is now 0
assert c.voters__weight(a1) == 0
assert c.voters(a1)[0] == 0 # Voter.weight
# Voter has voted
assert c.voters__voted(a1) is True
assert c.voters(a1)[1] is True # Voter.voted
# Delegate's weight is 2
assert c.voters__weight(a0) == 2
assert c.voters(a0)[0] == 2 # Voter.weight
# Voter cannot delegate twice
assert_tx_failed(lambda: c.delegate(a2, transact={"from": a1}))
# Voter cannot delegate to themselves
Expand All @@ -174,7 +174,7 @@ def test_delegate(w3, c, assert_tx_failed):
# a3 -> a1 -> a0
c.delegate(a1, transact={"from": a3})
# Delegate's weight is 3
assert c.voters__weight(a0) == 3
assert c.voters(a0)[0] == 3 # Voter.weight


def test_vote(w3, c, assert_tx_failed):
Expand All @@ -192,7 +192,7 @@ def test_vote(w3, c, assert_tx_failed):
# Voter can vote
c.vote(0, transact={})
# Vote count changes based on voters weight
assert c.proposals__voteCount(0) == 3
assert c.proposals(0)[1] == 3 # Proposal.voteCount
# Voter cannot vote twice
assert_tx_failed(lambda: c.vote(0))
# Voter cannot vote if they've delegated
Expand All @@ -202,7 +202,7 @@ def test_vote(w3, c, assert_tx_failed):
c.vote(1, transact={"from": a2})
c.vote(1, transact={"from": a5})
c.vote(1, transact={"from": a6})
assert c.proposals__voteCount(1) == 4
assert c.proposals(1)[1] == 4 # Proposal.voteCount
# Can't vote on a non-proposal
assert_tx_failed(lambda: c.vote(2, transact={"from": a7}))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ def test_bad_code_struct_exc(assert_compile_failed, get_contract_with_gas_estima
assert_compile_failed(lambda: get_contract_with_gas_estimation(bad_code), ArgumentException)


def test_external__value_arg_without_return(w3, get_contract_with_gas_estimation):
def test_external_value_arg_without_return(w3, get_contract_with_gas_estimation):
contract_1 = """
@payable
@public
Expand Down
18 changes: 12 additions & 6 deletions tests/parser/globals/test_getters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import pytest


def test_state_accessor(get_contract_with_gas_estimation_for_constants):
state_accessor = """
y: HashMap[int128, int128]
Expand All @@ -17,6 +20,9 @@ def foo() -> int128:
assert c.foo() == 5


# TODO: Either wait for this, or refactor test suite to use Brownie
# (which doesn't suffer from this issue)
@pytest.mark.xfail(reason="https://github.com/ethereum/web3.py/issues/1634#issuecomment-650797252")
def test_getter_code(get_contract_with_gas_estimation_for_constants):
getter_code = """
struct W:
Expand Down Expand Up @@ -48,9 +54,9 @@ def __init__():
assert c.x() == 7
assert c.y(1) == 9
assert c.z() == b"cow"
assert c.w__a(1) == 11
assert c.w__b(1, 2) == 13
assert c.w__c(1) == b"horse"
assert c.w__e(2, 1, 2) == 17
assert c.w__f(3) == 750
assert c.w__g(3) == 751
assert c.w(1)[0] == 11 # W.a
assert c.w(1)[1][2] == 13 # W.b[2]
assert c.w(1)[2] == b"horse" # W.c
assert c.w(2)[3][1][2] == 17 # W.e[1][2]
assert c.w(3)[4] == 750 # W.f
assert c.w(3)[5] == 751 # W.g
10 changes: 2 additions & 8 deletions vyper/parser/global_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,9 @@ def _mk_getter_helper(cls, typ, depth=0):
)
)
return o
# Struct type: for each member variable, make a separate getter, extend
# its function name with the name of the variable, do not add input
# arguments, add a member access to the return statement
# Struct type: return type is just the abi-encoded struct
elif isinstance(typ, StructType):
o = []
for k, v in typ.members.items():
for funname, head, tail, base in cls._mk_getter_helper(v, depth):
o.append(("__" + k + funname, head, "." + k + tail, base))
return o
return [("", "", "", typ.name)]
else:
raise Exception("Unexpected type")

Expand Down