在Spectrum测试链上部署智能合约

安装环境准备

brew install go

安装 geth

npm install -g solc

Solidity以太坊智能合约语言

brew  install solidity

1,启动一个Spectrum节点

1,./smc --testnet --port 30308 --rpc --rpccorsdomain "*" --rpcaddr "0.0.0.0" --rpcapi db,eth,net,web3,personal,admin,miner,txpool --ws --wsapi admin,eth,mine,debug,personal,txpool,web3,net --wsorigins="*" --wsaddr="0.0.0.0" --datadir /Users/a212/Desktop/Spectrum/build/bin/datadir --rpcport 18545 console

参数名称    参数描述
datadir 设置当前区块链网络数据存放的位置
nodiscover  私有链地址,不会被网上看到
console 启动命令行模式,可以在Geth中执行命令
identity    区块链的标示,用于标示目前网络的名字
rpc 开启rpc通道
rpcapi  要开放哪些rpc api
rpccorsdomain   允许能连接到你的节点执行rpc api的url,使用逗号分隔。*表示任何url都可以连接
rpcaddr HTTP-RPC服务器接口地址,默认为localhost
rpcport HTTP-RPC服务器端口地址,默认为8545
networkid   网络标识,私有链取一个大于4的随意的值


2,如果觉得开启console一直同步数据感觉烦人,可以换另外一种方式,启动节点:
./smc --rpc console
找到:database=/Users/a212/Library/Spectrum/smc/chaindata 
IPC endpoint closed: /Users/a212/Library/Spectrum/smc.ipc 
开启另外一个终端:
sudo ./smc attach ipc://Users/a212/Library/Spectrum/smc.ipc 

geth attach是通过rpc或者ipc和已经启动的节点进行交互,geth attach只能使用已经打开的模块的api。
IPC是实现进程通讯,以npipe为底层实现,上层采用Json-Rpc为消息格式,并使用go的reflect包实现对内部Api的调用。 
ipc通信是用于同一个主机间钱包等客户端与geth之间的通信。

屏幕快照 2018-10-16 上午11.48.40.png

屏幕快照 2018-10-16 上午11.49.38.png

创建账号

personal.newAccount()
user1 = eth.accounts[0]

"0xa4aa2105cc3e6b6a83faaaf72782d1e9a68e90ca"

> eth.getBalance(user1)

0

需要等待官方打测试smt币

> eth.getBalance(user1)

10000000000000000000

> personal.unlockAccount(user1,"123456")
true
> personal
{
  listAccounts: ["0x12257ee710524a3088e5377c95d19e21218eaf34"],
  listWallets: [{
      accounts: [{...}],
      status: "Unlocked",
      url: "keystore:///Users/a212/Desktop/Spectrum/build/bin/datadir/keystore/UTC--2018-10-16T06-47-54.472451000Z--12257ee710524a3088e5377c95d19e21218eaf34"
  }],
  deriveAccount: function(),
  ecRecover: function(),
  getListAccounts: function(callback),
  getListWallets: function(callback),
  importRawKey: function(),
  lockAccount: function(),
  newAccount: function github.com/SmartMeshFoundation/Spectrum/console.(*bridge).NewAccount-fm(),
  openWallet: function github.com/SmartMeshFoundation/Spectrum/console.(*bridge).OpenWallet-fm(),
  sendTransaction: function(),
  sign: function github.com/SmartMeshFoundation/Spectrum/console.(*bridge).Sign-fm(),
  unlockAccount: function github.com/SmartMeshFoundation/Spectrum/console.(*bridge).UnlockAccount-fm()
}

2,需要给coinbase合约地址发送一个1smt激活合约

Wait send 1 finney at least to "0x1C2609bD4D21d066190b12B99CB0E086C072a7ee" to upgrade to signature node

屏幕快照 2018-10-16 下午3.31.09.png

> user1
"0x12257ee710524a3088e5377c95d19e21218eaf34"
> user2
"0xe7298bec5b48a92c7a425b1bbdd468e06fec2076"
> user2 = eth.coinbase
"0x1c2609bd4d21d066190b12b99cb0e086c072a7ee"
> amount = web3.toWei(1,'ether')
"1000000000000000000"
> eth.getBalance(user1)
9997870474000000000
> personal.unlockAccount(user1,"123456")
true
> eth.sendTransaction({from:user1,to:user2,value:amount})
"0xe748c5be9cb2b921800481a03d0b6d7c8b607b3805783925d9b0ef4990a20c49"

JSON RPC请求数据流

屏幕快照 2018-10-16 下午12.12.43.png

屏幕快照 2018-10-16 下午4.23.52.png

激活成功!

2,智能合约代码:

contract Multiply7 {
   event Print(uint);
   function multiply(uint input) returns (uint) {
      Print(input * 7);
      return input * 7;
   }
}

3,编译智能合约

通过remix:https://remix.ethereum.org,

推荐老版本remix: https://ethereum.github.io/browser-solidity/

屏幕快照 2018-10-16 下午12.31.46.png

4,abi和bytecode

 abi=[{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"d","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]

屏幕快照 2018-03-03 下午8.10.55.png

字节码前加0x

bytecode="0x6060604052341561000f57600080fd5b60b18061001d6000396000f300606060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa1146044575b600080fd5b3415604e57600080fd5b606260048080359060200190919050506078565b6040518082815260200191505060405180910390f35b60006007820290509190505600a165627a7a72305820e8baa72a4bd1ffe2e8fa51812e8d65fc7b145baf48382c4ae5039510fd35a6ff0029"

5,通过abiDefinition和bytecode实例化创建合约

var contract = eth.contract(abi);
var initializer = {from:web3.eth.accounts[0],data:bytecode,gas:300000};
personal.unlockAccount(user1,"jianghua")
var token = contract.new(initializer)

屏幕快照 2018-03-03 下午8.12.37.png

屏幕快照 2018-03-03 下午8.13.02.png

屏幕快照 2018-10-16 下午12.31.37.png

6,Copy到Spectrum Console下

注意解锁账号:
> personal.unlockAccount(user1,"123456")
true
var multiply7Contract = web3.eth.contract([{"constant":false,"inputs":[{"name":"input","type":"uint256"}],"name":"multiply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"","type":"uint256"}],"name":"Print","type":"event"}]);
var multiply7 = multiply7Contract.new(
   {
     from: web3.eth.accounts[0], 
     data: '0x608060405234801561001057600080fd5b5060f58061001f6000396000f300608060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa1146044575b600080fd5b348015604f57600080fd5b50606c600480360381019080803590602001909291905050506082565b6040518082815260200191505060405180910390f35b60007f24abdb5865df5079dcc5ac590ff6f01d5c16edbc5fab4e195d9febd1114503da600783026040518082815260200191505060405180910390a16007820290509190505600a165627a7a7230582071fc21715068fac907dfef99ecedadf796838b73e1410b3e57aeea30e61dd4f10029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })

7,需要通过挖矿这一步骤,对合约地址进行确认

INFO [10-16|15:56:52] Submitted contract creation              fullhash=0x992f2989028f27c1ebae3d48edafdf58be40530afde0f42cb015bdd72504e434 contract=0x844871f19862210E019c509007cA3a0266D13dcA


> null [object Object]
Contract mined! address: 0x844871f19862210e019c509007ca3a0266d13dca transactionHash: 0x992f2989028f27c1ebae3d48edafdf58be40530afde0f42cb015bdd72504e434

到此为止,合约的布署已布署到了区块链上。

8,到这里,合约的布署与调用完成

> multiply7
{
  abi: [{
      constant: false,
      inputs: [{...}],
      name: "multiply",
      outputs: [{...}],
      payable: false,
      stateMutability: "nonpayable",
      type: "function"
  }, {
      anonymous: false,
      inputs: [{...}],
      name: "Print",
      type: "event"
  }],
  address: "0x844871f19862210e019c509007ca3a0266d13dca",
  transactionHash: "0x992f2989028f27c1ebae3d48edafdf58be40530afde0f42cb015bdd72504e434",
  Print: function(),
  allEvents: function(),
  multiply: function()
}
> multiply7.multiply.call(2)
14
> 

到了这一步,我们可以得到自己合约的返回值14

9,检查合约是否部署成功

> eth.getCode("0x844871f19862210e019c509007ca3a0266d13dca")

"0x608060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa1146044575b600080fd5b348015604f57600080fd5b50606c600480360381019080803590602001909291905050506082565b6040518082815260200191505060405180910390f35b60007f24abdb5865df5079dcc5ac590ff6f01d5c16edbc5fab4e195d9febd1114503da600783026040518082815260200191505060405180910390a16007820290509190505600a165627a7a7230582071fc21715068fac907dfef99ecedadf796838b73e1410b3e57aeea30e61dd4f10029"

10,验证交易信息

 eth.getTransaction("0x992f2989028f27c1ebae3d48edafdf58be40530afde0f42cb015bdd72504e434")
{
  blockHash: "0xf9eb50ed3b38b0330501931c10a7df88bd38a7dbda8e1d6ff94a7814dc75ea10",
  blockNumber: 1120146,
  from: "0x12257ee710524a3088e5377c95d19e21218eaf34",
  gas: 4700000,
  gasPrice: 18000000000,
  hash: "0x992f2989028f27c1ebae3d48edafdf58be40530afde0f42cb015bdd72504e434",
  input: "0x608060405234801561001057600080fd5b5060f58061001f6000396000f300608060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa1146044575b600080fd5b348015604f57600080fd5b50606c600480360381019080803590602001909291905050506082565b6040518082815260200191505060405180910390f35b60007f24abdb5865df5079dcc5ac590ff6f01d5c16edbc5fab4e195d9febd1114503da600783026040518082815260200191505060405180910390a16007820290509190505600a165627a7a7230582071fc21715068fac907dfef99ecedadf796838b73e1410b3e57aeea30e61dd4f10029",
  nonce: 0,
  r: "0xa36eb3d28f91d8bbfa339f62af6cd501ee3c6e73ab55268e5e23ac2aa1635aa0",
  s: "0x8719db92e96077779db57eb104a0c88004e1d185d2a491a46ee4eff758e432d",
  to: null,
  transactionIndex: 1,
  v: "0x2a",
  value: 0
}

11,验证区块的内容

eth.getBlock(1120146)
{
  difficulty: 3,
  extraData: "0xd582050183736d6387676f312e392e32856c696e757800000000000000000000c02c146db51beeb473f8a034add49c2ce6f8c79e58d87f5b6aaf4cd73551dddb3f0a340909d2da0eef244b27eafb01cc0a9a714115f7386101093ee46b7ea73700",
  gasLimit: 4712388,
  gasUsed: 118307,
  hash: "0xf9eb50ed3b38b0330501931c10a7df88bd38a7dbda8e1d6ff94a7814dc75ea10",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0x4110bd1ff0b73fa12c259acf39c950277f266787",
  mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  nonce: "0x0000000000000000",
  number: 1120146,
  parentHash: "0x4b3749b324a71850f93a8e15d4d0eb9e303a8e7cfa91b71c8455ea32731f3280",
  receiptsRoot: "0xea3d6906408005540599d931021621a17cf2d12f28c1d9b6897da37e9dc433e8",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 1118,
  stateRoot: "0x96adc89355a06f41850389d77b2c0c66a14e7664dbd4dbfdaecc988b885eaee9",
  timestamp: 1539676627,
  totalDifficulty: 2805713,
  transactions: ["0x992f2989028f27c1ebae3d48edafdf58be40530afde0f42cb015bdd72504e434"],
  transactionsRoot: "0x262de7ea597052547af9d693a82d1db060a70504dd0e4706e0226343153289c7",
  uncles: []
}

通过区块浏览器查询: 测试网: https://chain.smartmesh.io/ 主网: http://spectrum.pub/

https://chain.smartmesh.io/tx.html?hash=0x992f2989028f27c1ebae3d48edafdf58be40530afde0f42cb015bdd72504e434

屏幕快照 2018-10-16 下午5.02.30.png

合约的布署与调用验证无误,大功告成!

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励