Skip to content

Commit

Permalink
Filecoin messages can again be converted to Eth Txs
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoff Stuart committed Jan 21, 2023
1 parent edad5f2 commit f1cbc87
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 32 deletions.
4 changes: 2 additions & 2 deletions chain/messagesigner/messagesigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ func (ms *MessageSigner) dstoreKey(addr address.Address) datastore.Key {

func SigningBytes(msg *types.Message, sigType crypto.SigType) ([]byte, error) {
if sigType == crypto.SigTypeDelegated {
txArgs, err := ethtypes.EthTxArgsFromMessage(msg)
txArgs, err := ethtypes.EthTxArgsFromUnsignedEthMessage(msg)
if err != nil {
return nil, xerrors.Errorf("failed to reconstruct eth transaction: %w", err)
return nil, xerrors.Errorf("failed to convert to eth message: %w", err)
}
rlpEncodedMsg, err := txArgs.ToRlpUnsignedMsg()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion chain/signatures.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func AuthenticateMessage(msg *types.SignedMessage, signer address.Address) error
typ := msg.Signature.Type
switch typ {
case crypto.SigTypeDelegated:
txArgs, err := ethtypes.EthTxArgsFromUnsignedMessage(&msg.Message)
txArgs, err := ethtypes.EthTxArgsFromUnsignedEthMessage(&msg.Message)
if err != nil {
return xerrors.Errorf("failed to reconstruct eth transaction: %w", err)
}
Expand Down
38 changes: 28 additions & 10 deletions chain/types/ethtypes/eth_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,18 @@ type EthTxArgs struct {
S big.Int `json:"s"`
}

// EthTxFromSignedMessage does NOT populate:
// EthTxFromMessageDelegatedSignature does NOT populate:
// - BlockHash
// - BlockNumber
// - TransactionIndex
// - From
func EthTxFromSignedMessage(smsg *types.SignedMessage) (EthTx, error) {
txArgs, err := EthTxArgsFromUnsignedMessage(&smsg.Message)
// - Hash
func EthTxFromMessageDelegatedSignature(smsg *types.SignedMessage) (EthTx, error) {
if smsg.Signature.Type != typescrypto.SigTypeDelegated {
return EthTx{}, xerrors.Errorf("signature is not delegated type, is type: %d", smsg.Signature.Type)
}

txArgs, err := EthTxArgsFromUnsignedEthMessage(&smsg.Message)
if err != nil {
return EthTx{}, xerrors.Errorf("failed to convert the unsigned message: %w", err)
}
Expand All @@ -75,13 +80,7 @@ func EthTxFromSignedMessage(smsg *types.SignedMessage) (EthTx, error) {
r, s, v = EthBigIntZero, EthBigIntZero, EthBigIntZero
}

hash, err := EthHashFromCid(smsg.Cid())
if err != nil {
return EthTx{}, xerrors.Errorf("failed to calculate EthHash: %w", err)
}

return EthTx{
Hash: hash,
Nonce: EthUint64(txArgs.Nonce),
ChainID: EthUint64(txArgs.ChainID),
To: txArgs.To,
Expand All @@ -97,7 +96,26 @@ func EthTxFromSignedMessage(smsg *types.SignedMessage) (EthTx, error) {
}, nil
}

func EthTxArgsFromUnsignedMessage(msg *types.Message) (EthTxArgs, error) {
// EthTxFromFilecoinMessage does NOT populate:
// - BlockHash
// - BlockNumber
// - TransactionIndex
// - Hash
// - From (not valid)
// - To (not valid)
func EthTxFromFilecoinMessage(msg *types.Message) EthTx {
return EthTx{
Nonce: EthUint64(msg.Nonce),
ChainID: EthUint64(build.Eip155ChainId),
Value: EthBigInt(msg.Value),
Type: Eip1559TxType,
Gas: EthUint64(msg.GasLimit),
MaxFeePerGas: EthBigInt(msg.GasFeeCap),
MaxPriorityFeePerGas: EthBigInt(msg.GasPremium),
}
}

func EthTxArgsFromUnsignedEthMessage(msg *types.Message) (EthTxArgs, error) {
var (
to *EthAddress
params []byte
Expand Down
8 changes: 4 additions & 4 deletions itests/eth_account_abstraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestEthAccountAbstraction(t *testing.T) {
msgFromPlaceholder, err = client.GasEstimateMessageGas(ctx, msgFromPlaceholder, nil, types.EmptyTSK)
require.NoError(t, err)

txArgs, err := ethtypes.EthTxArgsFromUnsignedMessage(msgFromPlaceholder)
txArgs, err := ethtypes.EthTxArgsFromUnsignedEthMessage(msgFromPlaceholder)
require.NoError(t, err)

digest, err := txArgs.ToRlpUnsignedMsg()
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestEthAccountAbstraction(t *testing.T) {
msgFromPlaceholder, err = client.GasEstimateMessageGas(ctx, msgFromPlaceholder, nil, types.EmptyTSK)
require.NoError(t, err)

txArgs, err = ethtypes.EthTxArgsFromUnsignedMessage(msgFromPlaceholder)
txArgs, err = ethtypes.EthTxArgsFromUnsignedEthMessage(msgFromPlaceholder)
require.NoError(t, err)

digest, err = txArgs.ToRlpUnsignedMsg()
Expand Down Expand Up @@ -178,7 +178,7 @@ func TestEthAccountAbstractionFailure(t *testing.T) {
require.NoError(t, err)

msgFromPlaceholder.Value = abi.TokenAmount(types.MustParseFIL("1000"))
txArgs, err := ethtypes.EthTxArgsFromUnsignedMessage(msgFromPlaceholder)
txArgs, err := ethtypes.EthTxArgsFromUnsignedEthMessage(msgFromPlaceholder)
require.NoError(t, err)

digest, err := txArgs.ToRlpUnsignedMsg()
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestEthAccountAbstractionFailure(t *testing.T) {
msgFromPlaceholder, err = client.GasEstimateMessageGas(ctx, msgFromPlaceholder, nil, types.EmptyTSK)
require.NoError(t, err)

txArgs, err = ethtypes.EthTxArgsFromUnsignedMessage(msgFromPlaceholder)
txArgs, err = ethtypes.EthTxArgsFromUnsignedEthMessage(msgFromPlaceholder)
require.NoError(t, err)

digest, err = txArgs.ToRlpUnsignedMsg()
Expand Down
46 changes: 31 additions & 15 deletions node/impl/full/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -1551,23 +1551,38 @@ func EthTxHashFromSignedFilecoinMessage(ctx context.Context, smsg *types.SignedM
}

func newEthTxFromFilecoinMessage(ctx context.Context, smsg *types.SignedMessage, sa StateAPI) (ethtypes.EthTx, error) {
tx, err := ethtypes.EthTxFromSignedMessage(smsg)
if err != nil {
return ethtypes.EthTx{}, xerrors.Errorf("failed to convert from signed message: %w", err)
}
var tx ethtypes.EthTx
var err error

fromEthAddr, err := lookupEthAddress(ctx, smsg.Message.From, sa)
if err != nil {
return ethtypes.EthTx{}, xerrors.Errorf("failed to lookup from ethaddress: %w", err)
}
// This is an eth tx
if smsg.Signature.Type == crypto.SigTypeDelegated {
tx, err = ethtypes.EthTxFromMessageDelegatedSignature(smsg)
if err != nil {
return ethtypes.EthTx{}, xerrors.Errorf("failed to convert from signed message: %w", err)
}

toEthAddr, err := lookupEthAddress(ctx, smsg.Message.To, sa)
if err != nil {
return ethtypes.EthTx{}, xerrors.Errorf("failed to lookup to ethaddress: %w", err)
}
tx.From, err = lookupEthAddress(ctx, smsg.Message.From, sa)
if err != nil {
return ethtypes.EthTx{}, xerrors.Errorf("failed to lookup from ethaddress: %w", err)
}

tx.From = fromEthAddr
tx.To = &toEthAddr
tx.Hash, err = tx.TxHash()
if err != nil {
return tx, err
}
} else if smsg.Signature.Type == crypto.SigTypeUnknown { // Executed BLS Filecoin message
tx = ethtypes.EthTxFromFilecoinMessage(smsg.VMMessage())
tx.Hash, err = ethtypes.EthHashFromCid(smsg.Message.Cid())
if err != nil {
return tx, err
}
} else { // Secp Filecoin Message or BLS Filecoin message in the mpool
tx = ethtypes.EthTxFromFilecoinMessage(smsg.VMMessage())
tx.Hash, err = ethtypes.EthHashFromCid(smsg.Cid())
if err != nil {
return tx, err
}
}

return tx, nil
}
Expand Down Expand Up @@ -1680,7 +1695,8 @@ func newEthTxReceipt(ctx context.Context, tx ethtypes.EthTx, lookup *api.MsgLook
LogsBloom: ethtypes.EmptyEthBloom[:],
}

if receipt.To == nil && lookup.Receipt.ExitCode.IsSuccess() {
// V.Int will be nil for non-eth transactions, so we want to skip unmarshalling the create return.
if receipt.To == nil && lookup.Receipt.ExitCode.IsSuccess() && tx.V.Int != nil {
// Create and Create2 return the same things.
var ret eam.CreateReturn
if err := ret.UnmarshalCBOR(bytes.NewReader(lookup.Receipt.Return)); err != nil {
Expand Down

0 comments on commit f1cbc87

Please sign in to comment.