-
Notifications
You must be signed in to change notification settings - Fork 99
/
prover.go
422 lines (390 loc) · 13.8 KB
/
prover.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Copyright 2022, Specular contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package proof
import (
"context"
"encoding/json"
"fmt"
"math/big"
"github.com/mantlenetworkio/mantle/fraud-proof/proof/proof"
"github.com/mantlenetworkio/mantle/fraud-proof/proof/prover"
proofState "github.com/mantlenetworkio/mantle/fraud-proof/proof/state"
"github.com/mantlenetworkio/mantle/l2geth/common"
"github.com/mantlenetworkio/mantle/l2geth/core"
"github.com/mantlenetworkio/mantle/l2geth/core/state"
"github.com/mantlenetworkio/mantle/l2geth/core/types"
"github.com/mantlenetworkio/mantle/l2geth/core/vm"
"github.com/mantlenetworkio/mantle/l2geth/log"
"github.com/mantlenetworkio/mantle/l2geth/rpc"
)
const (
// defaultProveReexec is the number of blocks the prover is willing to go back
// and reexecute to produce missing historical state necessary to run a specific
// trace.
defaultProveReexec = uint64(128)
)
type ProverConfig struct {
Reexec *uint64
}
type ExecutionState struct {
VMHash common.Hash
BlockGasUsed *big.Int
StateType proofState.StateType
Block *types.Block
TransactionIdx uint64
StepIdx uint64
}
func (s *ExecutionState) MarshalJson() ([]byte, error) {
return json.Marshal(&struct {
VMHash common.Hash `json:"vmHash"`
BlockGasUsed *big.Int `json:"blockGasUsed"`
StateType string `json:"stateType"`
BlockHash common.Hash `json:"blockHash"`
TransactionIdx uint64 `json:"txnIdx"`
StepIdx uint64 `json:"stepIdx"`
}{
VMHash: s.VMHash,
BlockGasUsed: s.BlockGasUsed,
StateType: string(s.StateType),
BlockHash: s.Block.Hash(),
TransactionIdx: s.TransactionIdx,
StepIdx: s.StepIdx,
})
}
func (s *ExecutionState) Hash() common.Hash {
return s.VMHash
}
// [GenerateStates] generates execution states across blocks [startNum, endNum)
//
// For example there are 2 blocks: block A with transactions A1, A2; block B without transactions.
//
// The states are (in order):
// 1. the BlockState of the block right before A
// 2. the InterState before A1
// 3. the IntraStates of A1 if A1 is not an EOA transfer
// 4. the InterState before A2 (after A1)
// 5. the IntraStates of A2 if A2 is not an EOA transfer
// 6. the InterState after A2
// 7. the BlockState of the block A
// 8. a dummy InterState
// 9. the BlockState of the block B
func GenerateStates(backend Backend, ctx context.Context, startNum, endNum uint64, config *ProverConfig) ([]*ExecutionState, error) {
var states []*ExecutionState
var msg types.Message
var block *types.Block
var err error
chainCtx := createChainContext(backend, ctx)
startParent, err := backend.BlockByNumber(ctx, rpc.BlockNumber(startNum-1))
if err != nil {
return nil, err
}
bs, statedb, err := generateStartBlockState(backend, ctx, startParent, config)
if err != nil {
return nil, err
}
//states = append(states, &ExecutionState{
// VMHash: bs.Hash(),
// BlockGasUsed: common.Big0,
// StateType: proofState.BlockStateType,
// Block: startParent,
// TransactionIdx: 0,
// StepIdx: 0,
//})
// append start vmHash
startHeader, err := backend.HeaderByNumber(ctx, rpc.BlockNumber(startNum))
if err != nil {
return nil, err
}
states = append(states, &ExecutionState{
VMHash: startHeader.Root,
BlockGasUsed: common.Big0,
StateType: proofState.BlockStateType,
Block: startParent,
TransactionIdx: 0,
StepIdx: 0,
})
log.Info("Get start state", "startNum", startNum, "VMHash", startHeader.Root.String())
for num := startNum; num < endNum; num++ {
// Preparation of block context
if block, err = backend.BlockByNumber(ctx, rpc.BlockNumber(num)); err != nil {
return nil, err
}
if block == nil {
return nil, fmt.Errorf("block #%d not found", num)
}
blockCtx := core.NewEVMBlockContext(block.Header(), chainCtx, nil)
blockHashTree, err := proofState.BlockHashTreeFromBlockContext(&blockCtx)
if err != nil {
return nil, err
}
transactions := block.Transactions()
receipts, _ := backend.GetReceipts(ctx, block.Hash())
blockGasUsed := new(big.Int).SetUint64(block.GasUsed())
localBlockGasUsed := new(big.Int)
// Trace all the transactions contained within
for i, tx := range transactions {
// Call Prepare to clear out the statedb access list
statedb.Prepare(tx.Hash(), block.Hash(), i)
// Push the interstate before transaction i
its := proofState.InterStateFromCaptured(
block.NumberU64(),
uint64(i),
statedb,
blockGasUsed,
transactions,
receipts,
blockHashTree,
)
states = append(states, &ExecutionState{
VMHash: its.Hash(),
BlockGasUsed: blockGasUsed,
StateType: proofState.InterStateType,
Block: block,
TransactionIdx: uint64(i),
StepIdx: 0,
})
// Execute transaction i with intra state generator enabled.
stateGenerator := prover.NewIntraStateGenerator(block.NumberU64(), uint64(i), statedb, *its, blockHashTree)
txCtx, err := generateTxCtx(backend, ctx, block, tx)
if err != nil {
return nil, err
}
vmenv := vm.NewEVM(*txCtx, statedb, backend.ChainConfig(), vm.Config{Debug: true, Tracer: stateGenerator})
signer := types.MakeSigner(backend.ChainConfig(), block.Number())
if msg, err = tx.AsMessage(signer); err != nil {
return nil, err
}
_, usedGas, _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas()))
if err != nil {
return nil, fmt.Errorf("tracing failed: %w", err)
}
generatedStates, err := stateGenerator.GetGeneratedStates()
if err != nil {
return nil, fmt.Errorf("tracing failed: %w", err)
}
for idx, s := range generatedStates {
states = append(states, &ExecutionState{
VMHash: s.VMHash,
BlockGasUsed: new(big.Int).Add(blockGasUsed, new(big.Int).SetUint64(tx.Gas()-s.Gas)),
StateType: proofState.IntraStateType,
Block: block,
TransactionIdx: uint64(i),
StepIdx: uint64(idx + 1),
})
}
// Include refund
localBlockGasUsed.Add(localBlockGasUsed, new(big.Int).SetUint64(usedGas))
}
// Push the inter state after all transactions
its := proofState.InterStateFromCaptured(
block.NumberU64(),
uint64(len(transactions)),
statedb,
localBlockGasUsed,
transactions,
receipts,
blockHashTree,
)
states = append(states, &ExecutionState{
VMHash: its.Hash(),
BlockGasUsed: localBlockGasUsed,
StateType: proofState.InterStateType,
Block: block,
TransactionIdx: uint64(len(block.Transactions())),
StepIdx: 0,
})
// Get next statedb to skip simulating block finalization
// Here the statedb is the state at the end of the current block
// (i.e. start of the new block)
if bs, statedb, err = generateStartBlockState(backend, ctx, block, config); err != nil {
return nil, err
}
states = append(states, &ExecutionState{
VMHash: bs.Hash(),
BlockGasUsed: common.Big0,
StateType: proofState.BlockStateType,
Block: block,
TransactionIdx: 0,
StepIdx: 0,
})
}
// append end vmHash
endParent, err := backend.BlockByNumber(ctx, rpc.BlockNumber(endNum-1))
if err != nil {
return nil, err
}
endHeader, err := backend.HeaderByNumber(ctx, rpc.BlockNumber(endNum))
if err != nil {
return nil, err
}
states = append(states, &ExecutionState{
VMHash: endHeader.Root,
BlockGasUsed: common.Big0,
StateType: proofState.BlockStateType,
Block: endParent,
TransactionIdx: 0,
StepIdx: 0,
})
log.Info("Get end state", "endNum", endNum, "VMHash", endHeader.Root.String())
return states, nil
}
// [GenerateProof] serves as an entrypoint for one-step proof generation.
// There are 6 types of one-step proofs:
// 1. BlockState -> InterState: block initiation
// 2. InterState -> IntraState: transaction initiation (contract call or creation)
// 3. InterState -> InterState: EOA transfer transaction
// 4. IntraState -> IntraState: one-step EVM execution (require tracing)
// 5. IntraState -> InterState: transaction finalization (require tracing)
// 6. InterState -> BlockState: block finalization
func GenerateProof(ctx context.Context, backend Backend, startState *ExecutionState, config *ProverConfig) (*proof.OneStepProof, error) {
if startState.Block == nil {
return nil, fmt.Errorf("bad start state")
}
transactions := startState.Block.Transactions()
if startState.TransactionIdx > uint64(len(transactions)) {
return nil, fmt.Errorf("bad start state")
}
reexec := defaultProveReexec
if config != nil && config.Reexec != nil {
reexec = *config.Reexec
}
// Type 1: block initiation or Type 6: block finalization
if startState.StateType == proofState.BlockStateType || (startState.StateType == proofState.InterStateType && startState.TransactionIdx == uint64(len(transactions))) {
statedb, err := backend.StateAtBlock(ctx, startState.Block, reexec, nil, true, false)
if err != nil {
return nil, err
}
chainCtx := createChainContext(backend, ctx)
vmctx := core.NewEVMBlockContext(startState.Block.Header(), chainCtx, nil)
blockHashTree, err := proofState.BlockHashTreeFromBlockContext(&vmctx)
if err != nil {
return nil, err
}
if startState.StateType == proofState.BlockStateType {
// Type 1: block initiation
bs, err := proofState.BlockStateFromBlock(startState.Block.NumberU64(), statedb, blockHashTree)
if err != nil {
return nil, err
}
return proof.GetBlockInitiationProof(bs)
} else {
// Type 6: block finalization
receipts, _ := backend.GetReceipts(ctx, startState.Block.Hash())
its := proofState.InterStateFromCaptured(
startState.Block.NumberU64(),
startState.TransactionIdx,
statedb,
startState.BlockGasUsed,
transactions,
receipts,
blockHashTree,
)
return proof.GetBlockFinalizationProof(its)
}
}
// Prepare block and transaction context
msg, txCtx, statedb, err := backend.StateAtTransaction(ctx, startState.Block, int(startState.TransactionIdx), reexec)
if err != nil {
return nil, err
}
chainCtx := createChainContext(backend, ctx)
vmctx := core.NewEVMBlockContext(startState.Block.Header(), chainCtx, nil)
receipts, err := backend.GetReceipts(ctx, startState.Block.Hash())
if err != nil {
return nil, err
}
blockHashTree, err := proofState.BlockHashTreeFromBlockContext(&vmctx)
if err != nil {
return nil, err
}
// Prepare the inter state before transaction for the prover
its := proofState.InterStateFromCaptured(
startState.Block.NumberU64(),
startState.TransactionIdx,
statedb,
startState.BlockGasUsed,
transactions,
receipts,
blockHashTree,
)
transaction := transactions[startState.TransactionIdx]
if startState.StateType == proofState.InterStateType {
// Type 2: transaction initiation or Type 3: EOA transfer transaction
return proof.GetTransactionInitaitionProof(backend.ChainConfig(), &vmctx, transaction, &txCtx, its, statedb)
}
// Type 4: one-step EVM execution or Type 5: transaction finalization. Both require tracing.
// Set up the prover
prover := prover.NewProver(
startState.VMHash,
startState.StepIdx,
backend.ChainConfig().Rules(vmctx.BlockNumber),
startState.Block.NumberU64(),
startState.TransactionIdx,
statedb,
*its,
blockHashTree,
transaction,
receipts[startState.TransactionIdx],
)
// Run the transaction with prover enabled.
vmenv := vm.NewEVM(txCtx, statedb, backend.ChainConfig(), vm.Config{Debug: true, Tracer: prover})
// Call Prepare to clear out the statedb access list
txHash := transactions[startState.TransactionIdx].Hash()
statedb.Prepare(txHash, startState.Block.Hash(), int(startState.TransactionIdx))
_, _, _, err = core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas()))
if err != nil {
return nil, fmt.Errorf("tracing failed: %w", err)
}
return prover.GetProof()
}
func generateTxCtx(backend Backend, ctx context.Context, block *types.Block, tx *types.Transaction) (*vm.Context, error) {
signer := types.MakeSigner(backend.ChainConfig(), block.Number())
msg, err := tx.AsMessage(signer)
if err != nil {
return nil, err
}
chainCtx := createChainContext(backend, ctx)
blockCtx := core.NewEVMContext(msg, block.Header(), chainCtx, nil)
return &blockCtx, nil
}
func generateStartBlockState(backend Backend, ctx context.Context, startBlock *types.Block, config *ProverConfig) (*proofState.BlockState, *state.StateDB, error) {
reexec := defaultProveReexec
if config != nil && config.Reexec != nil {
reexec = *config.Reexec
}
// The statedb here is the state at the end of the parent blcok
statedb, err := backend.StateAtBlock(ctx, startBlock, reexec, nil, true, false)
if err != nil {
return nil, nil, err
}
chainCtx := createChainContext(backend, ctx)
parentBlockCtx := core.NewEVMBlockContext(startBlock.Header(), chainCtx, nil)
blockHashTree, err := proofState.BlockHashTreeFromBlockContext(&parentBlockCtx)
if err != nil {
return nil, nil, err
}
bs, err := proofState.BlockStateFromBlock(parentBlockCtx.BlockNumber.Uint64(), statedb, blockHashTree)
if err != nil {
return nil, nil, err
}
return bs, statedb, nil
}
func transactionToMessage(tx types.Transaction, backend Backend, startBlockNum *big.Int) (core.Message, error) {
signer := types.MakeSigner(backend.ChainConfig(), startBlockNum)
msg, err := tx.AsMessage(signer)
if err != nil {
return nil, err
}
return msg, nil
}