Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
refactor (#103)
Browse files Browse the repository at this point in the history
fixes

Co-authored-by: Nikita Neznaemov <[email protected]>
  • Loading branch information
criro1 and Nikita Neznaemov authored Aug 26, 2022
1 parent 89d9dbf commit bd6231a
Show file tree
Hide file tree
Showing 161 changed files with 1,358 additions and 659 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ GOFLAGS ?= -mod=vendor

PROTO_PACKAGES_GO := state
PROTO_PACKAGES_GW := gateway
PROTO_PACKAGES_CC_WITHSERVICE_PREFIX := extensions
PROTO_PACKAGES_CC_WITH_SERVICE_PREFIX := extensions
PROTO_PACKAGES_CC := examples

test:
Expand All @@ -17,11 +17,11 @@ refresh-deps:

proto: clean
@for pkg in $(PROTO_PACKAGES_CC) ;do echo $$pkg && buf generate --template buf.gen.cc.yaml $$pkg -o ./$$(echo $$pkg | cut -d "/" -f1); done
@for pkg in $(PROTO_PACKAGES_CC_WITHSERVICE_PREFIX) ;do echo $$pkg && buf generate --template buf.gen.cc-with-service-prefix.yaml $$pkg -o ./$$(echo $$pkg | cut -d "/" -f1); done
@for pkg in $(PROTO_PACKAGES_CC_WITH_SERVICE_PREFIX) ;do echo $$pkg && buf generate --template buf.gen.cc-with-service-prefix.yaml $$pkg -o ./$$(echo $$pkg | cut -d "/" -f1); done
@for pkg in $(PROTO_PACKAGES_GW) ;do echo $$pkg && buf generate --template buf.gen.gw.yaml $$pkg -o ./$$(echo $$pkg | cut -d "/" -f1); done
@for pkg in $(PROTO_PACKAGES_GO) ;do echo $$pkg && buf generate --template buf.gen.go.yaml $$pkg -o ./$$(echo $$pkg | cut -d "/" -f1); done
clean:
@for pkg in $(PROTO_PACKAGES_CC); do find $$pkg \( -name '*.pb.go' -or -name '*.pb.cc.go' -or -name '*.pb.gw.go' -or -name '*.swagger.json' -or -name '*.pb.md' \) -delete;done
@for pkg in $(PROTO_PACKAGES_CC_WITHSERVICE_PREFIX); do find $$pkg \( -name '*.pb.go' -or -name '*.pb.cc.go' -or -name '*.pb.gw.go' -or -name '*.swagger.json' -or -name '*.pb.md' \) -delete;done
@for pkg in $(PROTO_PACKAGES_CC_WITH_SERVICE_PREFIX); do find $$pkg \( -name '*.pb.go' -or -name '*.pb.cc.go' -or -name '*.pb.gw.go' -or -name '*.swagger.json' -or -name '*.pb.md' \) -delete;done
@for pkg in $(PROTO_PACKAGES_GW); do find $$pkg \( -name '*.pb.go' -or -name '*.pb.gw.go' -or -name '*.swagger.json' -or -name '*.pb.md' \) -delete;done
@for pkg in $(PROTO_PACKAGES_GO); do find $$pkg \( -name '*.pb.go' -or -name '*.pb.md' \) -delete;done
@for pkg in $(PROTO_PACKAGES_GO); do find $$pkg \( -name '*.pb.go' -or -name '*.pb.md' \) -delete;done
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ message CommercialPaperList {

#### Chaincode transaction and events payload

This file defines the data payload used using in the business logic methods.
This file defines the data payload used in business logic methods.
In this example transaction and event payloads are exactly the same for the sake of brevity, but you could
create a different schema for each type of payload.

Expand Down Expand Up @@ -216,7 +216,7 @@ message RedeemCommercialPaper {

In [examples/cpaper_extended/chaincode.go](examples/cpaper_extended/chaincode.go) file we will define the mappings,
chaincode initialization method and business logic in the transaction methods.
For brevity we will only display snippets of the code here, please refer to the original file for full example.
For brevity, we will only display snippets of the code here, please refer to the original file for full example.

Firstly we define mapping rules. These specify the struct used to hold a specific chaincode state, it's primary key, list mapping, unique keys, etc.
Then we define the schemas used for emitting events.
Expand Down Expand Up @@ -354,7 +354,7 @@ func invokeCPaperBuy(c router.Context) (interface{}, error) {

And finally we should write tests to ensure our business logic is behaving as it should.
Again, for brevity, we omitted most of the code from [examples/cpaper_extended/chaincode_test.go](examples/cpaper_extended/chaincode_test.go).
CCKit support chaincode testing with [Mockstub](testing).
CCKit support chaincode testing with [MockStub](testing).

```go
var _ = Describe(`CommercialPaper`, func() {
Expand Down
2 changes: 1 addition & 1 deletion convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
package convert

import (
"errors"
"time"

"github.com/golang/protobuf/ptypes/timestamp"
"github.com/pkg/errors"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package convert_test
import (
"testing"

"github.com/s7techlab/cckit/convert"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/s7techlab/cckit/convert"
)

func TestState(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions convert/from_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package convert

import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/pkg/errors"
)

// FromBytes converts []byte to target interface
Expand Down Expand Up @@ -74,12 +74,12 @@ func FromBytesToStruct(bb []byte, target interface{}) (result interface{}, err e
}
}

// JsonUnmarshalPtr unmarshalls []byte as json to pointer, and returns value pointed to
// JSONUnmarshalPtr unmarshalls []byte as json to pointer, and returns value pointed to
func JSONUnmarshalPtr(bb []byte, to interface{}) (result interface{}, err error) {
targetPtr := reflect.New(reflect.ValueOf(to).Elem().Type()).Interface()
err = json.Unmarshal(bb, targetPtr)
if err != nil {
return nil, fmt.Errorf(ErrUnableToConvertValueToStruct.Error())
return nil, ErrUnableToConvertValueToStruct
}
return reflect.Indirect(reflect.ValueOf(targetPtr)).Interface(), nil
}
Expand All @@ -89,7 +89,7 @@ func ProtoUnmarshal(bb []byte, messageType proto.Message) (message proto.Message
msg := proto.Clone(messageType)
err = proto.Unmarshal(bb, msg)
if err != nil {
return nil, errors.Wrap(err, ErrUnableToConvertValueToStruct.Error())
return nil, ErrUnableToConvertValueToStruct
}
return msg, nil
}
Expand Down
12 changes: 5 additions & 7 deletions convert/to_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,24 @@ import (
"strconv"

"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
)

// ArgsToBytes converts func arguments to bytes
func ArgsToBytes(iargs ...interface{}) (aa [][]byte, err error) {
args := make([][]byte, len(iargs))
func ArgsToBytes(iArgs ...interface{}) ([][]byte, error) {
args := make([][]byte, len(iArgs))

for i, arg := range iargs {
for i, arg := range iArgs {
val, err := ToBytes(arg)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf(`unable to convert invoke arg[%d]`, i))
return nil, fmt.Errorf(`convert invoke arg[%d]: %w`, i, err)
}
args[i] = val
}

return args, nil
}

// ToBytes converts inteface{} (string, []byte , struct to ToByter interface to []byte for storing in state
// ToBytes converts interface{} (string, []byte , struct to ToByter interface to []byte for storing in state
func ToBytes(value interface{}) ([]byte, error) {
if value == nil {
return nil, nil
Expand Down Expand Up @@ -62,6 +61,5 @@ func ToBytes(value interface{}) ([]byte, error) {
`toBytes converting supports ToByter interface,struct,array,slice,bool and string, current type is %s`,
valueType)
}

}
}
1 change: 1 addition & 0 deletions examples/cars/bin/cars/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/hyperledger/fabric-chaincode-go/shim"

"github.com/s7techlab/cckit/examples/cars"
)

Expand Down
1 change: 1 addition & 0 deletions examples/cars/bin/cars_noac/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/hyperledger/fabric-chaincode-go/shim"

"github.com/s7techlab/cckit/examples/cars"
)

Expand Down
10 changes: 5 additions & 5 deletions examples/cars/cars.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Simple CRUD chaincode for store information about cars
// Package cars simple CRUD chaincode for store information about cars
package cars

import (
Expand Down Expand Up @@ -71,13 +71,13 @@ func queryCars(c router.Context) (interface{}, error) {
// carRegister car register chaincode method handler
func invokeCarRegister(c router.Context) (interface{}, error) {
// arg name defined in router method definition
p := c.Param(`car`).(CarPayload)
param := c.Param(`car`).(CarPayload)

t, _ := c.Time() // tx time
car := &Car{ // data for chaincode state
Id: p.Id,
Title: p.Title,
Owner: p.Owner,
Id: param.Id,
Title: param.Title,
Owner: param.Owner,
UpdatedAt: t,
}

Expand Down
2 changes: 1 addition & 1 deletion examples/cars/cars_noacces_control.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Simple CRUD chaincode for store information about cars
// Package cars simple CRUD chaincode for store information about cars
package cars

import (
Expand Down
1 change: 1 addition & 0 deletions examples/cars/cars_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"

"github.com/hyperledger/fabric-chaincode-go/shim"

"github.com/s7techlab/cckit/router"
p "github.com/s7techlab/cckit/router/param"
)
Expand Down
6 changes: 3 additions & 3 deletions examples/cars/cars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package cars_test
import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/s7techlab/cckit/examples/cars"
"github.com/s7techlab/cckit/extensions/owner"
"github.com/s7techlab/cckit/identity/testdata"
"github.com/s7techlab/cckit/state"
testcc "github.com/s7techlab/cckit/testing"
expectcc "github.com/s7techlab/cckit/testing/expect"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestCars(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion examples/cpaper/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Commercial paper Hyperledger Fabric chaincode example

This is an faithful reimplementation of the official example [Commercial paper scenario](https://hyperledger-fabric.readthedocs.io/en/release-1.4/developapps/scenario.html)
This is a faithful reimplementation of the official example [Commercial paper scenario](https://hyperledger-fabric.readthedocs.io/en/release-1.4/developapps/scenario.html)

Original code written in JavaScript using the Fabric Chaincode Node SDK is available [here](https://github.com/hyperledger/fabric-samples/tree/release-1.4/commercial-paper/organization/digibank/contract)
6 changes: 3 additions & 3 deletions examples/cpaper/chaincode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"testing"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/s7techlab/cckit/examples/cpaper"
testcc "github.com/s7techlab/cckit/testing"
expectcc "github.com/s7techlab/cckit/testing/expect"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

const (
Expand Down
12 changes: 6 additions & 6 deletions examples/cpaper_asservice/bin/api/mock/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ func main() {
log.Fatalln(err)
}

// Mockstub for commercial paper
cpaperMock := testing.NewMockStub(chaincodeName, cc)
// MockStub for commercial paper
cPaperMock := testing.NewMockStub(chaincodeName, cc)

// default identity for signing requests to peeer (mocked)
// default identity for signing requests to peer (mocked)
apiIdentity, err := testing.IdentityFromFile(`MSP`, `../../../testdata/admin.pem`, ioutil.ReadFile)
if err != nil {
log.Fatalln(err)
}
// Generated gateway for access to chaincode from external application
cpaperGateway := cpaper_asservice.NewCPaperServiceGateway(
cPaperGateway := cpaper_asservice.NewCPaperServiceGateway(
// Chaincode invocation service mock. For real network you can use example with hlf-sdk-go
testing.NewPeer().WithChannel(channelName, cpaperMock),
testing.NewPeer().WithChannel(channelName, cPaperMock),
channelName,
chaincodeName,
gateway.WithDefaultSigner(apiIdentity))
Expand All @@ -60,7 +60,7 @@ func main() {

// Create gRPC server
s := grpc.NewServer()
cpaper_asservice.RegisterCPaperServiceServer(s, cpaperGateway)
cpaper_asservice.RegisterCPaperServiceServer(s, cPaperGateway)

// Runs gRPC server in goroutine
go func() {
Expand Down
1 change: 1 addition & 0 deletions examples/cpaper_asservice/bin/chaincode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/hyperledger/fabric-chaincode-go/shim"

"github.com/s7techlab/cckit/examples/cpaper_asservice"
)

Expand Down
2 changes: 1 addition & 1 deletion examples/cpaper_asservice/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewCCEncrypted() (*router.Chaincode, error) {
Pre(encryption.ArgsDecrypt).
// default Context replaced with EncryptedStateContext only if key is provided in transient map
Use(encryption.EncStateContext).
// invoke response will be encrypted cause it will be placed in blocks
// invoke response will be encrypted because it will be placed in blocks
After(encryption.EncryptInvokeResponse())

return router.NewChaincode(r), nil
Expand Down
Loading

0 comments on commit bd6231a

Please sign in to comment.