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

Sender from signers #1752

Merged
merged 30 commits into from
Jul 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3702164
Sender from signers
shargon Jul 7, 2020
576973b
Remove co-
shargon Jul 7, 2020
a618ebf
Merge branch 'master' into sender-from-signers
shargon Jul 8, 2020
4d70d15
Move signers outside attributes
shargon Jul 8, 2020
d775492
Fix UT and remove Signers class
shargon Jul 9, 2020
fc6a3fa
Fix UT
shargon Jul 9, 2020
ec0262e
Add FeeOnly scope
shargon Jul 9, 2020
09860a4
Remove orderBy
shargon Jul 9, 2020
f4a6205
Merge branch 'master' into sender-from-signers
erikzhang Jul 10, 2020
a726699
Remove _signersCache
erikzhang Jul 10, 2020
67dc7e3
Fix Signers
erikzhang Jul 10, 2020
881fb88
Fix WitnessScope
erikzhang Jul 10, 2020
e55dfee
Fix Sender
erikzhang Jul 10, 2020
e28cd1d
Update TransactionAttributeType.cs
erikzhang Jul 10, 2020
eb7e302
Update Wallet.cs
erikzhang Jul 10, 2020
c536c30
Fix Wallet
erikzhang Jul 10, 2020
0f5db32
Rename
erikzhang Jul 10, 2020
8a8b837
Update Wallet.cs
erikzhang Jul 10, 2020
ffc55fe
Update Wallet.cs
erikzhang Jul 10, 2020
e018eb6
Partial UT fix
shargon Jul 10, 2020
59f934d
More UT fixes
shargon Jul 10, 2020
eea80a2
Fix Sender's WitnessScope
erikzhang Jul 10, 2020
cf8ffba
Fix Wallet
erikzhang Jul 10, 2020
6b4a56b
Fix UT
shargon Jul 10, 2020
a2ae4a2
Explicit FeeOnly for DeployNativeContracts
shargon Jul 10, 2020
f09a8a5
Same order as serialization
shargon Jul 10, 2020
4870875
Test FeeOnly
shargon Jul 10, 2020
abc7999
Merge branch 'master' into sender-from-signers
shargon Jul 10, 2020
cb8f978
dotnet format
shargon Jul 10, 2020
be47d0d
format
erikzhang Jul 10, 2020
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
9 changes: 7 additions & 2 deletions src/neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,14 @@ private static Transaction DeployNativeContracts()
{
Version = 0,
Script = script,
Sender = (new[] { (byte)OpCode.PUSH1 }).ToScriptHash(),
SystemFee = 0,
Attributes = Array.Empty<TransactionAttribute>(),
Attributes = new TransactionAttribute[]
{
new Signer()
{
Account = (new[] { (byte)OpCode.PUSH1 }).ToScriptHash()
}
},
Witnesses = new[]
{
new Witness
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Neo.Network.P2P.Payloads
{
public class Cosigner : TransactionAttribute
public class Signer : TransactionAttribute
{
// This limits maximum number of AllowedContracts or AllowedGroups here
private const int MaxSubitems = 16;
Expand All @@ -16,7 +16,7 @@ public class Cosigner : TransactionAttribute
public UInt160[] AllowedContracts;
public ECPoint[] AllowedGroups;

public override TransactionAttributeType Type => TransactionAttributeType.Cosigner;
public override TransactionAttributeType Type => TransactionAttributeType.Signer;
public override bool AllowMultiple => true;

public override int Size => base.Size +
Expand Down
20 changes: 6 additions & 14 deletions src/neo/Network/P2P/Payloads/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class Transaction : IEquatable<Transaction>, IInventory, IInteroperable

private byte version;
private uint nonce;
private UInt160 sender;
private long sysfee;
private long netfee;
private uint validUntilBlock;
Expand All @@ -47,11 +46,12 @@ public class Transaction : IEquatable<Transaction>, IInventory, IInteroperable
public TransactionAttribute[] Attributes
{
get => attributes;
set { attributes = value; _cosigners = null; _hash = null; _size = 0; }
set { attributes = value; _signers = null; _hash = null; _size = 0; }
}

private Dictionary<UInt160, Cosigner> _cosigners;
public IReadOnlyDictionary<UInt160, Cosigner> Cosigners => _cosigners ??= attributes.OfType<Cosigner>().ToDictionary(p => p.Account);
private Dictionary<UInt160, Signer> _signers;
public IReadOnlyDictionary<UInt160, Signer> Signers => _signers ??= attributes.OfType<Signer>().ToDictionary(p => p.Account);
shargon marked this conversation as resolved.
Show resolved Hide resolved
public UInt160 Sender => attributes.OfType<Signer>().Select(p => p.Account).First();

/// <summary>
/// The <c>NetworkFee</c> for the transaction divided by its <c>Size</c>.
Expand Down Expand Up @@ -95,12 +95,6 @@ public byte[] Script
set { script = value; _hash = null; _size = 0; }
}

public UInt160 Sender
{
get => sender;
set { sender = value; _hash = null; }
}

private int _size;
public int Size
{
Expand Down Expand Up @@ -173,7 +167,6 @@ public void DeserializeUnsigned(BinaryReader reader)
Version = reader.ReadByte();
if (Version > 0) throw new FormatException();
Nonce = reader.ReadUInt32();
Sender = reader.ReadSerializable<UInt160>();
SystemFee = reader.ReadInt64();
if (SystemFee < 0) throw new FormatException();
NetworkFee = reader.ReadInt64();
Expand All @@ -183,7 +176,7 @@ public void DeserializeUnsigned(BinaryReader reader)
Attributes = DeserializeAttributes(reader).ToArray();
try
{
_ = Cosigners;
_ = Signers;
}
catch (ArgumentException)
{
Expand Down Expand Up @@ -217,8 +210,7 @@ public override int GetHashCode()

public UInt160[] GetScriptHashesForVerifying(StoreView snapshot)
{
var hashes = new HashSet<UInt160>(Cosigners.Keys) { Sender };
return hashes.OrderBy(p => p).ToArray();
return Signers.Keys.OrderBy(p => p).ToArray();
}

void ISerializable.Serialize(BinaryWriter writer)
Expand Down
4 changes: 2 additions & 2 deletions src/neo/Network/P2P/Payloads/TransactionAttributeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Neo.Network.P2P.Payloads
{
public enum TransactionAttributeType : byte
{
[ReflectionCache(typeof(Cosigner))]
Cosigner = 0x01
[ReflectionCache(typeof(Signer))]
Signer = 0x01
}
}
14 changes: 7 additions & 7 deletions src/neo/SmartContract/ApplicationEngine.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,23 @@ internal bool CheckWitnessInternal(UInt160 hash)
{
if (ScriptContainer is Transaction tx)
{
if (!tx.Cosigners.TryGetValue(hash, out Cosigner cosigner)) return false;
if (cosigner.Scopes == WitnessScope.Global) return true;
if (cosigner.Scopes.HasFlag(WitnessScope.CalledByEntry))
if (!tx.Signers.TryGetValue(hash, out Signer signer)) return false;
if (signer.Scopes == WitnessScope.Global) return true;
if (signer.Scopes.HasFlag(WitnessScope.CalledByEntry))
{
if (CallingScriptHash == EntryScriptHash)
return true;
}
if (cosigner.Scopes.HasFlag(WitnessScope.CustomContracts))
if (signer.Scopes.HasFlag(WitnessScope.CustomContracts))
{
if (cosigner.AllowedContracts.Contains(CurrentScriptHash))
if (signer.AllowedContracts.Contains(CurrentScriptHash))
return true;
}
if (cosigner.Scopes.HasFlag(WitnessScope.CustomGroups))
if (signer.Scopes.HasFlag(WitnessScope.CustomGroups))
{
var contract = Snapshot.Contracts[CallingScriptHash];
// check if current group is the required one
if (contract.Manifest.Groups.Select(p => p.PubKey).Intersect(cosigner.AllowedGroups).Any())
if (contract.Manifest.Groups.Select(p => p.PubKey).Intersect(signer.AllowedGroups).Any())
return true;
}
return false;
Expand Down
28 changes: 25 additions & 3 deletions src/neo/Wallets/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Numerics;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Security.Policy;
using System.Text;
using static Neo.Wallets.Helper;
using ECPoint = Neo.Cryptography.ECC.ECPoint;
Expand Down Expand Up @@ -278,7 +279,7 @@ public Transaction MakeTransaction(TransferOutput[] outputs, UInt160 from = null
balances_gas = accounts.Select(p => (Account: p, Value: NativeContract.GAS.BalanceOf(snapshot, p))).Where(p => p.Value.Sign > 0).ToList();

var cosigners = cosignerList.Select(p =>
new Cosigner()
new Signer()
{
// default access for transfers should be valid only for first invocation
Scopes = WitnessScope.CalledByEntry,
Expand Down Expand Up @@ -314,14 +315,35 @@ private Transaction MakeTransaction(StoreView snapshot, byte[] script, Transacti
Random rand = new Random();
foreach (var (account, value) in balances_gas)
{
var attr = new List<TransactionAttribute>();

if (!attributes.OfType<Signer>().Any(u => u.Account == account))
{
// Add a new signer

attr.Add(new Signer()
{
Account = account,
Scopes = WitnessScope.CalledByEntry
});
attr.AddRange(attributes);
}
else
{
// Use the first signer of this account

var ac = attributes.OfType<Signer>().First(u => u.Account == account);
attr.Add(ac);
attr.AddRange(attributes.Where(u => u != ac));
}

Transaction tx = new Transaction
{
Version = 0,
Nonce = (uint)rand.Next(),
Script = script,
Sender = account,
ValidUntilBlock = snapshot.Height + Transaction.MaxValidUntilBlockIncrement,
Attributes = attributes,
Attributes = attr.ToArray(),
};

// will try to execute 'transfer' script to check if it works
Expand Down
2 changes: 0 additions & 2 deletions tests/neo.UnitTests/Consensus/UT_ConsensusContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ private Transaction CreateTransactionWithSize(int v)
NetworkFee = 0,
Nonce = (uint)Environment.TickCount,
Script = new byte[0],
Sender = UInt160.Zero,
SystemFee = 0,
ValidUntilBlock = (uint)r.Next(),
Version = 0,
Expand All @@ -148,7 +147,6 @@ private Transaction CreateTransactionWithSytemFee(long fee)
NetworkFee = 0,
Nonce = (uint)Environment.TickCount,
Script = new byte[0],
Sender = UInt160.Zero,
SystemFee = fee,
ValidUntilBlock = int.MaxValue,
Version = 0,
Expand Down
1 change: 0 additions & 1 deletion tests/neo.UnitTests/Cryptography/UT_Cryptography_Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ public void TestTest()
Transaction tx = new Transaction
{
Script = TestUtils.GetByteArray(32, 0x42),
Sender = UInt160.Zero,
SystemFee = 4200000000,
Attributes = Array.Empty<TransactionAttribute>(),
Witnesses = new[]
Expand Down
2 changes: 0 additions & 2 deletions tests/neo.UnitTests/IO/Caching/UT_RelayCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public void TestGetKeyForItem()
{
Version = 0,
Nonce = 1,
Sender = UInt160.Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff01"),
SystemFee = 0,
NetworkFee = 0,
ValidUntilBlock = 100,
Expand All @@ -33,7 +32,6 @@ public void TestGetKeyForItem()
Witnesses = new Witness[0]
};
relayCache.Add(tx);

relayCache.Contains(tx).Should().BeTrue();
relayCache.TryGet(tx.Hash, out IInventory tmp).Should().BeTrue();
(tmp is Transaction).Should().BeTrue();
Expand Down
1 change: 0 additions & 1 deletion tests/neo.UnitTests/Ledger/UT_Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ public void TestInvalidTransactionInPersist()
NetworkFee = 0,
Nonce = (uint)Environment.TickCount,
Script = new byte[] { 1 },
Sender = UInt160.Zero,
SystemFee = 0,
ValidUntilBlock = Blockchain.GenesisBlock.Index + 1,
Version = 0,
Expand Down
6 changes: 2 additions & 4 deletions tests/neo.UnitTests/Ledger/UT_MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ private Transaction CreateTransactionWithFee(long fee)
mock.Setup(p => p.VerifyForEachBlock(It.IsAny<StoreView>(), It.IsAny<BigInteger>())).Returns(VerifyResult.Succeed);
mock.Setup(p => p.Verify(It.IsAny<StoreView>(), It.IsAny<BigInteger>())).Returns(VerifyResult.Succeed);
mock.Object.Script = randomBytes;
mock.Object.Sender = UInt160.Zero;
mock.Object.NetworkFee = fee;
mock.Object.Attributes = Array.Empty<TransactionAttribute>();
mock.Object.Witnesses = new[]
Expand All @@ -97,11 +96,10 @@ private Transaction CreateTransactionWithFeeAndBalanceVerify(long fee)
var randomBytes = new byte[16];
random.NextBytes(randomBytes);
Mock<Transaction> mock = new Mock<Transaction>();
UInt160 sender = UInt160.Zero;
mock.Setup(p => p.VerifyForEachBlock(It.IsAny<StoreView>(), It.IsAny<BigInteger>())).Returns((StoreView snapshot, BigInteger amount) => NativeContract.GAS.BalanceOf(snapshot, sender) >= amount + fee ? VerifyResult.Succeed : VerifyResult.InsufficientFunds);
mock.Setup(p => p.VerifyForEachBlock(It.IsAny<StoreView>(), It.IsAny<BigInteger>())).Returns((StoreView snapshot, BigInteger amount) =>
NativeContract.GAS.BalanceOf(snapshot, UInt160.Zero) >= amount + fee ? VerifyResult.Succeed : VerifyResult.InsufficientFunds);
mock.Setup(p => p.Verify(It.IsAny<StoreView>(), It.IsAny<BigInteger>())).Returns(VerifyResult.Succeed);
mock.Object.Script = randomBytes;
mock.Object.Sender = sender;
mock.Object.NetworkFee = fee;
mock.Object.Attributes = Array.Empty<TransactionAttribute>();
mock.Object.Witnesses = new[]
Expand Down
3 changes: 1 addition & 2 deletions tests/neo.UnitTests/Ledger/UT_PoolItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ public static Transaction GenerateTx(long networkFee, int size, byte[] overrideS
{
Nonce = (uint)TestRandom.Next(),
Script = overrideScriptBytes ?? new byte[0],
Sender = UInt160.Zero,
NetworkFee = networkFee,
Attributes = Array.Empty<TransactionAttribute>(),
Witnesses = new[]
Expand All @@ -132,7 +131,7 @@ public static Transaction GenerateTx(long networkFee, int size, byte[] overrideS
};

tx.Attributes.Length.Should().Be(0);
tx.Cosigners.Count.Should().Be(0);
tx.Signers.Count.Should().Be(0);

int diff = size - tx.Size;
if (diff < 0) throw new ArgumentException();
Expand Down
1 change: 0 additions & 1 deletion tests/neo.UnitTests/Ledger/UT_SendersFeeMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ private Transaction CreateTransactionWithFee(long networkFee, long systemFee)
mock.Setup(p => p.VerifyForEachBlock(It.IsAny<StoreView>(), It.IsAny<BigInteger>())).Returns(VerifyResult.Succeed);
mock.Setup(p => p.Verify(It.IsAny<StoreView>(), It.IsAny<BigInteger>())).Returns(VerifyResult.Succeed);
mock.Object.Script = randomBytes;
mock.Object.Sender = UInt160.Zero;
mock.Object.NetworkFee = networkFee;
mock.Object.SystemFee = systemFee;
mock.Object.Attributes = Array.Empty<TransactionAttribute>();
Expand Down
14 changes: 7 additions & 7 deletions tests/neo.UnitTests/Ledger/UT_TrimmedBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public static TrimmedBlock GetTrimmedBlockWithNoTransaction()
public void TestGetIsBlock()
{
TrimmedBlock block = GetTrimmedBlockWithNoTransaction();
block.Hashes = new UInt256[] { TestUtils.GetTransaction().Hash };
block.Hashes = new UInt256[] { TestUtils.GetTransaction(UInt160.Zero).Hash };
block.IsBlock.Should().BeTrue();
}

[TestMethod]
public void TestGetBlock()
{
var snapshot = Blockchain.Singleton.GetSnapshot();
var tx1 = TestUtils.GetTransaction();
var tx1 = TestUtils.GetTransaction(UInt160.Zero);
tx1.Script = new byte[] { 0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,
Expand All @@ -53,7 +53,7 @@ public void TestGetBlock()
Transaction = tx1,
BlockIndex = 1
};
var tx2 = TestUtils.GetTransaction();
var tx2 = TestUtils.GetTransaction(UInt160.Zero);
tx2.Script = new byte[] { 0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,
Expand Down Expand Up @@ -89,15 +89,15 @@ public void TestGetHeader()
public void TestGetSize()
{
TrimmedBlock tblock = GetTrimmedBlockWithNoTransaction();
tblock.Hashes = new UInt256[] { TestUtils.GetTransaction().Hash };
tblock.Hashes = new UInt256[] { TestUtils.GetTransaction(UInt160.Zero).Hash };
tblock.Size.Should().Be(146);
}

[TestMethod]
public void TestDeserialize()
{
TrimmedBlock tblock = GetTrimmedBlockWithNoTransaction();
tblock.Hashes = new UInt256[] { TestUtils.GetTransaction().Hash };
tblock.Hashes = new UInt256[] { TestUtils.GetTransaction(UInt160.Zero).Hash };
var newBlock = new TrimmedBlock();
using (MemoryStream ms = new MemoryStream(1024))
using (BinaryWriter writer = new BinaryWriter(ms))
Expand All @@ -119,7 +119,7 @@ public void TestDeserialize()
public void TestClone()
{
TrimmedBlock tblock = GetTrimmedBlockWithNoTransaction();
tblock.Hashes = new UInt256[] { TestUtils.GetTransaction().Hash };
tblock.Hashes = new UInt256[] { TestUtils.GetTransaction(UInt160.Zero).Hash };
ICloneable<TrimmedBlock> cloneable = tblock;
var clonedBlock = cloneable.Clone();
clonedBlock.ToJson().ToString().Should().Be(tblock.ToJson().ToString());
Expand All @@ -129,7 +129,7 @@ public void TestClone()
public void TestFromReplica()
{
TrimmedBlock tblock = GetTrimmedBlockWithNoTransaction();
tblock.Hashes = new UInt256[] { TestUtils.GetTransaction().Hash };
tblock.Hashes = new UInt256[] { TestUtils.GetTransaction(UInt160.Zero).Hash };
ICloneable<TrimmedBlock> cloneable = new TrimmedBlock();
cloneable.FromReplica(tblock);
((TrimmedBlock)cloneable).ToJson().ToString().Should().Be(tblock.ToJson().ToString());
Expand Down
8 changes: 4 additions & 4 deletions tests/neo.UnitTests/Network/P2P/Payloads/UT_Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void Size_Get_1_Transaction()

uut.Transactions = new[]
{
TestUtils.GetTransaction()
TestUtils.GetTransaction(UInt160.Zero)
};

uut.Size.Should().Be(165);
Expand All @@ -70,9 +70,9 @@ public void Size_Get_3_Transaction()

uut.Transactions = new[]
{
TestUtils.GetTransaction(),
TestUtils.GetTransaction(),
TestUtils.GetTransaction()
TestUtils.GetTransaction(UInt160.Zero),
TestUtils.GetTransaction(UInt160.Zero),
TestUtils.GetTransaction(UInt160.Zero)
};

uut.Size.Should().Be(267);
Expand Down
Loading