Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 54 additions & 17 deletions internal/blocks/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ func init() {
}

type blockResult struct {
block *flow.Block
events []flow.BlockEvents
collections []*flow.Collection
included []string
block *flow.Block
events []flow.BlockEvents
transactions []*flow.Transaction
results []*flow.TransactionResult
included []string
}

func (r *blockResult) JSON() any {
Expand All @@ -56,23 +57,36 @@ func (r *blockResult) JSON() any {
result["totalSeals"] = len(r.block.Seals)
result["totalCollections"] = len(r.block.CollectionGuarantees)

// Keep collection info for backwards compatibility
collections := make([]any, 0, len(r.block.CollectionGuarantees))
for i, guarantee := range r.block.CollectionGuarantees {
for _, guarantee := range r.block.CollectionGuarantees {
collection := make(map[string]any)
collection["id"] = guarantee.CollectionID.String()
collections = append(collections, collection)
}
result["collection"] = collections

if command.ContainsFlag(r.included, "transactions") {
txs := make([]string, 0)
for _, tx := range r.collections[i].TransactionIDs {
txs = append(txs, tx.String())
// Add transaction details if requested
if command.ContainsFlag(r.included, "transactions") && len(r.transactions) > 0 {
txs := make([]map[string]any, 0, len(r.transactions))
for i, tx := range r.transactions {
txData := make(map[string]any)
txData["id"] = tx.ID().String()
txData["status"] = r.results[i].Status.String()

// System transactions have empty collection ID
if r.results[i].CollectionID == flow.EmptyID {
txData["type"] = "system"
} else {
txData["type"] = "user"
txData["collectionId"] = r.results[i].CollectionID.String()
}
collection["transactions"] = txs
}

collections = append(collections, collection)
txs = append(txs, txData)
}
result["transactions"] = txs
}

result["collection"] = collections
return result
}

Expand All @@ -99,17 +113,40 @@ func (r *blockResult) String() string {
_, _ = fmt.Fprintf(writer, "Status\t%s\n", blockStatusToString(r.block.Status))

_, _ = fmt.Fprintf(writer, "Total Seals\t%v\n", len(r.block.Seals))

_, _ = fmt.Fprintf(writer, "Total Collections\t%v\n", len(r.block.CollectionGuarantees))

// Show collections
for i, guarantee := range r.block.CollectionGuarantees {
_, _ = fmt.Fprintf(writer, " Collection %d:\t%s\n", i, guarantee.CollectionID)
}

if command.ContainsFlag(r.included, "transactions") {
for x, tx := range r.collections[i].TransactionIDs {
_, _ = fmt.Fprintf(writer, " Transaction %d: %s\n", x, tx)
// Show transactions if included
if command.ContainsFlag(r.included, "transactions") && len(r.transactions) > 0 {
_, _ = fmt.Fprintf(writer, "\nTransactions:\n")

userCount := 0
systemCount := 0

for i, tx := range r.transactions {
var txType string
if r.results[i].CollectionID == flow.EmptyID {
txType = "system"
systemCount++
} else {
txType = "user"
userCount++
}

_, _ = fmt.Fprintf(writer, " [%d] %s\t%s (%s)\n",
i,
tx.ID().String(),
r.results[i].Status.String(),
txType,
)
}

_, _ = fmt.Fprintf(writer, "\nTotal: %d transactions (%d user, %d system)\n",
len(r.transactions), userCount, systemCount)
}

if len(r.events) > 0 {
Expand Down
13 changes: 9 additions & 4 deletions internal/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ func Test_GetBlock(t *testing.T) {
assert.Equal(t, uint64(100), args.Get(3).(uint64))
}).Return(nil, nil)

srv.GetCollection.Return(nil, nil)
srv.GetTransactionsByBlockID.Return(
[]*flow.Transaction{tests.NewTransaction()},
[]*flow.TransactionResult{tests.NewTransactionResult(nil)},
nil,
)

returnBlock := tests.NewBlock()
returnBlock.Height = uint64(100)
Expand All @@ -64,8 +68,9 @@ func Test_GetBlock(t *testing.T) {

func Test_Result(t *testing.T) {
result := blockResult{
block: tests.NewBlock(),
collections: []*flow.Collection{tests.NewCollection()},
block: tests.NewBlock(),
transactions: []*flow.Transaction{},
results: []*flow.TransactionResult{},
}

assert.Equal(t, strings.TrimPrefix(`
Expand All @@ -86,7 +91,7 @@ Total Collections 3
t,
map[string]any{
"blockId": "0303030303030303030303030303030303030303030303030303030303030303",
"collection": []any{
"collections": []any{
map[string]any{"id": "0202020202020202020202020202020202020202020202020202020202020202"},
map[string]any{"id": "0404040404040404040404040404040404040404040404040404040404040404"},
map[string]any{"id": "0606060606060606060606060606060606060606060606060606060606060606"},
Expand Down
21 changes: 10 additions & 11 deletions internal/blocks/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,20 @@ func get(
}
}

collections := make([]*flowsdk.Collection, 0)
var transactions []*flowsdk.Transaction
var results []*flowsdk.TransactionResult
if command.ContainsFlag(blockFlags.Include, "transactions") {
for _, guarantee := range block.CollectionGuarantees {
collection, err := flow.GetCollection(context.Background(), guarantee.CollectionID)
if err != nil {
return nil, err
}
collections = append(collections, collection)
transactions, results, err = flow.GetTransactionsByBlockID(context.Background(), block.ID)
if err != nil {
return nil, err
}
}

return &blockResult{
block: block,
events: events,
collections: collections,
included: blockFlags.Include,
block: block,
events: events,
transactions: transactions,
results: results,
included: blockFlags.Include,
}, nil
}
Loading