Skip to content

Commit f2fd5c5

Browse files
committed
Include system transactions in flow blocks get <txid>
1 parent da43284 commit f2fd5c5

File tree

3 files changed

+74
-33
lines changed

3 files changed

+74
-33
lines changed

internal/blocks/blocks.go

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ func init() {
4242
}
4343

4444
type blockResult struct {
45-
block *flow.Block
46-
events []flow.BlockEvents
47-
collections []*flow.Collection
48-
included []string
45+
block *flow.Block
46+
events []flow.BlockEvents
47+
transactions []*flow.Transaction
48+
results []*flow.TransactionResult
49+
included []string
4950
}
5051

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

60+
// Keep collection info for backwards compatibility
5961
collections := make([]any, 0, len(r.block.CollectionGuarantees))
60-
for i, guarantee := range r.block.CollectionGuarantees {
62+
for _, guarantee := range r.block.CollectionGuarantees {
6163
collection := make(map[string]any)
6264
collection["id"] = guarantee.CollectionID.String()
63-
64-
if command.ContainsFlag(r.included, "transactions") {
65-
txs := make([]string, 0)
66-
for _, tx := range r.collections[i].TransactionIDs {
67-
txs = append(txs, tx.String())
65+
collections = append(collections, collection)
66+
}
67+
result["collections"] = collections
68+
69+
// Add transaction details if requested
70+
if command.ContainsFlag(r.included, "transactions") && len(r.transactions) > 0 {
71+
txs := make([]map[string]any, 0, len(r.transactions))
72+
for i, tx := range r.transactions {
73+
txData := make(map[string]any)
74+
txData["id"] = tx.ID().String()
75+
txData["status"] = r.results[i].Status.String()
76+
77+
// System transactions have empty collection ID
78+
if r.results[i].CollectionID == flow.EmptyID {
79+
txData["type"] = "system"
80+
} else {
81+
txData["type"] = "user"
82+
txData["collectionId"] = r.results[i].CollectionID.String()
6883
}
69-
collection["transactions"] = txs
70-
}
7184

72-
collections = append(collections, collection)
85+
txs = append(txs, txData)
86+
}
87+
result["transactions"] = txs
7388
}
7489

75-
result["collection"] = collections
7690
return result
7791
}
7892

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

101115
_, _ = fmt.Fprintf(writer, "Total Seals\t%v\n", len(r.block.Seals))
102-
103116
_, _ = fmt.Fprintf(writer, "Total Collections\t%v\n", len(r.block.CollectionGuarantees))
104117

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

108-
if command.ContainsFlag(r.included, "transactions") {
109-
for x, tx := range r.collections[i].TransactionIDs {
110-
_, _ = fmt.Fprintf(writer, " Transaction %d: %s\n", x, tx)
123+
// Show transactions if included
124+
if command.ContainsFlag(r.included, "transactions") && len(r.transactions) > 0 {
125+
_, _ = fmt.Fprintf(writer, "\nTransactions:\n")
126+
127+
userCount := 0
128+
systemCount := 0
129+
130+
for i, tx := range r.transactions {
131+
var txType string
132+
if r.results[i].CollectionID == flow.EmptyID {
133+
txType = "system"
134+
systemCount++
135+
} else {
136+
txType = "user"
137+
userCount++
111138
}
139+
140+
_, _ = fmt.Fprintf(writer, " [%d] %s\t%s (%s)\n",
141+
i,
142+
tx.ID().String(),
143+
r.results[i].Status.String(),
144+
txType,
145+
)
112146
}
147+
148+
_, _ = fmt.Fprintf(writer, "\nTotal: %d transactions (%d user, %d system)\n",
149+
len(r.transactions), userCount, systemCount)
113150
}
114151

115152
if len(r.events) > 0 {

internal/blocks/blocks_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ func Test_GetBlock(t *testing.T) {
4747
assert.Equal(t, uint64(100), args.Get(3).(uint64))
4848
}).Return(nil, nil)
4949

50-
srv.GetCollection.Return(nil, nil)
50+
srv.GetTransactionsByBlockID.Return(
51+
[]*flow.Transaction{tests.NewTransaction()},
52+
[]*flow.TransactionResult{tests.NewTransactionResult(nil)},
53+
nil,
54+
)
5155

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

6569
func Test_Result(t *testing.T) {
6670
result := blockResult{
67-
block: tests.NewBlock(),
68-
collections: []*flow.Collection{tests.NewCollection()},
71+
block: tests.NewBlock(),
72+
transactions: []*flow.Transaction{},
73+
results: []*flow.TransactionResult{},
6974
}
7075

7176
assert.Equal(t, strings.TrimPrefix(`
@@ -86,7 +91,7 @@ Total Collections 3
8691
t,
8792
map[string]any{
8893
"blockId": "0303030303030303030303030303030303030303030303030303030303030303",
89-
"collection": []any{
94+
"collections": []any{
9095
map[string]any{"id": "0202020202020202020202020202020202020202020202020202020202020202"},
9196
map[string]any{"id": "0404040404040404040404040404040404040404040404040404040404040404"},
9297
map[string]any{"id": "0606060606060606060606060606060606060606060606060606060606060606"},

internal/blocks/get.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,20 @@ func get(
8282
}
8383
}
8484

85-
collections := make([]*flowsdk.Collection, 0)
85+
var transactions []*flowsdk.Transaction
86+
var results []*flowsdk.TransactionResult
8687
if command.ContainsFlag(blockFlags.Include, "transactions") {
87-
for _, guarantee := range block.CollectionGuarantees {
88-
collection, err := flow.GetCollection(context.Background(), guarantee.CollectionID)
89-
if err != nil {
90-
return nil, err
91-
}
92-
collections = append(collections, collection)
88+
transactions, results, err = flow.GetTransactionsByBlockID(context.Background(), block.ID)
89+
if err != nil {
90+
return nil, err
9391
}
9492
}
9593

9694
return &blockResult{
97-
block: block,
98-
events: events,
99-
collections: collections,
100-
included: blockFlags.Include,
95+
block: block,
96+
events: events,
97+
transactions: transactions,
98+
results: results,
99+
included: blockFlags.Include,
101100
}, nil
102101
}

0 commit comments

Comments
 (0)