-
Notifications
You must be signed in to change notification settings - Fork 107
/
transactions.go
267 lines (221 loc) · 7.06 KB
/
transactions.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
package vault
import (
"fmt"
"github.com/oasisprotocol/oasis-core/go/common/errors"
"github.com/oasisprotocol/oasis-core/go/consensus/cometbft/api"
stakingState "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/apps/staking/state"
vaultState "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/apps/vault/state"
vault "github.com/oasisprotocol/oasis-core/go/vault/api"
)
func (app *vaultApplication) create(ctx *api.Context, create *vault.Create) error {
state := vaultState.NewMutableState(ctx.State())
params, err := state.ConsensusParameters(ctx)
if err != nil {
return fmt.Errorf("failed to fetch consensus parameters: %w", err)
}
if err = create.Validate(params); err != nil {
return fmt.Errorf("%w: %w", vault.ErrInvalidArgument, err)
}
if ctx.IsCheckOnly() {
return nil
}
// Charge gas for this transaction.
if err = ctx.Gas().UseGas(1, vault.GasOpCreate, params.GasCosts); err != nil {
return err
}
// Return early for simulation as we only need gas accounting.
if ctx.IsSimulation() {
return nil
}
// Start a new transaction and rollback in case we fail.
ctx = ctx.NewTransaction()
defer ctx.Close()
// Determine caller nonce.
stakeState := stakingState.NewMutableState(ctx.State())
callerAcct, err := stakeState.Account(ctx, ctx.CallerAddress())
if err != nil {
return err
}
// Create a new vault.
newVault := &vault.Vault{
State: vault.StateActive,
Nonce: 0,
Creator: ctx.CallerAddress(),
ID: callerAcct.General.Nonce,
AdminAuthority: create.AdminAuthority,
SuspendAuthority: create.SuspendAuthority,
}
if err = state.CreateVault(ctx, newVault); err != nil {
return err
}
ctx.Commit()
return nil
}
func (app *vaultApplication) authorizeAction(ctx *api.Context, authAction *vault.AuthorizeAction) error {
state := vaultState.NewMutableState(ctx.State())
params, err := state.ConsensusParameters(ctx)
if err != nil {
return fmt.Errorf("failed to fetch consensus parameters: %w", err)
}
if err = authAction.Validate(params); err != nil {
return fmt.Errorf("%w: %w", vault.ErrInvalidArgument, err)
}
// Ensure vault exists.
vlt, err := state.Vault(ctx, authAction.Vault)
if err != nil {
return err
}
// Validate action nonce. Currently queuing multiple future actions is not allowed.
if authAction.Nonce != vlt.Nonce {
return vault.ErrInvalidNonce
}
// Validate whether the caller is authorized to submit an action.
if !authAction.Action.IsAuthorized(vlt, ctx.CallerAddress()) {
return vault.ErrForbidden
}
if ctx.IsCheckOnly() {
return nil
}
// Charge gas for this transaction.
if err = ctx.Gas().UseGas(1, vault.GasOpAuthorizeAction, params.GasCosts); err != nil {
return err
}
// Start a new transaction and rollback in case we fail.
ctx = ctx.NewTransaction()
defer ctx.Close()
// Check if there is an existing pending action for this nonce. In this case, we will be
// updating the action.
pendingAction, err := state.PendingAction(ctx, authAction.Vault, authAction.Nonce)
switch err {
case nil:
// Ensure that the action is the same so that the authorizer really signed the correct
// action.
if !pendingAction.Action.Equal(&authAction.Action) {
return vault.ErrInvalidArgument
}
case vault.ErrNoSuchAction:
// Create a new pending action.
pendingAction = &vault.PendingAction{
Nonce: authAction.Nonce,
Action: authAction.Action,
}
default:
return err
}
ctx.EmitEvent(api.NewEventBuilder(app.Name()).TypedAttribute(&vault.ActionSubmittedEvent{
Submitter: ctx.CallerAddress(),
Vault: authAction.Vault,
Nonce: authAction.Nonce,
}))
// Update list of authorizers.
if !pendingAction.ContainsAuthorizationFrom(ctx.CallerAddress()) {
pendingAction.AuthorizedBy = append(pendingAction.AuthorizedBy, ctx.CallerAddress())
if err = state.SetPendingAction(ctx, authAction.Vault, pendingAction); err != nil {
return err
}
}
// Check if action has become executable.
var canExecute bool
for _, auth := range pendingAction.Action.Authorities(vlt) {
if auth.Verify(pendingAction.AuthorizedBy) {
canExecute = true
break
}
}
if !canExecute {
ctx.Commit()
return nil
}
// Execute action.
evExec := &vault.ActionExecutedEvent{
Vault: authAction.Vault,
Nonce: authAction.Nonce,
}
err = app.executeAction(ctx, vlt, &pendingAction.Action)
switch {
case api.IsUnavailableStateError(err):
// Propagate state unavailability errors.
return err
default:
// Record other errors (or success) in the execution event.
evExec.Result.Module, evExec.Result.Code = errors.Code(err)
ctx.Logger().Debug("vault executed action",
"err", err,
"vault", authAction.Vault,
"nonce", authAction.Nonce,
"action", pendingAction.Action,
)
}
ctx.EmitEvent(api.NewEventBuilder(app.Name()).TypedAttribute(evExec))
// Remove pending action as it has been executed.
if err = state.RemovePendingAction(ctx, authAction.Vault, authAction.Nonce); err != nil {
return err
}
vlt.Nonce++
if err = state.SetVault(ctx, vlt); err != nil {
return err
}
ctx.Commit()
return nil
}
func (app *vaultApplication) cancelAction(ctx *api.Context, cancelAction *vault.CancelAction) error {
// Validate arguments.
if err := cancelAction.Validate(); err != nil {
return fmt.Errorf("%w: %w", vault.ErrInvalidArgument, err)
}
// Ensure vault exists.
state := vaultState.NewMutableState(ctx.State())
vlt, err := state.Vault(ctx, cancelAction.Vault)
if err != nil {
return err
}
// Validate action nonce. Currently queuing multiple future actions is not allowed.
if cancelAction.Nonce != vlt.Nonce {
return vault.ErrInvalidNonce
}
// Before we know what the canceled action is, we can only check that the caller is part of at
// least one of the vault's authorities. Later, we will check against the action authority.
if !vlt.AuthoritiesContain(ctx.CallerAddress()) {
return vault.ErrForbidden
}
if ctx.IsCheckOnly() {
return nil
}
// Charge gas for this transaction.
params, err := state.ConsensusParameters(ctx)
if err != nil {
return fmt.Errorf("failed to fetch consensus parameters: %w", err)
}
if err = ctx.Gas().UseGas(1, vault.GasOpCancelAction, params.GasCosts); err != nil {
return err
}
// Return early for simulation as we only need gas accounting.
if ctx.IsSimulation() {
return nil
}
// Start a new transaction and rollback in case we fail.
ctx = ctx.NewTransaction()
defer ctx.Close()
// Fetch the action that is being canceled.
pendingAction, err := state.PendingAction(ctx, cancelAction.Vault, cancelAction.Nonce)
if err != nil {
return err
}
// Perform action-specific authority check now that we know what the action is.
if !pendingAction.Action.IsAuthorized(vlt, ctx.CallerAddress()) {
return vault.ErrForbidden
}
ctx.EmitEvent(api.NewEventBuilder(app.Name()).TypedAttribute(&vault.ActionCanceledEvent{
Vault: cancelAction.Vault,
Nonce: cancelAction.Nonce,
}))
if err = state.RemovePendingAction(ctx, cancelAction.Vault, cancelAction.Nonce); err != nil {
return err
}
vlt.Nonce++
if err = state.SetVault(ctx, vlt); err != nil {
return err
}
ctx.Commit()
return nil
}