|
| 1 | +package translator |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "math/big" |
| 8 | + |
| 9 | + "github.com/SyndicateProtocol/metabased-rollup/op-translator/internal/config" |
| 10 | + "github.com/SyndicateProtocol/metabased-rollup/op-translator/internal/constants" |
| 11 | + "github.com/SyndicateProtocol/metabased-rollup/op-translator/internal/utils" |
| 12 | + "github.com/SyndicateProtocol/metabased-rollup/op-translator/pkg/rpc-clients" |
| 13 | + "github.com/SyndicateProtocol/metabased-rollup/op-translator/pkg/types" |
| 14 | + |
| 15 | + "github.com/ethereum/go-ethereum/common" |
| 16 | + "github.com/ethereum/go-ethereum/common/hexutil" |
| 17 | + ethtypes "github.com/ethereum/go-ethereum/core/types" |
| 18 | + "github.com/hashicorp/go-multierror" |
| 19 | + "github.com/rs/zerolog/log" |
| 20 | +) |
| 21 | + |
| 22 | +type MetaBasedBatchProviderArb struct { |
| 23 | + MetaBasedChain IRPCClient |
| 24 | + SequencingChain IRPCClient |
| 25 | + TransactionParser *L3TransactionParser |
| 26 | + SequencingBlockFetcher *SequencingBlockFetcher |
| 27 | + |
| 28 | + SettlementStartBlock int |
| 29 | +} |
| 30 | + |
| 31 | +func InitMetaBasedBatchProviderArb(cfg *config.Config) *MetaBasedBatchProviderArb { |
| 32 | + sequencingChain, err := rpc.Connect(cfg.SequencingChainRPCURL) |
| 33 | + if err != nil { |
| 34 | + log.Panic().Err(err).Msg("Failed to initialize sequencing chain") |
| 35 | + } |
| 36 | + |
| 37 | + metaBasedChain, err := rpc.Connect(cfg.MetaBasedChainRPCURL) |
| 38 | + if err != nil { |
| 39 | + log.Panic().Err(err).Msg("Failed to initialize metabased chain") |
| 40 | + } |
| 41 | + |
| 42 | + return &MetaBasedBatchProviderArb{ |
| 43 | + MetaBasedChain: metaBasedChain, |
| 44 | + SequencingChain: sequencingChain, |
| 45 | + TransactionParser: InitL3TransactionParser(cfg), |
| 46 | + SequencingBlockFetcher: InitSequencingBlockFetcher(sequencingChain, cfg), |
| 47 | + |
| 48 | + SettlementStartBlock: cfg.SettlementStartBlock, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func NewMetaBasedBatchProviderArb( |
| 53 | + settlementChainClient IRPCClient, |
| 54 | + sequencingChainClient IRPCClient, |
| 55 | + sequencingContractAddress common.Address, |
| 56 | + settlementStartBlock int, |
| 57 | + settlementChainBlockTime int, |
| 58 | +) *MetaBasedBatchProviderArb { |
| 59 | + return &MetaBasedBatchProviderArb{ |
| 60 | + MetaBasedChain: settlementChainClient, |
| 61 | + SequencingChain: sequencingChainClient, |
| 62 | + TransactionParser: NewL3TransactionParser(sequencingContractAddress), |
| 63 | + SequencingBlockFetcher: NewSequencingBlockFetcher(sequencingChainClient, settlementChainBlockTime), |
| 64 | + |
| 65 | + SettlementStartBlock: settlementStartBlock, |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func (m *MetaBasedBatchProviderArb) Close() { |
| 70 | + log.Debug().Msg("Closing Arbitrum MetaBasedBatchProvider") |
| 71 | + m.SequencingChain.CloseConnection() |
| 72 | + m.MetaBasedChain.CloseConnection() |
| 73 | +} |
| 74 | + |
| 75 | +// NOTE [SEQ-144]: THIS ASSUMES THAT THE L3 HAS THE SAME BLOCK TIME AS THE SETTLEMENT L2 |
| 76 | +func (m *MetaBasedBatchProviderArb) getParentBlockHash(ctx context.Context, blockNumStr string) (string, error) { |
| 77 | + log.Debug().Msgf("Getting parent block hash for block number %s", blockNumStr) |
| 78 | + blockNum, err := utils.HexToInt(blockNumStr) |
| 79 | + if err != nil { |
| 80 | + return "", err |
| 81 | + } |
| 82 | + |
| 83 | + if blockNum < m.SettlementStartBlock { |
| 84 | + return "", errors.New("block number before start block") |
| 85 | + } |
| 86 | + |
| 87 | + if blockNum == m.SettlementStartBlock { |
| 88 | + return constants.ZeroHash, nil |
| 89 | + } |
| 90 | + log.Debug().Msgf("Settlement start block: %d", m.SettlementStartBlock) |
| 91 | + |
| 92 | + parentBlockNum := int64(blockNum - m.SettlementStartBlock - 1) |
| 93 | + |
| 94 | + log.Debug().Msgf("Getting block hash for block number %d", parentBlockNum) |
| 95 | + previousBlock, err := m.MetaBasedChain.AsEthClient().HeaderByNumber(ctx, big.NewInt(parentBlockNum)) |
| 96 | + if err != nil { |
| 97 | + return "", err |
| 98 | + } |
| 99 | + log.Debug().Msgf("Previous block: %v", previousBlock) |
| 100 | + |
| 101 | + return previousBlock.Hash().Hex(), nil |
| 102 | +} |
| 103 | + |
| 104 | +func (m *MetaBasedBatchProviderArb) FilterReceipts(receipts []*ethtypes.Receipt) (txns []hexutil.Bytes, result error) { |
| 105 | + for i, rec := range receipts { |
| 106 | + if rec.Status != ethtypes.ReceiptStatusSuccessful { |
| 107 | + continue |
| 108 | + } |
| 109 | + |
| 110 | + for j, txLog := range rec.Logs { |
| 111 | + if m.TransactionParser.IsLogTransactionProcessed(txLog) { |
| 112 | + proc, err := m.TransactionParser.ParseTransactionProcessed(txLog) |
| 113 | + if err != nil { |
| 114 | + result = multierror.Append(result, fmt.Errorf("malformatted l2 receipt log in receipt %d, log %d: %w", i, j, err)) |
| 115 | + } else { |
| 116 | + txns = append(txns, proc.EncodedTxn) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + return txns, result |
| 122 | +} |
| 123 | + |
| 124 | +func (m *MetaBasedBatchProviderArb) GetBatch(ctx context.Context, block types.Block) (*types.Batch, error) { |
| 125 | + blockNumber, err := block.GetBlockNumber() |
| 126 | + if err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + blockHash, err := block.GetBlockHash() |
| 130 | + if err != nil { |
| 131 | + return nil, err |
| 132 | + } |
| 133 | + |
| 134 | + seqBlockNumbers, err := m.SequencingBlockFetcher.GetSequencingBlocks(block) |
| 135 | + if err != nil { |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + log.Debug().Msgf("Translating block number %s and hash %s: linked block numbers: %s", blockNumber, blockHash, seqBlockNumbers) |
| 139 | + |
| 140 | + receipts, err := m.SequencingChain.BlocksReceiptsByNumbers(ctx, seqBlockNumbers) |
| 141 | + if err != nil { |
| 142 | + return nil, err |
| 143 | + } |
| 144 | + log.Debug().Msgf("Translating block number %s and hash %s: receipts: %v", blockNumber, blockHash, receipts) |
| 145 | + |
| 146 | + txns, err := m.FilterReceipts(receipts) |
| 147 | + if err != nil { |
| 148 | + return nil, err |
| 149 | + } |
| 150 | + log.Debug().Msgf("Translating block number %s and hash %s: filtered transactions: %v", blockNumber, blockHash, txns) |
| 151 | + |
| 152 | + parentHash, err := m.getParentBlockHash(ctx, blockNumber) |
| 153 | + if err != nil { |
| 154 | + return nil, err |
| 155 | + } |
| 156 | + |
| 157 | + timestamp, err := block.GetBlockTimestamp() |
| 158 | + if err != nil { |
| 159 | + return nil, err |
| 160 | + } |
| 161 | + |
| 162 | + batch, err := types.NewBatch(parentHash, blockNumber, blockHash, timestamp, txns) |
| 163 | + if err != nil { |
| 164 | + return nil, err |
| 165 | + } |
| 166 | + log.Debug().Msgf("Translating block number %s and hash %s: batch: %v", blockNumber, blockHash, batch) |
| 167 | + |
| 168 | + return batch, nil |
| 169 | +} |
0 commit comments