@@ -9,17 +9,22 @@ package views
99import (
1010 "context"
1111 "encoding/json"
12+ "fmt"
13+ "sync"
14+ "time"
1215
1316 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/assert"
1417 "github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
1518 "github.com/hyperledger-labs/fabric-token-sdk/token"
1619 "github.com/hyperledger-labs/fabric-token-sdk/token/services/network"
1720 "github.com/hyperledger-labs/fabric-token-sdk/token/services/ttx"
21+ "github.com/pkg/errors"
1822)
1923
2024type TxFinality struct {
21- TxID string
22- TMSID * token.TMSID
25+ TxID string
26+ TMSID * token.TMSID
27+ Timeout time.Duration
2328}
2429
2530type TxFinalityView struct {
@@ -39,11 +44,11 @@ func (r *TxFinalityView) Call(context view.Context) (interface{}, error) {
3944 assert .NotNil (tms )
4045 nw := network .GetInstance (context , tms .Network (), tms .Channel ())
4146 assert .NotNil (nw )
42- assert .NoError (nw .AddFinalityListener (tms .Namespace (), r .TxID , & finalityListener { errs : errs } ))
47+ assert .NoError (nw .AddFinalityListener (tms .Namespace (), r .TxID , newFinalityListener ( r . Timeout , errs ) ))
4348
4449 // Listen for finality from DBs
4550 go func () {
46- _ , err := context .RunView (ttx .NewFinalityWithOpts (ttx .WithTxID (r .TxID ), ttx .WithTMSID (tms .ID ())))
51+ _ , err := context .RunView (ttx .NewFinalityWithOpts (ttx .WithTxID (r .TxID ), ttx .WithTMSID (tms .ID ()), ttx . WithTimeout ( r . Timeout ) ))
4752 errs <- err
4853 }()
4954
@@ -65,10 +70,21 @@ func (p *TxFinalityViewFactory) NewView(in []byte) (view.View, error) {
6570}
6671
6772type finalityListener struct {
68- errs chan error
73+ success func ()
74+ }
75+
76+ func newFinalityListener (timeout time.Duration , errs chan error ) * finalityListener {
77+ var once sync.Once
78+
79+ if timeout > 0 {
80+ time .AfterFunc (timeout , func () { once .Do (func () { errs <- errors .New ("timeout exceeded" ) }) })
81+ }
82+ return & finalityListener {
83+ success : func () { once .Do (func () { errs <- nil }) },
84+ }
6985}
7086
7187func (l * finalityListener ) OnStatus (ctx context.Context , txID string , status int , message string , tokenRequestHash []byte ) {
72- // fmt.Printf("Received finality from network for TX [%s][%d]", txID, status)
73- l .errs <- nil
88+ fmt .Printf ("Received finality from network for TX [%s][%d]" , txID , status )
89+ l .success ()
7490}
0 commit comments