Skip to content

Commit

Permalink
Fix shared transaction overhead mess (WalletWasabi#13312)
Browse files Browse the repository at this point in the history
  • Loading branch information
lontivero authored Aug 7, 2024
1 parent 642d175 commit 1e51ede
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
4 changes: 2 additions & 2 deletions WalletWasabi.Tests/Helpers/ArenaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using System.Threading;
using System.Threading.Tasks;
using WalletWasabi.BitcoinCore.Rpc;
using WalletWasabi.Helpers;
using WalletWasabi.WabiSabi.Backend;
using WalletWasabi.WabiSabi.Backend.DoSPrevention;
using WalletWasabi.WabiSabi.Backend.Rounds;
using WalletWasabi.WabiSabi.Models.MultipartyTransaction;

namespace WalletWasabi.Tests.Helpers;

Expand Down Expand Up @@ -83,5 +83,5 @@ public ArenaBuilder With(RoundParameterFactory roundParameterFactory)
public static ArenaBuilder From(WabiSabiConfig cfg, IRPCClient mockRpc, Prison prison) => new() { Config = cfg, Rpc = mockRpc, Prison = prison };

private static RoundParameterFactory CreateRoundParameterFactory(WabiSabiConfig cfg, Network network) =>
WabiSabiFactory.CreateRoundParametersFactory(cfg, network, maxVsizeAllocationPerAlice: 11 + 31 + MultipartyTransactionParameters.SharedOverhead);
WabiSabiFactory.CreateRoundParametersFactory(cfg, network, Constants.P2wpkhInputVirtualSize + Constants.P2wpkhOutputVirtualSize);
}
3 changes: 2 additions & 1 deletion WalletWasabi.Tests/Helpers/WabiSabiFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public static Round CreateRound(RoundParameters parameters) =>
public static Round CreateRound(WabiSabiConfig cfg) =>
CreateRound(CreateRoundParameters(cfg) with
{
MaxVsizeAllocationPerAlice = 11 + 31 + MultipartyTransactionParameters.SharedOverhead
MaxVsizeAllocationPerAlice =
Constants.P2wpkhInputVirtualSize + Constants.P2wpkhOutputVirtualSize // enough vsize for one input and one output
});

public static MockRpcClient CreatePreconfiguredRpcClient(params Coin[] coins)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,21 @@ public async Task RoundNotFoundAsync()
[Fact]
public async Task ScriptNotAllowedAsync()
{
using Key key = new();
var outputScript = key.PubKey.GetAddress(ScriptPubKeyType.Legacy, Network.Main).ScriptPubKey;

WabiSabiConfig cfg = new();
RoundParameters parameters = WabiSabiFactory.CreateRoundParameters(cfg)
with
{ MaxVsizeAllocationPerAlice = 11 + 34 + MultipartyTransactionParameters.SharedOverhead };
{ MaxVsizeAllocationPerAlice = Constants.P2wpkhInputVirtualSize + outputScript.EstimateOutputVsize() };
var round = WabiSabiFactory.CreateRound(parameters);

using Arena arena = await ArenaBuilder.From(cfg).CreateAndStartAsync(round);
using Key key = new();

round.SetPhase(Phase.OutputRegistration);
round.Alices.Add(WabiSabiFactory.CreateAlice(Money.Coins(1), round));

var req = WabiSabiFactory.CreateOutputRegistrationRequest(round, key.PubKey.GetAddress(ScriptPubKeyType.Legacy, Network.Main).ScriptPubKey);
var req = WabiSabiFactory.CreateOutputRegistrationRequest(round, outputScript);
var ex = await Assert.ThrowsAsync<WabiSabiProtocolException>(async () => await arena.RegisterOutputAsync(req, CancellationToken.None));
Assert.Equal(WabiSabiProtocolErrorCode.ScriptNotAllowed, ex.ErrorCode);

Expand All @@ -127,17 +129,17 @@ public async Task ScriptNotAllowedAsync()
[Fact]
public async Task NonStandardOutputAsync()
{
var sha256Bounty = Script.FromHex("aa20000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f87");
WabiSabiConfig cfg = new();
RoundParameters parameters = WabiSabiFactory.CreateRoundParameters(cfg)
with
{ MaxVsizeAllocationPerAlice = 11 + 31 + MultipartyTransactionParameters.SharedOverhead + 13 };
{ MaxVsizeAllocationPerAlice = Constants.P2wpkhInputVirtualSize + sha256Bounty.EstimateOutputVsize() };
var round = WabiSabiFactory.CreateRound(parameters);
using Arena arena = await ArenaBuilder.From(cfg).CreateAndStartAsync(round);

round.SetPhase(Phase.OutputRegistration);
round.Alices.Add(WabiSabiFactory.CreateAlice(Money.Coins(1), round));

var sha256Bounty = Script.FromHex("aa20000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f87");
var req = WabiSabiFactory.CreateOutputRegistrationRequest(round, sha256Bounty);
var ex = await Assert.ThrowsAsync<WabiSabiProtocolException>(async () => await arena.RegisterOutputAsync(req, CancellationToken.None));

Expand Down
2 changes: 1 addition & 1 deletion WalletWasabi/Helpers/VirtualSizeHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static class VirtualSizeHelpers
public const int VirtualByteInWeightUnits = 4;

public static int WeightUnitsToVirtualSize(int weightUnits) =>
(weightUnits / VirtualByteInWeightUnits) + ((weightUnits % VirtualByteInWeightUnits) == 0 ? 0 : 1); // ceiling(VirtualSize / VirtualByteInWeightUnits)
(weightUnits + VirtualByteInWeightUnits - 1) / VirtualByteInWeightUnits;

public static int VirtualSize(int nonSegwitBytes, int segwitBytes) =>
WeightUnitsToVirtualSize(WeightUnits(nonSegwitBytes, segwitBytes));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private static int SharedOverheadFn(long minInputCount, long minOutputCount)
1 + // marker
1; // flag

return VirtualSizeHelpers.WeightUnits(nonSegwitData, segwitData);
return VirtualSizeHelpers.VirtualSize(nonSegwitData, segwitData);
}

private static int VarIntLength(long value) =>
Expand Down

0 comments on commit 1e51ede

Please sign in to comment.