mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-17 23:45:10 +00:00
Created packages for the flat file store and the processor and moved functions, variables and constants from listener.go to those packages. Encapsulated everything not used outside the packages, introduced model.go files which later might be extracted into a model package and renamed parser/parsedBlock.go to parser/block.go. Signed-off-by: Stanislav Jakuschevskij <stas@two-giants.com>
76 lines
1.7 KiB
Go
76 lines
1.7 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) {
|
|
if err := simulateFailureIfRequired(); err != nil {
|
|
fmt.Println("[expected error]: " + err.Error())
|
|
return
|
|
}
|
|
|
|
writes := []string{}
|
|
for _, write := range data.Writes {
|
|
// TODO write also the TxID and block number so that you can compare easier to the output
|
|
marshaled, err := json.Marshal(write)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
writes = append(writes, string(marshaled))
|
|
}
|
|
|
|
f, err := os.OpenFile(storeFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if _, err := f.Write([]byte(strings.Join(writes, "\n") + "\n")); err != nil {
|
|
f.Close()
|
|
panic(err)
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func simulateFailureIfRequired() error {
|
|
if SimulatedFailureCount > 0 && transactionCount >= SimulatedFailureCount {
|
|
transactionCount = 0
|
|
return errors.New("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)
|
|
}
|