forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mockendorserserver.go
113 lines (97 loc) · 2.98 KB
/
mockendorserserver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package mocks
import (
"fmt"
"net"
"sync"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-sdk-go/pkg/util/test"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/rwsetutil"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/ledger/rwset/kvrwset"
pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
// MockEndorserServer mock endoreser server to process endorsement proposals
type MockEndorserServer struct {
ProposalError error
AddkvWrite bool
srv *grpc.Server
wg sync.WaitGroup
}
// ProcessProposal mock implementation that returns success if error is not set
// error if it is
func (m *MockEndorserServer) ProcessProposal(context context.Context,
proposal *pb.SignedProposal) (*pb.ProposalResponse, error) {
if m.ProposalError == nil {
return &pb.ProposalResponse{Response: &pb.Response{
Status: 200,
}, Endorsement: &pb.Endorsement{Endorser: []byte("endorser"), Signature: []byte("signature")},
Payload: m.createProposalResponsePayload()}, nil
}
return &pb.ProposalResponse{Response: &pb.Response{
Status: 500,
Message: m.ProposalError.Error(),
}}, m.ProposalError
}
func (m *MockEndorserServer) createProposalResponsePayload() []byte {
prp := &pb.ProposalResponsePayload{}
ccAction := &pb.ChaincodeAction{}
txRwSet := &rwsetutil.TxRwSet{}
if m.AddkvWrite {
txRwSet.NsRwSets = []*rwsetutil.NsRwSet{
{NameSpace: "ns1", KvRwSet: &kvrwset.KVRWSet{
Reads: []*kvrwset.KVRead{{Key: "key1", Version: &kvrwset.Version{BlockNum: 1, TxNum: 1}}},
Writes: []*kvrwset.KVWrite{{Key: "key2", IsDelete: false, Value: []byte("value2")}},
}}}
}
txRWSetBytes, err := txRwSet.ToProtoBytes()
if err != nil {
return nil
}
ccAction.Results = txRWSetBytes
ccActionBytes, err := proto.Marshal(ccAction)
if err != nil {
return nil
}
prp.Extension = ccActionBytes
prpBytes, err := proto.Marshal(prp)
if err != nil {
return nil
}
return prpBytes
}
// Start the mock broadcast server
func (m *MockEndorserServer) Start(address string) string {
if m.srv != nil {
panic("MockBroadcastServer already started")
}
m.srv = grpc.NewServer()
lis, err := net.Listen("tcp", address)
if err != nil {
panic(fmt.Sprintf("Error starting BroadcastServer %s", err))
}
addr := lis.Addr().String()
test.Logf("Starting MockEventServer [%s]", addr)
pb.RegisterEndorserServer(m.srv, m)
m.wg.Add(1)
go func() {
defer m.wg.Done()
if err := m.srv.Serve(lis); err != nil {
test.Logf("StartMockBroadcastServer failed [%s]", err)
}
}()
return addr
}
// Stop the mock broadcast server and wait for completion.
func (m *MockEndorserServer) Stop() {
if m.srv == nil {
panic("MockBroadcastServer not started")
}
m.srv.Stop()
m.wg.Wait()
m.srv = nil
}