File tree Expand file tree Collapse file tree 2 files changed +22
-13
lines changed
off_chain_data/application-go Expand file tree Collapse file tree 2 files changed +22
-13
lines changed Original file line number Diff line number Diff line change @@ -44,16 +44,20 @@ func listen(clientConnection *grpc.ClientConn) {
4444 }
4545
4646 ctx , stop := signal .NotifyContext (context .Background (), os .Interrupt , syscall .SIGTERM )
47+ stopCalled := false
4748 defer func () {
48- stop ()
49- fmt .Println ("Context closed." )
49+ if ! stopCalled {
50+ stop ()
51+ stopCalled = true
52+ fmt .Println ("Context closed." )
53+ }
5054 }()
5155
5256 network := gateway .GetNetwork (channelName )
5357 blocks , err := network .BlockEvents (
5458 ctx ,
55- client .WithCheckpoint (checkpointer ),
5659 client .WithStartBlock (0 ), // Used only if there is no checkpoint block number
60+ client .WithCheckpoint (checkpointer ),
5761 )
5862 if err != nil {
5963 panic (err )
@@ -64,6 +68,18 @@ func listen(clientConnection *grpc.ClientConn) {
6468
6569 go func () {
6670 defer wg .Done ()
71+ defer func () {
72+ msg := "[expected error]: simulated write failure"
73+ r := recover ()
74+ v , ok := r .(string )
75+ if ok && v == msg {
76+ if ! stopCalled {
77+ stop ()
78+ stopCalled = true
79+ fmt .Println ("Context closed after simulated write failure." )
80+ }
81+ }
82+ }()
6783
6884 for blockProto := range blocks {
6985 select {
Original file line number Diff line number Diff line change @@ -2,7 +2,6 @@ package store
22
33import (
44 "encoding/json"
5- "errors"
65 "fmt"
76 "math"
87 "offChainData/utils"
@@ -18,14 +17,10 @@ var transactionCount uint = 0 // Used only to simulate failures
1817// Apply writes for a given transaction to off-chain data store, ideally in a single operation for fault tolerance.
1918// This implementation just writes to a file.
2019func ApplyWritesToOffChainStore (data LedgerUpdate ) {
21- if err := simulateFailureIfRequired (); err != nil {
22- fmt .Println ("[expected error]: " + err .Error ())
23- return
24- }
20+ simulateFailureIfRequired ()
2521
2622 writes := []string {}
2723 for _ , write := range data .Writes {
28- // TODO write also the TxID and block number so that you can compare easier to the output
2924 marshaled , err := json .Marshal (write )
3025 if err != nil {
3126 panic (err )
@@ -49,15 +44,13 @@ func ApplyWritesToOffChainStore(data LedgerUpdate) {
4944 }
5045}
5146
52- func simulateFailureIfRequired () error {
47+ func simulateFailureIfRequired () {
5348 if SimulatedFailureCount > 0 && transactionCount >= SimulatedFailureCount {
5449 transactionCount = 0
55- return errors . New ( " simulated write failure" )
50+ panic ( "[expected error]: simulated write failure" )
5651 }
5752
5853 transactionCount += 1
59-
60- return nil
6154}
6255
6356func getSimulatedFailureCount () uint {
You can’t perform that action at this time.
0 commit comments