2022-09-09 17:59:00 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-09-28 14:58:24 +00:00
|
|
|
"bytes"
|
2022-09-09 17:59:00 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-09-12 21:46:15 +00:00
|
|
|
mathbig "math/big"
|
2022-09-09 17:59:00 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
2022-11-10 11:27:58 +00:00
|
|
|
"github.com/minio/blake2b-simd"
|
2022-09-09 17:59:00 +00:00
|
|
|
"github.com/multiformats/go-multihash"
|
2022-10-21 11:13:16 +00:00
|
|
|
"github.com/multiformats/go-varint"
|
2022-09-12 21:46:15 +00:00
|
|
|
"golang.org/x/xerrors"
|
2022-09-09 17:59:00 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
2022-11-10 02:46:13 +00:00
|
|
|
builtintypes "github.com/filecoin-project/go-state-types/builtin"
|
2022-09-12 21:46:15 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
2022-11-16 13:53:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
EthTopic1 = "topic1"
|
|
|
|
EthTopic2 = "topic2"
|
|
|
|
EthTopic3 = "topic3"
|
|
|
|
EthTopic4 = "topic4"
|
2022-09-09 17:59:00 +00:00
|
|
|
)
|
|
|
|
|
2022-09-29 22:55:13 +00:00
|
|
|
type EthUint64 uint64
|
2022-09-09 17:59:00 +00:00
|
|
|
|
2022-09-29 22:55:13 +00:00
|
|
|
func (e EthUint64) MarshalJSON() ([]byte, error) {
|
2022-09-29 20:46:59 +00:00
|
|
|
if e == 0 {
|
|
|
|
return json.Marshal("0x0")
|
|
|
|
}
|
2022-09-09 17:59:00 +00:00
|
|
|
return json.Marshal(fmt.Sprintf("0x%x", e))
|
|
|
|
}
|
|
|
|
|
2022-09-29 22:55:13 +00:00
|
|
|
func (e *EthUint64) UnmarshalJSON(b []byte) error {
|
2022-09-09 17:59:00 +00:00
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(b, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-29 22:55:13 +00:00
|
|
|
parsedInt, err := strconv.ParseUint(strings.Replace(s, "0x", "", -1), 16, 64)
|
2022-09-09 17:59:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-29 22:55:13 +00:00
|
|
|
eint := EthUint64(parsedInt)
|
2022-09-12 21:46:15 +00:00
|
|
|
*e = eint
|
2022-09-09 17:59:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-16 12:08:48 +00:00
|
|
|
func EthUint64FromHex(s string) (EthUint64, error) {
|
|
|
|
parsedInt, err := strconv.ParseUint(strings.Replace(s, "0x", "", -1), 16, 64)
|
|
|
|
if err != nil {
|
|
|
|
return EthUint64(0), err
|
|
|
|
}
|
|
|
|
return EthUint64(parsedInt), nil
|
|
|
|
}
|
|
|
|
|
2022-09-09 17:59:00 +00:00
|
|
|
type EthBigInt big.Int
|
|
|
|
|
2022-11-10 11:27:58 +00:00
|
|
|
var EthBigIntZero = EthBigInt{Int: big.Zero().Int}
|
2022-09-12 21:46:15 +00:00
|
|
|
|
2022-09-09 17:59:00 +00:00
|
|
|
func (e EthBigInt) MarshalJSON() ([]byte, error) {
|
|
|
|
if e.Int == nil {
|
|
|
|
return json.Marshal("0x0")
|
|
|
|
}
|
|
|
|
return json.Marshal(fmt.Sprintf("0x%x", e.Int))
|
|
|
|
}
|
|
|
|
|
2022-09-12 21:46:15 +00:00
|
|
|
func (e *EthBigInt) UnmarshalJSON(b []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(b, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
replaced := strings.Replace(s, "0x", "", -1)
|
|
|
|
if len(replaced)%2 == 1 {
|
|
|
|
replaced = "0" + replaced
|
|
|
|
}
|
|
|
|
|
|
|
|
i := new(mathbig.Int)
|
|
|
|
i.SetString(replaced, 16)
|
|
|
|
|
|
|
|
*e = EthBigInt(big.NewFromGo(i))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-28 14:58:24 +00:00
|
|
|
type EthBytes []byte
|
|
|
|
|
|
|
|
func (e EthBytes) MarshalJSON() ([]byte, error) {
|
2022-09-29 20:46:59 +00:00
|
|
|
if len(e) == 0 {
|
|
|
|
return json.Marshal("0x00")
|
|
|
|
}
|
|
|
|
s := hex.EncodeToString(e)
|
|
|
|
if len(s)%2 == 1 {
|
|
|
|
s = "0" + s
|
|
|
|
}
|
|
|
|
return json.Marshal("0x" + s)
|
2022-09-28 14:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EthBytes) UnmarshalJSON(b []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(b, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s = strings.Replace(s, "0x", "", -1)
|
|
|
|
if len(s)%2 == 1 {
|
|
|
|
s = "0" + s
|
|
|
|
}
|
|
|
|
|
|
|
|
decoded, err := hex.DecodeString(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*e = decoded
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-09 17:59:00 +00:00
|
|
|
type EthBlock struct {
|
2022-11-12 17:44:52 +00:00
|
|
|
Hash EthHash `json:"hash"`
|
2022-09-09 17:59:00 +00:00
|
|
|
ParentHash EthHash `json:"parentHash"`
|
|
|
|
Sha3Uncles EthHash `json:"sha3Uncles"`
|
|
|
|
Miner EthAddress `json:"miner"`
|
|
|
|
StateRoot EthHash `json:"stateRoot"`
|
|
|
|
TransactionsRoot EthHash `json:"transactionsRoot"`
|
|
|
|
ReceiptsRoot EthHash `json:"receiptsRoot"`
|
|
|
|
// TODO: include LogsBloom
|
2022-11-12 17:44:52 +00:00
|
|
|
Difficulty EthUint64 `json:"difficulty"`
|
|
|
|
TotalDifficulty EthUint64 `json:"totalDifficulty"`
|
|
|
|
Number EthUint64 `json:"number"`
|
|
|
|
GasLimit EthUint64 `json:"gasLimit"`
|
|
|
|
GasUsed EthUint64 `json:"gasUsed"`
|
|
|
|
Timestamp EthUint64 `json:"timestamp"`
|
|
|
|
Extradata []byte `json:"extraData"`
|
|
|
|
MixHash EthHash `json:"mixHash"`
|
|
|
|
Nonce EthNonce `json:"nonce"`
|
|
|
|
BaseFeePerGas EthBigInt `json:"baseFeePerGas"`
|
|
|
|
Size EthUint64 `json:"size"`
|
2022-09-12 21:46:15 +00:00
|
|
|
// can be []EthTx or []string depending on query params
|
|
|
|
Transactions []interface{} `json:"transactions"`
|
|
|
|
Uncles []EthHash `json:"uncles"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
EmptyEthHash = EthHash{}
|
2022-09-29 22:55:13 +00:00
|
|
|
EmptyEthInt = EthUint64(0)
|
2022-09-12 21:46:15 +00:00
|
|
|
EmptyEthNonce = [8]byte{0, 0, 0, 0, 0, 0, 0, 0}
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewEthBlock() EthBlock {
|
|
|
|
return EthBlock{
|
|
|
|
Sha3Uncles: EmptyEthHash,
|
|
|
|
StateRoot: EmptyEthHash,
|
|
|
|
TransactionsRoot: EmptyEthHash,
|
|
|
|
ReceiptsRoot: EmptyEthHash,
|
|
|
|
Difficulty: EmptyEthInt,
|
|
|
|
Extradata: []byte{},
|
|
|
|
MixHash: EmptyEthHash,
|
|
|
|
Nonce: EmptyEthNonce,
|
2022-09-29 22:55:13 +00:00
|
|
|
GasLimit: EthUint64(build.BlockGasLimit), // TODO we map Ethereum blocks to Filecoin tipsets; this is inconsistent.
|
2022-09-12 21:46:15 +00:00
|
|
|
Uncles: []EthHash{},
|
|
|
|
Transactions: []interface{}{},
|
|
|
|
}
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 21:46:15 +00:00
|
|
|
type EthCall struct {
|
2022-10-26 17:19:29 +00:00
|
|
|
From *EthAddress `json:"from"`
|
2022-09-29 20:46:59 +00:00
|
|
|
To *EthAddress `json:"to"`
|
2022-09-29 22:55:13 +00:00
|
|
|
Gas EthUint64 `json:"gas"`
|
2022-09-29 20:46:59 +00:00
|
|
|
GasPrice EthBigInt `json:"gasPrice"`
|
|
|
|
Value EthBigInt `json:"value"`
|
|
|
|
Data EthBytes `json:"data"`
|
2022-09-12 21:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *EthCall) UnmarshalJSON(b []byte) error {
|
|
|
|
type TempEthCall EthCall
|
|
|
|
var params TempEthCall
|
|
|
|
|
|
|
|
if err := json.Unmarshal(b, ¶ms); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*c = EthCall(params)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-09 17:59:00 +00:00
|
|
|
type EthTxReceipt struct {
|
2022-11-16 13:53:27 +00:00
|
|
|
TransactionHash EthHash `json:"transactionHash"`
|
|
|
|
TransactionIndex EthUint64 `json:"transactionIndex"`
|
|
|
|
BlockHash EthHash `json:"blockHash"`
|
|
|
|
BlockNumber EthUint64 `json:"blockNumber"`
|
|
|
|
From EthAddress `json:"from"`
|
|
|
|
To *EthAddress `json:"to"`
|
2022-09-09 17:59:00 +00:00
|
|
|
StateRoot EthHash `json:"root"`
|
2022-09-29 22:55:13 +00:00
|
|
|
Status EthUint64 `json:"status"`
|
2022-09-09 17:59:00 +00:00
|
|
|
ContractAddress *EthAddress `json:"contractAddress"`
|
2022-09-29 22:55:13 +00:00
|
|
|
CumulativeGasUsed EthUint64 `json:"cumulativeGasUsed"`
|
|
|
|
GasUsed EthUint64 `json:"gasUsed"`
|
2022-09-09 17:59:00 +00:00
|
|
|
EffectiveGasPrice EthBigInt `json:"effectiveGasPrice"`
|
2022-09-29 20:46:59 +00:00
|
|
|
LogsBloom EthBytes `json:"logsBloom"`
|
2022-11-16 13:53:27 +00:00
|
|
|
Logs []EthLog `json:"logs"`
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2022-10-20 10:36:15 +00:00
|
|
|
EthAddressLength = 20
|
|
|
|
EthHashLength = 32
|
2022-09-09 17:59:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type EthNonce [8]byte
|
|
|
|
|
|
|
|
func (n EthNonce) String() string {
|
|
|
|
return "0x" + hex.EncodeToString(n[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n EthNonce) MarshalJSON() ([]byte, error) {
|
2022-10-20 10:36:15 +00:00
|
|
|
return json.Marshal(n.String())
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-07 15:39:24 +00:00
|
|
|
func (n *EthNonce) UnmarshalJSON(b []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(b, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s = strings.Replace(s, "0x", "", -1)
|
|
|
|
if len(s)%2 == 1 {
|
|
|
|
s = "0" + s
|
|
|
|
}
|
|
|
|
|
|
|
|
decoded, err := hex.DecodeString(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
copy(n[:], decoded[:8])
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-20 10:36:15 +00:00
|
|
|
type EthAddress [EthAddressLength]byte
|
2022-09-09 17:59:00 +00:00
|
|
|
|
2022-10-20 10:36:15 +00:00
|
|
|
func (ea EthAddress) String() string {
|
|
|
|
return "0x" + hex.EncodeToString(ea[:])
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 10:36:15 +00:00
|
|
|
func (ea EthAddress) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(ea.String())
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 10:36:15 +00:00
|
|
|
func (ea *EthAddress) UnmarshalJSON(b []byte) error {
|
2022-09-09 17:59:00 +00:00
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(b, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
addr, err := EthAddressFromHex(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-20 10:36:15 +00:00
|
|
|
copy(ea[:], addr[:])
|
2022-09-09 17:59:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-20 10:36:15 +00:00
|
|
|
func (ea EthAddress) ToFilecoinAddress() (address.Address, error) {
|
|
|
|
idmask := [12]byte{0xff}
|
|
|
|
if bytes.Equal(ea[:12], idmask[:]) {
|
|
|
|
// This is a masked ID address.
|
|
|
|
id := binary.BigEndian.Uint64(ea[12:])
|
|
|
|
return address.NewIDAddress(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, translate the address into an address controlled by the
|
|
|
|
// Ethereum Address Manager.
|
2022-11-10 02:46:13 +00:00
|
|
|
addr, err := address.NewDelegatedAddress(builtintypes.EthereumAddressManagerActorID, ea[:])
|
2022-10-20 10:36:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return address.Undef, fmt.Errorf("failed to translate supplied address (%s) into a "+
|
|
|
|
"Filecoin f4 address: %w", hex.EncodeToString(ea[:]), err)
|
2022-10-19 12:52:13 +00:00
|
|
|
}
|
2022-10-20 10:36:15 +00:00
|
|
|
return addr, nil
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 11:13:16 +00:00
|
|
|
func TryEthAddressFromFilecoinAddress(addr address.Address, allowId bool) (EthAddress, bool, error) {
|
|
|
|
switch addr.Protocol() {
|
|
|
|
case address.ID:
|
|
|
|
if !allowId {
|
|
|
|
return EthAddress{}, false, nil
|
|
|
|
}
|
|
|
|
id, err := address.IDFromAddress(addr)
|
|
|
|
if err != nil {
|
|
|
|
return EthAddress{}, false, err
|
|
|
|
}
|
|
|
|
var ethaddr EthAddress
|
|
|
|
ethaddr[0] = 0xff
|
|
|
|
binary.BigEndian.PutUint64(ethaddr[12:], id)
|
|
|
|
return ethaddr, true, nil
|
|
|
|
case address.Delegated:
|
|
|
|
payload := addr.Payload()
|
|
|
|
namespace, n, err := varint.FromUvarint(payload)
|
|
|
|
if err != nil {
|
|
|
|
return EthAddress{}, false, xerrors.Errorf("invalid delegated address namespace in: %s", addr)
|
|
|
|
}
|
|
|
|
payload = payload[n:]
|
2022-11-10 02:46:13 +00:00
|
|
|
if namespace == builtintypes.EthereumAddressManagerActorID {
|
2022-10-21 11:13:16 +00:00
|
|
|
addr, err := EthAddressFromBytes(payload)
|
|
|
|
return addr, err == nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return EthAddress{}, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func EthAddressFromFilecoinAddress(addr address.Address) (EthAddress, error) {
|
|
|
|
ethAddr, ok, err := TryEthAddressFromFilecoinAddress(addr, true)
|
|
|
|
if !ok && err == nil {
|
|
|
|
err = xerrors.Errorf("failed to convert filecoin address %s to an equivalent eth address", addr)
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
2022-10-21 11:13:16 +00:00
|
|
|
return ethAddr, err
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func EthAddressFromHex(s string) (EthAddress, error) {
|
|
|
|
handlePrefix(&s)
|
2022-10-20 10:36:15 +00:00
|
|
|
b, err := decodeHexString(s, EthAddressLength)
|
2022-09-09 17:59:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return EthAddress{}, err
|
|
|
|
}
|
|
|
|
var h EthAddress
|
2022-10-20 10:36:15 +00:00
|
|
|
copy(h[EthAddressLength-len(b):], b)
|
2022-09-09 17:59:00 +00:00
|
|
|
return h, nil
|
|
|
|
}
|
|
|
|
|
2022-09-29 20:46:59 +00:00
|
|
|
func EthAddressFromBytes(b []byte) (EthAddress, error) {
|
|
|
|
var a EthAddress
|
2022-10-20 10:36:15 +00:00
|
|
|
if len(b) != EthAddressLength {
|
|
|
|
return EthAddress{}, xerrors.Errorf("cannot parse bytes into anœ EthAddress: incorrect input length")
|
2022-09-29 20:46:59 +00:00
|
|
|
}
|
|
|
|
copy(a[:], b[:])
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
2022-10-20 10:36:15 +00:00
|
|
|
type EthHash [EthHashLength]byte
|
2022-09-09 17:59:00 +00:00
|
|
|
|
|
|
|
func (h EthHash) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(h.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *EthHash) UnmarshalJSON(b []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(b, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hash, err := EthHashFromHex(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-12 21:46:15 +00:00
|
|
|
copy(h[:], hash[:])
|
2022-09-09 17:59:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlePrefix(s *string) {
|
|
|
|
if strings.HasPrefix(*s, "0x") || strings.HasPrefix(*s, "0X") {
|
|
|
|
*s = (*s)[2:]
|
|
|
|
}
|
|
|
|
if len(*s)%2 == 1 {
|
|
|
|
*s = "0" + *s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeHexString(s string, length int) ([]byte, error) {
|
|
|
|
b, err := hex.DecodeString(s)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, xerrors.Errorf("cannot parse hash: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(b) > length {
|
|
|
|
return []byte{}, xerrors.Errorf("length of decoded bytes is longer than %d", length)
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2022-11-12 17:44:52 +00:00
|
|
|
func NewEthHashFromCid(c cid.Cid) (EthHash, error) {
|
2022-09-09 17:59:00 +00:00
|
|
|
return EthHashFromHex(c.Hash().HexString()[8:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func EthHashFromHex(s string) (EthHash, error) {
|
|
|
|
handlePrefix(&s)
|
2022-10-20 10:36:15 +00:00
|
|
|
b, err := decodeHexString(s, EthHashLength)
|
2022-09-09 17:59:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return EthHash{}, err
|
|
|
|
}
|
|
|
|
var h EthHash
|
2022-10-20 10:36:15 +00:00
|
|
|
copy(h[EthHashLength-len(b):], b)
|
2022-09-09 17:59:00 +00:00
|
|
|
return h, nil
|
|
|
|
}
|
|
|
|
|
2022-11-10 11:27:58 +00:00
|
|
|
func EthHashData(b []byte) EthHash {
|
|
|
|
return EthHash(blake2b.Sum256(b))
|
|
|
|
}
|
|
|
|
|
2022-09-09 17:59:00 +00:00
|
|
|
func (h EthHash) String() string {
|
|
|
|
return "0x" + hex.EncodeToString(h[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h EthHash) ToCid() cid.Cid {
|
|
|
|
// err is always nil
|
|
|
|
mh, _ := multihash.EncodeName(h[:], "blake2b-256")
|
|
|
|
|
|
|
|
return cid.NewCidV1(cid.DagCBOR, mh)
|
|
|
|
}
|
2022-10-22 15:31:58 +00:00
|
|
|
|
|
|
|
type EthFeeHistory struct {
|
|
|
|
OldestBlock uint64 `json:"oldestBlock"`
|
|
|
|
BaseFeePerGas []EthBigInt `json:"baseFeePerGas"`
|
|
|
|
GasUsedRatio []float64 `json:"gasUsedRatio"`
|
|
|
|
Reward *[][]EthBigInt `json:"reward,omitempty"`
|
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
|
|
|
|
// An opaque identifier generated by the Lotus node to refer to an installed filter.
|
|
|
|
type EthFilterID string
|
|
|
|
|
|
|
|
// An opaque identifier generated by the Lotus node to refer to an active subscription.
|
|
|
|
type EthSubscriptionID string
|
|
|
|
|
|
|
|
type EthFilterSpec struct {
|
|
|
|
// Interpreted as an epoch or one of "latest" for last mined block, "earliest" for first,
|
|
|
|
// "pending" for not yet committed messages.
|
|
|
|
// Optional, default: "latest".
|
|
|
|
FromBlock *string `json:"fromBlock,omitempty"`
|
|
|
|
|
|
|
|
// Interpreted as an epoch or one of "latest" for last mined block, "earliest" for first,
|
|
|
|
// "pending" for not yet committed messages.
|
|
|
|
// Optional, default: "latest".
|
|
|
|
ToBlock *string `json:"toBlock,omitempty"`
|
|
|
|
|
|
|
|
// Actor address or a list of addresses from which event logs should originate.
|
|
|
|
// Optional, default nil.
|
|
|
|
// The JSON decoding must treat a string as equivalent to an array with one value, for example
|
|
|
|
// "0x8888f1f195afa192cfee86069858" must be decoded as [ "0x8888f1f195afa192cfee86069858" ]
|
|
|
|
Address EthAddressList `json:"address"`
|
|
|
|
|
|
|
|
// List of topics to be matched.
|
|
|
|
// Optional, default: empty list
|
|
|
|
Topics EthTopicSpec `json:"topics"`
|
|
|
|
|
2022-11-14 10:27:06 +00:00
|
|
|
// Restricts event logs returned to those emitted from messages contained in this tipset.
|
2022-11-10 11:27:58 +00:00
|
|
|
// If BlockHash is present in in the filter criteria, then neither FromBlock nor ToBlock are allowed.
|
|
|
|
// Added in EIP-234
|
|
|
|
BlockHash *EthHash `json:"blockHash,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// EthAddressSpec represents a list of addresses.
|
|
|
|
// The JSON decoding must treat a string as equivalent to an array with one value, for example
|
|
|
|
// "0x8888f1f195afa192cfee86069858" must be decoded as [ "0x8888f1f195afa192cfee86069858" ]
|
|
|
|
type EthAddressList []EthAddress
|
|
|
|
|
|
|
|
func (e *EthAddressList) UnmarshalJSON(b []byte) error {
|
2022-11-15 17:28:26 +00:00
|
|
|
if bytes.Equal(b, []byte{'n', 'u', 'l', 'l'}) {
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
if len(b) > 0 && b[0] == '[' {
|
|
|
|
var addrs []EthAddress
|
|
|
|
err := json.Unmarshal(b, &addrs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = addrs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var addr EthAddress
|
|
|
|
err := json.Unmarshal(b, &addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = []EthAddress{addr}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TopicSpec represents a specification for matching by topic. An empty spec means all topics
|
|
|
|
// will be matched. Otherwise topics are matched conjunctively in the first dimension of the
|
|
|
|
// slice and disjunctively in the second dimension. Topics are matched in order.
|
|
|
|
// An event log with topics [A, B] will be matched by the following topic specs:
|
|
|
|
// [] "all"
|
|
|
|
// [[A]] "A in first position (and anything after)"
|
|
|
|
// [nil, [B] ] "anything in first position AND B in second position (and anything after)"
|
|
|
|
// [[A], [B]] "A in first position AND B in second position (and anything after)"
|
|
|
|
// [[A, B], [A, B]] "(A OR B) in first position AND (A OR B) in second position (and anything after)"
|
|
|
|
//
|
|
|
|
// The JSON decoding must treat string values as equivalent to arrays with one value, for example
|
|
|
|
// { "A", [ "B", "C" ] } must be decoded as [ [ A ], [ B, C ] ]
|
|
|
|
type EthTopicSpec []EthHashList
|
|
|
|
|
2022-11-14 10:27:06 +00:00
|
|
|
// EthHashList represents a list of EthHashes.
|
|
|
|
// The JSON decoding treats string values as equivalent to arrays with one value.
|
2022-11-10 11:27:58 +00:00
|
|
|
type EthHashList []EthHash
|
|
|
|
|
|
|
|
func (e *EthHashList) UnmarshalJSON(b []byte) error {
|
|
|
|
if bytes.Equal(b, []byte{'n', 'u', 'l', 'l'}) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(b) > 0 && b[0] == '[' {
|
|
|
|
var hashes []EthHash
|
|
|
|
err := json.Unmarshal(b, &hashes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = hashes
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var hash EthHash
|
|
|
|
err := json.Unmarshal(b, &hash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = []EthHash{hash}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-15 17:28:26 +00:00
|
|
|
// FilterResult represents the response from executing a filter: a list of block hashes, a list of transaction hashes
|
2022-11-10 11:27:58 +00:00
|
|
|
// or a list of logs
|
|
|
|
// This is a union type. Only one field will be populated.
|
|
|
|
// The JSON encoding must produce an array of the populated field.
|
|
|
|
type EthFilterResult struct {
|
2022-11-15 17:28:26 +00:00
|
|
|
Results []interface{}
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h EthFilterResult) MarshalJSON() ([]byte, error) {
|
2022-11-15 17:28:26 +00:00
|
|
|
if h.Results != nil {
|
|
|
|
return json.Marshal(h.Results)
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
return []byte{'[', ']'}, nil
|
|
|
|
}
|
|
|
|
|
2022-11-15 17:28:26 +00:00
|
|
|
func (h *EthFilterResult) UnmarshalJSON(b []byte) error {
|
|
|
|
if bytes.Equal(b, []byte{'n', 'u', 'l', 'l'}) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := json.Unmarshal(b, &h.Results)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-14 10:27:06 +00:00
|
|
|
// EthLog represents the results of an event filter execution.
|
2022-11-10 11:27:58 +00:00
|
|
|
type EthLog struct {
|
|
|
|
// Address is the address of the actor that produced the event log.
|
|
|
|
Address EthAddress `json:"address"`
|
|
|
|
|
|
|
|
// Data is the values of the event log, excluding topics
|
|
|
|
Data []EthHash `json:"data"`
|
|
|
|
|
|
|
|
// List of topics associated with the event log.
|
|
|
|
Topics []EthHash `json:"topics"`
|
|
|
|
|
|
|
|
// Following fields are derived from the transaction containing the log
|
|
|
|
|
|
|
|
// Indicates whether the log was removed due to a chain reorganization.
|
|
|
|
Removed bool `json:"removed"`
|
|
|
|
|
|
|
|
// LogIndex is the index of the event log in the sequence of events produced by the message execution.
|
|
|
|
// (this is the index in the events AMT on the message receipt)
|
|
|
|
LogIndex EthUint64 `json:"logIndex"`
|
|
|
|
|
|
|
|
// TransactionIndex is the index in the tipset of the transaction that produced the event log.
|
|
|
|
// The index corresponds to the sequence of messages produced by ChainGetParentMessages
|
|
|
|
TransactionIndex EthUint64 `json:"transactionIndex"`
|
|
|
|
|
2022-11-14 10:27:06 +00:00
|
|
|
// TransactionHash is the cid of the message that produced the event log.
|
2022-11-10 11:27:58 +00:00
|
|
|
TransactionHash EthHash `json:"transactionHash"`
|
|
|
|
|
2022-11-14 10:27:06 +00:00
|
|
|
// BlockHash is the hash of the tipset containing the message that produced the log.
|
2022-11-10 11:27:58 +00:00
|
|
|
BlockHash EthHash `json:"blockHash"`
|
|
|
|
|
2022-11-14 10:27:06 +00:00
|
|
|
// BlockNumber is the epoch of the tipset containing the message.
|
2022-11-10 11:27:58 +00:00
|
|
|
BlockNumber EthUint64 `json:"blockNumber"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type EthSubscriptionParams struct {
|
|
|
|
// List of topics to be matched.
|
|
|
|
// Optional, default: empty list
|
|
|
|
Topics EthTopicSpec `json:"topics,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type EthSubscriptionResponse struct {
|
|
|
|
// The persistent identifier for the subscription which can be used to unsubscribe.
|
|
|
|
SubscriptionID EthSubscriptionID `json:"subscription"`
|
|
|
|
|
|
|
|
// The object matching the subscription. This may be a Block (tipset), a Transaction (message) or an EthLog
|
|
|
|
Result interface{} `json:"result"`
|
|
|
|
}
|