Skip to content

Commit

Permalink
Transfer v0.5.1 fixes to upstream (0xPolygon#760)
Browse files Browse the repository at this point in the history
* fix leak in startConsensus

* [Memory fix] Remove memory leak in event subscriptions (0xPolygon#741)

* Remove dangling pointers in the subscription struct

* Remove leftover log

* Fix zero from field in Tx (0xPolygon#719)

* Add e2e test to make sure transaction have set from field

* Recover tx from field before writing block into storage

* Recover tx from field if it's not set in JSON-RPC

* Fix lint error

* Fix lint error

* Fix lint error

* Move recovering from address to blockchain

* Removed unused code

* Fix missing tests

* Fix typo

* Cleanup inactive transactions in non-validator (0xPolygon#742)

* Disable seal configuration but keep CLI flag for sealing in order to remove it in the next minor release

* Add unadopted count in account queue in order to clean up inactive transactions in non-validator's TxPool

* Fix lint error

* Fix lint error

* Fix lint error

* Make Sealing() unexported in TxPool

* Rename unadopted to skips in AccountQueue of TxPool

* Add resetSkips and incrementSkips in AccountQueue

* Fix updateUnadoptedCounts and rename to updateAccountSkipsCounts

* Fixed updateAccountSkipsCounts

* Fix lint error

* Fix lint error occured after merging base branch

* Rename local var in updateAccountSkipsCounts

* [TxPool] Fix double nonce in promoted (0xPolygon#739)

* add test

* Prune lower transactions on promoting

* Fix pruning in Promote

* Fix failed test

Co-authored-by: kourin <kourin.code@gmail.com>

Co-authored-by: dbrajovic <dbrajovic3@gmail.com>
Co-authored-by: Miloš Živković <milos.zivkovic@mvpworkshop.co>
Co-authored-by: kourin <kourin.code@gmail.com>
  • Loading branch information
4 people committed Sep 27, 2022
1 parent 7975703 commit 6b93e54
Show file tree
Hide file tree
Showing 26 changed files with 989 additions and 225 deletions.
67 changes: 65 additions & 2 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Blockchain struct {
db storage.Storage // The Storage object (database)
consensus Verifier
executor Executor
txSigner TxSigner

config *chain.Chain // Config containing chain information
genesis types.Hash // The hash of the genesis block
Expand Down Expand Up @@ -94,6 +95,11 @@ type Executor interface {
ProcessBlock(parentRoot types.Hash, block *types.Block, blockCreator types.Address) (*state.Transition, error)
}

type TxSigner interface {
// Sender returns the sender of the transaction
Sender(tx *types.Transaction) (types.Address, error)
}

type BlockResult struct {
Root types.Hash
Receipts []*types.Receipt
Expand Down Expand Up @@ -185,12 +191,14 @@ func NewBlockchain(
config *chain.Chain,
consensus Verifier,
executor Executor,
txSigner TxSigner,
) (*Blockchain, error) {
b := &Blockchain{
logger: logger.Named("blockchain"),
config: config,
consensus: consensus,
executor: executor,
txSigner: txSigner,
stream: &eventStream{},
gpAverage: &gasPriceAverage{
price: big.NewInt(0),
Expand Down Expand Up @@ -571,6 +579,13 @@ func (b *Blockchain) readBody(hash types.Hash) (*types.Body, bool) {
return nil, false
}

// To return from field in the transactions of the past blocks
if updated := b.recoverFromFieldsInTransactions(bb.Transactions); updated {
if err := b.db.WriteBody(hash, bb); err != nil {
b.logger.Warn("failed to write body into storage", "hash", hash, "err", err)
}
}

return bb, true
}

Expand Down Expand Up @@ -966,10 +981,15 @@ func (b *Blockchain) updateGasPriceAvgWithBlock(block *types.Block) {
// writeBody writes the block body to the DB.
// Additionally, it also updates the txn lookup, for txnHash -> block lookups
func (b *Blockchain) writeBody(block *types.Block) error {
body := block.Body()
// Recover 'from' field in tx before saving
// Because the block passed from the consensus layer doesn't have from field in tx,
// due to missing encoding in RLP
if err := b.recoverFromFieldsInBlock(block); err != nil {
return err
}

// Write the full body (txns + receipts)
if err := b.db.WriteBody(block.Header.Hash, body); err != nil {
if err := b.db.WriteBody(block.Header.Hash, block.Body()); err != nil {
return err
}

Expand All @@ -990,6 +1010,49 @@ func (b *Blockchain) ReadTxLookup(hash types.Hash) (types.Hash, bool) {
return v, ok
}

// recoverFromFieldsInBlock recovers 'from' fields in the transactions of the given block
// return error if the invalid signature found
func (b *Blockchain) recoverFromFieldsInBlock(block *types.Block) error {
for _, tx := range block.Transactions {
if tx.From != types.ZeroAddress {
continue
}

sender, err := b.txSigner.Sender(tx)
if err != nil {
return err
}

tx.From = sender
}

return nil
}

// recoverFromFieldsInTransactions recovers 'from' fields in the transactions
// log as warning if failing to recover one address
func (b *Blockchain) recoverFromFieldsInTransactions(transactions []*types.Transaction) bool {
updated := false

for _, tx := range transactions {
if tx.From != types.ZeroAddress {
continue
}

sender, err := b.txSigner.Sender(tx)
if err != nil {
b.logger.Warn("failed to recover from address in Tx", "hash", tx.Hash, "err", err)

continue
}

tx.From = sender
updated = true
}

return updated
}

// verifyGasLimit is a helper function for validating a gas limit in a header
func (b *Blockchain) verifyGasLimit(header *types.Header, parentHeader *types.Header) error {
if header.GasUsed > header.GasLimit {
Expand Down

0 comments on commit 6b93e54

Please sign in to comment.