2022-11-22 13:28:53 +00:00
|
|
|
package requests
|
|
|
|
|
|
|
|
import (
|
2023-07-18 08:47:04 +00:00
|
|
|
"context"
|
2023-06-04 19:53:05 +00:00
|
|
|
"encoding/json"
|
2022-11-22 13:28:53 +00:00
|
|
|
"fmt"
|
2023-11-11 12:04:18 +00:00
|
|
|
|
2023-10-21 23:17:18 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/common/hexutil"
|
2022-11-22 13:28:53 +00:00
|
|
|
|
2023-07-18 08:47:04 +00:00
|
|
|
ethereum "github.com/ledgerwatch/erigon"
|
2023-06-04 19:53:05 +00:00
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
|
|
|
"github.com/ledgerwatch/erigon-lib/common/hexutility"
|
2023-07-18 08:47:04 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
2022-11-22 13:28:53 +00:00
|
|
|
)
|
|
|
|
|
2023-07-18 08:47:04 +00:00
|
|
|
func Compare(expected types.Log, actual types.Log) ([]error, bool) {
|
|
|
|
var errs []error
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case expected.Address != actual.Address:
|
|
|
|
errs = append(errs, fmt.Errorf("expected address: %v, actual address %v", expected.Address, actual.Address))
|
|
|
|
case expected.TxHash != actual.TxHash:
|
|
|
|
errs = append(errs, fmt.Errorf("expected txhash: %v, actual txhash %v", expected.TxHash, actual.TxHash))
|
|
|
|
case expected.BlockHash != actual.BlockHash:
|
|
|
|
errs = append(errs, fmt.Errorf("expected blockHash: %v, actual blockHash %v", expected.BlockHash, actual.BlockHash))
|
|
|
|
case expected.BlockNumber != actual.BlockNumber:
|
|
|
|
errs = append(errs, fmt.Errorf("expected blockNumber: %v, actual blockNumber %v", expected.BlockNumber, actual.BlockNumber))
|
|
|
|
case expected.TxIndex != actual.TxIndex:
|
|
|
|
errs = append(errs, fmt.Errorf("expected txIndex: %v, actual txIndex %v", expected.TxIndex, actual.TxIndex))
|
|
|
|
case !hashSlicesAreEqual(expected.Topics, actual.Topics):
|
|
|
|
errs = append(errs, fmt.Errorf("expected topics: %v, actual topics %v", expected.Topics, actual.Topics))
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs, len(errs) == 0
|
2023-06-04 19:53:05 +00:00
|
|
|
}
|
|
|
|
|
2023-07-20 22:10:18 +00:00
|
|
|
func NewLog(hash libcommon.Hash, blockNum uint64, address libcommon.Address, topics []libcommon.Hash, data hexutility.Bytes, txIndex uint, blockHash libcommon.Hash, index hexutil.Uint, removed bool) types.Log {
|
2023-07-18 08:47:04 +00:00
|
|
|
return types.Log{
|
2023-06-04 19:53:05 +00:00
|
|
|
Address: address,
|
|
|
|
Topics: topics,
|
|
|
|
Data: data,
|
2023-07-20 22:10:18 +00:00
|
|
|
BlockNumber: blockNum,
|
2023-06-04 19:53:05 +00:00
|
|
|
TxHash: hash,
|
|
|
|
TxIndex: txIndex,
|
|
|
|
BlockHash: blockHash,
|
2023-07-18 08:47:04 +00:00
|
|
|
Index: txIndex,
|
2023-06-04 19:53:05 +00:00
|
|
|
Removed: removed,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-18 08:47:04 +00:00
|
|
|
func (reqGen *requestGenerator) FilterLogs(ctx context.Context, query ethereum.FilterQuery) ([]types.Log, error) {
|
2023-08-25 11:19:39 +00:00
|
|
|
var result []types.Log
|
2023-06-04 19:53:05 +00:00
|
|
|
|
2023-11-17 10:41:45 +00:00
|
|
|
if err := reqGen.rpcCall(ctx, &result, Methods.ETHGetLogs, query); err != nil {
|
2023-08-25 11:19:39 +00:00
|
|
|
return nil, err
|
2022-11-22 13:28:53 +00:00
|
|
|
}
|
|
|
|
|
2023-08-25 11:19:39 +00:00
|
|
|
return result, nil
|
2023-07-18 08:47:04 +00:00
|
|
|
}
|
2022-11-22 13:28:53 +00:00
|
|
|
|
2023-07-18 08:47:04 +00:00
|
|
|
func (reqGen *requestGenerator) SubscribeFilterLogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) {
|
2023-07-28 13:03:32 +00:00
|
|
|
return reqGen.Subscribe(ctx, Methods.ETHLogs, ch, query)
|
2022-11-22 13:28:53 +00:00
|
|
|
}
|
2023-06-04 19:53:05 +00:00
|
|
|
|
|
|
|
// ParseResponse converts any of the models interfaces to a string for readability
|
|
|
|
func parseResponse(resp interface{}) (string, error) {
|
|
|
|
result, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error trying to marshal response: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(result), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func hashSlicesAreEqual(s1, s2 []libcommon.Hash) bool {
|
|
|
|
if len(s1) != len(s2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(s1); i++ {
|
|
|
|
if s1[i] != s2[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|