-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add off-chain-data go client application #1269
Open
twoGiants
wants to merge
16
commits into
hyperledger:main
Choose a base branch
from
twoGiants:off-chain-data-go
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,753
−7
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a012f78
Add off-chain-data go client application
twoGiants 3caadda
Add endorser tx unwrapping test
twoGiants 6ac8bed
Implement payload interface
twoGiants c6651f0
Implement Transaction interface
twoGiants d566a86
Encapsulate block parser in a package
twoGiants 3df357d
Implement block parsing
twoGiants d4071cb
Implement block and transaction processor
twoGiants df52240
Implement store
twoGiants 407a942
Implement caching
twoGiants 3a70427
Extract block processor and store from listener
twoGiants 77fad6f
Implement graceful shutdown of listen function
twoGiants dfcc123
Refactor parser package, decompose files
twoGiants 41e0544
Execute transactions in go routines concurrently
twoGiants ed3737d
Fix simulated failure issue
twoGiants f9285d5
Replace panic with error handling
twoGiants 69c521f
Update README and ci
twoGiants File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2024 IBM All Rights Reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"google.golang.org/grpc" | ||
) | ||
|
||
var allCommands = map[string]func(clientConnection *grpc.ClientConn){ | ||
"getAllAssets": getAllAssets, | ||
"transact": transact, | ||
"listen": listen, | ||
} | ||
|
||
func main() { | ||
commands := os.Args[1:] | ||
if len(commands) == 0 { | ||
printUsage() | ||
panic(errors.New("missing command")) | ||
} | ||
|
||
for _, name := range commands { | ||
if _, exists := allCommands[name]; !exists { | ||
printUsage() | ||
panic(fmt.Errorf("unknown command: %s", name)) | ||
} | ||
fmt.Println("command:", name) | ||
} | ||
|
||
client := newGrpcConnection() | ||
defer client.Close() | ||
|
||
for _, name := range commands { | ||
command := allCommands[name] | ||
command(client) | ||
} | ||
} | ||
|
||
func printUsage() { | ||
fmt.Println("Arguments: <command1> [<command2> ...]") | ||
fmt.Println("Available commands:", availableCommands()) | ||
} | ||
|
||
func availableCommands() string { | ||
result := make([]string, len(allCommands)) | ||
i := 0 | ||
for command := range allCommands { | ||
result[i] = command | ||
i++ | ||
} | ||
|
||
return strings.Join(result, ", ") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* Copyright 2024 IBM All Rights Reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"crypto/x509" | ||
"fmt" | ||
"offChainData/utils" | ||
"os" | ||
"path" | ||
"time" | ||
|
||
"github.com/hyperledger/fabric-gateway/pkg/client" | ||
"github.com/hyperledger/fabric-gateway/pkg/hash" | ||
"github.com/hyperledger/fabric-gateway/pkg/identity" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials" | ||
) | ||
|
||
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") | ||
|
||
// Path to crypto materials. | ||
cryptoPath = utils.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") | ||
|
||
// Path to user certificate. | ||
certPath = utils.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") | ||
|
||
// Gateway peer endpoint. | ||
peerEndpoint = utils.EnvOrDefault("PEER_ENDPOINT", "dns:///localhost:7051") | ||
|
||
// Gateway peer SSL host name override. | ||
peerHostAlias = utils.EnvOrDefault("PEER_HOST_ALIAS", peerName) | ||
) | ||
|
||
func newGrpcConnection() *grpc.ClientConn { | ||
certificatePEM, err := os.ReadFile(tlsCertPath) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to read TLS certificate file: %w", err)) | ||
} | ||
|
||
certificate, err := identity.CertificateFromPEM(certificatePEM) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
certPool := x509.NewCertPool() | ||
certPool.AddCert(certificate) | ||
transportCredentials := credentials.NewClientTLSFromCert(certPool, peerHostAlias) | ||
|
||
connection, err := grpc.NewClient(peerEndpoint, grpc.WithTransportCredentials(transportCredentials)) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to create gRPC connection: %w", err)) | ||
} | ||
|
||
return connection | ||
} | ||
|
||
func newConnectOptions(clientConnection *grpc.ClientConn) (identity.Identity, []client.ConnectOption) { | ||
return newIdentity(), []client.ConnectOption{ | ||
client.WithSign(newSign()), | ||
client.WithHash(hash.SHA256), | ||
client.WithClientConnection(clientConnection), | ||
client.WithEvaluateTimeout(5 * time.Second), | ||
client.WithEndorseTimeout(15 * time.Second), | ||
client.WithSubmitTimeout(5 * time.Second), | ||
client.WithCommitStatusTimeout(1 * time.Minute), | ||
} | ||
} | ||
|
||
func newIdentity() *identity.X509Identity { | ||
certificatePEM, err := os.ReadFile(certPath) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to read certificate file: %w", err)) | ||
} | ||
|
||
certificate, err := identity.CertificateFromPEM(certificatePEM) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
id, err := identity.NewX509Identity(mspID, certificate) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return id | ||
} | ||
|
||
func newSign() identity.Sign { | ||
privateKeyPEM, err := readFirstFile(keyDirectoryPath) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to read private key file: %w", err)) | ||
} | ||
|
||
privateKey, err := identity.PrivateKeyFromPEM(privateKeyPEM) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
sign, err := identity.NewPrivateKeySign(privateKey) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return sign | ||
} | ||
|
||
func readFirstFile(dirPath string) ([]byte, error) { | ||
dir, err := os.Open(dirPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fileNames, err := dir.Readdirnames(1) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return os.ReadFile(path.Join(dirPath, fileNames[0])) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,71 @@ | ||||||
/* | ||||||
* Copyright 2024 IBM All Rights Reserved. | ||||||
* | ||||||
* SPDX-License-Identifier: Apache-2.0 | ||||||
*/ | ||||||
package contract | ||||||
|
||||||
import ( | ||||||
"fmt" | ||||||
"strconv" | ||||||
|
||||||
"github.com/hyperledger/fabric-gateway/pkg/client" | ||||||
) | ||||||
|
||||||
type AssetTransferBasic struct { | ||||||
contract *client.Contract | ||||||
} | ||||||
|
||||||
func NewAssetTransferBasic(contract *client.Contract) *AssetTransferBasic { | ||||||
return &AssetTransferBasic{contract} | ||||||
} | ||||||
|
||||||
func (atb *AssetTransferBasic) CreateAsset(anAsset Asset) error { | ||||||
if _, err := atb.contract.Submit( | ||||||
"CreateAsset", | ||||||
client.WithArguments( | ||||||
anAsset.ID, | ||||||
anAsset.Color, | ||||||
strconv.FormatUint(anAsset.Size, 10), | ||||||
anAsset.Owner, | ||||||
strconv.FormatUint(anAsset.AppraisedValue, 10), | ||||||
)); err != nil { | ||||||
return fmt.Errorf("in CreateAsset: %w", err) | ||||||
} | ||||||
return nil | ||||||
} | ||||||
|
||||||
func (atb *AssetTransferBasic) TransferAsset(id, newOwner string) (string, error) { | ||||||
result, err := atb.contract.Submit( | ||||||
"TransferAsset", | ||||||
client.WithArguments( | ||||||
id, | ||||||
newOwner, | ||||||
), | ||||||
) | ||||||
if err != nil { | ||||||
return "", fmt.Errorf("in TransferAsset: %w", err) | ||||||
} | ||||||
|
||||||
return string(result), nil | ||||||
} | ||||||
|
||||||
func (atb *AssetTransferBasic) DeleteAsset(id string) error { | ||||||
if _, err := atb.contract.Submit( | ||||||
"DeleteAsset", | ||||||
client.WithArguments( | ||||||
id, | ||||||
), | ||||||
); err != nil { | ||||||
return fmt.Errorf("in DeleteAsset: %w", err) | ||||||
} | ||||||
return nil | ||||||
} | ||||||
|
||||||
func (atb *AssetTransferBasic) GetAllAssets() ([]byte, error) { | ||||||
result, err := atb.contract.Evaluate("GetAllAssets") | ||||||
if err != nil { | ||||||
return []byte{}, fmt.Errorf("in GetAllAssets: %w", err) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See https://go.dev/wiki/CodeReviewComments#declaring-empty-slices
Suggested change
|
||||||
} | ||||||
return result, nil | ||||||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
clientConnection
parameter name can be omitted here. Your choice which you find clearer though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Omitted.