This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
tx_execution.go
240 lines (214 loc) · 6.24 KB
/
tx_execution.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
package exec
import (
"fmt"
"reflect"
"strings"
"github.com/hyperledger/burrow/binary"
"github.com/hyperledger/burrow/crypto"
"github.com/hyperledger/burrow/event"
"github.com/hyperledger/burrow/event/query"
"github.com/hyperledger/burrow/execution/errors"
"github.com/hyperledger/burrow/execution/names"
"github.com/hyperledger/burrow/permission"
"github.com/hyperledger/burrow/txs"
)
func EventStringAccountInput(addr crypto.Address) string { return fmt.Sprintf("Acc/%s/Input", addr) }
func EventStringAccountOutput(addr crypto.Address) string { return fmt.Sprintf("Acc/%s/Output", addr) }
func EventStringAccountCall(addr crypto.Address) string { return fmt.Sprintf("Acc/%s/Call", addr) }
func EventStringLogEvent(addr crypto.Address) string { return fmt.Sprintf("Log/%s", addr) }
func EventStringTxExecution(txHash []byte) string { return fmt.Sprintf("Execution/Tx/%X", txHash) }
func EventStringGovernAccount(addr *crypto.Address) string { return fmt.Sprintf("Govern/Acc/%v", addr) }
func NewTxExecution(txEnv *txs.Envelope) *TxExecution {
return &TxExecution{
TxHeader: &TxHeader{
TxHash: txEnv.Tx.Hash(),
TxType: txEnv.Tx.Type(),
},
Envelope: txEnv,
Receipt: txEnv.Tx.GenerateReceipt(),
}
}
func (txe *TxExecution) StreamEvents() []*StreamEvent {
var ses []*StreamEvent
ses = append(ses,
&StreamEvent{
BeginTx: &BeginTx{
TxHeader: txe.TxHeader,
Exception: txe.Exception,
Result: txe.Result,
},
},
&StreamEvent{
Envelope: txe.Envelope,
},
)
for _, ev := range txe.Events {
ses = append(ses, &StreamEvent{
Event: ev,
})
}
for _, txeNested := range txe.TxExecutions {
ses = append(ses, txeNested.StreamEvents()...)
}
return append(ses, &StreamEvent{
EndTx: &EndTx{
TxHash: txe.TxHash,
},
})
}
func (*TxExecution) EventType() EventType {
return TypeTxExecution
}
func (txe *TxExecution) GetTxHash() binary.HexBytes {
if txe == nil || txe.TxHeader == nil {
return nil
}
return txe.TxHeader.TxHash
}
func (txe *TxExecution) Header(eventType EventType, eventID string, exception *errors.Exception) *Header {
return &Header{
TxType: txe.GetTxType(),
TxHash: txe.GetTxHash(),
Height: txe.GetHeight(),
EventType: eventType,
EventID: eventID,
Exception: exception,
}
}
// Emit events
func (txe *TxExecution) Input(address crypto.Address, exception *errors.Exception) {
txe.Append(&Event{
Header: txe.Header(TypeAccountInput, EventStringAccountInput(address), exception),
Input: &InputEvent{
Address: address,
},
})
}
func (txe *TxExecution) Output(address crypto.Address, exception *errors.Exception) {
txe.Append(&Event{
Header: txe.Header(TypeAccountOutput, EventStringAccountOutput(address), exception),
Output: &OutputEvent{
Address: address,
},
})
}
func (txe *TxExecution) Log(log *LogEvent) error {
txe.Append(&Event{
Header: txe.Header(TypeLog, EventStringLogEvent(log.Address), nil),
Log: log,
})
return nil
}
func (txe *TxExecution) Call(call *CallEvent, exception *errors.Exception) error {
txe.Append(&Event{
Header: txe.Header(TypeCall, EventStringAccountCall(call.CallData.Callee), exception),
Call: call,
})
return nil
}
func (txe *TxExecution) GovernAccount(governAccount *GovernAccountEvent, exception *errors.Exception) {
txe.Append(&Event{
Header: txe.Header(TypeGovernAccount, EventStringGovernAccount(governAccount.AccountUpdate.Address), exception),
GovernAccount: governAccount,
})
}
// Errors pushed to TxExecutions end up in merkle state so it is essential that they are deterministic and independent
// of the code path taken to execution (e.g. replay takes a different path to that of normal consensus reactor so stack
// traces may differ - as they may across architectures)
func (txe *TxExecution) PushError(err error) {
if txe.Exception == nil {
// Don't forget the nil jig
ex := errors.AsException(err)
if ex != nil {
txe.Exception = ex
}
}
}
func (txe *TxExecution) CallTrace() string {
var calls []string
for _, ev := range txe.Events {
if ev.Call != nil {
ex := ""
if ev.Header.Exception != nil {
ex = fmt.Sprintf(" [%v]", ev.Header.Exception)
}
calls = append(calls, fmt.Sprintf("%v: %v -> %v: %v%s",
ev.Call.CallType, ev.Call.CallData.Caller, ev.Call.CallData.Callee, ev.Call.Return, ex))
}
}
return strings.Join(calls, "\n")
}
func (txe *TxExecution) ExceptionalCalls() []*Event {
var exCalls []*Event
for _, ev := range txe.Events {
if ev.Call != nil && ev.Header.Exception != nil {
exCalls = append(exCalls, ev)
}
}
return exCalls
}
func (txe *TxExecution) CallError() *errors.CallError {
if txe.Exception == nil {
return nil
}
var nestedErrors []errors.NestedCallError
for _, ev := range txe.Events {
if ev.Call != nil && ev.Header.Exception != nil {
nestedErrors = append(nestedErrors, errors.NestedCallError{
CodedError: ev.Header.Exception,
Caller: ev.Call.CallData.Caller,
Callee: ev.Call.CallData.Callee,
StackDepth: ev.Call.StackDepth,
})
}
}
return &errors.CallError{
CodedError: txe.Exception,
NestedErrors: nestedErrors,
}
}
func (txe *TxExecution) TaggedEvents() Events {
return txe.Events
}
// Set result
func (txe *TxExecution) Return(returnValue []byte, gasUsed uint64) {
if txe.Result == nil {
txe.Result = &Result{}
}
txe.Result.Return = returnValue
txe.Result.GasUsed = gasUsed
}
func (txe *TxExecution) Name(entry *names.Entry) {
if txe.Result == nil {
txe.Result = &Result{}
}
txe.Result.NameEntry = entry
}
func (txe *TxExecution) Permission(permArgs *permission.PermArgs) {
if txe.Result == nil {
txe.Result = &Result{}
}
txe.Result.PermArgs = permArgs
}
func (txe *TxExecution) Append(tail ...*Event) {
for i, ev := range tail {
if ev != nil && ev.Header != nil {
ev.Header.Index = uint64(len(txe.Events) + i)
ev.Header.Height = txe.GetHeight()
}
}
txe.Events = append(txe.Events, tail...)
}
// Tags
func (txe *TxExecution) Get(key string) (interface{}, bool) {
switch key {
case event.EventIDKey:
return EventStringTxExecution(txe.TxHash), true
case event.EventTypeKey:
return txe.EventType(), true
}
return query.GetReflect(reflect.ValueOf(txe), key)
}
func QueryForTxExecution(txHash []byte) query.Queryable {
return event.QueryForEventID(EventStringTxExecution(txHash))
}