chaincode.go 2.77 KB
Newer Older
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
/*
 Copyright Digital Asset Holdings, LLC 2016 All Rights Reserved.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

package noop

import (
	"errors"

	"github.com/golang/protobuf/proto"
	"github.com/hyperledger/fabric/core/chaincode/shim"
	"github.com/hyperledger/fabric/protos"
)

var logger = shim.NewLogger("noop")

type ledgerHandler interface {
Gabor Hosszu's avatar
Gabor Hosszu committed
30
	GetTransactionByID(txID string) (*protos.Transaction, error)
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
}

// SystemChaincode is type representing the chaincode
// In general, one should not use vars in memory that can hold state
// across invokes but this is used JUST for MOCKING
type SystemChaincode struct {
	mockLedgerH ledgerHandler
}

func (t *SystemChaincode) getLedger() ledgerHandler {
	if t.mockLedgerH == nil {
		panic("Chaincode is unable to get the ledger.")
	} else {
		return t.mockLedgerH
	}
}

// Init initailizes the system chaincode
49
func (t *SystemChaincode) Init(stub shim.ChaincodeStubInterface) ([]byte, error) {
50
51
52
53
54
55
	logger.SetLevel(shim.LogDebug)
	logger.Debugf("NOOP INIT")
	return nil, nil
}

// Invoke runs an invocation on the system chaincode
56
57
func (t *SystemChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
	args := stub.GetStringArgs()
Gabor Hosszu's avatar
Gabor Hosszu committed
58
59
	if len(args) != 1 {
		return nil, errors.New("Noop execute operation must have one single argument.")
60
	}
Gabor Hosszu's avatar
Gabor Hosszu committed
61
62
	logger.Infof("Executing noop invoke.")
	return nil, nil
63
64
65
}

// Query callback representing the query of a chaincode
66
67
func (t *SystemChaincode) Query(stub shim.ChaincodeStubInterface) ([]byte, error) {
	function, args := stub.GetFunctionAndParameters()
68
69
70
71
72
73
74
75
76
	switch function {
	case "getTran":
		if len(args) < 1 {
			return nil, errors.New("getTran operation must include a single argument, the TX hash hex")
		}
		logger.Infof("Executing NOOP QUERY")
		logger.Infof("--> %x", args[0])

		var txHashHex = args[0]
Gabor Hosszu's avatar
Gabor Hosszu committed
77
		var tx, txerr = t.getLedger().GetTransactionByID(txHashHex)
78
79
80
81
82
83
84
85
		if nil != txerr || nil == tx {
			return nil, txerr
		}
		newCCIS := &protos.ChaincodeInvocationSpec{}
		var merr = proto.Unmarshal(tx.Payload, newCCIS)
		if nil != merr {
			return nil, merr
		}
Gabor Hosszu's avatar
Gabor Hosszu committed
86
87
88
89
90
		if len(newCCIS.ChaincodeSpec.CtorMsg.Args) < 1 {
			return nil, errors.New("The requested transaction is malformed.")
		}
		var dataInByteForm = newCCIS.ChaincodeSpec.CtorMsg.Args[0]
		return dataInByteForm, nil
91
92
93
94
	default:
		return nil, errors.New("Unsupported operation")
	}
}