Skip to content

Commit

Permalink
all: removed blockhash from statedb (ethereum#23126)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan committed Sep 27, 2024
1 parent 47d27fe commit d0e1908
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 84 deletions.
2 changes: 1 addition & 1 deletion consensus/tests/engine_v1_tests/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ func createBlockFromHeader(bc *BlockChain, customHeader *types.Header, txs []*ty
var gasUsed = new(uint64)
var receipts types.Receipts
for i, tx := range txs {
statedb.Prepare(tx.Hash(), header.Hash(), i)
statedb.Prepare(tx.Hash(), i)
receipt, _, err, _ := ApplyTransaction(bc.Config(), nil, bc, &header.Coinbase, gp, statedb, nil, &header, tx, gasUsed, vm.Config{})
if err != nil {
return nil, fmt.Errorf("%v when applying transaction", err)
Expand Down
2 changes: 1 addition & 1 deletion consensus/tests/engine_v2_tests/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ func createBlockFromHeader(bc *BlockChain, customHeader *types.Header, txs []*ty
var gasUsed = new(uint64)
var receipts types.Receipts
for i, tx := range txs {
statedb.Prepare(tx.Hash(), header.Hash(), i)
statedb.Prepare(tx.Hash(), i)
receipt, _, err, _ := ApplyTransaction(bc.Config(), nil, bc, &header.Coinbase, gp, statedb, nil, &header, tx, gasUsed, vm.Config{})
if err != nil {
return nil, fmt.Errorf("%v when applying transaction", err)
Expand Down
2 changes: 1 addition & 1 deletion core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (b *BlockGen) AddTxWithChain(bc *BlockChain, tx *types.Transaction) {
b.SetCoinbase(common.Address{})
}
feeCapacity := state.GetTRC21FeeCapacityFromState(b.statedb)
b.statedb.Prepare(tx.Hash(), common.Hash{}, len(b.txs))
b.statedb.Prepare(tx.Hash(), len(b.txs))
receipt, gas, err, tokenFeeUsed := ApplyTransaction(b.config, feeCapacity, bc, &b.header.Coinbase, b.gasPool, b.statedb, nil, b.header, tx, &b.header.GasUsed, vm.Config{})
if err != nil {
panic(err)
Expand Down
32 changes: 14 additions & 18 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ type StateDB struct {
// The refund counter, also used by state transitioning.
refund uint64

thash, bhash common.Hash
txIndex int
logs map[common.Hash][]*types.Log
logSize uint
thash common.Hash
txIndex int
logs map[common.Hash][]*types.Log
logSize uint

preimages map[common.Hash][]byte

Expand Down Expand Up @@ -150,7 +150,6 @@ func (self *StateDB) Reset(root common.Hash) error {
self.stateObjects = make(map[common.Address]*stateObject)
self.stateObjectsDirty = make(map[common.Address]struct{})
self.thash = common.Hash{}
self.bhash = common.Hash{}
self.txIndex = 0
self.logs = make(map[common.Hash][]*types.Log)
self.logSize = 0
Expand All @@ -164,15 +163,18 @@ func (self *StateDB) AddLog(log *types.Log) {
self.journal = append(self.journal, addLogChange{txhash: self.thash})

log.TxHash = self.thash
log.BlockHash = self.bhash
log.TxIndex = uint(self.txIndex)
log.Index = self.logSize
self.logs[self.thash] = append(self.logs[self.thash], log)
self.logSize++
}

func (self *StateDB) GetLogs(hash common.Hash) []*types.Log {
return self.logs[hash]
func (s *StateDB) GetLogs(hash common.Hash, blockHash common.Hash) []*types.Log {
logs := s.logs[hash]
for _, l := range logs {
l.BlockHash = blockHash
}
return logs
}

func (self *StateDB) Logs() []*types.Log {
Expand Down Expand Up @@ -249,11 +251,6 @@ func (self *StateDB) TxIndex() int {
return self.txIndex
}

// BlockHash returns the current block hash set by Prepare.
func (self *StateDB) BlockHash() common.Hash {
return self.bhash
}

func (self *StateDB) GetCode(addr common.Address) []byte {
stateObject := self.getStateObject(addr)
if stateObject != nil {
Expand Down Expand Up @@ -651,11 +648,10 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {

// Prepare sets the current transaction hash and index and block hash which is
// used when the EVM emits new state logs.
func (self *StateDB) Prepare(thash, bhash common.Hash, ti int) {
self.thash = thash
self.bhash = bhash
self.txIndex = ti
self.accessList = newAccessList()
func (s *StateDB) Prepare(thash common.Hash, ti int) {
s.thash = thash
s.txIndex = ti
s.accessList = newAccessList()
}

// DeleteSuicides flags the suicided objects for deletion so that it
Expand Down
4 changes: 2 additions & 2 deletions core/state/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,9 @@ func (test *snapshotTest) checkEqual(state, checkstate *StateDB) error {
return fmt.Errorf("got GetRefund() == %d, want GetRefund() == %d",
state.GetRefund(), checkstate.GetRefund())
}
if !reflect.DeepEqual(state.GetLogs(common.Hash{}), checkstate.GetLogs(common.Hash{})) {
if !reflect.DeepEqual(state.GetLogs(common.Hash{}, common.Hash{}), checkstate.GetLogs(common.Hash{}, common.Hash{})) {
return fmt.Errorf("got GetLogs(common.Hash{}) == %v, want GetLogs(common.Hash{}) == %v",
state.GetLogs(common.Hash{}), checkstate.GetLogs(common.Hash{}))
state.GetLogs(common.Hash{}, common.Hash{}), checkstate.GetLogs(common.Hash{}, common.Hash{}))
}
return nil
}
Expand Down
117 changes: 61 additions & 56 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,19 @@ func NewStateProcessor(config *params.ChainConfig, bc *BlockChain, engine consen
// transactions failed to execute due to insufficient gas it will return an error.
func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, tradingState *tradingstate.TradingStateDB, cfg vm.Config, balanceFee map[common.Address]*big.Int) (types.Receipts, []*types.Log, uint64, error) {
var (
receipts types.Receipts
usedGas = new(uint64)
header = block.Header()
allLogs []*types.Log
gp = new(GasPool).AddGas(block.GasLimit())
receipts types.Receipts
usedGas = new(uint64)
header = block.Header()
blockHash = block.Hash()
blockNumber = block.Number()
allLogs []*types.Log
gp = new(GasPool).AddGas(block.GasLimit())
)
// Mutate the the block and state according to any hard-fork specs
if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 {
misc.ApplyDAOHardFork(statedb)
}
if common.TIPSigning.Cmp(header.Number) == 0 {
if common.TIPSigning.Cmp(blockNumber) == 0 {
statedb.DeleteAddress(common.BlockSignersBinary)
}
parentState := statedb.Copy()
Expand All @@ -94,11 +96,11 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, tra
if (block.Number().Uint64() >= common.BlackListHFNumber) && !common.IsTestnet {
// check if sender is in black list
if tx.From() != nil && common.Blacklist[*tx.From()] {
return nil, nil, 0, fmt.Errorf("Block contains transaction with sender in black-list: %v", tx.From().Hex())
return nil, nil, 0, fmt.Errorf("block contains transaction with sender in black-list: %v", tx.From().Hex())
}
// check if receiver is in black list
if tx.To() != nil && common.Blacklist[*tx.To()] {
return nil, nil, 0, fmt.Errorf("Block contains transaction with receiver in black-list: %v", tx.To().Hex())
return nil, nil, 0, fmt.Errorf("block contains transaction with receiver in black-list: %v", tx.To().Hex())
}
}
// validate minFee slot for XDCZ
Expand All @@ -115,8 +117,8 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, tra
return nil, nil, 0, err
}
}
statedb.Prepare(tx.Hash(), block.Hash(), i)
receipt, gas, err, tokenFeeUsed := applyTransaction(p.config, balanceFee, p.bc, nil, gp, statedb, tradingState, header, tx, usedGas, vmenv)
statedb.Prepare(tx.Hash(), i)
receipt, gas, err, tokenFeeUsed := applyTransaction(p.config, balanceFee, p.bc, nil, gp, statedb, header, blockNumber, blockHash, tx, usedGas, vmenv)
if err != nil {
return nil, nil, 0, err
}
Expand All @@ -138,17 +140,19 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, tra
func (p *StateProcessor) ProcessBlockNoValidator(cBlock *CalculatedBlock, statedb *state.StateDB, tradingState *tradingstate.TradingStateDB, cfg vm.Config, balanceFee map[common.Address]*big.Int) (types.Receipts, []*types.Log, uint64, error) {
block := cBlock.block
var (
receipts types.Receipts
usedGas = new(uint64)
header = block.Header()
allLogs []*types.Log
gp = new(GasPool).AddGas(block.GasLimit())
receipts types.Receipts
usedGas = new(uint64)
header = block.Header()
blockHash = block.Hash()
blockNumber = block.Number()
allLogs []*types.Log
gp = new(GasPool).AddGas(block.GasLimit())
)
// Mutate the the block and state according to any hard-fork specs
if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 {
misc.ApplyDAOHardFork(statedb)
}
if common.TIPSigning.Cmp(header.Number) == 0 {
if common.TIPSigning.Cmp(blockNumber) == 0 {
statedb.DeleteAddress(common.BlockSignersBinary)
}
if cBlock.stop {
Expand Down Expand Up @@ -192,8 +196,8 @@ func (p *StateProcessor) ProcessBlockNoValidator(cBlock *CalculatedBlock, stated
return nil, nil, 0, err
}
}
statedb.Prepare(tx.Hash(), block.Hash(), i)
receipt, gas, err, tokenFeeUsed := applyTransaction(p.config, balanceFee, p.bc, nil, gp, statedb, tradingState, header, tx, usedGas, vmenv)
statedb.Prepare(tx.Hash(), i)
receipt, gas, err, tokenFeeUsed := applyTransaction(p.config, balanceFee, p.bc, nil, gp, statedb, header, blockNumber, blockHash, tx, usedGas, vmenv)
if err != nil {
return nil, nil, 0, err
}
Expand All @@ -215,23 +219,24 @@ func (p *StateProcessor) ProcessBlockNoValidator(cBlock *CalculatedBlock, stated
return receipts, allLogs, *usedGas, nil
}

func applyTransaction(config *params.ChainConfig, tokensFee map[common.Address]*big.Int, bc *BlockChain, author *common.Address, gp *GasPool, statedb *state.StateDB, XDCxState *tradingstate.TradingStateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (*types.Receipt, uint64, error, bool) {
func applyTransaction(config *params.ChainConfig, tokensFee map[common.Address]*big.Int, bc *BlockChain, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (*types.Receipt, uint64, error, bool) {
to := tx.To()
if to != nil && *to == common.BlockSignersBinary && config.IsTIPSigning(header.Number) {
return ApplySignTransaction(config, statedb, header, tx, usedGas)
}
if to != nil && *to == common.TradingStateAddrBinary && config.IsTIPXDCXReceiver(header.Number) {
return ApplyEmptyTransaction(config, statedb, header, tx, usedGas)
}
if to != nil && *to == common.XDCXLendingAddressBinary && config.IsTIPXDCXReceiver(header.Number) {
return ApplyEmptyTransaction(config, statedb, header, tx, usedGas)
if to != nil {
if *to == common.BlockSignersBinary && config.IsTIPSigning(blockNumber) {
return ApplySignTransaction(config, statedb, blockNumber, blockHash, tx, usedGas)
}
if *to == common.TradingStateAddrBinary && config.IsTIPXDCXReceiver(blockNumber) {
return ApplyEmptyTransaction(config, statedb, blockNumber, blockHash, tx, usedGas)
}
if *to == common.XDCXLendingAddressBinary && config.IsTIPXDCXReceiver(blockNumber) {
return ApplyEmptyTransaction(config, statedb, blockNumber, blockHash, tx, usedGas)
}
}
if tx.IsTradingTransaction() && config.IsTIPXDCXReceiver(header.Number) {
return ApplyEmptyTransaction(config, statedb, header, tx, usedGas)
if tx.IsTradingTransaction() && config.IsTIPXDCXReceiver(blockNumber) {
return ApplyEmptyTransaction(config, statedb, blockNumber, blockHash, tx, usedGas)
}

if tx.IsLendingFinalizedTradeTransaction() && config.IsTIPXDCXReceiver(header.Number) {
return ApplyEmptyTransaction(config, statedb, header, tx, usedGas)
if tx.IsLendingFinalizedTradeTransaction() && config.IsTIPXDCXReceiver(blockNumber) {
return ApplyEmptyTransaction(config, statedb, blockNumber, blockHash, tx, usedGas)
}

var balanceFee *big.Int
Expand All @@ -240,7 +245,7 @@ func applyTransaction(config *params.ChainConfig, tokensFee map[common.Address]*
balanceFee = value
}
}
msg, err := tx.AsMessage(types.MakeSigner(config, header.Number), balanceFee, header.Number)
msg, err := tx.AsMessage(types.MakeSigner(config, blockNumber), balanceFee, blockNumber)
if err != nil {
return nil, 0, err, false
}
Expand All @@ -263,7 +268,7 @@ func applyTransaction(config *params.ChainConfig, tokensFee map[common.Address]*

// Bypass blacklist address
maxBlockNumber := new(big.Int).SetInt64(9147459)
if header.Number.Cmp(maxBlockNumber) <= 0 {
if blockNumber.Cmp(maxBlockNumber) <= 0 {
addrMap := make(map[string]string)
addrMap["0x5248bfb72fd4f234e062d3e9bb76f08643004fcd"] = "29410"
addrMap["0x5ac26105b35ea8935be382863a70281ec7a985e9"] = "23551"
Expand Down Expand Up @@ -391,7 +396,7 @@ func applyTransaction(config *params.ChainConfig, tokensFee map[common.Address]*

addrFrom := msg.From().Hex()

currentBlockNumber := header.Number.Int64()
currentBlockNumber := blockNumber.Int64()
if addr, ok := blockMap[currentBlockNumber]; ok {
if strings.ToLower(addr) == strings.ToLower(addrFrom) {
bal := addrMap[addr]
Expand All @@ -414,10 +419,10 @@ func applyTransaction(config *params.ChainConfig, tokensFee map[common.Address]*

// Update the state with pending changes.
var root []byte
if config.IsByzantium(header.Number) {
if config.IsByzantium(blockNumber) {
statedb.Finalise(true)
} else {
root = statedb.IntermediateRoot(config.IsEIP158(header.Number)).Bytes()
root = statedb.IntermediateRoot(config.IsEIP158(blockNumber)).Bytes()
}
*usedGas += gas

Expand All @@ -438,10 +443,10 @@ func applyTransaction(config *params.ChainConfig, tokensFee map[common.Address]*
}

// Set the receipt logs and create the bloom filter.
receipt.Logs = statedb.GetLogs(tx.Hash())
receipt.Logs = statedb.GetLogs(tx.Hash(), blockHash)
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
receipt.BlockHash = statedb.BlockHash()
receipt.BlockNumber = header.Number
receipt.BlockHash = blockHash
receipt.BlockNumber = blockNumber
receipt.TransactionIndex = uint(statedb.TxIndex())
if balanceFee != nil && failed {
state.PayFeeWithTRC21TxFail(statedb, msg.From(), *to)
Expand All @@ -457,18 +462,18 @@ func ApplyTransaction(config *params.ChainConfig, tokensFee map[common.Address]*
// Create a new context to be used in the EVM environment
blockContext := NewEVMBlockContext(header, bc, author)
vmenv := vm.NewEVM(blockContext, vm.TxContext{}, statedb, XDCxState, config, cfg)
return applyTransaction(config, tokensFee, bc, author, gp, statedb, XDCxState, header, tx , usedGas, vmenv)
return applyTransaction(config, tokensFee, bc, author, gp, statedb, header, header.Number, header.Hash(), tx, usedGas, vmenv)
}

func ApplySignTransaction(config *params.ChainConfig, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64) (*types.Receipt, uint64, error, bool) {
func ApplySignTransaction(config *params.ChainConfig, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64) (*types.Receipt, uint64, error, bool) {
// Update the state with pending changes
var root []byte
if config.IsByzantium(header.Number) {
if config.IsByzantium(blockNumber) {
statedb.Finalise(true)
} else {
root = statedb.IntermediateRoot(config.IsEIP158(header.Number)).Bytes()
root = statedb.IntermediateRoot(config.IsEIP158(blockNumber)).Bytes()
}
from, err := types.Sender(types.MakeSigner(config, header.Number), tx)
from, err := types.Sender(types.MakeSigner(config, blockNumber), tx)
if err != nil {
return nil, 0, err, false
}
Expand All @@ -488,23 +493,23 @@ func ApplySignTransaction(config *params.ChainConfig, statedb *state.StateDB, he
// Set the receipt logs and create a bloom for filtering
log := &types.Log{}
log.Address = common.BlockSignersBinary
log.BlockNumber = header.Number.Uint64()
log.BlockNumber = blockNumber.Uint64()
statedb.AddLog(log)
receipt.Logs = statedb.GetLogs(tx.Hash())
receipt.Logs = statedb.GetLogs(tx.Hash(), blockHash)
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
receipt.BlockHash = statedb.BlockHash()
receipt.BlockNumber = header.Number
receipt.BlockHash = blockHash
receipt.BlockNumber = blockNumber
receipt.TransactionIndex = uint(statedb.TxIndex())
return receipt, 0, nil, false
}

func ApplyEmptyTransaction(config *params.ChainConfig, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64) (*types.Receipt, uint64, error, bool) {
func ApplyEmptyTransaction(config *params.ChainConfig, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64) (*types.Receipt, uint64, error, bool) {
// Update the state with pending changes
var root []byte
if config.IsByzantium(header.Number) {
if config.IsByzantium(blockNumber) {
statedb.Finalise(true)
} else {
root = statedb.IntermediateRoot(config.IsEIP158(header.Number)).Bytes()
root = statedb.IntermediateRoot(config.IsEIP158(blockNumber)).Bytes()
}
// Create a new receipt for the transaction, storing the intermediate root and gas used by the tx
// based on the eip phase, we're passing wether the root touch-delete accounts.
Expand All @@ -515,12 +520,12 @@ func ApplyEmptyTransaction(config *params.ChainConfig, statedb *state.StateDB, h
// Set the receipt logs and create a bloom for filtering
log := &types.Log{}
log.Address = *tx.To()
log.BlockNumber = header.Number.Uint64()
log.BlockNumber = blockNumber.Uint64()
statedb.AddLog(log)
receipt.Logs = statedb.GetLogs(tx.Hash())
receipt.Logs = statedb.GetLogs(tx.Hash(), blockHash)
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
receipt.BlockHash = statedb.BlockHash()
receipt.BlockNumber = header.Number
receipt.BlockHash = blockHash
receipt.BlockNumber = blockNumber
receipt.TransactionIndex = uint(statedb.TxIndex())
return receipt, 0, nil, false
}
Expand Down
6 changes: 3 additions & 3 deletions eth/api_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ func (api *PrivateDebugAPI) traceBlock(ctx context.Context, block *types.Block,
// Generate the next state snapshot fast without tracing
msg, _ := tx.AsMessage(signer, balacne, block.Number())
txContext := core.NewEVMTxContext(msg)
statedb.Prepare(tx.Hash(), block.Hash(), i)
statedb.Prepare(tx.Hash(), i)

vmenv := vm.NewEVM(blockCtx, txContext, statedb, XDCxState, api.config, vm.Config{})
owner := common.Address{}
Expand Down Expand Up @@ -740,7 +740,7 @@ func (api *PrivateDebugAPI) traceTx(ctx context.Context, message core.Message, t
vmenv := vm.NewEVM(vmctx, txContext, statedb, nil, api.config, vm.Config{Debug: true, Tracer: tracer})

// Call Prepare to clear out the statedb access list
statedb.Prepare(txctx.TxHash, txctx.BlockHash, txctx.TxIndex)
statedb.Prepare(txctx.TxHash, txctx.TxIndex)

owner := common.Address{}
ret, gas, failed, err, _ := core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.Gas()), owner)
Expand Down Expand Up @@ -792,7 +792,7 @@ func (api *PrivateDebugAPI) computeTxEnv(blockHash common.Hash, txIndex int, ree
usedGas := new(uint64)
// Iterate over and process the individual transactions
for idx, tx := range block.Transactions() {
statedb.Prepare(tx.Hash(), block.Hash(), idx)
statedb.Prepare(tx.Hash(), idx)
if idx == txIndex {
var balanceFee *big.Int
if tx.To() != nil {
Expand Down
Loading

0 comments on commit d0e1908

Please sign in to comment.