-
Notifications
You must be signed in to change notification settings - Fork 47
/
blockhelp.go
424 lines (401 loc) · 15.2 KB
/
blockhelp.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
423
424
// Copyright 2023 The AmazeChain Authors
// This file is part of the AmazeChain library.
//
// The AmazeChain library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The AmazeChain library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
// Package core implements the Ethereum consensus protocol.
package internal
import (
"fmt"
"github.com/amazechain/amc/common/block"
"github.com/amazechain/amc/common/math"
"github.com/amazechain/amc/common/transaction"
"github.com/amazechain/amc/common/types"
"github.com/amazechain/amc/common/u256"
"github.com/amazechain/amc/internal/consensus"
"github.com/amazechain/amc/internal/consensus/misc"
"github.com/amazechain/amc/internal/vm"
"github.com/amazechain/amc/internal/vm/evmtypes"
"github.com/amazechain/amc/modules/state"
"github.com/amazechain/amc/params"
)
//var (
// BlockExecutionTimer = metrics2.GetOrCreateSummary("chain_execution_seconds")
//)
type SyncMode string
const (
TriesInMemory = 128
)
type RejectedTx struct {
Index int `json:"index" gencodec:"required"`
Err string `json:"error" gencodec:"required"`
}
type RejectedTxs []*RejectedTx
type EphemeralExecResult struct {
StateRoot types.Hash `json:"stateRoot"`
TxRoot types.Hash `json:"txRoot"`
ReceiptRoot types.Hash `json:"receiptsRoot"`
LogsHash types.Hash `json:"logsHash"`
Bloom types.Bloom `json:"logsBloom" gencodec:"required"`
Receipts block.Receipts `json:"receipts"`
Rejected RejectedTxs `json:"rejected,omitempty"`
Difficulty *math.HexOrDecimal256 `json:"currentDifficulty" gencodec:"required"`
GasUsed math.HexOrDecimal64 `json:"gasUsed"`
StateSyncReceipt *block.Receipt `json:"-"`
}
// ExecuteBlockEphemerally runs a block from provided stateReader and
// writes the result to the provided stateWriter
//func ExecuteBlockEphemerally(
// chainConfig *params.ChainConfig,
// vmConfig *vm.Config,
// blockHashFunc func(n uint64) types.Hash,
// engine consensus.Engine,
// block *block.Block,
// stateReader state.StateReader,
// stateWriter state.WriterWithChangeSets,
// epochReader consensus.EpochReader,
// chainReader consensus.ChainHeaderReader,
// getTracer func(txIndex int, txHash types.Hash) (vm.Tracer, error),
//) (*EphemeralExecResult, error) {
//
// defer BlockExecutionTimer.UpdateDuration(time.Now())
// block.Uncles()
// ibs := state.New(stateReader)
// header := block.Header()
//
// usedGas := new(uint64)
// gp := new(GasPool)
// gp.AddGas(block.GasLimit())
//
// var (
// rejectedTxs []*RejectedTx
// includedTxs types.Transactions
// receipts types.Receipts
// )
//
// if !vmConfig.ReadOnly {
// if err := InitializeBlockExecution(engine, chainReader, epochReader, block.Header(), block.Transactions(), block.Uncles(), chainConfig, ibs); err != nil {
// return nil, err
// }
// }
//
// if chainConfig.DAOForkSupport && chainConfig.DAOForkBlock != nil && chainConfig.DAOForkBlock.Cmp(block.Number()) == 0 {
// misc.ApplyDAOHardFork(ibs)
// }
// noop := state.NewNoopWriter()
// //fmt.Printf("====txs processing start: %d====\n", block.NumberU64())
// for i, tx := range block.Transactions() {
// ibs.Prepare(tx.Hash(), block.Hash(), i)
// writeTrace := false
// if vmConfig.Debug && vmConfig.Tracer == nil {
// tracer, err := getTracer(i, tx.Hash())
// if err != nil {
// return nil, fmt.Errorf("could not obtain tracer: %w", err)
// }
// vmConfig.Tracer = tracer
// writeTrace = true
// }
//
// receipt, _, err := ApplyTransaction(chainConfig, blockHashFunc, engine, nil, gp, ibs, noop, header, tx, usedGas, *vmConfig)
// if writeTrace {
// if ftracer, ok := vmConfig.Tracer.(vm.FlushableTracer); ok {
// ftracer.Flush(tx)
// }
//
// vmConfig.Tracer = nil
// }
// if err != nil {
// if !vmConfig.StatelessExec {
// return nil, fmt.Errorf("could not apply tx %d from block %d [%v]: %w", i, block.NumberU64(), tx.Hash().Hex(), err)
// }
// rejectedTxs = append(rejectedTxs, &RejectedTx{i, err.Error()})
// } else {
// includedTxs = append(includedTxs, tx)
// if !vmConfig.NoReceipts {
// receipts = append(receipts, receipt)
// }
// }
// }
//
// receiptSha := types.DeriveSha(receipts)
// if !vmConfig.StatelessExec && chainConfig.IsByzantium(header.Number.Uint64()) && !vmConfig.NoReceipts && receiptSha != block.ReceiptHash() {
// return nil, fmt.Errorf("mismatched receipt headers for block %d (%s != %s)", block.NumberU64(), receiptSha.Hex(), block.ReceiptHash().Hex())
// }
//
// if !vmConfig.StatelessExec && *usedGas != header.GasUsed {
// return nil, fmt.Errorf("gas used by execution: %d, in header: %d", *usedGas, header.GasUsed)
// }
//
// var bloom types.Bloom
// if !vmConfig.NoReceipts {
// bloom = types.CreateBloom(receipts)
// if !vmConfig.StatelessExec && bloom != header.Bloom {
// return nil, fmt.Errorf("bloom computed by execution: %x, in header: %x", bloom, header.Bloom)
// }
// }
// if !vmConfig.ReadOnly {
// txs := block.Transactions()
// if _, _, _, err := FinalizeBlockExecution(engine, stateReader, block.Header(), txs, block.Uncles(), stateWriter, chainConfig, ibs, receipts, epochReader, chainReader, false); err != nil {
// return nil, err
// }
// }
// blockLogs := ibs.Logs()
// execRs := &EphemeralExecResult{
// TxRoot: types.DeriveSha(includedTxs),
// ReceiptRoot: receiptSha,
// Bloom: bloom,
// LogsHash: rlpHash(blockLogs),
// Receipts: receipts,
// Difficulty: (*math.HexOrDecimal256)(header.Difficulty),
// GasUsed: math.HexOrDecimal64(*usedGas),
// Rejected: rejectedTxs,
// }
//
// return execRs, nil
//}
// ExecuteBlockEphemerallyBor runs a block from provided stateReader and
// writes the result to the provided stateWriter
//func ExecuteBlockEphemerallyBor(
// chainConfig *params.ChainConfig,
// vmConfig *vm.Config,
// blockHashFunc func(n uint64) types.Hash,
// engine consensus.Engine,
// block *types.Block,
// stateReader state.StateReader,
// stateWriter state.WriterWithChangeSets,
// epochReader consensus.EpochReader,
// chainReader consensus.ChainHeaderReader,
// getTracer func(txIndex int, txHash types.Hash) (vm.Tracer, error),
//) (*EphemeralExecResult, error) {
//
// defer BlockExecutionTimer.UpdateDuration(time.Now())
// block.Uncles()
// ibs := state.New(stateReader)
// header := block.Header()
//
// usedGas := new(uint64)
// gp := new(GasPool)
// gp.AddGas(block.GasLimit())
//
// var (
// rejectedTxs []*RejectedTx
// includedTxs types.Transactions
// receipts types.Receipts
// )
//
// if !vmConfig.ReadOnly {
// if err := InitializeBlockExecution(engine, chainReader, epochReader, block.Header(), block.Transactions(), block.Uncles(), chainConfig, ibs); err != nil {
// return nil, err
// }
// }
//
// if chainConfig.DAOForkSupport && chainConfig.DAOForkBlock != nil && chainConfig.DAOForkBlock.Cmp(block.Number()) == 0 {
// misc.ApplyDAOHardFork(ibs)
// }
// noop := state.NewNoopWriter()
// //fmt.Printf("====txs processing start: %d====\n", block.NumberU64())
// for i, tx := range block.Transactions() {
// ibs.Prepare(tx.Hash(), block.Hash(), i)
// writeTrace := false
// if vmConfig.Debug && vmConfig.Tracer == nil {
// tracer, err := getTracer(i, tx.Hash())
// if err != nil {
// return nil, fmt.Errorf("could not obtain tracer: %w", err)
// }
// vmConfig.Tracer = tracer
// writeTrace = true
// }
//
// receipt, _, err := ApplyTransaction(chainConfig, blockHashFunc, engine, nil, gp, ibs, noop, header, tx, usedGas, *vmConfig)
// if writeTrace {
// if ftracer, ok := vmConfig.Tracer.(vm.FlushableTracer); ok {
// ftracer.Flush(tx)
// }
//
// vmConfig.Tracer = nil
// }
// if err != nil {
// if !vmConfig.StatelessExec {
// return nil, fmt.Errorf("could not apply tx %d from block %d [%v]: %w", i, block.NumberU64(), tx.Hash().Hex(), err)
// }
// rejectedTxs = append(rejectedTxs, &RejectedTx{i, err.Error()})
// } else {
// includedTxs = append(includedTxs, tx)
// if !vmConfig.NoReceipts {
// receipts = append(receipts, receipt)
// }
// }
// }
//
// receiptSha := types.DeriveSha(receipts)
// if !vmConfig.StatelessExec && chainConfig.IsByzantium(header.Number.Uint64()) && !vmConfig.NoReceipts && receiptSha != block.ReceiptHash() {
// return nil, fmt.Errorf("mismatched receipt headers for block %d (%s != %s)", block.NumberU64(), receiptSha.Hex(), block.ReceiptHash().Hex())
// }
//
// if !vmConfig.StatelessExec && *usedGas != header.GasUsed {
// return nil, fmt.Errorf("gas used by execution: %d, in header: %d", *usedGas, header.GasUsed)
// }
//
// var bloom types.Bloom
// if !vmConfig.NoReceipts {
// bloom = types.CreateBloom(receipts)
// if !vmConfig.StatelessExec && bloom != header.Bloom {
// return nil, fmt.Errorf("bloom computed by execution: %x, in header: %x", bloom, header.Bloom)
// }
// }
// if !vmConfig.ReadOnly {
// txs := block.Transactions()
// if _, _, _, err := FinalizeBlockExecution(engine, stateReader, block.Header(), txs, block.Uncles(), stateWriter, chainConfig, ibs, receipts, epochReader, chainReader, false); err != nil {
// return nil, err
// }
// }
//
// var logs []*types.Log
// for _, receipt := range receipts {
// logs = append(logs, receipt.Logs...)
// }
//
// blockLogs := ibs.Logs()
// stateSyncReceipt := &types.Receipt{}
// if chainConfig.Consensus == params.BorConsensus && len(blockLogs) > 0 {
// slices.SortStableFunc(blockLogs, func(i, j *types.Log) bool { return i.Index < j.Index })
//
// if len(blockLogs) > len(logs) {
// stateSyncReceipt.Logs = blockLogs[len(logs):] // get state-sync logs from `state.Logs()`
//
// // fill the state sync with the correct information
// types.DeriveFieldsForBorReceipt(stateSyncReceipt, block.Hash(), block.NumberU64(), receipts)
// stateSyncReceipt.Status = types.ReceiptStatusSuccessful
// }
// }
//
// execRs := &EphemeralExecResult{
// TxRoot: types.DeriveSha(includedTxs),
// ReceiptRoot: receiptSha,
// Bloom: bloom,
// LogsHash: rlpHash(blockLogs),
// Receipts: receipts,
// Difficulty: (*math.HexOrDecimal256)(header.Difficulty),
// GasUsed: math.HexOrDecimal64(*usedGas),
// Rejected: rejectedTxs,
// StateSyncReceipt: stateSyncReceipt,
// }
//
// return execRs, nil
//}
func SysCallContract(contract types.Address, data []byte, chainConfig params.ChainConfig, ibs *state.IntraBlockState, header *block.Header, engine consensus.Engine) (result []byte, err error) {
if chainConfig.DAOForkSupport && chainConfig.DAOForkBlock != nil && chainConfig.DAOForkBlock.Cmp(header.Number64().ToBig()) == 0 {
misc.ApplyDAOHardFork(ibs)
}
msg := transaction.NewMessage(
state.SystemAddress,
&contract,
0, u256.Num0,
math.MaxUint64, u256.Num0,
nil, nil,
data, nil, false,
true, // isFree
)
vmConfig := vm.Config{NoReceipts: true}
// Create a new context to be used in the EVM environment
isBor := chainConfig.Bor != nil
var txContext evmtypes.TxContext
var author *types.Address
if isBor {
author = &header.Coinbase
txContext = evmtypes.TxContext{}
} else {
author = &state.SystemAddress
txContext = NewEVMTxContext(msg)
}
blockContext := NewEVMBlockContext(header, GetHashFn(header, nil), engine, author)
evm := vm.NewEVM(blockContext, txContext, ibs, &chainConfig, vmConfig)
ret, _, err := evm.Call(
vm.AccountRef(msg.From()),
*msg.To(),
msg.Data(),
msg.Gas(),
msg.Value(),
false,
)
if isBor && err != nil {
return nil, nil
}
return ret, err
}
// from the null sender, with 50M gas.
//func SysCallContractTx(contract types.Address, data []byte) (tx transaction.Transaction, err error) {
// //nonce := ibs.GetNonce(SystemAddress)
// tx = transaction.NewTransaction(0, contract, u256.Num0, 50_000_000, u256.Num0, data)
// return tx.FakeSign(state.SystemAddress)
//}
//func CallContract(contract types.Address, data []byte, chainConfig params.ChainConfig, ibs *state.IntraBlockState, header *block.Header, engine consensus.Engine) (result []byte, err error) {
// gp := new(common.GasPool)
// gp.AddGas(50_000_000)
// var gasUsed uint64
//
// if chainConfig.DAOForkSupport && chainConfig.DAOForkBlock != nil && chainConfig.DAOForkBlock.Cmp(header.Number64().ToBig()) == 0 {
// misc.ApplyDAOHardFork(ibs)
// }
// noop := state.NewNoopWriter()
// tx, err := CallContractTx(contract, data, ibs)
// if err != nil {
// return nil, fmt.Errorf("SysCallContract: %w ", err)
// }
// vmConfig := vm.Config{NoReceipts: true}
// _, result, err = ApplyTransaction(&chainConfig, GetHashFn(header, nil), engine, &state.SystemAddress, gp, ibs, noop, header, tx, &gasUsed, vmConfig)
// if err != nil {
// return result, fmt.Errorf("SysCallContract: %w ", err)
// }
// return result, nil
//}
//// from the null sender, with 50M gas.
//func CallContractTx(contract types.Address, data []byte, ibs *state.IntraBlockState) (tx transaction.Transaction, err error) {
// from := types.Address{}
// nonce := ibs.GetNonce(from)
// tx = transaction.NewTransaction(nonce, contract, u256.Num0, 50_000_000, u256.Num0, data)
// return tx.FakeSign(from)
//}
func FinalizeBlockExecution(engine consensus.Engine, header *block.Header,
txs transaction.Transactions, stateWriter state.WriterWithChangeSets, cc *params.ChainConfig, ibs *state.IntraBlockState,
receipts block.Receipts, headerReader consensus.ChainHeaderReader, isMining bool) (newBlock block.IBlock, newTxs transaction.Transactions, newReceipt block.Receipts, err error) {
//syscall := func(contract types.Address, data []byte) ([]byte, error) {
// return SysCallContract(contract, data, *cc, ibs, header, engine)
//}
if isMining {
newBlock, _, _, err = engine.FinalizeAndAssemble(headerReader, header, ibs, txs, nil, receipts)
} else {
_, _, err = engine.Finalize(headerReader, header, ibs, txs, nil)
}
if err != nil {
return nil, nil, nil, err
}
if err := ibs.CommitBlock(cc.Rules(header.Number.Uint64()), stateWriter); err != nil {
return nil, nil, nil, fmt.Errorf("committing block %d failed: %w", header.Number.Uint64(), err)
}
if err := stateWriter.WriteChangeSets(); err != nil {
return nil, nil, nil, fmt.Errorf("writing changesets for block %d failed: %w", header.Number.Uint64(), err)
}
if err := stateWriter.WriteHistory(); err != nil {
return nil, nil, nil, fmt.Errorf("writing history for block %d failed: %w", header.Number.Uint64(), err)
}
return newBlock, newTxs, newReceipt, nil
}
//func InitializeBlockExecution(engine consensus.Engine, chain consensus.ChainHeaderReader, header *block.Header, txs transaction.Transactions, uncles []*block.Header, cc *params.ChainConfig, ibs *state.IntraBlockState) error {
// //engine.Initialize(cc, chain, header, txs, uncles, func(contract types.Address, data []byte) ([]byte, error) {
// // return SysCallContract(contract, data, *cc, ibs, header, engine)
// //})
// noop := state.NewNoopWriter()
// ibs.FinalizeTx(cc.Rules(header.Number.Uint64()), noop)
// return nil
//}