-
Notifications
You must be signed in to change notification settings - Fork 29
/
callback.go
95 lines (76 loc) · 2.38 KB
/
callback.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
package transaction
import (
"context"
"github.com/0chain/gosdk/zcncore"
"github.com/0chain/gosdk/zmagmacore/errors"
)
type (
// callback implements zcncore.TransactionCallback interface.
callback struct {
// waitCh represents channel for making callback.OnTransactionComplete,
// callback.OnVerifyComplete and callBack.OnAuthComplete operations async.
waitCh chan interface{}
err error
}
)
var (
// Ensure callback implements interface.
_ zcncore.TransactionCallback = (*callback)(nil)
)
func newCallBack() *callback {
return &callback{
waitCh: make(chan interface{}),
}
}
// OnTransactionComplete implements zcncore.TransactionCallback interface.
func (cb *callback) OnTransactionComplete(zcnTxn *zcncore.Transaction, status int) {
if status != zcncore.StatusSuccess {
msg := "status is not success: " + TxnStatus(status).String() + "; err: " + zcnTxn.GetTransactionError()
cb.err = errors.New("on_transaction_complete", msg)
}
cb.sendCall()
}
func (cb *callback) waitCompleteCall(ctx context.Context) error {
select {
case <-ctx.Done():
return errors.New("completing_transaction", "completing transaction context deadline exceeded")
case <-cb.waitCh:
return cb.err
}
}
// OnVerifyComplete implements zcncore.TransactionCallback interface.
func (cb *callback) OnVerifyComplete(zcnTxn *zcncore.Transaction, status int) {
if status != zcncore.StatusSuccess {
msg := "status is not success: " + TxnStatus(status).String() + "; err: " + zcnTxn.GetVerifyError()
cb.err = errors.New("on_transaction_verify", msg)
}
cb.sendCall()
}
func (cb *callback) waitVerifyCall(ctx context.Context) error {
select {
case <-ctx.Done():
return errors.New("verifying_transaction", "verifying transaction context deadline exceeded")
case <-cb.waitCh:
return cb.err
}
}
// OnAuthComplete implements zcncore.TransactionCallback interface.
func (cb *callback) OnAuthComplete(zcnTxn *zcncore.Transaction, status int) {
if status != zcncore.StatusSuccess {
msg := "status is not success: " + TxnStatus(status).String()
cb.err = errors.New("on_transaction_verify", msg)
}
cb.sendCall()
}
//nolint:unused
func (cb *callback) waitAuthCall(ctx context.Context) error {
select {
case <-ctx.Done():
return errors.New("auth_transaction", "authenticating transaction context deadline exceeded")
case <-cb.waitCh:
return cb.err
}
}
func (cb *callback) sendCall() {
cb.waitCh <- true
}