@@ -17,9 +17,11 @@ package utxorpc
1717import (
1818 "bytes"
1919 "context"
20+ "encoding/binary"
2021 "encoding/hex"
2122 "errors"
2223 "fmt"
24+ "math"
2325
2426 "connectrpc.com/connect"
2527 "github.com/blinklabs-io/dingo/event"
@@ -42,86 +44,33 @@ func (s *submitServiceServer) SubmitTx(
4244 ctx context.Context ,
4345 req * connect.Request [submit.SubmitTxRequest ],
4446) (* connect.Response [submit.SubmitTxResponse ], error ) {
45- txRawList := req .Msg .GetTx () // []*AnyChainTx
47+ txRaw := req .Msg .GetTx ()
4648
47- s .utxorpc .config .Logger .Info (
48- fmt .Sprintf (
49- "Got a SubmitTx request with %d transactions" ,
50- len (txRawList ),
51- ),
52- )
49+ s .utxorpc .config .Logger .Info ("Got a SubmitTx request" )
5350 resp := & submit.SubmitTxResponse {}
5451
55- // Loop through the transactions and add each to the mempool
56- errorList := make ([]error , len (txRawList ))
57- hasError := false
58- placeholderRef := []byte {}
59- for i , txi := range txRawList {
60- txRawBytes := txi .GetRaw () // raw bytes
61- txType , err := gledger .DetermineTransactionType (txRawBytes )
62- if err != nil {
63- resp .Ref = append (resp .Ref , placeholderRef )
64- errorList [i ] = err
65- s .utxorpc .config .Logger .Error (
66- fmt .Sprintf (
67- "failed decoding tx %d: %v" ,
68- i ,
69- err ,
70- ),
71- )
72- hasError = true
73- continue
74- }
75- tx , err := gledger .NewTransactionFromCbor (txType , txRawBytes )
76- if err != nil {
77- resp .Ref = append (resp .Ref , placeholderRef )
78- errorList [i ] = err
79- s .utxorpc .config .Logger .Error (
80- fmt .Sprint (
81- fmt .Errorf (
82- "failed to decode transaction from CBOR: %w" ,
83- err ,
84- ),
85- ),
86- )
87- hasError = true
88- continue
89- }
90- if tx == nil {
91- resp .Ref = append (resp .Ref , placeholderRef )
92- errorList [i ] = errors .New ("decoded transaction is nil" )
93- s .utxorpc .config .Logger .Error ("decoded transaction is nil" )
94- hasError = true
95- continue
96- }
97- txHash := tx .Hash ()
98- // Add transaction to mempool
99- err = s .utxorpc .config .Mempool .AddTransaction (txType , txRawBytes )
100- if err != nil {
101- resp .Ref = append (resp .Ref , placeholderRef )
102- errorList [i ] = fmt .Errorf ("%s" , err .Error ())
103- s .utxorpc .config .Logger .Error (
104- fmt .Sprintf (
105- "failed to add tx %s to mempool: %s" ,
106- txHash .String (),
107- err ,
108- ),
109- )
110- hasError = true
111- continue
112- }
113- if err != nil {
114- resp .Ref = append (resp .Ref , placeholderRef )
115- errorList [i ] = err
116- hasError = true
117- continue
118- }
119- resp .Ref = append (resp .Ref , txHash .Bytes ())
52+ txRawBytes := txRaw .GetRaw ()
53+ txType , err := gledger .DetermineTransactionType (txRawBytes )
54+ if err != nil {
55+ return nil , fmt .Errorf ("failed decoding tx: %w" , err )
56+ }
57+ tx , err := gledger .NewTransactionFromCbor (txType , txRawBytes )
58+ if err != nil {
59+ return nil , fmt .Errorf (
60+ "failed to decode transaction from CBOR: %w" ,
61+ err ,
62+ )
12063 }
121- if hasError {
122- return connect . NewResponse ( resp ), fmt . Errorf ( "%v" , errorList )
64+ if tx == nil {
65+ return nil , errors . New ( "decoded transaction is nil" )
12366 }
124-
67+ txHash := tx .Hash ()
68+ // Add transaction to mempool
69+ err = s .utxorpc .config .Mempool .AddTransaction (txType , txRawBytes )
70+ if err != nil {
71+ return nil , fmt .Errorf ("failed to add tx to mempool: %w" , err )
72+ }
73+ resp .Ref = txHash .Bytes ()
12574 return connect .NewResponse (resp ), nil
12675}
12776
@@ -188,69 +137,81 @@ func (s *submitServiceServer) EvalTx(
188137 req * connect.Request [submit.EvalTxRequest ],
189138) (* connect.Response [submit.EvalTxResponse ], error ) {
190139 s .utxorpc .config .Logger .Info ("Got an EvalTx request" )
191- txRawList := req .Msg .GetTx () // []*AnyChainTx
140+ txRaw := req .Msg .GetTx ()
192141 resp := & submit.EvalTxResponse {}
193- for _ , txi := range txRawList {
194- txRawBytes := txi .GetRaw ()
195- // Decode TX
196- txType , err := gledger .DetermineTransactionType (txRawBytes )
197- if err != nil {
198- return nil , fmt .Errorf (
199- "could not parse transaction to determine type: %w" ,
200- err ,
201- )
202- }
203- tx , err := gledger .NewTransactionFromCbor (txType , txRawBytes )
204- if err != nil {
205- return nil , fmt .Errorf ("failed to parse transaction CBOR: %w" , err )
206- }
207- // Evaluate TX
208- fee , totalExUnits , redeemerExUnits , err := s .utxorpc .config .LedgerState .EvaluateTx (
209- tx ,
142+
143+ txRawBytes := txRaw .GetRaw ()
144+ // Decode TX
145+ txType , err := gledger .DetermineTransactionType (txRawBytes )
146+ if err != nil {
147+ return nil , fmt .Errorf (
148+ "could not parse transaction to determine type: %w" ,
149+ err ,
210150 )
211- // Populate response
212- tmpRedeemers := make ([]* cardano.Redeemer , 0 , len (redeemerExUnits ))
213- for key , val := range redeemerExUnits {
214- tmpRedeemers = append (
215- tmpRedeemers ,
216- & cardano.Redeemer {
217- Purpose : cardano .RedeemerPurpose (key .Tag ),
218- Index : key .Index ,
219- ExUnits : & cardano.ExUnits {
220- Steps : uint64 (val .Steps ), // nolint:gosec
221- Memory : uint64 (val .Memory ), // nolint:gosec
222- },
223- // TODO: Payload
151+ }
152+ tx , err := gledger .NewTransactionFromCbor (txType , txRawBytes )
153+ if err != nil {
154+ return nil , fmt .Errorf ("failed to parse transaction CBOR: %w" , err )
155+ }
156+ // Evaluate TX
157+ fee , totalExUnits , redeemerExUnits , err := s .utxorpc .config .LedgerState .EvaluateTx (
158+ tx ,
159+ )
160+ // Populate response
161+ tmpRedeemers := make ([]* cardano.Redeemer , 0 , len (redeemerExUnits ))
162+ for key , val := range redeemerExUnits {
163+ tmpRedeemers = append (
164+ tmpRedeemers ,
165+ & cardano.Redeemer {
166+ Purpose : cardano .RedeemerPurpose (key .Tag ),
167+ Index : key .Index ,
168+ ExUnits : & cardano.ExUnits {
169+ Steps : uint64 (val .Steps ), // nolint:gosec
170+ Memory : uint64 (val .Memory ), // nolint:gosec
171+ },
172+ // TODO: Payload
173+ },
174+ )
175+ }
176+ var txEval * cardano.TxEval
177+ if err != nil {
178+ txEval = & cardano.TxEval {
179+ Errors : []* cardano.EvalError {
180+ {
181+ Msg : err .Error (),
224182 },
225- )
183+ },
226184 }
227- var txEval * cardano.TxEval
228- if err != nil {
229- txEval = & cardano.TxEval {
230- Errors : []* cardano.EvalError {
231- {
232- Msg : err .Error (),
233- },
185+ } else {
186+ var feeBigInt * cardano.BigInt
187+ if fee <= math .MaxInt64 {
188+ feeBigInt = & cardano.BigInt {
189+ BigInt : & cardano.BigInt_Int {
190+ Int : int64 (fee ),
234191 },
235192 }
236193 } else {
237- txEval = & cardano. TxEval {
238- Fee : fee ,
239- ExUnits : & cardano.ExUnits {
240- Steps : uint64 ( totalExUnits . Steps ), // nolint:gosec
241- Memory : uint64 ( totalExUnits . Memory ), // nolint:gosec
194+ buf := make ([] byte , 8 )
195+ binary . BigEndian . PutUint64 ( buf , fee )
196+ feeBigInt = & cardano.BigInt {
197+ BigInt : & cardano. BigInt_BigUInt {
198+ BigUInt : buf ,
242199 },
243- Redeemers : tmpRedeemers ,
244200 }
245201 }
246- resp .Report = append (
247- resp .Report ,
248- & submit.AnyChainEval {
249- Chain : & submit.AnyChainEval_Cardano {
250- Cardano : txEval ,
251- },
202+ txEval = & cardano.TxEval {
203+ Fee : feeBigInt ,
204+ ExUnits : & cardano.ExUnits {
205+ Steps : uint64 (totalExUnits .Steps ), // nolint:gosec
206+ Memory : uint64 (totalExUnits .Memory ), // nolint:gosec
252207 },
253- )
208+ Redeemers : tmpRedeemers ,
209+ }
210+ }
211+ resp .Report = & submit.AnyChainEval {
212+ Chain : & submit.AnyChainEval_Cardano {
213+ Cardano : txEval ,
214+ },
254215 }
255216 return connect .NewResponse (resp ), nil
256217}
0 commit comments