laconicd/x/evm/types/logs.go

128 lines
3.4 KiB
Go
Raw Normal View History

package types
import (
"errors"
"fmt"
2021-09-03 18:06:36 +00:00
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
ethermint "github.com/tharsis/ethermint/types"
)
// NewTransactionLogs creates a new NewTransactionLogs instance.
func NewTransactionLogs(hash common.Hash, logs []*Log) TransactionLogs {
return TransactionLogs{
Hash: hash.String(),
Logs: logs,
}
}
2021-04-17 10:00:07 +00:00
// NewTransactionLogsFromEth creates a new NewTransactionLogs instance using []*ethtypes.Log.
func NewTransactionLogsFromEth(hash common.Hash, ethlogs []*ethtypes.Log) TransactionLogs {
2021-04-17 10:00:07 +00:00
return TransactionLogs{
Hash: hash.String(),
Logs: NewLogsFromEth(ethlogs),
2021-04-17 10:00:07 +00:00
}
}
// Validate performs a basic validation of a GenesisAccount fields.
func (tx TransactionLogs) Validate() error {
if ethermint.IsEmptyHash(tx.Hash) {
return fmt.Errorf("hash cannot be the empty %s", tx.Hash)
}
for i, log := range tx.Logs {
2021-04-17 10:00:07 +00:00
if log == nil {
return fmt.Errorf("log %d cannot be nil", i)
}
if err := log.Validate(); err != nil {
return fmt.Errorf("invalid log %d: %w", i, err)
}
2021-04-17 10:00:07 +00:00
if log.TxHash != tx.Hash {
return fmt.Errorf("log tx hash mismatch (%s ≠ %s)", log.TxHash, tx.Hash)
}
}
return nil
}
2021-04-17 10:00:07 +00:00
// EthLogs returns the Ethereum type Logs from the Transaction Logs.
func (tx TransactionLogs) EthLogs() []*ethtypes.Log {
return LogsToEthereum(tx.Logs)
}
// Validate performs a basic validation of an ethereum Log fields.
func (log *Log) Validate() error {
if err := ethermint.ValidateAddress(log.Address); err != nil {
return fmt.Errorf("invalid log address %w", err)
}
if ethermint.IsEmptyHash(log.BlockHash) {
2021-04-17 10:00:07 +00:00
return fmt.Errorf("block hash cannot be the empty %s", log.BlockHash)
}
if log.BlockNumber == 0 {
return errors.New("block number cannot be zero")
}
if ethermint.IsEmptyHash(log.TxHash) {
2021-04-17 10:00:07 +00:00
return fmt.Errorf("tx hash cannot be the empty %s", log.TxHash)
}
return nil
}
2021-04-17 10:00:07 +00:00
// ToEthereum returns the Ethereum type Log from a Ethermint proto compatible Log.
2021-04-17 10:00:07 +00:00
func (log *Log) ToEthereum() *ethtypes.Log {
2021-09-03 18:06:36 +00:00
var topics []common.Hash // nolint: prealloc
2021-04-17 10:00:07 +00:00
for i := range log.Topics {
2021-09-03 18:06:36 +00:00
topics = append(topics, common.HexToHash(log.Topics[i]))
2021-04-17 10:00:07 +00:00
}
return &ethtypes.Log{
2021-09-03 18:06:36 +00:00
Address: common.HexToAddress(log.Address),
2021-04-17 10:00:07 +00:00
Topics: topics,
Data: log.Data,
BlockNumber: log.BlockNumber,
2021-09-03 18:06:36 +00:00
TxHash: common.HexToHash(log.TxHash),
2021-04-17 10:00:07 +00:00
TxIndex: uint(log.TxIndex),
2021-04-18 15:54:18 +00:00
Index: uint(log.Index),
2021-09-03 18:06:36 +00:00
BlockHash: common.HexToHash(log.BlockHash),
2021-04-17 10:00:07 +00:00
Removed: log.Removed,
}
}
func NewLogsFromEth(ethlogs []*ethtypes.Log) []*Log {
var logs []*Log // nolint: prealloc
for _, ethlog := range ethlogs {
logs = append(logs, NewLogFromEth(ethlog))
}
return logs
}
2021-04-17 10:00:07 +00:00
// LogsToEthereum casts the Ethermint Logs to a slice of Ethereum Logs.
func LogsToEthereum(logs []*Log) []*ethtypes.Log {
var ethLogs []*ethtypes.Log // nolint: prealloc
2021-04-17 10:00:07 +00:00
for i := range logs {
ethLogs = append(ethLogs, logs[i].ToEthereum())
2021-04-17 10:00:07 +00:00
}
return ethLogs
}
// NewLogFromEth creates a new Log instance from a Ethereum type Log.
func NewLogFromEth(log *ethtypes.Log) *Log {
var topics []string // nolint: prealloc
for _, topic := range log.Topics {
topics = append(topics, topic.String())
2021-04-17 10:00:07 +00:00
}
return &Log{
Address: log.Address.String(),
Topics: topics,
Data: log.Data,
BlockNumber: log.BlockNumber,
TxHash: log.TxHash.String(),
TxIndex: uint64(log.TxIndex),
Index: uint64(log.Index),
2021-04-17 10:00:07 +00:00
BlockHash: log.BlockHash.String(),
Removed: log.Removed,
}
}