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"
|
|
|
|
|
2022-10-20 12:40:32 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/builtin"
|
2022-09-09 17:59:00 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"github.com/multiformats/go-multihash"
|
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-10-21 10:59:09 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-state-types/builtin"
|
|
|
|
"github.com/filecoin-project/go-state-types/builtin/v8/eam"
|
2022-09-28 14:58:24 +00:00
|
|
|
init8 "github.com/filecoin-project/go-state-types/builtin/v8/init"
|
2022-09-12 21:46:15 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
type EthBigInt big.Int
|
|
|
|
|
2022-09-12 21:46:15 +00:00
|
|
|
var (
|
|
|
|
EthBigIntZero = EthBigInt{Int: big.Zero().Int}
|
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
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-09-29 22:55:13 +00:00
|
|
|
Difficulty EthUint64 `json:"difficulty"`
|
|
|
|
Number EthUint64 `json:"number"`
|
|
|
|
GasLimit EthUint64 `json:"gasLimit"`
|
|
|
|
GasUsed EthUint64 `json:"gasUsed"`
|
|
|
|
Timestamp EthUint64 `json:"timestamp"`
|
2022-09-12 21:46:15 +00:00
|
|
|
Extradata []byte `json:"extraData"`
|
|
|
|
MixHash EthHash `json:"mixHash"`
|
|
|
|
Nonce EthNonce `json:"nonce"`
|
|
|
|
BaseFeePerGas EthBigInt `json:"baseFeePerGas"`
|
2022-09-29 22:55:13 +00:00
|
|
|
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-09-29 20:46:59 +00:00
|
|
|
From EthAddress `json:"from"`
|
|
|
|
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-09-28 14:58:24 +00:00
|
|
|
TransactionHash EthHash `json:"transactionHash"`
|
2022-09-29 22:55:13 +00:00
|
|
|
TransactionIndex EthUint64 `json:"transactionIndex"`
|
2022-09-28 14:58:24 +00:00
|
|
|
BlockHash EthHash `json:"blockHash"`
|
2022-09-29 22:55:13 +00:00
|
|
|
BlockNumber EthUint64 `json:"blockNumber"`
|
2022-09-28 14:58:24 +00:00
|
|
|
From EthAddress `json:"from"`
|
|
|
|
To *EthAddress `json:"to"`
|
2022-09-09 17:59:00 +00:00
|
|
|
// Logs
|
|
|
|
// LogsBloom
|
|
|
|
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"`
|
|
|
|
Logs []string `json:"logs"`
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 14:58:24 +00:00
|
|
|
func NewEthTxReceipt(tx EthTx, lookup *MsgLookup, replay *InvocResult) (EthTxReceipt, error) {
|
|
|
|
receipt := EthTxReceipt{
|
|
|
|
TransactionHash: tx.Hash,
|
|
|
|
TransactionIndex: tx.TransactionIndex,
|
|
|
|
BlockHash: tx.BlockHash,
|
|
|
|
BlockNumber: tx.BlockNumber,
|
|
|
|
From: tx.From,
|
|
|
|
To: tx.To,
|
|
|
|
StateRoot: EmptyEthHash,
|
2022-09-29 20:46:59 +00:00
|
|
|
LogsBloom: []byte{0},
|
|
|
|
Logs: []string{},
|
2022-09-28 14:58:24 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 10:59:09 +00:00
|
|
|
if receipt.To == nil && lookup.Receipt.ExitCode.IsSuccess() {
|
|
|
|
// Create and Create2 return the same things.
|
|
|
|
var ret eam.CreateReturn
|
|
|
|
if err := ret.UnmarshalCBOR(bytes.NewReader(lookup.Receipt.Return)); err != nil {
|
|
|
|
return EthTxReceipt{}, xerrors.Errorf("failed to parse contract creation result: %w", err)
|
|
|
|
}
|
|
|
|
addr := EthAddress(ret.EthAddress)
|
|
|
|
receipt.ContractAddress = &addr
|
2022-09-28 14:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if lookup.Receipt.ExitCode.IsSuccess() {
|
|
|
|
receipt.Status = 1
|
|
|
|
}
|
|
|
|
if lookup.Receipt.ExitCode.IsError() {
|
|
|
|
receipt.Status = 0
|
|
|
|
}
|
|
|
|
|
2022-09-29 22:55:13 +00:00
|
|
|
receipt.GasUsed = EthUint64(lookup.Receipt.GasUsed)
|
2022-09-28 14:58:24 +00:00
|
|
|
|
|
|
|
// TODO: handle CumulativeGasUsed
|
|
|
|
receipt.CumulativeGasUsed = EmptyEthInt
|
|
|
|
|
|
|
|
effectiveGasPrice := big.Div(replay.GasCost.TotalCost, big.NewInt(lookup.Receipt.GasUsed))
|
|
|
|
receipt.EffectiveGasPrice = EthBigInt(effectiveGasPrice)
|
|
|
|
return receipt, nil
|
|
|
|
}
|
|
|
|
|
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-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-10-20 12:40:32 +00:00
|
|
|
addr, err := address.NewDelegatedAddress(builtin.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
|
|
|
}
|
|
|
|
|
|
|
|
func EthAddressFromFilecoinIDAddress(addr address.Address) (EthAddress, error) {
|
|
|
|
id, err := address.IDFromAddress(addr)
|
|
|
|
if err != nil {
|
|
|
|
return EthAddress{}, err
|
|
|
|
}
|
|
|
|
var ethaddr EthAddress
|
2022-10-19 12:52:13 +00:00
|
|
|
ethaddr[0] = 0xff
|
|
|
|
binary.BigEndian.PutUint64(ethaddr[12:], id)
|
2022-09-09 17:59:00 +00:00
|
|
|
return ethaddr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func EthHashFromCid(c cid.Cid) (EthHash, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|