Skip to content

Commit

Permalink
feat: use f4 eth addresses wherever possible
Browse files Browse the repository at this point in the history
  • Loading branch information
simlecode committed Dec 15, 2022
1 parent a10fd3b commit 624e834
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 28 deletions.
62 changes: 49 additions & 13 deletions app/submodule/eth/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,51 @@ func (a *ethAPI) ethBlockFromFilecoinTipSet(ctx context.Context, ts *types.TipSe
return block, nil
}

// lookupEthAddress makes its best effort at finding the Ethereum address for a
// Filecoin address. It does the following:
//
// 1. If the supplied address is an f410 address, we return its payload as the EthAddress.
// 2. Otherwise (f0, f1, f2, f3), we look up the actor on the state tree. If it has a predictable address, we return it if it's f410 address.
// 3. Otherwise, we fall back to returning a masked ID Ethereum address. If the supplied address is an f0 address, we
// use that ID to form the masked ID address.
// 4. Otherwise, we fetch the actor's ID from the state tree and form the masked ID with it.
func (a *ethAPI) lookupEthAddress(ctx context.Context, addr address.Address) (types.EthAddress, error) {
// Attempt to convert directly.
if ethAddr, ok, err := types.TryEthAddressFromFilecoinAddress(addr, false); err != nil {
return types.EthAddress{}, err
} else if ok {
return ethAddr, nil
}

// todo: 合入 v10 actor后
// Lookup on the target actor.
// actor, err := a.chain.StateGetActor(ctx, addr, types.EmptyTSK)
// if err != nil {
// return types.EthAddress{}, err
// }
// if actor.Address != nil {
// if ethAddr, ok, err := types.TryEthAddressFromFilecoinAddress(*actor.Address, false); err != nil {
// return types.EthAddress{}, err
// } else if ok {
// return ethAddr, nil
// }
// }

// Check if we already have an ID addr, and use it if possible.
if ethAddr, ok, err := types.TryEthAddressFromFilecoinAddress(addr, true); err != nil {
return types.EthAddress{}, err
} else if ok {
return ethAddr, nil
}

// Otherwise, resolve the ID addr.
idAddr, err := a.chain.StateLookupID(ctx, addr, types.EmptyTSK)
if err != nil {
return types.EthAddress{}, err
}
return types.EthAddressFromFilecoinAddress(idAddr)
}

func (a *ethAPI) ethTxFromFilecoinMessageLookup(ctx context.Context, msgLookup *types.MsgLookup) (types.EthTx, error) {
if msgLookup == nil {
return types.EthTx{}, fmt.Errorf("msg does not exist")
Expand All @@ -601,30 +646,21 @@ func (a *ethAPI) ethTxFromFilecoinMessageLookup(ctx context.Context, msgLookup *
return types.EthTx{}, err
}

fromFilIDAddr, err := a.chain.StateLookupID(ctx, msg.To, types.EmptyTSK)
if err != nil {
return types.EthTx{}, err
}

fromEthAddr, err := types.EthAddressFromFilecoinIDAddress(fromFilIDAddr)
if err != nil {
return types.EthTx{}, err
}

toFilAddr, err := a.chain.StateLookupID(ctx, msg.From, types.EmptyTSK)
fromEthAddr, err := a.lookupEthAddress(ctx, msg.From)
if err != nil {
return types.EthTx{}, err
}

toEthAddr, err := types.EthAddressFromFilecoinIDAddress(toFilAddr)
toEthAddr, err := a.lookupEthAddress(ctx, msg.To)
if err != nil {
return types.EthTx{}, err
}

toAddr := &toEthAddr
input := msg.Params
// Check to see if we need to decode as contract deployment.
if toFilAddr == builtintypes.EthereumAddressManagerActorAddr {
// We don't need to resolve the to address, because there's only one form (an ID).
if msg.To == builtintypes.EthereumAddressManagerActorAddr {
switch msg.Method {
case builtintypes.MethodsEAM.Create:
toAddr = nil
Expand Down
16 changes: 2 additions & 14 deletions venus-shared/types/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,22 +287,10 @@ func (a EthAddress) ToFilecoinAddress() (address.Address, error) {
return address.NewIDAddress(id)
}

func EthAddressFromFilecoinIDAddress(addr address.Address) (EthAddress, error) {
id, err := address.IDFromAddress(addr)
if err != nil {
return EthAddress{}, err
}

var ethaddr EthAddress
ethaddr[0] = 0xff
binary.BigEndian.PutUint64(ethaddr[12:], id)
return ethaddr, nil
}

func TryEthAddressFromFilecoinAddress(addr address.Address, allowID bool) (EthAddress, bool, error) {
func TryEthAddressFromFilecoinAddress(addr address.Address, allowId bool) (EthAddress, bool, error) {
switch addr.Protocol() {
case address.ID:
if !allowID {
if !allowId {
return EthAddress{}, false, nil
}
id, err := address.IDFromAddress(addr)
Expand Down
2 changes: 1 addition & 1 deletion venus-shared/types/eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestParseEthAddr(t *testing.T) {
addr, err := address.NewIDAddress(id)
require.Nil(t, err)

eaddr, err := EthAddressFromFilecoinIDAddress(addr)
eaddr, err := EthAddressFromFilecoinAddress(addr)
require.Nil(t, err)

faddr, err := eaddr.ToFilecoinAddress()
Expand Down

0 comments on commit 624e834

Please sign in to comment.