From 73a177d4574bb3bc4c4eaf4354cf74bb2af90918 Mon Sep 17 00:00:00 2001 From: Omri Date: Thu, 29 Jun 2023 18:47:50 +0200 Subject: [PATCH] fix: fixed bug where health event from settlement layer were ignored (#385) --- .go-version | 1 + rpc/middleware/middleware_test.go | 2 +- rpc/middleware/status.go | 6 +++++- settlement/dymension/dymension.go | 8 ++++---- settlement/dymension/dymension_test.go | 2 +- 5 files changed, 12 insertions(+), 7 deletions(-) create mode 100644 .go-version diff --git a/.go-version b/.go-version new file mode 100644 index 000000000..88ebadf2c --- /dev/null +++ b/.go-version @@ -0,0 +1 @@ +1.19.10 diff --git a/rpc/middleware/middleware_test.go b/rpc/middleware/middleware_test.go index 90f93bf2e..44a92a93c 100644 --- a/rpc/middleware/middleware_test.go +++ b/rpc/middleware/middleware_test.go @@ -80,7 +80,7 @@ func TestStatusMiddleware(t *testing.T) { { healthy: &sharedtypes.HealthStatus{IsHealthy: false, Error: errors.New("Node Unhealthy")}, expectedStatus: http.StatusServiceUnavailable, - expectedMessage: "Node Unhealthy", + expectedMessage: "node is unhealthy: Node Unhealthy", }, } diff --git a/rpc/middleware/status.go b/rpc/middleware/status.go index db5ee78d7..9eccf5072 100644 --- a/rpc/middleware/status.go +++ b/rpc/middleware/status.go @@ -2,6 +2,7 @@ package middleware import ( "errors" + "fmt" "net/http" "github.com/dymensionxyz/dymint/rpc/sharedtypes" @@ -34,8 +35,11 @@ func status(healthStatus *sharedtypes.HealthStatus, h http.Handler, logger log.L isHealthy, err := healthStatus.Get() if !isHealthy { w.WriteHeader(http.StatusServiceUnavailable) + errorPrefix := "node is unhealthy" if err == nil { - err = errors.New("node is unhealthy") + err = errors.New(errorPrefix) + } else { + err = fmt.Errorf("%s: %w", errorPrefix, err) } _, err := w.Write([]byte(err.Error())) if err != nil { diff --git a/settlement/dymension/dymension.go b/settlement/dymension/dymension.go index 0562c5571..e530b842a 100644 --- a/settlement/dymension/dymension.go +++ b/settlement/dymension/dymension.go @@ -216,7 +216,7 @@ WaitForBatchAcceptance: if err != nil { d.logger.Error("Failed submitting batch to settlement layer. Emitting unhealthy event", "startHeight", batch.StartHeight, "endHeight", batch.EndHeight, "error", err) - heatlhEventData := settlement.EventDataSettlementHealthStatus{Healthy: false, Error: err} + heatlhEventData := &settlement.EventDataSettlementHealthStatus{Healthy: false, Error: err} utils.SubmitEventOrPanic(d.ctx, d.pubsub, heatlhEventData, map[string][]string{settlement.EventTypeKey: {settlement.EventSettlementHealthStatus}}) // Sleep to allow context cancellation to take effect before retrying @@ -232,7 +232,7 @@ WaitForBatchAcceptance: case <-d.batchAcceptedCh: d.logger.Debug("Batch accepted by settlement layer. Emitting healthy event", "startHeight", batch.StartHeight, "endHeight", batch.EndHeight) - heatlhEventData := settlement.EventDataSettlementHealthStatus{Healthy: true} + heatlhEventData := &settlement.EventDataSettlementHealthStatus{Healthy: true} utils.SubmitEventOrPanic(d.ctx, d.pubsub, heatlhEventData, map[string][]string{settlement.EventTypeKey: {settlement.EventSettlementHealthStatus}}) return @@ -250,7 +250,7 @@ WaitForBatchAcceptance: utils.SubmitEventOrPanic(d.ctx, d.pubsub, batchAcceptedEvent, map[string][]string{settlement.EventTypeKey: {settlement.EventNewSettlementBatchAccepted}}) // Emit health event - heatlhEventData := settlement.EventDataSettlementHealthStatus{Healthy: true} + heatlhEventData := &settlement.EventDataSettlementHealthStatus{Healthy: true} utils.SubmitEventOrPanic(d.ctx, d.pubsub, heatlhEventData, map[string][]string{settlement.EventTypeKey: {settlement.EventSettlementHealthStatus}}) return @@ -258,7 +258,7 @@ WaitForBatchAcceptance: // Batch was not accepted by the settlement layer. Emitting unhealthy event d.logger.Debug("Batch not accepted by settlement layer. Emitting unhealthy event", "startHeight", batch.StartHeight, "endHeight", batch.EndHeight) - heatlhEventData := settlement.EventDataSettlementHealthStatus{Healthy: false, Error: settlement.ErrBatchNotAccepted} + heatlhEventData := &settlement.EventDataSettlementHealthStatus{Healthy: false, Error: settlement.ErrBatchNotAccepted} utils.SubmitEventOrPanic(d.ctx, d.pubsub, heatlhEventData, map[string][]string{settlement.EventTypeKey: {settlement.EventSettlementHealthStatus}}) // Stop the ticker and restart the loop diff --git a/settlement/dymension/dymension_test.go b/settlement/dymension/dymension_test.go index 860220aec..3954c1141 100644 --- a/settlement/dymension/dymension_test.go +++ b/settlement/dymension/dymension_test.go @@ -205,7 +205,7 @@ func TestPostBatch(t *testing.T) { select { case healthEvent := <-HealthSubscription.Out(): t.Logf("got health event: %v", healthEvent) - healthStatusEvent := healthEvent.Data().(settlement.EventDataSettlementHealthStatus) + healthStatusEvent := healthEvent.Data().(*settlement.EventDataSettlementHealthStatus) assert.Equal(t, c.expectedHealthEventValue, healthStatusEvent.Healthy) assert.Equal(t, c.expectedError, healthStatusEvent.Error) wg.Done()