|
| 1 | +package batcher |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math/big" |
| 6 | + |
| 7 | + "github.com/ethereum/go-ethereum/common/hexutil" |
| 8 | + "github.com/ethereum/go-ethereum/crypto" |
| 9 | + |
| 10 | + "github.com/ethereum-optimism/optimism/espresso/bindings" |
| 11 | + "github.com/ethereum-optimism/optimism/op-node/rollup/derive" |
| 12 | + "github.com/ethereum-optimism/optimism/op-service/eth" |
| 13 | + "github.com/ethereum-optimism/optimism/op-service/txmgr" |
| 14 | +) |
| 15 | + |
| 16 | +// computeCommitment computes the batch commitment hash from a transaction candidate. |
| 17 | +// For calldata transactions, it returns keccak256(calldata). |
| 18 | +// For blob transactions, it returns keccak256(concat(blobVersionedHashes)). |
| 19 | +func computeCommitment(candidate *txmgr.TxCandidate) ([32]byte, error) { |
| 20 | + if len(candidate.Blobs) == 0 { |
| 21 | + return crypto.Keccak256Hash(candidate.TxData), nil |
| 22 | + } |
| 23 | + |
| 24 | + concatenatedBlobHashes := make([]byte, 0) |
| 25 | + for _, blob := range candidate.Blobs { |
| 26 | + blobCommitment, err := blob.ComputeKZGCommitment() |
| 27 | + if err != nil { |
| 28 | + return [32]byte{}, fmt.Errorf("failed to compute KZG commitment for blob: %w", err) |
| 29 | + } |
| 30 | + blobHash := eth.KZGToVersionedHash(blobCommitment) |
| 31 | + concatenatedBlobHashes = append(concatenatedBlobHashes, blobHash.Bytes()...) |
| 32 | + } |
| 33 | + return crypto.Keccak256Hash(concatenatedBlobHashes), nil |
| 34 | +} |
| 35 | + |
| 36 | +// sendTxWithFallbackAuth authenticates a batch transaction via the BatchAuthenticator contract |
| 37 | +// using the fallback batcher's sender identity (msg.sender check on-chain), then sends the |
| 38 | +// batch data to the BatchInbox address. |
| 39 | +// |
| 40 | +// The contract's fallback path checks msg.sender against systemConfig.batcherHash(), so no |
| 41 | +// separate signature is needed — the L1 transaction is already signed by the TxManager's key. |
| 42 | +func (l *BatchSubmitter) sendTxWithFallbackAuth(txdata txData, isCancel bool, candidate *txmgr.TxCandidate, queue TxSender[txRef], receiptsCh chan txmgr.TxReceipt[txRef]) { |
| 43 | + transactionReference := txRef{id: txdata.ID(), isCancel: isCancel, isBlob: txdata.daType == DaTypeBlob, daType: txdata.daType, size: txdata.Len()} |
| 44 | + l.Log.Debug("Sending fallback-authenticated L1 transaction", "txRef", transactionReference) |
| 45 | + |
| 46 | + commitment, err := computeCommitment(candidate) |
| 47 | + if err != nil { |
| 48 | + receiptsCh <- txmgr.TxReceipt[txRef]{ |
| 49 | + ID: transactionReference, |
| 50 | + Err: fmt.Errorf("failed to compute commitment: %w", err), |
| 51 | + } |
| 52 | + return |
| 53 | + } |
| 54 | + l.Log.Debug("Computed fallback batch commitment", "txRef", transactionReference, "commitment", hexutil.Encode(commitment[:])) |
| 55 | + |
| 56 | + batchAuthenticatorAbi, err := bindings.BatchAuthenticatorMetaData.GetAbi() |
| 57 | + if err != nil { |
| 58 | + receiptsCh <- txmgr.TxReceipt[txRef]{ |
| 59 | + ID: transactionReference, |
| 60 | + Err: fmt.Errorf("failed to get batch authenticator ABI: %w", err), |
| 61 | + } |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + // Pass an empty signature — the contract checks msg.sender for the fallback path. |
| 66 | + authenticateBatchCalldata, err := batchAuthenticatorAbi.Pack("authenticateBatchInfo", commitment, []byte{}) |
| 67 | + if err != nil { |
| 68 | + receiptsCh <- txmgr.TxReceipt[txRef]{ |
| 69 | + ID: transactionReference, |
| 70 | + Err: fmt.Errorf("failed to pack authenticateBatchInfo calldata: %w", err), |
| 71 | + } |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + verifyCandidate := txmgr.TxCandidate{ |
| 76 | + TxData: authenticateBatchCalldata, |
| 77 | + To: &l.RollupConfig.BatchAuthenticatorAddress, |
| 78 | + } |
| 79 | + |
| 80 | + l.Log.Debug( |
| 81 | + "Sending fallback authenticateBatchInfo transaction", |
| 82 | + "txRef", transactionReference, |
| 83 | + "commitment", hexutil.Encode(commitment[:]), |
| 84 | + "address", l.RollupConfig.BatchAuthenticatorAddress.String(), |
| 85 | + ) |
| 86 | + verificationReceipt, err := l.Txmgr.Send(l.killCtx, verifyCandidate) |
| 87 | + if err != nil { |
| 88 | + l.Log.Error("Failed to send fallback authenticateBatchInfo transaction", "txRef", transactionReference, "err", err) |
| 89 | + receiptsCh <- txmgr.TxReceipt[txRef]{ |
| 90 | + ID: transactionReference, |
| 91 | + Err: fmt.Errorf("failed to send fallback authenticateBatchInfo transaction: %w", err), |
| 92 | + } |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + receipt, err := l.Txmgr.Send(l.killCtx, *candidate) |
| 97 | + if err != nil { |
| 98 | + l.Log.Error("Failed to send batch inbox transaction", "txRef", transactionReference, "err", err) |
| 99 | + receiptsCh <- txmgr.TxReceipt[txRef]{ |
| 100 | + ID: transactionReference, |
| 101 | + Err: fmt.Errorf("failed to send batch inbox transaction: %w", err), |
| 102 | + } |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + distance := new(big.Int).Sub(receipt.BlockNumber, verificationReceipt.BlockNumber) |
| 107 | + lookbackWindow := new(big.Int).SetUint64(derive.BatchAuthLookbackWindow) |
| 108 | + if distance.Sign() < 0 || distance.Cmp(lookbackWindow) >= 0 { |
| 109 | + l.Log.Error("authenticateBatchInfo transaction too far from batch inbox transaction", "txRef", transactionReference, "distance", distance) |
| 110 | + receiptsCh <- txmgr.TxReceipt[txRef]{ |
| 111 | + ID: transactionReference, |
| 112 | + Err: fmt.Errorf("authenticateBatchInfo transaction too far from batch inbox transaction: %s", distance), |
| 113 | + } |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + receiptsCh <- txmgr.TxReceipt[txRef]{ |
| 118 | + ID: transactionReference, |
| 119 | + Receipt: receipt, |
| 120 | + Err: nil, |
| 121 | + } |
| 122 | +} |
0 commit comments