|
| 1 | +// (c) Cartesi and individual authors (see AUTHORS) |
| 2 | +// SPDX-License-Identifier: Apache-2.0 (see LICENSE) |
| 3 | + |
| 4 | +package evmreader |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "context" |
| 9 | + "log/slog" |
| 10 | + |
| 11 | + . "github.com/cartesi/rollups-node/internal/node/model" |
| 12 | + "github.com/ethereum/go-ethereum/accounts/abi/bind" |
| 13 | +) |
| 14 | + |
| 15 | +func (r *EvmReader) checkForOutputExecution( |
| 16 | + ctx context.Context, |
| 17 | + apps []application, |
| 18 | + mostRecentBlockNumber uint64, |
| 19 | +) { |
| 20 | + |
| 21 | + appAddresses := appsToAddresses(apps) |
| 22 | + |
| 23 | + slog.Debug("Checking for new Output Executed Events", "apps", appAddresses) |
| 24 | + |
| 25 | + for _, app := range apps { |
| 26 | + |
| 27 | + LastOutputCheck := app.LastOutputCheckBlock |
| 28 | + |
| 29 | + // Safeguard: Only check blocks starting from the block where the InputBox |
| 30 | + // contract was deployed as Inputs can be added to that same block |
| 31 | + if LastOutputCheck < r.inputBoxDeploymentBlock { |
| 32 | + LastOutputCheck = r.inputBoxDeploymentBlock |
| 33 | + } |
| 34 | + |
| 35 | + if mostRecentBlockNumber > LastOutputCheck { |
| 36 | + |
| 37 | + slog.Info("Checking output execution for application", |
| 38 | + "app", app.ContractAddress, |
| 39 | + "last output check block", LastOutputCheck, |
| 40 | + "most recent block", mostRecentBlockNumber) |
| 41 | + |
| 42 | + r.readAndUpdateOutputs(ctx, app, LastOutputCheck, mostRecentBlockNumber) |
| 43 | + |
| 44 | + } else if mostRecentBlockNumber < LastOutputCheck { |
| 45 | + slog.Warn( |
| 46 | + "Not reading output execution: most recent block is lower than the last processed one", //nolint:lll |
| 47 | + "app", app.ContractAddress, |
| 48 | + "last output check block", LastOutputCheck, |
| 49 | + "most recent block", mostRecentBlockNumber, |
| 50 | + ) |
| 51 | + } else { |
| 52 | + slog.Info("Not reading output execution: already checked the most recent blocks", |
| 53 | + "app", app.ContractAddress, |
| 54 | + "last output check block", LastOutputCheck, |
| 55 | + "most recent block", mostRecentBlockNumber, |
| 56 | + ) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | +} |
| 61 | + |
| 62 | +func (r *EvmReader) readAndUpdateOutputs( |
| 63 | + ctx context.Context, app application, lastOutputCheck, mostRecentBlockNumber uint64) { |
| 64 | + |
| 65 | + contract := app.applicationContract |
| 66 | + |
| 67 | + opts := &bind.FilterOpts{ |
| 68 | + Start: lastOutputCheck + 1, |
| 69 | + End: &mostRecentBlockNumber, |
| 70 | + } |
| 71 | + |
| 72 | + outputExecutedEvents, err := contract.RetrieveOutputExecutionEvents(opts) |
| 73 | + if err != nil { |
| 74 | + slog.Error("Error reading output events", "app", app.ContractAddress, "error", err) |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + // Should we check the output hash?? |
| 79 | + var executedOutputs []*Output |
| 80 | + for _, event := range outputExecutedEvents { |
| 81 | + |
| 82 | + // Compare output to check it is the correct one |
| 83 | + output, err := r.repository.GetOutput(ctx, event.OutputIndex, app.ContractAddress) |
| 84 | + if err != nil { |
| 85 | + slog.Error("Error retrieving output", |
| 86 | + "app", app.ContractAddress, "index", event.OutputIndex, "error", err) |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + if !bytes.Equal(output.RawData, event.Output) { |
| 91 | + slog.Debug("Output mismatch", |
| 92 | + "app", app.ContractAddress, "index", event.OutputIndex, |
| 93 | + "actual", output.RawData, "event's", event.Output) |
| 94 | + |
| 95 | + slog.Error("Output mismatch. Application is in an invalid state", |
| 96 | + "app", app.ContractAddress, |
| 97 | + "index", event.OutputIndex) |
| 98 | + |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + slog.Info("Output executed", "app", app.ContractAddress, "index", event.OutputIndex) |
| 103 | + output.TransactionHash = &event.Raw.TxHash |
| 104 | + executedOutputs = append(executedOutputs, output) |
| 105 | + } |
| 106 | + |
| 107 | + err = r.repository.UpdateOutputExecutionTransaction( |
| 108 | + ctx, app.ContractAddress, executedOutputs, mostRecentBlockNumber) |
| 109 | + if err != nil { |
| 110 | + slog.Error("Error storing output execution statuses", "app", app, "error", err) |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments