Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
blockchain_guide
提交
e1cd41dd
B
blockchain_guide
项目概览
OpenDocCN
/
blockchain_guide
通知
0
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
blockchain_guide
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
e1cd41dd
编写于
7月 20, 2016
作者:
B
Baohua Yang
提交者:
GitHub
7月 20, 2016
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #10 from swbsin/master
Add more chaincode example implementations
上级
8d64dbb2
bb03364f
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
1349 addition
and
71 deletion
+1349
-71
hyperledger/chaincode_example04.go
hyperledger/chaincode_example04.go
+335
-21
hyperledger/chaincode_example04.md
hyperledger/chaincode_example04.md
+128
-6
hyperledger/chaincode_example05.go
hyperledger/chaincode_example05.go
+216
-19
hyperledger/chaincode_example05.md
hyperledger/chaincode_example05.md
+98
-5
hyperledger/chaincode_example06.go
hyperledger/chaincode_example06.go
+402
-10
hyperledger/chaincode_example06.md
hyperledger/chaincode_example06.md
+170
-10
未找到文件。
hyperledger/chaincode_example04.go
浏览文件 @
e1cd41dd
...
...
@@ -20,8 +20,8 @@ import (
type
SimpleChaincode
struct
{
}
var
schoolId
int
=
0
var
Record
Id
int
=
0
var
BackGroundNo
int
=
0
var
Record
No
int
=
0
type
School
struct
{
Name
string
...
...
@@ -35,37 +35,104 @@ type School struct{
type
Student
struct
{
Name
string
Address
string
BackgroundId
[]
string
BackgroundId
[]
int
}
//当离开学校才能记入
type
Background
struct
{
SchoolAddress
string
StartTime
string
EndTime
string
Status
//0:退学 1:毕业
Id
int
ExitTime
int64
Status
string
//0:毕业 1:退学
}
type
Record
struct
{
Id
int
SchoolAddress
string
StudentAddress
string
ModifyTime
string
SchoolSign
string
ModifyTime
int64
ModifyOperation
string
}
func
(
t
*
SimpleChaincode
)
Init
(
stub
*
shim
.
ChaincodeStub
,
function
string
,
args
[]
string
)
([]
byte
,
error
)
{
if
len
(
args
)
!=
0
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 0"
)
if
function
==
"createSchool"
{
return
t
.
createSchool
(
stub
,
args
)
}
else
if
function
==
"createStudent"
{
return
t
.
createStudent
(
stub
,
args
)
}
return
nil
,
nil
}
func
(
t
*
SimpleChaincode
)
Invoke
(
stub
*
shim
.
ChaincodeStub
,
function
string
,
args
[]
string
)
([]
byte
,
error
)
{
if
function
==
"enrollStudent"
{
if
len
(
args
)
!=
3
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 1"
)
}
return
t
.
enrollStudent
(
stub
,
args
)
}
else
if
function
==
"updateDiploma"
{
if
len
(
args
)
!=
4
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 1"
)
}
return
t
.
updateDiploma
(
stub
,
args
)
}
return
nil
,
nil
}
func
(
t
*
SimpleChaincode
)
Query
(
stub
*
shim
.
ChaincodeStub
,
function
string
,
args
[]
string
)
([]
byte
,
error
)
{
if
function
==
"getStudentByAddress"
{
if
len
(
args
)
!=
1
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 1"
)
}
_
,
stuBytes
,
err
:=
getStudentByAddress
(
stub
,
args
[
0
])
if
err
!=
nil
{
fmt
.
Println
(
"Error get centerBank"
)
return
nil
,
err
}
return
stuBytes
,
nil
}
else
if
function
==
"getRecordById"
{
if
len
(
args
)
!=
1
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 1"
)
}
_
,
recBytes
,
err
:=
getRecordById
(
stub
,
args
[
0
])
if
err
!=
nil
{
fmt
.
Println
(
"Error get centerBank"
)
return
nil
,
err
}
return
recBytes
,
nil
}
else
if
function
==
"getRecords"
{
if
len
(
args
)
!=
0
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 0"
)
}
records
,
err
:=
getRecords
(
stub
)
if
err
!=
nil
{
fmt
.
Println
(
"Error unmarshalling"
)
return
nil
,
err
}
recBytes
,
err1
:=
json
.
Marshal
(
&
records
)
if
err1
!=
nil
{
fmt
.
Println
(
"Error marshalling banks"
)
}
return
recBytes
,
nil
}
else
if
function
==
"getSchoolByAddress"
{
if
len
(
args
)
!=
1
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 1"
)
}
_
,
schBytes
,
err
:=
getSchoolByAddress
(
stub
,
args
[
0
])
if
err
!=
nil
{
fmt
.
Println
(
"Error get centerBank"
)
return
nil
,
err
}
return
schBytes
,
nil
}
else
if
function
==
"getBackgroundById"
{
if
len
(
args
)
!=
1
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 1"
)
}
_
,
backBytes
,
err
:=
getBackgroundById
(
stub
,
args
[
0
])
if
err
!=
nil
{
fmt
.
Println
(
"Error get centerBank"
)
return
nil
,
err
}
return
backBytes
,
nil
}
return
nil
,
nil
}
...
...
@@ -91,41 +158,288 @@ func GetAddress() (string,string,string) {
address
=
hex
.
EncodeToString
(
h
.
Sum
(
nil
))
priKey
=
address
+
"1"
pubKey
=
address
+
"2"
var
address
,
return
address
,
priKey
,
pubKey
}
func
(
t
*
SimpleChaincode
)
createSchool
(
stub
*
shim
.
ChaincodeStub
,
args
[]
string
)
([]
byte
,
error
)
{
return
nil
,
nil
if
len
(
args
)
!=
2
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 2"
)
}
var
school
School
var
schoolBytes
[]
byte
var
stuAddress
[]
string
var
address
,
priKey
,
pubKey
string
address
,
priKey
,
pubKey
=
GetAddress
()
school
=
School
{
Name
:
args
[
0
],
Location
:
args
[
1
],
Address
:
address
,
PriKey
:
priKey
,
PubKey
:
pubKey
,
StudentAddress
:
studentAddress
}
err
:=
writeSchool
(
stub
,
school
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"write Error"
+
err
.
Error
())
}
schoolBytes
,
err
=
json
.
Marshal
(
&
school
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error retrieving schoolBytes"
)
}
return
schoolBytes
,
nil
}
func
(
t
*
SimpleChaincode
)
createStudent
(
stub
*
shim
.
ChaincodeStub
,
args
[]
string
)
([]
byte
,
error
)
{
return
nil
,
nil
if
len
(
args
)
!=
1
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 1"
)
}
var
student
Student
var
studentBytes
[]
byte
var
stuAddress
string
var
bgId
[]
int
stuAddress
,
_
,
_
=
GetAddress
()
student
=
Student
{
Name
:
args
[
0
],
Address
:
stuAddress
,
BackgroundId
,
bgId
}
err
:=
writeStudent
(
stub
,
student
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"write Error"
+
err
.
Error
())
}
studentBytes
,
err
=
json
.
Marshal
(
&
student
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error retrieving schoolBytes"
)
}
return
studentBytes
,
nil
}
func
(
t
*
SimpleChaincode
)
enrollStudent
(
stub
*
shim
.
ChaincodeStub
,
args
[]
string
)
([]
byte
,
error
)
{
return
nil
,
nil
}
school
,
schBytes
,
error
:=
getSchoolByAddress
(
stub
,
args
[
0
])
if
error
!=
nil
{
return
nil
,
errors
.
New
(
"Error get data"
)
}
student
,
stuBytes
,
err
:=
getStudentByAddress
(
stub
,
args
[
2
])
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error get data"
)
}
schoolSign
:=
args
[
1
]
var
record
Record
record
=
Record
{
Id
:
RecordNo
,
SchoolAddress
:
args
[
0
],
StudentAddress
:
args
[
2
],
SchoolSign
:
schoolSign
,
ModifyTime
:
time
.
Now
()
.
Unix
(),
ModifyOperation
:
"2"
}
err
=
writeRecord
(
stub
,
record
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error write data"
)
}
schoolAddress
.
StudentAddress
=
append
(
schoolAddress
.
StudentAddress
,
student
.
Address
)
err
=
writeSchool
(
stub
,
School
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error write data"
)
}
err
=
writeStudent
(
stub
,
School
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error write data"
)
}
RecordNo
=
RecordNo
+
1
recordBytes
,
err
=
json
.
Marshal
(
&
record
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error retrieving schoolBytes"
)
}
return
recordBytes
,
nil
}
func
(
t
*
SimpleChaincode
)
updateDiploma
(
stub
*
shim
.
ChaincodeStub
,
args
[]
string
)
([]
byte
,
error
)
{
return
nil
,
nil
var
recordBytes
[]
byte
school
,
schBytes
,
error
:=
getSchoolByAddress
(
stub
,
args
[
0
])
if
error
!=
nil
{
return
nil
,
errors
.
New
(
"Error get data"
)
}
student
,
stuBytes
,
err
:=
getStudentByAddress
(
stub
,
args
[
2
])
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error get data"
)
}
schoolSign
:=
args
[
1
]
var
record
Record
record
=
Record
{
Id
:
RecordNo
,
SchoolAddress
:
args
[
0
],
StudentAddress
:
args
[
2
],
SchoolSign
:
schoolSign
,
ModifyTime
:
time
.
Now
()
.
Unix
(),
ModifyOperation
:
args
[
3
]}
err
=
writeRecord
(
stub
,
record
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error write data"
)
}
var
background
Background
background
=
Background
{
Id
:
BackGroundNo
,
ExitTime
:
time
.
Now
()
.
Unix
(),
Status
:
args
[
3
]}
err
=
writeBackground
(
stub
,
background
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error write data"
)
}
BackGroundNo
=
BackGroundNo
+
1
recordBytes
,
err
=
json
.
Marshal
(
&
record
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error retrieving schoolBytes"
)
}
err
=
writeStudent
(
stub
,
student
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error write data"
)
}
err
=
writeSchool
(
stub
,
school
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error write data"
)
}
return
recordBytes
,
nil
}
func
getStudentByAddress
(
stub
*
shim
.
ChaincodeStub
,
id
string
)
([]
Background
,[]
byte
,
error
)
{
return
nil
,
nil
,
nil
func
getStudentByAddress
(
stub
*
shim
.
ChaincodeStub
,
address
string
)
(
Student
,[]
byte
,
error
)
{
var
student
Student
stuBytes
,
err
:=
stub
.
GetState
(
"address"
)
if
err
!=
nil
{
fmt
.
Println
(
"Error retrieving data"
)
}
err
=
json
.
Unmarshal
(
stuBytes
,
&
student
)
if
err
!=
nil
{
fmt
.
Println
(
"Error unmarshalling data"
)
}
return
student
,
stuBytes
,
nil
}
func
getSchoolByAddress
(
stub
*
shim
.
ChaincodeStub
,
address
string
)(
School
,[]
byte
,
error
){
var
school
School
schBytes
,
err
:=
stub
.
GetState
(
"address"
)
if
err
!=
nil
{
fmt
.
Println
(
"Error retrieving data"
)
}
err
=
json
.
Unmarshal
(
schBytes
,
&
school
)
if
err
!=
nil
{
fmt
.
Println
(
"Error unmarshalling data"
)
}
return
school
,
schBytes
,
nil
}
func
getRecordById
(
stub
*
shim
.
ChaincodeStub
,
id
string
)
(
Record
,[]
byte
,
error
)
{
return
nil
,
nil
,
nil
var
record
Record
recBytes
,
err
:=
stub
.
GetState
(
"Record"
+
id
)
if
err
!=
nil
{
fmt
.
Println
(
"Error retrieving data"
)
}
err
=
json
.
Unmarshal
(
recBytes
,
&
Record
)
if
err
!=
nil
{
fmt
.
Println
(
"Error unmarshalling data"
)
}
return
record
,
recBytes
,
nil
}
func
getRecords
(
stub
*
shim
.
ChaincodeStub
)
([]
Record
,
error
)
{
var
records
[]
Record
var
number
string
var
err
error
var
record
Record
if
RecordNo
<=
10
{
i
:=
0
for
i
<
RecordNo
{
number
=
strconv
.
Itoa
(
i
)
record
,
_
,
err
=
getRecordById
(
stub
,
number
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error get detail"
)
}
records
=
append
(
records
,
record
)
i
=
i
+
1
}
}
else
{
i
:=
0
for
i
<
10
{
number
=
strconv
.
Itoa
(
i
)
record
,
_
,
err
=
getRecordById
(
stub
,
number
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error get detail"
)
}
records
=
append
(
records
,
record
)
i
=
i
+
1
}
return
records
,
nil
}
return
nil
,
nil
}
func
writeRecord
(
stub
*
shim
.
ChaincodeStub
,
centerBank
CenterBank
)
(
error
)
{
func
getBackgroundById
(
stub
*
shim
.
ChaincodeStub
,
id
string
)(
Background
,[]
byte
,
error
){
var
background
Background
backBytes
,
err
:=
stub
.
GetState
(
"BackGround"
+
id
)
if
err
!=
nil
{
fmt
.
Println
(
"Error retrieving data"
)
}
err
=
json
.
Unmarshal
(
backBytes
,
&
BackGround
)
if
err
!=
nil
{
fmt
.
Println
(
"Error unmarshalling data"
)
}
return
background
,
backBytes
,
nil
}
func
writeRecord
(
stub
*
shim
.
ChaincodeStub
,
record
Record
)
(
error
)
{
var
recId
string
recordBytes
,
err
:=
json
.
Marshal
(
&
record
)
if
err
!=
nil
{
return
err
}
recId
,
_
=
strconv
.
Itoa
(
record
.
Id
)
err
=
stub
.
PutState
(
"Record"
+
recId
,
recordBytes
)
if
err
!=
nil
{
return
errors
.
New
(
"PutState Error"
+
err
.
Error
())
}
return
nil
}
func
writeSchool
(
stub
*
shim
.
ChaincodeStub
,
school
School
)(
error
){
schBytes
,
err
:=
json
.
Marshal
(
&
school
)
if
err
!=
nil
{
return
err
}
err
=
stub
.
PutState
(
school
.
Address
,
schBytes
)
if
err
!=
nil
{
return
errors
.
New
(
"PutState Error"
+
err
.
Error
())
}
return
nil
}
func
writeStudent
(
stub
*
shim
.
ChaincodeStub
,
student
Student
)(
error
){
stuBytes
,
err
:=
json
.
Marshal
(
&
student
)
if
err
!=
nil
{
return
err
}
err
=
stub
.
PutState
(
student
.
Address
,
stuBytes
)
if
err
!=
nil
{
return
errors
.
New
(
"PutState Error"
+
err
.
Error
())
}
return
nil
}
func
writeBackground
(
stub
*
shim
.
ChaincodeStub
,
background
Background
)(
error
){
var
backId
string
backBytes
,
err
:=
json
.
Marshal
(
&
background
)
if
err
!=
nil
{
return
err
}
backId
,
_
=
strconv
.
Itoa
(
background
.
Id
)
err
=
stub
.
PutState
(
"BackGround"
+
backId
,
backBytes
)
if
err
!=
nil
{
return
errors
.
New
(
"PutState Error"
+
err
.
Error
())
}
return
nil
}
\ No newline at end of file
hyperledger/chaincode_example04.md
浏览文件 @
e1cd41dd
...
...
@@ -18,28 +18,150 @@
-
所在位置
-
账号地址
-
账号公钥
-
账户私钥
-
学校学生
-
个人
-
姓名
-
账号地址
-
过往学历
-
学历信息
-
学历信息编号
-
就读学校
-
就读年份
-
完成就读年份
-
就读状态 //0:毕业 1:退学
-
修改记录(入学也相当于一种修改记录)
-
编号
-
学校账户地址(一般根据账户地址可以算出公钥地址,然后可以进行校验)
-
学校签名
-
个人账户地址
-
个人公钥地址(个人不需要公钥地址)
-
修改时间
-
修改操作//0:正常毕业 1:退学 2:入学
对学历操作信息所有的操作都归为记录。
#### function及各自实现的功能
-
`init`
初始化函数,并创建一所学校
-
`invoke`
调用合约内部的函数
-
`query`
查询相关的信息
-
`updateDiploma`
由学校更新学生学历信息,并签名
-
`enrollStudent`
学校招生
-
`addSchool`
添加一名新学校
-
`addStudent`
添加一名新学生
-
`getStudentByAddress`
通过学生的地址访问学生的账号信息
-
`updateDiploma`
由学校更新学生学历信息,并签名(返回记录信息) invoke
-
`enrollStudent`
学校招生(返回学校信息) invoke
-
`createSchool`
添加一名新学校 init
-
`createStudent`
添加一名新学生 init
-
`getStudentByAddress`
通过学生的地址访问学生的学历信息 query
-
`getRecordById`
通过Id获取记录 query
-
`getRecords`
获取全部记录(如果记录数大于10,返回前10个) query
-
`getSchoolByAddress`
通过地址获取学校的信息
-
`getBackgroundById`
通过地点获取所存储的学历信息
-
`writeRecord`
写入记录
-
`writeSchool`
写入新创建的学校
-
`writeStudent`
写入新创建的学生
#### 接口设计
`createSchool`
request参数:
```
args[0] 学校名称
args[1] 学校所在位置
```
response参数:
```
学校信息的json表示,当创建一所新学校时,该学校学生账户地址列表为空
```
`createStudent`
request参数:
```
args[0] 学生的姓名
```
response参数:
```
学生信息的json表示,刚创建过往学历信息列表为空
```
`updateDiploma`
request参数
```
args[0] 学校账户地址
args[1] 学校签名
args[2] 待修改学生的账户地址
args[3] //对该学生的学历进行怎样的修改,0:正常毕业 1:退学
```
response参数
```
返回修改记录的json表示
```
`enrollStudent`
request参数:
```
args[0] 学校账户地址
args[1] 学校签名
args[2] 学生账户地址
```
response参数
```
返回修改记录的json表示
```
`getStudentByAddress`
request参数
```
args[0] address
```
response参数
```
学生信息的json表示
```
`getRecordById`
request参数
```
args[0] 修改记录的ID
```
response参数
```
修改记录的json表示
```
`getRecords`
response参数
```
获取修改记录数组(如果个数大于10,返回前10个)
```
`getSchoolByAddress`
request参数
```
args[0] address
```
response参数
```
学校信息的json表示
```
`getBackgroundById`
request参数
```
args[0] ID
```
response参数
```
学历信息的json表示
```
#### 测试
hyperledger/chaincode_example05.go
浏览文件 @
e1cd41dd
...
...
@@ -23,7 +23,7 @@ type SimpleChaincode struct {
}
var
homeNo
int
=
0
var
transaction
Id
int
=
0
var
transaction
No
int
=
0
type
Home
struct
{
Address
string
...
...
@@ -39,23 +39,30 @@ type Transaction struct{
BuyerAddress
string
BuyerAddressSign
string
SellerAddress
string
SellerAddressSign
string
Energy
int
Money
int
Id
int
Time
string
Time
int64
}
func
(
t
*
SimpleChaincode
)
Init
(
stub
*
shim
.
ChaincodeStub
,
function
string
,
args
[]
string
)
([]
byte
,
error
)
{
if
len
(
args
)
!=
0
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 0"
)
if
function
==
"createUser"
{
return
t
.
createUser
(
stub
,
args
)
}
return
nil
,
nil
}
func
(
t
*
SimpleChaincode
)
Invoke
(
stub
*
shim
.
ChaincodeStub
,
function
string
,
args
[]
string
)
([]
byte
,
error
)
{
if
function
==
"createUser"
{
return
t
.
createUser
(
stub
,
args
)
if
function
==
"changeStatus"
{
if
len
(
args
)
!=
3
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 1"
)
}
return
t
.
changeStatus
(
stub
,
args
)
}
else
if
function
==
"buyByAddress"
{
if
len
(
args
)
!=
4
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 1"
)
}
return
t
.
buyByAddress
(
stub
,
args
)
}
return
nil
,
errors
.
New
(
"Received unknown function invocation"
)
}
...
...
@@ -69,8 +76,45 @@ func (t *SimpleChaincode) Query(stub *shim.ChaincodeStub, function string, args
if
err
!=
nil
{
fmt
.
Println
(
"Error get home"
)
return
nil
,
err
}
}
return
homeBytes
,
nil
}
else
if
function
==
"getHomes"
{
if
len
(
args
)
!=
0
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 0"
)
}
homes
,
err
:=
getHomes
(
stub
)
if
err
!=
nil
{
fmt
.
Println
(
"Error unmarshalling"
)
return
nil
,
err
}
homeBytes
,
err1
:=
json
.
Marshal
(
&
homes
)
if
err1
!=
nil
{
fmt
.
Println
(
"Error marshalling banks"
)
}
return
homeBytes
,
nil
}
else
if
function
==
"getTransactions"
{
if
len
(
args
)
!=
0
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 0"
)
}
transactions
,
err
:=
getTransactions
(
stub
)
if
err
!=
nil
{
fmt
.
Println
(
"Error unmarshalling"
)
return
nil
,
err
}
txBytes
,
err1
:=
json
.
Marshal
(
&
transaction
)
if
err1
!=
nil
{
fmt
.
Println
(
"Error marshalling data"
)
}
return
txBytes
,
nil
}
else
if
function
==
"getTransactionById"
{
if
len
(
args
)
!=
1
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 1"
)
}
_
,
txBytes
,
err
:=
getTransactionById
(
stub
,
args
[
0
])
if
err
!=
nil
{
return
nil
,
err
}
return
txBytes
,
nil
}
return
nil
,
errors
.
New
(
"Received unknown function invocation"
)
}
...
...
@@ -130,16 +174,72 @@ func (t *SimpleChaincode) createUser(stub *shim.ChaincodeStub, args []string) ([
return
homeBytes
,
nil
}
func
writeHome
(
stub
*
shim
.
ChaincodeStub
,
home
Home
)
(
error
)
{
homeBytes
,
err
:=
json
.
Marshal
(
&
home
)
if
err
!=
nil
{
return
err
func
buyByAddress
(
stub
*
shim
.
ChaincodeStub
,
args
[]
string
)
(
[]
byte
,
error
)
{
if
len
(
args
)
!=
4
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 4"
)
}
err
=
stub
.
PutState
(
home
.
Address
,
homeBytes
)
if
err
!=
nil
{
return
errors
.
New
(
"PutState Error"
+
err
.
Error
())
homeSeller
,
homeSellerBytes
,
err
:=
getHomeByAddress
(
stub
,
args
[
0
])
homeBuyer
,
homeBuyerBytes
,
error
:=
getHomeByAddress
(
stub
,
args
[
2
])
if
args
[
1
]
!=
args
[
2
]
+
"11"
{
buyValue
,
erro
:=
strconv
.
Atoi
(
args
[
3
])
if
erro
!=
nil
{
return
nil
,
Errors
.
New
(
"want integer number"
)
}
if
homeSeller
.
Energy
<
buyValue
&&
homeBuyer
.
Money
<
buyValue
{
return
nil
,
Errors
.
New
(
"not enough money or energy"
)
}
homeSeller
.
Energy
=
homeSeller
.
Energy
-
buyValue
homeSeller
.
Money
=
homeSeller
.
Money
+
buyValue
homeBuyer
.
Energy
=
homeBuyer
.
Energy
+
buyValue
homeBuyer
.
Money
=
homeBuyer
.
Money
-
buyValue
err
=
writeHome
(
stub
,
homeSeller
)
if
err
==
nil
{
return
nil
,
err
}
err
=
writeHome
(
stub
,
homeBuyer
)
if
err
==
nil
{
return
nil
,
err
}
transaction
:=
Transaction
{
BuyAddress
:
args
[
2
],
BuyerAddressSign
:
args
[
1
],
SellerAddress
:
args
[
0
],
Energy
:
buyValue
,
Money
:
buyValue
,
Id
:
transactionNo
,
Time
:
time
.
Now
()
.
Unix
()}
err
=
writeTransaction
(
stub
,
transaction
)
if
err
!=
nil
{
return
nil
,
err
}
transactionNo
=
transactionNo
+
1
txBytes
,
err
=
json
.
Marshal
(
&
transaction
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error retrieving schoolBytes"
)
}
return
txBytes
,
nil
}
return
nil
return
nil
,
err
}
func
changeStatus
(
stub
*
shim
.
ChaincodeStub
,
args
[]
string
)([]
byte
,
error
){
if
len
(
args
)
!=
3
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 3"
)
}
home
,
homeBytes
,
err
:=
getHomeByAddress
(
stub
,
args
[
0
])
if
err
!=
nil
{
return
nil
}
if
args
[
1
]
==
args
[
0
]
+
"11"
{
status
,
_
:=
strconv
.
Atoi
(
args
[
2
])
home
.
Status
=
status
err
=
writeHome
(
stub
,
home
)
if
err
!=
nil
{
return
homeBytes
,
nil
}
}
return
nil
,
err
}
func
getHomeByAddress
(
stub
*
shim
.
ChaincodeStub
,
address
string
)
(
Home
,[]
byte
,
error
)
{
...
...
@@ -155,10 +255,107 @@ func getHomeByAddress(stub *shim.ChaincodeStub, address string) (Home,[]byte, er
return
home
,
homeBytes
,
nil
}
func
getHomesByEnergyline
(
stub
*
shim
.
ChaincodeStub
,
address
string
)([]
Home
,[]
byte
,
error
){
return
nil
,
nil
,
nil
func
getHomes
(
stub
*
shim
.
ChaincodeStub
,
address
string
)([]
Home
,
error
){
var
homes
[]
Home
var
number
string
var
err
error
var
home
Home
if
homeNo
<=
10
{
i
:=
0
for
i
<
homeNo
{
number
=
strconv
.
Itoa
(
i
)
home
,
_
,
err
=
getHomeById
(
stub
,
number
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error get detail"
)
}
homes
=
append
(
homes
,
home
)
i
=
i
+
1
}
}
else
{
i
:=
0
for
i
<
10
{
number
=
strconv
.
Itoa
(
i
)
home
,
_
,
err
=
getHomeById
(
stub
,
number
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error get detail"
)
}
homes
=
append
(
homes
,
home
)
i
=
i
+
1
}
return
homes
,
nil
}
return
nil
,
nil
}
func
buyByAddress
(
stub
*
shim
.
ChaincodeStub
,
args
[]
string
)
(
[]
byte
,
error
)
{
func
getTransactionById
(
stub
*
shim
.
ChaincodeStub
,
id
string
)(
Transaction
,[]
byte
,
error
){
var
transaction
Transaction
txBytes
,
err
:=
stub
.
GetState
(
"transaction"
+
id
)
if
err
!=
nil
{
fmt
.
Println
(
"Error retrieving home"
)
}
err
=
json
.
Unmarshal
(
txBytes
,
&
transaction
)
if
err
!=
nil
{
fmt
.
Println
(
"Error unmarshalling home"
)
}
return
transaction
,
txBytes
,
nil
}
func
getTransactions
(
stub
*
shim
.
ChaincodeStub
)([]
Transaction
,
error
){
var
transactions
[]
Transaction
var
number
string
var
err
error
var
transaction
Transaction
if
transactionNo
<=
10
{
i
:=
0
for
i
<
transactionNo
{
number
=
strconv
.
Itoa
(
i
)
transaction
,
_
,
err
=
getTransactioinById
(
stub
,
number
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error get detail"
)
}
transactions
=
append
(
transactions
,
transaction
)
i
=
i
+
1
}
}
else
{
i
:=
0
for
i
<
10
{
number
=
strconv
.
Itoa
(
i
)
transaction
,
_
,
err
=
getTransactionById
(
stub
,
number
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error get detail"
)
}
transactions
=
append
(
transactions
,
transaction
)
i
=
i
+
1
}
return
transactions
,
nil
}
return
nil
,
nil
}
func
writeHome
(
stub
*
shim
.
ChaincodeStub
,
home
Home
)
(
error
)
{
homeBytes
,
err
:=
json
.
Marshal
(
&
home
)
if
err
!=
nil
{
return
err
}
err
=
stub
.
PutState
(
home
.
Address
,
homeBytes
)
if
err
!=
nil
{
return
errors
.
New
(
"PutState Error"
+
err
.
Error
())
}
return
nil
}
func
writeTransaction
(
stub
*
shim
.
ChaincodeStub
,
transaction
Transaction
)(
error
){
txBytes
,
err
:=
json
.
Marshal
(
&
transaction
)
if
err
!-
nil
{
return
nil
}
id
,
_
:=
strconv
.
Itoa
(
transaction
.
Id
)
err
=
stub
.
PutState
(
"transaction"
+
id
,
txBytes
)
if
err
!=
nil
{
return
errors
.
New
(
"PutState Error"
+
err
.
Error
())
}
return
nil
}
\ No newline at end of file
hyperledger/chaincode_example05.md
浏览文件 @
e1cd41dd
...
...
@@ -10,7 +10,7 @@
在某一块区域内存在一个能源微电网,每一户家庭可能为生产者也可能为消费者。部分家庭拥有太阳能电池板,太阳能电池板的剩余电量为可以售出的电力的值,为了简化,单位为1.需要电力的家庭可以向有足够余额的电力的家庭购买电力。
账户私钥应该由安装在本地的客户端生成,本例中为了简便,使用模拟私钥和公钥。
账户私钥应该由安装在本地的客户端生成,本例中为了简便,使用模拟私钥和公钥。
每位用户的私钥为guid+“1”,公钥为guid+“2”。签名方式简化为私钥+"1"
#### 数据结构设计
在该智能合约中暂时只有一种角色,为每一户家庭用户。
...
...
@@ -22,15 +22,108 @@
-
编号
-
状态 //0:不可购买, 1:可以购买
-
账户公钥
-
账户私钥
-
交易(一笔交易必须同时具有卖方和买方的公钥签名,方能承认这笔交易。公钥签名生成规则,公钥+待创建交易的ID号,在本交易类型中,只要买家有足够的货币,卖家自动会对交易进行签名)
-
购买方地址
-
销售方地址
-
电量销售量
-
电量交易金额
-
编号
-
交易时间
#### function及各自实现的功能
-
`init`
初始化操作
-
`invoke`
调用合约内部的函数
-
`query`
查询相关的信息
-
`importNet`
某一位用户加入能源微网
-
`buyByAddress`
向某一位用户购买一定量的电力
-
`getAddressesBuyNumber`
查询具有一定额的电力的用户的地址
-
`changeStatus`
某一位用户修改自身的状态
-
`createUser`
创建新用户,并加入到能源微网中 init
-
`buyByAddress`
向某一位用户购买一定量的电力 invoke
-
`getTransactionById`
通过id获取交易内容 query
-
`getTransactions`
获取交易(如果交易数大于10,获取前10个) query
-
`getHomes`
获取用户(如果用户数大于10,获取前10个) query
-
`getHomeByAddress`
通过地址获取用户 query
-
`changeStatus`
某一位用户修改自身的状态 invoke
-
`writeUser`
将新用户写入到键值对中
-
`writeTransaction`
记录交易
#### 接口设计
`createUser`
request参数:
```
args[0] 剩余能量值
args[1] 剩余金额
```
response参数:
```
新建家庭用户的json表示
```
`buyByAddress`
request参数:
```
args[0] 卖家的账户地址
args[1] 买家签名
args[2] 买家的账户地址
args[3] 想要购买的电量数值
```
response参数:
```
购买成功的话返回该transaction的json串。
购买失败返回error
```
`getTransactionById`
request参数:
```
args[0] 交易编号
```
response参数:
```
查询结果的transaction 交易表示
```
`getTransactions`
request参数:
```
none
```
response参数:
```
获取所有的交易列表(如果交易大于10,则返回前10个)
```
`getHomeByAddress`
request参数
```
args[0] address
```
response参数
```
用户信息的json表示
```
`getHomes`
response参数
```
获取所有的用户列表(如果用户个数大于10,则返回前10个)
```
`changeStatus`
request参数:
```
args[0] 账户地址
args[1] 账户签名
args[2] 对自己的账户进行的操作,0:设置为不可购买 1:设置状态为可购买
```
response参数:
```
修改后的用户信息json表示
```
#### 测试
hyperledger/chaincode_example06.go
浏览文件 @
e1cd41dd
...
...
@@ -22,6 +22,7 @@ type SimpleChaincode struct {
}
var
ExpressOrderId
int
=
0
var
ExpressId
int
=
0
type
ExpressOrder
struct
{
Id
int
...
...
@@ -32,13 +33,14 @@ type ExpressOrder struct{
SenderPhone
string
ReceiverPhone
string
ExpressMoney
int
ExpressMoneyType
int
ExpressMoneyType
string
ExpressMoneySenderPay
int
ExpressPointAddress
[]
string
ExpressOrderStatus
//0:收货方未签名 1:收货方签名
PayingMoney
int
ExpressOrderSign
string
//0:收货方未签名 1:收货方签名
}
type
Participant
er
struct
{
type
Us
er
struct
{
Name
string
Location
string
Address
string
...
...
@@ -48,15 +50,18 @@ type Participanter struct{
Money
int
}
type
Express
Cp
struct
{
type
Express
struct
{
Name
string
Location
string
Phone
string
Money
int
ExpressPointAddress
[]
string
ExpressPointerAddress
[]
string
Address
string
PriKey
string
PubKey
string
}
type
Express
CpPoint
struct
{
type
Express
Pointer
struct
{
Name
string
Location
string
Phone
string
...
...
@@ -66,17 +71,71 @@ type ExpressCpPoint struct{
}
func
(
t
*
SimpleChaincode
)
Init
(
stub
*
shim
.
ChaincodeStub
,
function
string
,
args
[]
string
)
([]
byte
,
error
)
{
if
len
(
args
)
!=
0
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 0"
)
if
function
==
"createUser"
{
return
t
.
createUser
(
stub
,
args
)
}
else
if
function
==
"craeteExpressPointer"
{
return
t
.
createExpressPointer
(
stub
,
args
)
}
else
if
function
==
"createExpress"
{
return
t
.
createExpress
(
stub
,
args
)
}
else
if
function
==
"createExpressOrder"
{
return
t
.
createExpressOrder
(
stub
,
args
)
}
return
nil
,
nil
}
func
(
t
*
SimpleChaincode
)
Invoke
(
stub
*
shim
.
ChaincodeStub
,
function
string
,
args
[]
string
)
([]
byte
,
error
)
{
if
function
==
"finishExpressOrder"
{
return
t
.
finishExpressOrder
(
stub
,
args
)
}
else
if
function
==
"addExpressPointer"
{
return
t
.
addExpressPointer
(
stub
,
args
[
0
])
}
else
if
function
==
"updateExpressOrder"
{
return
t
.
updateExpressOrder
(
stub
,
args
[
0
])
}
return
nil
,
nil
}
func
(
t
*
SimpleChaincode
)
Query
(
stub
*
shim
.
ChaincodeStub
,
function
string
,
args
[]
string
)
([]
byte
,
error
)
{
if
function
==
"getExpressOrderById"
{
if
len
(
args
)
!=
1
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 3"
)
}
_
,
eoBytes
,
err
:=
getExpressOrderById
(
stub
,
args
[
0
])
if
err
!=
nil
{
fmt
.
Println
(
"Error get data"
)
return
nil
,
err
}
return
eoBytes
,
nil
}
else
if
function
==
"getExpress"
{
if
len
(
args
)
!=
0
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 0"
)
}
_
,
exBytes
,
err
:=
getExpress
(
stub
)
if
err
!=
nil
{
fmt
.
Println
(
"Error get data"
)
return
nil
,
err
}
return
exBytes
,
nil
}
else
if
function
==
"getUserByAddress"
{
if
len
(
args
)
!=
1
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 1"
)
}
_
,
userBytes
,
err
:=
getUserByAddress
(
stub
,
args
[
0
])
if
err
!=
nil
{
fmt
.
Println
(
"Error get data"
)
return
nil
,
err
}
return
userBytes
,
nil
}
else
if
function
==
"getExpressPointerByAddress"
{
if
len
(
args
)
!=
1
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 1"
)
}
_
,
expressCpBytes
,
err
:=
getExpressPointerByAddress
(
stub
,
args
[
0
])
if
err
!=
nil
{
fmt
.
Println
(
"Error get data"
)
return
nil
,
err
}
return
expressCpBytes
,
nil
}
return
nil
,
nil
}
...
...
@@ -106,8 +165,341 @@ func GetAddress() (string,string,string) {
return
address
,
priKey
,
pubKey
}
func
(
t
*
SimpleChaincode
)
createUser
(
stub
*
shim
.
ChaincodeStub
,
args
[]
string
)
([]
byte
,
error
)
{
if
len
(
args
)
!=
4
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 4"
)
}
var
user
User
var
userBytes
[]
byte
var
number
int
var
error
err
var
address
,
priKey
,
pubKey
string
address
,
priKey
,
pubKey
=
GetAddress
()
number
,
err
=
strconv
.
Atoi
(
args
[
3
])
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Want integer number"
)
}
user
=
User
{
Name
:
args
[
0
],
Loction
:
args
[
1
],
Address
:
address
,
PriKey
:
priKey
,
PubKey
:
pubKey
,
Phone
:
args
[
2
],
Money
:
number
}
err
=
writeUser
(
stub
,
user
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"write error"
)
}
userBytes
,
err
=
json
.
Marshal
(
&
user
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error retrieve"
)
}
return
userBytes
,
nil
}
func
(
t
*
SimpleChaincode
)
createExpressPointer
(
stub
*
shim
.
ChaincodeStub
,
args
[]
string
)([]
byte
,
error
){
if
len
(
args
)
!=
3
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments. Expecting 3"
)
}
var
expressPointer
ExpressPointer
var
epBytes
[]
byte
var
address
,
priKey
,
pubKey
string
address
,
priKey
,
pubKey
=
GetAddress
()
expressPointer
=
ExpressPointer
{
Name
:
args
[
0
],
Location
:
args
[
1
],
ExpressAddress
:
address
,
PriKey
:
priKey
,
PubKey
:
pubKey
,
Phone
:
phone
}
err
=
writeUser
(
stub
,
expressPointer
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"write error"
)
}
epBytes
,
err
=
json
.
Marshal
(
&
expressPointer
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error retrieve"
)
}
return
epBytes
,
nil
}
func
(
t
*
SimpleChaincode
)
createExpress
(
stub
*
shim
.
ChaincodeStub
,
args
[]
string
)([]
byte
,
error
){
if
len
(
args
)
!=
3
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments.Expecting 4"
)
}
if
expressId
!=
0
{
return
nil
,
errors
.
New
(
"can not create two express company"
)
}
var
express
Express
var
pointerAddress
[]
string
var
expressBytes
[]
byte
var
money
int
var
err
error
var
address
,
priKey
,
pubKey
string
money
,
err
=
strconv
.
Atoi
(
args
[
3
])
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Want integer number"
)
}
express
=
Express
{
Name
:
args
[
0
],
Location
:
args
[
1
],
Phone
:
args
[
2
],
Money
:
money
,
Address
:
address
,
PriKey
:
priKey
,
PubKey
:
pubKey
,
ExpressPointerAddress
:
pointerAddress
}
err
=
writeExpress
(
stub
,
express
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"write error"
)
}
expressBytes
,
err
=
json
.
Marshal
(
&
express
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error retrieve"
)
}
return
expressBytes
,
nil
}
func
(
t
*
SimpleChaincode
)
createExpressOrder
(
stub
*
shim
.
ChaincodeStub
,
args
[]
string
)([]
byte
,
error
){
if
len
(
args
)
!=
8
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments.Expecting 9"
)
}
var
expressOrder
ExpressOrder
var
expressPointAddress
[]
string
var
eoBytes
[]
byte
var
err
error
var
money
int
var
payingMoney
int
money
,
err
=
strconv
.
Atoi
(
args
[
7
])
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"want integer number"
)
}
payingMoney
,
err
=
strconv
.
Atoi
(
args
[
8
])
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"want integer number"
)
}
expressOrder
=
ExpressOrder
{
Id
:
ExpressOrderId
,
SenderLocation
:
args
[
0
],
ReceiverLocation
:
args
[
1
],
SenderAddress
:
args
[
2
],
ReceiverAddress
:
args
[
3
],
SenderPhone
:
args
[
4
],
ReceiverPhone
:
args
[
5
],
ExpressMoneyType
:
args
[
6
],
ExpressMoney
:
args
[
7
],
ExpressPointAddress
:
expressPointAddress
,
PayingMoney
:
payingMoney
}
err
=
writeExpressOrder
(
expressOrder
)
if
err
!=
nil
{
return
err
}
ExpressOrderId
=
ExpressOrderId
+
1
eoBytes
,
err
=
json
.
Marshal
(
&
expressOrder
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error retrieve data"
)
}
return
eoBytes
,
nil
}
func
(
t
*
SimpleChaincode
)
addExpressPointer
(
stub
*
shim
.
ChaincodeStub
,
address
string
)([]
byte
,
error
){
express
,
expressBytes
,
err
:=
getExpress
(
stub
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error get data"
)
}
expressOrder
,
eoBytes
,
error
:=
getExpressPointerByAddress
(
stub
,
address
)
if
error
!=
nil
{
return
nil
,
errors
.
New
(
"Error get data"
)
}
express
.
ExpressPointerAddress
=
append
(
express
.
ExpressPointerAddress
,
expressOrder
.
ExpressAddress
)
err
=
writeExpress
(
express
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Error write data"
)
}
expressBytes
,
err
:=
json
.
Marshal
(
&
express
)
if
err
!=
nil
{
return
nil
}
return
expressBytes
,
nil
}
func
(
t
*
SimpleChaincode
)
updateExpressOrder
(
stub
*
shim
.
ChaincodeStub
,
args
[]
string
)([]
byte
,
error
){
if
len
(
arg
)
!=
2
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments.Expecting 2"
)
}
expressOrder
,
epBytes
,
err
:=
getExpressOrderById
(
args
[
0
])
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"get error"
)
}
func
(
t
*
SimpleChaincode
)
createParticipanter
(
stub
*
shim
.
ChaincodeStub
,
args
[]
string
)
([]
byte
,
error
)
{
return
nil
,
nil
expressOrder
.
ExpressPointAddress
=
append
(
expressOrder
.
ExpressPointAddress
,
args
[
1
])
err
=
writeExpressOrder
(
expressOrder
)
if
err
!=
nil
{
return
nil
,
nil
}
eoBytes
,
err
:=
json
.
Marshal
(
&
expressOrder
)
if
err
!=
nil
{
return
nil
}
return
eoBytes
,
nil
}
func
(
t
*
SimpleChaincode
)
finishExpressOrder
(
stub
*
shim
.
ChaincodeStub
,
args
[]
string
)([]
byte
,
error
){
if
len
(
arg
)
!=
3
{
return
nil
,
errors
.
New
(
"Incorrect number of arguments.Expecting 3"
)
}
user
,
err
:=
getUserByAddress
(
stub
,
args
[
0
])
if
err
!=
nil
{
return
err
}
expressOrder
,
epBytes
,
error
:=
getExpressOrderById
(
args
[
1
])
if
error
!=
nil
{
return
error
}
express
,
epBytes
,
errorone
:=
getExpress
(
stub
)
errorone
!=
nil
{
return
errorone
}
if
expressOrder
.
ExpressMoney
!=
0
{
express
.
Money
=
express
.
Money
+
expressOrder
.
ExpressMoney
expressOrder
.
ExpressMoney
=
0
}
else
{
express
.
Money
=
express
.
Money
+
expressOrder
.
PayingMoney
user
.
Money
=
user
.
Money
-
expressOrder
.
PayingMoney
}
expressOrder
.
ExpressOrderSign
=
args
[
3
]
err
=
writeExpress
(
express
)
if
err
!=
nil
{
return
err
}
err
=
writeUser
(
user
)
if
err
!=
nil
{
return
err
}
err
=
writeExpressOrder
(
expressOrder
)
if
err
!=
nil
{
return
err
}
eoBytes
,
err
:=
json
.
Marshal
(
&
expressOrder
)
if
err
!=
nil
{
return
nil
}
return
eoBytes
,
nil
}
func
(
t
*
SimpleChaincode
)
getExpressOrderById
(
stub
*
shim
.
ChaincodeStub
,
id
string
)(
ExpressOrder
,[]
byte
,
error
){
var
expressOrder
ExpressOrder
eoBytes
,
err
:=
stub
.
GetState
(
"expressOrder"
+
id
)
if
err
!=
nil
{
fmt
.
Println
(
"Error retrieving data"
)
}
err
=
json
.
Unmarshal
(
eoBygtes
,
&
ExpessOrder
)
if
err
!=
nil
{
fmt
.
Println
(
"Error unmarshalling data"
)
}
return
expressOrder
,
eoByes
,
nil
}
func
(
t
*
SimpleChaincode
)
getExpress
(
stub
*
shim
.
ChaincodeStub
)(
Express
,[]
byte
,
error
){
var
express
Express
exBytes
,
err
:=
stub
.
GetState
(
"Express"
)
if
err
!=
nil
{
fmt
.
Println
(
"Error retrieving data"
)
}
err
=
json
.
Unmarshal
(
exBytes
,
&
express
)
if
err
!=
nil
{
fmt
.
Println
(
"Error unmarshalling data"
)
}
return
express
,
exBytes
,
nil
}
func
(
t
*
SimpleChaincode
)
getUserByAddress
(
stub
*
shim
.
ChaincodeStub
,
address
string
)(
User
,[]
byte
,
error
){
var
user
User
userBytes
,
err
:=
stub
.
GetState
(
address
)
if
err
!=
nil
{
fmt
.
Println
(
"Error retrieving data"
)
}
err
=
json
.
Unmarshal
(
userBytes
,
&
user
)
if
err
!=
nil
{
fmt
.
Println
(
"Error unmarshalling data"
)
}
return
user
,
userBytes
,
nil
}
func
(
t
*
SimpleChaincode
)
getExpressPointerByAddress
(
stub
*
shim
.
ChaincodeStub
,
address
string
)(
ExpressPointer
,[]
byte
,
error
){
var
expressPointer
ExpressPointer
epBytes
,
err
:=
stub
.
GetState
(
address
)
if
err
!=
nil
{
fmt
.
Println
(
"Error retrieving data"
)
}
err
=
json
.
Unmarshal
(
epBytes
,
&
expressPointer
)
if
err
!=
nil
{
fmt
.
Println
(
"Error unmarshalling data"
)
}
return
expressPointer
,
epBytes
,
nil
}
func
(
t
*
SimpleChaincode
)
writeExpress
(
stub
*
shim
.
ChaincodeStub
,
Express
express
)(
error
){
exBytes
,
err
:=
json
.
Marshal
(
&
express
)
if
err
!=
nil
{
return
err
}
err
=
stub
.
PutState
(
"Express"
,
exBytes
)
if
err
!=
nil
{
return
errors
.
New
(
"PutState Error"
+
err
.
Error
())
}
return
nil
}
func
(
t
*
SimpleChaincode
)
writeExpressOrder
(
stub
*
shim
.
ChaincodeStub
,
ExpressOrder
expressOrder
)(
error
){
eoBytes
,
err
:=
json
.
Marshal
(
&
expressOrder
)
if
err
!=
nil
{
return
err
}
id
,
_
:=
strconv
.
Itoa
(
expressOrder
.
Id
)
err
=
stub
.
PutState
(
"ExpressOrder"
+
id
,
eoBytes
)
if
err
!=
nil
{
return
errors
.
New
(
"PutState Error"
+
err
.
Error
())
}
return
nil
}
func
(
t
*
SimpleChaincode
)
writeUser
(
stub
*
shim
.
chaincodeStub
,
User
user
)(
error
){
userBytes
,
err
:=
json
.
Marshal
(
&
user
)
if
err
!=
nil
{
return
err
}
err
=
stub
.
PutState
(
user
.
Address
,
userBytes
)
if
err
!=
nil
{
return
errors
.
New
(
"PutState Error"
+
err
.
Error
())
}
return
nil
}
func
(
t
*
SimpleChaincdoe
)
writeExpressPointer
(
stub
*
shim
.
chaincodeStub
,
ExpressPointer
expressPointer
)(
error
){
epBytes
,
err
:=
json
.
Marshal
(
&
expressPointer
)
if
err
!=
nil
{
return
err
}
err
=
stub
.
PutState
(
user
.
Address
,
userBytes
)
if
err
!=
nil
{
return
errors
.
New
(
"PutState Error"
+
err
.
Error
())
}
return
nil
}
\ No newline at end of file
hyperledger/chaincode_example06.md
浏览文件 @
e1cd41dd
...
...
@@ -26,7 +26,7 @@
-
更新寄货单。物流公司旗下快递点根据配送信息更新电子寄货单。
-
收货方签收确认。收货方收到货物后,使用自己的私钥进行签收,完成相应的付款。
账户私钥应该由安装在本地的客户端生成,本例中为了简便,使用模拟私钥和公钥。
账户私钥应该由安装在本地的客户端生成,本例中为了简便,使用模拟私钥和公钥。
每位用户的私钥为guid+“1”,公钥为guid+“2”。用户签名为私钥+“1”
#### 数据结构设计
-
寄货单
...
...
@@ -39,7 +39,7 @@
-
物流费用支付类型 //0:寄货方支付 1:收货方支付
-
寄货方预支付费用 //模拟实际预支付,寄货方支付物流费下值为物流费,否则为0
-
快递配送信息 // 快递运送状态,所经过快递分拨中心与快递点的数组
-
寄货单状态 // 0:收货方签名 1:收货方未签名
-
收货方签名
-
寄货方
-
姓名
...
...
@@ -53,32 +53,192 @@
-
所在地址
-
账户地址
-
账户公钥
-
账户私钥
-
联系方式
-
账户余额
-
物流公司
-
账户公钥
-
账户私钥
-
名称
-
地址
-
联系方式
-
账户余额
-
物流公司旗下分拨中心与快递点
-
快递点
-
名称
-
所在地址
-
联系方式
-
快递点编号
-
快递点公钥
-
快递点私钥
-
快递点账户地址
#### function及各自实现的功能
-
`init`
初始化物流公司及其下相应快递点
-
`invoke`
调用合约内部的函数
-
`query`
查询相关的信息
-
`createUser`
创建用户
-
`createExpressOrder`
创建寄货单
-
`updateExpressOrder`
更新寄货单
-
`finishExpressOrder`
收货方签收寄货单
-
`addExpressPoint`
物流公司添加新的快递点
-
`getExpressOrderById`
查询订单状态
-
`updateExpress`
更新物流公司信息
-
`createUser`
创建用户 init
-
`createExpress`
创建物流公司 init
-
`createExpressPoint`
创建快递点 init
-
`createExpressOrder`
寄货方创建寄货单 init
-
`finishExpressOrder`
收货方签收寄货单 invoke
-
`addExpressPointer`
物流公司添加新的快递点 invoke
-
`updateExpressOrder`
更新物流公司订单,添加快递点的信息 invoke
-
`getExpressOrderById`
查询订单状态 query
-
`getExpress`
获取物流公司信息 query
-
`getUserByAddress`
获取用户信息 query
-
`getExpressPointByAddress`
获取快递点信息 query
-
`writeExpress`
存储物流公司信息 (以物流公司账户地址进行存储)
-
`writeExpressOrder`
存储寄货单 (以“express”+id 进行存储)
-
`writeUser`
存储用户信息 (以地址进行存储)
-
`writeExpressPoint`
存储物流点信息 (以快递点账户地址进行存储)
#### 接口设计
`createUser`
request参数
```
args[0] 姓名
args[1] 所在地址
args[2] 联系方式
args[3] 账户余额
```
response参数
```
user信息的json表示
```
`createExpressPointer`
request参数
```
args[0] 姓名
args[1] 所在地址
args[2] 联系方式
```
response参数
```
物流点的信息的json表示
```
`createExpress`
request 参数
```
args[0] 名称
args[1] 地址
args[2] 联系方式
args[3] 账户余额
```
response 参数
```
物流公司信息的json表示
```
`addExpressPointer`
request参数
```
args[0] 添加快递点
```
response参数
```
物流公司信息的json表示
```
`createExpressOrder`
request参数
```
args[0] 寄货方地址
args[1] 收货方地址
args[2] 寄货方账户地址
args[3] 收货方账户地址
args[4] 寄货方联系方式
args[5] 收货方联系方式
args[6] 物流费用支付类型
args[7] 寄货方预支付费用 (收货方支付的话值为0)
args[8] 物流费用
```
response 参数
```
订单信息的json表示
```
`updateExpressOrder`
request参数
```
args[0] 订单id
args[1] 快递点地址
```
response参数
```
订单信息的json表示
```
`finishExpressOrder`
request参数
```
args[0] 收货方账户地址
args[1] 账户订单编号
args[2] 收货方签名
```
response参数
```
订单信息的json表示
```
`getExpressOrderById`
request参数:
```
args[0] id
```
response参数:
```
快递订单的json表示
```
`getExpress`
response参数:
```
快递信息的json表示
```
`getUserByAddress`
request参数
```
args[0] address
```
response参数
```
用户信息的json表示
```
`getExpressPointerByAddress`
request参数
```
args[0] address
```
response参数
```
快递点的json信息表示
```
#### 测试
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录