/
utils.go
107 lines (90 loc) · 3.08 KB
/
utils.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
package utils
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strings"
support "github.com/artela-network/artela/x/evm/txs/support"
evmtypes "github.com/artela-network/artela/x/evm/types"
abci "github.com/cometbft/cometbft/abci/types"
tmrpctypes "github.com/cometbft/cometbft/rpc/core/types"
"github.com/cosmos/cosmos-sdk/client"
ethtypes "github.com/ethereum/go-ethereum/core/types"
)
func TxLogsFromEvents(events []abci.Event, msgIndex int) ([]*ethtypes.Log, error) {
for _, event := range events {
if event.Type != evmtypes.EventTypeTxLog {
continue
}
if msgIndex > 0 {
// not the eth tx we want
msgIndex--
continue
}
return ParseTxLogsFromEvent(event)
}
return nil, fmt.Errorf("eth tx logs not found for message index %d", msgIndex)
}
// ParseTxLogsFromEvent parse tx logs from one event
func ParseTxLogsFromEvent(event abci.Event) ([]*ethtypes.Log, error) {
logs := make([]*support.Log, 0, len(event.Attributes))
for _, attr := range event.Attributes {
if !bytes.Equal([]byte(attr.Key), []byte(evmtypes.AttributeKeyTxLog)) {
continue
}
var log support.Log
if err := json.Unmarshal([]byte(attr.Value), &log); err != nil {
return nil, err
}
logs = append(logs, &log)
}
return support.LogsToEthereum(logs), nil
}
func BlockMaxGasFromConsensusParams(ctx context.Context, clientCtx client.Context, blockHeight int64) (int64, error) {
resConsParams, err := clientCtx.Client.ConsensusParams(ctx, &blockHeight)
defaultGasLimit := int64(^uint32(0)) // #nosec G701
if err != nil {
return defaultGasLimit, err
}
gasLimit := resConsParams.ConsensusParams.Block.MaxGas
if gasLimit == -1 {
// Sets gas limit to max uint32 to not error with javascript dev tooling
// This -1 value indicating no block gas limit is set to max uint64 with geth hexutils
// which errors certain javascript dev tooling which only supports up to 53 bits
gasLimit = defaultGasLimit
}
return gasLimit, nil
}
// ShouldIgnoreGasUsed returns true if the gasUsed in result should be ignored
// workaround for issue: https://github.com/cosmos/cosmos-sdk/issues/10832
func ShouldIgnoreGasUsed(res *abci.ResponseDeliverTx) bool {
return res.GetCode() == 11 && strings.Contains(res.GetLog(), "no block gas left to run tx: out of gas")
}
// GetLogsFromBlockResults returns the list of event logs from the tendermint block result response
func GetLogsFromBlockResults(blockRes *tmrpctypes.ResultBlockResults) ([][]*ethtypes.Log, error) {
blockLogs := [][]*ethtypes.Log{}
for _, txResult := range blockRes.TxsResults {
logs, err := AllTxLogsFromEvents(txResult.Events)
if err != nil {
return nil, err
}
blockLogs = append(blockLogs, logs...)
}
return blockLogs, nil
}
// AllTxLogsFromEvents parses all ethereum logs from cosmos events
func AllTxLogsFromEvents(events []abci.Event) ([][]*ethtypes.Log, error) {
allLogs := make([][]*ethtypes.Log, 0, 4)
for _, event := range events {
if event.Type != evmtypes.EventTypeTxLog {
continue
}
logs, err := ParseTxLogsFromEvent(event)
if err != nil {
return nil, err
}
allLogs = append(allLogs, logs)
}
return allLogs, nil
}