以太坊私有链部署合约
以太坊私有链部署合约
1.建⽴私有链
[root@greg02~]#mkdirmychain
[root@greg02~]#cdmychain
[root@greg02mychain]#geth--networkid123--dev--datadirdata1--rpc--rpcaddr192.168.179.130--rpcport8989--port3000
#运⾏后产⽣如下⽂件
[root@greg02mychain]#ls
data1
[root@greg02mychain]#lsdata1/
store
各项含义:
--identity:指定节点ID;
--rpc:表⽰开启HTTP-RPC服务;
--rpcaddr:HTTP-RPC服务ip地址;
--rpcport:指定HTTP-RPC服务监听端⼝号(默认为8545);
--datadir:指定区块链数据的存储位置;
--port:指定和其他节点连接所⽤的端⼝号(默认为30303);
--nodiscover:关闭节点发现机制,防⽌加⼊有同样初始配置的陌⽣节点。
保持节点的运⾏,不要关闭终端,重新打开⼀个终端,使⽤gethattach连接节点,并且打开gethconsole
[root@greg02mychain]#gethattachipc:/root/mychain/data1/
WelcometotheGethJavaScriptconsole!
instance:Geth/v1.8.3-unstable-746392cf/linux-amd64/go1.9.2
coinba:0x804bf1ee046894d6425a8bed1abe553f776ae0df
atblock:0(Thu,01Jan197008:00:00CST)
datadir:/root/mychain/data1
modules:admin:1.0clique:1.0debug:1.0eth:1.0miner:1.0net:1.0personal:1.0rpc:1.0shh:1.0txpool:1.0web3:1.0
>
这是⼀个交互式的JavaScript执⾏环境,在这⾥⾯可以执⾏JavaScript代码,其中>是命令提⽰符。在这个环境⾥也内置了⼀些⽤来操
作以太坊的JavaScript对象,可以直接使⽤这些对象。这些对象主要包括:
eth:包含⼀些跟操作区块链相关的⽅法;
net:包含⼀些查看p2p⽹络状态的⽅法;
admin:包含⼀些与管理节点相关的⽅法;
miner:包含启动&停⽌挖矿的⼀些⽅法;
personal:主要包含⼀些管理账户的⽅法;
txpool:包含⼀些查看交易内存池的⽅法;
web3:包含了以上对象,还包含⼀些单位换算的⽅法。
2.创建账户
>ount('grego')
"0x88d6725760f34a91059485e396759d422441e427"
>counts
["0x804bf1ee046894d6425a8bed1abe553f776ae0df"
>
"0x804bf1ee046894d6425a8bed1abe553f776ae0df"
3.编写只能合约
pragmasolidity^0.4.0;
contractBallot{
structVoter{
uintweight;
boolvoted;
uint8vote;
addressdelegate;
}
structProposal{
uintvoteCount;
}
addresschairperson;
mapping(address=>Voter)voters;
Proposal[]proposals;
///Createanewballotwith$(_numProposals)differentproposals.
functionBallot(uint8_numProposals)public{
chairperson=;
voters[chairperson].weight=1;
=_numProposals;
}
///Give$(toVoter)therighttovoteonthisballot.
///Mayonlybecalledby$(chairperson).
functiongiveRightToVote(addresstoVoter)public{
if(!=chairperson||voters[toVoter].voted)return;
voters[toVoter].weight=1;
}
///Delegateyourvotetothevoter$(to).
functiondelegate(addressto)public{
Voterstoragender=voters[];//assignsreference
if()return;
while(voters[to].delegate!=address(0)&&voters[to].delegate!=)
to=voters[to].delegate;
if(to==)return;
=true;
te=to;
VoterstoragedelegateTo=voters[to];
if()
proposals[].voteCount+=;
el
+=;
}
///Giveasinglevotetoproposal$(toProposal).
functionvote(uint8toProposal)public{
Voterstoragender=voters[];
if(||toProposal>=)return;
=true;
=toProposal;
proposals[toProposal].voteCount+=;
proposals[toProposal].voteCount+=;
}
functionwinningProposal()publicconstantreturns(uint8_winningProposal){
uint256winningVoteCount=0;
for(uint8prop=0;prop<;prop++)
if(proposals[prop].voteCount>winningVoteCount){
winningVoteCount=proposals[prop].voteCount;
_winningProposal=prop;
}
}
}
BYTECODE:
ABI:
[
{
"constant":fal,
"inputs":[
{
"name":"to",
"type":"address"
}
],
"name":"delegate",
"outputs":[],
"payable":fal,
"stateMutability":"nonpayable",
"type":"function"
},
{
"constant":true,
"inputs":[],
"name":"winningProposal",
"outputs":[
{
"name":"_winningProposal",
"type":"uint8"
}
],
"payable":fal,
"stateMutability":"view",
"type":"function"
},
{
"constant":fal,
"inputs":[
{
"name":"toVoter",
"type":"address"
}
],
"name":"giveRightToVote",
"outputs":[],
"payable":fal,
"stateMutability":"nonpayable",
"type":"function"
},
{
"constant":fal,
"inputs":[
{
"name":"toProposal",
"type":"uint8"
}
],
"name":"vote",
"outputs":[],
"payable":fal,
"stateMutability":"nonpayable",
"type":"function"
},
{
"inputs":[
{
"name":"_numProposals",
"type":"uint8"
}
],
"payable":fal,
"stateMutability":"nonpayable",
"type":"constructor"
}
]
ABI删除空格并转义
[{"constant":fal,"inputs":[{"name":"to","type":"address"}],"name":"delegate","outputs":[],"payable":fal,"stateMutability":"nonpayable","type
在console⾥创建ABI合约对象
>varabi=('[{"constant":fal,"inputs":[{"name":"to","type":"address"}],"name":"delegate","outputs":[],"payable":fal,"stateMutabilit
undefined
>myContract=ct(abi)
{
abi:[{
constant:fal,
inputs:[{...}],
name:"delegate",
outputs:[],
payable:fal,
stateMutability:"nonpayable",
type:"function"
},{
constant:true,
inputs:[],
name:"winningProposal",
outputs:[{...}],
payable:fal,
stateMutability:"view",
type:"function"
},{
constant:fal,
inputs:[{...}],
name:"giveRightToVote",
outputs:[],
payable:fal,
stateMutability:"nonpayable",
type:"function"
},{
constant:fal,
inputs:[{...}],
name:"vote",
outputs:[],
payable:fal,
stateMutability:"nonpayable",
type:"function"
},{
inputs:[{...}],
payable:fal,
stateMutability:"nonpayable",
type:"constructor"
}],
eth:{
accounts:["0x804bf1ee046894d6425a8bed1abe553f776ae0df","0x88d6725760f34a91059485e396759d422441e427","0x44d18ce9e2e54c93cf382e0a1e571
blockNumber:0,
coinba:"0x804bf1ee046894d6425a8bed1abe553f776ae0df",
compile:{
lll:function(),
rpent:function(),
solidity:function()
},
defaultAccount:undefined,
defaultBlock:"latest",
gasPrice:1,
hashrate:0,
mining:true,
pendingTransactions:[],
protocolVersion:"0x3f",
syncing:fal,
call:function(),
contract:function(abi),
estimateGas:function(),
filter:function(options,callback,filterCreationErrorCallback),
getAccounts:function(callback),
getBalance:function(),
getBlock:function(),
getBlockNumber:function(callback),
getBlockTransactionCount:function(),
getBlockUncleCount:function(),
getCode:function(),
getCoinba:function(callback),
getCompilers:function(),
getGasPrice:function(callback),
getHashrate:function(callback),
getMining:function(callback),
getPendingTransactions:function(callback),
getProtocolVersion:function(callback),
getRawTransaction:function(),
getRawTransactionFromBlock:function(),
getStorageAt:function(),
getSyncing:function(callback),
getTransaction:function(),
getTransactionCount:function(),
getTransactionFromBlock:function(),
getTransactionReceipt:function(),
getUncle:function(),
getWork:function(),
iban:function(iban),
icapNamereg:function(),
isSyncing:function(callback),
namereg:function(),
rend:function(),
ndIBANTransaction:function(),
ndRawTransaction:function(),
ndTransaction:function(),
sign:function(),
signTransaction:function(),
submitTransaction:function(),
submitWork:function()
},
at:function(address,callback),
getData:function(),
new:function()
}
检查coinba账号余额
>account1=
"0x804bf1ee046894d6425a8bed1abe553f776ae0df"
>ance(account1)
1.853269984665644927e+77
解锁coinba账号
>
"0x804bf1ee046894d6425a8bed1abe553f776ae0df"
>Account(account1,'')
true
BYTECODE预估费⽤
>teGas({data:bytecode})
597031
字节码前⾯需要添加0x。⼿续费⼤概为597031``gas
部署合约
>contractInstance=({data:bytecodegas:1000000,from:account1},function(e,contract){
......if(!e){
.........if(!s){
............("Contracttransactionnd:TransactionHash:"+ctionHash+"waitingtobemined...");
............}el{
............("Contractmined!Address:"+s);
............(contract);
............}
.........}el{
.........(e)
.........}
......})
Contracttransactionnd:TransactionHash:0xca1561453f4cc9541ec84dd4ef3a25cbb61ef4d1e9bf1f829fef5107f9017266waitingtobemined...
{
abi:[{
constant:fal,
inputs:[{...}],
name:"delegate",
outputs:[],
payable:fal,
stateMutability:"nonpayable",
type:"function"
},{
constant:true,
inputs:[],
name:"winningProposal",
outputs:[{...}],
payable:fal,
stateMutability:"view",
type:"function"
},{
constant:fal,
inputs:[{...}],
name:"giveRightToVote",
outputs:[],
payable:fal,
stateMutability:"nonpayable",
type:"function"
},{
constant:fal,
inputs:[{...}],
name:"vote",
outputs:[],
payable:fal,
stateMutability:"nonpayable",
type:"function"
},{
inputs:[{...}],
payable:fal,
stateMutability:"nonpayable",
type:"constructor"
}],
address:undefined,
transactionHash:"0xca1561453f4cc9541ec84dd4ef3a25cbb61ef4d1e9bf1f829fef5107f9017266"
}
合约等待挖矿
>()
null
>()
true
本文发布于:2023-01-24 13:53:50,感谢您对本站的认可!
本文链接:http://www.wtabcd.cn/fanwen/fan/88/127431.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |