-
Notifications
You must be signed in to change notification settings - Fork 487
/
Copy pathblock_processor.go
352 lines (302 loc) · 10.6 KB
/
block_processor.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
345
346
347
348
349
350
351
352
//
// Copyright 2021, Offchain Labs, Inc. All rights reserved.
//
package arbos
import (
"encoding/binary"
"fmt"
"math"
"math/big"
"strconv"
"github.com/offchainlabs/arbstate/arbos/arbosState"
"github.com/offchainlabs/arbstate/arbos/l2pricing"
"github.com/offchainlabs/arbstate/arbos/util"
"github.com/offchainlabs/arbstate/solgen/go/precompilesgen"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/trie"
)
// set by the precompile module, to avoid a package dependence cycle
var ArbRetryableTxAddress common.Address
var ArbSysAddress common.Address
var RedeemScheduledEventID common.Hash
var L2ToL1TransactionEventID common.Hash
var EmitReedeemScheduledEvent func(*vm.EVM, uint64, uint64, [32]byte, [32]byte, common.Address) error
var EmitTicketCreatedEvent func(*vm.EVM, [32]byte) error
func createNewHeader(prevHeader *types.Header, l1info *L1Info, state *arbosState.ArbosState) *types.Header {
l2Pricing := state.L2PricingState()
baseFee, err := l2Pricing.GasPriceWei()
state.Restrict(err)
var lastBlockHash common.Hash
blockNumber := big.NewInt(0)
timestamp := uint64(0)
coinbase := common.Address{}
if l1info != nil {
timestamp = l1info.l1Timestamp.Uint64()
coinbase = l1info.poster
}
if prevHeader != nil {
lastBlockHash = prevHeader.Hash()
blockNumber.Add(prevHeader.Number, big.NewInt(1))
if timestamp < prevHeader.Time {
timestamp = prevHeader.Time
}
}
return &types.Header{
ParentHash: lastBlockHash,
UncleHash: [32]byte{},
Coinbase: coinbase,
Root: [32]byte{}, // Filled in later
TxHash: [32]byte{}, // Filled in later
ReceiptHash: [32]byte{}, // Filled in later
Bloom: [256]byte{}, // Filled in later
Difficulty: big.NewInt(1),
Number: blockNumber,
GasLimit: l2pricing.L2GasLimit,
GasUsed: 0,
Time: timestamp,
Extra: []byte{}, // Unused
MixDigest: [32]byte{}, // Unused
Nonce: [8]byte{}, // Filled in later
BaseFee: baseFee,
}
}
func ProduceBlock(
message *L1IncomingMessage,
delayedMessagesRead uint64,
lastBlockHeader *types.Header,
statedb *state.StateDB,
chainContext core.ChainContext,
chainConfig *params.ChainConfig,
) (*types.Block, types.Receipts) {
state, err := arbosState.OpenSystemArbosState(statedb, true)
if err != nil {
panic(err)
}
if statedb.GetTotalBalanceDelta().BitLen() != 0 {
panic("ProduceBlock called with dirty StateDB (non-zero total balance delta)")
}
txes, err := message.ParseL2Transactions(chainConfig.ChainID)
if err != nil {
log.Warn("error parsing incoming message", "err", err)
txes = types.Transactions{}
}
poster := message.Header.Poster
l1Info := &L1Info{
poster: poster,
l1BlockNumber: message.Header.BlockNumber.Big(),
l1Timestamp: message.Header.Timestamp.Big(),
}
gasLeft, _ := state.L2PricingState().PerBlockGasLimit()
header := createNewHeader(lastBlockHeader, l1Info, state)
signer := types.MakeSigner(chainConfig, header.Number)
nextL1BlockNumber, _ := state.Blockhashes().NextBlockNumber()
if l1Info.l1BlockNumber.Uint64() >= nextL1BlockNumber {
// Make an ArbitrumInternalTx the first tx to update the L1 block number
// Note: 0 is the TxIndex. If this transaction is ever not the first, that needs updated.
tx := InternalTxUpdateL1BlockNumber(chainConfig.ChainID, l1Info.l1BlockNumber, header.Number, 0)
txes = append([]*types.Transaction{types.NewTx(tx)}, txes...)
}
complete := types.Transactions{}
receipts := types.Receipts{}
gasPrice := header.BaseFee
time := header.Time
expectedBalanceDelta := new(big.Int)
redeems := types.Transactions{}
// We'll check that the block can fit each message, so this pool is set to not run out
gethGas := core.GasPool(1 << 63)
for len(txes) > 0 || len(redeems) > 0 {
// repeatedly process the next tx, doing redeems created along the way in FIFO order
var tx *types.Transaction
if len(redeems) > 0 {
tx = redeems[0]
redeems = redeems[1:]
retry, ok := (tx.GetInner()).(*types.ArbitrumRetryTx)
if !ok {
panic("retryable tx is somehow not a retryable")
}
retryable, _ := state.RetryableState().OpenRetryable(retry.TicketId, time)
if retryable == nil {
// retryable was already deleted
continue
}
} else {
tx = txes[0]
txes = txes[1:]
}
sender, err := signer.Sender(tx)
if err != nil {
continue
}
aggregator := &poster
var dataGas uint64 = 0
if gasPrice.Sign() > 0 {
dataGas = math.MaxUint64
pricing := state.L1PricingState()
posterCost, _ := pricing.PosterDataCost(sender, aggregator, tx.Data())
posterCostInL2Gas := new(big.Int).Div(posterCost, gasPrice)
if posterCostInL2Gas.IsUint64() {
dataGas = posterCostInL2Gas.Uint64()
} else {
log.Error("Could not get poster cost in L2 terms", posterCost, gasPrice)
}
}
if dataGas > tx.Gas() {
// this txn is going to be rejected later
dataGas = 0
}
computeGas := tx.Gas() - dataGas
if computeGas > gasLeft {
continue
}
snap := statedb.Snapshot()
statedb.Prepare(tx.Hash(), len(receipts)) // the number of successful state transitions
gasLeft -= computeGas
gasPool := gethGas
receipt, err := core.ApplyTransaction(
chainConfig,
chainContext,
&header.Coinbase,
&gasPool,
statedb,
header,
tx,
&header.GasUsed,
vm.Config{},
)
if err != nil {
// Ignore this transaction if it's invalid under our more lenient state transaction function
statedb.RevertToSnapshot(snap)
continue
}
// Update expectedTotalBalanceDelta (also done in logs loop)
switch txInner := tx.GetInner().(type) {
case *types.ArbitrumDepositTx:
// L1->L2 deposits add eth to the system
expectedBalanceDelta.Add(expectedBalanceDelta, txInner.Value)
case *types.ArbitrumSubmitRetryableTx:
// Retryable submission can include a deposit which adds eth to the system
expectedBalanceDelta.Add(expectedBalanceDelta, txInner.DepositValue)
}
if gasPool > gethGas {
delta := strconv.FormatUint(gasPool.Gas()-gethGas.Gas(), 10)
panic("ApplyTransaction() gave back " + delta + " gas")
}
gasUsed := gethGas.Gas() - gasPool.Gas()
gethGas = gasPool
if gasUsed > computeGas {
delta := strconv.FormatUint(gasUsed-computeGas, 10)
panic("ApplyTransaction() used " + delta + " more gas than it should have")
}
for _, txLog := range receipt.Logs {
if txLog.Address == ArbRetryableTxAddress && txLog.Topics[0] == RedeemScheduledEventID {
event := &precompilesgen.ArbRetryableTxRedeemScheduled{}
err := util.ParseRedeemScheduledLog(event, txLog)
if err != nil {
log.Error("Failed to parse RedeemScheduled log", "err", err)
} else {
retryable, _ := state.RetryableState().OpenRetryable(event.TicketId, time)
redeem, _ := retryable.MakeTx(
chainConfig.ChainID,
event.SequenceNum,
gasPrice,
event.DonatedGas,
event.TicketId,
event.GasDonor,
)
redeems = append(redeems, types.NewTx(redeem))
}
} else if txLog.Address == ArbSysAddress && txLog.Topics[0] == L2ToL1TransactionEventID {
// L2->L1 withdrawals remove eth from the system
event := &precompilesgen.ArbSysL2ToL1Transaction{}
err := util.ParseL2ToL1TransactionLog(event, txLog)
if err != nil {
log.Error("Failed to parse L2ToL1Transaction log", "err", err)
} else {
expectedBalanceDelta.Sub(expectedBalanceDelta, event.Callvalue)
}
}
}
complete = append(complete, tx)
receipts = append(receipts, receipt)
gasLeft -= gasUsed
}
binary.BigEndian.PutUint64(header.Nonce[:], delayedMessagesRead)
header.Root = statedb.IntermediateRoot(true)
// Touch up the block hashes in receipts
tmpBlock := types.NewBlock(header, complete, nil, receipts, trie.NewStackTrie(nil))
blockHash := tmpBlock.Hash()
for _, receipt := range receipts {
receipt.BlockHash = blockHash
for _, txLog := range receipt.Logs {
txLog.BlockHash = blockHash
}
}
FinalizeBlock(header, complete, receipts, statedb)
header.Root = statedb.IntermediateRoot(true)
block := types.NewBlock(header, complete, nil, receipts, trie.NewStackTrie(nil))
if len(block.Transactions()) != len(receipts) {
panic(fmt.Sprintf("Block has %d txes but %d receipts", len(block.Transactions()), len(receipts)))
}
balanceDelta := statedb.GetTotalBalanceDelta()
if balanceDelta.Cmp(expectedBalanceDelta) != 0 {
// Panic if funds have been minted or debug mode is enabled (i.e. this is a test)
if balanceDelta.Cmp(expectedBalanceDelta) > 0 || chainConfig.DebugMode() {
panic(fmt.Sprintf("Unexpected total balance delta %v (expected %v)", balanceDelta, expectedBalanceDelta))
} else {
// This is a real chain and funds were burnt, not minted, so only log an error and don't panic
log.Error("Unexpected total balance delta", "delta", balanceDelta, "expected", expectedBalanceDelta)
}
}
return block, receipts
}
type ArbitrumHeaderInfo struct {
SendRoot common.Hash
SendCount uint64
}
func (info ArbitrumHeaderInfo) Extra() []byte {
return info.SendRoot[:]
}
func (info ArbitrumHeaderInfo) MixDigest() [32]byte {
mixDigest := common.Hash{}
binary.BigEndian.PutUint64(mixDigest[:8], info.SendCount)
return mixDigest
}
func DeserializeHeaderExtraInformation(header *types.Header) (ArbitrumHeaderInfo, error) {
if header.Number.Sign() == 0 || len(header.Extra) == 0 {
// The genesis block doesn't have an ArbOS encoded extra field
return ArbitrumHeaderInfo{}, nil
}
if len(header.Extra) != 32 {
return ArbitrumHeaderInfo{}, fmt.Errorf("unexpected header extra field length %v", len(header.Extra))
}
extra := ArbitrumHeaderInfo{}
copy(extra.SendRoot[:], header.Extra)
extra.SendCount = binary.BigEndian.Uint64(header.MixDigest[:8])
return extra, nil
}
func FinalizeBlock(header *types.Header, txs types.Transactions, receipts types.Receipts, statedb *state.StateDB) {
if header != nil {
state, err := arbosState.OpenSystemArbosState(statedb, false)
if err != nil {
panic(err)
}
state.SetLastTimestampSeen(header.Time)
_ = state.RetryableState().TryToReapOneRetryable(header.Time)
maxSafePrice := new(big.Int).Mul(header.BaseFee, big.NewInt(2))
state.L2PricingState().SetMaxGasPriceWei(maxSafePrice)
// Add outbox info to the header for client-side proving
acc := state.SendMerkleAccumulator()
root, _ := acc.Root()
size, _ := acc.Size()
arbitrumHeader := ArbitrumHeaderInfo{root, size}
header.Extra = arbitrumHeader.Extra()
header.MixDigest = arbitrumHeader.MixDigest()
state.UpgradeArbosVersionIfNecessary(header.Time)
}
}