ApeWorX / ape

The smart contract development tool for Pythonistas, Data Scientists, and Security Professionals
https://apeworx.io
Apache License 2.0
845 stars 124 forks source link

Ape version 0.8 got `Reason: Missing kwargs: ['nonce']` when making transactions in tests #2115

Closed arjunaskykok closed 2 weeks ago

arjunaskykok commented 3 weeks ago

Environment information

What went wrong?

Here's how to reproduce.

$ python3 -m venv .venv8
(.venv8) $ pip install eth-ape'[recommended-plugins]'
(.venv8) $ mkdir ape_nonce; cd ape_nonce
(.venv8) $ ape init
(.venv8) $ cat contracts/Nonce.vy
# @version ^0.3.0

struct Voter:
    weight: uint256
    voted: bool
    vote: uint256

struct Proposal:
    name: String[100]
    voteCount: uint256

voters: public(HashMap[address, Voter])
proposals: public(HashMap[uint256, Proposal])
voterCount: public(uint256)
chairperson: public(address)
amountProposals: public(uint256)

MAX_NUM_PROPOSALS: constant(uint256) = 3

@external
def __init__():
    self.chairperson = msg.sender

@external
def addProposal(_proposalName: String[100]):
    assert msg.sender == self.chairperson
    i: uint256 = self.amountProposals
    self.proposals[i] = Proposal({
        name: _proposalName,
        voteCount: 0
    })
    self.amountProposals += 1

@external
def giveRightToVote(voter: address, _weight: uint256):
    assert msg.sender == self.chairperson
    assert not self.voters[voter].voted
    assert self.voters[voter].weight == 0
    self.voters[voter].weight = _weight
    self.voterCount += 1

(.venv8) $ cat tests/conftest.py
import pytest

@pytest.fixture
def deployer(accounts):
    return accounts[0]

@pytest.fixture
def contract(deployer, project):
    return deployer.deploy(project.Nonce)
(.venv8) $ cat tests/test_nonce.py
import pytest
from ape.exceptions import ContractLogicError

def test_giveRightToVote(contract, deployer, accounts):
    user = accounts[1]
    assert contract.voterCount() == 0
    assert contract.voters(user).weight == 0
    contract.giveRightToVote(user, 1, sender=deployer)
    assert contract.voterCount() == 1
    assert contract.voters(user).weight == 1

    power_user = accounts[2]
    assert contract.voters(power_user).weight == 0
    contract.giveRightToVote(power_user, 9, sender=deployer)
    assert contract.voterCount() == 2
    assert contract.voters(power_user).weight == 9
(.venv8) $ ape --version
0.8.2
(.venv8) $ ape compile
SUCCESS: 'local project' compiled.
(.venv8) $ ape test
============================================= test session starts =============================================
platform linux -- Python 3.10.12, pytest-8.2.1, pluggy-1.5.0
rootdir: /home/ubuntu/Code/python/ape/ape_nonce
plugins: eth-ape-0.8.2, web3-6.19.0
collected 1 item

tests/test_nonce.py F                                                                                   [100%]

================================================== FAILURES ===================================================
____________________________________________ test_giveRightToVote _____________________________________________
/home/ubuntu/Code/python/ape/ape_nonce/tests/test_nonce.py:10: in test_giveRightToVote
    contract.giveRightToVote(user, 1, sender=deployer)
E   ape.exceptions.ProviderError: Unable to create CallTreeNode. Reason: Missing kwargs: ['nonce']
        accounts   = [0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266, 0x70997970C51812dc3A010C7d01b50e0d17dc79C8, 0x3C44CdDdB6a900fa2b585dd299e...a2C08b23698B3D3cc7Ca32193d9955, 0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f, 0xa0Ee7A142d267C1f36714E4a8F75612F20a79720]
        contract   = <Nonce 0x5FbDB2315678afecb367f032d93F642f64180aa3>
        deployer   = <TestAccount 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266>
        user       = <TestAccount 0x70997970C51812dc3A010C7d01b50e0d17dc79C8>
-------------------------------------------- Captured stdout setup --------------------------------------------
INFO: Confirmed 0x9048d4f3010e20e3056379e81588a29c3ed29d5957c13f046a05242c3810e2e4 (total fees paid = 192123750000000)
INFO: Confirmed 0x9048d4f3010e20e3056379e81588a29c3ed29d5957c13f046a05242c3810e2e4 (total fees paid = 192123750000000)
SUCCESS: Contract 'Nonce' deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
--------------------------------------------- Captured log setup ----------------------------------------------
INFO     ape:provider.py:955 Confirmed 0x9048d4f3010e20e3056379e81588a29c3ed29d5957c13f046a05242c3810e2e4 (total fees paid = 192123750000000)
INFO     ape:provider.py:955 Confirmed 0x9048d4f3010e20e3056379e81588a29c3ed29d5957c13f046a05242c3810e2e4 (total fees paid = 192123750000000)
SUCCESS  ape:accounts.py:278 Contract 'Nonce' deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
=========================================== short test summary info ===========================================
FAILED tests/test_nonce.py::test_giveRightToVote - ape.exceptions.ProviderError: Unable to create CallTreeNode. Reason: Missing kwargs: ['nonce']
============================================== 1 failed in 0.62s ==============================================

But Ape version 0.7 is fine.

(.venv8) $ deactivate
$ python3 -m venv .venv
$ source .venv/bin/activate
(.venv) $ pip install eth-ape'[recommended-plugins]'==0.7
(.venv) $ ape compile
INFO: Compiling 'Nonce.vy'.
(.venv) $ ape test
============================================= test session starts =============================================
platform linux -- Python 3.10.12, pytest-7.4.4, pluggy-1.5.0
rootdir: /home/ubuntu/Code/python/ape/ape_nonce
plugins: eth-ape-0.7.0, web3-6.16.0
collected 1 item

tests/test_nonce.py .                                                                                   [100%]

============================================== 1 passed in 0.56s ==============================================

Thank you!

linear[bot] commented 3 weeks ago

APE-1753 Ape version 0.8 got `Reason: Missing kwargs: ['nonce']` when making transactions in tests

fubuloubu commented 3 weeks ago

Just to verify, renaming Nonce.vy to something else doesn't change the behavior does it?

arjunaskykok commented 3 weeks ago

Just to verify, renaming Nonce.vy to something else doesn't change the behavior does it?

Nope. The original example is coming from here: https://github.com/PacktPublishing/Hands-On-Blockchain-for-Python-Developers--2nd-Edition/tree/main/chapter_6/voting_app

That's not the only one. Another example is: https://github.com/PacktPublishing/Hands-On-Blockchain-for-Python-Developers--2nd-Edition/tree/main/chapter_16/dex

Many code for my upcoming book are broken with Ape 0.8.

antazoey commented 3 weeks ago

What network / provider are you using?

arjunaskykok commented 3 weeks ago

What network / provider are you using?

I was running the test when I got the error.

(.venv8) $ ape test

What is the provider being used when we're running the test? Don't know. EthereumTester (eth-tester) perhaps?

antazoey commented 3 weeks ago

What is the provider being used when we're running the test? Don't know. EthereumTester (eth-tester) perhaps?

eth-tester is the default but most projects these days use ape-foundry or ape-hardhat which offers better tracing support needed for coverage and stuff.

Nonetheless, thank you! This is the info I need to help repro and fix the problem(s). ◡̈

antazoey commented 2 weeks ago

I have a fix incoming for this!

arjunaskykok commented 2 weeks ago

Thanks!