进入 fabcar
$ cd fabric-samples/fabcar
准备环境
杀掉活跃的容器
$ docker rm -f $(docker ps -aq)
清理缓存的网络
$ docker network prune
删除fabcar智能合约的底层链码图像,如果是第一次运行这个项目可以不执行
$ docker rmi dev-peer0.org1.example.com-fabcar-1.0-5c906e402ed29f20260ae42283216aa75549c571e2e380f3615826365d8269ba
安装客户端并启动网络
$ npm install
$ ./startFabric.sh
注册管理员用户
node enrollAdmin.js
注册user1 用户
node registerUser.js
查询账目
node query.js
运行结果:
Store path:/Users/qiuyahao/fabric/fabric-samples/fabcar/hfc-key-store
Successfully loaded user1 from persistence
Query has completed, checking results
Response is [{"Key":"CAR0", "Record":{"colour":"blue","make":"Toyota","model":"Prius","owner":"Tomoko"}},{"Key":"CAR1", "Record":{"colour":"red","make":"Ford","model":"Mustang","owner":"Brad"}},{"Key":"CAR2", "Record":{"colour":"green","make":"Hyundai","model":"Tucson","owner":"Jin Soo"}},{"Key":"CAR3", "Record":{"colour":"yellow","make":"Volkswagen","model":"Passat","owner":"Max"}},{"Key":"CAR4", "Record":{"colour":"black","make":"Tesla","model":"S","owner":"Adriana"}},{"Key":"CAR5", "Record":{"colour":"purple","make":"Peugeot","model":"205","owner":"Michel"}},{"Key":"CAR6", "Record":{"colour":"white","make":"Chery","model":"S22L","owner":"Aarav"}},{"Key":"CAR7", "Record":{"colour":"violet","make":"Fiat","model":"Punto","owner":"Pari"}},{"Key":"CAR8", "Record":{"colour":"indigo","make":"Tata","model":"Nano","owner":"Valeria"}},{"Key":"CAR9", "Record":{"colour":"brown","make":"Holden","model":"Barina","owner":"Shotaro"}}]
查询某一个的账目
修改 query.js , 可以查询key为CAR4 的记录
const request = {
//targets : --- letting this default to the peers assigned to the channel
chaincodeId: 'fabcar',
fcn: 'queryCar',
args: ['CAR4']
};
运行 node query.js,
Response is {"colour":"black","make":"Tesla","model":"S","owner":"Adriana"}
更新账目
修改 invoke.js, 添加一条数据
var request = {
//targets: let default to the peer assigned to the client
chaincodeId: 'fabcar',
fcn: 'createCar',
args: ['CAR10', 'Chevy', 'Volt', 'Red', 'Nick'],
chainId: 'mychannel',
txId: tx_id
};
执行 node invoke.js,可以添加一条数据进入账目
修改 query.js,
const request = {
//targets : --- letting this default to the peers assigned to the channel
chaincodeId: 'fabcar',
fcn: 'queryCar',
args: ['CAR10']
};
运行 node query.js ,
Response is {"colour":"Red","make":"Chevy","model":"Volt","owner":"Nick"}
修改 invoke.js, 修改CAR10的拥有者为 Dave
var request = {
//targets: let default to the peer assigned to the client
chaincodeId: 'fabcar',
fcn: 'changeCarOwner',
args: ['CAR10', 'Dave'],
chainId: 'mychannel',
txId: tx_id
};
执行 node invoke.js, 再运行 node query.js , 运行结果:
Response is {"colour":"Red","make":"Chevy","model":"Volt","owner":"Dave"}