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

Commit 94b5791

Browse files
authored
events unary methods (#84)
* events unary methods * extended event information in subscription chan * event mapping resolving
1 parent 97ff153 commit 94b5791

35 files changed

Lines changed: 2773 additions & 851 deletions

examples/cpaper_asservice/bin/api/mock/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import (
99
"time"
1010

1111
"github.com/grpc-ecosystem/grpc-gateway/runtime"
12+
"google.golang.org/grpc"
13+
1214
"github.com/s7techlab/cckit/examples/cpaper_asservice"
1315
cpaperservice "github.com/s7techlab/cckit/examples/cpaper_asservice/service"
1416
"github.com/s7techlab/cckit/gateway"
15-
gwmock "github.com/s7techlab/cckit/gateway/mock"
1617
"github.com/s7techlab/cckit/testing"
17-
"google.golang.org/grpc"
1818
)
1919

2020
const (
@@ -42,7 +42,7 @@ func main() {
4242
cpaperMock := testing.NewMockStub(chaincodeName, cc)
4343

4444
// Chaincode invocation service mock. For real network you can use example with hlf-sdk-go
45-
cpaperMockService := gwmock.New(testing.NewPeer().WithChannel(channelName, cpaperMock))
45+
cpaperMockService := gateway.NewChaincodeService(testing.NewPeer().WithChannel(channelName, cpaperMock))
4646

4747
// default identity for signing requests to peeer (mocked)
4848
apiIdentity, err := testing.IdentityFromFile(`MSP`, `../../../testdata/admin.pem`, ioutil.ReadFile)

examples/cpaper_asservice/chaincode_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ import (
66
"testing"
77
"time"
88

9-
. "github.com/onsi/ginkgo"
10-
. "github.com/onsi/gomega"
11-
129
"github.com/golang/protobuf/ptypes"
1310
"github.com/golang/protobuf/ptypes/empty"
1411
"github.com/hyperledger/fabric/msp"
12+
. "github.com/onsi/ginkgo"
13+
. "github.com/onsi/gomega"
14+
1515
"github.com/s7techlab/cckit/examples/cpaper_asservice"
1616
"github.com/s7techlab/cckit/examples/cpaper_asservice/schema"
1717
s "github.com/s7techlab/cckit/examples/cpaper_asservice/service"
18-
"github.com/s7techlab/cckit/extensions/encryption"
18+
enctest "github.com/s7techlab/cckit/extensions/encryption/testing"
1919
"github.com/s7techlab/cckit/router"
2020
testcc "github.com/s7techlab/cckit/testing"
2121
expectcc "github.com/s7techlab/cckit/testing/expect"
@@ -39,7 +39,7 @@ var (
3939
cc, ccEnc *testcc.MockStub
4040

4141
encKey = make([]byte, 32)
42-
ccEncWrapped *encryption.MockStub
42+
ccEncWrapped *enctest.MockStub
4343

4444
issuePayload = &schema.IssueCommercialPaper{
4545
Issuer: IssuerName,
@@ -70,7 +70,7 @@ var _ = Describe(`CommercialPaper`, func() {
7070
ccEnc = testcc.NewMockStub(`cpaper_as_service_encrypted`, ccEncImpl)
7171

7272
// all queries/invokes arguments to cc will be encrypted
73-
ccEncWrapped = encryption.NewMockStub(ccEnc, encKey)
73+
ccEncWrapped = enctest.NewMockStub(ccEnc, encKey)
7474

7575
identity, err = testcc.IdentityFromFile(MspName, `./testdata/admin.pem`, ioutil.ReadFile)
7676
Expect(err).NotTo(HaveOccurred())

examples/cpaper_asservice/service/service.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/golang/protobuf/ptypes/empty"
77
"github.com/pkg/errors"
8+
89
"github.com/s7techlab/cckit/examples/cpaper_asservice/schema"
910
"github.com/s7techlab/cckit/router"
1011
"github.com/s7techlab/cckit/state"
@@ -18,26 +19,32 @@ func New() *CPaperImpl {
1819
return &CPaperImpl{}
1920
}
2021

21-
// State with chaincode mappings
22-
func State(ctx router.Context) m.MappedState {
23-
return m.WrapState(ctx.State(), m.StateMappings{}.
22+
var (
23+
StateMappings = m.StateMappings{}.
2424
// Create mapping for Commercial Paper entity
2525
Add(&schema.CommercialPaper{},
2626
m.PKeySchema(&schema.CommercialPaperId{}), // Key namespace will be <"CommercialPaper", Issuer, PaperNumber>
2727
m.List(&schema.CommercialPaperList{}), // Structure of result for List method
2828
m.UniqKey("ExternalId"), // External Id is unique
29-
))
30-
}
29+
)
3130

32-
// Event with chaincode mappings
33-
func Event(ctx router.Context) state.Event {
34-
return m.WrapEvent(ctx.Event(), m.EventMappings{}.
31+
EventMappings = m.EventMappings{}.
3532
// Event name will be "IssueCommercialPaper", payload - same as issue payload
3633
Add(&schema.IssueCommercialPaper{}).
3734
// Event name will be "BuyCommercialPaper"
3835
Add(&schema.BuyCommercialPaper{}).
3936
// Event name will be "RedeemCommercialPaper"
40-
Add(&schema.RedeemCommercialPaper{}))
37+
Add(&schema.RedeemCommercialPaper{})
38+
)
39+
40+
// State with chaincode mappings
41+
func State(ctx router.Context) m.MappedState {
42+
return m.WrapState(ctx.State(), StateMappings)
43+
}
44+
45+
// Event with chaincode mappings
46+
func Event(ctx router.Context) state.Event {
47+
return m.WrapEvent(ctx.Event(), EventMappings)
4148
}
4249

4350
func (cc *CPaperImpl) List(ctx router.Context, in *empty.Empty) (*schema.CommercialPaperList, error) {

extensions/encryption/encryption_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import (
55
"encoding/base64"
66
"testing"
77

8+
"github.com/hyperledger/fabric-protos-go/peer"
89
. "github.com/onsi/ginkgo"
910
. "github.com/onsi/gomega"
1011

11-
"github.com/hyperledger/fabric-protos-go/peer"
1212
"github.com/s7techlab/cckit/examples/payment"
1313
"github.com/s7techlab/cckit/examples/payment/schema"
1414
"github.com/s7techlab/cckit/extensions/encryption"
1515
"github.com/s7techlab/cckit/extensions/encryption/testdata"
16+
enctest "github.com/s7techlab/cckit/extensions/encryption/testing"
1617
identitytestdata "github.com/s7techlab/cckit/identity/testdata"
1718
"github.com/s7techlab/cckit/state"
1819
"github.com/s7techlab/cckit/state/mapping"
@@ -35,7 +36,7 @@ var (
3536
encryptPaymentCCWithEncStateContext *testcc.MockStub
3637
externalCC *testcc.MockStub
3738

38-
encCCInvoker *encryption.MockStub
39+
encCCInvoker *enctest.MockStub
3940

4041
pType = `SALE`
4142
encKey []byte
@@ -80,7 +81,7 @@ var _ = Describe(`Router`, func() {
8081
`paymentsEncWithContext`,
8182
payment.NewEncryptedPaymentCCWithEncStateContext())
8283

83-
encCCInvoker = encryption.NewMockStub(encryptPaymentCCWithEncStateContext, encKey)
84+
encCCInvoker = enctest.NewMockStub(encryptPaymentCCWithEncStateContext, encKey)
8485
encCCInvoker.DecryptInvokeResponse = true
8586

8687
externalCC = testcc.NewMockStub(`external`,
@@ -180,7 +181,7 @@ var _ = Describe(`Router`, func() {
180181
It("Allow to get encrypted payments by type as unencrypted values", func() {
181182
// MockInvoke sets key in transient map and encrypt input arguments
182183
payments := expectcc.PayloadIs(
183-
encryption.MockInvoke(encryptOnDemandPaymentCC, encKey, `paymentList`, pType),
184+
enctest.MockInvoke(encryptOnDemandPaymentCC, encKey, `paymentList`, pType),
184185
&schema.PaymentList{}).(*schema.PaymentList)
185186

186187
Expect(payments.Items).To(HaveLen(1))
@@ -218,7 +219,7 @@ var _ = Describe(`Router`, func() {
218219

219220
It("Allow to create payment providing key in encryptPaymentCC ", func() {
220221
// encode all arguments
221-
expectcc.ResponseOk(encryption.MockInvoke(encryptPaymentCC, encKey, `paymentCreate`, pType, pID3, pAmount3))
222+
expectcc.ResponseOk(enctest.MockInvoke(encryptPaymentCC, encKey, `paymentCreate`, pType, pID3, pAmount3))
222223
})
223224
})
224225

@@ -252,7 +253,7 @@ var _ = Describe(`Router`, func() {
252253
}),
253254
})))
254255

255-
closer()
256+
_ = closer()
256257
close(done)
257258
}, 0.2)
258259

extensions/encryption/must.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package encryption
2+
3+
import (
4+
"github.com/hyperledger/fabric-protos-go/peer"
5+
)
6+
7+
// MustEncryptEvent helper for EncryptEvent. Panics in case of error.
8+
func MustEncryptEvent(encKey []byte, event *peer.ChaincodeEvent) *peer.ChaincodeEvent {
9+
encrypted, err := EncryptEvent(encKey, event)
10+
if err != nil {
11+
panic(err)
12+
}
13+
return encrypted
14+
}
15+
16+
// MustDecryptEvent helper for DecryptEvent. Panics in case of error.
17+
func MustDecryptEvent(encKey []byte, event *peer.ChaincodeEvent) *peer.ChaincodeEvent {
18+
decrypted, err := DecryptEvent(encKey, event)
19+
if err != nil {
20+
panic(err)
21+
}
22+
23+
return decrypted
24+
}
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
1-
package encryption
1+
package testing
22

33
import (
44
"fmt"
55

66
"github.com/hyperledger/fabric-protos-go/peer"
7+
8+
"github.com/s7techlab/cckit/extensions/encryption"
79
"github.com/s7techlab/cckit/response"
8-
"github.com/s7techlab/cckit/testing"
10+
testcc "github.com/s7techlab/cckit/testing"
911
)
1012

1113
// MockInvoke helper for invoking MockStub with transient key and encrypted args
12-
func MockInvoke(cc *testing.MockStub, encKey []byte, args ...interface{}) peer.Response {
13-
encArgs, err := EncryptArgs(encKey, args...)
14+
func MockInvoke(cc *testcc.MockStub, encKey []byte, args ...interface{}) peer.Response {
15+
encArgs, err := encryption.EncryptArgs(encKey, args...)
1416
if err != nil {
1517
return response.Error(`unable to encrypt input args`)
1618
}
17-
return cc.AddTransient(TransientMapWithKey(encKey)).InvokeBytes(encArgs...)
19+
return cc.AddTransient(encryption.TransientMapWithKey(encKey)).InvokeBytes(encArgs...)
1820
}
1921

2022
// MockQuery helper for querying MockStub with transient key and encrypted args
21-
func MockQuery(cc *testing.MockStub, encKey []byte, args ...interface{}) peer.Response {
22-
encArgs, err := EncryptArgs(encKey, args...)
23+
func MockQuery(cc *testcc.MockStub, encKey []byte, args ...interface{}) peer.Response {
24+
encArgs, err := encryption.EncryptArgs(encKey, args...)
2325
if err != nil {
2426
return response.Error(`unable to encrypt input args`)
2527
}
26-
return cc.AddTransient(TransientMapWithKey(encKey)).QueryBytes(encArgs...)
28+
return cc.AddTransient(encryption.TransientMapWithKey(encKey)).QueryBytes(encArgs...)
2729
}
2830

2931
// MockStub wrapper for querying and invoking encrypted chaincode
3032
type MockStub struct {
31-
MockStub *testing.MockStub
33+
MockStub *testcc.MockStub
3234
//EncKey key for encrypt data before query/invoke
3335
EncKey []byte
3436

@@ -37,7 +39,7 @@ type MockStub struct {
3739
}
3840

3941
// NewMockStub creates wrapper for querying and invoking encrypted chaincode
40-
func NewMockStub(mockStub *testing.MockStub, encKey []byte) *MockStub {
42+
func NewMockStub(mockStub *testcc.MockStub, encKey []byte) *MockStub {
4143
return &MockStub{MockStub: mockStub, EncKey: encKey}
4244
}
4345

@@ -52,7 +54,7 @@ func (s *MockStub) Invoke(args ...interface{}) (response peer.Response) {
5254
//after receiving response we can decrypt received peer response
5355
// actual only for invoke, query responses are not encrypted
5456
if s.DecryptInvokeResponse && len(response.Payload) > 0 && string(response.Payload) != `null` {
55-
if decrypted, err = Decrypt(s.EncKey, response.Payload); err != nil {
57+
if decrypted, err = encryption.Decrypt(s.EncKey, response.Payload); err != nil {
5658
panic(fmt.Sprintf(
5759
`decrypt mock invoke error with payload %s (%d): %s`,
5860
string(response.Payload), len(response.Payload), err))
@@ -68,11 +70,11 @@ func (s *MockStub) Query(args ...interface{}) peer.Response {
6870
}
6971

7072
func (s *MockStub) Init(args ...interface{}) peer.Response {
71-
encArgs, err := EncryptArgs(s.EncKey, args...)
73+
encArgs, err := encryption.EncryptArgs(s.EncKey, args...)
7274
if err != nil {
7375
return response.Error(`unable to encrypt input args`)
7476
}
75-
return s.MockStub.AddTransient(TransientMapWithKey(s.EncKey)).InitBytes(encArgs...)
77+
return s.MockStub.AddTransient(encryption.TransientMapWithKey(s.EncKey)).InitBytes(encArgs...)
7678
}
7779

7880
func (s *MockStub) From(args ...interface{}) *MockStub {
@@ -81,20 +83,5 @@ func (s *MockStub) From(args ...interface{}) *MockStub {
8183
}
8284

8385
func (s *MockStub) LastEvent() *peer.ChaincodeEvent {
84-
return MustDecryptEvent(s.EncKey, s.MockStub.ChaincodeEvent)
85-
}
86-
87-
// MustEncryptEvent helper for EncryptEvent. Panics in case of error.
88-
func MustEncryptEvent(encKey []byte, event *peer.ChaincodeEvent) *peer.ChaincodeEvent {
89-
encrypted, err := EncryptEvent(encKey, event)
90-
testing.PanicIfError(err)
91-
return encrypted
92-
}
93-
94-
// MustDecryptEvent helper for DecryptEvent. Panics in case of error.
95-
func MustDecryptEvent(encKey []byte, event *peer.ChaincodeEvent) *peer.ChaincodeEvent {
96-
decrypted, err := DecryptEvent(encKey, event)
97-
testing.PanicIfError(err)
98-
99-
return decrypted
86+
return encryption.MustDecryptEvent(s.EncKey, s.MockStub.ChaincodeEvent)
10087
}

gateway/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.: generate
22

33
generate:
4-
@echo "chaincode invoke service"
4+
@echo "chaincode gateway service"
55
@protoc -I=. \
66
-I=../../ \
77
-I=../vendor \

0 commit comments

Comments
 (0)