使用ubuntu系统安装ethereum开发环境
安装 Nodejs
sudo apt-get update
sudo apt install curl
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo apt-get install npm
安装 ethereumjs-testrpc
sudo apt-get install build-essential
sudo apt-get install git
sudo npm install -g ethereumjs-testrpc
执行testrpc
在整个开发过程中需要一直云心testrpc。 每次执行testrpc,都会产生10个带有test funds的新地址。这些不是真正的钱,我们可以放心地进行任何测试而不用损失钱财。
编写Ethereum智能合约的最流行的语言是solidiy。我们会使用truffle 开发框架,它可以帮助我们创建新的智能合约、编译、发布、测试智能合约。
$ sudo npm install -g truffle
$ mkdir solidity-experiments
$ cd solidity-experiments/
$ truffle init
Truffle会为创建一个样例工程而新建一些文件。可以使用truffle compile进行编译。运行truffle migrate来讲合约部署到模拟网络中。
$ truffle compile
Compiling ConvertLib.sol...
Compiling MetaCoin.sol...
Compiling Migrations.sol...
Writing artifacts to ./build/contracts
$ truffle migrate
Using network 'development'.
Running migration: 1_initial_migration.js
Deploying Migrations...
... 0x686ed32f73afdf4a84298642c60e2002a6d0d736a5478cc8cb22a655ac018a67
Migrations: 0xa7edbac1156f98907a24d18df8104b5b1bd7027c
Saving successful migration to network...
... 0xe3bf1e50d2262d9ffb015091e5f2974c8ebe0d6fd0df97a7dbcde8a0e51c694a
Saving artifacts...
Running migration: 2_deploy_contracts.js
Deploying ConvertLib...
... 0x2e0e6718f01d0da6da2ada13d6e4ad662c5a20e784e04c404e9d4ef1d392bdae
ConvertLib: 0xf4388ce4d4ce8a443228d65ecfa5149205db049f
Linking ConvertLib to MetaCoin
Deploying MetaCoin...
... 0xb03a3cde0672a2bd4dda6c01dd31641d95bd680c4e21162b3370ed6db7a5620d
MetaCoin: 0x4fc68713f7ac86bb84ac1ef1a09881a9b8d4100f
Saving successful migration to network...
... 0xb9a2245c27ff1c6506c0bc6349caf86a31bc9f700388defe04566b6d237b54b6
Saving artifacts...
如果遇到# [Truffle Migrate doesn't work: Error: No network specified. Cannot determine current network] 问题,可以编辑truffle.js文件
module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*" // Match any network id
}
}
};
编写第一个Ethereum智能合约
首先我们编写一个简单的ProofofExistence的智能合约。 主要思想就是创建一个数字公证:它存储了文档的哈希值作为存在的证明。使用truffle create contract命令来开始:
$ truffle create contract ProofOfExistence1
现在打开contracts/ProofOfExistence1.sol,然后将以下代码复制到这个文件当中:
pragma solidity ^0.4.15;
// Proof of Existence contract, version 1
contract ProofOfExistence1 {
// state
bytes32 public proof;
// calculate and store the proof for a document
// *transactional function*
function notarize(string document) {
proof = proofFor(document);
}
// helper function to get a document's sha256
// *read-only function*
function proofFor(string document) constant returns (bytes32) {
return sha256(document);
}
}
每一个合约都有状态state和函数functions。在智能合约中区分两种类型的函数是十分重要的:
- 只读(常量)函数:此函数不会产生任何状态state改变。它们只会读取状态,进行计算,返回值。这些函数会被各种节点在本地计算,不会消耗gas。会被关键字constant标记。
- 交易型函数: 会对状态进行改变的函数。这些改变会在区块链中有所体现,交易型函数需要将交易发送到网络中,会消耗gas、
下面我们将ProofOfExistence1部署到网络中。我们需要编辑migration 文件migrations/2_deploy_contracts.js 让truffle来部署新的交易。
var ProofOfExistence1 = artifacts.require("./ProofOfExistence1.sol");
module.exports = function(deployer) {
deployer.deploy(ProofOfExistence1);
};
需要重新运行migration
truffle migrate --reset
和智能合约进行交互
我们的合约已经部署好了,我们可以测试一下。我们使用函数调用的方式发送消息,读取public state。我们可以使用truffle console来做这些
$ truffle console
// get the deployed version of our contract
truffle(default)> var poe = ProofOfExistence1.at(ProofOfExistence1.address)
// and print its address
truffle(default)> poe.address
0x3d3bce79cccc331e9e095e8985def13651a86004
// let's register our first "document"
truffle(default)> poe.notarize('An amazing idea')
{ tx: '0x18ac...cb1a',
receipt:
{ transactionHash: '0x18ac...cb1a',
...
},
logs: [] }
// let's now get the proof for that document
truffle(default)> poe.proofFor('An amazing idea')
0xa3287ff8d1abde95498962c4e1dd2f50a9f75bd8810bd591a64a387b93580ee7
// To check if the contract's state was correctly changed:
truffle(default)> poe.proof()
0xa3287ff8d1abde95498962c4e1dd2f50a9f75bd8810bd591a64a387b93580ee7
// The hash matches the one we previously calculated
首先是要获取部署的合约存入到poe的变量中。然后调用交易函数notarize,它包含了一个状态的改变。当我们调用一个交易函数时,我们获得了一个promise来解析一个交易对象,而不是实际的函数返回。改变EVM的状态我们需要支付gas,然后对网络发送一笔交易。这就是为什么我们得到一个交易信息对象作为promise的结果,指的是这种状态改变的交易。在这种情况下,我们对交易ID不感兴趣,所以我们不用考虑promise。在编写一个真正的应用程序的时候,我们需要保存它来检查生成的交易并捕获错误。
接下来,我们调用只读(常量)函数proofFor。一定要使用关键字constant来标记只读函数,否则Truffle将尝试创建一笔交易来执行他们。这是一种告诉truffle我们不会和区块链进行交互而仅仅是读取值。使用这个只读函数,我们获得了文档的哈希值。
我们现在需要将其与我们的智能合约的状态形成对比。要检查状态是否正确改变,我们需要读取证明公共状态变量。为了获得一个公共状态变量的值,我们可以调用一个同名的函数,它返回一个值的Promise。在我们的例子中,输出哈希值是相同的。
支持多个文档proofs
创建新文件contracts/ProofOfExistence2.sol
pragma solidity ^0.4.15;
// Proof of Existence contract, version 2
contract ProofOfExistence2 {
// state
bytes32[] private proofs;
// store a proof of existence in the contract state
// *transactional function*
function storeProof(bytes32 proof) {
proofs.push(proof);
}
// calculate and store the proof for a document
// *transactional function*
function notarize(string document) {
bytes32 proof = proofFor(document);
storeProof(proof);
}
// helper function to get a document's sha256
// *read-only function*
function proofFor(string document) constant returns (bytes32) {
return sha256(document);
}
// check if a document has been notarized
// *read-only function*
function checkDocument(string document) constant returns (bool) {
bytes32 proof = proofFor(document);
return hasProof(proof);
}
// returns true if proof is stored
// *read-only function*
function hasProof(bytes32 proof) constant returns (bool) {
for (uint256 i = 0; i < proofs.length; i++) {
if (proofs[i] == proof) {
return true;
}
}
return false;
}
}
使用了private的proofs数组存储
之后我们可以进行交互
// deploy contracts
truffle(default)> migrate --reset
// Get the new version of the contract
truffle(default)> var poe = ProofOfExistence2.at(ProofOfExistence2.address)
// let's check for some new document, and it shouldn't be there.
truffle(default)> poe.checkDocument('hello')
false
// let's now add that document to the proof store
truffle(default)> poe.notarize('hello')
{ tx: '0x1d2d...413f',
receipt: { ... },
logs: []
}
// let's now check again if the document has been notarized!
truffle(default)> poe.checkDocument('hello')
true
// success!
// we can also store other documents and they are recorded too
truffle(default)> poe.notarize('some other document');
truffle(default)> poe.checkDocument('some other document')
true
这个版本比上一个要好,但仍旧存在某些问题。每次我们检查一个文档是否被公证(notarize),我们需要遍历整个proofs数组。当文档数增加时,这会使得交易损耗更多的gas。存储proof的更好地数据结构是map。Solidity是支持map操作的,称之为mapping。
pragma solidity ^0.4.15;
// Proof of Existence contract, version 3
contract ProofOfExistence3 {
mapping (bytes32 => bool) private proofs;
// store a proof of existence in the contract state
function storeProof(bytes32 proof) {
proofs[proof] = true;
}
// calculate and store the proof for a document
function notarize(string document) {
var proof = proofFor(document);
storeProof(proof);
}
// helper function to get a document's sha256
function proofFor(string document) constant returns (bytes32) {
return sha256(document);
}
// check if a document has been notarized
function checkDocument(string document) constant returns (bool) {
var proof = proofFor(document);
return hasProof(proof);
}
// returns true if proof is stored
function hasProof(bytes32 proof) constant returns(bool) {
return proofs[proof];
}
}
网友评论