mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-18 16:05:10 +00:00
Starting from the processor.Block.Process all methods now return errors if something goes wrong with unpacking of the blocks and reading the transactions. In each function where the error is being propagated back to client it is wrapped in a message with the function name. This makes it easier to track down the error and see the propagation chain. Finally the error is logged to the terminal and the go routine shuts down gracefully. The graceful shutdown executes all deferred functions which close the context, the checkpointer and the gateway. Before panics were used everywhere which was an issue because the unpacking of the blocks happened in a go routine. When a panic happens in a go routine only the deferred functions of the go routine are called but not those of the client which lead to unexpected behavior. The transact function is also executed in a go routine therefore the same typo of error handling was implemented there. Signed-off-by: Stanislav Jakuschevskij <stas@two-giants.com>
78 lines
1.9 KiB
Go
78 lines
1.9 KiB
Go
package store
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
"offChainData/utils"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
var storeFile = utils.EnvOrDefault("STORE_FILE", "store.log")
|
|
var SimulatedFailureCount = getSimulatedFailureCount()
|
|
var transactionCount uint = 0 // Used only to simulate failures
|
|
|
|
// Apply writes for a given transaction to off-chain data store, ideally in a single operation for fault tolerance.
|
|
// This implementation just writes to a file.
|
|
func ApplyWritesToOffChainStore(data LedgerUpdate) error {
|
|
funcName := "ApplyWritesToOffChainStore"
|
|
|
|
if err := simulateFailureIfRequired(); err != nil {
|
|
return fmt.Errorf("in %s: %w", funcName, err)
|
|
}
|
|
|
|
writes := []string{}
|
|
for _, write := range data.Writes {
|
|
marshaled, err := json.Marshal(write)
|
|
if err != nil {
|
|
return fmt.Errorf("in %s: %w", funcName, err)
|
|
}
|
|
|
|
writes = append(writes, string(marshaled))
|
|
}
|
|
|
|
f, err := os.OpenFile(storeFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
if err != nil {
|
|
return fmt.Errorf("in %s: %w", funcName, err)
|
|
}
|
|
|
|
if _, err := f.Write([]byte(strings.Join(writes, "\n") + "\n")); err != nil {
|
|
f.Close()
|
|
return fmt.Errorf("in %s: %w", funcName, err)
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
|
return fmt.Errorf("in %s: %w", funcName, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func simulateFailureIfRequired() error {
|
|
if SimulatedFailureCount > 0 && transactionCount >= SimulatedFailureCount {
|
|
transactionCount = 0
|
|
return errors.New("expected error: simulated write failure")
|
|
}
|
|
|
|
transactionCount += 1
|
|
|
|
return nil
|
|
}
|
|
|
|
func getSimulatedFailureCount() uint {
|
|
valueAsString := utils.EnvOrDefault("SIMULATED_FAILURE_COUNT", "0")
|
|
valueAsFloat, err := strconv.ParseFloat(valueAsString, 64)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
result := math.Floor(valueAsFloat)
|
|
if valueAsFloat < 0 {
|
|
panic(fmt.Errorf("invalid SIMULATED_FAILURE_COUNT value: %s", valueAsString))
|
|
}
|
|
|
|
return uint(result)
|
|
}
|