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

Commit

Permalink
Merge branch 'master' into state-multi-fkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
vitiko committed Dec 9, 2019
2 parents ee7403a + 067d186 commit 03a53fc
Show file tree
Hide file tree
Showing 20 changed files with 385 additions and 139 deletions.
110 changes: 72 additions & 38 deletions examples/cpaper_asservice/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/s7techlab/cckit/testing/gomega"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/empty"
"github.com/s7techlab/cckit/examples/cpaper_asservice/schema"
"github.com/s7techlab/cckit/examples/cpaper_asservice/service"
"github.com/s7techlab/cckit/router"
"github.com/s7techlab/cckit/extensions/owner"
idtestdata "github.com/s7techlab/cckit/identity/testdata"
"github.com/s7techlab/cckit/state"
testcc "github.com/s7techlab/cckit/testing"
"github.com/s7techlab/cckit/testing/expect"
)

func TestCommercialPaperService(t *testing.T) {
Expand All @@ -23,8 +25,17 @@ func TestCommercialPaperService(t *testing.T) {
}

var (
CPaperSvc = service.New()
CPaper = service.New()

// service testing util
hdl, ctx = testcc.NewTxHandler(`Commercial paper`)

ids = idtestdata.MustIdentities(idtestdata.Certificates, idtestdata.DefaultMSP)
// actors
Issuer = ids[0]
Buyer = ids[1]

// payloads
id = &schema.CommercialPaperId{
Issuer: "SomeIssuer",
PaperNumber: "0001",
Expand Down Expand Up @@ -65,76 +76,99 @@ var (
MaturityDate: issue.MaturityDate,
ExternalId: issue.ExternalId,
}

cc = testcc.NewCCService(`Commercial paper`)
)

var _ = Describe(`Commercial paper service`, func() {

It("Allow to init", func() {
hdl.From(Issuer).Init(func() (interface{}, error) {
return owner.SetFromCreator(ctx)
}).Expect().HasError(nil)
})

It("Allow issuer to issue new commercial paper", func() {
expect.SvcResponse(cc.Exec(func(ctx router.Context) (interface{}, error) {
return CPaperSvc.Issue(ctx, issue)
})).Is(cpaperInState)
hdl.From(Issuer).Tx(func() {
hdl.SvcExpect(CPaper.Issue(ctx, issue)).Is(cpaperInState)
})
})

It("Disallow issuer to issue same commercial paper", func() {
hdl.From(Issuer).Tx(func() {
_, err := CPaper.Issue(ctx, issue)

Expect(err).To(ErrorIs(state.ErrKeyAlreadyExists))
})
})

It("Allow issuer to get commercial paper by composite primary key", func() {
expect.SvcResponse(cc.Exec(func(ctx router.Context) (interface{}, error) {
return CPaperSvc.Get(ctx, id)
})).Is(cpaperInState)
hdl.Tx(func() {
hdl.SvcExpect(CPaper.Get(ctx, id)).Is(cpaperInState)
})
})

It("Allow issuer to get commercial paper by unique key", func() {
expect.SvcResponse(cc.Exec(func(ctx router.Context) (interface{}, error) {
return CPaperSvc.GetByExternalId(ctx, &schema.ExternalId{
hdl.Tx(func() {
res, err := CPaper.GetByExternalId(ctx, &schema.ExternalId{
Id: issue.ExternalId,
})
})).Is(cpaperInState)

Expect(err).NotTo(HaveOccurred())
Expect(res).To(StringerEqual(cpaperInState))
})

})

It("Allow issuer to get a list of commercial papers", func() {
expect.SvcResponse(cc.Exec(func(ctx router.Context) (interface{}, error) {
return CPaperSvc.List(ctx, &empty.Empty{})
})).Is(&schema.CommercialPaperList{
Items: []*schema.CommercialPaper{cpaperInState},
hdl.Tx(func() {
res, err := CPaper.List(ctx, &empty.Empty{})

Expect(err).NotTo(HaveOccurred())
Expect(res.Items).To(HaveLen(1))
Expect(res.Items[0]).To(StringerEqual(cpaperInState))
})
})

It("Allow buyer to buy commercial paper", func() {
expect.SvcResponse(cc.Exec(func(ctx router.Context) (interface{}, error) {
return CPaperSvc.Buy(ctx, buy)
})).ProduceEvent(`BuyCommercialPaper`, buy)
hdl.From(Buyer).Tx(func() {
hdl.SvcExpect(CPaper.Buy(ctx, buy)).
// Produce Event - no error also
ProduceEvent(`BuyCommercialPaper`, buy)
})

newState := proto.Clone(cpaperInState).(*schema.CommercialPaper)
newState.Owner = buy.NewOwner
newState.State = schema.CommercialPaper_TRADING

expect.SvcResponse(cc.Exec(func(ctx router.Context) (interface{}, error) {
return CPaperSvc.Get(ctx, id)
})).Is(newState)
hdl.Tx(func() {
hdl.SvcExpect(CPaper.Get(ctx, id)).Is(newState)
})
})

It("Allow buyer to redeem commercial paper", func() {
expect.SvcResponse(cc.Exec(func(ctx router.Context) (interface{}, error) {
return CPaperSvc.Redeem(ctx, redeem)
})).ProduceEvent(`RedeemCommercialPaper`, redeem)
// Invoke example
hdl.Invoke(func() (interface{}, error) {
return CPaper.Redeem(ctx, redeem)
}).Expect().ProduceEvent(`RedeemCommercialPaper`, redeem)

newState := proto.Clone(cpaperInState).(*schema.CommercialPaper)
newState.State = schema.CommercialPaper_REDEEMED

expect.SvcResponse(cc.Exec(func(ctx router.Context) (interface{}, error) {
return CPaperSvc.Get(ctx, id)
})).Is(newState)
hdl.Invoke(func() (interface{}, error) {
return CPaper.Get(ctx, id)
}).Expect().Is(newState)
})

It("Allow issuer to delete commercial paper", func() {
expect.SvcResponse(cc.Exec(func(ctx router.Context) (interface{}, error) {
return CPaperSvc.Delete(ctx, id)
}))

expect.SvcResponse(cc.Exec(func(ctx router.Context) (interface{}, error) {
return CPaperSvc.List(ctx, &empty.Empty{})
})).Is(&schema.CommercialPaperList{
Items: []*schema.CommercialPaper{},
hdl.From(Issuer).Tx(func() {
_, err := CPaper.Delete(ctx, id)
Expect(err).NotTo(HaveOccurred())
})

hdl.Tx(func() {
res, err := CPaper.List(ctx, &empty.Empty{})

Expect(err).NotTo(HaveOccurred())
Expect(res.Items).To(HaveLen(0))
})
})
})
21 changes: 7 additions & 14 deletions examples/cpaper_extended/chaincode_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package cpaper_extended_test

import (
"io/ioutil"
"testing"
"time"

"github.com/s7techlab/cckit/examples/cpaper_extended"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/golang/protobuf/ptypes"
"github.com/hyperledger/fabric/protos/peer"
"github.com/s7techlab/cckit/examples/cpaper_extended"
"github.com/s7techlab/cckit/examples/cpaper_extended/schema"
"github.com/s7techlab/cckit/examples/cpaper_extended/testdata"
idtestdata "github.com/s7techlab/cckit/identity/testdata"
testcc "github.com/s7techlab/cckit/testing"
expectcc "github.com/s7techlab/cckit/testing/expect"

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

const (
MspName = "msp"

IssuerName = "SomeIssuer"
BuyerName = "SomeBuyer"
)
Expand All @@ -39,14 +37,9 @@ var _ = Describe(`CommercialPaper`, func() {

BeforeSuite(func() {
// Init chaincode with admin identity

adminIdentity, err := testcc.IdentityFromFile(MspName, `testdata/admin.pem`, ioutil.ReadFile)
Expect(err).NotTo(HaveOccurred())

adminIdentity := testdata.Certificates[0].MustIdentity(idtestdata.DefaultMSP)
expectcc.ResponseOk(
paperChaincode.
From(adminIdentity).
Init())
paperChaincode.From(adminIdentity).Init())
})

Describe("Commercial Paper lifecycle", func() {
Expand Down
12 changes: 12 additions & 0 deletions examples/cpaper_extended/testdata/testdata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package testdata

import (
idtestdata "github.com/s7techlab/cckit/identity/testdata"
)

var (
Certificates = idtestdata.Certs{{
CertFilename: `admin.pem`, PKeyFilename: `admin.key.pem`,
}}.
UseReadFile(idtestdata.ReadLocal())
)
14 changes: 8 additions & 6 deletions examples/erc20/erc20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ import (
"testing"

"github.com/s7techlab/cckit/examples/erc20"
identitytestdata "github.com/s7techlab/cckit/identity/testdata"
idtestdata "github.com/s7techlab/cckit/identity/testdata"
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) {
func TestErc20(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Cars Suite")
RunSpecs(t, "ERC-20 Suite")
}

var (
ids = idtestdata.MustIdentities(idtestdata.Certificates, idtestdata.DefaultMSP)

// load actor certificates
TokenOwner = identitytestdata.Identities[0]
AccountHolder1 = identitytestdata.Identities[1]
Spender1 = identitytestdata.Identities[2]
TokenOwner = ids[0]
AccountHolder1 = ids[1]
Spender1 = ids[2]
)
var _ = Describe(`ERC-20`, func() {

Expand Down
8 changes: 6 additions & 2 deletions gateway/event_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ func (s *ChaincodeEventServerStream) SendMsg(m interface{}) (err error) {
}
}

s.events <- e
select {
case <-s.context.Done():
return s.context.Err()
case s.events <- e:
}

return nil
}

Expand All @@ -82,7 +87,6 @@ func (s *ChaincodeEventServerStream) Events() <-chan *peer.ChaincodeEvent {

func (s *ChaincodeEventServerStream) Close() {
s.once.Do(func() {
close(s.events)
s.ready = false
})
}
3 changes: 0 additions & 3 deletions gateway/service/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package service

import (
"context"
"log"

"github.com/hyperledger/fabric/protos/peer"
"github.com/pkg/errors"
Expand Down Expand Up @@ -92,8 +91,6 @@ func (cs *ChaincodeService) Events(in *ChaincodeLocator, stream Chaincode_Events

for {
e, ok := <-events.Events()

log.Println(`event received`, e.EventName)
if !ok {
return nil
}
Expand Down
17 changes: 11 additions & 6 deletions gateway/service/mock/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,18 @@ func (cs *ChaincodeService) Events(in *service.ChaincodeLocator, stream service.
return
}
events := mockStub.EventSubscription()
ctx := stream.Context()
for {
e, ok := <-events
if !ok {
return nil
}
if err = stream.Send(e); err != nil {
return err
select {
case <-ctx.Done():
return ctx.Err()
case e, ok := <-events:
if !ok {
return nil
}
if err = stream.Send(e); err != nil {
return err
}
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions gateway/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
cpservice "github.com/s7techlab/cckit/examples/cpaper_asservice/service"
"github.com/s7techlab/cckit/gateway/service"
"github.com/s7techlab/cckit/gateway/service/mock"
"github.com/s7techlab/cckit/identity/testdata"
idtestdata "github.com/s7techlab/cckit/identity/testdata"
testcc "github.com/s7techlab/cckit/testing"
)

Expand All @@ -29,7 +29,10 @@ const (
var (
cPaperService *mock.ChaincodeService
cPaperGateway *cpservice.CPaperGateway
ctx = service.ContextWithSigner(context.Background(), testdata.Identities[0])

ctx = service.ContextWithSigner(
context.Background(),
idtestdata.Certificates[0].MustIdentity(idtestdata.DefaultMSP))
)

var _ = Describe(`Service`, func() {
Expand Down
12 changes: 4 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
module github.com/s7techlab/cckit

go 1.12
go 1.13

require (
github.com/Knetic/govaluate v3.0.0+incompatible // indirect
github.com/fsouza/go-dockerclient v1.4.0 // indirect
github.com/gogo/protobuf v1.2.1
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/golang/mock v1.2.0 // indirect
github.com/golang/protobuf v1.3.1
github.com/grpc-ecosystem/grpc-gateway v1.9.0
github.com/hyperledger/fabric v1.4.0
github.com/hyperledger/fabric v1.4.4
github.com/hyperledger/fabric-amcl v0.0.0-20181230093703-5ccba6eab8d6 // indirect
github.com/hyperledger/fabric-lib-go v1.0.0 // indirect
github.com/miekg/pkcs11 v1.0.2 // indirect
github.com/mwitkow/go-proto-validators v0.0.0-20190212092829-1f388280e944
github.com/onsi/ginkgo v1.8.0
Expand All @@ -20,9 +18,7 @@ require (
github.com/pkg/errors v0.8.1
github.com/s7techlab/hlf-sdk-go v0.1.3
github.com/spf13/viper v1.4.0 // indirect
golang.org/x/net v0.0.0-20190522155817-f3200d17e092
github.com/sykesm/zap-logfmt v0.0.3 // indirect
google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19
google.golang.org/grpc v1.21.0
)

replace git.apache.org/thrift.git => github.com/apache/thrift v0.12.0

0 comments on commit 03a53fc

Please sign in to comment.