forked from oasisprotocol/oasis-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
297 lines (250 loc) · 9.11 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package transaction
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"reflect"
"sync"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/common/crypto/hash"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
"github.com/oasisprotocol/oasis-core/go/common/errors"
"github.com/oasisprotocol/oasis-core/go/common/prettyprint"
)
// moduleName is the module name used for error definitions.
const moduleName = "consensus/transaction"
var (
// ErrInvalidNonce is the error returned when a nonce is invalid.
ErrInvalidNonce = errors.New(moduleName, 1, "transaction: invalid nonce")
// ErrUpgradePending is the error returned when an upgrade is pending and the transaction thus
// cannot be processed right now. The submitter should retry the transaction in this case.
ErrUpgradePending = errors.New(moduleName, 4, "transaction: upgrade pending")
// SignatureContext is the context used for signing transactions.
SignatureContext = signature.NewContext("oasis-core/consensus: tx", signature.WithChainSeparation())
registeredMethods sync.Map
_ prettyprint.PrettyPrinter = (*Transaction)(nil)
_ prettyprint.PrettyPrinter = (*SignedTransaction)(nil)
)
// Transaction is an unsigned consensus transaction.
type Transaction struct {
// Nonce is a nonce to prevent replay.
Nonce uint64 `json:"nonce"`
// Fee is an optional fee that the sender commits to pay to execute this
// transaction.
Fee *Fee `json:"fee,omitempty"`
// Method is the method that should be called.
Method MethodName `json:"method"`
// Body is the method call body.
Body cbor.RawMessage `json:"body,omitempty"`
}
// PrettyPrintBody writes a pretty-printed representation of transaction's body
// to the given writer.
func (t Transaction) PrettyPrintBody(ctx context.Context, prefix string, w io.Writer) {
bodyType := t.Method.BodyType()
if bodyType == nil {
fmt.Fprintf(w, "%s<unknown method body: %s>\n", prefix, base64.StdEncoding.EncodeToString(t.Body))
return
}
// Deserialize into correct type.
v := reflect.New(reflect.TypeOf(bodyType)).Interface()
if err := cbor.Unmarshal(t.Body, v); err != nil {
fmt.Fprintf(w, "%s<error: %s>\n", prefix, err)
fmt.Fprintf(w, "%s<malformed: %s>\n", prefix, base64.StdEncoding.EncodeToString(t.Body))
return
}
// If the body type supports pretty printing, use that.
if pp, ok := v.(prettyprint.PrettyPrinter); ok {
pp.PrettyPrint(ctx, prefix, w)
return
}
// Otherwise, just serialize into JSON and display that.
data, err := json.MarshalIndent(v, prefix, " ")
if err != nil {
fmt.Fprintf(w, "%s <raw: %s>\n", prefix, base64.StdEncoding.EncodeToString(t.Body))
return
}
fmt.Fprintf(w, "%s%s\n", prefix, data)
}
// PrettyPrint writes a pretty-printed representation of the transaction to the
// given writer.
func (t Transaction) PrettyPrint(ctx context.Context, prefix string, w io.Writer) {
fmt.Fprintf(w, "%sMethod: %s\n", prefix, t.Method)
fmt.Fprintf(w, "%sBody:\n", prefix)
t.PrettyPrintBody(ctx, prefix+" ", w)
fmt.Fprintf(w, "%sNonce: %d\n", prefix, t.Nonce)
if t.Fee != nil {
fmt.Fprintf(w, "%sFee:\n", prefix)
t.Fee.PrettyPrint(ctx, prefix+" ", w)
} else {
fmt.Fprintf(w, "%sFee: none\n", prefix)
}
if genesisHash, ok := ctx.Value(prettyprint.ContextKeyGenesisHash).(hash.Hash); ok {
fmt.Println("Other info:")
fmt.Printf(" Genesis document's hash: %s\n", genesisHash)
}
}
// PrettyType returns a representation of the type that can be used for pretty printing.
func (t *Transaction) PrettyType() (interface{}, error) {
bodyType := t.Method.BodyType()
if bodyType == nil {
return nil, fmt.Errorf("unknown method body type")
}
// Deserialize into correct type.
body := reflect.New(reflect.TypeOf(bodyType)).Interface()
if err := cbor.Unmarshal(t.Body, body); err != nil {
return nil, fmt.Errorf("failed to unmarshal transaction body: %w", err)
}
// If the body type supports pretty printing, use that.
if pp, ok := body.(prettyprint.PrettyPrinter); ok {
var err error
if body, err = pp.PrettyType(); err != nil {
return nil, fmt.Errorf("failed to pretty print transaction body: %w", err)
}
}
return &PrettyTransaction{
Nonce: t.Nonce,
Fee: t.Fee,
Method: t.Method,
Body: body,
}, nil
}
// SanityCheck performs a basic sanity check on the transaction.
func (t *Transaction) SanityCheck() error {
return t.Method.SanityCheck()
}
// NewTransaction creates a new transaction.
func NewTransaction(nonce uint64, fee *Fee, method MethodName, body interface{}) *Transaction {
var rawBody []byte
if body != nil {
rawBody = cbor.Marshal(body)
}
return &Transaction{
Nonce: nonce,
Fee: fee,
Method: method,
Body: cbor.RawMessage(rawBody),
}
}
// PrettyTransaction is used for pretty-printing transactions so that the actual content is
// displayed instead of the binary blob.
//
// It should only be used for pretty printing.
type PrettyTransaction struct {
Nonce uint64 `json:"nonce"`
Fee *Fee `json:"fee,omitempty"`
Method MethodName `json:"method"`
Body interface{} `json:"body,omitempty"`
}
// SignedTransaction is a signed transaction.
type SignedTransaction struct {
signature.Signed
}
// Hash returns the cryptographic hash of the encoded transaction.
func (s *SignedTransaction) Hash() hash.Hash {
return hash.NewFrom(s)
}
// PrettyPrint writes a pretty-printed representation of the type
// to the given writer.
func (s SignedTransaction) PrettyPrint(ctx context.Context, prefix string, w io.Writer) {
fmt.Fprintf(w, "%sHash: %s\n", prefix, s.Hash())
fmt.Fprintf(w, "%sSigner: %s\n", prefix, s.Signature.PublicKey)
fmt.Fprintf(w, "%s (signature: %s)\n", prefix, s.Signature.Signature)
// Check if signature is valid.
if !s.Signature.Verify(SignatureContext, s.Blob) {
fmt.Fprintf(w, "%s [INVALID SIGNATURE]\n", prefix)
}
// Display the blob even if signature verification failed as it may
// be useful to look into it regardless.
var tx Transaction
fmt.Fprintf(w, "%sContent:\n", prefix)
if err := cbor.Unmarshal(s.Blob, &tx); err != nil {
fmt.Fprintf(w, "%s <error: %s>\n", prefix, err)
fmt.Fprintf(w, "%s <malformed: %s>\n", prefix, base64.StdEncoding.EncodeToString(s.Blob))
return
}
tx.PrettyPrint(ctx, prefix+" ", w)
}
// PrettyType returns a representation of the type that can be used for pretty printing.
func (s SignedTransaction) PrettyType() (interface{}, error) {
var tx Transaction
if err := cbor.Unmarshal(s.Blob, &tx); err != nil {
return nil, fmt.Errorf("malformed signed blob: %w", err)
}
return signature.NewPrettySigned(s.Signed, tx)
}
// Open first verifies the blob signature and then unmarshals the blob.
func (s *SignedTransaction) Open(tx *Transaction) error { // nolint: interfacer
return s.Signed.Open(SignatureContext, tx)
}
// Sign signs a transaction.
func Sign(signer signature.Signer, tx *Transaction) (*SignedTransaction, error) {
signed, err := signature.SignSigned(signer, SignatureContext, tx)
if err != nil {
return nil, err
}
return &SignedTransaction{Signed: *signed}, nil
}
// MethodSeparator is the separator used to separate backend name from method name.
const MethodSeparator = "."
// MethodPriority is the method handling priority.
type MethodPriority uint8
const (
// MethodPriorityNormal is the normal method priority.
MethodPriorityNormal = 0
// MethodPriorityCritical is the priority for methods critical to the protocol operation.
MethodPriorityCritical = 255
)
// MethodMetadata is the method metadata.
type MethodMetadata struct {
Priority MethodPriority
}
// MethodMetadataProvider is the method metadata provider interface that can be implemented by
// method body types to provide additional method metadata.
type MethodMetadataProvider interface {
// MethodMetadata returns the method metadata.
MethodMetadata() MethodMetadata
}
// MethodName is a method name.
type MethodName string
// SanityCheck performs a basic sanity check on the method name.
func (m MethodName) SanityCheck() error {
if len(m) == 0 {
return fmt.Errorf("transaction: empty method")
}
return nil
}
// BodyType returns the registered body type associated with this method.
func (m MethodName) BodyType() interface{} {
bodyType, _ := registeredMethods.Load(string(m))
return bodyType
}
// Metadata returns the method metadata.
func (m MethodName) Metadata() MethodMetadata {
mp, ok := m.BodyType().(MethodMetadataProvider)
if !ok {
// Return defaults.
return MethodMetadata{
Priority: MethodPriorityNormal,
}
}
return mp.MethodMetadata()
}
// IsCritical returns true if the method is critical for the operation of the protocol.
func (m MethodName) IsCritical() bool {
return m.Metadata().Priority == MethodPriorityCritical
}
// NewMethodName creates a new method name.
//
// Module and method pair must be unique. If they are not, this method
// will panic.
func NewMethodName(module, method string, bodyType interface{}) MethodName {
// Check for duplicate method names.
name := module + MethodSeparator + method
if _, isRegistered := registeredMethods.Load(name); isRegistered {
panic(fmt.Errorf("transaction: method already registered: %s", name))
}
registeredMethods.Store(name, bodyType)
return MethodName(name)
}