Skip to content

Commit

Permalink
feat: API for EthGetMessageCidByTransactionHash
Browse files Browse the repository at this point in the history
  • Loading branch information
simlecode committed Jan 30, 2023
1 parent a50d90c commit 6e2b634
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 23 deletions.
64 changes: 58 additions & 6 deletions app/submodule/eth/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,6 @@ func (a *ethAPI) EthGetBlockByHash(ctx context.Context, blkHash types.EthHash, f
return newEthBlockFromFilecoinTipSet(ctx, ts, fullTxInfo, a.em.chainModule.MessageStore, a.chain)
}

func (a *ethAPI) EthGetTransactionHashByCid(ctx context.Context, cid cid.Cid) (*types.EthHash, error) {
hash, err := ethTxHashFromFilecoinMessageCid(ctx, cid, a.em.chainModule.MessageStore, a.chain)
return &hash, err
}

func (a *ethAPI) parseBlkParam(ctx context.Context, blkParam string) (tipset *types.TipSet, err error) {
if blkParam == "earliest" {
return nil, fmt.Errorf("block param \"earliest\" is not supported")
Expand Down Expand Up @@ -210,6 +205,56 @@ func (a *ethAPI) EthGetTransactionByHash(ctx context.Context, txHash *types.EthH
return nil, nil
}

func (a *ethAPI) EthGetMessageCidByTransactionHash(ctx context.Context, txHash *types.EthHash) (*cid.Cid, error) {
// Ethereum's behavior is to return null when the txHash is invalid, so we use nil to check if txHash is valid
if txHash == nil {
return nil, nil
}

c := cid.Undef
if a.ethTxHashManager != nil {
var err error
c, err = a.ethTxHashManager.TransactionHashLookup.GetCidFromHash(*txHash)
// We fall out of the first condition and continue
if errors.Is(err, ethhashlookup.ErrNotFound) {
log.Debug("could not find transaction hash %s in lookup table", txHash.String())
} else if err != nil {
return nil, fmt.Errorf("database error: %w", err)
} else {
return &c, nil
}
}
// This isn't an eth transaction we have the mapping for, so let's try looking it up as a filecoin message
if c == cid.Undef {
c = txHash.ToCid()
}

_, err := a.em.chainModule.MessageStore.LoadSignedMessage(ctx, c)
if err == nil {
// This is an Eth Tx, Secp message, Or BLS message in the mpool
return &c, nil
}

_, err = a.em.chainModule.MessageStore.LoadUnsignedMessage(ctx, c)
if err == nil {
// This is a BLS message
return &c, nil
}

// Ethereum clients expect an empty response when the message was not found
return nil, nil
}

func (a *ethAPI) EthGetTransactionHashByCid(ctx context.Context, cid cid.Cid) (*types.EthHash, error) {
hash, err := ethTxHashFromFilecoinMessageCid(ctx, cid, a.em.chainModule.MessageStore, a.chain)
if hash == types.EmptyEthHash {
// not found
return nil, nil
}

return &hash, err
}

func (a *ethAPI) EthGetTransactionCount(ctx context.Context, sender types.EthAddress, blkParam string) (types.EthUint64, error) {
addr, err := sender.ToFilecoinAddress()
if err != nil {
Expand Down Expand Up @@ -826,10 +871,17 @@ func lookupEthAddress(ctx context.Context, addr address.Address, ca v1.IChain) (
func ethTxHashFromFilecoinMessageCid(ctx context.Context, c cid.Cid, ms *chain.MessageStore, ca v1.IChain) (types.EthHash, error) {
smsg, err := ms.LoadSignedMessage(ctx, c)
if err == nil {
// This is an Eth Tx, Secp message, Or BLS message in the mpool
return ethTxHashFromSignedFilecoinMessage(ctx, smsg, ca)
}

return types.EthHashFromCid(c)
_, err = ms.LoadUnsignedMessage(ctx, c)
if err == nil {
// This is a BLS message
return types.EthHashFromCid(c)
}

return types.EmptyEthHash, nil
}

func ethTxHashFromSignedFilecoinMessage(ctx context.Context, smsg *types.SignedMessage, ca v1.IChain) (types.EthHash, error) {
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ type FevmConfig struct {
func newFevmConfig() *FevmConfig {
return &FevmConfig{
EnableEthHashToFilecoinCidMapping: false,
EthTxHashMappingLifetimeDays: 0,
}
}

Expand Down
17 changes: 0 additions & 17 deletions pkg/events/filter/index.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package filter

import (
"bytes"
"context"
"database/sql"
"errors"
Expand All @@ -11,7 +10,6 @@ import (

"github.com/ipfs/go-cid"
_ "github.com/mattn/go-sqlite3"
cbg "github.com/whyrusleeping/cbor-gen"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
Expand Down Expand Up @@ -217,21 +215,6 @@ func (ei *EventIndex) CollectEvents(ctx context.Context, te *TipSetEvents, rever
return nil
}

// decodeLogBytes decodes a CBOR-serialized array into its original form.
//
// This function swallows errors and returns the original array if it failed
// to decode.
func decodeLogBytes(orig []byte) []byte {
if len(orig) == 0 {
return orig
}
decoded, err := cbg.ReadByteArray(bytes.NewReader(orig), uint64(len(orig)))
if err != nil {
return orig
}
return decoded
}

// PrefillFilter fills a filter's collection of events from the historic index
func (ei *EventIndex) PrefillFilter(ctx context.Context, f *EventFilter) error {
clauses := []string{}
Expand Down
1 change: 1 addition & 0 deletions venus-shared/api/chain/v1/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type IETH interface {
EthGetBlockByNumber(ctx context.Context, blkNum string, fullTxInfo bool) (types.EthBlock, error) //perm:read
EthGetTransactionByHash(ctx context.Context, txHash *types.EthHash) (*types.EthTx, error) //perm:read
EthGetTransactionHashByCid(ctx context.Context, cid cid.Cid) (*types.EthHash, error) //perm:read
EthGetMessageCidByTransactionHash(ctx context.Context, txHash *types.EthHash) (*cid.Cid, error) //perm:read
EthGetTransactionCount(ctx context.Context, sender types.EthAddress, blkOpt string) (types.EthUint64, error) //perm:read
EthGetTransactionReceipt(ctx context.Context, txHash types.EthHash) (*types.EthTxReceipt, error) //perm:read
EthGetTransactionByBlockHashAndIndex(ctx context.Context, blkHash types.EthHash, txIndex types.EthUint64) (types.EthTx, error) //perm:read
Expand Down
15 changes: 15 additions & 0 deletions venus-shared/api/chain/v1/method.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
* [EthGetBlockTransactionCountByHash](#ethgetblocktransactioncountbyhash)
* [EthGetBlockTransactionCountByNumber](#ethgetblocktransactioncountbynumber)
* [EthGetCode](#ethgetcode)
* [EthGetMessageCidByTransactionHash](#ethgetmessagecidbytransactionhash)
* [EthGetStorageAt](#ethgetstorageat)
* [EthGetTransactionByBlockHashAndIndex](#ethgettransactionbyblockhashandindex)
* [EthGetTransactionByBlockNumberAndIndex](#ethgettransactionbyblocknumberandindex)
Expand Down Expand Up @@ -2371,6 +2372,20 @@ Inputs:

Response: `"0x07"`

### EthGetMessageCidByTransactionHash


Perms: read

Inputs:
```json
[
"0x37690cfec6c1bf4c3b9288c7a5d783e98731e90b0a4c177c2a374c7a9427355e"
]
```

Response: `null`

### EthGetStorageAt


Expand Down
15 changes: 15 additions & 0 deletions venus-shared/api/chain/v1/mock/mock_fullnode.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions venus-shared/api/chain/v1/proxy_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions venus-shared/compatible-checks/api-diff.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ github.com/filecoin-project/venus/venus-shared/api/chain/v1.FullNode <> github.c
+ Concurrent
- CreateBackup
- Discover
+ EthGetMessageCidByTransactionHash
+ EthGetTransactionHashByCid
+ GasBatchEstimateMessageGas
> GasEstimateMessageGas {[func(context.Context, *internal.Message, *types.MessageSendSpec, types.TipSetKey) (*internal.Message, error) <> func(context.Context, *types.Message, *api.MessageSendSpec, types.TipSetKey) (*types.Message, error)] base=func in type: #2 input; nested={[*types.MessageSendSpec <> *api.MessageSendSpec] base=pointed type; nested={[types.MessageSendSpec <> api.MessageSendSpec] base=struct field; nested={[types.MessageSendSpec <> api.MessageSendSpec] base=exported fields count: 3 != 2; nested=nil}}}}
Expand Down
1 change: 1 addition & 0 deletions venus-shared/compatible-checks/api-perm.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ v1: github.com/filecoin-project/venus/venus-shared/api/chain/v1 <> github.com/fi
- IChainInfo.VerifyEntry
- IMinerState.StateMinerSectorSize
- IMinerState.StateMinerWorkerAddress
- IETH.EthGetMessageCidByTransactionHash
- IETH.EthGetTransactionHashByCid
- IMessagePool.GasBatchEstimateMessageGas
- IMessagePool.MpoolDeleteByAdress
Expand Down

0 comments on commit 6e2b634

Please sign in to comment.