Merge pull request #10380 from filecoin-project/asr/fix-eth-api

fix: EthAPI: Make newEthBlockFromFilecoinTipSet faster and correct
This commit is contained in:
Jiaying Wang 2023-03-03 21:10:20 -05:00 committed by GitHub
commit d3dfb5ca62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1776,6 +1776,8 @@ func newEthBlockFromFilecoinTipSet(ctx context.Context, ts *types.TipSet, fullTx
return ethtypes.EthBlock{}, err return ethtypes.EthBlock{}, err
} }
bn := ethtypes.EthUint64(ts.Height())
blkCid, err := ts.Key().Cid() blkCid, err := ts.Key().Cid()
if err != nil { if err != nil {
return ethtypes.EthBlock{}, err return ethtypes.EthBlock{}, err
@ -1792,20 +1794,35 @@ func newEthBlockFromFilecoinTipSet(ctx context.Context, ts *types.TipSet, fullTx
block := ethtypes.NewEthBlock(len(msgs) > 0) 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) gasUsed := int64(0)
for txIdx, msg := range msgs { compOutput, err := sa.StateCompute(ctx, ts.Height(), nil, ts.Key())
msgLookup, err := sa.StateSearchMsg(ctx, types.EmptyTSK, msg.Cid(), api.LookbackNoLimit, false)
if err != nil || msgLookup == nil {
return ethtypes.EthBlock{}, nil
}
gasUsed += msgLookup.Receipt.GasUsed
tx, err := newEthTxFromMessageLookup(ctx, msgLookup, txIdx, cs, sa)
if err != nil { if err != nil {
return ethtypes.EthBlock{}, 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 += msg.MsgRct.GasUsed
smsgCid, err := getSignedMessage(ctx, cs, msg.MsgCid)
if err != 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 { if fullTxInfo {
block.Transactions = append(block.Transactions, tx) block.Transactions = append(block.Transactions, tx)
} else { } else {
@ -1814,7 +1831,7 @@ func newEthBlockFromFilecoinTipSet(ctx context.Context, ts *types.TipSet, fullTx
} }
block.Hash = blkHash block.Hash = blkHash
block.Number = ethtypes.EthUint64(ts.Height()) block.Number = bn
block.ParentHash = parentBlkHash block.ParentHash = parentBlkHash
block.Timestamp = ethtypes.EthUint64(ts.Blocks()[0].Timestamp) block.Timestamp = ethtypes.EthUint64(ts.Blocks()[0].Timestamp)
block.BaseFeePerGas = ethtypes.EthBigInt{Int: ts.Blocks()[0].ParentBaseFee.Int} block.BaseFeePerGas = ethtypes.EthBigInt{Int: ts.Blocks()[0].ParentBaseFee.Int}
@ -1995,20 +2012,9 @@ func newEthTxFromMessageLookup(ctx context.Context, msgLookup *api.MsgLookup, tx
return ethtypes.EthTx{}, err return ethtypes.EthTx{}, err
} }
smsg, err := cs.GetSignedMessage(ctx, msgLookup.Message) smsg, err := getSignedMessage(ctx, cs, msgLookup.Message)
if err != nil { if err != nil {
// We couldn't find the signed message, it might be a BLS message, so search for a regular message. return ethtypes.EthTx{}, xerrors.Errorf("failed to get signed msg: %w", err)
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,
},
}
} }
tx, err := newEthTxFromSignedMessage(ctx, smsg, sa) tx, err := newEthTxFromSignedMessage(ctx, smsg, sa)
@ -2364,6 +2370,25 @@ func calculateRewardsAndGasUsed(rewardPercentiles []float64, txGasRewards gasRew
return rewards, totalGasUsed 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", msgCid, err)
}
smsg = &types.SignedMessage{
Message: *msg,
Signature: crypto.Signature{
Type: crypto.SigTypeBLS,
},
}
}
return smsg, nil
}
type gasRewardTuple struct { type gasRewardTuple struct {
gas uint64 gas uint64
reward ethtypes.EthBigInt reward ethtypes.EthBigInt