Skip to content

Commit

Permalink
before remove of receiptBlobs thing
Browse files Browse the repository at this point in the history
  • Loading branch information
badgersrus committed Dec 17, 2024
1 parent 0741e60 commit 4640ce4
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 36 deletions.
23 changes: 19 additions & 4 deletions go/common/l1_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,29 @@ func (tx *L1TxData) HasSequencerEnclaveID() bool {
}

func (p *ProcessedL1Data) AddEvent(txType L1TxType, tx *L1TxData) {
eventType := uint8(txType)

// Find existing event group
for i := range p.Events {
if p.Events[i].Type == uint8(txType) {
p.Events[i].Txs = append(p.Events[i].Txs, tx)
return
if p.Events[i].Type != eventType {
continue
}

txHash := tx.Transaction.Hash()

// check for duplicate transaction
for _, existingTx := range p.Events[i].Txs {
if existingTx.Transaction.Hash() == txHash {
return // Skip duplicate transaction
}
}

p.Events[i].Txs = append(p.Events[i].Txs, tx)
return
}

p.Events = append(p.Events, L1Event{
Type: uint8(txType), // Convert to uint8 when storing
Type: eventType,
Txs: []*L1TxData{tx},
})
}
Expand Down
33 changes: 22 additions & 11 deletions go/enclave/components/rollup_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,43 +126,54 @@ func (rc *rollupConsumerImpl) extractAndVerifyRollups(processed *common.Processe
rollupTxs := processed.GetEvents(common.RollupTx)
rollups := make([]*common.ExtRollup, 0, len(rollupTxs))

blobs, blobHashes, err := rc.extractBlobsAndHashes(rollupTxs)
if err != nil {
return nil, err
}

//println("---- START OF ROLLUP CHECK -------")
//println("length of rollupTxs: ", len(rollupTxs))
for i, tx := range rollupTxs {
t := rc.MgmtContractLib.DecodeTx(tx.Transaction)
if t == nil {
continue
}

//println("TX HASH: ", tx.Transaction.Hash().Hex())
rollupHashes, ok := t.(*ethadapter.L1RollupHashes)
if !ok {
continue
}

// Only use blobs from this transaction
blobs := tx.Blobs
_, blobHashes, err := ethadapter.MakeSidecar(blobs)
if err != nil {
return nil, fmt.Errorf("could not create blob sidecar and blob hashes. Cause: %w", err)
}

if err := verifyBlobHashes(rollupHashes, blobHashes); err != nil {
rc.logger.Warn(fmt.Sprintf("blob hashes in rollup at index %d do not match the rollup blob hashes. Cause: %s", i, err))
continue // Blob hashes don't match, skip this rollup
rc.logger.Warn(fmt.Sprintf("blob hashes in rollup at index %d do not match the rollup blob hashes. Cause: %s", i, err), log.NodeIDKey)
continue
}

//println("ROLLUP CONSUMER blobs: ", len(blobs))
//for i, h := range blobHashes {
// println("ROLLUP CONSUMER blob hash: ", h.Hex(), " at index: ", i)
//}

r, err := ethadapter.ReconstructRollup(blobs)
if err != nil {
// This is a critical error because we've already verified the blob hashes
// If we can't reconstruct the rollup at this point, something is seriously wrong
return nil, fmt.Errorf("could not recreate rollup from blobs. Cause: %w", err)
}

//println("Adding rollup to list: ", r.Hash().Hex())
rollups = append(rollups, r)

rc.logger.Info("Extracted rollup from block", log.RollupHashKey, r.Hash(), log.BlockHashKey, processed.BlockHeader.Hash())
}
if len(rollups) > 1 {
println("HERE")
if rollups[0].Hash() == rollups[1].Hash() {
println("ROLLUPS THE SAME")
println("ROLLUPS THE SAME: ", rollups[0].Hash().Hex())
return []*common.ExtRollup{rollups[0]}, nil
//rc.logger.Crit("DUPLICATE ROLLUP")
}

}
return rollups, nil
}
Expand Down
16 changes: 11 additions & 5 deletions go/enclave/enclave_admin_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,8 @@ func NewEnclaveAdminAPI(config *enclaveconfig.EnclaveConfig, storage storage.Sto
return eas
}

func (e *enclaveAdminService) AddSequencer(id common.EnclaveID, proof types.Receipt) common.SystemError {
e.mainMutex.Lock()
defer e.mainMutex.Unlock()

func (e *enclaveAdminService) AddSequencer(id common.EnclaveID, _ types.Receipt) common.SystemError {
// todo - use the proof

err := e.storage.StoreNodeType(context.Background(), id, common.BackupSequencer)
if err != nil {
return responses.ToInternalError(err)
Expand Down Expand Up @@ -494,6 +490,16 @@ func (e *enclaveAdminService) ingestL1Block(ctx context.Context, br *common.Bloc
// Unsure what to do here; block has been stored
}

sequencerAddedTxs := processed.GetEvents(common.SequencerAddedTx)
for _, tx := range sequencerAddedTxs {
if tx.HasSequencerEnclaveID() {
err = e.AddSequencer(tx.SequencerEnclaveID, *tx.Receipt)
if err != nil {
e.logger.Crit("Encountered error while adding sequencer enclaveID", log.ErrKey, err)
}

}
}
if ingestion.IsFork() {
e.registry.OnL1Reorg(ingestion)
err := e.service.OnL1Fork(ctx, ingestion.ChainFork)
Expand Down
1 change: 1 addition & 0 deletions go/ethadapter/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,6 @@ func ReconstructRollup(blobs []*kzg4844.Blob) (*common.ExtRollup, error) {
if err := rlp.DecodeBytes(data, &rollup); err != nil {
return nil, fmt.Errorf("could not decode rollup bytes. Cause: %w", err)
}

return &rollup, nil
}
3 changes: 3 additions & 0 deletions go/ethadapter/mgmtcontractlib/mgmt_contract_lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ func (c *contractLibImpl) CreateBlobRollup(t *ethadapter.L1RollupTx) (types.TxDa
return nil, fmt.Errorf("failed to make sidecar: %w", err)
}

for i, b := range blobHashes {
println("MGMT CONTRACT creating blob with hashes ", b.Hex(), " at index: ", i)
}
return &types.BlobTx{
To: *c.addr,
Data: data,
Expand Down
3 changes: 2 additions & 1 deletion go/host/enclave/guardian.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ func (g *Guardian) HandleBatch(batch *common.ExtBatch) {
// todo - @matt - does it make sense to use a timeout context?
err := g.submitL2Batch(context.Background(), batch)
if err != nil {
g.logger.Error("Error submitting batch to enclave", log.ErrKey, err)
// FIXME change back to Error
g.logger.Warn("Error submitting batch to enclave", log.ErrKey, err)
}
}

Expand Down
22 changes: 7 additions & 15 deletions go/host/l1/blockrepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,38 +209,35 @@ func (r *Repository) ExtractTenTransactions(block *common.L1Block) (*common.Proc
Events: []common.L1Event{},
}

// Fetch all relevant logs for the block
logs, err := r.fetchRelevantLogs(block)
if err != nil {
return nil, err
}

// Process each log sequentially, preserving their natural order
// we have to loop through to preserve receipt order
for _, l := range logs {
txData, err := r.createTransactionData(l.TxHash)
if err != nil {
r.logger.Error("Error creating transaction data", "txHash", l.TxHash, "error", err)
continue
}

// Categorize logs inline based on their topics
switch l.Topics[0] {
case crosschain.CrossChainEventID:

r.processCrossChainLogs(l, txData, processed)
case crosschain.ValueTransferEventID:
r.processValueTransferLogs(l, txData, processed)
case crosschain.SequencerEnclaveGrantedEventID:
r.processSequencerLogs(l, txData, processed)
r.processDecodedTransaction(txData, processed) // we need to decode the InitialiseSecretTx
r.processManagementContractTx(txData, processed) // we need to decode the InitialiseSecretTx
case crosschain.ImportantContractAddressUpdatedID:
r.processDecodedTransaction(txData, processed)
r.processManagementContractTx(txData, processed)
case crosschain.RollupAddedID:
r.processManagementContractTx(txData, processed)
case crosschain.NetworkSecretRequestedID:
processed.AddEvent(common.SecretRequestTx, txData)
case crosschain.NetworkSecretRespondedID:
processed.AddEvent(common.SecretResponseTx, txData)
case crosschain.RollupAddedID:
r.processDecodedTransaction(txData, processed)
default:
r.logger.Warn("Unknown log topic", "topic", l.Topics[0], "txHash", l.TxHash)
}
Expand All @@ -249,7 +246,7 @@ func (r *Repository) ExtractTenTransactions(block *common.L1Block) (*common.Proc
return processed, nil
}

// fetchRelevantLogs retrieves all logs from relevant contract addresses
// fetchRelevantLogs retrieves all logs from management contract and message bus addresses
func (r *Repository) fetchRelevantLogs(block *common.L1Block) ([]types.Log, error) {
blkHash := block.Hash()
var allAddresses []gethcommon.Address
Expand Down Expand Up @@ -295,7 +292,6 @@ func (r *Repository) processCrossChainLogs(l types.Log, txData *common.L1TxData,
func (r *Repository) processValueTransferLogs(l types.Log, txData *common.L1TxData, processed *common.ProcessedL1Data) {
if transfers, err := crosschain.ConvertLogsToValueTransfers([]types.Log{l}, crosschain.ValueTransferEventName, crosschain.MessageBusABI); err == nil {
txData.ValueTransfers = transfers
println("VALUE TRANSFER ADDED")
processed.AddEvent(common.CrossChainValueTranserTx, txData)
}
}
Expand All @@ -304,26 +300,22 @@ func (r *Repository) processValueTransferLogs(l types.Log, txData *common.L1TxDa
func (r *Repository) processSequencerLogs(l types.Log, txData *common.L1TxData, processed *common.ProcessedL1Data) {
if enclaveID, err := getEnclaveIdFromLog(l); err == nil {
txData.SequencerEnclaveID = enclaveID
println("SEQUENCER ADDED TX")
processed.AddEvent(common.SequencerAddedTx, txData)
}
}

// processDecodedTransaction handles decoded transaction types
func (r *Repository) processDecodedTransaction(txData *common.L1TxData, processed *common.ProcessedL1Data) {
func (r *Repository) processManagementContractTx(txData *common.L1TxData, processed *common.ProcessedL1Data) {
b := processed.BlockHeader
if decodedTx := r.mgmtContractLib.DecodeTx(txData.Transaction); decodedTx != nil {
switch t := decodedTx.(type) {
case *ethadapter.L1InitializeSecretTx:
println("INITIALIZE TX ADDED")
processed.AddEvent(common.InitialiseSecretTx, txData)
case *ethadapter.L1SetImportantContractsTx:
println("SET IMPORTANT ADDED")
processed.AddEvent(common.SetImportantContractsTx, txData)
case *ethadapter.L1RollupHashes:
if blobs, err := r.blobResolver.FetchBlobs(context.Background(), b, t.BlobHashes); err == nil {
txData.Blobs = blobs
println("ROLLUP ADDED")
processed.AddEvent(common.RollupTx, txData)
}
}
Expand Down

0 comments on commit 4640ce4

Please sign in to comment.