Skip to content

Commit

Permalink
Merge pull request ethereum#143 from OffchainLabs/classic-l1-blocknumber
Browse files Browse the repository at this point in the history
add l1blockNumber for classic blocks
  • Loading branch information
PlasmaPower authored Aug 8, 2022
2 parents 7389746 + 6664419 commit df4c19b
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1333,16 +1333,54 @@ func (s *PublicBlockChainAPI) rpcMarshalHeader(ctx context.Context, header *type
return fields
}

func (s *PublicBlockChainAPI) arbClassicL1BlockNumber(ctx context.Context, block *types.Block) (hexutil.Uint64, error) {
startBlockNum := block.Number().Int64()
blockNum := startBlockNum
i := int64(0)
for {
transactions := block.Transactions()
if len(transactions) > 0 {
legacyTx, ok := transactions[0].GetInner().(*types.ArbitrumLegacyTxData)
if !ok {
return 0, fmt.Errorf("couldn't read legacy transaction from block %d", blockNum)
}
return hexutil.Uint64(legacyTx.L1BlockNumber), nil
}
if blockNum == 0 {
return 0, nil
}
i++
blockNum = startBlockNum - i
if i > 5 {
return 0, fmt.Errorf("couldn't find block with transactions. Reached %d", blockNum)
}
var err error
block, err = s.b.BlockByNumber(ctx, rpc.BlockNumber(blockNum))
if err != nil {
return 0, err
}
}
}

// rpcMarshalBlock uses the generalized output filler, then adds the total difficulty field, which requires
// a `PublicBlockchainAPI`.
func (s *PublicBlockChainAPI) rpcMarshalBlock(ctx context.Context, b *types.Block, inclTx bool, fullTx bool) (map[string]interface{}, error) {
fields, err := RPCMarshalBlock(b, inclTx, fullTx, s.b.ChainConfig())
chainConfig := s.b.ChainConfig()
fields, err := RPCMarshalBlock(b, inclTx, fullTx, chainConfig)
if err != nil {
return nil, err
}
if inclTx {
fields["totalDifficulty"] = (*hexutil.Big)(s.b.GetTd(ctx, b.Hash()))
}
if chainConfig.IsArbitrum() && !chainConfig.IsArbitrumNitro(b.Number()) {
l1BlockNumber, err := s.arbClassicL1BlockNumber(ctx, b)
if err != nil {
log.Error("error trying to fill legacy l1BlockNumber", "err", err)
} else {
fields["l1BlockNumber"] = hexutil.Uint64(l1BlockNumber)
}
}
return fields, err
}

Expand Down

0 comments on commit df4c19b

Please sign in to comment.