github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/evm/types/logs.go (about)

     1  package types
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"fmt"
     7  
     8  	ethcmn "github.com/ethereum/go-ethereum/common"
     9  	ethtypes "github.com/ethereum/go-ethereum/core/types"
    10  )
    11  
    12  // TransactionLogs define the logs generated from a transaction execution
    13  // with a given hash. It it used for import/export data as transactions are not persisted
    14  // on blockchain state after an upgrade.
    15  type TransactionLogs struct {
    16  	Hash ethcmn.Hash     `json:"hash"`
    17  	Logs []*ethtypes.Log `json:"logs"`
    18  }
    19  
    20  // NewTransactionLogs creates a new NewTransactionLogs instance.
    21  func NewTransactionLogs(hash ethcmn.Hash, logs []*ethtypes.Log) TransactionLogs {
    22  	return TransactionLogs{
    23  		Hash: hash,
    24  		Logs: logs,
    25  	}
    26  }
    27  
    28  // MarshalLogs encodes an array of logs using amino
    29  func MarshalLogs(logs []*ethtypes.Log) ([]byte, error) {
    30  	return ModuleCdc.MarshalBinaryLengthPrefixed(logs)
    31  }
    32  
    33  // UnmarshalLogs decodes an amino-encoded byte array into an array of logs
    34  func UnmarshalLogs(in []byte) ([]*ethtypes.Log, error) {
    35  	logs := []*ethtypes.Log{}
    36  	err := ModuleCdc.UnmarshalBinaryLengthPrefixed(in, &logs)
    37  	return logs, err
    38  }
    39  
    40  // Validate performs a basic validation of a GenesisAccount fields.
    41  func (tx TransactionLogs) Validate() error {
    42  	if bytes.Equal(tx.Hash.Bytes(), ethcmn.Hash{}.Bytes()) {
    43  		return fmt.Errorf("hash cannot be the empty %s", tx.Hash.String())
    44  	}
    45  
    46  	for i, log := range tx.Logs {
    47  		if err := ValidateLog(log); err != nil {
    48  			return fmt.Errorf("invalid log %d: %w", i, err)
    49  		}
    50  		if !bytes.Equal(log.TxHash.Bytes(), tx.Hash.Bytes()) {
    51  			return fmt.Errorf("log tx hash mismatch (%s ≠ %s)", log.TxHash.String(), tx.Hash.String())
    52  		}
    53  	}
    54  	return nil
    55  }
    56  
    57  // ValidateLog performs a basic validation of an ethereum Log fields.
    58  func ValidateLog(log *ethtypes.Log) error {
    59  	if log == nil {
    60  		return errors.New("log cannot be nil")
    61  	}
    62  	if bytes.Equal(log.Address.Bytes(), ethcmn.Address{}.Bytes()) {
    63  		return fmt.Errorf("log address cannot be empty %s", log.Address.String())
    64  	}
    65  	if bytes.Equal(log.BlockHash.Bytes(), ethcmn.Hash{}.Bytes()) {
    66  		return fmt.Errorf("block hash cannot be the empty %s", log.BlockHash.String())
    67  	}
    68  	if log.BlockNumber == 0 {
    69  		return errors.New("block number cannot be zero")
    70  	}
    71  	if bytes.Equal(log.TxHash.Bytes(), ethcmn.Hash{}.Bytes()) {
    72  		return fmt.Errorf("tx hash cannot be the empty %s", log.TxHash.String())
    73  	}
    74  	return nil
    75  }