Macbook Pro:蒙特雷
英特尔酷睿 i7
布朗尼 v1.17.2
Ganache CLI v6.12.2 (ganache-core: 2.13.2)
我正在根据参考(https://www.youtube.com/watch?v=M576WGiDBdQ&t=25510s)学习可靠性。
我使用 brownie 框架编写并部署了一个智能合约(scripts/deploy.py),它成功了。
然后尝试编写一个测试脚本(tests/test_simple_storage.py),在终端给我错误信息。
google了一下,尝试删除brownie项目的build文件夹中的所有文件,再次部署合约(scripts/deploy.py),还是同样的错误。
并尝试更改私钥和索引,对于accounts[0]到account[-1]和account[1],同样的错误结果。
仅供参考:我在终端中输入了命令“ganache-cli —deterministic”。所以账户和私钥不是随机的。
将 Ganache 的帐户 [0] 的私钥保存在 .env 文件中,就像这样
.env
export PRIVATE_KEY=0x91114a07f248a1c50951cb11557af5424cc6a49bf61521874c9ae3f4ae239a6d
错误信息:</p>
(base) liwei@liweideMacBook-Pro Brownie_Simple_Storage % brownie test
Brownie v1.17.2 - Python development framework for Ethereum
========================================================== test session starts ===========================================================
platform darwin -- Python 3.8.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /Users/liwei/Desktop/demos/practice/Brownie_Simple_Storage
plugins: eth-brownie-1.17.2, xdist-1.34.0, hypothesis-6.27.3, web3-5.25.0, forked-1.3.0
collected 0 items / 1 error
================================================================= ERRORS =================================================================
_____________________________________________ ERROR collecting tests/test_simple_storage.py ______________________________________________
tests/test_simple_storage.py:5: in <module>
account = accounts[0]
E IndexError: list index out of range
======================================================== short test summary info =========================================================
FAILED tests/test_simple_storage.py - IndexError: list index out of range
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================ 1 error in 0.25s ============================================================
(base) liwei@liweideMacBook-Pro Brownie_Simple_Storage %
测试脚本--“tests/test_simple_storage.py”</p>
from brownie import accounts, SimpleStorage
# Arrange
account = accounts[0]
print(account)
# Act
simple_storage = SimpleStorage.deploy({"from": account})
starting_value = simple_storage.retrieve()
expected = 0
# Assert
assert starting_value == expected
智能合约部署良好,使用相同的本地区块链,ganache-cli “scripts/deploy.py”</p>
from brownie import accounts, config, SimpleStorage
import os
def deploy_simple_storage():
# load from you set encrypted , not from ganache-cli which is brownie automated connceted to
# account = accounts.load("MG515-account")
# print(account)
# add private key use enviroment variables
# account = accounts.add(os.getenv("PRIVATE_KEY"))
# print(account)
# .deploy() , always need a "from"key in a dictinary when making a transaction
account = accounts.add(config["wallets"]["from_key"])
simple_storage = SimpleStorage.deploy({"from": account})
stored_value = simple_storage.retrieve()
print("Current stored value is :")
print(stored_value)
print("Updating Contract...")
transaction = simple_storage.store(15, {"from": account})
transaction.wait(1)
updated_store_value = simple_storage.retrieve()
print("Current stored value is :")
print(updated_store_value)
print(account)
def main():
deploy_simple_storage()
更新于 20220111
阅读 brownie 文档后找到这个命令“network.connect("development")”,这个命令设置网络正确吗,我记得 brownie 应该默认连接到开发网络。
所以更新了这样的代码
from brownie import accounts, SimpleStorage, network
network.connect("development")
# Arrange
account = accounts[0]
print(accounts)
# Act
simple_storage = SimpleStorage.deploy({"from": account})
starting_value = simple_storage.retrieve()
expected = 0
# Assert
assert starting_value == expected
终端给出一条警告信息
(base) liwei@liweideMacBook-Pro Brownie_Simple_Storage % brownie test
Brownie v1.17.2 - Python development framework for Ethereum
========================================================== test session starts ==========================================================
platform darwin -- Python 3.8.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /Users/liwei/Desktop/demos/practice/Brownie_Simple_Storage
plugins: eth-brownie-1.17.2, xdist-1.34.0, hypothesis-6.27.3, web3-5.25.0, forked-1.3.0
collected 0 items
=========================================================== warnings summary ============================================================
../../../../.local/pipx/venvs/eth-brownie/lib/python3.8/site-packages/brownie/network/main.py:44
/Users/liwei/.local/pipx/venvs/eth-brownie/lib/python3.8/site-packages/brownie/network/main.py:44: BrownieEnvironmentWarning: Development network has a block height of 2
warnings.warn(
-- Docs: https://docs.pytest.org/en/stable/warnings.html
========================================================== 1 warning in 0.49s ===========================================================