Skip to content

Commit f476c00

Browse files
authored
add customized info/metadata (#382)
1 parent 193721d commit f476c00

File tree

8 files changed

+396
-204
lines changed

8 files changed

+396
-204
lines changed

cmd/check_construction.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
cliErrs "github.com/coinbase/rosetta-cli/pkg/errors"
2323
"github.com/coinbase/rosetta-cli/pkg/logger"
24+
"github.com/fatih/color"
2425

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

5961
func runCheckConstructionCmd(_ *cobra.Command, _ []string) error {
@@ -66,6 +68,10 @@ func runCheckConstructionCmd(_ *cobra.Command, _ []string) error {
6668
)
6769
}
6870

71+
metadataMap := logger.ConvertStringToMap(Config.InfoMetaData)
72+
metadataMap = logger.AddRequestUUIDToMap(metadataMap, Config.RequestUUID)
73+
constructionMetadata = logger.ConvertMapToString(metadataMap)
74+
6975
ensureDataDirectoryExists()
7076
ctx, cancel := context.WithCancel(Context)
7177

@@ -74,6 +80,7 @@ func runCheckConstructionCmd(_ *cobra.Command, _ []string) error {
7480
fetcher.WithRetryElapsedTime(time.Duration(Config.RetryElapsedTime) * time.Second),
7581
fetcher.WithTimeout(time.Duration(Config.HTTPTimeout) * time.Second),
7682
fetcher.WithMaxRetries(Config.MaxRetries),
83+
fetcher.WithMetaData(metadata),
7784
}
7885
if Config.ForceRetry {
7986
fetcherOpts = append(fetcherOpts, fetcher.WithForceRetry())
@@ -87,22 +94,26 @@ func runCheckConstructionCmd(_ *cobra.Command, _ []string) error {
8794
_, _, fetchErr := fetcher.InitializeAsserter(ctx, Config.Network, Config.ValidationFile)
8895
if fetchErr != nil {
8996
cancel()
97+
err := fmt.Errorf("unable to initialize asserter for fetcher: %w%s", fetchErr.Err, constructionMetadata)
98+
color.Red(err.Error())
9099
return results.ExitConstruction(
91100
Config,
92101
nil,
93102
nil,
94-
fmt.Errorf("unable to initialize asserter for fetcher: %w", fetchErr.Err),
103+
err,
95104
)
96105
}
97106

98107
_, err := utils.CheckNetworkSupported(ctx, Config.Network, fetcher)
99108
if err != nil {
100109
cancel()
110+
err = fmt.Errorf("unable to confirm network %s is supported: %w%s", types.PrintStruct(Config.Network), err, constructionMetadata)
111+
color.Red(err.Error())
101112
return results.ExitConstruction(
102113
Config,
103114
nil,
104115
nil,
105-
fmt.Errorf("unable to confirm network %s is supported: %w", types.PrintStruct(Config.Network), err),
116+
err,
106117
)
107118
}
108119

@@ -111,11 +122,13 @@ func runCheckConstructionCmd(_ *cobra.Command, _ []string) error {
111122
ctx, fetcher, Config.Network, asserterConfigurationFile,
112123
); err != nil {
113124
cancel()
125+
err = fmt.Errorf("network options don't match asserter configuration file %s: %w%s", asserterConfigurationFile, err, constructionMetadata)
126+
color.Red(err.Error())
114127
return results.ExitConstruction(
115128
Config,
116129
nil,
117130
nil,
118-
fmt.Errorf("network options don't match asserter configuration file %s: %w", asserterConfigurationFile, err),
131+
err,
119132
)
120133
}
121134
}
@@ -129,27 +142,30 @@ func runCheckConstructionCmd(_ *cobra.Command, _ []string) error {
129142
&SignalReceived,
130143
)
131144
if err != nil {
145+
err = fmt.Errorf("unable to initialize construction tester: %w%s", err, constructionMetadata)
146+
color.Red(err.Error())
132147
return results.ExitConstruction(
133148
Config,
134149
nil,
135150
nil,
136-
fmt.Errorf("unable to initialize construction tester: %w", err),
151+
err,
137152
)
138153
}
139154
defer constructionTester.CloseDatabase(ctx)
140155

141156
if err := constructionTester.PerformBroadcasts(ctx); err != nil {
157+
err = fmt.Errorf("unable to perform broadcasts: %w%s", err, constructionMetadata)
158+
color.Red(err.Error())
142159
return results.ExitConstruction(
143160
Config,
144161
nil,
145162
nil,
146-
fmt.Errorf("unable to perform broadcasts: %w", err),
163+
err,
147164
)
148165
}
149166

150167
g, ctx := errgroup.WithContext(ctx)
151-
ctx = logger.AddRequestUUIDToContext(ctx, Config.RequestUUID)
152-
ctx = logger.AddInfoMetaDataToContext(ctx, Config.InfoMetaData)
168+
ctx = logger.AddMetadataMapToContext(ctx, metadataMap)
153169

154170
g.Go(func() error {
155171
return constructionTester.StartPeriodicLogger(ctx)

cmd/check_data.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/coinbase/rosetta-sdk-go/fetcher"
2626
"github.com/coinbase/rosetta-sdk-go/types"
2727
"github.com/coinbase/rosetta-sdk-go/utils"
28+
"github.com/fatih/color"
2829
"github.com/spf13/cobra"
2930
"golang.org/x/sync/errgroup"
3031
)
@@ -69,17 +70,23 @@ bootstrap balance config. You can look at the examples folder for an example
6970
of what one of these files looks like.`,
7071
RunE: runCheckDataCmd,
7172
}
73+
metadata string
7274
)
7375

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

80+
metadataMap := logger.ConvertStringToMap(Config.InfoMetaData)
81+
metadataMap = logger.AddRequestUUIDToMap(metadataMap, Config.RequestUUID)
82+
metadata = logger.ConvertMapToString(metadataMap)
83+
7884
fetcherOpts := []fetcher.Option{
7985
fetcher.WithMaxConnections(Config.MaxOnlineConnections),
8086
fetcher.WithRetryElapsedTime(time.Duration(Config.RetryElapsedTime) * time.Second),
8187
fetcher.WithTimeout(time.Duration(Config.HTTPTimeout) * time.Second),
8288
fetcher.WithMaxRetries(Config.MaxRetries),
89+
fetcher.WithMetaData(metadata),
8390
}
8491
if Config.ForceRetry {
8592
fetcherOpts = append(fetcherOpts, fetcher.WithForceRetry())
@@ -93,11 +100,13 @@ func runCheckDataCmd(_ *cobra.Command, _ []string) error {
93100
_, _, fetchErr := fetcher.InitializeAsserter(ctx, Config.Network, Config.ValidationFile)
94101
if fetchErr != nil {
95102
cancel()
103+
err := fmt.Errorf("unable to initialize asserter for fetcher: %w%s", fetchErr.Err, metadata)
104+
color.Red(err.Error())
96105
return results.ExitData(
97106
Config,
98107
nil,
99108
nil,
100-
fmt.Errorf("unable to initialize asserter for fetcher: %w", fetchErr.Err),
109+
err,
101110
"",
102111
"",
103112
)
@@ -106,11 +115,13 @@ func runCheckDataCmd(_ *cobra.Command, _ []string) error {
106115
networkStatus, err := utils.CheckNetworkSupported(ctx, Config.Network, fetcher)
107116
if err != nil {
108117
cancel()
118+
err = fmt.Errorf("unable to confirm network %s is supported: %w%s", types.PrintStruct(Config.Network), err, metadata)
119+
color.Red(err.Error())
109120
return results.ExitData(
110121
Config,
111122
nil,
112123
nil,
113-
fmt.Errorf("unable to confirm network %s is supported: %w", types.PrintStruct(Config.Network), err),
124+
err,
114125
"",
115126
"",
116127
)
@@ -121,11 +132,13 @@ func runCheckDataCmd(_ *cobra.Command, _ []string) error {
121132
ctx, fetcher, Config.Network, asserterConfigurationFile,
122133
); err != nil {
123134
cancel()
135+
err = fmt.Errorf("network options don't match asserter configuration file %s: %w%s", asserterConfigurationFile, err, metadata)
136+
color.Red(err.Error())
124137
return results.ExitData(
125138
Config,
126139
nil,
127140
nil,
128-
fmt.Errorf("network options don't match asserter configuration file %s: %w", asserterConfigurationFile, err),
141+
err,
129142
"",
130143
"",
131144
)
@@ -143,20 +156,21 @@ func runCheckDataCmd(_ *cobra.Command, _ []string) error {
143156
&SignalReceived,
144157
)
145158
if err != nil {
159+
err = fmt.Errorf("unable to initialize data tester: %w%s", err, metadata)
160+
color.Red(err.Error())
146161
return results.ExitData(
147162
Config,
148163
nil,
149164
nil,
150-
fmt.Errorf("unable to initialize data tester: %w", err),
165+
err,
151166
"",
152167
"",
153168
)
154169
}
155170
defer dataTester.CloseDatabase(ctx)
156171

157172
g, ctx := errgroup.WithContext(ctx)
158-
ctx = logger.AddRequestUUIDToContext(ctx, Config.RequestUUID)
159-
ctx = logger.AddInfoMetaDataToContext(ctx, Config.InfoMetaData)
173+
ctx = logger.AddMetadataMapToContext(ctx, metadataMap)
160174

161175
g.Go(func() error {
162176
return dataTester.StartPeriodicLogger(ctx)

configuration/types.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,14 @@ type Configuration struct {
461461
// then this value must be true.
462462
CoinSupported bool `json:"coin_supported"`
463463

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

468474
Construction *ConstructionConfiguration `json:"construction"`

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module github.com/coinbase/rosetta-cli
33
go 1.16
44

55
require (
6-
github.com/coinbase/rosetta-sdk-go v0.8.1
6+
github.com/coinbase/rosetta-sdk-go v0.8.3-0.20230113233127-4c691644d82a
7+
github.com/coinbase/rosetta-sdk-go/types v1.0.0
78
github.com/fatih/color v1.13.0
89
github.com/google/go-cmp v0.5.6 // indirect
910
github.com/mattn/go-colorable v0.1.12 // indirect

go.sum

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
9898
github.com/cloudflare/cloudflare-go v0.14.0/go.mod h1:EnwdgGMaFOruiPZRFSgn+TsQ3hQ7C/YWzIGLeu5c304=
9999
github.com/coinbase/kryptology v1.8.0 h1:Aoq4gdTsJhSU3lNWsD5BWmFSz2pE0GlmrljaOxepdYY=
100100
github.com/coinbase/kryptology v1.8.0/go.mod h1:RYXOAPdzOGUe3qlSFkMGn58i3xUA8hmxYHksuq+8ciI=
101-
github.com/coinbase/rosetta-sdk-go v0.8.1 h1:WE+Temc8iz7Ra7sCpV9ymBJx78vItqFJ2xcSiPet1Pc=
102-
github.com/coinbase/rosetta-sdk-go v0.8.1/go.mod h1:tXPR6AIW9ogsH4tYIaFOKOgfJNanCvcyl7JKLd4DToc=
101+
github.com/coinbase/rosetta-sdk-go v0.8.3-0.20230113233127-4c691644d82a h1:Bym3+QUv9zIij91/4yX3hs5E2TsDOJ6+8egOeF8KBYI=
102+
github.com/coinbase/rosetta-sdk-go v0.8.3-0.20230113233127-4c691644d82a/go.mod h1:ChOHc+BNq7zqJDDkui0DA124GOvlAiRbdgAc1U9GMDQ=
103+
github.com/coinbase/rosetta-sdk-go/types v1.0.0 h1:jpVIwLcPoOeCR6o1tU+Xv7r5bMONNbHU7MuEHboiFuA=
104+
github.com/coinbase/rosetta-sdk-go/types v1.0.0/go.mod h1:eq7W2TMRH22GTW0N0beDnN931DW0/WOI1R2sdHNHG4c=
103105
github.com/consensys/bavard v0.1.8-0.20210406032232-f3452dc9b572/go.mod h1:Bpd0/3mZuaj6Sj+PqrmIquiOKy397AKGThQPaGzNXAQ=
104106
github.com/consensys/bavard v0.1.8-0.20210915155054-088da2f7f54a/go.mod h1:9ItSMtA/dXMAiL7BG6bqW2m3NdSEObYWoH223nGHukI=
105107
github.com/consensys/gnark-crypto v0.4.1-0.20210426202927-39ac3d4b3f1f/go.mod h1:815PAHg3wvysy0SyIqanF8gZ0Y1wjk/hrDHD/iT88+Q=
@@ -328,8 +330,8 @@ github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643/go.mod h1:43+3pMjjK
328330
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
329331
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
330332
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
331-
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
332-
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
333+
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
334+
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
333335
github.com/mitchellh/pointerstructure v1.2.0/go.mod h1:BRAsLI5zgXmw97Lf6s25bs8ohIXc3tViBH44KcwB2g4=
334336
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
335337
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=

0 commit comments

Comments
 (0)