如何在松露中測試從零地址發送交易
我有一些具有此功能的智能合約:
function someFunction() external returns (uint256) { require(msg.sender != address(0), "zero address"); //some code.... return 10; }
然後我執行我的測試它停止工作
const {constants,expectRevert,BN} = require('@openzeppelin/test-helpers'); //setting up .... it("Must fail if caller has zero address", async () => { await expectRevert(myContract.someFunction({from:constants.ZERO_ADDRESS}),"zero address") })
並返回此錯誤
UnhandledRejections 檢測到 Promise { 錯誤:nonce 生成函式失敗或私鑰 在 Transaction.sign 處無效 (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\ utils\transaction.js:292:1) 在 Transaction.exports.modules.564403.FakeTransaction.hash (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core \node_modules\ethereumjs-tx\dist\fake.js:63:1) ature.js:11:1) 在 Transaction.fakeHash
$$ as hash $$(C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\utils\transaction.js:18:1) nsaction.js:230:1)
在 RuntimeError.combine (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\utils\runtimeerror.js:42:1) 在新的 RuntimeError (C :\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\utils\runtimeerror.js:19:1) \fake.js:63:1) 在函式.RuntimeError.fromResults (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\utils\runtimeerror.js:94:1) 在 C:\Users \Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\blockchain_double.js:568:1 { uncaught: true } } 錯誤:nonce 生成函式失敗或私鑰無效在 Object.exports.sign (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\secp256k1\lib\elliptic\index.js:207:1) 在 Object.在 Object.exports.ecsign (C:\Users\ Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\node_modules\ethereumjs-tx\node_modules\ethereumjs-util\dist\signature.js:11:1)在 Transaction.exports。 modules.994857.Transaction.sign (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\node_modules\ethereumjs-tx\dist\transaction.js:230: 1) ure.js:11:1) 在 Transaction.sign (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\utils\transaction.js :292:1) action.js:230:1)\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\node_modules\ethereumjs-tx\node_modules\ethereumjs-util\dist\signature.js:11:1)在交易.exports.modules.994857.Transaction.sign (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\node_modules\ethereumjs-tx\dist\transaction.js :230:1) ure.js:11:1) 在 Transaction.sign (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\utils\ transaction.js:292:1) action.js:230:1)\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\node_modules\ethereumjs-tx\node_modules\ethereumjs-util\dist\signature.js:11:1)在交易.exports.modules.994857.Transaction.sign (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\node_modules\ethereumjs-tx\dist\transaction.js :230:1) ure.js:11:1) 在 Transaction.sign (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\utils\ transaction.js:292:1) action.js:230:1)\node_modules\ganache-core\node_modules\ethereumjs-tx\dist\transaction.js:230:1) ure.js:11:1) 在 Transaction.sign (C:\Users\Acer\AppData\Roaming\npm\node_modules \truffle\build\webpack:\node_modules\ganache-core\lib\utils\transaction.js:292:1) action.js:230:1)\node_modules\ganache-core\node_modules\ethereumjs-tx\dist\transaction.js:230:1) ure.js:11:1) 在 Transaction.sign (C:\Users\Acer\AppData\Roaming\npm\node_modules \truffle\build\webpack:\node_modules\ganache-core\lib\utils\transaction.js:292:1) action.js:230:1)
在 Transaction.exports.modules.564403.FakeTransaction.hash (C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\node_modules\ethereumjs-tx\dist\fake .js:63:1) ake.js:63:1) 在 Transaction.fakeHash$$ as hash $$(C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\utils\transaction.js:18:1) 在 RuntimeError.combine (C:\Users \Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\utils\runtimeerror.js:42:1) 在新的 RuntimeError (C:\Users\Acer\AppData\Roaming \npm\node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\utils\runtimeerror.js:19:1) 在 Function.RuntimeError.fromResults (C:\Users\Acer\AppData\Roaming\npm\ node_modules\truffle\build\webpack:\node_modules\ganache-core\lib\utils\runtimeerror.js:94:1) 在 C:\Users\Acer\AppData\Roaming\npm\node_modules\truffle\build\webpack:\ node_modules\ganache-core\lib\blockchain_double.js:568:1 { 未擷取:真 }
我使用solidity-coverage進行測試。
是否可以檢查從零地址發送的交易?
我該如何解決這個問題?
沒有人可以有零地址。更準確地說,給定地址,不可能找到與該地址對應的私鑰。如果有人能做到,那麼整個世界將不僅是區塊鏈,而且我們所知道的整個現代社會都會崩潰。
所以你不需要檢查零地址,因為沒有人可以擁有它。零地址只是人們把錢扔給它的地方。這就像一個全世界的垃圾場,但垃圾就是金錢