Skip to content

Commit

Permalink
MimbleWimble(Tests): implement MW types
Browse files Browse the repository at this point in the history
Add types needed for MimbleWimble transactions and implement
serialization.

Made all MimbleWimble types implement ISerializable and
interface and Read static method. Reuse NBitcoin's
BitcoinStream for serialization and deserialization, but don't
use its IBitcoinSerializable interface because it mixes reading
and writing in one method.

Added new test project for MimbleWimble tests. Added test for
peg-in transaction.

Added property-based tests for serialization and
deserilaization.

Added test that deserilizes transaction generated and
serialized by modified litecoin test (see [1]).

Added tests for parsing peg-out transaction and MW to MW
(HogEx) transaction.

[1] https://github.com/litecoin-project/litecoin/blob/5ac781487cc9589131437b23c69829f04002b97e/src/libmw/test/tests/models/tx/Test_Transaction.cpp
  • Loading branch information
webwarrior-ws committed Mar 26, 2024
1 parent 527a9be commit 5f5e144
Show file tree
Hide file tree
Showing 13 changed files with 988 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
/src/NLitecoin/bin
/tests/NLitecoin.Tests/obj
/tests/NLitecoin.Tests/bin
/tests/NLitecoin.MimbleWimble.Tests/obj
/tests/NLitecoin.MimbleWimble.Tests/bin
10 changes: 8 additions & 2 deletions NLitecoin.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.6.33829.357
MinimumVisualStudioVersion = 10.0.40219.1
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "NLitecoin", "src\NLitecoin\NLitecoin.fsproj", "{1128287F-0435-4FA9-B4DD-420A9E1FDB23}"
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "NLitecoin", "src\NLitecoin\NLitecoin.fsproj", "{1128287F-0435-4FA9-B4DD-420A9E1FDB23}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NLitecoin.Tests", "tests\NLitecoin.Tests\NLitecoin.Tests.csproj", "{F954D3AC-36BC-483E-8DB1-4CC65DF1694D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NLitecoin.Tests", "tests\NLitecoin.Tests\NLitecoin.Tests.csproj", "{F954D3AC-36BC-483E-8DB1-4CC65DF1694D}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "NLitecoin.MimbleWimble.Tests", "tests\NLitecoin.MimbleWimble.Tests\NLitecoin.MimbleWimble.Tests.fsproj", "{6A67D54D-BE2C-449F-88D2-D6739A5F3F89}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -21,6 +23,10 @@ Global
{F954D3AC-36BC-483E-8DB1-4CC65DF1694D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F954D3AC-36BC-483E-8DB1-4CC65DF1694D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F954D3AC-36BC-483E-8DB1-4CC65DF1694D}.Release|Any CPU.Build.0 = Release|Any CPU
{6A67D54D-BE2C-449F-88D2-D6739A5F3F89}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6A67D54D-BE2C-449F-88D2-D6739A5F3F89}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6A67D54D-BE2C-449F-88D2-D6739A5F3F89}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6A67D54D-BE2C-449F-88D2-D6739A5F3F89}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
47 changes: 29 additions & 18 deletions src/NLitecoin/Litecoin.fs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,14 @@ module private TxListExtensions =
type LitecoinTransaction() =
inherit Transaction()

static let mwebExtensionTxFlag = 8uy

member val MimbleWimbleTransaction: Option<MimbleWimble.Transaction> = None with get, set

override self.GetConsensusFactory() = LitecoinConsensusFactory.Instance

member private self.Read (stream: BitcoinStream) (witSupported: bool) =
let flags = 0uy
let mutable flags = 0uy
self.nVersion <- stream.ReadWrite self.nVersion
// Try to read the vin. In case the dummy is there, this will be read as an empty vector.
stream.ReadWrite(&self.vin)
Expand All @@ -64,7 +68,7 @@ type LitecoinTransaction() =

if self.vin.Count = 0 && witSupported && not hasNoDummy then
// We read a dummy or an empty vin.
let flags = stream.ReadWrite flags
flags <- stream.ReadWrite flags
if flags <> 0uy then
// Assume we read a dummy and a flag.
stream.ReadWrite(&self.vin)
Expand All @@ -79,22 +83,17 @@ type LitecoinTransaction() =
stream.ReadWrite(&self.vout)
self.vout <- self.vout.WithTransaction self

let flags =
if ((flags &&& 1uy) <> 0uy) && witSupported then
// The witness flag is present, and we support witnesses.
let wit = Witness self.Inputs
wit.ReadWrite stream
flags ^^^ 1uy
else
flags
let flags =
if (flags &&& 8uy) <> 0uy then //MWEB extension tx flag
(* The MWEB flag is present, but currently no MWEB data is supported.
* This fix just prevent from throwing exception bellow so cannonical litecoin transaction can be read
*)
flags ^^^ 8uy
else
flags
if ((flags &&& 1uy) <> 0uy) && witSupported then
// The witness flag is present, and we support witnesses.
let wit = Witness self.Inputs
wit.ReadWrite stream
flags <- flags ^^^ 1uy

if (flags &&& mwebExtensionTxFlag) <> 0uy then
let version = ref 0uy
stream.ReadWrite version
self.MimbleWimbleTransaction <- Some(MimbleWimble.Transaction.Read stream)
flags <- flags ^^^ 8uy

if flags <> 0uy then
// Unknown flag in the serialization
Expand All @@ -118,6 +117,12 @@ type LitecoinTransaction() =
else
0uy

let flags =
if self.MimbleWimbleTransaction.IsSome then
flags ||| mwebExtensionTxFlag
else
flags

let flags =
if flags <> 0uy then
// Use extended format in case witnesses are to be serialized.
Expand All @@ -135,6 +140,12 @@ type LitecoinTransaction() =
let wit = Witness self.Inputs
wit.ReadWrite stream

match self.MimbleWimbleTransaction with
| Some mwebTransaction ->
stream.ReadWrite MimbleWimble.Transaction.Version |> ignore
(mwebTransaction :> MimbleWimble.ISerializeable).Write stream
| None -> ()

override self.ReadWrite(stream: BitcoinStream) =
let witSupported =
(((uint32 stream.TransactionOptions) &&& (uint32 TransactionOptions.Witness)) <> 0u) &&
Expand Down
Loading

0 comments on commit 5f5e144

Please sign in to comment.