Broadcast wallet transactions in chunks (#2253)

This commit is contained in:
Ori Newman 2023-12-13 16:15:25 +02:00 committed by GitHub
parent 5a3b8a0066
commit a0149cd8d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -75,23 +75,29 @@ func send(conf *sendConfig) error {
signedTransactions[i] = signedTransaction signedTransactions[i] = signedTransaction
} }
if len(signedTransactions) > 1 { fmt.Printf("Broadcasting %d transaction(s)\n", len(signedTransactions))
fmt.Printf("Broadcasting %d transactions\n", len(signedTransactions))
}
// Since we waited for user input when getting the password, which could take unbound amount of time - // Since we waited for user input when getting the password, which could take unbound amount of time -
// create a new context for broadcast, to reset the timeout. // create a new context for broadcast, to reset the timeout.
broadcastCtx, broadcastCancel := context.WithTimeout(context.Background(), daemonTimeout) broadcastCtx, broadcastCancel := context.WithTimeout(context.Background(), daemonTimeout)
defer broadcastCancel() defer broadcastCancel()
response, err := daemonClient.Broadcast(broadcastCtx, &pb.BroadcastRequest{Transactions: signedTransactions}) const chunkSize = 100 // To avoid sending a message bigger than the gRPC max message size, we split it to chunks
if err != nil { for offset := 0; offset < len(signedTransactions); offset += chunkSize {
return err end := len(signedTransactions)
} if offset+chunkSize <= len(signedTransactions) {
fmt.Println("Transactions were sent successfully") end = offset + chunkSize
fmt.Println("Transaction ID(s): ") }
for _, txID := range response.TxIDs {
fmt.Printf("\t%s\n", txID) chunk := signedTransactions[offset:end]
response, err := daemonClient.Broadcast(broadcastCtx, &pb.BroadcastRequest{Transactions: chunk})
if err != nil {
return err
}
fmt.Printf("Broadcasted %d transaction(s) (broadcasted %.2f%% of the transactions so far)\n", len(chunk), 100*float64(end)/float64(len(signedTransactions)))
fmt.Println("Broadcasted Transaction ID(s): ")
for _, txID := range response.TxIDs {
fmt.Printf("\t%s\n", txID)
}
} }
if conf.Verbose { if conf.Verbose {