forked from hyperledger-archives/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
238 lines (211 loc) · 5.37 KB
/
tx.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2017 Monax Industries Limited
//
// 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 txs
import (
"crypto/sha256"
"encoding/json"
"fmt"
"github.com/hyperledger/burrow/acm"
"github.com/hyperledger/burrow/binary"
"github.com/hyperledger/burrow/crypto"
"github.com/hyperledger/burrow/event/query"
"github.com/hyperledger/burrow/txs/payload"
)
const (
HashLength = 32
HashLengthHex = HashLength * 2
)
// Tx is the canonical object that we serialise to produce the SignBytes that we sign
type Tx struct {
ChainID string
payload.Payload
txHash []byte
}
// Wrap the Payload in Tx required for signing and serialisation
func NewTx(payload payload.Payload) *Tx {
return &Tx{
Payload: payload,
}
}
// Enclose this Tx in an Envelope to be signed
func (tx *Tx) Enclose() *Envelope {
return &Envelope{
Tx: tx,
}
}
// Encloses in Envelope and signs envelope
func (tx *Tx) Sign(signingAccounts ...acm.AddressableSigner) (*Envelope, error) {
env := tx.Enclose()
err := env.Sign(signingAccounts...)
if err != nil {
return nil, err
}
tx.Rehash()
return env, nil
}
// Generate SignBytes, panicking on any failure
func (tx *Tx) MustSignBytes() []byte {
bs, err := tx.SignBytes()
if err != nil {
panic(err)
}
return bs
}
// Produces the canonical SignBytes (the Tx message that will be signed) for a Tx
func (tx *Tx) SignBytes() ([]byte, error) {
bs, err := json.Marshal(tx)
if err != nil {
return nil, fmt.Errorf("could not generate canonical SignBytes for Payload %v: %v", tx.Payload, err)
}
return bs, nil
}
// Serialisation intermediate for switching on type
type wrapper struct {
ChainID string
Type payload.Type
Payload json.RawMessage
}
func (tx *Tx) MarshalJSON() ([]byte, error) {
bs, err := json.Marshal(tx.Payload)
if err != nil {
return nil, err
}
return json.Marshal(wrapper{
ChainID: tx.ChainID,
Type: tx.Type(),
Payload: bs,
})
}
func (tx *Tx) UnmarshalJSON(data []byte) error {
w := new(wrapper)
err := json.Unmarshal(data, w)
if err != nil {
return err
}
tx.ChainID = w.ChainID
// Now we know the Type we can deserialise the Payload
tx.Payload, err = payload.New(w.Type)
return json.Unmarshal(w.Payload, tx.Payload)
}
// Protobuf support
func (tx *Tx) Marshal() ([]byte, error) {
if tx == nil {
return nil, nil
}
return tx.MarshalJSON()
}
func (tx *Tx) Unmarshal(data []byte) error {
if len(data) == 0 {
return nil
}
return tx.UnmarshalJSON(data)
}
func (tx *Tx) MarshalTo(data []byte) (int, error) {
bs, err := tx.Marshal()
if err != nil {
return 0, err
}
return copy(data, bs), nil
}
func (tx *Tx) Size() int {
bs, _ := tx.Marshal()
return len(bs)
}
func (tx *Tx) Type() payload.Type {
if tx == nil {
return payload.TypeUnknown
}
return tx.Payload.Type()
}
// Generate a Hash for this transaction based on the SignBytes. The hash is memoized over the lifetime
// of the Tx so repeated calls to Hash() are effectively free
func (tx *Tx) Hash() binary.HexBytes {
if tx == nil {
return nil
}
if tx.txHash == nil {
return tx.Rehash()
}
return tx.txHash
}
func (tx *Tx) String() string {
if tx == nil {
return "Tx{nil}"
}
return fmt.Sprintf("Tx{TxHash: %s; Payload: %v}", tx.Hash(), tx.Payload)
}
// Regenerate the Tx hash if it has been mutated or as called by Hash() in first instance
func (tx *Tx) Rehash() []byte {
hasher := sha256.New()
hasher.Write(tx.MustSignBytes())
tx.txHash = hasher.Sum(nil)
tx.txHash = tx.txHash[:HashLength]
return tx.txHash
}
func (tx *Tx) Tagged() query.Tagged {
return query.MergeTags(query.MustReflectTags(tx), query.MustReflectTags(tx.Payload))
}
// Generate a transaction Receipt containing the Tx hash and other information if the Tx is call.
// Returned by ABCI methods.
func (tx *Tx) GenerateReceipt() *Receipt {
receipt := &Receipt{
TxType: tx.Type(),
TxHash: tx.Hash(),
}
if callTx, ok := tx.Payload.(*payload.CallTx); ok {
receipt.CreatesContract = callTx.Address == nil
if receipt.CreatesContract {
receipt.ContractAddress = crypto.NewContractAddress(callTx.Input.Address, tx.Hash())
} else {
receipt.ContractAddress = *callTx.Address
}
}
return receipt
}
var cdc = NewAminoCodec()
func DecodeReceipt(bs []byte) (*Receipt, error) {
receipt := new(Receipt)
err := cdc.UnmarshalBinaryBare(bs, receipt)
if err != nil {
return nil, err
}
return receipt, nil
}
func (receipt *Receipt) Encode() ([]byte, error) {
return cdc.MarshalBinaryBare(receipt)
}
func EnvelopeFromAny(chainID string, p *payload.Any) *Envelope {
if p.CallTx != nil {
return Enclose(chainID, p.CallTx)
}
if p.SendTx != nil {
return Enclose(chainID, p.SendTx)
}
if p.NameTx != nil {
return Enclose(chainID, p.NameTx)
}
if p.PermsTx != nil {
return Enclose(chainID, p.PermsTx)
}
if p.GovTx != nil {
return Enclose(chainID, p.GovTx)
}
if p.ProposalTx != nil {
return Enclose(chainID, p.ProposalTx)
}
if p.BatchTx != nil {
return Enclose(chainID, p.BatchTx)
}
return nil
}