Skip to content

Commit 8ac9e73

Browse files
authored
fix(utxorpc): correctly handle empty datum fields in ReadUtxos response (#286)
1 parent 3393973 commit 8ac9e73

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

internal/utxorpc/query.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
connect "connectrpc.com/connect"
2424
"github.com/blinklabs-io/gouroboros/ledger"
25+
2526
// ocommon "github.com/blinklabs-io/gouroboros/protocol/common"
2627
query "github.com/utxorpc/go-codegen/utxorpc/v1alpha/query"
2728
"github.com/utxorpc/go-codegen/utxorpc/v1alpha/query/queryconnect"
@@ -59,14 +60,12 @@ func (s *queryServiceServer) ReadParams(
5960
// Get protoParams
6061
protoParams, err := oConn.LocalStateQuery().Client.GetCurrentProtocolParams()
6162
if err != nil {
62-
log.Printf("ERROR: %s", err)
6363
return nil, err
6464
}
6565

6666
// Get chain point (slot and hash)
6767
point, err := oConn.LocalStateQuery().Client.GetChainPoint()
6868
if err != nil {
69-
log.Printf("ERROR: %s", err)
7069
return nil, err
7170
}
7271
var acp query.AnyChainParams
@@ -89,7 +88,6 @@ func (s *queryServiceServer) ReadUtxos(
8988
ctx context.Context,
9089
req *connect.Request[query.ReadUtxosRequest],
9190
) (*connect.Response[query.ReadUtxosResponse], error) {
92-
9391
keys := req.Msg.GetKeys() // []*TxoRef
9492
log.Printf("Got a ReadUtxos request with keys %v", keys)
9593
resp := &query.ReadUtxosResponse{}
@@ -120,14 +118,12 @@ func (s *queryServiceServer) ReadUtxos(
120118
// Get UTxOs
121119
utxos, err := oConn.LocalStateQuery().Client.GetUTxOByTxIn(tmpTxIns)
122120
if err != nil {
123-
log.Printf("ERROR: %s", err)
124121
return nil, err
125122
}
126123

127124
// Get chain point (slot and hash)
128125
point, err := oConn.LocalStateQuery().Client.GetChainPoint()
129126
if err != nil {
130-
log.Printf("ERROR: %s", err)
131127
return nil, err
132128
}
133129

@@ -141,6 +137,23 @@ func (s *queryServiceServer) ReadUtxos(
141137
uint32(utxoId.Idx) == txo.Index {
142138
aud.NativeBytes = utxo.Cbor()
143139
audc.Cardano = utxo.Utxorpc()
140+
if audc.Cardano.Datum != nil {
141+
// Check if Datum.Hash is all zeroes
142+
isAllZeroes := true
143+
for _, b := range audc.Cardano.Datum.Hash {
144+
if b != 0 {
145+
isAllZeroes = false
146+
break
147+
}
148+
}
149+
if isAllZeroes {
150+
// No actual datum; set Datum to nil to omit it
151+
audc.Cardano.Datum = nil
152+
log.Print("Datum Hash is all zeroes; setting Datum to nil")
153+
} else {
154+
log.Printf("Datum Hash present: %x", audc.Cardano.Datum.Hash)
155+
}
156+
}
144157
aud.ParsedState = &audc
145158
}
146159
resp.Items = append(resp.Items, &aud)
@@ -150,6 +163,8 @@ func (s *queryServiceServer) ReadUtxos(
150163
Slot: point.Slot,
151164
Hash: point.Hash,
152165
}
166+
log.Printf("Prepared response with LedgerTip: Slot=%v, Hash=%v", resp.LedgerTip.Slot, resp.LedgerTip.Hash)
167+
log.Printf("Final response: %v", resp)
153168
return connect.NewResponse(resp), nil
154169
}
155170

0 commit comments

Comments
 (0)