Skip to content

Commit

Permalink
Add first batch of pull request rework
Browse files Browse the repository at this point in the history
- update Application section in README
- remove param name in app.go
- add error checks in processor/block.go
- move vars from model to transact logic
- move newAsset to transact
- use ID for well-known initialisms
- move randomelement, randomnint and differentelement to transact
- remove AssertDefined
- blockTxIdsJoinedByComma: use standard library to join elements
- return nil, instead of []byte{}
- remove go routine in listen.go
- move cache to parser
- inline processor in listen.go
- move store to main package
- move util to main package
- fixed failing cache issue
- fixed staticcheck issues

Signed-off-by: Stanislav Jakuschevskij <[email protected]>
  • Loading branch information
twoGiants committed Jan 20, 2025
1 parent 69c521f commit d4a1546
Show file tree
Hide file tree
Showing 23 changed files with 496 additions and 608 deletions.
3 changes: 3 additions & 0 deletions off_chain_data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ The client application provides several "commands" that can be invoked using the
- **getAllAssets**: Retrieve the current details of all assets recorded on the ledger. See:
- TypeScript: [application-typescript/src/getAllAssets.ts](application-typescript/src/getAllAssets.ts)
- Java: [application-java/app/src/main/java/GetAllAssets.java](application-java/app/src/main/java/GetAllAssets.java)
- Go: [application-go/getAllAssets.go](application-go/getAllAssets.go)
- **listen**: Listen for block events, and use them to replicate ledger updates in an off-chain data store. See:
- TypeScript: [application-typescript/src/listen.ts](application-typescript/src/listen.ts)
- Java: [application-java/app/src/main/java/Listen.java](application-java/app/src/main/java/Listen.java)
- Go: [application-go/listen.go](application-go/listen.go)
- **transact**: Submit a set of transactions to create, modify and delete assets. See:
- TypeScript: [application-typescript/src/transact.ts](application-typescript/src/transact.ts)
- Java: [application-java/app/src/main/java/Transact.java](application-java/app/src/main/java/Transact.java)
- Go: [application-go/transact.go](application-go/transact.go)

To keep the sample code concise, the **listen** command writes ledger updates to an output file named `store.log` in the current working directory (which for the Java sample is the `application-java/app` directory). A real implementation could write ledger updates directly to an off-chain data store of choice. You can inspect the information captured in this file as you run the sample.

Expand Down
2 changes: 1 addition & 1 deletion off_chain_data/application-go/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"google.golang.org/grpc"
)

var allCommands = map[string]func(clientConnection *grpc.ClientConn){
var allCommands = map[string]func(*grpc.ClientConn){
"getAllAssets": getAllAssets,
"transact": transact,
"listen": listen,
Expand Down
19 changes: 9 additions & 10 deletions off_chain_data/application-go/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package main
import (
"crypto/x509"
"fmt"
"offChainData/utils"
"os"
"path"
"time"
Expand All @@ -24,27 +23,27 @@ import (
const peerName = "peer0.org1.example.com"

var (
channelName = utils.EnvOrDefault("CHANNEL_NAME", "mychannel")
chaincodeName = utils.EnvOrDefault("CHAINCODE_NAME", "basic")
mspID = utils.EnvOrDefault("MSP_ID", "Org1MSP")
channelName = envOrDefault("CHANNEL_NAME", "mychannel")
chaincodeName = envOrDefault("CHAINCODE_NAME", "basic")
mspID = envOrDefault("MSP_ID", "Org1MSP")

// Path to crypto materials.
cryptoPath = utils.EnvOrDefault("CRYPTO_PATH", "../../test-network/organizations/peerOrganizations/org1.example.com")
cryptoPath = envOrDefault("CRYPTO_PATH", "../../test-network/organizations/peerOrganizations/org1.example.com")

// Path to user private key directory.
keyDirectoryPath = utils.EnvOrDefault("KEY_DIRECTORY_PATH", cryptoPath+"/users/[email protected]/msp/keystore")
keyDirectoryPath = envOrDefault("KEY_DIRECTORY_PATH", cryptoPath+"/users/[email protected]/msp/keystore")

// Path to user certificate.
certPath = utils.EnvOrDefault("CERT_PATH", cryptoPath+"/users/[email protected]/msp/signcerts/cert.pem")
certPath = envOrDefault("CERT_PATH", cryptoPath+"/users/[email protected]/msp/signcerts/cert.pem")

// Path to peer tls certificate.
tlsCertPath = utils.EnvOrDefault("TLS_CERT_PATH", cryptoPath+"/peers/peer0.org1.example.com/tls/ca.crt")
tlsCertPath = envOrDefault("TLS_CERT_PATH", cryptoPath+"/peers/peer0.org1.example.com/tls/ca.crt")

// Gateway peer endpoint.
peerEndpoint = utils.EnvOrDefault("PEER_ENDPOINT", "dns:///localhost:7051")
peerEndpoint = envOrDefault("PEER_ENDPOINT", "dns:///localhost:7051")

// Gateway peer SSL host name override.
peerHostAlias = utils.EnvOrDefault("PEER_HOST_ALIAS", peerName)
peerHostAlias = envOrDefault("PEER_HOST_ALIAS", peerName)
)

func newGrpcConnection() *grpc.ClientConn {
Expand Down
2 changes: 1 addition & 1 deletion off_chain_data/application-go/contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (atb *AssetTransferBasic) DeleteAsset(id string) error {
func (atb *AssetTransferBasic) GetAllAssets() ([]byte, error) {
result, err := atb.contract.Evaluate("GetAllAssets")
if err != nil {
return []byte{}, fmt.Errorf("in GetAllAssets: %w", err)
return nil, fmt.Errorf("in GetAllAssets: %w", err)
}
return result, nil
}
31 changes: 0 additions & 31 deletions off_chain_data/application-go/contract/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,10 @@
*/
package contract

import (
"offChainData/utils"

"github.com/google/uuid"
)

var (
colors = []string{"red", "green", "blue"}
Owners = []string{"alice", "bob", "charlie"}
)

const (
maxInitialValue = 1000
maxInitialSize = 10
)

type Asset struct {
ID string `json:"ID"`
Color string `json:"Color"`
Size uint64 `json:"Size"`
Owner string `json:"Owner"`
AppraisedValue uint64 `json:"AppraisedValue"`
}

func NewAsset() Asset {
id, err := uuid.NewRandom()
if err != nil {
panic(err)
}

return Asset{
ID: id.String(),
Color: utils.RandomElement(colors),
Size: uint64(utils.RandomInt(maxInitialSize) + 1),
Owner: utils.RandomElement(Owners),
AppraisedValue: uint64(utils.RandomInt(maxInitialValue) + 1),
}
}
2 changes: 1 addition & 1 deletion off_chain_data/application-go/getAllAssets.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"bytes"
"encoding/json"
"fmt"
atb "offChainData/contract"
atb "offchaindata/contract"

"github.com/hyperledger/fabric-gateway/pkg/client"
"google.golang.org/grpc"
Expand Down
2 changes: 1 addition & 1 deletion off_chain_data/application-go/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module offChainData
module offchaindata

go 1.22.0

Expand Down
Loading

0 comments on commit d4a1546

Please sign in to comment.