forked from hyperledger-archives/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
executable file
·115 lines (105 loc) · 4.16 KB
/
transaction.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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 protos
import (
"fmt"
"github.com/golang/protobuf/proto"
"github.com/openblockchain/obc-peer/openchain/util"
)
// Bytes returns this transaction as an array of bytes.
func (transaction *Transaction) Bytes() ([]byte, error) {
data, err := proto.Marshal(transaction)
if err != nil {
logger.Error(fmt.Sprintf("Error marshalling transaction: %s", err))
return nil, fmt.Errorf("Could not marshal transaction: %s", err)
}
return data, nil
}
// NewTransaction creates a new transaction. It defines the function to call,
// the chaincodeID on which the function should be called, and the arguments
// string. The arguments could be a string of JSON, but there is no strict
// requirement.
func NewTransaction(chaincodeID ChaincodeID, uuid string, function string, arguments []string) (*Transaction, error) {
data, err := proto.Marshal(&chaincodeID)
if err != nil {
return nil, fmt.Errorf("Could not marshal chaincode : %s", err)
}
transaction := new(Transaction)
transaction.ChaincodeID = data
transaction.Uuid = uuid
transaction.Timestamp = util.CreateUtcTimestamp()
/*
// Build the spec
spec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_GOLANG,
ChaincodeID: chaincodeID, ChaincodeInput: &pb.ChaincodeInput{Function: function, Args: arguments}}
// Build the ChaincodeInvocationSpec message
invocation := &pb.ChaincodeInvocationSpec{ChaincodeSpec: spec}
data, err := proto.Marshal(invocation)
if err != nil {
return nil, fmt.Errorf("Could not marshal payload for chaincode invocation: %s", err)
}
transaction.Payload = data
*/
return transaction, nil
}
// NewChaincodeDeployTransaction is used to deploy chaincode.
func NewChaincodeDeployTransaction(chaincodeDeploymentSpec *ChaincodeDeploymentSpec, uuid string) (*Transaction, error) {
transaction := new(Transaction)
transaction.Type = Transaction_CHAINCODE_NEW
transaction.Uuid = uuid
transaction.Timestamp = util.CreateUtcTimestamp()
cID := chaincodeDeploymentSpec.ChaincodeSpec.GetChaincodeID()
if cID != nil {
data, err := proto.Marshal(cID)
if err != nil {
return nil, fmt.Errorf("Could not marshal chaincode : %s", err)
}
transaction.ChaincodeID = data
}
//if chaincodeDeploymentSpec.ChaincodeSpec.GetCtorMsg() != nil {
// transaction.Function = chaincodeDeploymentSpec.ChaincodeSpec.GetCtorMsg().Function
// transaction.Args = chaincodeDeploymentSpec.ChaincodeSpec.GetCtorMsg().Args
//}
data, err := proto.Marshal(chaincodeDeploymentSpec)
if err != nil {
logger.Error(fmt.Sprintf("Error mashalling payload for chaincode deployment: %s", err))
return nil, fmt.Errorf("Could not marshal payload for chaincode deployment: %s", err)
}
transaction.Payload = data
return transaction, nil
}
// NewChaincodeExecute is used to deploy chaincode.
func NewChaincodeExecute(chaincodeInvocationSpec *ChaincodeInvocationSpec, uuid string, typ Transaction_Type) (*Transaction, error) {
transaction := new(Transaction)
transaction.Type = typ
transaction.Uuid = uuid
transaction.Timestamp = util.CreateUtcTimestamp()
cID := chaincodeInvocationSpec.ChaincodeSpec.GetChaincodeID()
if cID != nil {
data, err := proto.Marshal(cID)
if err != nil {
return nil, fmt.Errorf("Could not marshal chaincode : %s", err)
}
transaction.ChaincodeID = data
}
data, err := proto.Marshal(chaincodeInvocationSpec)
if err != nil {
return nil, fmt.Errorf("Could not marshal payload for chaincode invocation: %s", err)
}
transaction.Payload = data
return transaction, nil
}