Skip to content

Commit

Permalink
add customized info/metadata (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
jingweicb authored Jan 16, 2023
1 parent 193721d commit f476c00
Show file tree
Hide file tree
Showing 8 changed files with 396 additions and 204 deletions.
30 changes: 23 additions & 7 deletions cmd/check_construction.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

cliErrs "github.com/coinbase/rosetta-cli/pkg/errors"
"github.com/coinbase/rosetta-cli/pkg/logger"
"github.com/fatih/color"

"github.com/coinbase/rosetta-cli/pkg/results"
"github.com/coinbase/rosetta-cli/pkg/tester"
Expand Down Expand Up @@ -54,6 +55,7 @@ and UTXO-based blockchains). However, we plan to add support for testing
arbitrary scenarios (for example, staking and governance).`,
RunE: runCheckConstructionCmd,
}
constructionMetadata string
)

func runCheckConstructionCmd(_ *cobra.Command, _ []string) error {
Expand All @@ -66,6 +68,10 @@ func runCheckConstructionCmd(_ *cobra.Command, _ []string) error {
)
}

metadataMap := logger.ConvertStringToMap(Config.InfoMetaData)
metadataMap = logger.AddRequestUUIDToMap(metadataMap, Config.RequestUUID)
constructionMetadata = logger.ConvertMapToString(metadataMap)

ensureDataDirectoryExists()
ctx, cancel := context.WithCancel(Context)

Expand All @@ -74,6 +80,7 @@ func runCheckConstructionCmd(_ *cobra.Command, _ []string) error {
fetcher.WithRetryElapsedTime(time.Duration(Config.RetryElapsedTime) * time.Second),
fetcher.WithTimeout(time.Duration(Config.HTTPTimeout) * time.Second),
fetcher.WithMaxRetries(Config.MaxRetries),
fetcher.WithMetaData(metadata),
}
if Config.ForceRetry {
fetcherOpts = append(fetcherOpts, fetcher.WithForceRetry())
Expand All @@ -87,22 +94,26 @@ func runCheckConstructionCmd(_ *cobra.Command, _ []string) error {
_, _, fetchErr := fetcher.InitializeAsserter(ctx, Config.Network, Config.ValidationFile)
if fetchErr != nil {
cancel()
err := fmt.Errorf("unable to initialize asserter for fetcher: %w%s", fetchErr.Err, constructionMetadata)
color.Red(err.Error())
return results.ExitConstruction(
Config,
nil,
nil,
fmt.Errorf("unable to initialize asserter for fetcher: %w", fetchErr.Err),
err,
)
}

_, err := utils.CheckNetworkSupported(ctx, Config.Network, fetcher)
if err != nil {
cancel()
err = fmt.Errorf("unable to confirm network %s is supported: %w%s", types.PrintStruct(Config.Network), err, constructionMetadata)
color.Red(err.Error())
return results.ExitConstruction(
Config,
nil,
nil,
fmt.Errorf("unable to confirm network %s is supported: %w", types.PrintStruct(Config.Network), err),
err,
)
}

Expand All @@ -111,11 +122,13 @@ func runCheckConstructionCmd(_ *cobra.Command, _ []string) error {
ctx, fetcher, Config.Network, asserterConfigurationFile,
); err != nil {
cancel()
err = fmt.Errorf("network options don't match asserter configuration file %s: %w%s", asserterConfigurationFile, err, constructionMetadata)
color.Red(err.Error())
return results.ExitConstruction(
Config,
nil,
nil,
fmt.Errorf("network options don't match asserter configuration file %s: %w", asserterConfigurationFile, err),
err,
)
}
}
Expand All @@ -129,27 +142,30 @@ func runCheckConstructionCmd(_ *cobra.Command, _ []string) error {
&SignalReceived,
)
if err != nil {
err = fmt.Errorf("unable to initialize construction tester: %w%s", err, constructionMetadata)
color.Red(err.Error())
return results.ExitConstruction(
Config,
nil,
nil,
fmt.Errorf("unable to initialize construction tester: %w", err),
err,
)
}
defer constructionTester.CloseDatabase(ctx)

if err := constructionTester.PerformBroadcasts(ctx); err != nil {
err = fmt.Errorf("unable to perform broadcasts: %w%s", err, constructionMetadata)
color.Red(err.Error())
return results.ExitConstruction(
Config,
nil,
nil,
fmt.Errorf("unable to perform broadcasts: %w", err),
err,
)
}

g, ctx := errgroup.WithContext(ctx)
ctx = logger.AddRequestUUIDToContext(ctx, Config.RequestUUID)
ctx = logger.AddInfoMetaDataToContext(ctx, Config.InfoMetaData)
ctx = logger.AddMetadataMapToContext(ctx, metadataMap)

g.Go(func() error {
return constructionTester.StartPeriodicLogger(ctx)
Expand Down
26 changes: 20 additions & 6 deletions cmd/check_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/coinbase/rosetta-sdk-go/fetcher"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/coinbase/rosetta-sdk-go/utils"
"github.com/fatih/color"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)
Expand Down Expand Up @@ -69,17 +70,23 @@ bootstrap balance config. You can look at the examples folder for an example
of what one of these files looks like.`,
RunE: runCheckDataCmd,
}
metadata string
)

func runCheckDataCmd(_ *cobra.Command, _ []string) error {
ensureDataDirectoryExists()
ctx, cancel := context.WithCancel(Context)

metadataMap := logger.ConvertStringToMap(Config.InfoMetaData)
metadataMap = logger.AddRequestUUIDToMap(metadataMap, Config.RequestUUID)
metadata = logger.ConvertMapToString(metadataMap)

fetcherOpts := []fetcher.Option{
fetcher.WithMaxConnections(Config.MaxOnlineConnections),
fetcher.WithRetryElapsedTime(time.Duration(Config.RetryElapsedTime) * time.Second),
fetcher.WithTimeout(time.Duration(Config.HTTPTimeout) * time.Second),
fetcher.WithMaxRetries(Config.MaxRetries),
fetcher.WithMetaData(metadata),
}
if Config.ForceRetry {
fetcherOpts = append(fetcherOpts, fetcher.WithForceRetry())
Expand All @@ -93,11 +100,13 @@ func runCheckDataCmd(_ *cobra.Command, _ []string) error {
_, _, fetchErr := fetcher.InitializeAsserter(ctx, Config.Network, Config.ValidationFile)
if fetchErr != nil {
cancel()
err := fmt.Errorf("unable to initialize asserter for fetcher: %w%s", fetchErr.Err, metadata)
color.Red(err.Error())
return results.ExitData(
Config,
nil,
nil,
fmt.Errorf("unable to initialize asserter for fetcher: %w", fetchErr.Err),
err,
"",
"",
)
Expand All @@ -106,11 +115,13 @@ func runCheckDataCmd(_ *cobra.Command, _ []string) error {
networkStatus, err := utils.CheckNetworkSupported(ctx, Config.Network, fetcher)
if err != nil {
cancel()
err = fmt.Errorf("unable to confirm network %s is supported: %w%s", types.PrintStruct(Config.Network), err, metadata)
color.Red(err.Error())
return results.ExitData(
Config,
nil,
nil,
fmt.Errorf("unable to confirm network %s is supported: %w", types.PrintStruct(Config.Network), err),
err,
"",
"",
)
Expand All @@ -121,11 +132,13 @@ func runCheckDataCmd(_ *cobra.Command, _ []string) error {
ctx, fetcher, Config.Network, asserterConfigurationFile,
); err != nil {
cancel()
err = fmt.Errorf("network options don't match asserter configuration file %s: %w%s", asserterConfigurationFile, err, metadata)
color.Red(err.Error())
return results.ExitData(
Config,
nil,
nil,
fmt.Errorf("network options don't match asserter configuration file %s: %w", asserterConfigurationFile, err),
err,
"",
"",
)
Expand All @@ -143,20 +156,21 @@ func runCheckDataCmd(_ *cobra.Command, _ []string) error {
&SignalReceived,
)
if err != nil {
err = fmt.Errorf("unable to initialize data tester: %w%s", err, metadata)
color.Red(err.Error())
return results.ExitData(
Config,
nil,
nil,
fmt.Errorf("unable to initialize data tester: %w", err),
err,
"",
"",
)
}
defer dataTester.CloseDatabase(ctx)

g, ctx := errgroup.WithContext(ctx)
ctx = logger.AddRequestUUIDToContext(ctx, Config.RequestUUID)
ctx = logger.AddInfoMetaDataToContext(ctx, Config.InfoMetaData)
ctx = logger.AddMetadataMapToContext(ctx, metadataMap)

g.Go(func() error {
return dataTester.StartPeriodicLogger(ctx)
Expand Down
10 changes: 8 additions & 2 deletions configuration/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,14 @@ type Configuration struct {
// then this value must be true.
CoinSupported bool `json:"coin_supported"`

// InfoMetaData is a map of key:value
// which aims to show in the log for search
// InfoMetaData is a string, rosetta-cli will convert it into a map[string]string
// key-value are separated by ":"
// different key-value pairs are separated by ","
// an example: if users want to record "instance_name" as "1234", and "blockchain_name" as "Bitcoin",
// this field would be "instance_name:1234,blockchain_name:Bitcoin"
// if adding spaces before and after ":" and ",", it will be trimmed when building map
// " instance_name : xxxx , blockchain_name : xxxx " will be recorded same as
// "instance_name:xxxx,blockchain_name:xxxx"
InfoMetaData string `json:"info_metadata,omitempty"`

Construction *ConstructionConfiguration `json:"construction"`
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module github.com/coinbase/rosetta-cli
go 1.16

require (
github.com/coinbase/rosetta-sdk-go v0.8.1
github.com/coinbase/rosetta-sdk-go v0.8.3-0.20230113233127-4c691644d82a
github.com/coinbase/rosetta-sdk-go/types v1.0.0
github.com/fatih/color v1.13.0
github.com/google/go-cmp v0.5.6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/cloudflare/cloudflare-go v0.14.0/go.mod h1:EnwdgGMaFOruiPZRFSgn+TsQ3hQ7C/YWzIGLeu5c304=
github.com/coinbase/kryptology v1.8.0 h1:Aoq4gdTsJhSU3lNWsD5BWmFSz2pE0GlmrljaOxepdYY=
github.com/coinbase/kryptology v1.8.0/go.mod h1:RYXOAPdzOGUe3qlSFkMGn58i3xUA8hmxYHksuq+8ciI=
github.com/coinbase/rosetta-sdk-go v0.8.1 h1:WE+Temc8iz7Ra7sCpV9ymBJx78vItqFJ2xcSiPet1Pc=
github.com/coinbase/rosetta-sdk-go v0.8.1/go.mod h1:tXPR6AIW9ogsH4tYIaFOKOgfJNanCvcyl7JKLd4DToc=
github.com/coinbase/rosetta-sdk-go v0.8.3-0.20230113233127-4c691644d82a h1:Bym3+QUv9zIij91/4yX3hs5E2TsDOJ6+8egOeF8KBYI=
github.com/coinbase/rosetta-sdk-go v0.8.3-0.20230113233127-4c691644d82a/go.mod h1:ChOHc+BNq7zqJDDkui0DA124GOvlAiRbdgAc1U9GMDQ=
github.com/coinbase/rosetta-sdk-go/types v1.0.0 h1:jpVIwLcPoOeCR6o1tU+Xv7r5bMONNbHU7MuEHboiFuA=
github.com/coinbase/rosetta-sdk-go/types v1.0.0/go.mod h1:eq7W2TMRH22GTW0N0beDnN931DW0/WOI1R2sdHNHG4c=
github.com/consensys/bavard v0.1.8-0.20210406032232-f3452dc9b572/go.mod h1:Bpd0/3mZuaj6Sj+PqrmIquiOKy397AKGThQPaGzNXAQ=
github.com/consensys/bavard v0.1.8-0.20210915155054-088da2f7f54a/go.mod h1:9ItSMtA/dXMAiL7BG6bqW2m3NdSEObYWoH223nGHukI=
github.com/consensys/gnark-crypto v0.4.1-0.20210426202927-39ac3d4b3f1f/go.mod h1:815PAHg3wvysy0SyIqanF8gZ0Y1wjk/hrDHD/iT88+Q=
Expand Down Expand Up @@ -328,8 +330,8 @@ github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643/go.mod h1:43+3pMjjK
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/pointerstructure v1.2.0/go.mod h1:BRAsLI5zgXmw97Lf6s25bs8ohIXc3tViBH44KcwB2g4=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
Expand Down
Loading

0 comments on commit f476c00

Please sign in to comment.