forked from hyperledger/fabric-sdk-go
/
mockpeer.go
102 lines (84 loc) · 2.64 KB
/
mockpeer.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package mocks
// TODO: Move protos to this library
import (
reqContext "context"
"encoding/pem"
"sync"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
)
// MockPeer is a mock fabricsdk.Peer.
type MockPeer struct {
RWLock *sync.RWMutex
Error error
MockName string
MockURL string
MockRoles []string
MockCert *pem.Block
Payload []byte
ResponseMessage string
MockMSP string
Status int32
ProcessProposalCalls int
Endorser []byte
}
// NewMockPeer creates basic mock peer
func NewMockPeer(name string, url string) *MockPeer {
mp := &MockPeer{MockName: name, MockMSP: "Org1MSP", MockURL: url, Status: 200, RWLock: &sync.RWMutex{}}
return mp
}
// Name returns the mock peer's mock name
func (p *MockPeer) Name() string {
return p.MockName
}
// SetName sets the mock peer's mock name
func (p *MockPeer) SetName(name string) {
p.MockName = name
}
// MSPID gets the Peer mspID.
func (p *MockPeer) MSPID() string {
return p.MockMSP
}
// SetMSPID sets the Peer mspID.
func (p *MockPeer) SetMSPID(mspID string) {
p.MockMSP = mspID
}
// Roles returns the mock peer's mock roles
func (p *MockPeer) Roles() []string {
return p.MockRoles
}
// SetRoles sets the mock peer's mock roles
func (p *MockPeer) SetRoles(roles []string) {
p.MockRoles = roles
}
// EnrollmentCertificate returns the mock peer's mock enrollment certificate
func (p *MockPeer) EnrollmentCertificate() *pem.Block {
return p.MockCert
}
// SetEnrollmentCertificate sets the mock peer's mock enrollment certificate
func (p *MockPeer) SetEnrollmentCertificate(pem *pem.Block) {
p.MockCert = pem
}
// URL returns the mock peer's mock URL
func (p *MockPeer) URL() string {
return p.MockURL
}
// ProcessTransactionProposal does not send anything anywhere but returns an empty mock ProposalResponse
func (p *MockPeer) ProcessTransactionProposal(ctx reqContext.Context, tp fab.ProcessProposalRequest) (*fab.TransactionProposalResponse, error) {
if p.RWLock != nil {
p.RWLock.Lock()
defer p.RWLock.Unlock()
}
p.ProcessProposalCalls++
return &fab.TransactionProposalResponse{
Endorser: p.MockURL,
Status: p.Status,
ProposalResponse: &pb.ProposalResponse{Response: &pb.Response{
Message: p.ResponseMessage, Status: p.Status, Payload: p.Payload},
Endorsement: &pb.Endorsement{Endorser: p.Endorser, Signature: []byte("signature")}},
}, p.Error
}