Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Apr 16, 2024
1 parent e250e5c commit 79c0e49
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 61 deletions.
36 changes: 15 additions & 21 deletions consensus/polybft/blockchain_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,32 +103,26 @@ func (p *blockchainWrapper) ProcessBlock(parent *types.Header, block *types.Bloc

var buf bytes.Buffer

// apply transactions from block
err = block.TxnIterator(
func(i int, t *types.Transaction) *types.IterationResult {
if err := transition.Write(t); err != nil {
return &types.IterationResult{Err: fmt.Errorf("process block tx error, tx = %s, err = %w", t.Hash(), err)}
}

if p.logger.GetLevel() <= hclog.Debug {
if p.logger.IsTrace() {
_, _ = buf.WriteString(t.String())
}
for i, t := range block.Transactions {
if err := transition.Write(t); err != nil {
p.logger.Error("failed to write transaction to the block", "tx", t, "err", err)

if p.logger.IsDebug() {
_, _ = buf.WriteString(t.Hash().String())
}
return nil, fmt.Errorf("failed to write transaction %s to the block: %w", t.Hash(), err)
}

if i != len(block.Transactions)-1 {
_, _ = buf.WriteString("\n")
}
if p.logger.GetLevel() <= hclog.Debug {
if p.logger.IsTrace() {
_, _ = buf.WriteString(t.String())
}

return &types.IterationResult{}
})
if p.logger.IsDebug() {
_, _ = buf.WriteString(t.Hash().String())
}

if err != nil {
return nil, err
if i != len(block.Transactions)-1 {
_, _ = buf.WriteString("\n")
}
}
}

p.logger.Debug("[BlockchainWrapper.ProcessBlock]", "txs count", len(block.Transactions), "txs", buf.String())
Expand Down
14 changes: 5 additions & 9 deletions state/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,15 @@ func (e *Executor) ProcessBlock(

var buf bytes.Buffer

err = block.TxnIterator(func(i int, t *types.Transaction) *types.IterationResult {
for i, t := range block.Transactions {
if t.Gas() > block.Header.GasLimit {
return &types.IterationResult{ShouldContinue: true}
continue
}

if err = txn.Write(t); err != nil {
return &types.IterationResult{Err: err}
e.logger.Error("failed to write transaction to the block", "tx", t, "err", err)

return nil, err
}

if e.logger.GetLevel() <= hclog.Debug {
Expand All @@ -167,12 +169,6 @@ func (e *Executor) ProcessBlock(
_, _ = buf.WriteString("\n")
}
}

return &types.IterationResult{}
})

if err != nil {
return nil, err
}

if e.logger.IsDebug() {
Expand Down
18 changes: 18 additions & 0 deletions types/access_list_tx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"bytes"
"fmt"
"math/big"

Expand Down Expand Up @@ -165,6 +166,23 @@ func (t *TxAccessList) unmarshalJSON(v *fastjson.Value) error {
return nil
}

func (t *TxAccessList) String() string {
var buf bytes.Buffer

for _, accessEntry := range *t {
buf.WriteString(fmt.Sprintf("address=%s", accessEntry.Address))
for i, storageKey := range accessEntry.StorageKeys {

Check failure on line 174 in types/access_list_tx.go

View workflow job for this annotation

GitHub Actions / Lint / Run Lint

ranges should only be cuddled with assignments used in the iteration (wsl)
if i == 0 {
buf.WriteString("\n")
}

buf.WriteString(fmt.Sprintf("\tkey=%s", storageKey))
}
}

return buf.String()
}

type AccessListTxn struct {
*BaseTx
GasPrice *big.Int
Expand Down
23 changes: 0 additions & 23 deletions types/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,29 +158,6 @@ func (b *Block) String() string {
return str
}

type IterationResult struct {
ShouldContinue bool
ShouldBreak bool
Err error
}

// TxnIterator represents an iterator for block transactions
func (b *Block) TxnIterator(handler func(int, *Transaction) *IterationResult) error {
for i, tx := range b.Transactions {
result := handler(i, tx)

if result == nil || result.ShouldContinue {
continue
}

if result.ShouldBreak || result.Err != nil {
return result.Err
}
}

return nil
}

// WithSeal returns a new block with the data from b but the header replaced with
// the sealed one.
func (b *Block) WithSeal(header *Header) *Block {
Expand Down
11 changes: 3 additions & 8 deletions types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,13 @@ type TxData interface {
}

func (tx *Transaction) String() string {
recipient := ""
if tx.To() != nil {
recipient = tx.To().String()
}

v, r, s := tx.RawSignatureValues()

return fmt.Sprintf("[%s] Nonce: %d, GasPrice: %d, GasTipCap: %d, GasFeeCap: %d, "+
"Gas: %d, To: %s, Value: %d, Input: %x, V: %d, R: %d, S: %s, Hash: %s, From: %s",
"Gas: %d, To: %s, Value: %d, Input: %x, V: %d, R: %d, S: %s, Hash: %s, From: %s, AccessList: %s",
tx.Type(), tx.Nonce(), tx.GasPrice(), tx.GasTipCap(), tx.GasFeeCap(),
tx.Gas(), recipient, tx.Value(), tx.Input(),
v, r, s, tx.Hash(), tx.From())
tx.Gas(), tx.To(), tx.Value(), tx.Input(),
v, r, s, tx.Hash(), tx.From(), tx.AccessList())
}

func (t *Transaction) Type() TxType {
Expand Down

0 comments on commit 79c0e49

Please sign in to comment.