this image is used to test web3.js apis.
441
pragma solidity >=0.4.22<0.5.0;
Contract Indentity {
# 定义一个结构体来表示一个人的相关信息
struct Identity {
string name;
Sex sex;
uint timestamp;
}
enum Sex {
Female,
Male
}
// 定义一个字典,key为文件某个人的ID,value为这个人的相关信息
mapping(string => Identity) identities;
// 定义一个事件,用来通知操作的相关信息
event logAddOnePersonStatus(bool status,uint timestamp,string namer,string id,string message);
// 定义一个函数,用于用于添加某人用户信息
function person_set(string name,string id,Sex sex) public {
// 由于在solidity的字典中,没有办法能够判断一个key是否在字典中已经存在,
// 所以这里我们通过检查其timestamp是否为0来确认用户信息是否已经存在于区块链上面。
if (identities[id].timestamp==0) {
identities[id] = Identity({name: name,timestamp: block.timestamp,sex: sex});
emit logAddOnePersonStatus(true,block.timestamp,name,id,"add informaiton succeed.");
}else {
emit logAddOnePersonStatus(false,block.timestamp,name,id,"person information already exists.");
}
}
// 定义一个获取某人信息的函数get
function person_get(string pid) public view returns (uint timestamp,string name,string id,Sex sex) {
return (identities[pid].timestamp,identities[pid].name,pid,identities[pid].sex);
}
// print the help information
function help_information() public pure returns (string){
return "function: person_set(string name,string id,Sex sex),function: person_get(string id),event: logAddOnePersonStatus";
}
}
# docker run -d --name web3js-test \
-p 8545:8545 \
-p 8546:8546 \
-p 30303:30303 \
happy365/web3js-test:geth-1.9.1 \
sh /root/run.sh
# docker exec -ti web3js-test sh
# geth attach ipc:/root/.ethereum/geth.ipc
Content type
Image
Digest
Size
20.4 MB
Last updated
about 7 years ago
docker pull happy365/web3js-test:geth-1.9.1