Skip to content
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

[FAB-17140] Add go commercial paper contract #102

Merged
merged 1 commit into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion ci/azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,14 @@ jobs:
steps:
- template: install-deps.yml
- template: install-fabric.yml
- template: commercialpaper-java.yml
- template: commercialpaper-java.yml
- job: commercialpaper_go
displayName: CommercialPaper (Go)
pool:
vmImage: ubuntu-18.04
dependsOn: []
timeoutInMinutes: 60
steps:
- template: install-deps.yml
- template: install-fabric.yml
- template: commercialpaper-go.yml
50 changes: 50 additions & 0 deletions ci/commercialpaper-go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#
# SPDX-License-Identifier: Apache-2.0
#

steps:
- script: go test ./...
workingDirectory: commercial-paper/organization/magnetocorp/contract-go
displayName: Go unit test magnetocorp
- script: go test ./...
workingDirectory: commercial-paper/organization/digibank/contract-go
displayName: Go unit test digibank
- script: bash start.sh
workingDirectory: basic-network
displayName: Start Fabric
- script: |
docker-compose -f docker-compose.yml up -d cliMagnetoCorp

docker exec cliMagnetoCorp bash -c 'cd /opt/gopath/src/github.com/hyperledger/fabric-samples/commercial-paper/organization/magnetocorp/contract-go && go mod vendor'

docker exec cliMagnetoCorp peer lifecycle chaincode package cp.tar.gz --lang golang --path github.com/hyperledger/fabric-samples/commercial-paper/organization/magnetocorp/contract-go --label cp_0
docker exec cliMagnetoCorp peer lifecycle chaincode install cp.tar.gz
export PACKAGE_ID=$(docker exec cliMagnetoCorp peer lifecycle chaincode queryinstalled 2>&1 | awk -F "[, ]+" '/Label: /{print $3}')

docker exec cliMagnetoCorp peer lifecycle chaincode approveformyorg --channelID mychannel --name papercontract -v 0 --package-id $PACKAGE_ID --sequence 1 --signature-policy "AND ('Org1MSP.member')"
docker exec cliMagnetoCorp peer lifecycle chaincode commit -o orderer.example.com:7050 --channelID mychannel --name papercontract -v 0 --sequence 1 --waitForEvent --signature-policy "AND ('Org1MSP.member')"
docker exec cliMagnetoCorp peer chaincode invoke -o orderer.example.com:7050 --channelID mychannel --name papercontract -c '{"Args":["org.papernet.commercialpaper:instantiate"]}' --waitForEvent

workingDirectory: commercial-paper/organization/magnetocorp/configuration/cli
displayName: Setup Commercial Paper contract

- script: retry -- npm install
workingDirectory: commercial-paper/organization/magnetocorp/application
displayName: Install Magnetocorp application
- script: |
set -ex
node addToWallet.js
node issue.js
workingDirectory: commercial-paper/organization/magnetocorp/application
displayName: Magnetocorp issue paper

- script: retry -- npm install
workingDirectory: commercial-paper/organization/digibank/application
displayName: Install Digibank application
- script: |
set -ex
node addToWallet.js
node buy.js
node redeem.js
workingDirectory: commercial-paper/organization/digibank/application
displayName: Digibank issue paper
2 changes: 1 addition & 1 deletion ci/commercialpaper-java.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ steps:
- script: |
docker-compose -f docker-compose.yml up -d cliDigiBank

docker exec cliDigiBank peer lifecycle chaincode package cp.tar.gz --lang java --path /opt/gopath/src/github.com/contract-java/build/libs --label cp_0
docker exec cliDigiBank peer lifecycle chaincode package cp.tar.gz --lang java --path /opt/gopath/src/github.com/hyperledger/fabric-samples/commercial-paper/organization/digibank/contract-java/build/libs --label cp_0
docker exec cliDigiBank peer lifecycle chaincode install cp.tar.gz
export PACKAGE_ID=$(docker exec cliDigiBank peer lifecycle chaincode queryinstalled 2>&1 | awk -F "[, ]+" '/Label: /{print $3}')

Expand Down
2 changes: 1 addition & 1 deletion ci/commercialpaper-javascript.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ steps:
- script: |
docker-compose -f docker-compose.yml up -d cliMagnetoCorp

docker exec cliMagnetoCorp peer lifecycle chaincode package cp.tar.gz --lang node --path /opt/gopath/src/github.com/contract --label cp_0
docker exec cliMagnetoCorp peer lifecycle chaincode package cp.tar.gz --lang node --path /opt/gopath/src/github.com/hyperledger/fabric-samples/commercial-paper/organization/magnetocorp/contract --label cp_0
docker exec cliMagnetoCorp peer lifecycle chaincode install cp.tar.gz
export PACKAGE_ID=$(docker exec cliMagnetoCorp peer lifecycle chaincode queryinstalled 2>&1 | awk -F "[, ]+" '/Label: /{print $3}')

Expand Down
17 changes: 15 additions & 2 deletions commercial-paper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ This will start a docker container for Fabric CLI commands, and put you in the c
**For a JavaScript Contract:**

```
docker exec cliMagnetoCorp peer lifecycle chaincode package cp.tar.gz --lang node --path /opt/gopath/src/github.com/contract --label cp_0
docker exec cliMagnetoCorp peer lifecycle chaincode package cp.tar.gz --lang node --path /opt/gopath/src/github.com/hyperledger/fabric-samples/commercial-paper/organization/magnetocorp/contract --label cp_0
docker exec cliMagnetoCorp peer lifecycle chaincode install cp.tar.gz
export PACKAGE_ID=$(docker exec cliMagnetoCorp peer lifecycle chaincode queryinstalled 2>&1 | awk -F "[, ]+" '/Label: /{print $3}')

Expand All @@ -87,13 +87,26 @@ pushd ./organization/magnetocorp/contract-java

popd

docker exec cliMagnetoCorp peer chaincode install -n papercontract -v 0 -p /opt/gopath/src/github.com/contract-java/build/install/papercontract -l java
docker exec cliMagnetoCorp peer chaincode install -n papercontract -v 0 -p /opt/gopath/src/github.com/hyperledger/fabric-samples/commercial-paper/organization/magnetocorp/contract-java/build/install/papercontract -l java

docker exec cliMagnetoCorp peer chaincode instantiate -n papercontract -v 0 -l java -c '{"Args":["org.papernet.commercialpaper:instantiate"]}' -C mychannel -P "AND ('Org1MSP.member')"
```

> If you want to try both a Java and JavaScript Contract, then you will need to restart the infrastructure and deploy the other contract.

**For a Go Contract:**
```
docker exec cliMagnetoCorp bash -c 'cd /opt/gopath/src/github.com/hyperledger/fabric-samples/commercial-paper/organization/magnetocorp/contract-go && go mod vendor'

docker exec cliMagnetoCorp peer lifecycle chaincode package cp.tar.gz --lang golang --path github.com/hyperledger/fabric-samples/commercial-paper/organization/magnetocorp/contract-go --label cp_0
docker exec cliMagnetoCorp peer lifecycle chaincode install cp.tar.gz
export PACKAGE_ID=$(docker exec cliMagnetoCorp peer lifecycle chaincode queryinstalled 2>&1 | awk -F "[, ]+" '/Label: /{print $3}')

docker exec cliMagnetoCorp peer lifecycle chaincode approveformyorg --channelID mychannel --name papercontract -v 0 --package-id $PACKAGE_ID --sequence 1 --signature-policy "AND ('Org1MSP.member')"
docker exec cliMagnetoCorp peer lifecycle chaincode commit -o orderer.example.com:7050 --channelID mychannel --name papercontract -v 0 --sequence 1 --waitForEvent --signature-policy "AND ('Org1MSP.member')"
docker exec cliMagnetoCorp peer chaincode invoke -o orderer.example.com:7050 --channelID mychannel --name papercontract -c '{"Args":["org.papernet.commercialpaper:instantiate"]}' --waitForEvent
```

## Client Applications

Note for Java applications you will need to compile the Java Code using maven. Use this command in each application-java directory
Expand Down
4 changes: 2 additions & 2 deletions commercial-paper/cp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ docker ps
# cd "${DIR}/commercial-paper/organization/magnetocorp/configuration/cli"
# docker-compose -f docker-compose.yml up -d cliMagnetoCorp

# docker exec cliMagnetoCorp peer lifecycle chaincode package cp.tar.gz --lang node --path /opt/gopath/src/github.com/contract --label cp_0
# docker exec cliMagnetoCorp peer lifecycle chaincode package cp.tar.gz --lang node --path /opt/gopath/src/github.com/hyperledger/fabric-samples/commercial-paper/organization/magnetocorp/contract --label cp_0
# docker exec cliMagnetoCorp peer lifecycle chaincode install cp.tar.gz
# export PACKAGE_ID=$(docker exec cliMagnetoCorp peer lifecycle chaincode queryinstalled 2>&1 | awk -F "[, ]+" '/Label: /{print $3}')

Expand All @@ -37,7 +37,7 @@ docker ps
cd "${DIR}/commercial-paper/organization/digibank/configuration/cli"
docker-compose -f docker-compose.yml up -d cliDigiBank
CLI_CONTAINER=cliDigiBank
docker exec ${CLI_CONTAINER} peer lifecycle chaincode package cp.tar.gz --lang java --path /opt/gopath/src/github.com/contract-java/build/libs --label cp_0
docker exec ${CLI_CONTAINER} peer lifecycle chaincode package cp.tar.gz --lang java --path /opt/gopath/src/github.com/hyperledger/fabric-samples/commercial-paper/organization/magnetocorp/contract-java/build/libs --label cp_0
docker exec ${CLI_CONTAINER} peer lifecycle chaincode install cp.tar.gz
export PACKAGE_ID=$(docker exec ${CLI_CONTAINER} peer lifecycle chaincode queryinstalled 2>&1 | awk -F "[, ]+" '/Label: /{print $3}')

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ services:
command: /bin/bash
volumes:
- /var/run/:/host/var/run/
- ./../../../../organization/digibank:/opt/gopath/src/github.com/
- ./../../../../organization/digibank:/opt/gopath/src/github.com/hyperledger/fabric-samples/commercial-paper/organization/digibank
awjh-ibm marked this conversation as resolved.
Show resolved Hide resolved
- ./../../../../../basic-network/crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
networks:
- basic
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* SPDX-License-Identifier: Apache-2.0
*/

package commercialpaper

import (
"encoding/json"
"fmt"

ledgerapi "github.com/hyperledger/fabric-samples/commercial-paper/organization/digibank/contract-go/ledger-api"
)

// State enum for commercial paper state property
type State uint

const (
// ISSUED state for when a paper has been issued
ISSUED State = iota + 1
// TRADING state for when a paper is trading
TRADING
// REDEEMED state for when a paper has been redeemed
REDEEMED
)

func (state State) String() string {
names := []string{"ISSUED", "TRADING", "REDEEMED"}

if state < ISSUED || state > REDEEMED {
return "UNKNOWN"
}

return names[state-1]
}

// CreateCommercialPaperKey creates a key for commercial papers
func CreateCommercialPaperKey(issuer string, paperNumber string) string {
return ledgerapi.MakeKey(issuer, paperNumber)
}

// Used for managing the fact status is private but want it in world state
type commercialPaperAlias CommercialPaper
type jsonCommercialPaper struct {
*commercialPaperAlias
State State `json:"currentState"`
Class string `json:"class"`
Key string `json:"key"`
}

// CommercialPaper defines a commercial paper
type CommercialPaper struct {
PaperNumber string `json:"paperNumber"`
Issuer string `json:"issuer"`
IssueDateTime string `json:"issueDateTime"`
FaceValue int `json:"faceValue"`
MaturityDateTime string `json:"maturityDateTime"`
Owner string `json:"owner"`
state State `metadata:"currentState"`
class string `metadata:"class"`
key string `metadata:"key"`
}

// UnmarshalJSON special handler for managing JSON marshalling
func (cp *CommercialPaper) UnmarshalJSON(data []byte) error {
jcp := jsonCommercialPaper{commercialPaperAlias: (*commercialPaperAlias)(cp)}

err := json.Unmarshal(data, &jcp)

if err != nil {
return err
}

cp.state = jcp.State

return nil
}

// MarshalJSON special handler for managing JSON marshalling
func (cp CommercialPaper) MarshalJSON() ([]byte, error) {
jcp := jsonCommercialPaper{commercialPaperAlias: (*commercialPaperAlias)(&cp), State: cp.state, Class: "org.papernet.commercialpaper", Key: ledgerapi.MakeKey(cp.Issuer, cp.PaperNumber)}

return json.Marshal(&jcp)
}

// GetState returns the state
func (cp *CommercialPaper) GetState() State {
return cp.state
}

// SetIssued returns the state to issued
func (cp *CommercialPaper) SetIssued() {
cp.state = ISSUED
}

// SetTrading sets the state to trading
func (cp *CommercialPaper) SetTrading() {
cp.state = TRADING
}

// SetRedeemed sets the state to redeemed
func (cp *CommercialPaper) SetRedeemed() {
cp.state = REDEEMED
}

// IsIssued returns true if state is issued
func (cp *CommercialPaper) IsIssued() bool {
return cp.state == ISSUED
}

// IsTrading returns true if state is trading
func (cp *CommercialPaper) IsTrading() bool {
return cp.state == TRADING
}

// IsRedeemed returns true if state is redeemed
func (cp *CommercialPaper) IsRedeemed() bool {
return cp.state == REDEEMED
}

// GetSplitKey returns values which should be used to form key
func (cp *CommercialPaper) GetSplitKey() []string {
return []string{cp.Issuer, cp.PaperNumber}
}

// Serialize formats the commercial paper as JSON bytes
func (cp *CommercialPaper) Serialize() ([]byte, error) {
return json.Marshal(cp)
}

// Deserialize formats the commercial paper from JSON bytes
func Deserialize(bytes []byte, cp *CommercialPaper) error {
err := json.Unmarshal(bytes, cp)

if err != nil {
return fmt.Errorf("Error deserializing commercial paper. %s", err.Error())
}

return nil
}
Loading