Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: EthAPI: Make newEthBlockFromFilecoinTipSet faster and correct #5798

Merged
merged 1 commit into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 48 additions & 27 deletions app/submodule/eth/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1058,11 +1058,7 @@ func (a *ethAPI) Web3ClientVersion(ctx context.Context) (string, error) {
}

func newEthBlockFromFilecoinTipSet(ctx context.Context, ts *types.TipSet, fullTxInfo bool, ms *chain.MessageStore, ca v1.IChain) (types.EthBlock, error) {
parent, err := ca.ChainGetTipSet(ctx, ts.Parents())
if err != nil {
return types.EthBlock{}, err
}
parentKeyCid, err := parent.Key().Cid()
parentKeyCid, err := ts.Parents().Cid()
if err != nil {
return types.EthBlock{}, err
}
Expand All @@ -1071,6 +1067,8 @@ func newEthBlockFromFilecoinTipSet(ctx context.Context, ts *types.TipSet, fullTx
return types.EthBlock{}, err
}

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

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

block := types.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 := ca.StateSearchMsg(ctx, types.EmptyTSK, msg.Cid(), constants.LookbackNoLimit, false)
if err != nil || msgLookup == nil {
return types.EthBlock{}, nil
compOutput, err := ca.StateCompute(ctx, ts.Height(), nil, ts.Key())
if err != nil {
return types.EthBlock{}, fmt.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, ms, ca)
gasUsed += msg.MsgRct.GasUsed
smsgCid, err := getSignedMessage(ctx, ms, msg.MsgCid)
if err != nil {
return types.EthBlock{}, nil
return types.EthBlock{}, fmt.Errorf("failed to get signed msg %s: %w", msg.MsgCid, err)
}
tx, err := newEthTxFromSignedMessage(ctx, smsgCid, ca)
if err != nil {
return types.EthBlock{}, fmt.Errorf("failed to convert msg to ethTx: %w", err)
}

ti := types.EthUint64(txIdx)

tx.ChainID = types.EthUint64(types2.Eip155ChainID)
tx.BlockHash = &blkHash
tx.BlockNumber = &bn
tx.TransactionIndex = &ti

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

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

smsg, err := ms.LoadSignedMessage(ctx, msgLookup.Message)
smsg, err := getSignedMessage(ctx, ms, 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 := ms.LoadUnsignedMessage(ctx, msgLookup.Message)
if err != nil {
return types.EthTx{}, err
}
smsg = &types.SignedMessage{
Message: *msg,
Signature: crypto.Signature{
Type: crypto.SigTypeUnknown,
Data: nil,
},
}
return types.EthTx{}, fmt.Errorf("failed to get signed msg: %w", err)
}

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

func getSignedMessage(ctx context.Context, ms *chain.MessageStore, msgCid cid.Cid) (*types.SignedMessage, error) {
smsg, err := ms.LoadSignedMessage(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 := ms.LoadUnsignedMessage(ctx, msgCid)
if err != nil {
return nil, fmt.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 {
gas uint64
reward types.EthBigInt
Expand Down
1 change: 0 additions & 1 deletion pkg/statemanger/state_manger.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,6 @@ func (s *Stmgr) ExecutionTrace(ctx context.Context, ts *types.TipSet) (cid.Cid,
}

func MakeMsgGasCost(msg *types.Message, ret *vm.Ret) types.MsgGasCost {
fmt.Println(msg.RequiredFunds(), ret.OutPuts.Refund)
return types.MsgGasCost{
Message: msg.Cid(),
GasUsed: big.NewInt(ret.Receipt.GasUsed),
Expand Down