基于以太坊私有链搭建一个分布式应用【3】 | 71街

Home > 感悟 > 基于以太坊私有链搭建一个分布式应用【3】

基于以太坊私有链搭建一个分布式应用【3】0+

3,938 views / 2018.03.15 / 11:11 下午

搭建Dapp(分布式应用)

非常不情愿的安装npm,然后sudo npm install -g truffle

会出现一个很NB的动画安装过程,默默地欣赏…

71j.cn@ubuntu:~$ mkdir dapp

71j.cn@ubuntu:~$ cd dapp

truffle unbox webpack # 初始化一个带ui的truffle项目

/usr/bin/env: ‘node’: No such file or directory

sudo ln -s /usr/bin/nodejs /usr/bin/node

又报错:

SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

因为 node的版本太低

sudo npm install -g n

sudo n stable

再来truffle unbox webpack,成功后可以看下当前的目录结构:

-contracts //智能合约存放目录

-migrations //智能合约abi及部署信息存目录

-test //测试文件

-truffle.js //truffle默认配置

-truffle-config.js //Windows下默认配置文件名与truffle冲突,可使用该文件解决

修改webpack.config.js,添加如下代码制定运行的服务器ip,注意如果只监听localhost, 理论上无法通过IP:PORT的方式访问。

devServer: {

host: ‘10.120.113.245’,

port: 8080,

},

配置truffle.js到私链:

module.exports = {

// See <http://truffleframework.com/docs/advanced/configuration>

// to customize your Truffle configuration!

networks: {

development: {

host: “10.120.113.245”,

port: 8101,

network_id: “*”, // * Match any network id

gas:4712388, //Gas limit used for deploys. Default is web3.eth.getBlock(“pending”).gasLimit==4712388.

gasPrice:18000000000, //Gas price used for deploys. Default is 100000000000 (100 Shannon).

from: “0xf4182f0dc92313f8e106fc033641c3351703911f”//From address used during migrations. Defaults to the first available account provided by your Ethereum client.

// provider – web3 provider instance Truffle should use to talk to the Ethereum network.

//          – function that returns a web3 provider instance (see below.)

//          – if specified, host and port are ignored.

}

},

solc: {

optimizer: {

enabled: true,

runs: 200

}

},

};

 

truffle migrate # Build, compile, & deploy the dap

这个过程比较慢,耐心等待。

npm run dev

就可以通过浏览器http:// 10.120.113.245:8101访问了。

 

部署自己的合约程序

 

deploy//这都是要自己编写的,具体的看代码吧,点这里下载

├── app

│   ├── index.html

│   ├── javascripts

│   │   └── app.js

│   └── stylesheets

│       └── app.css

├── contracts

│   ├── Conference.sol

│   └── Migrations.sol//这个是truffle管理合约用的合约

└── test

└── conference.js

 

rm -rf dapp/app

rm -rf dapp/contracts

rm -rf dapp/test

然后把deploy中的文件都复制到dapp中,进入dapp:

71j.cn@ubuntu:~/dapp$ truffle migrate –reset

Using network ‘development’.

 

Running migration: 1_initial_migration.js

Replacing Migrations…

… 0x08a2db9ceda488eb534f4e0364dc21d569ad7a7d822c12f2007021ff67d8dc3b

Migrations: 0xec2264304e81fe2a3d5f3f316ecd627ea44c285d

Saving successful migration to network…

… 0xfa324a40aac5d0d89da7c05ddb5382bc2c56bd21e92a4f2b5b34325c526c7d5e

Saving artifacts…

Running migration: 2_deploy_contracts.js

Replacing Conference…

… 0x2c45e7be5c5a40770a99c4a59e1f09bdc69168c96d45be62e46c6ba053d77cda

Conference: 0x7d2c18809639c3a019e3b82c3ec39da526a8beb2

Saving successful migration to network…

… 0x37fe62d2822977d15a2261c28f599df0418e567b0a0ed38870e4d556d5f12d4c

Saving artifacts…

 

然后看看我们的测试用例编写的怎样?

appadmin@ubuntu:~/dapp$ truffle test

Using network ‘development’.

 

[ ‘0xf4182f0dc92313f8e106fc033641c3351703911f’,

‘0x899a969874bec249e4ef439c75adb0c358913b95’,

‘0x6797c39109ec4676d3b5dbb4dc76f1143a0f633a’ ]

 

 

Contract: Conference

✓ Initial conference settings should match (2068ms)

✓ Should update quota (3570ms)

1) Should let you buy a ticket

 

Events emitted during test:

—————————

 

Deposit(_from: 0x899a969874bec249e4ef439c75adb0c358913b95, _amount: 1000000000000000000)

 

—————————

✓ Should issue a refund by owner only (10310ms)

 

 

3 passing (20s)

1 failing

 

1) Contract: Conference Should let you buy a ticket:

AssertionError: Difference should be what was sent: expected 11001296558000000000 to equal ‘1000000000000000000’

at test/conference.js:63:11

at <anonymous>

at process._tickCallback (internal/process/next_tick.js:118:7)

 

为什么有一个没通过?因为那哥们在挖矿呢…,把挖矿的改成account[3]就好了。

npm run dev

通过浏览器访问http:// 10.120.113.245:8101即可。

本站内容受著作权法保护。个人 blog 转载时请遵循 “署名-非商业用途-保持一致” 的创作共用协议;商业网站或未授权媒体不得复制本站内容。
Categories: 感悟 Tags:

Comments (0) Trackbacks (0) 本篇共有 0 篇评论↓
  1. No comments yet.
  1. No trackbacks yet.