Skip to content

Commit 5a3a552

Browse files
author
Jenita
committed
feat: added changes to implementutxoWholeResult
Signed-off-by: Jenita <[email protected]>
1 parent fdcfa3f commit 5a3a552

File tree

3 files changed

+123
-14
lines changed

3 files changed

+123
-14
lines changed

cmd/gouroboros/query.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,17 +279,40 @@ func testQuery(f *globalFlags) {
279279
}
280280
}
281281
case "utxo-whole-result":
282-
result, err := o.LocalStateQuery().Client.GetUTxOWhole()
282+
utxos, err := o.LocalStateQuery().Client.GetUTxOWhole()
283283
if err != nil {
284284
fmt.Printf("ERROR: failure querying UTxO whole: %s\n", err)
285285
os.Exit(1)
286286
}
287-
jsonData, err := json.MarshalIndent(result, "", " ")
288-
if err != nil {
289-
fmt.Printf("ERROR: failed to marshal UTxO whole to JSON: %s\n", err)
290-
os.Exit(1)
287+
288+
for utxoId, utxo := range utxos.Results {
289+
fmt.Println("---")
290+
fmt.Printf("UTxO ID: %s#%d\n", utxoId.Hash.String(), utxoId.Idx)
291+
fmt.Printf("Address: %x\n", utxo.Address)
292+
fmt.Printf("Amount: %d\n", utxo.Amount)
293+
294+
// Handle assets
295+
if utxo.Assets != nil {
296+
assets := utxo.Assets()
297+
fmt.Printf("Assests: %d\n", assets)
298+
if assets != nil {
299+
fmt.Printf("Assets: %s\n", assets)
300+
}
301+
}
302+
303+
// Handle datum
304+
if utxo.Datum != nil {
305+
datum := utxo.Datum()
306+
fmt.Printf("Datum: %d\n", datum)
307+
if datum != nil {
308+
if cborData := datum.Cbor(); err == nil {
309+
fmt.Printf("Datum CBOR: %x\n", cborData)
310+
} else {
311+
fmt.Printf("Datum present (error decoding: %v)\n", err)
312+
}
313+
}
314+
}
291315
}
292-
fmt.Println(string(jsonData))
293316
default:
294317
fmt.Printf("ERROR: unknown query: %s\n", queryFlags.flagset.Args()[0])
295318
os.Exit(1)

protocol/localstatequery/client.go

Lines changed: 90 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package localstatequery
1616

1717
import (
18-
"encoding/json"
1918
"errors"
2019
"fmt"
2120
"sync"
@@ -477,6 +476,90 @@ func (c *Client) GetUTxOByAddress(
477476
return &result, nil
478477
}
479478

479+
func (c *Client) GetUTxOWholeRaw() ([]byte, error) {
480+
c.busyMutex.Lock()
481+
defer c.busyMutex.Unlock()
482+
483+
currentEra, err := c.getCurrentEra()
484+
if err != nil {
485+
return nil, err
486+
}
487+
488+
query := buildShelleyQuery(
489+
currentEra,
490+
QueryTypeShelleyUtxoWhole,
491+
)
492+
493+
msg := NewMsgQuery(query)
494+
if !c.acquired {
495+
if err := c.acquire(AcquireVolatileTip{}); err != nil {
496+
return nil, err
497+
}
498+
}
499+
if err := c.SendMessage(msg); err != nil {
500+
return nil, err
501+
}
502+
resultCbor, ok := <-c.queryResultChan
503+
if !ok {
504+
return nil, protocol.ErrProtocolShuttingDown
505+
}
506+
return resultCbor, nil
507+
}
508+
509+
func decodeSimpleUTxO(rawCBOR []byte) (map[string]map[string]interface{}, error) {
510+
var cborData []interface{}
511+
if _, err := cbor.Decode(rawCBOR, &cborData); err != nil {
512+
return nil, fmt.Errorf("CBOR decode error: %v", err)
513+
}
514+
515+
result := make(map[string]map[string]interface{})
516+
517+
for _, entry := range cborData {
518+
entrySlice, ok := entry.([]interface{})
519+
if !ok || len(entrySlice) != 2 {
520+
continue
521+
}
522+
523+
// Decode input (TxId + index)
524+
input, ok := entrySlice[0].([]interface{})
525+
if !ok || len(input) != 2 {
526+
continue
527+
}
528+
529+
txId, ok := input[0].([]byte)
530+
if !ok {
531+
continue
532+
}
533+
534+
outputIdx, ok := input[1].(uint64)
535+
if !ok {
536+
continue
537+
}
538+
539+
// Decode output (address + value)
540+
output, ok := entrySlice[1].([]interface{})
541+
if !ok || len(output) != 2 {
542+
continue
543+
}
544+
545+
address, ok := output[0].([]byte)
546+
if !ok {
547+
continue
548+
}
549+
550+
value := output[1] // Could be uint64 (lovelace) or map (multi-asset)
551+
552+
// Create entry
553+
key := fmt.Sprintf("%x#%d", txId, outputIdx)
554+
result[key] = map[string]interface{}{
555+
"address": address,
556+
"value": value,
557+
}
558+
}
559+
560+
return result, nil
561+
}
562+
480563
// GetUTxOWhole returns the current UTxO set
481564
func (c *Client) GetUTxOWhole() (*UTxOWholeResult, error) {
482565
c.Protocol.Logger().
@@ -500,12 +583,12 @@ func (c *Client) GetUTxOWhole() (*UTxOWholeResult, error) {
500583
if err := c.runQuery(query, &result); err != nil {
501584
return nil, err
502585
}
503-
jsonData, err := json.MarshalIndent(result, "", " ")
504-
if err != nil {
505-
return nil, fmt.Errorf("error marshaling UTxOWhole result to JSON: %w", err)
506-
}
507-
fmt.Println("UTxOWhole Result:")
508-
fmt.Println(string(jsonData))
586+
// jsonData, err := json.MarshalIndent(result, "", " ")
587+
// if err != nil {
588+
// return nil, fmt.Errorf("error marshaling UTxOWhole result to JSON: %w", err)
589+
// }
590+
// fmt.Println("UTxOWhole Result:")
591+
// fmt.Println(string(jsonData))
509592
return &result, nil
510593
}
511594

protocol/localstatequery/queries.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,10 @@ func (u *UtxoId) MarshalCBOR() ([]byte, error) {
596596
/*
597597
result [{* utxo => value }]
598598
*/
599-
type UTxOWholeResult any
599+
type UTxOWholeResult struct {
600+
cbor.StructAsArray
601+
Results map[UtxoId]ledger.BabbageTransactionOutput
602+
}
600603

601604
// TODO (#863)
602605
type DebugEpochStateResult any

0 commit comments

Comments
 (0)