Skip to content

Commit

Permalink
add http client as an argument to abifetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
serichoi65 committed Feb 21, 2025
1 parent 4b84e9b commit ba9e318
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 30 deletions.
4 changes: 3 additions & 1 deletion cmd/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"github.com/spf13/viper"
"go.uber.org/zap"
"log"
"net/http"
"time"
)

var runDatabaseCmd = &cobra.Command{
Expand Down Expand Up @@ -59,7 +61,7 @@ var runDatabaseCmd = &cobra.Command{

client := ethereum.NewClient(ethereum.ConvertGlobalConfigToEthereumConfig(&cfg.EthereumRpcConfig), l)

af := abiFetcher.NewAbiFetcher(client, l)
af := abiFetcher.NewAbiFetcher(client, &http.Client{Timeout: 5 * time.Second}, l, cfg)

pgConfig := postgres.PostgresConfigFromDbConfig(&cfg.DatabaseConfig)

Expand Down
4 changes: 3 additions & 1 deletion cmd/debugger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"context"
"fmt"
"net/http"
"time"

"github.com/Layr-Labs/sidecar/pkg/abiFetcher"
"github.com/Layr-Labs/sidecar/pkg/clients/ethereum"
Expand Down Expand Up @@ -56,7 +58,7 @@ func main() {

client := ethereum.NewClient(ethereum.ConvertGlobalConfigToEthereumConfig(&cfg.EthereumRpcConfig), l)

af := abiFetcher.NewAbiFetcher(client, l)
af := abiFetcher.NewAbiFetcher(client, &http.Client{Timeout: 5 * time.Second}, l, cfg)

pgConfig := postgres.PostgresConfigFromDbConfig(&cfg.DatabaseConfig)

Expand Down
4 changes: 3 additions & 1 deletion cmd/operatorRestakedStrategies.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/spf13/viper"
"go.uber.org/zap"
"log"
"net/http"
"time"
)

var runOperatorRestakedStrategiesCmd = &cobra.Command{
Expand All @@ -47,7 +49,7 @@ var runOperatorRestakedStrategiesCmd = &cobra.Command{

client := ethereum.NewClient(ethereum.ConvertGlobalConfigToEthereumConfig(&cfg.EthereumRpcConfig), l)

af := abiFetcher.NewAbiFetcher(client, l)
af := abiFetcher.NewAbiFetcher(client, &http.Client{Timeout: 5 * time.Second}, l, cfg)

pgConfig := postgres.PostgresConfigFromDbConfig(&cfg.DatabaseConfig)

Expand Down
3 changes: 2 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/Layr-Labs/sidecar/pkg/sidecar"
pgStorage "github.com/Layr-Labs/sidecar/pkg/storage/postgres"
"log"
"net/http"
"time"

"github.com/Layr-Labs/sidecar/internal/config"
Expand Down Expand Up @@ -74,7 +75,7 @@ var runCmd = &cobra.Command{

client := ethereum.NewClient(ethereum.ConvertGlobalConfigToEthereumConfig(&cfg.EthereumRpcConfig), l)

af := abiFetcher.NewAbiFetcher(client, l)
af := abiFetcher.NewAbiFetcher(client, &http.Client{Timeout: 5 * time.Second}, l, cfg)

pgConfig := postgres.PostgresConfigFromDbConfig(&cfg.DatabaseConfig)

Expand Down
11 changes: 11 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ type SidecarPrimaryConfig struct {
IsPrimary bool
}

type IpfsConfig struct {
Url string
}

type Config struct {
Debug bool
EthereumRpcConfig EthereumRpcConfig
Expand All @@ -109,6 +113,7 @@ type Config struct {
DataDogConfig DataDogConfig
PrometheusConfig PrometheusConfig
SidecarPrimaryConfig SidecarPrimaryConfig
IpfsConfig IpfsConfig
}

func StringWithDefault(value, defaultValue string) string {
Expand Down Expand Up @@ -147,6 +152,8 @@ var (
PrometheusPort = "prometheus.port"

SidecarPrimaryUrl = "sidecar-primary.url"

IpfsUrl = "ipfs.url"
)

func NewConfig() *Config {
Expand Down Expand Up @@ -202,6 +209,10 @@ func NewConfig() *Config {
SidecarPrimaryConfig: SidecarPrimaryConfig{
Url: viper.GetString(normalizeFlagName(SidecarPrimaryUrl)),
},

IpfsConfig: IpfsConfig{
Url: StringWithDefault(viper.GetString(normalizeFlagName(IpfsUrl)), "https://ipfs.io/ipfs"),
},
}
}

Expand Down
20 changes: 10 additions & 10 deletions pkg/abiFetcher/abiFetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ import (
"io"
"net/http"
"strings"
"time"

"github.com/Layr-Labs/sidecar/internal/config"
"github.com/Layr-Labs/sidecar/pkg/clients/ethereum"
"github.com/btcsuite/btcutil/base58"
"go.uber.org/zap"
)

type AbiFetcher struct {
EthereumClient *ethereum.Client
httpClient *http.Client
Logger *zap.Logger
Config *config.Config
}

type Response struct {
Expand All @@ -28,11 +30,15 @@ type Response struct {

func NewAbiFetcher(
e *ethereum.Client,
hc *http.Client,
l *zap.Logger,
cfg *config.Config,
) *AbiFetcher {
return &AbiFetcher{
EthereumClient: e,
httpClient: hc,
Logger: l,
Config: cfg,
}
}

Expand All @@ -51,7 +57,6 @@ func (af *AbiFetcher) FetchMetadataFromAddress(ctx context.Context, address stri
zap.String("address", address),
zap.String("bytecodeHash", bytecodeHash),
)
fmt.Printf("bytecodeHash: %s", bytecodeHash)

// fetch ABI using IPFS
// TODO: add a fallback method using Etherscan
Expand Down Expand Up @@ -93,7 +98,7 @@ func (af *AbiFetcher) GetIPFSUrlFromBytecode(bytecode string) (string, error) {
// Convert to base58
base58Hash := base58.Encode(bytes)

return fmt.Sprintf("https://ipfs.io/ipfs/%s", base58Hash), nil
return fmt.Sprintf("%s/%s", af.Config.IpfsConfig.Url, base58Hash), nil
}

func (af *AbiFetcher) FetchAbiFromIPFS(address string, bytecode string) (string, error) {
Expand All @@ -107,13 +112,8 @@ func (af *AbiFetcher) FetchAbiFromIPFS(address string, bytecode string) (string,
}
af.Logger.Sugar().Debug("Successfully retrieved IPFS URL",
zap.String("address", address),
zap.String("IPFS URL", url),
zap.String("ipfsUrl", url),
)
fmt.Printf("IPFS URL: %s \n", url)

httpClient := &http.Client{
Timeout: 5 * time.Second,
}

req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil)
if err != nil {
Expand All @@ -124,7 +124,7 @@ func (af *AbiFetcher) FetchAbiFromIPFS(address string, bytecode string) (string,
return "", err
}

resp, err := httpClient.Do(req)
resp, err := af.httpClient.Do(req)
if err != nil {
af.Logger.Sugar().Errorw("Failed to perform HTTP request",
zap.Error(err),
Expand Down
46 changes: 38 additions & 8 deletions pkg/abiFetcher/abiFetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package abiFetcher

import (
"context"
"net/http"
"reflect"
"testing"
"time"

"os"

Expand All @@ -11,6 +14,8 @@ import (
"github.com/Layr-Labs/sidecar/internal/tests"
"github.com/Layr-Labs/sidecar/pkg/clients/ethereum"
"github.com/Layr-Labs/sidecar/pkg/postgres"
"github.com/agiledragon/gomonkey/v2"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"gorm.io/gorm"
Expand Down Expand Up @@ -39,20 +44,24 @@ func setup() (
}

func Test_AbiFetcher(t *testing.T) {
_, _, l, _, err := setup()
_, _, l, cfg, err := setup()

if err != nil {
t.Fatal(err)
}

httpClient := &http.Client{
Timeout: 5 * time.Second,
}

baseUrl := "http://72.46.85.253:8545"
ethConfig := ethereum.DefaultNativeCallEthereumClientConfig()
ethConfig.BaseUrl = baseUrl

client := ethereum.NewClient(ethConfig, l)

t.Run("Test getting IPFS url from bytecode", func(t *testing.T) {
af := NewAbiFetcher(client, l)
af := NewAbiFetcher(client, httpClient, l, cfg)

address := "0x29a954e9e7f12936db89b183ecdf879fbbb99f14"
bytecode, err := af.EthereumClient.GetCode(context.Background(), address)
Expand All @@ -65,15 +74,36 @@ func Test_AbiFetcher(t *testing.T) {
assert.Equal(t, expectedUrl, url)
})
t.Run("Test fetching ABI from IPFS", func(t *testing.T) {
af := NewAbiFetcher(client, l)
mockUrl := "https://test"
patches := gomonkey.ApplyMethod(reflect.TypeOf(&AbiFetcher{}), "GetIPFSUrlFromBytecode",
func(_ *AbiFetcher, _ string) (string, error) {
return mockUrl, nil
})
defer patches.Reset()

address := "0x29a954e9e7f12936db89b183ecdf879fbbb99f14"
bytecode, err := af.EthereumClient.GetCode(context.Background(), address)
assert.Nil(t, err)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

abi, err := af.FetchAbiFromIPFS(address, bytecode)
mockAbiResponse := `{
"output": {
"abi": "[{\"type\":\"function\",\"name\":\"test\"}]"
}
}`

httpmock.RegisterResponder("GET", mockUrl,
httpmock.NewStringResponder(200, mockAbiResponse))

mockHttpClient := &http.Client{
Transport: httpmock.DefaultTransport,
}

af := NewAbiFetcher(client, mockHttpClient, l, cfg)

address := "0x29a954e9e7f12936db89b183ecdf879fbbb99f14"
abi, err := af.FetchAbiFromIPFS(address, "mocked")
assert.Nil(t, err)

assert.Greater(t, len(abi), 0)
expectedAbi := `"[{\"type\":\"function\",\"name\":\"test\"}]"`
assert.Equal(t, abi, expectedAbi)
})
}
11 changes: 6 additions & 5 deletions pkg/contractManager/contractManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (cm *ContractManager) GetContractWithProxy(
return contract, nil
}

// this function is called when there's an "Upgraded" event
// HandleContractUpgrade parses an Upgraded contract log and inserts the new upgraded implementation into the database
func (cm *ContractManager) HandleContractUpgrade(ctx context.Context, blockNumber uint64, upgradedLog *parser.DecodedLog) error {
// the new address that the contract points to
newProxiedAddress := ""
Expand Down Expand Up @@ -87,10 +87,11 @@ func (cm *ContractManager) HandleContractUpgrade(ctx context.Context, blockNumbe
}

newProxiedAddress = "0x" + storageValue[26:]
if newProxiedAddress == "" {
cm.Logger.Sugar().Debugw("No new proxied address found", zap.String("address", upgradedLog.Address))
return fmt.Errorf("no new proxied address found for %s during the 'Upgraded' event", upgradedLog.Address)
}
}

if newProxiedAddress == "" {
cm.Logger.Sugar().Debugw("No new proxied address found", zap.String("address", upgradedLog.Address))
return fmt.Errorf("no new proxied address found for %s during the 'Upgraded' event", upgradedLog.Address)
}

err := cm.CreateUpgradedProxyContract(ctx, blockNumber, upgradedLog.Address, newProxiedAddress)
Expand Down
3 changes: 2 additions & 1 deletion pkg/contractManager/contractManager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"reflect"
"testing"
"time"

"os"

Expand Down Expand Up @@ -72,7 +73,7 @@ func Test_ContractManager(t *testing.T) {

client := ethereum.NewClient(ethConfig, l, mockHttpClient)

af := abiFetcher.NewAbiFetcher(client, l)
af := abiFetcher.NewAbiFetcher(client, &http.Client{Timeout: 5 * time.Second}, l, cfg)

metricsClients, err := metrics.InitMetricsSinksFromConfig(cfg, l)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/indexer/restakedStrategies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"net/http"
"testing"
"time"

Expand Down Expand Up @@ -76,7 +77,7 @@ func Test_IndexerRestakedStrategies(t *testing.T) {

client := ethereum.NewClient(ethConfig, l)

af := abiFetcher.NewAbiFetcher(client, l)
af := abiFetcher.NewAbiFetcher(client, &http.Client{Timeout: 5 * time.Second}, l, cfg)

metricsClients, err := metrics.InitMetricsSinksFromConfig(cfg, l)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion pkg/pipeline/pipelineIntegration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"database/sql"
"fmt"
"log"
"net/http"
"testing"
"time"

"os"

Expand Down Expand Up @@ -77,7 +79,7 @@ func setup(ethConfig *ethereum.EthereumClientConfig) (
ethConfig.BaseUrl = rpcUrl
client := ethereum.NewClient(ethConfig, l)

af := abiFetcher.NewAbiFetcher(client, l)
af := abiFetcher.NewAbiFetcher(client, &http.Client{Timeout: 5 * time.Second}, l, cfg)

dbname, _, grm, err := postgres.GetTestPostgresDatabase(cfg.DatabaseConfig, cfg, l)
if err != nil {
Expand Down

0 comments on commit ba9e318

Please sign in to comment.