forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperator_fee.go
More file actions
261 lines (218 loc) · 9.26 KB
/
operator_fee.go
File metadata and controls
261 lines (218 loc) · 9.26 KB
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
package dsl
import (
"math/big"
"time"
"github.com/ethereum-optimism/optimism/op-chain-ops/devkeys"
"github.com/ethereum-optimism/optimism/op-core/forks"
"github.com/ethereum-optimism/optimism/op-core/predeploys"
"github.com/ethereum-optimism/optimism/op-devstack/devtest"
"github.com/ethereum-optimism/optimism/op-devstack/stack/match"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum-optimism/optimism/op-service/txintent/bindings"
"github.com/ethereum-optimism/optimism/op-service/txintent/contractio"
"github.com/ethereum/go-ethereum/core/types"
)
type OperatorFee struct {
commonImpl
l1Client *L1ELNode
l2Network *L2Network
systemConfig bindings.SystemConfig
l1Block bindings.L1Block
gasPriceOracle bindings.GasPriceOracle
originalScalar uint32
originalConstant uint64
}
type OperatorFeeValidationResult struct {
TransactionReceipt *types.Receipt
ExpectedOperatorFee *big.Int
ActualTotalFee *big.Int
VaultBalanceIncrease *big.Int
}
func NewOperatorFee(t devtest.T, l2Network *L2Network, l1EL *L1ELNode) *OperatorFee {
systemConfig := bindings.NewBindings[bindings.SystemConfig](
bindings.WithClient(l1EL.EthClient()),
bindings.WithTo(l2Network.Escape().Deployment().SystemConfigProxyAddr()),
bindings.WithTest(t))
l1Block := bindings.NewBindings[bindings.L1Block](
bindings.WithClient(l2Network.inner.L2ELNode(match.FirstL2EL).EthClient()),
bindings.WithTo(predeploys.L1BlockAddr),
bindings.WithTest(t))
gasPriceOracle := bindings.NewBindings[bindings.GasPriceOracle](
bindings.WithClient(l2Network.inner.L2ELNode(match.FirstL2EL).EthClient()),
bindings.WithTo(predeploys.GasPriceOracleAddr),
bindings.WithTest(t))
originalScalar, err := contractio.Read(systemConfig.OperatorFeeScalar(), t.Ctx())
t.Require().NoError(err)
originalConstant, err := contractio.Read(systemConfig.OperatorFeeConstant(), t.Ctx())
t.Require().NoError(err)
return &OperatorFee{
commonImpl: commonFromT(t),
l1Client: l1EL,
l2Network: l2Network,
systemConfig: systemConfig,
l1Block: l1Block,
gasPriceOracle: gasPriceOracle,
originalScalar: originalScalar,
originalConstant: originalConstant,
}
}
func (of *OperatorFee) CheckCompatibility() bool {
_, err := contractio.Read(of.systemConfig.OperatorFeeScalar(), of.ctx)
if err != nil {
of.t.Skipf("Operator fee methods not available in devstack: %v", err)
return false
}
return true
}
func (of *OperatorFee) GetSystemOwner() *EOA {
systemOwnerKey := devkeys.SystemConfigOwner.Key(of.l2Network.ChainID().ToBig())
return NewKey(of.t, of.l2Network.Escape().Keys().Secret(systemOwnerKey)).User(of.l1Client)
}
func (of *OperatorFee) SetOperatorFee(scalar uint32, constant uint64) {
systemOwner := of.GetSystemOwner()
_, err := contractio.Write(
of.systemConfig.SetOperatorFeeScalars(scalar, constant),
of.ctx,
systemOwner.Plan())
of.require.NoError(err)
of.t.Logf("Set operator fee on L1: scalar=%d, constant=%d", scalar, constant)
}
func (of *OperatorFee) WaitForL2SyncWithCurrentL1State() {
// Read current L1 values
l1Scalar, err := contractio.Read(of.systemConfig.OperatorFeeScalar(), of.ctx)
of.require.NoError(err)
l1Constant, err := contractio.Read(of.systemConfig.OperatorFeeConstant(), of.ctx)
of.require.NoError(err)
// Wait for L2 to sync with current L1 values
of.WaitForL2Sync(l1Scalar, l1Constant)
}
func (of *OperatorFee) WaitForL2Sync(expectedScalar uint32, expectedConstant uint64) {
of.require.Eventually(func() bool {
scalar, err := contractio.Read(of.l1Block.OperatorFeeScalar(), of.ctx)
if err != nil {
return false
}
constant, err := contractio.Read(of.l1Block.OperatorFeeConstant(), of.ctx)
if err != nil {
return false
}
return scalar == expectedScalar && constant == expectedConstant
}, 2*time.Minute, 5*time.Second, "L2 operator fee parameters did not sync within 2 minutes")
}
func (of *OperatorFee) VerifyL2Config(expectedScalar uint32, expectedConstant uint64) {
scalar, err := contractio.Read(of.l1Block.OperatorFeeScalar(), of.ctx)
of.require.NoError(err)
of.require.Equal(expectedScalar, scalar)
constant, err := contractio.Read(of.l1Block.OperatorFeeConstant(), of.ctx)
of.require.NoError(err)
of.require.Equal(expectedConstant, constant)
}
func (of *OperatorFee) ValidateTransactionFees(from *EOA, to *EOA, amount *big.Int, expectedScalar uint32, expectedConstant uint64) OperatorFeeValidationResult {
// Ensure there is at least one user transaction, to trigger flow of operator fees to vault.
tx := from.Transfer(to.Address(), eth.WeiBig(amount))
receipt, err := tx.Included.Eval(of.ctx)
of.require.NoError(err)
of.require.Equal(types.ReceiptStatusSuccessful, receipt.Status)
blockHash := receipt.BlockHash
info, txs, err := from.el.stackEL().EthClient().InfoAndTxsByHash(of.ctx, blockHash)
of.require.NoError(err)
// Infer active fork from block info
isJovian := of.l2Network.IsForkActiveAt(forks.Jovian, info.Time())
// Verify GPO upgraded when jovian is active
// We have nothing to assert when jovian is inactive because an isthmus L2 can
// run against isthmus L1 contracts or jovian L1 contracts.
if isJovian {
isJovianinGPO, err := contractio.Read(of.gasPriceOracle.IsJovian(), of.ctx)
of.require.NoError(err)
of.require.True(isJovianinGPO)
}
// Get updated balance in operator fee vault to compute delta
vaultAfter, err := from.el.stackEL().EthClient().BalanceAt(of.ctx, predeploys.OperatorFeeVaultAddr, receipt.BlockNumber)
of.require.NoError(err)
vaultBefore, err := from.el.stackEL().EthClient().BalanceAt(of.ctx, predeploys.OperatorFeeVaultAddr, big.NewInt(0).Sub(receipt.BlockNumber, big.NewInt(1)))
of.require.NoError(err)
vaultIncrease := new(big.Int).Sub(vaultAfter, vaultBefore)
// Loop through transactions in block to compute expected operator fee vault increase
expectedOperatorFeeVaultIncrease := big.NewInt(0)
if !(expectedScalar == 0 && expectedConstant == 0) {
// The test submits one user transaction but we loop over all user transactions
// to make the test robust to any other traffic on the chain.
for _, tx := range txs {
if tx.Type() == types.DepositTxType {
continue
}
receipt, err := from.el.stackEL().EthClient().TransactionReceipt(of.ctx, tx.Hash())
of.require.NoError(err)
operatorFee := new(big.Int).Mul(big.NewInt(int64(receipt.GasUsed)), big.NewInt(int64(expectedScalar)))
if isJovian {
// Jovian formula: (gasUsed * operatorFeeScalar * 100) + operatorFeeConstant
operatorFee.Mul(operatorFee, big.NewInt(100))
} else {
// Isthmus formula: (gasUsed * operatorFeeScalar / 1e6) + operatorFeeConstant
operatorFee.Div(operatorFee, big.NewInt(1000000))
}
operatorFee.Add(operatorFee, big.NewInt(int64(expectedConstant)))
expectedOperatorFeeVaultIncrease =
expectedOperatorFeeVaultIncrease.Add(expectedOperatorFeeVaultIncrease, operatorFee)
}
}
// Use Cmp for big.Int comparison to avoid representation issues
of.require.Equal(0, expectedOperatorFeeVaultIncrease.Cmp(vaultIncrease),
"operator fee vault balance mismatch: expected %s, got %s",
expectedOperatorFeeVaultIncrease.String(), vaultIncrease.String())
actualTotalFee := new(big.Int).Mul(receipt.EffectiveGasPrice, big.NewInt(int64(receipt.GasUsed)))
if receipt.L1Fee != nil {
actualTotalFee.Add(actualTotalFee, receipt.L1Fee)
}
if expectedScalar != 0 || expectedConstant != 0 {
of.require.NotNil(receipt.OperatorFeeScalar)
of.require.NotNil(receipt.OperatorFeeConstant)
of.require.Equal(expectedScalar, uint32(*receipt.OperatorFeeScalar))
of.require.Equal(expectedConstant, *receipt.OperatorFeeConstant)
}
return OperatorFeeValidationResult{
TransactionReceipt: receipt,
ExpectedOperatorFee: expectedOperatorFeeVaultIncrease,
ActualTotalFee: actualTotalFee,
VaultBalanceIncrease: vaultIncrease,
}
}
func (of *OperatorFee) RestoreOriginalConfig() {
of.SetOperatorFee(of.originalScalar, of.originalConstant)
}
func RunOperatorFeeTest(t devtest.T, l2Chain *L2Network, l1EL *L1ELNode, funderL1, funderL2 *Funder) {
fundAmount := eth.OneTenthEther
alice := funderL2.NewFundedEOA(fundAmount)
alice.WaitForBalance(fundAmount)
bob := funderL2.NewFundedEOA(eth.ZeroWei)
operatorFee := NewOperatorFee(t, l2Chain, l1EL)
operatorFee.CheckCompatibility()
systemOwner := operatorFee.GetSystemOwner()
funderL1.FundAtLeast(systemOwner, fundAmount)
// First, ensure L2 is synced with current L1 state before starting tests
t.Log("Ensuring L2 is synced with current L1 state...")
operatorFee.WaitForL2SyncWithCurrentL1State()
testCases := []struct {
name string
scalar uint32
constant uint64
}{
{"ZeroFees", 0, 0},
{"NonZeroFees", 300, 400},
}
for _, tc := range testCases {
t.Run(tc.name, func(t devtest.T) {
operatorFee.SetOperatorFee(tc.scalar, tc.constant)
operatorFee.WaitForL2Sync(tc.scalar, tc.constant)
operatorFee.VerifyL2Config(tc.scalar, tc.constant)
result := operatorFee.ValidateTransactionFees(alice, bob, big.NewInt(1000), tc.scalar, tc.constant)
t.Log("Test completed successfully:",
"testCase", tc.name,
"gasUsed", result.TransactionReceipt.GasUsed,
"actualTotalFee", result.ActualTotalFee.String(),
"expectedOperatorFee", result.ExpectedOperatorFee.String(),
"vaultBalanceIncrease", result.VaultBalanceIncrease.String())
})
}
operatorFee.RestoreOriginalConfig()
}