Skip to content

Commit 8a7b78f

Browse files
committed
chore: clean up
1 parent a019e42 commit 8a7b78f

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

cmd/p2p/sensor/rpc.go

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ func handleRPC(conns *p2p.Conns, networkID uint64) {
6060
defer r.Body.Close()
6161

6262
// Check if this is a batch request (starts with '[') or single request
63-
trimmedBody := strings.TrimSpace(string(body))
64-
if len(trimmedBody) > 0 && trimmedBody[0] == '[' {
63+
trimmed := strings.TrimSpace(string(body))
64+
if len(trimmed) > 0 && trimmed[0] == '[' {
6565
// Handle batch request
6666
handleBatchRequest(w, body, conns, chainID)
6767
return
@@ -138,27 +138,24 @@ func handleBatchRequest(w http.ResponseWriter, body []byte, conns *p2p.Conns, ch
138138
txs := make(types.Transactions, 0)
139139

140140
for _, req := range requests {
141-
if req.Method == "eth_sendRawTransaction" {
142-
// Try to parse and validate the transaction
143-
tx, response := validateTransaction(req, chainID)
144-
if tx != nil {
145-
// Transaction is valid, add to batch
146-
txs = append(txs, tx)
147-
// Create success response placeholder (will be updated after broadcast)
148-
responses = append(responses, rpcResponse{
149-
JSONRPC: "2.0",
150-
Result: tx.Hash().Hex(),
151-
ID: req.ID,
152-
})
153-
} else {
154-
// Transaction is invalid, add error response
155-
responses = append(responses, response)
156-
}
157-
} else {
158-
// Not a sendRawTransaction request
141+
if req.Method != "eth_sendRawTransaction" {
159142
response := processSingleRequest(req, conns, chainID)
160143
responses = append(responses, response)
144+
continue
161145
}
146+
147+
tx, response := validateTransaction(req, chainID)
148+
if tx == nil {
149+
responses = append(responses, response)
150+
continue
151+
}
152+
153+
txs = append(txs, tx)
154+
responses = append(responses, rpcResponse{
155+
JSONRPC: "2.0",
156+
Result: tx.Hash().Hex(),
157+
ID: req.ID,
158+
})
162159
}
163160

164161
// Broadcast all valid transactions in a single batch if there are any
@@ -200,7 +197,7 @@ func validateTransaction(req rpcRequest, chainID *big.Int) (*types.Transaction,
200197
}
201198

202199
// Extract raw transaction hex string
203-
rawTxHex, ok := req.Params[0].(string)
200+
hex, ok := req.Params[0].(string)
204201
if !ok {
205202
return nil, rpcResponse{
206203
JSONRPC: "2.0",
@@ -213,7 +210,7 @@ func validateTransaction(req rpcRequest, chainID *big.Int) (*types.Transaction,
213210
}
214211

215212
// Decode hex string to bytes
216-
txBytes, err := hexutil.Decode(rawTxHex)
213+
bytes, err := hexutil.Decode(hex)
217214
if err != nil {
218215
return nil, rpcResponse{
219216
JSONRPC: "2.0",
@@ -227,7 +224,7 @@ func validateTransaction(req rpcRequest, chainID *big.Int) (*types.Transaction,
227224

228225
// Unmarshal transaction
229226
tx := new(types.Transaction)
230-
if err := tx.UnmarshalBinary(txBytes); err != nil {
227+
if err := tx.UnmarshalBinary(bytes); err != nil {
231228
return nil, rpcResponse{
232229
JSONRPC: "2.0",
233230
Error: &rpcError{

0 commit comments

Comments
 (0)