Skip to content

Commit

Permalink
Merge pull request #5719 from filecoin-project/feat/transport-code
Browse files Browse the repository at this point in the history
Feat/transport code
  • Loading branch information
diwufeiwen authored Feb 13, 2023
2 parents 154fdad + 19237aa commit 1e8f438
Show file tree
Hide file tree
Showing 28 changed files with 394 additions and 242 deletions.
2 changes: 1 addition & 1 deletion app/submodule/eth/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/filecoin-project/venus/venus-shared/types"
)

var ErrModuleDisabled = errors.New("module disabled, enable with Fevm.EnableEthRPC")
var ErrModuleDisabled = errors.New("module disabled, enable with Fevm.EnableEthRPC / VENUS_FEVM_ENABLEETHRPC")

type ethAPIDummy struct{}

Expand Down
88 changes: 72 additions & 16 deletions app/submodule/eth/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"time"
Expand All @@ -18,6 +19,7 @@ import (
"github.com/filecoin-project/go-state-types/exitcode"
"github.com/filecoin-project/specs-actors/actors/builtin"
"github.com/filecoin-project/venus/pkg/chain"
"github.com/filecoin-project/venus/pkg/config"
"github.com/filecoin-project/venus/pkg/constants"
"github.com/filecoin-project/venus/pkg/crypto"
"github.com/filecoin-project/venus/pkg/ethhashlookup"
Expand Down Expand Up @@ -45,16 +47,31 @@ func newEthAPI(em *EthSubModule) (*ethAPI, error) {
mpool: em.mpoolModule.API(),
}

transactionHashLookup, err := ethhashlookup.NewTransactionHashLookup(filepath.Join(a.em.sqlitePath, "txhash.db"))
dbPath := filepath.Join(a.em.sqlitePath, "txhash.db")

// Check if the db exists, if not, we'll back-fill some entries
_, err := os.Stat(dbPath)
dbAlreadyExists := err == nil

transactionHashLookup, err := ethhashlookup.NewTransactionHashLookup(dbPath)
if err != nil {
return nil, err
}

a.ethTxHashManager = &ethTxHashManager{
chainAPI: a.chain,
messageStore: em.chainModule.MessageStore,
forkUpgradeConfig: em.cfg.NetworkParams.ForkUpgradeParam,
TransactionHashLookup: transactionHashLookup,
}

if !dbAlreadyExists {
err = a.ethTxHashManager.PopulateExistingMappings(em.ctx, 0)
if err != nil {
return nil, err
}
}

return a, nil
}

Expand Down Expand Up @@ -699,10 +716,8 @@ func (a *ethAPI) ethCallToFilecoinMessage(ctx context.Context, tx types.EthCall)
return nil, fmt.Errorf("failed to encode tx input into a cbor byte-string")
}
params = buf.Bytes()
method = builtintypes.MethodsEVM.InvokeContract
} else {
method = builtintypes.MethodSend
}
method = builtintypes.MethodsEVM.InvokeContract
}

return &types.Message{
Expand Down Expand Up @@ -1111,6 +1126,7 @@ func ethTxFromNativeMessage(ctx context.Context, msg *types.Message, ca v1.IChai
Gas: types.EthUint64(msg.GasLimit),
MaxFeePerGas: types.EthBigInt(msg.GasFeeCap),
MaxPriorityFeePerGas: types.EthBigInt(msg.GasPremium),
AccessList: []types.EthHash{},
}
}

Expand Down Expand Up @@ -1292,6 +1308,8 @@ func newEthTxReceipt(ctx context.Context, tx types.EthTx, lookup *types.MsgLooku

type ethTxHashManager struct {
chainAPI v1.IChain
messageStore *chain.MessageStore
forkUpgradeConfig *config.ForkUpgradeConfig
TransactionHashLookup *ethhashlookup.EthTxHashLookup
}

Expand Down Expand Up @@ -1326,6 +1344,55 @@ func (m *ethTxHashManager) Revert(ctx context.Context, from, to *types.TipSet) e
return nil
}

func (m *ethTxHashManager) PopulateExistingMappings(ctx context.Context, minHeight abi.ChainEpoch) error {
if minHeight < m.forkUpgradeConfig.UpgradeHyggeHeight {
minHeight = m.forkUpgradeConfig.UpgradeHyggeHeight
}

ts, err := m.chainAPI.ChainHead(ctx)
if err != nil {
return err
}
for ts.Height() > minHeight {
for _, block := range ts.Blocks() {
msgs, err := m.messageStore.SecpkMessagesForBlock(ctx, block)
if err != nil {
// If we can't find the messages, we've either imported from snapshot or pruned the store
log.Debug("exiting message mapping population at epoch ", ts.Height())
return nil
}

for _, msg := range msgs {
m.ProcessSignedMessage(ctx, msg)
}
}

var err error
ts, err = m.chainAPI.ChainGetTipSet(ctx, ts.Parents())
if err != nil {
return err
}
}

return nil
}

func (m *ethTxHashManager) ProcessSignedMessage(ctx context.Context, msg *types.SignedMessage) {
if msg.Signature.Type != crypto.SigTypeDelegated {
return
}

ethTx, err := newEthTxFromSignedMessage(ctx, msg, m.chainAPI)
if err != nil {
log.Errorf("error converting filecoin message to eth tx: %s", err)
}

err = m.TransactionHashLookup.UpsertHash(ethTx.Hash, msg.Cid())
if err != nil {
log.Errorf("error inserting tx mapping to db: %s", err)
}
}

func waitForMpoolUpdates(ctx context.Context, ch <-chan types.MpoolUpdate, manager *ethTxHashManager) {
for {
select {
Expand All @@ -1335,19 +1402,8 @@ func waitForMpoolUpdates(ctx context.Context, ch <-chan types.MpoolUpdate, manag
if u.Type != types.MpoolAdd {
continue
}
if u.Message.Signature.Type != crypto.SigTypeDelegated {
continue
}

ethTx, err := newEthTxFromSignedMessage(ctx, u.Message, manager.chainAPI)
if err != nil {
log.Errorf("error converting filecoin message to eth tx: %s", err)
}

err = manager.TransactionHashLookup.UpsertHash(ethTx.Hash, u.Message.Cid())
if err != nil {
log.Errorf("error inserting tx mapping to db: %s", err)
}
manager.ProcessSignedMessage(ctx, u.Message)
}
}
}
Expand Down
51 changes: 28 additions & 23 deletions app/submodule/eth/eth_event_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,45 +701,50 @@ func (e *ethSubscription) addFilter(ctx context.Context, f filter.Filter) {
e.filters = append(e.filters, f)
}

func (e *ethSubscription) send(ctx context.Context, v interface{}) {
resp := types.EthSubscriptionResponse{
SubscriptionID: e.id,
Result: v,
}

outParam, err := json.Marshal(resp)
if err != nil {
log.Warnw("marshaling subscription response", "sub", e.id, "error", err)
return
}

if err := e.out(ctx, outParam); err != nil {
log.Warnw("sending subscription response", "sub", e.id, "error", err)
return
}
}

func (e *ethSubscription) start(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case v := <-e.in:
resp := types.EthSubscriptionResponse{
SubscriptionID: e.id,
}

var err error
switch vt := v.(type) {
case *filter.CollectedEvent:
resp.Result, err = ethFilterResultFromEvents([]*filter.CollectedEvent{vt}, e.messageStore, e.chainAPI)
evs, err := ethFilterResultFromEvents([]*filter.CollectedEvent{vt}, e.messageStore, e.chainAPI)
if err != nil {
continue
}

for _, r := range evs.Results {
e.send(ctx, r)
}
case *types.TipSet:
eb, err := newEthBlockFromFilecoinTipSet(ctx, vt, true, e.messageStore, e.chainAPI)
ev, err := newEthBlockFromFilecoinTipSet(ctx, vt, true, e.messageStore, e.chainAPI)
if err != nil {
break
}

resp.Result = eb
e.send(ctx, ev)
default:
log.Warnf("unexpected subscription value type: %T", vt)
}

if err != nil {
continue
}

outParam, err := json.Marshal(resp)
if err != nil {
log.Warnw("marshaling subscription response", "sub", e.id, "error", err)
continue
}

if err := e.out(ctx, outParam); err != nil {
log.Warnw("sending subscription response", "sub", e.id, "error", err)
continue
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion app/submodule/eth/eth_submodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/filecoin-project/venus/app/submodule/chain"
"github.com/filecoin-project/venus/app/submodule/mpool"
"github.com/filecoin-project/venus/pkg/config"
"github.com/filecoin-project/venus/pkg/constants"
v1api "github.com/filecoin-project/venus/venus-shared/api/chain/v1"
)

Expand All @@ -32,7 +33,8 @@ func NewEthSubModule(ctx context.Context,
em.ethEventAPI = ee

em.ethAPIAdapter = &ethAPIDummy{}
if em.cfg.FevmConfig.EnableEthRPC {
if em.cfg.FevmConfig.EnableEthRPC || constants.FevmEnableEthRPC {
log.Debug("enable eth rpc")
em.ethAPIAdapter, err = newEthAPI(em)
if err != nil {
return nil, err
Expand Down
10 changes: 5 additions & 5 deletions cmd/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ var evmCallSimulateCmd = &cmds.Command{
return err
}

params, err := types.DecodeHexString(req.Arguments[2])
params, err := types.DecodeHexStringTrimSpace(req.Arguments[2])
if err != nil {
return err
}
Expand Down Expand Up @@ -155,7 +155,7 @@ var evmGetContractAddressCmd = &cmds.Command{
return err
}

salt, err := types.DecodeHexString(req.Arguments[1])
salt, err := types.DecodeHexStringTrimSpace(req.Arguments[1])
if err != nil {
return fmt.Errorf("could not decode salt: %v", err)
}
Expand All @@ -174,7 +174,7 @@ var evmGetContractAddressCmd = &cmds.Command{

return err
}
contract, err := types.DecodeHexString(string(contractHex))
contract, err := types.DecodeHexStringTrimSpace(string(contractHex))
if err != nil {
return fmt.Errorf("could not decode contract file: %v", err)
}
Expand Down Expand Up @@ -210,7 +210,7 @@ var evmDeployCmd = &cmds.Command{
return fmt.Errorf("failed to read contract: %w", err)
}
if isHex, _ := req.Options["hex"].(bool); isHex {
contract, err = types.DecodeHexString(string(contract))
contract, err = types.DecodeHexStringTrimSpace(string(contract))
if err != nil {
return fmt.Errorf("failed to decode contract: %w", err)
}
Expand Down Expand Up @@ -327,7 +327,7 @@ var evmInvokeCmd = &cmds.Command{
return fmt.Errorf("failed to decode address: %w", err)
}

callData, err := types.DecodeHexString(req.Arguments[1])
callData, err := types.DecodeHexStringTrimSpace(req.Arguments[1])
if err != nil {
return fmt.Errorf("decoding hex input data: %w", err)
}
Expand Down
22 changes: 17 additions & 5 deletions cmd/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ var msgSendCmd = &cmds.Command{
cmds.Uint64Option("method", "The method to invoke on the target actor"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
ctx := req.Context

toAddr, err := address.NewFromString(req.Arguments[0])
if err != nil {
return err
Expand Down Expand Up @@ -123,6 +125,16 @@ var msgSendCmd = &cmds.Command{
}
}

if fromAddr.Protocol() == address.Delegated {
if !(toAddr.Protocol() == address.ID || toAddr.Protocol() == address.Delegated) {
// Resolve id addr if possible.
toAddr, err = env.(*node.Env).ChainAPI.StateLookupID(ctx, toAddr, types.EmptyTSK)
if err != nil {
return fmt.Errorf("f4 addresses can only send to other f4 or id addresses. could not find id address for %s", toAddr.String())
}
}
}

if methodID == builtin.MethodSend && fromAddr.String() == toAddr.String() {
return errors.New("self-transfer is not allowed")
}
Expand All @@ -132,14 +144,14 @@ var msgSendCmd = &cmds.Command{
return err
}

if err := utils.LoadBuiltinActors(req.Context, env.(*node.Env).ChainAPI); err != nil {
if err := utils.LoadBuiltinActors(ctx, env.(*node.Env).ChainAPI); err != nil {
return err
}

var params []byte
rawPJ := req.Options["params-json"]
if rawPJ != nil {
decparams, err := decodeTypedParams(req.Context, env.(*node.Env), toAddr, methodID, rawPJ.(string))
decparams, err := decodeTypedParams(ctx, env.(*node.Env), toAddr, methodID, rawPJ.(string))
if err != nil {
return fmt.Errorf("failed to decode json params: %s", err)
}
Expand Down Expand Up @@ -178,18 +190,18 @@ var msgSendCmd = &cmds.Command{
}
msg.Nonce = nonce

sm, err := env.(*node.Env).WalletAPI.WalletSignMessage(req.Context, msg.From, msg)
sm, err := env.(*node.Env).WalletAPI.WalletSignMessage(ctx, msg.From, msg)
if err != nil {
return err
}

_, err = env.(*node.Env).MessagePoolAPI.MpoolPush(req.Context, sm)
_, err = env.(*node.Env).MessagePoolAPI.MpoolPush(ctx, sm)
if err != nil {
return err
}
c = sm.Cid()
} else {
sm, err := env.(*node.Env).MessagePoolAPI.MpoolPushMessage(req.Context, msg, nil)
sm, err := env.(*node.Env).MessagePoolAPI.MpoolPushMessage(ctx, msg, nil)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion extern/filecoin-ffi
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ require (
github.com/filecoin-project/go-fil-markets v1.25.2
github.com/filecoin-project/go-jsonrpc v0.1.5
github.com/filecoin-project/go-paramfetch v0.0.4
github.com/filecoin-project/go-state-types v0.10.0-alpha-11
github.com/filecoin-project/go-state-types v0.10.0-rc2
github.com/filecoin-project/pubsub v1.0.0
github.com/filecoin-project/specs-actors v0.9.15
github.com/filecoin-project/specs-actors/v2 v2.3.6
Expand Down Expand Up @@ -316,6 +316,6 @@ require (

replace (
github.com/filecoin-project/filecoin-ffi => ./extern/filecoin-ffi
github.com/filecoin-project/go-jsonrpc => github.com/ipfs-force-community/go-jsonrpc v0.1.7-0.20230202064402-ca8d607e4c4b
github.com/filecoin-project/go-jsonrpc => github.com/ipfs-force-community/go-jsonrpc v0.1.7-0.20230208090556-076d7e409dc1
github.com/filecoin-project/test-vectors => ./extern/test-vectors
)
Loading

0 comments on commit 1e8f438

Please sign in to comment.