|
| 1 | +package checkethconfig |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/ethpandaops/assertoor/pkg/coordinator/clients/execution" |
| 11 | + "github.com/ethpandaops/assertoor/pkg/coordinator/types" |
| 12 | + "github.com/sirupsen/logrus" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + TaskName = "check_eth_config" |
| 17 | + TaskDescriptor = &types.TaskDescriptor{ |
| 18 | + Name: TaskName, |
| 19 | + Description: "Checks that all execution clients return the same eth_config (EIP-7910)", |
| 20 | + Config: DefaultConfig(), |
| 21 | + NewTask: NewTask, |
| 22 | + } |
| 23 | +) |
| 24 | + |
| 25 | +type Task struct { |
| 26 | + ctx *types.TaskContext |
| 27 | + options *types.TaskOptions |
| 28 | + config Config |
| 29 | + logger logrus.FieldLogger |
| 30 | +} |
| 31 | + |
| 32 | +func NewTask(ctx *types.TaskContext, options *types.TaskOptions) (types.Task, error) { |
| 33 | + return &Task{ |
| 34 | + ctx: ctx, |
| 35 | + options: options, |
| 36 | + logger: ctx.Logger.GetLogger(), |
| 37 | + }, nil |
| 38 | +} |
| 39 | + |
| 40 | +func (t *Task) Config() interface{} { |
| 41 | + return t.config |
| 42 | +} |
| 43 | + |
| 44 | +func (t *Task) Timeout() time.Duration { |
| 45 | + return t.options.Timeout.Duration |
| 46 | +} |
| 47 | + |
| 48 | +func (t *Task) LoadConfig() error { |
| 49 | + config := DefaultConfig() |
| 50 | + |
| 51 | + // parse static config |
| 52 | + if t.options.Config != nil { |
| 53 | + if err := t.options.Config.Unmarshal(&config); err != nil { |
| 54 | + return fmt.Errorf("error parsing task config for %v: %w", TaskName, err) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // load dynamic vars |
| 59 | + err := t.ctx.Vars.ConsumeVars(&config, t.options.ConfigVars) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + // validate config |
| 65 | + if err := config.Validate(); err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + t.config = config |
| 70 | + |
| 71 | + return nil |
| 72 | +} |
| 73 | + |
| 74 | +func (t *Task) Execute(ctx context.Context) error { |
| 75 | + // Get the client pool from the scheduler |
| 76 | + clientPool := t.ctx.Scheduler.GetServices().ClientPool() |
| 77 | + |
| 78 | + // Get matching clients from the pool |
| 79 | + var clients []*execution.Client |
| 80 | + |
| 81 | + if t.config.ClientPattern == "" && t.config.ExcludeClientPattern == "" { |
| 82 | + clients = clientPool.GetExecutionPool().GetReadyEndpoints(true) |
| 83 | + if len(clients) == 0 { |
| 84 | + t.logger.Error("check failed: no matching clients found") |
| 85 | + t.ctx.SetResult(types.TaskResultFailure) |
| 86 | + |
| 87 | + return nil |
| 88 | + } |
| 89 | + } else { |
| 90 | + poolClients := clientPool.GetClientsByNamePatterns(t.config.ClientPattern, t.config.ExcludeClientPattern) |
| 91 | + if len(poolClients) == 0 { |
| 92 | + t.logger.Errorf("check failed: no matching clients found with pattern %v", t.config.ClientPattern) |
| 93 | + t.ctx.SetResult(types.TaskResultFailure) |
| 94 | + |
| 95 | + return nil |
| 96 | + } |
| 97 | + |
| 98 | + clients = make([]*execution.Client, len(poolClients)) |
| 99 | + for i, c := range poolClients { |
| 100 | + clients[i] = c.ExecutionClient |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Query eth_config from all clients |
| 105 | + type clientResult struct { |
| 106 | + client *execution.Client |
| 107 | + configJSON string |
| 108 | + err error |
| 109 | + } |
| 110 | + |
| 111 | + results := make([]clientResult, len(clients)) |
| 112 | + |
| 113 | + for i, client := range clients { |
| 114 | + t.logger.Infof("querying eth_config from client %v", client.GetName()) |
| 115 | + |
| 116 | + ethConfig, err := client.GetRPCClient().GetEthConfig(ctx) |
| 117 | + if err != nil { |
| 118 | + results[i] = clientResult{ |
| 119 | + client: client, |
| 120 | + err: err, |
| 121 | + } |
| 122 | + |
| 123 | + t.logger.WithField("client", client.GetName()).Errorf("RPC error when querying eth_config: %v", err) |
| 124 | + |
| 125 | + if t.config.FailOnMismatch { |
| 126 | + t.ctx.SetResult(types.TaskResultFailure) |
| 127 | + return nil |
| 128 | + } |
| 129 | + |
| 130 | + continue |
| 131 | + } |
| 132 | + |
| 133 | + // Convert to JSON for comparison |
| 134 | + configBytes, err := json.MarshalIndent(ethConfig, "", " ") |
| 135 | + if err != nil { |
| 136 | + results[i] = clientResult{ |
| 137 | + client: client, |
| 138 | + err: fmt.Errorf("failed to marshal config: %w", err), |
| 139 | + } |
| 140 | + |
| 141 | + t.logger.WithField("client", client.GetName()).Errorf("error marshaling eth_config: %v", err) |
| 142 | + |
| 143 | + if t.config.FailOnMismatch { |
| 144 | + t.ctx.SetResult(types.TaskResultFailure) |
| 145 | + return nil |
| 146 | + } |
| 147 | + |
| 148 | + continue |
| 149 | + } |
| 150 | + |
| 151 | + results[i] = clientResult{ |
| 152 | + client: client, |
| 153 | + configJSON: string(configBytes), |
| 154 | + } |
| 155 | + |
| 156 | + t.logger.WithField("client", client.GetName()).Debugf("eth_config response:\n%s", string(configBytes)) |
| 157 | + t.logger.Infof("client %v returned eth_config successfully", client.GetName()) |
| 158 | + } |
| 159 | + |
| 160 | + // Check for consistency |
| 161 | + var referenceConfig string |
| 162 | + |
| 163 | + mismatchFound := false |
| 164 | + configMap := make(map[string][]string) // config JSON -> list of client names |
| 165 | + |
| 166 | + for _, result := range results { |
| 167 | + if result.err != nil { |
| 168 | + continue |
| 169 | + } |
| 170 | + |
| 171 | + if referenceConfig == "" { |
| 172 | + referenceConfig = result.configJSON |
| 173 | + t.ctx.Outputs.SetVar("ethConfig", result.configJSON) |
| 174 | + } |
| 175 | + |
| 176 | + // Track which clients returned which config |
| 177 | + configMap[result.configJSON] = append(configMap[result.configJSON], result.client.GetName()) |
| 178 | + |
| 179 | + if result.configJSON != referenceConfig { |
| 180 | + mismatchFound = true |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + if mismatchFound { |
| 185 | + // Build diff output |
| 186 | + var diffBuilder strings.Builder |
| 187 | + |
| 188 | + diffBuilder.WriteString("eth_config mismatch detected across clients:\n\n") |
| 189 | + |
| 190 | + configIndex := 1 |
| 191 | + for config, clientNames := range configMap { |
| 192 | + diffBuilder.WriteString(fmt.Sprintf("Config variant #%d (clients: %v):\n", configIndex, clientNames)) |
| 193 | + diffBuilder.WriteString(config) |
| 194 | + diffBuilder.WriteString("\n\n") |
| 195 | + |
| 196 | + configIndex++ |
| 197 | + } |
| 198 | + |
| 199 | + t.logger.Error(diffBuilder.String()) |
| 200 | + |
| 201 | + if t.config.FailOnMismatch { |
| 202 | + t.ctx.SetResult(types.TaskResultFailure) |
| 203 | + } else { |
| 204 | + t.ctx.SetResult(types.TaskResultNone) |
| 205 | + } |
| 206 | + |
| 207 | + return nil |
| 208 | + } |
| 209 | + |
| 210 | + // All checks passed |
| 211 | + if len(results) > 0 { |
| 212 | + t.logger.Infof("all %d clients returned consistent eth_config", len(results)) |
| 213 | + t.logger.Debugf("consistent eth_config:\n%s", referenceConfig) |
| 214 | + t.ctx.SetResult(types.TaskResultSuccess) |
| 215 | + } |
| 216 | + |
| 217 | + return nil |
| 218 | +} |
0 commit comments