Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
data_swapper
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
link33
data_swapper
Commits
a694866d
Commit
a694866d
authored
Dec 17, 2021
by
harrylee
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add data_swapper
parents
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
135 additions
and
0 deletions
+135
-0
data_swapper.go
data_swapper.go
+112
-0
go.mod
go.mod
+23
-0
go.sum
go.sum
+0
-0
No files found.
data_swapper.go
0 → 100644
View file @
a694866d
package
main
import
(
"fmt"
"strings"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb
"github.com/hyperledger/fabric/protos/peer"
)
const
(
channelID
=
"mychannel"
brokerContractName
=
"broker"
emitInterchainEventFunc
=
"EmitInterchainEvent"
)
type
DataSwapper
struct
{}
func
(
s
*
DataSwapper
)
Init
(
stub
shim
.
ChaincodeStubInterface
)
pb
.
Response
{
return
shim
.
Success
(
nil
)
}
func
(
s
*
DataSwapper
)
Invoke
(
stub
shim
.
ChaincodeStubInterface
)
pb
.
Response
{
function
,
args
:=
stub
.
GetFunctionAndParameters
()
fmt
.
Printf
(
"invoke: %s
\n
"
,
function
)
switch
function
{
case
"register"
:
return
s
.
register
(
stub
)
case
"interchainGet"
:
return
s
.
interchainGet
(
stub
,
args
)
case
"interchainSet"
:
return
s
.
interchainSet
(
stub
,
args
)
case
"get"
:
return
s
.
get
(
stub
,
args
)
case
"set"
:
return
s
.
set
(
stub
,
args
)
default
:
return
shim
.
Error
(
"invalid function: "
+
function
+
", args: "
+
strings
.
Join
(
args
,
","
))
}
}
func
(
s
*
DataSwapper
)
register
(
stub
shim
.
ChaincodeStubInterface
)
pb
.
Response
{
args
:=
util
.
ToChaincodeArgs
(
"register"
)
response
:=
stub
.
InvokeChaincode
(
brokerContractName
,
args
,
channelID
)
if
response
.
Status
!=
shim
.
OK
{
return
shim
.
Error
(
fmt
.
Sprintf
(
"invoke chaincode '%s' err: %s"
,
brokerContractName
,
response
.
Message
))
}
return
response
}
// get is business function which will invoke the to,tid,id
func
(
s
*
DataSwapper
)
get
(
stub
shim
.
ChaincodeStubInterface
,
args
[]
string
)
pb
.
Response
{
switch
len
(
args
)
{
case
1
:
// args[0]: key
value
,
err
:=
stub
.
GetState
(
args
[
0
])
if
err
!=
nil
{
return
shim
.
Error
(
err
.
Error
())
}
return
shim
.
Success
(
value
)
case
2
:
// args[0]: destination service id
// args[1]: key
b
:=
util
.
ToChaincodeArgs
(
emitInterchainEventFunc
,
args
[
0
],
"interchainGet,interchainSet,"
,
args
[
1
],
args
[
1
],
""
)
response
:=
stub
.
InvokeChaincode
(
brokerContractName
,
b
,
channelID
)
if
response
.
Status
!=
shim
.
OK
{
return
shim
.
Error
(
fmt
.
Errorf
(
"invoke broker chaincode %s error: %s"
,
brokerContractName
,
response
.
Message
)
.
Error
())
}
return
shim
.
Success
(
nil
)
default
:
return
shim
.
Error
(
"incorrect number of arguments"
)
}
}
// get is business function which will invoke the to,tid,id
func
(
s
*
DataSwapper
)
set
(
stub
shim
.
ChaincodeStubInterface
,
args
[]
string
)
pb
.
Response
{
if
len
(
args
)
!=
2
{
return
shim
.
Error
(
"incorrect number of arguments"
)
}
err
:=
stub
.
PutState
(
args
[
0
],
[]
byte
(
args
[
1
]))
if
err
!=
nil
{
return
shim
.
Error
(
err
.
Error
())
}
return
shim
.
Success
(
nil
)
}
// interchainSet is the callback function getting data by interchain
func
(
s
*
DataSwapper
)
interchainSet
(
stub
shim
.
ChaincodeStubInterface
,
args
[]
string
)
pb
.
Response
{
return
s
.
set
(
stub
,
args
)
}
// interchainGet gets data by interchain
func
(
s
*
DataSwapper
)
interchainGet
(
stub
shim
.
ChaincodeStubInterface
,
args
[]
string
)
pb
.
Response
{
value
,
err
:=
stub
.
GetState
(
args
[
0
])
if
err
!=
nil
{
return
shim
.
Error
(
err
.
Error
())
}
return
shim
.
Success
(
value
)
}
func
main
()
{
err
:=
shim
.
Start
(
new
(
DataSwapper
))
if
err
!=
nil
{
fmt
.
Printf
(
"Error starting chaincode: %s"
,
err
)
}
}
go.mod
0 → 100644
View file @
a694866d
module gitlab.33.cn/link33/data_swapper
go 1.13
require (
github.com/Knetic/govaluate v3.0.0+incompatible // indirect
github.com/Shopify/sarama v1.29.1 // indirect
github.com/fsouza/go-dockerclient v1.7.3 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/hashicorp/go-version v1.3.0 // indirect
github.com/hyperledger/fabric v1.4.3
github.com/hyperledger/fabric-amcl v0.0.0-20210603140002-2670f91851c8 // indirect
github.com/miekg/pkcs11 v1.0.3 // indirect
github.com/onsi/gomega v1.14.0 // indirect
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 // indirect
github.com/spf13/viper v1.8.1 // indirect
github.com/sykesm/zap-logfmt v0.0.4 // indirect
go.uber.org/zap v1.18.1 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
google.golang.org/grpc v1.39.0 // indirect
)
replace github.com/golang/protobuf => github.com/golang/protobuf v1.3.2
go.sum
0 → 100644
View file @
a694866d
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment