implement weight estimation
MODIFY
WalletWasabi.Tests/UnitTests/WabiSabi/Backend/InputRegistrationTests.cs
WalletWasabi.Tests/UnitTests/WabiSabi/Backend/InputRegistrationTests.cs
@@ -275,5 +275,59 @@ namespace WalletWasabi.Tests.UnitTests.WabiSabi.Backend
var ex = await Assert.ThrowsAnyAsync<WabiSabiProtocolException>(async () => await handler.RegisterInputAsync(req));
Assert.Equal(WabiSabiProtocolErrorCode.TooMuchFunds, ex.ErrorCode);
}
+
+ [Fact]
+ public async Task NotEnoughWeightAsync()
+ {
+ MockArena arena = new();
+ var cfg = new WabiSabiConfig() { MinRegistrableWeight = 1000 };
+ var round = WabiSabiFactory.CreateRound(cfg);
+ arena.OnTryGetRound = _ => round;
+ MockRpcClient rpc = new();
+ using Key key = new();
+
+ rpc.OnGetTxOutAsync = (_, _, _) => new GetTxOutResponse
+ {
+ Confirmations = 1,
+ ScriptPubKeyType = "witness_v0_keyhash",
+ TxOut = new TxOut(Money.Coins(1), key.PubKey.GetSegwitAddress(Network.Main))
+ };
+
+ await using PostRequestHandler handler = new(cfg, new Prison(), arena, rpc);
+ var req = new InputsRegistrationRequest(
+ round.Id,
+ WabiSabiFactory.CreateInputRoundSignaturePairs(new[] { key }, round.Hash),
+ null!,
+ null!);
+ var ex = await Assert.ThrowsAnyAsync<WabiSabiProtocolException>(async () => await handler.RegisterInputAsync(req));
+ Assert.Equal(WabiSabiProtocolErrorCode.NotEnoughWeight, ex.ErrorCode);
+ }
+
+ [Fact]
+ public async Task TooMuchWeightAsync()
+ {
+ MockArena arena = new();
+ var cfg = new WabiSabiConfig() { MaxRegistrableWeight = 1 };
+ var round = WabiSabiFactory.CreateRound(cfg);
+ arena.OnTryGetRound = _ => round;
+ MockRpcClient rpc = new();
+ using Key key = new();
+
+ rpc.OnGetTxOutAsync = (_, _, _) => new GetTxOutResponse
+ {
+ Confirmations = 1,
+ ScriptPubKeyType = "witness_v0_keyhash",
+ TxOut = new TxOut(Money.Coins(1), key.PubKey.GetSegwitAddress(Network.Main))
+ };
+
+ await using PostRequestHandler handler = new(cfg, new Prison(), arena, rpc);
+ var req = new InputsRegistrationRequest(
+ round.Id,
+ WabiSabiFactory.CreateInputRoundSignaturePairs(new[] { key }, round.Hash),
+ null!,
+ null!);
+ var ex = await Assert.ThrowsAnyAsync<WabiSabiProtocolException>(async () => await handler.RegisterInputAsync(req));
+ Assert.Equal(WabiSabiProtocolErrorCode.TooMuchWeight, ex.ErrorCode);
+ }
}
}

MODIFY
WalletWasabi/WabiSabi/Backend/Models/WabiSabiProtocolErrorCode.cs
WalletWasabi/WabiSabi/Backend/Models/WabiSabiProtocolErrorCode.cs
@@ -18,6 +18,8 @@ namespace WalletWasabi.WabiSabi.Backend.Models
TooManyInputs,
NotEnoughFunds,
TooMuchFunds,
- NonUniqueInputs
+ NonUniqueInputs,
+ NotEnoughWeight,
+ TooMuchWeight
}
}

MODIFY
WalletWasabi/WabiSabi/Backend/PostRequests/PostRequestHandler.cs
WalletWasabi/WabiSabi/Backend/PostRequests/PostRequestHandler.cs
@@ -85,7 +85,15 @@ namespace WalletWasabi.WabiSabi.Backend.PostRequests
throw new WabiSabiProtocolException(WabiSabiProtocolErrorCode.WrongRoundSignature);
}
inputValueSum += txOutResponse.TxOut.Value;
- // inputWeightSum+=txOutResponse.TxOut.ScriptPubKey what to do with this?
+
+ if (txOutResponse.ScriptPubKeyType == "witness_v0_keyhash")
+ {
+ inputWeightSum += 160;
+ }
+ else
+ {
+ throw new NotImplementedException($"{txOutResponse.ScriptPubKeyType} weight estimation isn't implemented.");
+ }
}
if (inputValueSum < round.MinRegistrableAmount)
@@ -97,6 +105,15 @@ namespace WalletWasabi.WabiSabi.Backend.PostRequests
throw new WabiSabiProtocolException(WabiSabiProtocolErrorCode.TooMuchFunds);
}
+ if (inputWeightSum < round.MinRegistrableWeight)
+ {
+ throw new WabiSabiProtocolException(WabiSabiProtocolErrorCode.NotEnoughWeight);
+ }
+ if (inputWeightSum > round.MaxRegistrableWeight)
+ {
+ throw new WabiSabiProtocolException(WabiSabiProtocolErrorCode.TooMuchWeight);
+ }
+
throw new NotImplementedException();
}
}

implement weight estimation