-
Notifications
You must be signed in to change notification settings - Fork 73
/
logs.go
137 lines (120 loc) · 3.94 KB
/
logs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package support
import (
"errors"
"fmt"
artela "github.com/artela-network/artela/ethereum/types"
"github.com/ethereum/go-ethereum/common"
ethereum "github.com/ethereum/go-ethereum/core/types"
)
// ----------------------------------------------------------------------------
// Transaction Logs
// ----------------------------------------------------------------------------
// NewTransactionLogs creates a new NewTransactionLogs instance.
// TransactionLogs define the logs support from a txs execution with a given hash.
func NewTransactionLogs(hash common.Hash, logs []*Log) TransactionLogs {
return TransactionLogs{
Hash: hash.String(),
Logs: logs,
}
}
// NewTransactionLogsFromEth creates a new NewTransactionLogs instance using []*ethereum.Log.
func NewTransactionLogsFromEth(hash common.Hash, ethlogs []*ethereum.Log) TransactionLogs {
return TransactionLogs{
Hash: hash.String(),
Logs: NewLogsFromEth(ethlogs),
}
}
// Validate performs a basic validation of a GenesisAccount fields.
func (tx TransactionLogs) Validate() error {
if artela.IsEmptyHash(tx.Hash) {
return fmt.Errorf("hash cannot be the empty %s", tx.Hash)
}
for i, log := range tx.Logs {
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)
}
if log.TxHash != tx.Hash {
return fmt.Errorf("log txs hash mismatch (%s ≠ %s)", log.TxHash, tx.Hash)
}
}
return nil
}
// EthLogs returns the Ethereum type Logs from the Transaction Logs.
func (tx TransactionLogs) EthLogs() []*ethereum.Log {
return LogsToEthereum(tx.Logs)
}
// ----------------------------------------------------------------------------
// Log
// ----------------------------------------------------------------------------
// Validate performs a basic validation of an ethereum Log fields.
// Log represents a protobuf compatible Ethereum Log that defines a contract
// log event.
func (log *Log) Validate() error {
if err := artela.ValidateAddress(log.Address); err != nil {
return fmt.Errorf("invalid log address %w", err)
}
if artela.IsEmptyHash(log.BlockHash) {
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 artela.IsEmptyHash(log.TxHash) {
return fmt.Errorf("txs hash cannot be the empty %s", log.TxHash)
}
return nil
}
// ToEthereum returns the Ethereum type Log from a artela proto compatible Log.
func (log *Log) ToEthereum() *ethereum.Log {
topics := make([]common.Hash, len(log.Topics))
for i, topic := range log.Topics {
topics[i] = common.HexToHash(topic)
}
return ðereum.Log{
Address: common.HexToAddress(log.Address),
Topics: topics,
Data: log.Data,
BlockNumber: log.BlockNumber,
TxHash: common.HexToHash(log.TxHash),
TxIndex: uint(log.TxIndex),
Index: uint(log.Index),
BlockHash: common.HexToHash(log.BlockHash),
Removed: log.Removed,
}
}
func NewLogsFromEth(ethlogs []*ethereum.Log) []*Log {
var logs []*Log //nolint: prealloc
for _, ethlog := range ethlogs {
logs = append(logs, NewLogFromEth(ethlog))
}
return logs
}
// LogsToEthereum casts the Artela Logs to a slice of Ethereum Logs.
func LogsToEthereum(logs []*Log) []*ethereum.Log {
var ethLogs []*ethereum.Log //nolint: prealloc
for i := range logs {
ethLogs = append(ethLogs, logs[i].ToEthereum())
}
return ethLogs
}
// NewLogFromEth creates a new Log instance from a Ethereum type Log.
func NewLogFromEth(log *ethereum.Log) *Log {
topics := make([]string, len(log.Topics))
for i, topic := range log.Topics {
topics[i] = topic.String()
}
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),
BlockHash: log.BlockHash.String(),
Removed: log.Removed,
}
}