From 38879422073bc6f9035c6fd274fd77c69e758f37 Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Wed, 13 Sep 2023 20:47:18 +0200 Subject: [PATCH 1/2] soroban-rpc: preflight: Fix double-counting bug (#960) Counterpart to https://github.com/stellar/stellar-core/pull/3912 --- cmd/soroban-rpc/lib/preflight/src/fees.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/cmd/soroban-rpc/lib/preflight/src/fees.rs b/cmd/soroban-rpc/lib/preflight/src/fees.rs index dca3fbd33..5018dac82 100644 --- a/cmd/soroban-rpc/lib/preflight/src/fees.rs +++ b/cmd/soroban-rpc/lib/preflight/src/fees.rs @@ -43,8 +43,6 @@ pub(crate) fn compute_host_function_transaction_data_and_min_fee( calculate_host_function_soroban_resources(&ledger_changes, &post_storage.footprint, budget) .context("cannot compute host function resources")?; - let read_write_entries = u32::try_from(soroban_resources.footprint.read_write.as_vec().len())?; - let contract_events_size = calculate_contract_events_size_bytes(events).context("cannot calculate events size")?; let invocation_return_size = u32::try_from(invocation_result.to_xdr()?.len())?; @@ -53,9 +51,8 @@ pub(crate) fn compute_host_function_transaction_data_and_min_fee( let transaction_resources = TransactionResources { instructions: soroban_resources.instructions, - read_entries: u32::try_from(soroban_resources.footprint.read_only.as_vec().len())? - + read_write_entries, - write_entries: read_write_entries, + read_entries: u32::try_from(soroban_resources.footprint.read_only.as_vec().len())?, + write_entries: u32::try_from(soroban_resources.footprint.read_write.as_vec().len())?, read_bytes: soroban_resources.read_bytes, write_bytes: soroban_resources.write_bytes, // Note: we could get a better transaction size if the full transaction was passed down to libpreflight @@ -440,7 +437,6 @@ pub(crate) fn compute_restore_footprint_transaction_data_and_min_fee( read_bytes: write_bytes + expiration_bytes, write_bytes, }; - let entry_count = u32::try_from(soroban_resources.footprint.read_write.as_vec().len())?; let transaction_size_bytes = estimate_max_transaction_size_for_operation( &OperationBody::RestoreFootprint(RestoreFootprintOp { ext: ExtensionPoint::V0, @@ -450,8 +446,8 @@ pub(crate) fn compute_restore_footprint_transaction_data_and_min_fee( .context("cannot estimate maximum transaction size")?; let transaction_resources = TransactionResources { instructions: 0, - read_entries: entry_count, - write_entries: entry_count, + read_entries: 0, + write_entries: u32::try_from(soroban_resources.footprint.read_write.as_vec().len())?, read_bytes: soroban_resources.read_bytes, write_bytes: soroban_resources.write_bytes, transaction_size_bytes, From d3664b5182ea9531649bc019e952120cf4a2e7c6 Mon Sep 17 00:00:00 2001 From: Tsachi Herman <24438559+tsachiherman@users.noreply.github.com> Date: Wed, 13 Sep 2023 16:17:55 -0400 Subject: [PATCH 2/2] soroban-rpc: fix datarace in bufferedResponseWriter.WriteOut (#961) * fix datarace. * revise --- .../network/requestdurationlimiter.go | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/cmd/soroban-rpc/internal/network/requestdurationlimiter.go b/cmd/soroban-rpc/internal/network/requestdurationlimiter.go index fef80fd39..05204591f 100644 --- a/cmd/soroban-rpc/internal/network/requestdurationlimiter.go +++ b/cmd/soroban-rpc/internal/network/requestdurationlimiter.go @@ -100,24 +100,20 @@ func (w *bufferedResponseWriter) WriteOut(ctx context.Context, rw http.ResponseW for k, v := range w.header { headers[k] = v } - complete := make(chan interface{}) - go func() { - if len(w.buffer) == 0 { - if w.statusCode != 0 { - rw.WriteHeader(w.statusCode) - } - return - } + + if len(w.buffer) == 0 { if w.statusCode != 0 { rw.WriteHeader(w.statusCode) } + return + } + if w.statusCode != 0 { + rw.WriteHeader(w.statusCode) + } + + if ctx.Err() == nil { // the following return size/error won't help us much at this point. The request is already finalized. rw.Write(w.buffer) //nolint:errcheck - close(complete) - }() - select { - case <-complete: - case <-ctx.Done(): } }