-
Notifications
You must be signed in to change notification settings - Fork 94
/
view_block.go
176 lines (152 loc) · 5.03 KB
/
view_block.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// 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 (
"fmt"
"strconv"
"time"
"github.com/coinbase/rosetta-sdk-go/fetcher"
"github.com/coinbase/rosetta-sdk-go/parser"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/coinbase/rosetta-sdk-go/utils"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
var (
viewBlockCmd = &cobra.Command{
Use: "view:block",
Short: "View a block",
Long: `While debugging a Data API implementation, it can be very
useful to inspect block contents. This command allows you to fetch any
block by index to inspect its contents. It uses the
fetcher (https://github.com/coinbase/rosetta-sdk-go/tree/master/fetcher) package
to automatically get all transactions in the block and assert the format
of the block is correct before printing.
If this command errors, it is likely because the block you are trying to
fetch is formatted incorrectly.`,
RunE: runViewBlockCmd,
Args: cobra.ExactArgs(1),
}
)
func printChanges(balanceChanges []*parser.BalanceChange) error {
for _, balanceChange := range balanceChanges {
parsedDiff, err := types.BigInt(balanceChange.Difference)
if err != nil {
return fmt.Errorf("%w: unable to parse Difference", err)
}
if parsedDiff.Sign() == 0 {
continue
}
fmt.Println(
types.PrintStruct(balanceChange.Account),
"->",
utils.PrettyAmount(parsedDiff, balanceChange.Currency),
)
}
return nil
}
func runViewBlockCmd(cmd *cobra.Command, args []string) error {
index, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
return fmt.Errorf("%w: unable to parse index %s", err, args[0])
}
// Create a new fetcher
newFetcher := fetcher.New(
Config.OnlineURL,
fetcher.WithRetryElapsedTime(time.Duration(Config.RetryElapsedTime)*time.Second),
fetcher.WithTimeout(time.Duration(Config.HTTPTimeout)*time.Second),
fetcher.WithMaxRetries(Config.MaxRetries),
)
// Initialize the fetcher's asserter
//
// Behind the scenes this makes a call to get the
// network status and uses the response to inform
// the asserter what are valid responses.
_, _, fetchErr := newFetcher.InitializeAsserter(Context, Config.Network)
if fetchErr != nil {
return fmt.Errorf("%w: unable to initialize asserter", fetchErr.Err)
}
_, err = utils.CheckNetworkSupported(Context, Config.Network, newFetcher)
if err != nil {
return fmt.Errorf("%w: unable to confirm network is supported", err)
}
// Fetch the specified block with retries (automatically
// asserted for correctness)
//
// On another note, notice that fetcher.BlockRetry
// automatically fetches all transactions that are
// returned in BlockResponse.OtherTransactions. If you use
// the client directly, you will need to implement a mechanism
// to fully populate the block by fetching all these
// transactions.
block, fetchErr := newFetcher.BlockRetry(
Context,
Config.Network,
&types.PartialBlockIdentifier{
Index: &index,
},
)
if fetchErr != nil {
return fmt.Errorf("%w: unable to fetch block", fetchErr.Err)
}
fmt.Printf("\n")
if !OnlyChanges {
color.Cyan("Current Block:")
fmt.Println(types.PrettyPrintStruct(block))
}
// Print out all balance changes in a given block. This does NOT exempt
// any operations/accounts from parsing.
color.Cyan("Balance Changes:")
p := parser.New(newFetcher.Asserter, func(*types.Operation) bool { return false }, nil)
balanceChanges, err := p.BalanceChanges(Context, block, false)
if err != nil {
return fmt.Errorf("%w: unable to calculate balance changes", err)
}
fmt.Println("Cummulative:", block.BlockIdentifier.Hash)
if err := printChanges(balanceChanges); err != nil {
return err
}
fmt.Printf("\n")
// Print out balance changes by transaction hash
//
// TODO: modify parser to allow for calculating balance
// changes for a single transaction.
for _, tx := range block.Transactions {
balanceChanges, err := p.BalanceChanges(Context, &types.Block{
Transactions: []*types.Transaction{
tx,
},
}, false)
if err != nil {
return fmt.Errorf("%w: unable to calculate balance changes", err)
}
fmt.Println("Transaction:", tx.TransactionIdentifier.Hash)
if err := printChanges(balanceChanges); err != nil {
return err
}
fmt.Printf("\n")
}
if !OnlyChanges {
// Print out all OperationGroups for each transaction in a block.
color.Cyan("Operation Groups:")
for _, tx := range block.Transactions {
fmt.Printf(
"Transaction %s Operation Groups: %s\n",
tx.TransactionIdentifier.Hash,
types.PrettyPrintStruct(parser.GroupOperations(tx)),
)
}
}
return nil
}