forked from OffchainLabs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathArbOwner.go
344 lines (300 loc) · 11.5 KB
/
ArbOwner.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Copyright 2021-2024, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE
package precompiles
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/params"
"github.com/offchainlabs/nitro/arbos/l1pricing"
"github.com/offchainlabs/nitro/arbos/programs"
"github.com/offchainlabs/nitro/util/arbmath"
am "github.com/offchainlabs/nitro/util/arbmath"
)
// ArbOwner precompile provides owners with tools for managing the rollup.
// All calls to this precompile are authorized by the OwnerPrecompile wrapper,
// which ensures only a chain owner can access these methods. For methods that
// are safe for non-owners to call, see ArbOwnerOld
type ArbOwner struct {
Address addr // 0x70
OwnerActs func(ctx, mech, bytes4, addr, []byte) error
OwnerActsGasCost func(bytes4, addr, []byte) (uint64, error)
}
var (
ErrOutOfBounds = errors.New("value out of bounds")
)
// AddChainOwner adds account as a chain owner
func (con ArbOwner) AddChainOwner(c ctx, evm mech, newOwner addr) error {
return c.State.ChainOwners().Add(newOwner)
}
// RemoveChainOwner removes account from the list of chain owners
func (con ArbOwner) RemoveChainOwner(c ctx, evm mech, addr addr) error {
member, _ := con.IsChainOwner(c, evm, addr)
if !member {
return errors.New("tried to remove non-owner")
}
return c.State.ChainOwners().Remove(addr, c.State.ArbOSVersion())
}
// IsChainOwner checks if the account is a chain owner
func (con ArbOwner) IsChainOwner(c ctx, evm mech, addr addr) (bool, error) {
return c.State.ChainOwners().IsMember(addr)
}
// GetAllChainOwners retrieves the list of chain owners
func (con ArbOwner) GetAllChainOwners(c ctx, evm mech) ([]common.Address, error) {
return c.State.ChainOwners().AllMembers(65536)
}
// SetL1BaseFeeEstimateInertia sets how slowly ArbOS updates its estimate of the L1 basefee
func (con ArbOwner) SetL1BaseFeeEstimateInertia(c ctx, evm mech, inertia uint64) error {
return c.State.L1PricingState().SetInertia(inertia)
}
// SetL2BaseFee sets the L2 gas price directly, bypassing the pool calculus
func (con ArbOwner) SetL2BaseFee(c ctx, evm mech, priceInWei huge) error {
return c.State.L2PricingState().SetBaseFeeWei(priceInWei)
}
// SetMinimumL2BaseFee sets the minimum base fee needed for a transaction to succeed
func (con ArbOwner) SetMinimumL2BaseFee(c ctx, evm mech, priceInWei huge) error {
if c.txProcessor.MsgIsNonMutating() && priceInWei.Sign() == 0 {
return errors.New("minimum base fee must be nonzero")
}
return c.State.L2PricingState().SetMinBaseFeeWei(priceInWei)
}
// SetSpeedLimit sets the computational speed limit for the chain
func (con ArbOwner) SetSpeedLimit(c ctx, evm mech, limit uint64) error {
return c.State.L2PricingState().SetSpeedLimitPerSecond(limit)
}
// SetMaxTxGasLimit sets the maximum size a tx (and block) can be
func (con ArbOwner) SetMaxTxGasLimit(c ctx, evm mech, limit uint64) error {
return c.State.L2PricingState().SetMaxPerBlockGasLimit(limit)
}
// SetL2GasPricingInertia sets the L2 gas pricing inertia
func (con ArbOwner) SetL2GasPricingInertia(c ctx, evm mech, sec uint64) error {
return c.State.L2PricingState().SetPricingInertia(sec)
}
// SetL2GasBacklogTolerance sets the L2 gas backlog tolerance
func (con ArbOwner) SetL2GasBacklogTolerance(c ctx, evm mech, sec uint64) error {
return c.State.L2PricingState().SetBacklogTolerance(sec)
}
// GetNetworkFeeAccount gets the network fee collector
func (con ArbOwner) GetNetworkFeeAccount(c ctx, evm mech) (addr, error) {
return c.State.NetworkFeeAccount()
}
// GetInfraFeeAccount gets the infrastructure fee collector
func (con ArbOwner) GetInfraFeeAccount(c ctx, evm mech) (addr, error) {
return c.State.InfraFeeAccount()
}
// SetNetworkFeeAccount sets the network fee collector to the new network fee account
func (con ArbOwner) SetNetworkFeeAccount(c ctx, evm mech, newNetworkFeeAccount addr) error {
return c.State.SetNetworkFeeAccount(newNetworkFeeAccount)
}
// SetInfraFeeAccount sets the infra fee collector to the new network fee account
func (con ArbOwner) SetInfraFeeAccount(c ctx, evm mech, newNetworkFeeAccount addr) error {
return c.State.SetInfraFeeAccount(newNetworkFeeAccount)
}
// ScheduleArbOSUpgrade to the requested version at the requested timestamp
func (con ArbOwner) ScheduleArbOSUpgrade(c ctx, evm mech, newVersion uint64, timestamp uint64) error {
return c.State.ScheduleArbOSUpgrade(newVersion, timestamp)
}
func (con ArbOwner) SetL1PricingEquilibrationUnits(c ctx, evm mech, equilibrationUnits huge) error {
return c.State.L1PricingState().SetEquilibrationUnits(equilibrationUnits)
}
func (con ArbOwner) SetL1PricingInertia(c ctx, evm mech, inertia uint64) error {
return c.State.L1PricingState().SetInertia(inertia)
}
func (con ArbOwner) SetL1PricingRewardRecipient(c ctx, evm mech, recipient addr) error {
return c.State.L1PricingState().SetPayRewardsTo(recipient)
}
func (con ArbOwner) SetL1PricingRewardRate(c ctx, evm mech, weiPerUnit uint64) error {
return c.State.L1PricingState().SetPerUnitReward(weiPerUnit)
}
func (con ArbOwner) SetL1PricePerUnit(c ctx, evm mech, pricePerUnit *big.Int) error {
return c.State.L1PricingState().SetPricePerUnit(pricePerUnit)
}
func (con ArbOwner) SetPerBatchGasCharge(c ctx, evm mech, cost int64) error {
return c.State.L1PricingState().SetPerBatchGasCost(cost)
}
func (con ArbOwner) SetAmortizedCostCapBips(c ctx, evm mech, cap uint64) error {
return c.State.L1PricingState().SetAmortizedCostCapBips(cap)
}
func (con ArbOwner) SetBrotliCompressionLevel(c ctx, evm mech, level uint64) error {
return c.State.SetBrotliCompressionLevel(level)
}
func (con ArbOwner) ReleaseL1PricerSurplusFunds(c ctx, evm mech, maxWeiToRelease huge) (huge, error) {
balance := evm.StateDB.GetBalance(l1pricing.L1PricerFundsPoolAddress)
l1p := c.State.L1PricingState()
recognized, err := l1p.L1FeesAvailable()
if err != nil {
return nil, err
}
weiToTransfer := new(big.Int).Sub(balance.ToBig(), recognized)
if weiToTransfer.Sign() < 0 {
return common.Big0, nil
}
if weiToTransfer.Cmp(maxWeiToRelease) > 0 {
weiToTransfer = maxWeiToRelease
}
if _, err := l1p.AddToL1FeesAvailable(weiToTransfer); err != nil {
return nil, err
}
return weiToTransfer, nil
}
// Sets the amount of ink 1 gas buys
func (con ArbOwner) SetInkPrice(c ctx, evm mech, inkPrice uint32) error {
params, err := c.State.Programs().Params()
if err != nil {
return err
}
ink, err := arbmath.IntToUint24(inkPrice)
if err != nil || ink == 0 {
return errors.New("ink price must be a positive uint24")
}
params.InkPrice = ink
return params.Save()
}
// Sets the maximum depth (in wasm words) a wasm stack may grow
func (con ArbOwner) SetWasmMaxStackDepth(c ctx, evm mech, depth uint32) error {
params, err := c.State.Programs().Params()
if err != nil {
return err
}
params.MaxStackDepth = depth
return params.Save()
}
// Gets the number of free wasm pages a tx gets
func (con ArbOwner) SetWasmFreePages(c ctx, evm mech, pages uint16) error {
params, err := c.State.Programs().Params()
if err != nil {
return err
}
params.FreePages = pages
return params.Save()
}
// Sets the base cost of each additional wasm page
func (con ArbOwner) SetWasmPageGas(c ctx, evm mech, gas uint16) error {
params, err := c.State.Programs().Params()
if err != nil {
return err
}
params.PageGas = gas
return params.Save()
}
// Sets the initial number of pages a wasm may allocate
func (con ArbOwner) SetWasmPageLimit(c ctx, evm mech, limit uint16) error {
params, err := c.State.Programs().Params()
if err != nil {
return err
}
params.PageLimit = limit
return params.Save()
}
// Sets the minimum costs to invoke a program
func (con ArbOwner) SetWasmMinInitGas(c ctx, _ mech, gas, cached uint64) error {
params, err := c.State.Programs().Params()
if err != nil {
return err
}
params.MinInitGas = am.SaturatingUUCast[uint8](am.DivCeil(gas, programs.MinInitGasUnits))
params.MinCachedInitGas = am.SaturatingUUCast[uint8](am.DivCeil(cached, programs.MinCachedGasUnits))
return params.Save()
}
// Sets the linear adjustment made to program init costs
func (con ArbOwner) SetWasmInitCostScalar(c ctx, _ mech, percent uint64) error {
params, err := c.State.Programs().Params()
if err != nil {
return err
}
params.InitCostScalar = am.SaturatingUUCast[uint8](am.DivCeil(percent, programs.CostScalarPercent))
return params.Save()
}
// Sets the number of days after which programs deactivate
func (con ArbOwner) SetWasmExpiryDays(c ctx, _ mech, days uint16) error {
params, err := c.State.Programs().Params()
if err != nil {
return err
}
params.ExpiryDays = days
return params.Save()
}
// Sets the age a program must be to perform a keepalive
func (con ArbOwner) SetWasmKeepaliveDays(c ctx, _ mech, days uint16) error {
params, err := c.State.Programs().Params()
if err != nil {
return err
}
params.KeepaliveDays = days
return params.Save()
}
// Sets the number of extra programs ArbOS caches during a given block
func (con ArbOwner) SetWasmBlockCacheSize(c ctx, _ mech, count uint16) error {
params, err := c.State.Programs().Params()
if err != nil {
return err
}
params.BlockCacheSize = count
return params.Save()
}
// Adds account as a wasm cache manager
func (con ArbOwner) AddWasmCacheManager(c ctx, _ mech, manager addr) error {
return c.State.Programs().CacheManagers().Add(manager)
}
// Removes account from the list of wasm cache managers
func (con ArbOwner) RemoveWasmCacheManager(c ctx, _ mech, manager addr) error {
managers := c.State.Programs().CacheManagers()
isMember, err := managers.IsMember(manager)
if err != nil {
return err
}
if !isMember {
return errors.New("tried to remove non-manager")
}
return managers.Remove(manager, c.State.ArbOSVersion())
}
func (con ArbOwner) SetChainConfig(c ctx, evm mech, serializedChainConfig []byte) error {
if c == nil {
return errors.New("nil context")
}
if c.txProcessor == nil {
return errors.New("uninitialized tx processor")
}
if c.txProcessor.MsgIsNonMutating() {
var newConfig params.ChainConfig
err := json.Unmarshal(serializedChainConfig, &newConfig)
if err != nil {
return fmt.Errorf("invalid chain config, can't deserialize: %w", err)
}
if newConfig.ChainID == nil {
return errors.New("invalid chain config, missing chain id")
}
chainId, err := c.State.ChainId()
if err != nil {
return fmt.Errorf("failed to get chain id from ArbOS state: %w", err)
}
if newConfig.ChainID.Cmp(chainId) != 0 {
return fmt.Errorf("invalid chain config, chain id mismatch, want: %v, have: %v", chainId, newConfig.ChainID)
}
oldSerializedConfig, err := c.State.ChainConfig()
if err != nil {
return fmt.Errorf("failed to get old chain config from ArbOS state: %w", err)
}
if bytes.Equal(oldSerializedConfig, serializedChainConfig) {
return errors.New("new chain config is the same as old one in ArbOS state")
}
if len(oldSerializedConfig) != 0 {
var oldConfig params.ChainConfig
err = json.Unmarshal(oldSerializedConfig, &oldConfig)
if err != nil {
return fmt.Errorf("failed to deserialize old chain config: %w", err)
}
if err := oldConfig.CheckCompatible(&newConfig, evm.Context.BlockNumber.Uint64(), evm.Context.Time); err != nil {
return fmt.Errorf("invalid chain config, not compatible with previous: %w", err)
}
}
currentConfig := evm.ChainConfig()
if err := currentConfig.CheckCompatible(&newConfig, evm.Context.BlockNumber.Uint64(), evm.Context.Time); err != nil {
return fmt.Errorf("invalid chain config, not compatible with EVM's chain config: %w", err)
}
}
return c.State.SetChainConfig(serializedChainConfig)
}