-
Notifications
You must be signed in to change notification settings - Fork 179
/
transaction.go
99 lines (79 loc) · 2.22 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
package fvm
import (
"fmt"
"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/cadence/runtime"
"github.com/onflow/flow-go/fvm/state"
"github.com/onflow/flow-go/model/flow"
)
func Transaction(tx *flow.TransactionBody) *TransactionProcedure {
return &TransactionProcedure{
ID: tx.ID(),
Transaction: tx,
}
}
type TransactionProcedure struct {
ID flow.Identifier
Transaction *flow.TransactionBody
Logs []string
Events []cadence.Event
// TODO: report gas consumption: https://github.com/dapperlabs/flow-go/issues/4139
GasUsed uint64
Err Error
}
type TransactionProcessor interface {
Process(*VirtualMachine, Context, *TransactionProcedure, state.Ledger) error
}
func (proc *TransactionProcedure) Run(vm *VirtualMachine, ctx Context, ledger state.Ledger) error {
for _, p := range ctx.TransactionProcessors {
err := p.Process(vm, ctx, proc, ledger)
vmErr, fatalErr := handleError(err)
if fatalErr != nil {
return fatalErr
}
if vmErr != nil {
proc.Err = vmErr
return nil
}
}
return nil
}
func (proc *TransactionProcedure) ConvertEvents(txIndex uint32) ([]flow.Event, error) {
flowEvents := make([]flow.Event, len(proc.Events))
for i, event := range proc.Events {
payload, err := jsoncdc.Encode(event)
if err != nil {
return nil, fmt.Errorf("failed to encode event: %w", err)
}
flowEvents[i] = flow.Event{
Type: flow.EventType(event.EventType.ID()),
TransactionID: proc.ID,
TransactionIndex: txIndex,
EventIndex: uint32(i),
Payload: payload,
}
}
return flowEvents, nil
}
type TransactionInvocator struct{}
func NewTransactionInvocator() *TransactionInvocator {
return &TransactionInvocator{}
}
func (i *TransactionInvocator) Process(
vm *VirtualMachine,
ctx Context,
proc *TransactionProcedure,
ledger state.Ledger,
) error {
env := newEnvironment(ctx, ledger)
env.setTransaction(vm, proc.Transaction)
location := runtime.TransactionLocation(proc.ID[:])
err := vm.Runtime.ExecuteTransaction(proc.Transaction.Script, proc.Transaction.Arguments, env, location)
if err != nil {
return err
}
proc.Events = env.getEvents()
proc.Logs = env.getLogs()
return nil
}