Skip to content

Commit

Permalink
Merge pull request #30 from kaleido-io/status-endpoints
Browse files Browse the repository at this point in the history
GET /status/* APIs for Liveness and Readiness Probes
  • Loading branch information
peterbroadhurst authored Sep 3, 2022
2 parents d227820 + a2578eb commit 8c1eba4
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 27 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ coverage.txt
.DS_Store
__debug*
.leveldb
.vscode/*.log
.vscode/*.log
*.iml
.idea/
2 changes: 1 addition & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func configCommand() *cobra.Command {
Long: "",
RunE: func(cmd *cobra.Command, args []string) error {
initConfig()
b, err := config.GenerateConfigMarkdown(context.Background(), config.GetKnownKeys())
b, err := config.GenerateConfigMarkdown(context.Background(), "", config.GetKnownKeys())
fmt.Println(string(b))
return err
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/config_docs_generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestGenerateConfigDocs(t *testing.T) {
initConfig()
f, err := os.Create(filepath.Join("..", "config.md"))
assert.NoError(t, err)
generatedConfig, err := config.GenerateConfigMarkdown(context.Background(), config.GetKnownKeys())
generatedConfig, err := config.GenerateConfigMarkdown(context.Background(), "", config.GetKnownKeys())
assert.NoError(t, err)
_, err = f.Write(generatedConfig)
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/config_docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
func TestConfigDocsUpToDate(t *testing.T) {
// Initialize config of all plugins
initConfig()
generatedConfig, err := config.GenerateConfigMarkdown(context.Background(), config.GetKnownKeys())
generatedConfig, err := config.GenerateConfigMarkdown(context.Background(), "", config.GetKnownKeys())
assert.NoError(t, err)
configOnDisk, err := os.ReadFile(filepath.Join("..", "config.md"))
assert.NoError(t, err)
Expand Down
17 changes: 0 additions & 17 deletions config.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
---
layout: default
title: pages.reference
parent: Reference
nav_order: 2
---

# Configuration Reference
{: .no_toc }

<!-- ## Table of contents
{: .no_toc .text-delta }
1. TOC
{:toc} -->

---


## api
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ go 1.17

require (
github.com/hashicorp/golang-lru v0.5.4
github.com/hyperledger/firefly-common v0.1.20
github.com/hyperledger/firefly-common v1.1.2
github.com/hyperledger/firefly-signer v0.9.19
github.com/hyperledger/firefly-transaction-manager v0.9.10
github.com/hyperledger/firefly-transaction-manager v0.9.11
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.4.0
github.com/stretchr/testify v1.8.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,12 @@ github.com/huandu/xstrings v1.3.1 h1:4jgBlKK6tLKFvO8u5pmYjG91cqytmDCDvGh7ECVFfFs
github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/hyperledger/firefly-common v0.1.13/go.mod h1:2NqPi5Ud9H6rSlZXkLbotxW7z4EAD89p3/8oNOpm9Gs=
github.com/hyperledger/firefly-common v0.1.17-0.20220808193503-961a6b241a1a/go.mod h1:MNbaI2spBsdZYOub6Duj9xueE7Qyu9itOmJ4vE8tjYw=
github.com/hyperledger/firefly-common v0.1.20 h1:0dShkjlIShyBxkXRmu3vLmpEK6xrqmfc8GhF6k0Vgbg=
github.com/hyperledger/firefly-common v0.1.20/go.mod h1:gMlv4Iy5JjnzXmSEdb+tWVDIc/2GhL9MRcgNX+VmI4M=
github.com/hyperledger/firefly-common v1.1.2 h1:tSsptTxlgOlbbsyU6Ae83rOWA6tx1o0oNdqI8PAgzsk=
github.com/hyperledger/firefly-common v1.1.2/go.mod h1:gMlv4Iy5JjnzXmSEdb+tWVDIc/2GhL9MRcgNX+VmI4M=
github.com/hyperledger/firefly-signer v0.9.19 h1:WOjgQhcxBtfP/wxqiz07bnMDglrXlt3X4+shmNsQGIc=
github.com/hyperledger/firefly-signer v0.9.19/go.mod h1:GPQRUZOFOAjkLmg8GDjZUjEdUD0gcar+CSVhwltIwyw=
github.com/hyperledger/firefly-transaction-manager v0.9.10 h1:m5iT5zwy+0mvNLE2eNYjMnLvqTIAxsKgku12aJxAbl8=
github.com/hyperledger/firefly-transaction-manager v0.9.10/go.mod h1:wB5YXPIL4sXXWLXO2pwjdAj9q4+iJsYt4NVaWtuttRI=
github.com/hyperledger/firefly-transaction-manager v0.9.11 h1:lhADYYqbGTnhU31oqveOsnQYqnCjjoRn9d4i4uWA3dI=
github.com/hyperledger/firefly-transaction-manager v0.9.11/go.mod h1:wB5YXPIL4sXXWLXO2pwjdAj9q4+iJsYt4NVaWtuttRI=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA=
Expand Down
5 changes: 5 additions & 0 deletions internal/ethereum/error_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
sendRPCMethods
callRPCMethods
blockRPCMethods
netVersionRPCMethods
)

// mapErrorToReason provides a common place for mapping Ethereum client
Expand Down Expand Up @@ -64,6 +65,10 @@ func mapError(methodType ethRPCMethodCategory, err error) ffcapi.ErrorReason {
if strings.Contains(errString, "cannot query unfinalized data") {
return ffcapi.ErrorReasonNotFound
}
case netVersionRPCMethods:
if strings.Contains(errString, "the method net_version does not exist/is not available") {
return ffcapi.ErrorReasonNotFound
}
}

// Best default in FFCAPI is to provide no mapping
Expand Down
49 changes: 49 additions & 0 deletions internal/ethereum/statuses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright © 2022 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ethereum

import (
"context"

"github.com/hyperledger/firefly-common/pkg/fftypes"
"github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi"
)

func (c *ethConnector) IsLive(_ context.Context) (*ffcapi.LiveResponse, ffcapi.ErrorReason, error) {
return &ffcapi.LiveResponse{
Up: true,
}, "", nil
}

func (c *ethConnector) IsReady(ctx context.Context) (*ffcapi.ReadyResponse, ffcapi.ErrorReason, error) {
var chainID string
err := c.backend.CallRPC(ctx, &chainID, "net_version")
if err != nil {
return &ffcapi.ReadyResponse{
Ready: false,
}, mapError(netVersionRPCMethods, err), err
}

details := &fftypes.JSONObject{
"chainID": chainID,
}

return &ffcapi.ReadyResponse{
Ready: true,
DownstreamDetails: fftypes.JSONAnyPtr(details.String()),
}, "", nil
}
67 changes: 67 additions & 0 deletions internal/ethereum/statuses_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright © 2022 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ethereum

import (
"fmt"
"github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
)

func TestIsLive(t *testing.T) {
ctx, c, _, done := newTestConnector(t)
defer done()

status, _, err := c.IsLive(ctx)
assert.NoError(t, err)
assert.True(t, status.Up)
}

func TestIsReady(t *testing.T) {
ctx, c, mRPC, done := newTestConnector(t)
defer done()

mRPC.On("CallRPC", mock.Anything, mock.Anything, "net_version").
Run(func(args mock.Arguments) {
*(args[1].(*string)) = "80001"
}).
Return(nil)

status, _, err := c.IsReady(ctx)
assert.NoError(t, err)
assert.True(t, status.Ready)
assert.NotNil(t, status.DownstreamDetails)

details := status.DownstreamDetails.JSONObject()
assert.Equal(t, details.GetString("chainID"), "80001")
}

func TestIsReadyError(t *testing.T) {
ctx, c, mRPC, done := newTestConnector(t)
defer done()

mRPC.On("CallRPC", mock.Anything, mock.Anything, "net_version").
Return(fmt.Errorf("the method net_version does not exist/is not available"))

status, reason, err := c.IsReady(ctx)
assert.Error(t, err)
assert.Equal(t, reason, ffcapi.ErrorReasonNotFound)
assert.False(t, status.Ready)
assert.Nil(t, status.DownstreamDetails)
}

0 comments on commit 8c1eba4

Please sign in to comment.