Skip to content

Commit 37bc858

Browse files
Added timeout for tx finality view
Signed-off-by: Alexandros Filios <alexandros.filios@ibm.com>
1 parent 2b04de8 commit 37bc858

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

integration/token/common/support.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ SPDX-License-Identifier: Apache-2.0
77
package common
88

99
import (
10+
"time"
11+
1012
"github.com/hyperledger-labs/fabric-smart-client/integration"
1113
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/common"
1214
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/services/endorser"
@@ -21,8 +23,9 @@ func CheckFinality(network *integration.Infrastructure, id *token2.NodeReference
2123
return
2224
}
2325
_, err := network.Client(id.ReplicaName()).CallView("TxFinality", common.JSONMarshall(&views.TxFinality{
24-
TxID: txID,
25-
TMSID: tmsID,
26+
TxID: txID,
27+
TMSID: tmsID,
28+
Timeout: 30 * time.Second,
2629
}))
2730
if fail {
2831
Expect(err).To(HaveOccurred())

integration/token/common/views/finality.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,22 @@ package views
99
import (
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

2024
type TxFinality struct {
21-
TxID string
22-
TMSID *token.TMSID
25+
TxID string
26+
TMSID *token.TMSID
27+
Timeout time.Duration
2328
}
2429

2530
type 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

6772
type 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

7187
func (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

Comments
 (0)