1.启动geth console
admin07@admin-MS:~/job/gopath/src/github.com/ethereum/go-ethereum$ ./build/bin/geth --datadir dev_data/node1 --networkid 314590 --ipcdisable --port 61910 --rpcport 8200 console
2.赋值mshk_abi
参考 https://www.jianshu.com/p/21efeccb07a7 先生成Hello_mshk_top.json文件,生成位置在test_truffle/build/contracts/Hello_mshk_top.json
获取Hello_mshk_top.json文件中的abi,使用http://www.bejson.com/jsonviewernew/ 将它转换为字符串,然后放入下面的JSON.parse('生成的abi')
>mshk_abi=JSON.parse('[{\"constant\":true,\"inputs\":[{\"name\":\"name\",\"type\":\"string\"}],\"name\":\"print\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"say\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"}]')
输出结果:
[{
constant: true,
inputs: [{
name: "name",
type: "string"
}],
name: "print",
outputs: [{
name: "",
type: "string"
}],
payable: false,
stateMutability: "pure",
type: "function"
}, {
constant: true,
inputs: [],
name: "say",
outputs: [{
name: "",
type: "string"
}],
payable: false,
stateMutability: "pure",
type: "function"
}]
3. 赋值mshk_bytecode
找到Hello_mshk_top.json文件中的bytecode部分,然后在geth中,将值赋值给mshk_bytecode变量
>mshk_bytecode="0x6060604052341561000f57600080fd5b6102488061001e6000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806311114af114610051578063954ab4b214610127575b600080fd5b341561005c57600080fd5b6100ac600480803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919050506101b5565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100ec5780820151818401526020810190506100d1565b50505050905090810190601f1680156101195780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561013257600080fd5b61013a6101c5565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561017a57808201518184015260208101905061015f565b50505050905090810190601f1680156101a75780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6101bd610208565b819050919050565b6101cd610208565b6040805190810160405280600e81526020017f48656c6c6f206d73686b2e746f70000000000000000000000000000000000000815250905090565b6020604051908101604052806000815250905600a165627a7a72305820aa448d81e0f4bf12f805920c9cefd0d3cc6f053d87dd8547663721256179ebcd0029"
输出结果:
"0x6060604052341561000f57600080fd5b6102488061001e6000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806311114af114610051578063954ab4b214610127575b600080fd5b341561005c57600080fd5b6100ac600480803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919050506101b5565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100ec5780820151818401526020810190506100d1565b50505050905090810190601f1680156101195780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561013257600080fd5b61013a6101c5565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561017a57808201518184015260208101905061015f565b50505050905090810190601f1680156101a75780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6101bd610208565b819050919050565b6101cd610208565b6040805190810160405280600e81526020017f48656c6c6f206d73686b2e746f70000000000000000000000000000000000000815250905090565b6020604051908101604052806000815250905600a165627a7a72305820aa448d81e0f4bf12f805920c9cefd0d3cc6f053d87dd8547663721256179ebcd0029"
4.estimateGas
> web3.eth.estimateGas({data: mshk_bytecode})
输出结果:
207296
5.部署
5.1. 通过abi创建合约对象
> mshk_Contract = eth.contract(mshk_abi);
输出:
{
abi: [{
constant: true,
inputs: [{...}],
name: "print",
outputs: [{...}],
payable: false,
stateMutability: "pure",
type: "function"
}, {
constant: true,
inputs: [],
name: "say",
outputs: [{...}],
payable: false,
stateMutability: "pure",
type: "function"
}],
eth: {
accounts: ["0x593f30c2b86449893f3e2fe24a98d9f2a0034421"],
blockNumber: 5206,
coinbase: "0x593f30c2b86449893f3e2fe24a98d9f2a0034421",
compile: {
lll: function(),
serpent: function(),
solidity: function()
},
defaultAccount: undefined,
defaultBlock: "latest",
gasPrice: 18000000000,
hashrate: 0,
mining: false,
pendingTransactions: [],
protocolVersion: "0x3f",
syncing: false,
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(),
getCoinbase: 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(),
resend: function(),
sendIBANTransaction: function(),
sendRawTransaction: function(),
sendTransaction: function(),
sign: function(),
signTransaction: function(),
submitTransaction: function(),
submitWork: function()
},
at: function(address, callback),
getData: function(),
new: function()
}
5.2. 部署
> mshk_hello = mshk_Contract.new({from:eth.accounts[0], data:mshk_bytecode, gas:300000}, function(e, contract){
if(!e) {
if(!contract.address) {
console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined...");
} else {
console.log("Contract mined! Address: " + contract.address);
console.log(contract);
}
}
})
输出结果:
INFO [07-04|20:37:16] Submitted contract creation fullhash=0xce95a9be747beae64e7325bb727a470a4fd9e8ddbfeb03ff98e71f01c0d3233e contract=0x668EFC5325df2511E0Cc9284ae3d51CBFb63b88a //合约地址
Contract transaction send: TransactionHash: 0xce95a9be747beae64e7325bb727a470a4fd9e8ddbfeb03ff98e71f01c0d3233e waiting to be mined... //等待被确认
{
abi: [{
constant: true,
inputs: [{...}],
name: "print",
outputs: [{...}],
payable: false,
stateMutability: "pure",
type: "function"
}, {
constant: true,
inputs: [],
name: "say",
outputs: [{...}],
payable: false,
stateMutability: "pure",
type: "function"
}],
address: undefined,
transactionHash: "0xce95a9be747beae64e7325bb727a470a4fd9e8ddbfeb03ff98e71f01c0d3233e"
}
如果这步里面得到的transactionHash为空,可能是account需要解锁一下。
执行下面命令进行解锁
personal.unlockAccount(eth.accounts[0])
5.3. 查看该交易状态
> eth.getTransaction("0xce95a9be747beae64e7325bb727a470a4fd9e8ddbfeb03ff98e71f01c0d3233e")
输出结果:
{
blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
blockNumber: null,
from: "0x593f30c2b86449893f3e2fe24a98d9f2a0034421",
gas: 300000,
gasPrice: 18000000000,
hash: "0xce95a9be747beae64e7325bb727a470a4fd9e8ddbfeb03ff98e71f01c0d3233e",
input: "0x6060604052341561000f57600080fd5b6102488061001e6000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806311114af114610051578063954ab4b214610127575b600080fd5b341561005c57600080fd5b6100ac600480803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919050506101b5565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100ec5780820151818401526020810190506100d1565b50505050905090810190601f1680156101195780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561013257600080fd5b61013a6101c5565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561017a57808201518184015260208101905061015f565b50505050905090810190601f1680156101a75780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6101bd610208565b819050919050565b6101cd610208565b6040805190810160405280600e81526020017f48656c6c6f206d73686b2e746f70000000000000000000000000000000000000815250905090565b6020604051908101604052806000815250905600a165627a7a72305820aa448d81e0f4bf12f805920c9cefd0d3cc6f053d87dd8547663721256179ebcd0029",
nonce: 0,
r: "0xad8ef555696ae6126830d053b1e2537262b5aa57c594236609cdbaced9f91aab",
s: "0x21f524c9ce5251d788dc2878dd59c4db2b358df481fbd6ac0ffdb9473fd33d9e",
to: null,
transactionIndex: 0,
v: "0x1b",
value: 0
}
5.4. 执行挖矿
> miner.start()
执行结果:
INFO [07-04|20:42:03] Updated mining threads threads=0
INFO [07-04|20:42:03] Transaction pool price threshold updated price=18000000000
INFO [07-04|20:42:03] Starting mining operation
null
> INFO [07-04|20:42:03] Commit new mining work number=5207 txs=1 uncles=0 elapsed=2.885ms
INFO [07-04|20:42:17] Successfully sealed new block number=5207 hash=da84e5…a2bc76
INFO [07-04|20:42:17] 🔨 mined potential block number=5207 hash=da84e5…a2bc76
INFO [07-04|20:42:17] Commit new mining work number=5208 txs=0 uncles=0 elapsed=151.327µs
Contract mined! Address: 0x668efc5325df2511e0cc9284ae3d51cbfb63b88a //创建合约的交易被确认
5.5. 再次查看交易详情
> eth.getTransaction("0xce95a9be747beae64e7325bb727a470a4fd9e8ddbfeb03ff98e71f01c0d3233e")
{
blockHash: "0xda84e59dfd9a8b6729e661a247ede2780553264cbdccd09b3b27e2c83ea2bc76", //这里有值
blockNumber: 5207, //这里有值
from: "0x593f30c2b86449893f3e2fe24a98d9f2a0034421",
gas: 300000,
gasPrice: 18000000000,
hash: "0xce95a9be747beae64e7325bb727a470a4fd9e8ddbfeb03ff98e71f01c0d3233e",
input: "0x6060604052341561000f57600080fd5b6102488061001e6000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806311114af114610051578063954ab4b214610127575b600080fd5b341561005c57600080fd5b6100ac600480803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919050506101b5565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100ec5780820151818401526020810190506100d1565b50505050905090810190601f1680156101195780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561013257600080fd5b61013a6101c5565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561017a57808201518184015260208101905061015f565b50505050905090810190601f1680156101a75780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6101bd610208565b819050919050565b6101cd610208565b6040805190810160405280600e81526020017f48656c6c6f206d73686b2e746f70000000000000000000000000000000000000815250905090565b6020604051908101604052806000815250905600a165627a7a72305820aa448d81e0f4bf12f805920c9cefd0d3cc6f053d87dd8547663721256179ebcd0029",
nonce: 0,
r: "0xad8ef555696ae6126830d053b1e2537262b5aa57c594236609cdbaced9f91aab",
s: "0x21f524c9ce5251d788dc2878dd59c4db2b358df481fbd6ac0ffdb9473fd33d9e",
to: null,
transactionIndex: 0,
v: "0x1b",
value: 0
}
6. 测试合约是否发布成功
> mshk_hello.say()
"Hello mshk.top"
> mshk_hello.print("Hello, World")
"Hello, World"