从零构建以太坊(Ethereum)智能合约到项目实战——第20章 搭建自己的私有链网络
P75 、1-以太坊私网建立 、合约编译、部署完全教程(1)
使用此博文进行安装配置:https://blog.csdn.net/w88193363/article/details/79402074
一、搭建私有链所需的依赖环境
操作系统:ubuntu16.04,开虚拟机的话要至少2G,否则会影响测试挖矿时的速度
软件:geth客户端 版本:1.8.1-stable-1e67410e
二、安装geth客户端
【Ctrl+Alt+T】打开终端,依次输入以下命令:
sudo apt-get update sudo apt-get install software-properties-common sudo add-apt-repository -y ppa:ethereum/ethereum sudo add-apt-repository -y ppa:ethereum/ethereum-dev sudo apt-get update sudo apt-get install ethereum
安装完成后,输入geth -h,如果有列出信息则说明安装成功,可查看列出信息,方便后续的操作
三、建立私链:
1、创建一个文件夹来存储你的私链数据
mkdir privatechain
cd privatechain
2、使用geth来加载
geth --networkid --dev --datadir data --rpc --rpcaddr 192.168.2.223 --rpcport --port 3000 console
各选项含义如下:
--identity:
指定节点 ID;--rpc:
表示开启 HTTP-RPC 服务;--rpcaddr:
HTTP-RPC 服务ip地址;--rpcport:
指定 HTTP-RPC 服务监听端口号(默认为 8545);--datadir:
指定区块链数据的存储位置;--port:
指定和其他节点连接所用的端口号(默认为 30303);--nodiscover:
关闭节点发现机制,防止加入有同样初始配置的陌生节点。
执行上面的命令,你应该能看到下面的信息:
INFO [10-15 | 03:14:50] IPC endpoint opened: /Users/liyuechun/Desktop/1015/privchain/geth.ipc |
INFO [10-15 | 03:14:50] HTTP endpoint opened: http://127.0.0.1:8545 |
为了方便操作,我们将上面的命令存为init.sh:
nano init.sh
//输入
geth --networkid --dev --datadir data --rpc --rpcaddr 192.168.31.140 --rpcport --port console //赋予执行权限
chmod init.sh
保持节点的运行,不要关闭终端,重新打开一个终端,使用geth attach
连接节点,并且打开geth console
geth attach ipc:data/geth.ipc
为了方便操作,我们将上面的命令存为connect.sh:
nano connect.sh //输入
geth attach ipc:data/geth.ipc //赋予执行权限
chmod connect.sh
这是一个交互式的 JavaScript 执行环境,在这里面可以执行 JavaScript 代码,其中 > 是命令提示符。在这个环境里也内置了一些用来操作以太坊的 JavaScript 对象,可以直接使用这些对象。这些对象主要包括:
eth:
包含一些跟操作区块链相关的方法;net:
包含一些查看p2p网络状态的方法;admin:
包含一些与管理节点相关的方法;miner:
包含启动&停止挖矿的一些方法;personal:
主要包含一些管理账户的方法;txpool:
包含一些查看交易内存池的方法;web3:
包含了以上对象,还包含一些单位换算的方法。
另外开启一个终端1
cd privatechain/data
geth attach ipc:geth.ipc
3、相关api命令
查看账户
personal.listAccounts
创建账户
personal.newAccount("密码")
4、web3命令
查看创世账户
web3.eth.coinbase
获取账户
personal.listAccounts[]
查看账户余额
eth.getBalance("账户地址")
//eth.getBalance(personal.listAccounts[0])
开始挖矿
miner.start();
停止挖矿
miner.stop();
终端1账户0向账户1转账
//解锁账户0(密码为空)
personal.unlockAccount(personal.listAccounts[]);
//以太币转wei
amount = web3.toWei(5,'ether');
//转账
eth.sendTransaction({from:personal.listAccounts[0],to:personal.listAccounts[1],value:amount});
//查看交易状态
txpool.status
//查看区块信息
eth.getBlock("pending",true);
//开始挖矿
miner.start();
//停止挖矿
miner.stop();
//查看交易状态
txpool.status
//查看账户1的余额
eth.getBalance(personal.listAccounts[1])
//转账成功!!!
//查看区块数
eth.blockNumber
//查看交易信息
eth.getTransaction("0xc9d079c276cb8d026705430d1f0c543d88d11e6058f09b17717c7dc7df222dcd")
//查看区块信息(区块号)
eth.getBlock(1)
5、mist钱包连接私链
Mist钱包下载地址:https://github.com/ethereum/mist/releases
Mist = Ethereum Wallet + Web3 浏览器
下载:Mist-linux64-0-11-1.zip
解压
unzip Mist-linux64---.zip /home/ethereum/Mist
使用mist链接到私链
"/home/ethereum/Mist/mist" --rpc "/home/ethereum/privatechain/init/data/geth.ipc"
为了方便操作,我们将上面的命令存为mist.sh:
nano mist.sh //输入
"/home/ethereum/Mist/mist" --rpc "/home/ethereum/privatechain/init/data/geth.ipc" //赋予执行权限
chmod mist.sh
打开即可
./mist.sh
6、编写智能合约
这里我们部署合约分两种情况:
- 命令行方式部署
- Mist钱包中部署
a、命令行方式部署
终端1编写合约
cd privatechain
nano DemeterContract.sol
输入以下内容,并保存:
pragma solidity ^0.5.; contract DemeterContract {
mapping (address => uint) balances;
event Transfer(address indexed _from, address indexed _to, uint256 _value); uint public ticketIndex;
mapping(address => mapping(uint => Ticket)) stores;
mapping(uint => address) ticketIdInStore; struct Ticket{
uint id; //票据id
string name; //票据名称 string content; //票据内容
address receiveraddr; //付款人地址
//address payeeaddr; //收款人地址
} constructor() public{
ticketIndex = ;
balances[tx.origin] = ;
} function sendCoin(address receiver, uint amount) public returns(bool sufficient) {
if (balances[msg.sender] < amount) return false;
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Transfer(msg.sender, receiver, amount);
return true;
} function getBalanceInEth(address addr) public view returns(uint){
return getBalance(addr);
} function getBalance(address addr) public view returns(uint) {
return balances[addr];
} /* 添加票据信息到区块链 */
function addTicketToStore(string memory _name,string memory _content, address _receiveraddr) public {
ticketIndex += ;
Ticket memory ticket = Ticket(ticketIndex, _name, _content, _receiveraddr);
stores[msg.sender][ticketIndex] = ticket;
ticketIdInStore[ticketIndex] = msg.sender;
} /* 添加票据信息到区块链 */
function addTicketToStoreByAddress(string memory _name,string memory _content, address _senderaddr, address _receiveraddr) public {
ticketIndex += ;
Ticket memory ticket = Ticket(ticketIndex, _name, _content, _receiveraddr);
stores[_senderaddr][ticketIndex] = ticket;
ticketIdInStore[ticketIndex] = _senderaddr;
} /* test */
function test(string memory _name, string memory _content, address _receiveraddr) public returns(uint,address,string memory,string memory) {
ticketIndex += ;
Ticket memory ticket = Ticket(ticketIndex, _name, _content, _receiveraddr);
stores[msg.sender][ticketIndex] = ticket;
ticketIdInStore[ticketIndex] = msg.sender; /**/
Ticket memory ticketback = stores[ticketIdInStore[ticketIndex]][ticketIndex]; return (ticketIndex,msg.sender,ticketback.name,ticketback.content);
} /* 通过票据id读取票据 */
function getTicket(uint _ticketId) public view returns(uint, string memory, string memory){
Ticket memory ticket = stores[ticketIdInStore[_ticketId]][_ticketId];
return (ticket.id, ticket.name, ticket.content);
} /* 通过票据id读取票据 */
function getTicketByByAddress(address searcher, uint _ticketId) public view returns(uint, string memory, string memory){
Ticket memory ticket = stores[ticketIdInStore[_ticketId]][_ticketId];
//ticketIdInStore[_ticketId]==searcher
if( ticketIdInStore[_ticketId] == searcher || ticket.receiveraddr == searcher )
return (ticket.id, ticket.name, ticket.content);
else
return (, "", "");
}
}
安装solc
参考:https://blog.csdn.net/u013137970/article/details/52255001、https://www.cnblogs.com/lidonghao/p/8497145.html
npm install -g solc
//npm install -g solc-cli
//检测是否安装成功
solcjs --help
//sudo add-apt-repository -y ppa:ethereum/ethereum
//sudo add-apt-repository -y ppa:ethereum/ethereum-dev
//sudo apt-get update //sudo apt-get install solc //检测是否安装成功:solc --help
终端2获取字节码
//使用solcjs获取合约的字节码
solcjs --bin DemeterContract.sol
//使用gedit打开 ,并复制字节码
gedit DemeterContract_sol_DemeterContract.bin
终端1定义字节码变量(在字节码前面加上 0x):
var bytecode = "0x608060405234801561001057600080fd5b5060006001819055506127106000803273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555061199d8061006d6000396000f3fe608060405234801561001057600080fd5b50600436106100b0576000357c010000000000000000000000000000000000000000000000000000000090048063c803932611610083578063c80393261461051e578063d252d3b314610690578063dc9460a8146107ca578063ea4a06081461095c578063f8b2cb4f1461097a576100b0565b806351bf46c0146100b55780637bd703e8146103465780637dc379fa1461039e57806390b98a11146104b8575b600080fd5b610225600480360360608110156100cb57600080fd5b81019080803590602001906401000000008111156100e857600080fd5b8201836020820111156100fa57600080fd5b8035906020019184600183028401116401000000008311171561011c57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192908035906020019064010000000081111561017f57600080fd5b82018360208201111561019157600080fd5b803590602001918460018302840111640100000000831117156101b357600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506109d2565b604051808581526020018473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018060200180602001838103835285818151815260200191508051906020019080838360005b838110156102a1578082015181840152602081019050610286565b50505050905090810190601f1680156102ce5780820380516001836020036101000a031916815260200191505b50838103825284818151815260200191508051906020019080838360005b838110156103075780820151818401526020810190506102ec565b50505050905090810190601f1680156103345780820380516001836020036101000a031916815260200191505b50965050505050505060405180910390f35b6103886004803603602081101561035c57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610dcc565b6040518082815260200191505060405180910390f35b6103ca600480360360208110156103b457600080fd5b8101908080359060200190929190505050610dde565b604051808481526020018060200180602001838103835285818151815260200191508051906020019080838360005b838110156104145780820151818401526020810190506103f9565b50505050905090810190601f1680156104415780820380516001836020036101000a031916815260200191505b50838103825284818151815260200191508051906020019080838360005b8381101561047a57808201518184015260208101905061045f565b50505050905090810190601f1680156104a75780820380516001836020036101000a031916815260200191505b509550505050505060405180910390f35b610504600480360360408110156104ce57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050611043565b604051808215151515815260200191505060405180910390f35b61068e6004803603606081101561053457600080fd5b810190808035906020019064010000000081111561055157600080fd5b82018360208201111561056357600080fd5b8035906020019184600183028401116401000000008311171561058557600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290803590602001906401000000008111156105e857600080fd5b8201836020820111156105fa57600080fd5b8035906020019184600183028401116401000000008311171561061c57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061119c565b005b6106dc600480360360408110156106a657600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019092919050505061132b565b604051808481526020018060200180602001838103835285818151815260200191508051906020019080838360005b8381101561072657808201518184015260208101905061070b565b50505050905090810190601f1680156107535780820380516001836020036101000a031916815260200191505b50838103825284818151815260200191508051906020019080838360005b8381101561078c578082015181840152602081019050610771565b50505050905090810190601f1680156107b95780820380516001836020036101000a031916815260200191505b509550505050505060405180910390f35b61095a600480360360808110156107e057600080fd5b81019080803590602001906401000000008111156107fd57600080fd5b82018360208201111561080f57600080fd5b8035906020019184600183028401116401000000008311171561083157600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192908035906020019064010000000081111561089457600080fd5b8201836020820111156108a657600080fd5b803590602001918460018302840111640100000000831117156108c857600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506116af565b005b61096461183f565b6040518082815260200191505060405180910390f35b6109bc6004803603602081101561099057600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050611845565b6040518082815260200191505060405180910390f35b600080606080600180600082825401925050819055506109f061188d565b60806040519081016040528060015481526020018981526020018881526020018773ffffffffffffffffffffffffffffffffffffffff16815250905080600260003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006001548152602001908152602001600020600082015181600001556020820151816001019080519060200190610aa59291906118cc565b506040820151816002019080519060200190610ac29291906118cc565b5060608201518160030160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055509050503360036000600154815260200190815260200160002060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550610b6961188d565b6002600060036000600154815260200190815260200160002060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600060015481526020019081526020016000206080604051908101604052908160008201548152602001600182018054600181600116156101000203166002900480601f016020809104026020016040519081016040528092919081815260200182805460018160011615610100020316600290048015610c9f5780601f10610c7457610100808354040283529160200191610c9f565b820191906000526020600020905b815481529060010190602001808311610c8257829003601f168201915b50505050508152602001600282018054600181600116156101000203166002900480601f016020809104026020016040519081016040528092919081815260200182805460018160011615610100020316600290048015610d415780601f10610d1657610100808354040283529160200191610d41565b820191906000526020600020905b815481529060010190602001808311610d2457829003601f168201915b505050505081526020016003820160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681525050905060015433826020015183604001518292508191508090509550955095509550505093509350935093565b6000610dd782611845565b9050919050565b6000606080610deb61188d565b600260006003600088815260200190815260200160002060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008681526020019081526020016000206080604051908101604052908160008201548152602001600182018054600181600116156101000203166002900480601f016020809104026020016040519081016040528092919081815260200182805460018160011615610100020316600290048015610f1d5780601f10610ef257610100808354040283529160200191610f1d565b820191906000526020600020905b815481529060010190602001808311610f0057829003601f168201915b50505050508152602001600282018054600181600116156101000203166002900480601f016020809104026020016040519081016040528092919081815260200182805460018160011615610100020316600290048015610fbf5780601f10610f9457610100808354040283529160200191610fbf565b820191906000526020600020905b815481529060010190602001808311610fa257829003601f168201915b505050505081526020016003820160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815250509050806000015181602001518260400151819150809050935093509350509193909250565b6000816000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410156110945760009050611196565b816000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540392505081905550816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a3600190505b92915050565b600180600082825401925050819055506111b461188d565b60806040519081016040528060015481526020018581526020018481526020018373ffffffffffffffffffffffffffffffffffffffff16815250905080600260003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600060015481526020019081526020016000206000820151816000015560208201518160010190805190602001906112699291906118cc565b5060408201518160020190805190602001906112869291906118cc565b5060608201518160030160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055509050503360036000600154815260200190815260200160002060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050505050565b600060608061133861188d565b600260006003600088815260200190815260200160002060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008681526020019081526020016000206080604051908101604052908160008201548152602001600182018054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561146a5780601f1061143f5761010080835404028352916020019161146a565b820191906000526020600020905b81548152906001019060200180831161144d57829003601f168201915b50505050508152602001600282018054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561150c5780601f106114e15761010080835404028352916020019161150c565b820191906000526020600020905b8154815290600101906020018083116114ef57829003601f168201915b505050505081526020016003820160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152505090508573ffffffffffffffffffffffffffffffffffffffff166003600087815260200190815260200160002060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16148061160957508573ffffffffffffffffffffffffffffffffffffffff16816060015173ffffffffffffffffffffffffffffffffffffffff16145b1561162f57806000015181602001518260400151819150809050935093509350506116a8565b60008090506040805190810160405280600181526020017f30000000000000000000000000000000000000000000000000000000000000008152506040805190810160405280600181526020017f3000000000000000000000000000000000000000000000000000000000000000815250935093509350505b9250925092565b600180600082825401925050819055506116c761188d565b60806040519081016040528060015481526020018681526020018581526020018373ffffffffffffffffffffffffffffffffffffffff16815250905080600260008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000600154815260200190815260200160002060008201518160000155602082015181600101908051906020019061177c9291906118cc565b5060408201518160020190805190602001906117999291906118cc565b5060608201518160030160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055509050508260036000600154815260200190815260200160002060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505050505050565b60015481565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b608060405190810160405280600081526020016060815260200160608152602001600073ffffffffffffffffffffffffffffffffffffffff1681525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061190d57805160ff191683800117855561193b565b8280016001018555821561193b579182015b8281111561193a57825182559160200191906001019061191f565b5b509050611948919061194c565b5090565b61196e91905b8082111561196a576000816000905550600101611952565b5090565b9056fea165627a7a72305820503f708ff6b3bba7652d212d7ca500d453c63ae800676f0574290c56ceb9dd450029"
终端2生成abi
//使用solcjs生成abi
solcjs --abi DemeterContract.sol
//使用gedit打开,并复制abi
gedit DemeterContract_sol_DemeterContract.abi
终端1定义字abi:
var abi = [{"constant":false,"inputs":[{"name":"_name","type":"string"},{"name":"_content","type":"string"},{"name":"_receiveraddr","type":"address"}],"name":"test","outputs":[{"name":"","type":"uint256"},{"name":"","type":"address"},{"name":"","type":"string"},{"name":"","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"addr","type":"address"}],"name":"getBalanceInEth","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_ticketId","type":"uint256"}],"name":"getTicket","outputs":[{"name":"","type":"uint256"},{"name":"","type":"string"},{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"receiver","type":"address"},{"name":"amount","type":"uint256"}],"name":"sendCoin","outputs":[{"name":"sufficient","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"string"},{"name":"_content","type":"string"},{"name":"_receiveraddr","type":"address"}],"name":"addTicketToStore","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"searcher","type":"address"},{"name":"_ticketId","type":"uint256"}],"name":"getTicketByByAddress","outputs":[{"name":"","type":"uint256"},{"name":"","type":"string"},{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"string"},{"name":"_content","type":"string"},{"name":"_senderaddr","type":"address"},{"name":"_receiveraddr","type":"address"}],"name":"addTicketToStoreByAddress","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"ticketIndex","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"addr","type":"address"}],"name":"getBalance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Transfer","type":"event"}]
终端1部署合约
//通过abi创建合约对象
var myContract = web3.eth.contract(abi);
//预估手续费
web3.eth.estimateGas({data:bytecode})
//1822408 account1 = eth.coinbase;
//解锁coinbase账号,我们通过coinbase账号来付费部署合约
personal.unlockAccount(account1) //定义合约实例,gas要大于手续费
contractInstance = myContract.new(
{
data: bytecode,
gas: 2000000,
from: account1
},
function(e, contract){
if(!e){
if(!contract.address){
console.log("Contract transaction send: Transaction Hash: "+contract.transactionHash
+" waiting to be mined...");
}else{
console.log("Contract mined! Address: "
+contract.address);
console.log(contract);
}
}else{
console.log(e)
}
}
)
//返回合约部署成功后的地址
//Contract mined! Address: 0xfcaeee24285696e4427cdb41a105829f3156d33f //查看交易状态(pending = 1)
txpool.status
//开始挖矿
miner.start();
//停止挖矿
miner.stop();
//查看交易状态(pending =0)
txpool.status
//查看是否部署成功(如果没有部署成功,续集重新部署)
eth.getCode(contractInstance.address)
//合约地址
contractInstance.address
查询合约发布情况
transReceipt=eth.getTransactionReceipt(contractInstance.transactionHash)
status 0x1 表示成功 0x0 表示失败 contractAddress就是合约地址
通过合约地址获取合约对象
entityContract = myContract.at(transReceipt.contractAddress)
调用合约的查询方法ticketIndex()
entityContract.ticketIndex()
通过交易transaction调用合约方法addTicketToStore
txSend=eth.sendTransaction({"from": eth.coinbase, "to": transReceipt.contractAddress, "data": "0xa869765a00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000014697320636f6e747261637420737563636573733f000000000000000000000000"})
参考博文:https://blog.csdn.net/boneix/article/details/80223189
//调用合约方法
contractInstance.ticketIndex()
//0
> contractInstance.addTicketToStore("name","content",eth.coinbase)
Error: invalid address
at web3.js::
at web3.js::
at web3.js::
at map (<native code>)
at web3.js::
at web3.js::
at web3.js::
at web3.js::
at apply (<native code>)
at web3.js::16
解决办法:https://blog.csdn.net/qq_31915745/article/details/84205849
> contractInstance.addTicketToStoreByAddress("name","content",eth.accounts[],eth.accounts[])
b、Mist钱包中部署
花费1个eth
将合约复制粘贴过来,并把合约版本改为 0.4.24(我们下载的mist钱包合约编译器的版本就是0.4.24)
开始部署
等待合约挖矿确认
P76 、2-以太坊私网建立 (2) - 同一台电脑/不同电脑运行多个节点
先思考两个问题
- 决定存储区块链数据的目录
- 选择网络id,默认1为主网
为了在同一台机器能够运行两个节点,我们需要为不同的节点设置不同的 port 和 rpcport。
//去除--dev
geth --networkid --datadir data2 --rpc --rpcaddr 192.168.1.23 --rpcport 9000 --port 3001 console
再新开启一个终端,执行
//去除--dev
geth --networkid --datadir data3 --rpc --rpcaddr 192.168.1.23 --rpcport --port console
查看节点信息
admin.nodeInfo.enode
查看连接
admin.peers
增加连接(有时候可以,有时候不可以,连接不稳定!)
admin.addPeer("enode://d1b089c2c7c8b00aff37d7671de93a011136b5caeafa2ba24d92aee50643ee368b819cee059d8bd046410131a805058c8b0cd31c6a969b7591cc9050bdcaea5b@117.164.239.245:3002?discport=7498")
查看连接
admin.peers
查看节点连接数
web3.net.peerCount
P77 、3-以太坊私网建立 (3) - 通过创世区块来初始化区块链
保存配置文件为genesis.json
{
"config": {
"chainId": 15,
"homesteadBlock": ,
"eip155Block": ,
"eip158Block":
},
"alloc": {
"bc9a3ece02d7cb31cf63dfdfc48db0b82770d014": {
"balance": ""
},
"457e7d69fd3ac576aa744228e6b3319cde768473": {
"balance": ""
}
},"difficulty": "","gasLimit": "2100000"
}
加载配置文件
geth init genesis.json --datadir data5
打开终端
geth --networkid --datadir data5 console
查看余额
eth.getBalance("0xbc9a3ece02d7cb31cf63dfdfc48db0b82770d014");
eth.getBalance("0x457e7d69fd3ac576aa744228e6b3319cde768473");
设置coinbase
miner.setEtherbase("0xbc9a3ece02d7cb31cf63dfdfc48db0b82770d014")
参考博文:https://blog.csdn.net/qq_36652517/article/details/86235738
https://learnblockchain.cn/2017/11/29/geth_cmd_options/
https://www.cnblogs.com/tinyxiong/p/7918706.html
从零构建以太坊(Ethereum)智能合约到项目实战——第20章 搭建自己的私有链网络的更多相关文章
- 从零构建以太坊(Ethereum)智能合约到项目实战——第21章 搭建联盟链
P78 .1-内容介绍 什么情况下建立自己测试用的PoA chain? 公司内网或无对外网络,无法同步区块 降低测试时等待区块的时间 不想碰到testrpc各种雷 PoA chain特点有 有别于Po ...
- 区块链入门到实战(27)之以太坊(Ethereum) – 智能合约开发
智能合约的优点 与传统合同相比,智能合约有一些显著优点: 不需要中间人 费用低 代码就是规则 区块链网络中有多个备份,不用担心丢失 避免人工错误 无需信任,就可履行协议 匿名履行协议 以太坊(Ethe ...
- 从零构建以太坊(Ethereum)智能合约到项目实战——第23章 从零构建和部署去中心化投票App,decentralization Voting Dapp
P90 .1-从零构建和部署去中心化投票App-01 P91 .2-从零构建和部署去中心化投票App-02 P92 .3-从零构建和部署去中心化投票App-03 参考博文:http://liyuech ...
- 从零构建以太坊(Ethereum)智能合约到项目实战——第22章 玩转truffle framework 、Web3.js 框架
P84 .1-玩转truffle framework.Web3.js 框架 内容介绍 truffle官方网站:https://truffleframework.com/ P85 .2-truffle ...
- 从零构建以太坊(Ethereum)智能合约到项目实战——第24章 IPFS + 区块链
P93 .1-IPFS环境配置P94 .2-IPFS+P .IPNS+P .个人博客搭建 - 如何在IPFS新增一个文件P95 .3-IPFS+P .IPNS+P .个人博客搭建 - 通过ipfs创建 ...
- 从零构建以太坊(Ethereum)智能合约到项目实战——第25章 Embark FrameWork
P109 .1-Embark Framework 开发入门篇P110 .2-Embark Framework 去中心化存储 (IPFS)
- 零门槛,包教会。让你在5分钟内使用以太坊ERC20智能合约发行属于自己的空气币
前言 目前区块链是互联网中最最火的风口,没有之一.我周围的很多朋友也加入了“炒币”行列,但很不幸,几乎都被“割韭菜”了.而经过我的几天研究,发现,如果自己要发行一种空气币,简直太简单了.只需要下面几个 ...
- 以太坊solidity智能合约-生成随机数
Solidity随机数生成 在以太坊的只能合约中,没有提供像其他面向对象编程一样的生成随机数的工具类或方法.其实,所谓的随机数也是伪随机的,没有哪一种语言能够真正的生成随机数. 对于solidity来 ...
- 从零构建以太坊(Ethereum)智能合约到项目实战——学习笔记10
P57 .1-Solidity Types - 玩转 Solidity 数组 (Arrays) 学习目标 1.掌握Arrays的可变不可变的创建 2.深度理解可变数组和不可变数组之间的区别 3.二维数 ...
随机推荐
- mui 把内容复制到剪切板
function copyShareUrl(){ mui.plusReady(function(){ //复制链接到剪切板 var copy_content = ""; //判断是 ...
- 你所了解的Java线程池
在jvm中,线程是一个宝贵的资源,创建与销毁都会抢占宝贵的内存资源,为了有效的重用线程,我们用线程池来管理线程,让创建的线程进行复用. JDK提供了一套Executor框架,帮助我们管理线程,核心成员 ...
- 【转载】 BIOS设置选项详细解释——CPU核心篇
原文地址: http://kuaibao.qq.com/s/20180226A1G1OC00?refer=spider ---------------------------------------- ...
- ztree-编辑节点(树节点添加,删除,修改)
<!DOCTYPE html> <HTML> <HEAD> <TITLE> ZTREE DEMO - addNodes / editName / rem ...
- 对图书管理系统5W1H的分析
5W1H 对图书管理系统的分析 1.Who 学生.老师和图书馆管理员 2.When 借还书的时候使用,还有用户流量的统计在每天晚上都会看一下有多少人哪些人看了的,基本绝大多数时间都可以看 3.Wher ...
- 【PAT甲级】1096 Consecutive Factors (20 分)
题意: 输入一个int范围内的正整数,输出它最多可以被分解为多少个连续的因子并输出这些因子以*连接. trick: 测试点5包含N本身是一个素数的数据,此时应当输出1并把N输出. 测试点5包含一个2e ...
- 腾讯云Windows Server下nodejs websocket ssl配置
1.从腾讯云申请SSL证书,下载解压,得到如下文件: 2.nodejs代码: // wss.js const fs = require('fs'); // 一些配置信息 const cfg = { p ...
- Dapr Pub/Sub 集成 RabbitMQ 、Golang、Java、DotNet Core
前置条件: <Dapr运用> <Dapr 运用之 Java gRPC 调用篇> <Dapr 运用之集成 Asp.Net Core Grpc 调用篇> 搭建 Rabb ...
- Python - int()
参考 https://docs.python.org/3/library/functions.html?highlight=int#int If x is not a number or if bas ...
- 为什么阿里Java手册推荐慎用 Object 的 clone 方法来拷贝对象
图片若无法显示,可至掘金查看https://juejin.im/post/5d425230f265da039519d248 前言 在阿里Java开发手册中,有这么一条建议:慎用 Object 的 cl ...