Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ETX bugfixes: store inboundEtxs before generating pending header, collect outbound etxs in proper order #1816

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (v *BlockValidator) ValidateBody(block *types.WorkObject) error {
// transition, such as amount of used gas, the receipt roots and the state root
// itself. ValidateState returns a database batch if the validation was a success
// otherwise nil and an error is returned.
func (v *BlockValidator) ValidateState(block *types.WorkObject, statedb *state.StateDB, receipts types.Receipts, utxoEtxs []*types.Transaction, usedGas uint64) error {
func (v *BlockValidator) ValidateState(block *types.WorkObject, statedb *state.StateDB, receipts types.Receipts, etxs types.Transactions, usedGas uint64) error {
start := time.Now()
header := types.CopyHeader(block.Header())
time1 := common.PrettyDuration(time.Since(start))
Expand All @@ -139,19 +139,11 @@ func (v *BlockValidator) ValidateState(block *types.WorkObject, statedb *state.S
return fmt.Errorf("invalid etx root (remote: %x local: %x)", header.EtxSetRoot(), root)
}
time5 := common.PrettyDuration(time.Since(start))
// Collect ETXs emitted from each successful transaction
var emittedEtxs types.Transactions
for _, receipt := range receipts {
if receipt.Status == types.ReceiptStatusSuccessful {
emittedEtxs = append(emittedEtxs, receipt.Etxs...)
}
}
emittedEtxs = append(emittedEtxs, utxoEtxs...)
time6 := common.PrettyDuration(time.Since(start))

// Confirm the ETXs emitted by the transactions in this block exactly match the
// ETXs given in the block body
if etxHash := types.DeriveSha(emittedEtxs, trie.NewStackTrie(nil)); etxHash != header.EtxHash() {
if etxHash := types.DeriveSha(etxs, trie.NewStackTrie(nil)); etxHash != header.EtxHash() {
return fmt.Errorf("invalid etx hash (remote: %x local: %x)", header.EtxHash(), etxHash)
}

Expand Down
12 changes: 5 additions & 7 deletions core/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ func (sl *Slice) Append(header *types.WorkObject, domPendingHeader *types.WorkOb
if err != nil {
return nil, false, false, err
}
} else if nodeCtx == common.ZONE_CTX && order < nodeCtx {
// Store the inbound etxs for all dom blocks and use
// it in the future if dom switch happens
// This should be pruned at the re-org tolerance depth
rawdb.WriteInboundEtxs(sl.sliceDb, block.Hash(), newInboundEtxs)
}

// If this was a coincident block, our dom will be passing us a set of newly
Expand Down Expand Up @@ -332,13 +337,6 @@ func (sl *Slice) Append(header *types.WorkObject, domPendingHeader *types.WorkOb

subReorg = sl.miningStrategy(bestPh, tempPendingHeader)

if order < nodeCtx {
// Store the inbound etxs for all dom blocks and use
// it in the future if dom switch happens
// This should be pruned at the re-org tolerance depth
rawdb.WriteInboundEtxs(sl.sliceDb, block.Hash(), newInboundEtxs)
}

setHead = sl.poem(sl.engine.TotalLogS(sl.hc, block), sl.engine.TotalLogS(sl.hc, sl.hc.CurrentHeader()))

if subReorg || (sl.hc.CurrentHeader().NumberU64(nodeCtx) < block.NumberU64(nodeCtx)+c_currentStateComputeWindow) {
Expand Down
15 changes: 10 additions & 5 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func (p *StateProcessor) Process(block *types.WorkObject) (types.Receipts, []*ty
maximumEtxGas := minimumEtxGas * params.MaximumEtxGasMultiplier // 40% of the block gas limit
totalEtxGas := uint64(0)
totalFees := big.NewInt(0)
qiEtxs := make([]*types.Transaction, 0)
emittedEtxs := make([]*types.Transaction, 0)
var totalQiTime time.Duration
for i, tx := range block.Transactions() {
if i == 0 && types.IsCoinBaseTx(tx, header.ParentHash(nodeCtx), nodeLocation) {
Expand All @@ -316,7 +316,7 @@ func (p *StateProcessor) Process(block *types.WorkObject) (types.Receipts, []*ty
return nil, nil, nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err)
}
for _, etx := range etxs {
qiEtxs = append(qiEtxs, types.NewTx(etx))
emittedEtxs = append(emittedEtxs, types.NewTx(etx))
}
totalFees.Add(totalFees, fees)
totalQiTime += time.Since(qiTimeBefore)
Expand Down Expand Up @@ -434,6 +434,11 @@ func (p *StateProcessor) Process(block *types.WorkObject) (types.Receipts, []*ty
} else {
return nil, nil, nil, nil, 0, ErrTxTypeNotSupported
}
for _, etx := range receipt.Etxs {
if receipt.Status == types.ReceiptStatusSuccessful {
emittedEtxs = append(emittedEtxs, etx)
}
}
receipts = append(receipts, receipt)
allLogs = append(allLogs, receipt.Logs...)
i++
Expand Down Expand Up @@ -566,7 +571,7 @@ func (p *StateProcessor) Process(block *types.WorkObject) (types.Receipts, []*ty
"tx time": common.PrettyDuration(timeTx),
}).Info("Total Tx Processing Time")

return receipts, qiEtxs, allLogs, statedb, *usedGas, nil
return receipts, emittedEtxs, allLogs, statedb, *usedGas, nil
}

func applyTransaction(msg types.Message, parent *types.WorkObject, config *params.ChainConfig, bc ChainContext, author *common.Address, gp *types.GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM, etxRLimit, etxPLimit *int, logger *log.Logger) (*types.Receipt, error) {
Expand Down Expand Up @@ -1106,7 +1111,7 @@ func (p *StateProcessor) Apply(batch ethdb.Batch, block *types.WorkObject) ([]*t
time1 := common.PrettyDuration(time.Since(start))
time2 := common.PrettyDuration(time.Since(start))
// Process our block
receipts, utxoEtxs, logs, statedb, usedGas, err := p.Process(block)
receipts, etxs, logs, statedb, usedGas, err := p.Process(block)
if err != nil {
return nil, err
}
Expand All @@ -1117,7 +1122,7 @@ func (p *StateProcessor) Apply(batch ethdb.Batch, block *types.WorkObject) ([]*t
}).Warn("Block hash changed after Processing the block")
}
time3 := common.PrettyDuration(time.Since(start))
err = p.validator.ValidateState(block, statedb, receipts, utxoEtxs, usedGas)
err = p.validator.ValidateState(block, statedb, receipts, etxs, usedGas)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Validator interface {

// ValidateState validates the given statedb and optionally the receipts and
// gas used.
ValidateState(block *types.WorkObject, state *state.StateDB, receipts types.Receipts, utxoEtxs []*types.Transaction, usedGas uint64) error
ValidateState(block *types.WorkObject, state *state.StateDB, receipts types.Receipts, etxs types.Transactions, usedGas uint64) error
}

// Prefetcher is an interface for pre-caching transaction signatures and state.
Expand Down
Loading