以太坊开发(truffle框架)验证区块链记录合约相关
基于上一个文章 https://blog.xyj2156.top/posts/eth_truffle_03
修改合约内容
这里为了简单省事,保留构造函数,这样不用修改 部署文件了。
pragma solidity ^0.4.23;
contract Hello {
string Name;
string testValue;
constructor (string name) {
Name = name;
}
function say() constant public returns (string){
return Name;
}
function setValue(string test) public {
testValue = test;
}
function getValue() constant public returns (string){
return testValue;
}
}
部署合约
保证testrpc
运行状态。
执行 truffle migrate --reset
控制台输出:
Using network 'development'.
Running migration: 1_helle.js
Deploying Hello...
... 0x7881bccb81f7bdc16bc1ac6788bcfce8d668d94909e237edbb65aa4965f30a1c
Hello: 0xa9147b013b26173f5cd4a2a22a12ea86787f840d // 合约地址
Saving artifacts...
Running migration: 1_initial_migration.js
Deploying Migrations...
... 0x2fae0e5587f061443a040629c6b06f5699584aa763f3ba822e49fc0e3a10c4e0
Migrations: 0xaa2ed926016b6dc96d5a820ea17f5ca9931fdd0b
Saving successful migration to network...
... 0x289ce60b22425a5e3d77ee35f89ed99482ed49543c7b00ae96b9771fd2511002
Saving artifacts...
测试状态记录
进入控制台 truffle console
- 首先获取合约的两个实例
a = Hello.at('0xa9147b013b26173f5cd4a2a22a12ea86787f840d');
b = Hello.at('0xa9147b013b26173f5cd4a2a22a12ea86787f840d');
- 调用 a 实例的 setValue 方法
a.setValue('testValue change')
此时,testrpc
会输出信息,应该是模拟挖矿。
验证是否记录数据
- 通过 b 实例的 getValue 获取设置的内容。
b.getValue();
,此时看到了 a实例设置的值。 - 重新获取一个Hello的实例,
c = Hello.at('0xa9147b013b26173f5cd4a2a22a12ea86787f840d');
- 通过 c实例 获取设置的内容。
c.getValue()
,发现竟然也能获取 a实例设置的值。
- 通过 b 实例的 getValue 获取设置的内容。
- 至此,对区块链记录数据的了解深入几分。
评论已关闭