Solidity
調試在映射中註冊
我有一份名為 Registry 的契約
pragma solidity ^0.4.11; contract Registry { mapping(bytes32 => address) public ID; mapping(address => address) public Contract; function register(bytes32 _id) { ID[_id] = tx.origin; Contract[tx.origin] = msg.sender; } function getPublicAddress(bytes32 _id) constant returns (address) { return (ID[_id]); } function getContractAddress(bytes32 _id) constant returns (address) { return Contract[ID[_id]]; } }
另一個叫使用者
contract User { address owner; address reg; Registry registry = Registry(reg); // Register in the system function register(bytes32 _id) { registry.register(_id); } function getContractAddress(bytes32 _id) { registry.getContractAddress(_id); } function getPublicAddress(bytes32 _id) { registry.getPublicAddress(_id); } }
,和不起作用,我不明白為什麼
registry()
。getPublicAddress()``getContractAddress()
我嘗試使用 Truffle 進行測試,但遇到了一個目前尚未解決的已知錯誤。我在這裡問過,GitHub上的討論在這裡。
我嘗試在我的私有區塊鏈中執行它,我有以下程式碼
register: function(publicAddress, contractAddress, _id) { // Getting that user's instance of the contract const contract = contractInstance("User", contractAddress); // Interaction with the contract contract.register(web3.fromAscii(_id), {from: publicAddress}, (err, res) => { // Log transaction to explore if (err) { console.log(err); } else { console.log('tx: ' + res); helpers.addTransaction(publicAddress, res); } }); }, getContractAddress: function(contractAddress, _id) { const contract = contractInstance("User", contractAddress); contract.getContractAddress.call(web3.fromAscii(_id), (err, res) => { if (err) { console.log(err); } else { console.log(res); } }) }, getPublicAddress: function(contractAddress, _id) { const contract = contractInstance("User", contractAddress); contract.getPublicAddress.call(web3.fromAscii(_id), (err, res) => { if (err) { console.log(err); } else { console.log(res); } }) }
註冊(和探勘)後,我就回來了
[]
打電話時
getPublicAddress()
或getContractAddress()
。誰能看出問題出在哪裡?
你的合約有很多bug,如下:
reg
參數,沒有辦法設置它。reg 的預設值為零,因此呼叫方法時不會呼叫註冊合約getPublicAddress
。getPublicAddress
和getContractAddress
方法沒有返回值。所以我修改了你的契約並在remix中測試一切正常,契約如下:
pragma solidity ^0.4.11; contract Registry { mapping(bytes32 => address) public ID; mapping(address => address) public Contract; event log(bytes32,address); function register(bytes32 _id) { ID[_id] = tx.origin; Contract[tx.origin] = msg.sender; } function getPublicAddress(bytes32 _id) constant returns (address) { log(_id,ID[_id]); return (ID[_id]); } function getContractAddress(bytes32 _id) constant returns (address) { log(_id,Contract[ID[_id]]); return Contract[ID[_id]]; } } contract User { address owner; Registry registry; // Register in the system function register(bytes32 _id) { registry.register(_id); } function getContractAddress(bytes32 _id) constant returns (address){ return registry.getContractAddress(_id); } function getPublicAddress(bytes32 _id) constant returns (address) { return registry.getPublicAddress(_id); } function setRegister(address _addr){ registry = Registry(_addr); } }
首先,你應該部署註冊合約,然後部署使用者合約,現在複製註冊合約地址來呼叫
setRegister
方法,探勘和測試。結果是希望有幫助~