参考

准备工作

  • 合约代码
    • 该合约演示了文件所有权账本,记录了文件及文件拥有者对应关系,文件唯一性由其hash值确定
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//proof.sol
contract Proof
{
    struct FileDetails
    {
        uint timestamp;
        string owner;
    }

    mapping (string => FileDetails) files;

    event logFileAddedStatus(bool status, uint timestamp, string owner, string fileHash);

    // to store the owner of file at the block timestamp
    function set(string owner, string fileHash)
    {
        // if filehash not exists then set it
        if(files[fileHash].timestamp == 0)
        {
            files[fileHash] = FileDetails(block.timestamp, owner);
            // we are triggering an event so that the frontend of our app knows
            // that the file's existence and ownership details have been stored
             logFileAddedStatus(true, block.timestamp, owner, fileHash);
        }
        else
        {
            // file's details has already been stored earlier
            logFileAddedStatus(false, block.timestamp, owner, fileHash);
        }
    }

    // get file information
    function get(string fileHash) returns (uint timestamp, string owner)
    {
        return (files[fileHash].timestamp, files[fileHash].owner);
    }

}
  • 编译
    • 打开solidity在线编译工具 remix,新建文件,贴入以上代码
    • 点击Start to compile
    • 编译完成后,点击Details获得合约部署代码WEB3DEPLOY,如下
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
var proofContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"fileHash","type":"string"}],"name":"get","outputs":[{"name":"timestamp","type":"uint256"},{"name":"owner","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"owner","type":"string"},{"name":"fileHash","type":"string"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"status","type":"bool"},{"indexed":false,"name":"timestamp","type":"uint256"},{"indexed":false,"name":"owner","type":"string"},{"indexed":false,"name":"fileHash","type":"string"}],"name":"logFileAddedStatus","type":"event"}]);
var proof = proofContract.new(
   {
     from: web3.eth.accounts[0],
     data: '0x608060405234801561001057600080fd5b506107a6806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063693ec85e14610051578063e942b5161461013a575b600080fd5b34801561005d57600080fd5b506100b8600480360381019080803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091929192905050506101e9565b6040518083815260200180602001828103825283818151815260200191508051906020019080838360005b838110156100fe5780820151818401526020810190506100e3565b50505050905090810190601f16801561012b5780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b34801561014657600080fd5b506101e7600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919291929050505061036f565b005b600060606000836040518082805190602001908083835b6020831015156102255780518252602082019150602081019050602083039250610200565b6001836020036101000a0380198251168184511680821785525050505050509050019150509081526020016040518091039020600001546000846040518082805190602001908083835b602083101515610294578051825260208201915060208101905060208303925061026f565b6001836020036101000a0380198251168184511680821785525050505050509050019150509081526020016040518091039020600101808054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561035f5780601f106103345761010080835404028352916020019161035f565b820191906000526020600020905b81548152906001019060200180831161034257829003601f168201915b5050505050905091509150915091565b600080826040518082805190602001908083835b6020831015156103a85780518252602082019150602081019050602083039250610383565b6001836020036101000a03801982511681845116808217855250505050505090500191505090815260200160405180910390206000015414156105b2576040805190810160405280428152602001838152506000826040518082805190602001908083835b602083101515610432578051825260208201915060208101905060208303925061040d565b6001836020036101000a038019825116818451168082178552505050505050905001915050908152602001604051809103902060008201518160000155602082015181600101908051906020019061048b9291906106d5565b509050507f0d3bbc3c02da6ed436712ca1a0f626f1269df703a105f034e4637c7b10fb7ba5600142848460405180851515151581526020018481526020018060200180602001838103835285818151815260200191508051906020019080838360005b838110156105095780820151818401526020810190506104ee565b50505050905090810190601f1680156105365780820380516001836020036101000a031916815260200191505b50838103825284818151815260200191508051906020019080838360005b8381101561056f578082015181840152602081019050610554565b50505050905090810190601f16801561059c5780820380516001836020036101000a031916815260200191505b50965050505050505060405180910390a16106d1565b7f0d3bbc3c02da6ed436712ca1a0f626f1269df703a105f034e4637c7b10fb7ba5600042848460405180851515151581526020018481526020018060200180602001838103835285818151815260200191508051906020019080838360005b8381101561062c578082015181840152602081019050610611565b50505050905090810190601f1680156106595780820380516001836020036101000a031916815260200191505b50838103825284818151815260200191508051906020019080838360005b83811015610692578082015181840152602081019050610677565b50505050905090810190601f1680156106bf5780820380516001836020036101000a031916815260200191505b50965050505050505060405180910390a15b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061071657805160ff1916838001178555610744565b82800160010185558215610744579182015b82811115610743578251825591602001919060010190610728565b5b5090506107519190610755565b5090565b61077791905b8082111561077357600081600090555060010161075b565b5090565b905600a165627a7a723058200baa1bec93ab158dfff9d2175f4f0dc385d4a1d0d2be98e2eb9321011b1757540029',
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })

部署合约

  • 启动console,并开启挖矿,解锁账户(需要输入密码),启用rpc服务
1
./geth --datadir data --mine --minerthreads=1 --rpc --unlock=0
  • 打开另外一个终端,连接到上一步的console
1
./geth attach ipc:data/geth.ipc
  • 在第2个终端,即attach终端的js console,输入合约编译后的代码,回车。忽略null,undefined提示,合约被提交,等待合约被挖出
  • 按照之前的配置difficulty=40000,1秒钟之内合约就会确认,消息如下。 获得的合约地址为0x0ea09543aff64c317ba2f103156591a0a9e4b2f4,以下用${ContractAddr}替代
1
Contract mined! address: 0x0ea09543aff64c317ba2f103156591a0a9e4b2f4 transactionHash: 0x7d3eb0d8ae1a12efb644ca85076d690fa7a8802161d9efc8eadff21790baf1e1

使用合约

  • 下载示例代码 Building-Blockchain-Projects,代码用nodejs编写,需要自己学习搭建nodejs环境
    • 修改Chapter04/Final/app.js文件,将下面一行的合约地址替换为上面得到的地址
    • 参考下面代码运行nodejs项目
    • 打开http://localhost:8080/,可以看到页面,尝试进行录入,查询操作
1
var proof = proofContract.at("${ContractAddr}");
1
2
3
4
git clone https://github.com/PacktPublishing/Building-Blockchain-Projects.git
cd Building-Blockchain-Projects/Chapter04/Final
npm install
node app.js
  • 操作完成后,关闭所有终端

持续学习中,理解可能有误,敬请谅解

版权所有,转载请注明来源