From 58c1dc73adbe4533ab6d16e5cf391678318d6569 Mon Sep 17 00:00:00 2001 From: Ales Verbic Date: Tue, 15 Oct 2024 17:49:22 -0400 Subject: [PATCH] fix: update address conversion with updated NewAddress (#262) add custom KugoLogger to suppress debug logs Signed-off-by: Ales Verbic --- go.mod | 2 +- go.sum | 4 +- input/chainsync/chainsync.go | 8 ++- input/chainsync/transactionOutput.go | 33 +--------- input/chainsync/transactionOutput_test.go | 9 +-- internal/logging/kugoCustomLogger.go | 74 +++++++++++++++++++++++ 6 files changed, 87 insertions(+), 43 deletions(-) create mode 100644 internal/logging/kugoCustomLogger.go diff --git a/go.mod b/go.mod index 73cc98e..15ee9c5 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ toolchain go1.21.6 require ( github.com/SundaeSwap-finance/kugo v1.0.5 github.com/SundaeSwap-finance/ogmigo/v6 v6.0.0-20231128043329-e8ced51013a1 - github.com/blinklabs-io/gouroboros v0.99.0 + github.com/blinklabs-io/gouroboros v0.100.0 github.com/gen2brain/beeep v0.0.0-20230602101333-f384c29b62dd github.com/gin-gonic/gin v1.10.0 github.com/kelseyhightower/envconfig v1.4.0 diff --git a/go.sum b/go.sum index a3b53e7..66eb91e 100644 --- a/go.sum +++ b/go.sum @@ -10,8 +10,8 @@ github.com/SundaeSwap-finance/ogmigo/v6 v6.0.0-20231128043329-e8ced51013a1 h1:Lf github.com/SundaeSwap-finance/ogmigo/v6 v6.0.0-20231128043329-e8ced51013a1/go.mod h1:CsDGcgbkKoz6S4h0RJ30go7oXG+KhGE2KLhBpRFnEqA= github.com/aws/aws-sdk-go v1.48.7 h1:gDcOhmkohlNk20j0uWpko5cLBbwSkB+xpkshQO45F7Y= github.com/aws/aws-sdk-go v1.48.7/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk= -github.com/blinklabs-io/gouroboros v0.99.0 h1:tGiti2mPjQM/rVOYi1ZrCSlKh8Qt3iQUkDdsOeWKuvw= -github.com/blinklabs-io/gouroboros v0.99.0/go.mod h1:gU9pBcL1h584sVqYF8H7JJcB2x0n1HdWcmWP11VYxPE= +github.com/blinklabs-io/gouroboros v0.100.0 h1:f1M0AA+Zi4YC4sbR0gJBOaMyKTClRTNY894r4Lm7TDk= +github.com/blinklabs-io/gouroboros v0.100.0/go.mod h1:otpDPTJTU/EVt3J4axaJAA1gF/0UtalSdVxVqXK8Zuk= github.com/blinklabs-io/ouroboros-mock v0.3.4 h1:codPfiI5vLeD6YdhKL5VwYSzy2N3Dsgx6xjcLsqFaJQ= github.com/blinklabs-io/ouroboros-mock v0.3.4/go.mod h1:e/wgG1ZYVenroN2XEMXy7DgEfdmP7KXVRHIQKuh8E/0= github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= diff --git a/input/chainsync/chainsync.go b/input/chainsync/chainsync.go index c678927..66756d3 100644 --- a/input/chainsync/chainsync.go +++ b/input/chainsync/chainsync.go @@ -27,6 +27,7 @@ import ( "github.com/SundaeSwap-finance/ogmigo/v6/ouroboros/chainsync" "github.com/blinklabs-io/adder/event" + "github.com/blinklabs-io/adder/internal/logging" "github.com/blinklabs-io/adder/plugin" ouroboros "github.com/blinklabs-io/gouroboros" @@ -427,7 +428,12 @@ func getKupoClient(c *ChainSync) (*kugo.Client, error) { return c.kupoClient, nil } - k := kugo.New(kugo.WithEndpoint(c.kupoUrl)) + KugoCustomLogger := logging.NewKugoCustomLogger(logging.LevelInfo) + + k := kugo.New( + kugo.WithEndpoint(c.kupoUrl), + kugo.WithLogger(KugoCustomLogger), + ) ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) defer cancel() diff --git a/input/chainsync/transactionOutput.go b/input/chainsync/transactionOutput.go index c5f7d14..bd27ef9 100644 --- a/input/chainsync/transactionOutput.go +++ b/input/chainsync/transactionOutput.go @@ -20,8 +20,6 @@ import ( "log/slog" "github.com/SundaeSwap-finance/kugo" - "github.com/blinklabs-io/gouroboros/base58" - "github.com/blinklabs-io/gouroboros/bech32" "github.com/blinklabs-io/gouroboros/cbor" "github.com/blinklabs-io/gouroboros/ledger" "github.com/blinklabs-io/gouroboros/ledger/common" @@ -80,21 +78,10 @@ func ExtractAssetDetailsFromMatch(match kugo.Match) (common.MultiAsset[uint64], } func NewResolvedTransactionOutput(match kugo.Match) (ledger.TransactionOutput, error) { - // FIXME - This is a patch to fix the issue with the address - // Attempt to create an address using Bech32 + // Get common.Address from base58 or bech32 string addr, err := common.NewAddress(match.Address) if err != nil { - // If Bech32 fails, try to convert from Base58 to Bech32 - bech32addr, err := ConvertBase58ToBech32(match.Address, "addr") - if err != nil { - return nil, fmt.Errorf("failed to convert base58 to bech32: %w", err) - } - - // Try to create the address again with the converted Bech32 address - addr, err = common.NewAddress(bech32addr) - if err != nil { - return nil, fmt.Errorf("failed to create address from base58-converted bech32 address: %w", err) - } + return nil, fmt.Errorf("failed to convert base58 to bech32: %w", err) } assets, amount, err := ExtractAssetDetailsFromMatch(match) @@ -147,19 +134,3 @@ func (txOut ResolvedTransactionOutput) Utxorpc() *utxorpc.TxOutput { // Placeholder for UTXO RPC representation return &utxorpc.TxOutput{} } - -// ConvertBase58ToBech32 converts a Base58 string to a Bech32 string -// using the given human-readable part (hrp) required for Bech32 encoding -func ConvertBase58ToBech32(base58Str, hrp string) (string, error) { - data := base58.Decode(base58Str) - converted, err := bech32.ConvertBits(data, 8, 5, true) - if err != nil { - return "", fmt.Errorf("failed to convert bits: %w", err) - } - bech32Str, err := bech32.Encode(hrp, converted) - if err != nil { - return "", fmt.Errorf("failed to encode Bech32: %w", err) - } - - return bech32Str, nil -} diff --git a/input/chainsync/transactionOutput_test.go b/input/chainsync/transactionOutput_test.go index 7be18c2..3d9cc75 100644 --- a/input/chainsync/transactionOutput_test.go +++ b/input/chainsync/transactionOutput_test.go @@ -68,14 +68,7 @@ func TestResolvedTransactionOutput_MarshalJSON(t *testing.T) { func TestConvertBase58ToBech32(t *testing.T) { base58Str := "Ae2tdPwUPEYwFx4dmJheyNPPYXtvHbJLeCaA96o6Y2iiUL18cAt7AizN2zG" - hrp := "addr" - expectedBech32Str := "addr1stvpskppsdvpcpyxtepdyde6mklt6hf2e7quwcxgfztszs5gnalwwccfrwsqqxhsrytd2f4f5v6" - - bech32Str, err := ConvertBase58ToBech32(base58Str, hrp) - assert.Nil(t, err, "Expected no error when converting Base58 to Bech32") - assert.Equal(t, expectedBech32Str, bech32Str, "The Bech32 string did not match the expected value") - - addr, err := common.NewAddress(bech32Str) + addr, err := common.NewAddress(base58Str) assert.Nil(t, err, "Expected no error when converting to common.Address") t.Logf("addr: %v", addr) } diff --git a/internal/logging/kugoCustomLogger.go b/internal/logging/kugoCustomLogger.go new file mode 100644 index 0000000..f032266 --- /dev/null +++ b/internal/logging/kugoCustomLogger.go @@ -0,0 +1,74 @@ +// Copyright 2024 Blink Labs Software +// +// 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 logging + +import ( + "log/slog" + "os" + + "github.com/SundaeSwap-finance/ogmigo/v6" +) + +// LogLevel represents the logging level (either INFO or DEBUG) +type LogLevel int + +const ( + LevelInfo LogLevel = iota + LevelDebug +) + +// KugoCustomLogger is a custom logger that uses slog and filters based on the log level +type KugoCustomLogger struct { + logger *slog.Logger + logLevel LogLevel +} + +// Info logs info-level messages +func (l *KugoCustomLogger) Info(message string, kvs ...ogmigo.KeyValue) { + l.logger.Info(message, convertKVs(kvs)...) +} + +// Debug logs debug-level messages only if log level is set to DEBUG +func (l *KugoCustomLogger) Debug(message string, kvs ...ogmigo.KeyValue) { + if l.logLevel >= LevelDebug { + l.logger.Debug(message, convertKVs(kvs)...) + } +} + +// With returns a new logger with additional context (key-value pairs) +func (l *KugoCustomLogger) With(kvs ...ogmigo.KeyValue) ogmigo.Logger { + return l // Here we just return the same logger, but you can add more context if needed +} + +// Helper function to convert ogmigo.KeyValue to slog key-value format +// Flattens the key-value pairs into a single slice +func convertKVs(kvs []ogmigo.KeyValue) []any { + result := make([]any, 0, len(kvs)*2) + for _, kv := range kvs { + result = append(result, kv.Key, kv.Value) + } + return result +} + +func NewKugoCustomLogger(level LogLevel) *KugoCustomLogger { + // Create a new slog logger that logs to stdout using JSON format + handler := slog.NewJSONHandler(os.Stdout, nil) + logger := slog.New(handler) + + return &KugoCustomLogger{ + logger: logger, + logLevel: level, + } +}