Skip to content
8 changes: 6 additions & 2 deletions cmd/rpc/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ func (s *Server) Transaction(w http.ResponseWriter, r *http.Request, _ httproute
// Transactions handles multiple transactions in a single request
func (s *Server) Transactions(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// create a slice to hold the incoming transactions
var txs []lib.TransactionI
var transactions []*lib.Transaction

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the logic behind using the concrete type? when I first made this change I used the interface in order to avoid the iteration done now to cast to the concrete type as a small optimization, any reason to do this?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I wanted to keep the interface here too, but encoding/json can't instantiate entries in []lib.TransactionI, so normal tx objects fail to decode on the batch endpoint. Decoding into []*lib.Transaction gives it a concrete target. The loop is only wrapping pointers in the interface slice, not copying the txs, and keeps us from needing a custom decoder or a wider submitTxs change.

// unmarshal the HTTP request body into the transactions slice
if ok := unmarshal(w, r, &txs); !ok {
if ok := unmarshal(w, r, &transactions); !ok {
return
}
txs := make([]lib.TransactionI, len(transactions))
for i := range transactions {
txs[i] = transactions[i]
}
// submit transactions to RPC server
s.submitTxs(w, txs)
}
Expand Down
Loading
Loading