从零构建以太坊(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 123 --dev --datadir data --rpc --rpcaddr 192.168.2.223 --rpcport 8545 --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 123 --dev --datadir data --rpc --rpcaddr 192.168.31.140 --rpcport 8545 --port 3000 console //赋予执行权限 chmod 775 init.sh

 

保持节点的运行,不要关闭终端,重新打开一个终端,使用geth attach连接节点,并且打开geth console

geth attach ipc:data/geth.ipc 

 为了方便操作,我们将上面的命令存为connect.sh:

nano connect.sh

//输入
geth attach ipc:data/geth.ipc

//赋予执行权限
chmod 775 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[1]

查看账户余额

eth.getBalance("账户地址")
//eth.getBalance(personal.listAccounts[0])

开始挖矿

miner.start();

停止挖矿

miner.stop();

 

终端1账户0向账户1转账

//解锁账户0(密码为空)
personal.unlockAccount(personal.listAccounts[0]);
//以太币转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-0-11-1.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 775 mist.sh

 

打开即可

./mist.sh

 

 

6、编写智能合约 

这里我们部署合约分两种情况:

  • 命令行方式部署
  • Mist钱包中部署

a、命令行方式部署

终端1编写合约

cd privatechain
nano DemeterContract.sol

 

输入以下内容,并保存:

pragma solidity ^0.5.4;

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 = 0;
        balances[tx.origin] = 10000;
    }

    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 += 1;
        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 += 1;
        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 += 1;
        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 (0, "0", "0");
    }
}

 

 

安装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:3930:15
    at web3.js:3756:20
    at web3.js:5025:28
    at map (<native code>)
    at web3.js:5024:12
    at web3.js:5050:18
    at web3.js:5075:23
    at web3.js:4137:16
    at apply (<native code>)
    at web3.js:4223:16

解决办法:https://blog.csdn.net/qq_31915745/article/details/84205849

> contractInstance.addTicketToStoreByAddress("name","content",eth.accounts[0],eth.accounts[1])

 

b、Mist钱包中部署

 

 花费1个eth

 

 将合约复制粘贴过来,并把合约版本改为 0.4.24(我们下载的mist钱包合约编译器的版本就是0.4.24)

 

 开始部署

 

 等待合约挖矿确认

 

 

 

 

 

 

 

 

P76 、2-以太坊私网建立 (2) - 同一台电脑/不同电脑运行多个节点

先思考两个问题

  • 决定存储区块链数据的目录
  • 选择网络id,默认1为主网

为了在同一台机器能够运行两个节点,我们需要为不同的节点设置不同的 port 和 rpcport。

//去除--dev
geth --networkid 123 --datadir data2 --rpc --rpcaddr 192.168.1.23 --rpcport 9000 --port 3001 console

 

再新开启一个终端,执行

//去除--dev
geth --networkid 123 --datadir data3 --rpc --rpcaddr 192.168.1.23 --rpcport 9001 --port 3002 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": 0,
                "eip155Block": 0,
                "eip158Block": 0
        },
        "alloc": {
                "bc9a3ece02d7cb31cf63dfdfc48db0b82770d014": {
                        "balance": "300000"
                },
                "457e7d69fd3ac576aa744228e6b3319cde768473": {
                        "balance": "400000"
                }
        },"difficulty": "4","gasLimit": "2100000"
}

 

加载配置文件

geth init genesis.json --datadir data5

 

打开终端

geth --networkid 123 --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

posted @ 2019-02-10 21:07  m0w3n  阅读(1798)  评论(0编辑  收藏  举报