1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | package shim import ( "github.com/golang/protobuf/ptypes/timestamp" "github.com/hyperledger/fabric/core/chaincode/shim/crypto/attr" ) type CustomMockStub struct { stub *MockStub CertAttributes map[string][]byte } // Constructor to initialise the CustomMockStub func NewCustomMockStub(name string, cc Chaincode, attributes map[string][]byte) *CustomMockStub { s := new(CustomMockStub) s.stub = NewMockStub(name, cc) s.CertAttributes = attributes return s } func (mock *CustomMockStub) ReadCertAttribute(attributeName string) ([]byte, error) { return mock.CertAttributes[attributeName], nil } func (mock *CustomMockStub) GetState(key string) ([]byte, error) { return mock.stub.GetState(key) } func (mock *CustomMockStub) GetTxID() string { return mock.stub.GetTxID() } func (mock *CustomMockStub) MockInit(uuid string, function string, args []string) ([]byte, error) { mock.stub.args = getBytes(function, args) mock.MockTransactionStart(uuid) bytes, err := mock.stub.cc.Init(mock, function, args) mock.MockTransactionEnd(uuid) return bytes, err } func (mock *CustomMockStub) MockInvoke(uuid string, function string, args []string) ([]byte, error) { mock.stub.args = getBytes(function, args) mock.MockTransactionStart(uuid) bytes, err := mock.stub.cc.Invoke(mock, function, args) mock.MockTransactionEnd(uuid) return bytes, err } func (mock *CustomMockStub) MockQuery(function string, args []string) ([]byte, error) { mock.stub.args = getBytes(function, args) // no transaction needed for queries bytes, err := mock.stub.cc.Query(mock, function, args) return bytes, err } func (mock *CustomMockStub) PutState(key string, value []byte) error { return mock.stub.PutState(key, value) } func (mock *CustomMockStub) MockTransactionStart(txid string) { mock.stub.MockTransactionStart(txid) } func (mock *CustomMockStub) MockTransactionEnd(uuid string) { mock.stub.MockTransactionEnd(uuid) } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |