forked from spacemeshos/go-spacemesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
278 lines (247 loc) · 6.41 KB
/
context.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
package core
import (
"bytes"
"fmt"
"github.com/spacemeshos/go-scale"
"github.com/spacemeshos/go-spacemesh/common/types"
)
// Context serves 2 purposes:
// - maintains changes to the system state, that will be applied only after succeful execution
// - accumulates set of reusable objects and data.
type Context struct {
Registry HandlerRegistry
Loader AccountLoader
// LayerID of the block.
LayerID LayerID
GenesisID types.Hash20
PrincipalHandler Handler
PrincipalTemplate Template
PrincipalAccount Account
ParseOutput ParseOutput
Gas struct {
BaseGas uint64
FixedGas uint64
}
Header Header
Args scale.Encodable
// consumed is in gas units and will be used
consumed uint64
// fee is in coins units
fee uint64
// an amount transfrered to other accounts
transferred uint64
touched []Address
changed map[Address]*Account
}
// Principal returns address of the account that signed transaction.
func (c *Context) Principal() Address {
return c.PrincipalAccount.Address
}
// Layer returns block layer id.
func (c *Context) Layer() LayerID {
return c.LayerID
}
// GetGenesisID returns genesis id.
func (c *Context) GetGenesisID() Hash20 {
return c.GenesisID
}
// Template of the principal account.
func (c *Context) Template() Template {
return c.PrincipalTemplate
}
// Handler of the principal account.
func (c *Context) Handler() Handler {
return c.PrincipalHandler
}
// Spawn account.
func (c *Context) Spawn(args scale.Encodable) error {
account, err := c.load(ComputePrincipal(c.Header.TemplateAddress, args))
if err != nil {
return err
}
if account.TemplateAddress != nil {
return ErrSpawned
}
handler := c.Registry.Get(c.Header.TemplateAddress)
if handler == nil {
return fmt.Errorf("%w: spawn is called with unknown handler", ErrInternal)
}
buf := bytes.NewBuffer(nil)
instance, err := handler.New(args)
if err != nil {
return fmt.Errorf("%w: %w", ErrMalformed, err)
}
_, err = instance.EncodeScale(scale.NewEncoder(buf))
if err != nil {
return fmt.Errorf("%w: %w", ErrInternal, err)
}
account.State = buf.Bytes()
account.TemplateAddress = &c.Header.TemplateAddress
c.change(account)
return nil
}
// Transfer amount to the address after validation passes.
func (c *Context) Transfer(to Address, amount uint64) error {
return c.transfer(&c.PrincipalAccount, to, amount, c.Header.MaxSpend)
}
func (c *Context) transfer(from *Account, to Address, amount, max uint64) error {
account, err := c.load(to)
if err != nil {
return err
}
if amount > from.Balance {
return ErrNoBalance
}
if c.transferred+amount > max {
return fmt.Errorf("%w: %d", ErrMaxSpend, max)
}
// noop. only gas is consumed
if from.Address == to {
return nil
}
c.transferred += amount
from.Balance -= amount
account.Balance += amount
c.change(account)
return nil
}
// Relay call to the remote account.
func (c *Context) Relay(remoteTemplate, address Address, call func(Host) error) error {
account, err := c.load(address)
if err != nil {
return err
}
if account.TemplateAddress == nil {
return ErrNotSpawned
}
if *account.TemplateAddress != remoteTemplate {
return fmt.Errorf(
"%w: %s != %s",
ErrTemplateMismatch,
remoteTemplate.String(),
account.TemplateAddress.String(),
)
}
handler := c.Registry.Get(remoteTemplate)
if handler == nil {
panic("template of the spawned account should exist in the registry")
}
template, err := handler.Load(account.State)
if err != nil {
return err
}
remote := &RemoteContext{
Context: c,
remote: account,
handler: handler,
template: template,
}
if err := call(remote); err != nil {
return err
}
// ideally such changes would be serialized once for the whole block execution
// but it requires more changes in the cache, so can be done as an optimization
// if it proves meaningful (most likely wont)
buf := bytes.NewBuffer(nil)
encoder := scale.NewEncoder(buf)
if _, err := template.EncodeScale(encoder); err != nil {
return fmt.Errorf("%w: %w", ErrInternal, err)
}
account.State = buf.Bytes()
c.change(account)
return nil
}
// Consume gas from the account after validation passes.
func (c *Context) Consume(gas uint64) (err error) {
amount := gas * c.Header.GasPrice
if amount > c.PrincipalAccount.Balance {
amount = c.PrincipalAccount.Balance
err = ErrNoBalance
} else if total := c.consumed + gas; total > c.Header.MaxGas {
gas = c.Header.MaxGas - c.consumed
amount = gas * c.Header.GasPrice
err = ErrMaxGas
}
c.consumed += gas
c.fee += amount
c.PrincipalAccount.Balance -= amount
return err
}
// Apply is executed if transaction was consumed.
func (c *Context) Apply(updater AccountUpdater) error {
c.PrincipalAccount.NextNonce = c.Header.Nonce + 1
if err := updater.Update(c.PrincipalAccount); err != nil {
return fmt.Errorf("%w: %w", ErrInternal, err)
}
for _, address := range c.touched {
account := c.changed[address]
if err := updater.Update(*account); err != nil {
return fmt.Errorf("%w: %w", ErrInternal, err)
}
}
return nil
}
// Consumed gas.
func (c *Context) Consumed() uint64 {
return c.consumed
}
// Fee computed from consumed gas.
func (c *Context) Fee() uint64 {
return c.fee
}
// Updated list of addresses.
func (c *Context) Updated() []types.Address {
rst := make([]types.Address, 0, len(c.touched)+1)
rst = append(rst, c.PrincipalAccount.Address)
rst = append(rst, c.touched...)
return rst
}
func (c *Context) load(address types.Address) (*Account, error) {
if address == c.Principal() {
return &c.PrincipalAccount, nil
}
if c.changed == nil {
c.changed = map[Address]*Account{}
}
account, exist := c.changed[address]
if !exist {
loaded, err := c.Loader.Get(address)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrInternal, err)
}
account = &loaded
}
return account, nil
}
func (c *Context) change(account *Account) {
if account.Address == c.Principal() {
return
}
_, exist := c.changed[account.Address]
if !exist {
c.touched = append(c.touched, account.Address)
}
c.changed[account.Address] = account
}
// RemoteContext ...
type RemoteContext struct {
*Context
remote *Account
handler Handler
template Template
}
// Template ...
func (r *RemoteContext) Template() Template {
return r.template
}
// Handler ...
func (r *RemoteContext) Handler() Handler {
return r.handler
}
// Transfer ...
func (r *RemoteContext) Transfer(to Address, amount uint64) error {
if err := r.transfer(r.remote, to, amount, amount); err != nil {
return err
}
return nil
}