forked from hyperledger/fabric-sdk-go
/
mockpeer.go
71 lines (59 loc) · 1.56 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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package mocks
import (
"sync"
fabmocks "github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks"
)
// MockPeer contains mock PeerState
type MockPeer struct {
*fabmocks.MockPeer
blockHeight uint64
lock sync.RWMutex
}
// MockPeerOpt is a mock peer option
type MockPeerOpt func(*MockPeer)
// WithMSP sets the MSP ID of the mock peer
func WithMSP(mspID string) MockPeerOpt {
return func(p *MockPeer) {
p.SetMSPID(mspID)
}
}
// WithBlockHeight sets the block height of the mock peer
func WithBlockHeight(blockHeight uint64) MockPeerOpt {
return func(p *MockPeer) {
p.blockHeight = blockHeight
}
}
// NewMockStatefulPeer returns a new MockPeer with the given options
func NewMockStatefulPeer(name, url string, opts ...MockPeerOpt) *MockPeer {
p := &MockPeer{
MockPeer: fabmocks.NewMockPeer(name, url),
}
for _, opt := range opts {
opt(p)
}
return p
}
// NewMockPeer returns a new MockPeer
// Deprecated: This function will be deprecated in the future. Use NewMockStatefulPeer instead.
func NewMockPeer(name, url string, blockHeight uint64) *MockPeer {
return &MockPeer{
MockPeer: fabmocks.NewMockPeer(name, url),
blockHeight: blockHeight,
}
}
// BlockHeight returns the block height
func (p *MockPeer) BlockHeight() uint64 {
p.lock.RLock()
defer p.lock.RUnlock()
return p.blockHeight
}
// SetBlockHeight sets the block height
func (p *MockPeer) SetBlockHeight(blockHeight uint64) {
p.lock.Lock()
defer p.lock.Unlock()
p.blockHeight = blockHeight
}