This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtx.go
133 lines (112 loc) · 2.93 KB
/
tx.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package testing
import (
"sync"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-protos-go/peer"
"go.uber.org/zap"
"github.com/s7techlab/cckit/response"
"github.com/s7techlab/cckit/router"
"github.com/s7techlab/cckit/testing/expect"
)
type (
TxHandler struct {
MockStub *MockStub
m sync.Mutex
Logger *zap.Logger
Context router.Context
}
TxResult struct {
Result interface{}
Err error
Event *peer.ChaincodeEvent
}
)
func NewTxHandler(name string) (*TxHandler, router.Context) {
var (
mockStub = NewMockStub(name, nil)
logger = router.NewLogger(name)
ctx = router.NewContext(mockStub, logger)
)
return &TxHandler{
MockStub: mockStub,
Logger: logger,
Context: ctx,
}, ctx
}
func (p *TxHandler) From(creator ...interface{}) *TxHandler {
p.MockStub.From(creator...)
return p
}
func (p *TxHandler) Init(txHdl func(ctx router.Context) (interface{}, error)) *TxResult {
return p.Invoke(txHdl)
}
// Invoke emulates chaincode invocation and returns transaction result
func (p *TxHandler) Invoke(invoke func(ctx router.Context) (interface{}, error)) *TxResult {
uuid := p.MockStub.generateTxUID()
p.MockStub.MockTransactionStart(uuid)
res, err := invoke(p.Context)
p.MockStub.TxResult = response.Create(res, err)
p.MockStub.MockTransactionEnd(uuid)
txRes := &TxResult{
Result: res,
Err: err,
Event: p.MockStub.ChaincodeEvent,
}
return txRes
}
// Tx emulates chaincode invocation
func (p *TxHandler) Tx(tx func()) {
p.m.Lock()
defer p.m.Unlock()
uuid := p.MockStub.generateTxUID()
p.MockStub.MockTransactionStart(uuid)
// expect that invoke will be with shim.OK status, need for dump state changes
// if during tx func got error - func setTxResult must be called
p.MockStub.TxResult = peer.Response{
Status: shim.OK,
Message: "",
Payload: nil,
}
tx()
p.MockStub.MockTransactionEnd(uuid)
}
// SetTxResult can be used for set txResult error during Tx
func (p *TxHandler) SetTxResult(err error) {
if p.MockStub.TxID == `` {
panic(`can be called only during Tx() evaluation`)
}
if err != nil {
p.MockStub.TxResult.Status = shim.ERROR
p.MockStub.TxResult.Message = err.Error()
}
}
// TxFunc returns tx closure, can be used directly as ginkgo func
func (p *TxHandler) TxFunc(tx func()) func() {
return func() {
p.Tx(tx)
}
}
// Expect returns assertion helper
func (p *TxHandler) Expect(res interface{}, err error) *expect.TxRes {
return &expect.TxRes{
Result: res,
Err: err,
Event: p.MockStub.ChaincodeEvent,
}
}
// TxTimestamp returns last tx timestamp
func (p *TxHandler) TxTimestamp() *timestamp.Timestamp {
return p.MockStub.TxTimestamp
}
// TxEvent returns last tx event
func (p *TxHandler) TxEvent() *peer.ChaincodeEvent {
return p.MockStub.ChaincodeEvent
}
func (r *TxResult) Expect() *expect.TxRes {
return &expect.TxRes{
Result: r.Result,
Err: r.Err,
Event: r.Event,
}
}