From 7c5d1993e9e36a02683ad78d6b7688355c18629b Mon Sep 17 00:00:00 2001 From: Patrick O'Grady Date: Wed, 6 May 2020 16:12:09 -0700 Subject: [PATCH] nits --- cmd/check.go | 7 +- cmd/create_configuration.go | 69 +++++++++++++++++++ cmd/create_spec.go | 51 -------------- cmd/root.go | 2 +- cmd/view_account.go | 14 ++++ cmd/view_block.go | 14 ++++ internal/processor/reconciler_handler.go | 14 ++++ internal/processor/reconciler_helper.go | 14 ++++ internal/processor/storage_helper.go | 14 ++++ .../{sync_handler.go => syncer_handler.go} | 14 ++++ internal/reconciler/reconciler.go | 30 +++++--- internal/storage/block_storage.go | 12 ++-- internal/storage/block_storage_test.go | 21 +++++- internal/syncer/syncer.go | 6 +- internal/syncer/syncer_test.go | 14 ++++ 15 files changed, 224 insertions(+), 72 deletions(-) create mode 100644 cmd/create_configuration.go delete mode 100644 cmd/create_spec.go rename internal/processor/{sync_handler.go => syncer_handler.go} (78%) diff --git a/cmd/check.go b/cmd/check.go index d01f7454..2e2413e4 100644 --- a/cmd/check.go +++ b/cmd/check.go @@ -141,7 +141,12 @@ func loadAccounts(filePath string) ([]*reconciler.AccountCurrency, error) { return nil, err } - log.Printf("Found %d accounts at %s: %s\n", len(accounts), filePath, types.PrettyPrintStruct(accounts)) + log.Printf( + "Found %d accounts at %s: %s\n", + len(accounts), + filePath, + types.PrettyPrintStruct(accounts), + ) return accounts, nil } diff --git a/cmd/create_configuration.go b/cmd/create_configuration.go new file mode 100644 index 00000000..9e5cbdde --- /dev/null +++ b/cmd/create_configuration.go @@ -0,0 +1,69 @@ +// Copyright 2020 Coinbase, Inc. +// +// 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 cmd + +import ( + "context" + "fmt" + "io/ioutil" + "log" + "os" + "path" + + "github.com/coinbase/rosetta-sdk-go/fetcher" + "github.com/coinbase/rosetta-sdk-go/types" + "github.com/spf13/cobra" +) + +const ( + fileMode = 0600 +) + +var ( + createConfigurationCmd = &cobra.Command{ + Use: "create:configuration", + Short: "", + Long: ``, + Run: runCreateConfigurationCmd, + Args: cobra.ExactArgs(1), + } +) + +func runCreateConfigurationCmd(cmd *cobra.Command, args []string) { + ctx := context.Background() + + // Create a new fetcher + newFetcher := fetcher.New( + ServerURL, + ) + + // Initialize the fetcher's asserter + _, _, err := newFetcher.InitializeAsserter(ctx) + if err != nil { + log.Fatal(err) + } + + configuration, err := newFetcher.Asserter.ClientConfiguration() + if err != nil { + log.Fatal(fmt.Errorf("%w: unable to generate spec", err)) + } + + specString := types.PrettyPrintStruct(configuration) + log.Printf("Spec File: %s\n", specString) + + if err := ioutil.WriteFile(path.Clean(args[0]), []byte(specString), os.FileMode(fileMode)); err != nil { + log.Fatal(err) + } +} diff --git a/cmd/create_spec.go b/cmd/create_spec.go deleted file mode 100644 index a9ab7145..00000000 --- a/cmd/create_spec.go +++ /dev/null @@ -1,51 +0,0 @@ -package cmd - -import ( - "context" - "fmt" - "io/ioutil" - "log" - "os" - "path" - - "github.com/coinbase/rosetta-sdk-go/fetcher" - "github.com/coinbase/rosetta-sdk-go/types" - "github.com/spf13/cobra" -) - -var ( - createSpecCmd = &cobra.Command{ - Use: "create:spec", - Short: "", - Long: ``, - Run: runCreateSpecCmd, - Args: cobra.ExactArgs(1), - } -) - -func runCreateSpecCmd(cmd *cobra.Command, args []string) { - ctx := context.Background() - - // Create a new fetcher - newFetcher := fetcher.New( - ServerURL, - ) - - // Initialize the fetcher's asserter - _, _, err := newFetcher.InitializeAsserter(ctx) - if err != nil { - log.Fatal(err) - } - - configuration, err := newFetcher.Asserter.ClientConfiguration() - if err != nil { - log.Fatal(fmt.Errorf("%w: unable to generate spec", err)) - } - - specString := types.PrettyPrintStruct(configuration) - log.Printf("Spec File: %s\n", specString) - - if err := ioutil.WriteFile(path.Clean(args[0]), []byte(specString), os.FileMode(0600)); err != nil { - log.Fatal(err) - } -} diff --git a/cmd/root.go b/cmd/root.go index cde0910c..b7c88363 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -46,5 +46,5 @@ func init() { rootCmd.AddCommand(checkCmd) rootCmd.AddCommand(viewBlockCmd) rootCmd.AddCommand(viewAccountCmd) - rootCmd.AddCommand(createSpecCmd) + rootCmd.AddCommand(createConfigurationCmd) } diff --git a/cmd/view_account.go b/cmd/view_account.go index af4a19df..cb01da3e 100644 --- a/cmd/view_account.go +++ b/cmd/view_account.go @@ -1,3 +1,17 @@ +// Copyright 2020 Coinbase, Inc. +// +// 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 cmd import ( diff --git a/cmd/view_block.go b/cmd/view_block.go index c0a2030e..12633e8b 100644 --- a/cmd/view_block.go +++ b/cmd/view_block.go @@ -1,3 +1,17 @@ +// Copyright 2020 Coinbase, Inc. +// +// 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 cmd import ( diff --git a/internal/processor/reconciler_handler.go b/internal/processor/reconciler_handler.go index 488f41fe..a2a7f890 100644 --- a/internal/processor/reconciler_handler.go +++ b/internal/processor/reconciler_handler.go @@ -1,3 +1,17 @@ +// Copyright 2020 Coinbase, Inc. +// +// 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 processor import ( diff --git a/internal/processor/reconciler_helper.go b/internal/processor/reconciler_helper.go index 09d84575..356c6612 100644 --- a/internal/processor/reconciler_helper.go +++ b/internal/processor/reconciler_helper.go @@ -1,3 +1,17 @@ +// Copyright 2020 Coinbase, Inc. +// +// 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 processor import ( diff --git a/internal/processor/storage_helper.go b/internal/processor/storage_helper.go index 8abbfa58..1a3bb666 100644 --- a/internal/processor/storage_helper.go +++ b/internal/processor/storage_helper.go @@ -1,3 +1,17 @@ +// Copyright 2020 Coinbase, Inc. +// +// 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 processor import ( diff --git a/internal/processor/sync_handler.go b/internal/processor/syncer_handler.go similarity index 78% rename from internal/processor/sync_handler.go rename to internal/processor/syncer_handler.go index c8e73099..f836a66a 100644 --- a/internal/processor/sync_handler.go +++ b/internal/processor/syncer_handler.go @@ -1,3 +1,17 @@ +// Copyright 2020 Coinbase, Inc. +// +// 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 processor import ( diff --git a/internal/reconciler/reconciler.go b/internal/reconciler/reconciler.go index 6b0c867c..cb20b8c8 100644 --- a/internal/reconciler/reconciler.go +++ b/internal/reconciler/reconciler.go @@ -100,7 +100,7 @@ type BalanceChange struct { Difference string `json:"difference,omitempty"` } -type ReconcilerHelper interface { +type Helper interface { BlockExists( ctx context.Context, block *types.BlockIdentifier, @@ -118,7 +118,7 @@ type ReconcilerHelper interface { ) (*types.Amount, *types.BlockIdentifier, error) } -type ReconcilerHandler interface { +type Handler interface { ReconciliationFailed( ctx context.Context, reconciliationType string, @@ -144,8 +144,8 @@ type ReconcilerHandler interface { // by a Rosetta Server. type Reconciler struct { network *types.NetworkIdentifier - helper ReconcilerHelper - handler ReconcilerHandler + helper Helper + handler Handler fetcher *fetcher.Fetcher accountConcurrency uint64 lookupBalanceByBlock bool @@ -169,8 +169,8 @@ type Reconciler struct { // NewReconciler creates a new Reconciler. func NewReconciler( network *types.NetworkIdentifier, - helper ReconcilerHelper, - handler ReconcilerHandler, + helper Helper, + handler Handler, fetcher *fetcher.Fetcher, accountConcurrency uint64, lookupBalanceByBlock bool, @@ -277,7 +277,10 @@ func (r *Reconciler) CompareBalance( // Head block should be set before we CompareBalance head, err := r.helper.CurrentBlock(ctx) if err != nil { - return zeroString, "", 0, fmt.Errorf("%w: unable to get current block for reconciliation", err) + return zeroString, "", 0, fmt.Errorf( + "%w: unable to get current block for reconciliation", + err, + ) } // Check if live block is < head (or wait) @@ -293,7 +296,11 @@ func (r *Reconciler) CompareBalance( // Check if live block is in store (ensure not reorged) exists, err := r.helper.BlockExists(ctx, liveBlock) if err != nil { - return zeroString, "", 0, fmt.Errorf("%w: unable to check if block exists: %+v", err, liveBlock) + return zeroString, "", 0, fmt.Errorf( + "%w: unable to check if block exists: %+v", + err, + liveBlock, + ) } if !exists { return zeroString, "", head.Index, fmt.Errorf( @@ -311,7 +318,12 @@ func (r *Reconciler) CompareBalance( head, ) if err != nil { - return zeroString, "", head.Index, fmt.Errorf("%w: unable to get cached balance for %+v:%+v", err, account, currency) + return zeroString, "", head.Index, fmt.Errorf( + "%w: unable to get cached balance for %+v:%+v", + err, + account, + currency, + ) } if liveBlock.Index < balanceBlock.Index { diff --git a/internal/storage/block_storage.go b/internal/storage/block_storage.go index a4268e4b..ded27a48 100644 --- a/internal/storage/block_storage.go +++ b/internal/storage/block_storage.go @@ -124,7 +124,7 @@ func GetBalanceKey(account *types.AccountIdentifier, currency *types.Currency) [ ) } -type BlockStorageHelper interface { +type Helper interface { AccountBalance( ctx context.Context, account *types.AccountIdentifier, @@ -142,14 +142,14 @@ type BlockStorageHelper interface { // on top of a Database and DatabaseTransaction interface. type BlockStorage struct { db Database - helper BlockStorageHelper + helper Helper } // NewBlockStorage returns a new BlockStorage. func NewBlockStorage( ctx context.Context, db Database, - helper BlockStorageHelper, + helper Helper, ) *BlockStorage { return &BlockStorage{ db: db, @@ -420,7 +420,11 @@ func (b *BlockStorage) SetNewStartIndex( } if head.Index < startIndex { - return fmt.Errorf("last processed block %d is less than start index %d", head.Index, startIndex) + return fmt.Errorf( + "last processed block %d is less than start index %d", + head.Index, + startIndex, + ) } currBlock := head diff --git a/internal/storage/block_storage_test.go b/internal/storage/block_storage_test.go index fc9f6257..6b865f58 100644 --- a/internal/storage/block_storage_test.go +++ b/internal/storage/block_storage_test.go @@ -526,7 +526,12 @@ func TestBalance(t *testing.T) { assert.NoError(t, err) assert.NoError(t, txn.Commit(ctx)) - retrievedAmount, block, err := storage.GetBalance(ctx, subAccountNewPointer, amount.Currency, newBlock) + retrievedAmount, block, err := storage.GetBalance( + ctx, + subAccountNewPointer, + amount.Currency, + newBlock, + ) assert.NoError(t, err) assert.Equal(t, amount, retrievedAmount) assert.Equal(t, newBlock, block) @@ -548,7 +553,12 @@ func TestBalance(t *testing.T) { assert.NoError(t, err) assert.NoError(t, txn.Commit(ctx)) - retrievedAmount, block, err := storage.GetBalance(ctx, subAccountMetadataNewPointer, amount.Currency, newBlock) + retrievedAmount, block, err := storage.GetBalance( + ctx, + subAccountMetadataNewPointer, + amount.Currency, + newBlock, + ) assert.NoError(t, err) assert.Equal(t, amount, retrievedAmount) assert.Equal(t, newBlock, block) @@ -570,7 +580,12 @@ func TestBalance(t *testing.T) { assert.NoError(t, err) assert.NoError(t, txn.Commit(ctx)) - retrievedAmount, block, err := storage.GetBalance(ctx, subAccountMetadata2NewPointer, amount.Currency, newBlock) + retrievedAmount, block, err := storage.GetBalance( + ctx, + subAccountMetadata2NewPointer, + amount.Currency, + newBlock, + ) assert.NoError(t, err) assert.Equal(t, amount, retrievedAmount) assert.Equal(t, newBlock, block) diff --git a/internal/syncer/syncer.go b/internal/syncer/syncer.go index 182f38db..a0125a31 100644 --- a/internal/syncer/syncer.go +++ b/internal/syncer/syncer.go @@ -37,7 +37,7 @@ const ( // SyncHandler is called at various times during the sync cycle // to handle different events. It is common to write logs or // perform reconciliation in the sync processor. -type SyncHandler interface { +type Handler interface { BlockAdded( ctx context.Context, block *types.Block, @@ -52,7 +52,7 @@ type SyncHandler interface { type Syncer struct { network *types.NetworkIdentifier fetcher *fetcher.Fetcher - handler SyncHandler + handler Handler cancel context.CancelFunc // Used to keep track of sync state @@ -72,7 +72,7 @@ type Syncer struct { func New( network *types.NetworkIdentifier, fetcher *fetcher.Fetcher, - handler SyncHandler, + handler Handler, cancel context.CancelFunc, pastBlocks []*types.BlockIdentifier, ) *Syncer { diff --git a/internal/syncer/syncer_test.go b/internal/syncer/syncer_test.go index bbbb780e..c7d8fcef 100644 --- a/internal/syncer/syncer_test.go +++ b/internal/syncer/syncer_test.go @@ -1,3 +1,17 @@ +// Copyright 2020 Coinbase, Inc. +// +// 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 syncer import (