Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Add] Transaction Builder #3477

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/Neo/Builders/TransactionAttributesBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// TransactionAttributesBuilder.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Network.P2P.Payloads;
using System;
using System.Linq;

namespace Neo.Builders
{
public sealed class TransactionAttributesBuilder
{
private TransactionAttribute[] _attributes = [];

private TransactionAttributesBuilder() { }

public static TransactionAttributesBuilder CreateEmpty()
{
return new TransactionAttributesBuilder();
}

public TransactionAttributesBuilder AddConflict(Action<Conflicts> conflictAction)
{
var conflicts = new Conflicts();
conflictAction(conflicts);
_attributes = [.. _attributes, conflicts];
return this;
}

public TransactionAttributesBuilder AddOracleResponse(Action<OracleResponse> oracleResponseAction)
{
var oracleResponse = new OracleResponse();
oracleResponseAction(oracleResponse);
_attributes = [.. _attributes, oracleResponse];
return this;
}

public TransactionAttributesBuilder AddHighPriority()
{
if (_attributes.Any(a => a is HighPriorityAttribute))
throw new InvalidOperationException("HighPriority already exists in the attributes.");

var highPriority = new HighPriorityAttribute();
_attributes = [.. _attributes, highPriority];
return this;
}

public TransactionAttributesBuilder AddNotValidBefore(uint block)
{
if (_attributes.Any(a => a is NotValidBefore b && b.Height == block))
throw new InvalidOperationException($"Block {block} already exists in the attributes.");

var validUntilBlock = new NotValidBefore()
{
Height = block
};

_attributes = [.. _attributes, validUntilBlock];
return this;
}

public TransactionAttribute[] Build()
{
return _attributes;
}
}
}
108 changes: 108 additions & 0 deletions src/Neo/Builders/TransactionBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// TransactionBuilder.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Network.P2P.Payloads;
using Neo.VM;
using System;

namespace Neo.Builders
{
public sealed class TransactionBuilder
{

shargon marked this conversation as resolved.
Show resolved Hide resolved
private byte _version = 0;
private uint _nonce = (uint)new Random().Next();
private uint _systemFee = 0;
private uint _networkFee = 0;
private uint _validUntilBlock = 0;
private byte[] _script = [];
private TransactionAttribute[] _attributes = [];
private Signer[] _signers = [];

Check warning on line 28 in src/Neo/Builders/TransactionBuilder.cs

View workflow job for this annotation

GitHub Actions / Format

Make field readonly

Check warning on line 28 in src/Neo/Builders/TransactionBuilder.cs

View workflow job for this annotation

GitHub Actions / Format

Make field readonly
private Witness[] _witnesses = [];

private TransactionBuilder() { }

public static TransactionBuilder CreateEmpty()
{
return new TransactionBuilder();
}

public TransactionBuilder Version(byte version)
{
_version = version;
return this;
}

public TransactionBuilder Nonce(uint nonce)
{
_nonce = nonce;
return this;
}

public TransactionBuilder SystemFee(uint systemFee)
{
_systemFee = systemFee;
return this;
}

public TransactionBuilder NetworkFee(uint networkFee)
{
_networkFee = networkFee;
return this;
}

public TransactionBuilder ValidUntil(uint blockIndex)
{
_validUntilBlock = blockIndex;
return this;
}

public TransactionBuilder AttachSystem(Action<ScriptBuilder> scriptBuilder)
{
var sb = new ScriptBuilder();
scriptBuilder(sb);
_script = sb.ToArray();
return this;
}

public TransactionBuilder AddAttributes(Action<TransactionAttributesBuilder> transactionAttributeBuilder)
{
var ab = TransactionAttributesBuilder.CreateEmpty();
transactionAttributeBuilder(ab);
_attributes = ab.Build();
return this;
}

public TransactionBuilder AddWitness(Action<WitnessBuilder> witnessBuilder)
{
var wb = WitnessBuilder.CreateEmpty();
witnessBuilder(wb);
_witnesses = [.. _witnesses, wb.Build()];
return this;
}

public Transaction Build()
{
return new Transaction()
{
Version = _version,
Nonce = _nonce,
SystemFee = _systemFee,
NetworkFee = _networkFee,
ValidUntilBlock = _validUntilBlock,
Script = _script,
Attributes = _attributes,
Signers = _signers,
Witnesses = _witnesses
};
}
}
}
71 changes: 71 additions & 0 deletions src/Neo/Builders/WitnessBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// WitnessBuilder.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Cryptography.ECC;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract;
using Neo.VM;
using System;

namespace Neo.Builders
{
public sealed class WitnessBuilder
{
private byte[] _invocationScript = [];
private byte[] _verificationScript = [];

private WitnessBuilder() { }

public static WitnessBuilder CreateEmpty()
{
return new WitnessBuilder();
}

public WitnessBuilder AddInvocation(Action<ScriptBuilder> scriptBuilder)
{
var sb = new ScriptBuilder();
scriptBuilder(sb);
_invocationScript = sb.ToArray();
return this;
}

public WitnessBuilder AddSigner(Action<ScriptBuilder> scriptBuilder)
{
var sb = new ScriptBuilder();
scriptBuilder(sb);
_verificationScript = sb.ToArray();
return this;
}

public WitnessBuilder AddSigner(Action<Contract> contract)
{
var c = new Contract();
contract(c);
_verificationScript = c.Script;
return this;
}

public WitnessBuilder AddSigner(ECPoint publicKey)
{
_verificationScript = Contract.CreateSignatureRedeemScript(publicKey);
return this;
}

public Witness Build()
{
return new Witness()
{
InvocationScript = _invocationScript,
VerificationScript = _verificationScript
};
}
}
}
85 changes: 85 additions & 0 deletions tests/Neo.UnitTests/Builders/UT_TransactionBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// UT_TransactionBuilder.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Neo.UnitTests.Builders
{
[TestClass]
public class UT_TransactionBuilder
{
[TestMethod]
public void TestVersion()
{
byte expectedVersion = 1;
var tx = TransactionBuilder.CreateEmpty()
.Version(expectedVersion)
.Build();

Assert.AreEqual(expectedVersion, tx.Version);
Assert.IsNotNull(tx.Hash);
}

[TestMethod]
public void TestNonce()
{
var expectedNonce = (uint)Random.Shared.Next();
var tx = TransactionBuilder.CreateEmpty()
.Nonce(expectedNonce)
.Build();

Assert.AreEqual(expectedNonce, tx.Nonce);
Assert.IsNotNull(tx.Hash);
}

[TestMethod]
public void TestSystemFee()
{
var expectedSystemFee = (uint)Random.Shared.Next();
var tx = TransactionBuilder.CreateEmpty()
.SystemFee(expectedSystemFee)
.Build();

Assert.AreEqual(expectedSystemFee, tx.SystemFee);
Assert.IsNotNull(tx.Hash);
}

[TestMethod]
public void TestNetworkFee()
{
var expectedNetworkFee = (uint)Random.Shared.Next();
var tx = TransactionBuilder.CreateEmpty()
.NetworkFee(expectedNetworkFee)
.Build();

Assert.AreEqual(expectedNetworkFee, tx.NetworkFee);
Assert.IsNotNull(tx.Hash);
}

[TestMethod]
public void TestValidUntilBlock()
{
var expectedValidUntilBlock = (uint)Random.Shared.Next();
var tx = TransactionBuilder.CreateEmpty()
.ValidUntil(expectedValidUntilBlock)
.Build();

Assert.AreEqual(expectedValidUntilBlock, tx.ValidUntilBlock);
Assert.IsNotNull(tx.Hash);
}
}
}
Loading