Skip to content

Commit

Permalink
fix: EthAPI: Make newEthBlockFromFilecoinTipSet faster and correct
Browse files Browse the repository at this point in the history
  • Loading branch information
arajasek committed Mar 2, 2023
1 parent dbbcf4b commit 842a16a
Showing 1 changed file with 47 additions and 22 deletions.
69 changes: 47 additions & 22 deletions node/impl/full/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,8 @@ func newEthBlockFromFilecoinTipSet(ctx context.Context, ts *types.TipSet, fullTx
return ethtypes.EthBlock{}, err
}

bn := ethtypes.EthUint64(ts.Height())

blkCid, err := ts.Key().Cid()
if err != nil {
return ethtypes.EthBlock{}, err
Expand All @@ -1792,19 +1794,34 @@ func newEthBlockFromFilecoinTipSet(ctx context.Context, ts *types.TipSet, fullTx

block := ethtypes.NewEthBlock(len(msgs) > 0)

// this seems to be a very expensive way to get gasUsed of the block. may need to find an efficient way to do it
gasUsed := int64(0)
for txIdx, msg := range msgs {
msgLookup, err := sa.StateSearchMsg(ctx, types.EmptyTSK, msg.Cid(), api.LookbackNoLimit, false)
if err != nil || msgLookup == nil {
return ethtypes.EthBlock{}, nil
compOutput, err := sa.StateCompute(ctx, ts.Height(), nil, ts.Key())
if err != nil {
return ethtypes.EthBlock{}, xerrors.Errorf("failed to compute state: %w", err)
}

for txIdx, msg := range compOutput.Trace {
// skip system messages like reward application and cron
if msg.Msg.From == builtintypes.SystemActorAddr {
continue
}
gasUsed += msgLookup.Receipt.GasUsed

tx, err := newEthTxFromMessageLookup(ctx, msgLookup, txIdx, cs, sa)
gasUsed += msg.MsgRct.GasUsed
smsgCid, err := getSignedMessage(ctx, cs, msg.MsgCid)
if err != nil {
return ethtypes.EthBlock{}, nil
return ethtypes.EthBlock{}, xerrors.Errorf("failed to get signed msg %s: %w", msg.MsgCid, err)
}
tx, err := newEthTxFromSignedMessage(ctx, smsgCid, sa)
if err != nil {
return ethtypes.EthBlock{}, xerrors.Errorf("failed to convert msg to ethTx: %w", err)
}

ti := ethtypes.EthUint64(txIdx)

tx.ChainID = ethtypes.EthUint64(build.Eip155ChainId)
tx.BlockHash = &blkHash
tx.BlockNumber = &bn
tx.TransactionIndex = &ti

if fullTxInfo {
block.Transactions = append(block.Transactions, tx)
Expand All @@ -1814,7 +1831,7 @@ func newEthBlockFromFilecoinTipSet(ctx context.Context, ts *types.TipSet, fullTx
}

block.Hash = blkHash
block.Number = ethtypes.EthUint64(ts.Height())
block.Number = bn
block.ParentHash = parentBlkHash
block.Timestamp = ethtypes.EthUint64(ts.Blocks()[0].Timestamp)
block.BaseFeePerGas = ethtypes.EthBigInt{Int: ts.Blocks()[0].ParentBaseFee.Int}
Expand Down Expand Up @@ -1995,20 +2012,9 @@ func newEthTxFromMessageLookup(ctx context.Context, msgLookup *api.MsgLookup, tx
return ethtypes.EthTx{}, err
}

smsg, err := cs.GetSignedMessage(ctx, msgLookup.Message)
smsg, err := getSignedMessage(ctx, cs, msgLookup.Message)
if err != nil {
// We couldn't find the signed message, it might be a BLS message, so search for a regular message.
msg, err := cs.GetMessage(ctx, msgLookup.Message)
if err != nil {
return ethtypes.EthTx{}, err
}
smsg = &types.SignedMessage{
Message: *msg,
Signature: crypto.Signature{
Type: crypto.SigTypeBLS,
Data: nil,
},
}
return ethtypes.EthTx{}, xerrors.Errorf("failed to get signed msg: %w", err)
}

tx, err := newEthTxFromSignedMessage(ctx, smsg, sa)
Expand Down Expand Up @@ -2364,6 +2370,25 @@ func calculateRewardsAndGasUsed(rewardPercentiles []float64, txGasRewards gasRew
return rewards, totalGasUsed
}

func getSignedMessage(ctx context.Context, cs *store.ChainStore, msgCid cid.Cid) (*types.SignedMessage, error) {
smsg, err := cs.GetSignedMessage(ctx, msgCid)
if err != nil {
// We couldn't find the signed message, it might be a BLS message, so search for a regular message.
msg, err := cs.GetMessage(ctx, msgCid)
if err != nil {
return nil, xerrors.Errorf("failed to find msg %s: %w", msg, err)
}
smsg = &types.SignedMessage{
Message: *msg,
Signature: crypto.Signature{
Type: crypto.SigTypeBLS,
},
}
}

return smsg, nil
}

type gasRewardTuple struct {
gas uint64
reward ethtypes.EthBigInt
Expand Down

0 comments on commit 842a16a

Please sign in to comment.