fixes to Eth JSON-RPC API.

This commit is contained in:
Raúl Kripalani 2022-10-22 16:27:09 +01:00 committed by vyzo
parent 0cff785068
commit a58e87b173

View File

@ -3,6 +3,7 @@ package full
import ( import (
"bytes" "bytes"
"context" "context"
"encoding/binary"
"fmt" "fmt"
"strconv" "strconv"
@ -17,7 +18,6 @@ import (
builtintypes "github.com/filecoin-project/go-state-types/builtin" builtintypes "github.com/filecoin-project/go-state-types/builtin"
"github.com/filecoin-project/go-state-types/builtin/v8/eam" "github.com/filecoin-project/go-state-types/builtin/v8/eam"
"github.com/filecoin-project/go-state-types/builtin/v8/evm" "github.com/filecoin-project/go-state-types/builtin/v8/evm"
init8 "github.com/filecoin-project/go-state-types/builtin/v8/init"
"github.com/filecoin-project/specs-actors/actors/builtin" "github.com/filecoin-project/specs-actors/actors/builtin"
"github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api"
@ -54,6 +54,7 @@ type EthModuleAPI interface {
EthCall(ctx context.Context, tx api.EthCall, blkParam string) (api.EthBytes, error) EthCall(ctx context.Context, tx api.EthCall, blkParam string) (api.EthBytes, error)
EthMaxPriorityFeePerGas(ctx context.Context) (api.EthBigInt, error) EthMaxPriorityFeePerGas(ctx context.Context) (api.EthBigInt, error)
EthSendRawTransaction(ctx context.Context, rawTx api.EthBytes) (api.EthHash, error) EthSendRawTransaction(ctx context.Context, rawTx api.EthBytes) (api.EthHash, error)
// EthFeeHistory(ctx context.Context, blkCount string)
} }
var _ EthModuleAPI = *new(api.FullNode) var _ EthModuleAPI = *new(api.FullNode)
@ -361,7 +362,9 @@ func (a *EthModule) EthGetBalance(ctx context.Context, address api.EthAddress, b
} }
actor, err := a.StateGetActor(ctx, filAddr, types.EmptyTSK) actor, err := a.StateGetActor(ctx, filAddr, types.EmptyTSK)
if err != nil { if xerrors.Is(err, types.ErrActorNotFound) {
return api.EthBigIntZero, nil
} else if err != nil {
return api.EthBigInt{}, err return api.EthBigInt{}, err
} }
@ -444,47 +447,54 @@ func (a *EthModule) applyEvmMsg(ctx context.Context, tx api.EthCall) (*api.Invoc
var params []byte var params []byte
var to address.Address var to address.Address
var method abi.MethodNum
if tx.To == nil { if tx.To == nil {
// TODO need to construct the actor through the EAM so that it acquires an f4 address. // this is a contract creation
// https://github.com/filecoin-project/ref-fvm/issues/992 to = builtintypes.EthereumAddressManagerActorAddr
to = builtintypes.InitActorAddr
constructorParams, err := actors.SerializeParams(&evm.ConstructorParams{ nonce, err := a.Mpool.GetNonce(ctx, from, types.EmptyTSK)
if err != nil {
nonce = 0 // assume a zero nonce on error (e.g. sender doesn't exist).
}
var salt [32]byte
binary.BigEndian.PutUint64(salt[:], nonce)
// TODO this probably needs to be Create instead of Create2, but Create
// is not callable externally.
params2, err := actors.SerializeParams(&eam.Create2Params{
Initcode: tx.Data, Initcode: tx.Data,
Salt: salt,
}) })
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to serialize constructor params: %w", err) return nil, fmt.Errorf("failed to serialize Create2 params: %w", err)
}
evmActorCid, ok := actors.GetActorCodeID(actors.Version8, "evm")
if !ok {
return nil, fmt.Errorf("failed to lookup evm actor code CID")
}
params, err = actors.SerializeParams(&init8.ExecParams{
CodeCID: evmActorCid,
ConstructorParams: constructorParams,
})
if err != nil {
return nil, fmt.Errorf("failed to serialize init actor exec params: %w", err)
} }
params = params2
method = builtintypes.MethodsEAM.Create2
} else { } else {
addr, err := tx.To.ToFilecoinAddress() addr, err := tx.To.ToFilecoinAddress()
if err != nil { if err != nil {
return nil, xerrors.Errorf("cannot get Filecoin address: %w", err) return nil, xerrors.Errorf("cannot get Filecoin address: %w", err)
} }
to = addr to = addr
if len(tx.Data) > 0 {
var buf bytes.Buffer var buf bytes.Buffer
if err := cbg.WriteByteArray(&buf, tx.Data); err != nil { if err := cbg.WriteByteArray(&buf, tx.Data); err != nil {
return nil, fmt.Errorf("failed to encode tx input into a cbor byte-string") return nil, fmt.Errorf("failed to encode tx input into a cbor byte-string")
} }
params = buf.Bytes() params = buf.Bytes()
method = builtintypes.MethodsEVM.InvokeContract
} else {
method = builtintypes.MethodSend
}
} }
msg := &types.Message{ msg := &types.Message{
From: from, From: from,
To: to, To: to,
Value: big.Int(tx.Value), Value: big.Int(tx.Value),
Method: builtintypes.MethodsEVM.InvokeContract, Method: method,
Params: params, Params: params,
GasLimit: build.BlockGasLimit, GasLimit: build.BlockGasLimit,
GasFeeCap: big.Zero(), GasFeeCap: big.Zero(),