Commit d46be7b8 authored by harrylee's avatar harrylee

add broker

parents
This diff is collapsed.
module gitlab.33cn/link33/broker
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/golang/protobuf v1.5.2
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
This diff is collapsed.
package main
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
)
type response struct {
OK bool `json:"ok"`
Message string `json:"message"`
Data []byte `json:"data"`
}
func successResponse(data []byte) pb.Response {
res := &response{
OK: true,
Data: data,
}
data, err := json.Marshal(res)
if err != nil {
panic(err)
}
return shim.Success(data)
}
func errorResponse(msg string) pb.Response {
res := &response{
OK: false,
Message: msg,
}
data, err := json.Marshal(res)
if err != nil {
panic(err)
}
return shim.Error(string(data))
}
// putMap for persisting meta state into ledger
func (broker *Broker) putMap(stub shim.ChaincodeStubInterface, metaName string, meta map[string]uint64) error {
if meta == nil {
return nil
}
metaBytes, err := json.Marshal(meta)
if err != nil {
return err
}
return stub.PutState(metaName, metaBytes)
}
func (broker *Broker) getMap(stub shim.ChaincodeStubInterface, metaName string) (map[string]uint64, error) {
metaBytes, err := stub.GetState(metaName)
if err != nil {
return nil, err
}
meta := make(map[string]uint64)
if metaBytes == nil {
return meta, nil
}
if err := json.Unmarshal(metaBytes, &meta); err != nil {
return nil, err
}
return meta, nil
}
func getChaincodeID(stub shim.ChaincodeStubInterface) (string, error) {
sp, err := stub.GetSignedProposal()
if err != nil {
return "", err
}
proposal := &pb.Proposal{}
if err := proto.Unmarshal(sp.ProposalBytes, proposal); err != nil {
return "", err
}
payload := &pb.ChaincodeProposalPayload{}
if err := proto.Unmarshal(proposal.Payload, payload); err != nil {
return "", err
}
spec := &pb.ChaincodeInvocationSpec{}
if err := proto.Unmarshal(payload.Input, spec); err != nil {
return "", err
}
return getKey(stub.GetChannelID(), spec.ChaincodeSpec.ChaincodeId.Name), nil
}
func getKey(channel, chaincodeName string) string {
return channel + delimiter + chaincodeName
}
func (broker *Broker) checkIndex(stub shim.ChaincodeStubInterface, addr string, index string, metaName string) error {
idx, err := strconv.ParseUint(index, 10, 64)
if err != nil {
return err
}
meta, err := broker.getMap(stub, metaName)
if err != nil {
return err
}
if idx != meta[addr]+1 {
return fmt.Errorf("incorrect index, expect %d", meta[addr]+1)
}
return nil
}
func (broker *Broker) outMsgKey(to string, idx string) string {
return fmt.Sprintf("out-msg-%s-%s", to, idx)
}
func (broker *Broker) inMsgKey(from string, idx string) string {
return fmt.Sprintf("in-msg-%s-%s", from, idx)
}
func (broker *Broker) onlyAdmin(stub shim.ChaincodeStubInterface) bool {
key, err := getChaincodeID(stub)
if err != nil {
fmt.Printf("Get cert public key %s\n", err.Error())
return false
}
adminList, err := broker.getMap(stub, adminList)
if err != nil {
fmt.Println("Get admin list info failed")
return false
}
if adminList[key] == 1 {
return false
}
return true
}
func (broker *Broker) onlyWhitelist(stub shim.ChaincodeStubInterface) bool {
key, err := getChaincodeID(stub)
if err != nil {
fmt.Printf("Get cert public key %s\n", err.Error())
return false
}
whiteList, err := broker.getMap(stub, whiteList)
if err != nil {
fmt.Println("Get white list info failed")
return false
}
if whiteList[key] != 1 {
return false
}
return true
}
func (broker *Broker) getList(stub shim.ChaincodeStubInterface) pb.Response {
whiteList, err := broker.getMap(stub, whiteList)
if err != nil {
return shim.Error(fmt.Sprintf("Get white list :%s", err.Error()))
}
var list [][]byte
for k, v := range whiteList {
if v == 0 {
list = append(list, []byte(k))
}
}
return shim.Success(bytes.Join(list, []byte(",")))
}
func (broker *Broker) checkAdmin(stub shim.ChaincodeStubInterface, function string) bool {
checks := map[string]struct{}{
"audit": {},
"invokeInterchain": {},
"invokeIndexUpdate": {},
}
if _, ok := checks[function]; !ok {
return true
}
return broker.onlyAdmin(stub)
}
func (broker *Broker) checkWhitelist(stub shim.ChaincodeStubInterface, function string) bool {
checks := map[string]struct{}{
"EmitInterchainEvent": {},
}
if _, ok := checks[function]; !ok {
return true
}
return broker.onlyWhitelist(stub)
}
package main
import (
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
)
// getOutMeta
func (broker *Broker) getOuterMeta(stub shim.ChaincodeStubInterface) pb.Response {
v, err := stub.GetState(outterMeta)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(v)
}
// getOutMessage to,index
func (broker *Broker) getOutMessage(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) < 2 {
return shim.Error("incorrect number of arguments, expecting 2")
}
destChainMethod := args[0]
sequenceNum := args[1]
key := broker.outMsgKey(destChainMethod, sequenceNum)
v, err := stub.GetState(key)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(v)
}
func (broker *Broker) getInnerMeta(stub shim.ChaincodeStubInterface) pb.Response {
v, err := stub.GetState(innerMeta)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(v)
}
// getInMessage from,index
func (broker *Broker) getInMessage(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) < 2 {
return shim.Error("incorrect number of arguments, expecting 2")
}
inServicePair := args[0]
sequenceNum := args[1]
key := broker.inMsgKey(inServicePair, sequenceNum)
v, err := stub.GetState(key)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(v)
}
func (broker *Broker) getCallbackMeta(stub shim.ChaincodeStubInterface) pb.Response {
v, err := stub.GetState(callbackMeta)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(v)
}
func (broker *Broker) getDstRollbackMeta(stub shim.ChaincodeStubInterface) pb.Response {
v, err := stub.GetState(dstRollbackMeta)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(v)
}
func (broker *Broker) markInCounter(stub shim.ChaincodeStubInterface, from string) error {
inMeta, err := broker.getMap(stub, innerMeta)
if err != nil {
return err
}
inMeta[from]++
return broker.putMap(stub, innerMeta, inMeta)
}
func (broker *Broker) markCallbackCounter(stub shim.ChaincodeStubInterface, from string, index uint64) error {
meta, err := broker.getMap(stub, callbackMeta)
if err != nil {
return err
}
meta[from] = index
return broker.putMap(stub, callbackMeta, meta)
}
func (broker *Broker) markDstRollbackCounter(stub shim.ChaincodeStubInterface, from string, index uint64) error {
meta, err := broker.getMap(stub, dstRollbackMeta)
if err != nil {
return err
}
meta[from] = index
return broker.putMap(stub, dstRollbackMeta, meta)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment