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

[Neo Core Style] update smartcontract struct and name #3488

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions src/Neo.CLI/CLI/MainService.Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,12 @@ public void OnShowTransactionCommand(UInt256 hash)
{
switch (attribute)
{
case Conflicts c:
case ConflictsAttribute c:
ConsoleHelper.Info("", " Type: ", $"{c.Type}");
ConsoleHelper.Info("", " Hash: ", $"{c.Hash}");
ConsoleHelper.Info("", " Size: ", $"{c.Size} Byte(s)");
break;
case OracleResponse o:
case OracleResponseAttribute o:
ConsoleHelper.Info("", " Type: ", $"{o.Type}");
ConsoleHelper.Info("", " Id: ", $"{o.Id}");
ConsoleHelper.Info("", " Code: ", $"{o.Code}");
Expand All @@ -208,7 +208,7 @@ public void OnShowTransactionCommand(UInt256 hash)
case HighPriorityAttribute p:
ConsoleHelper.Info("", " Type: ", $"{p.Type}");
break;
case NotValidBefore n:
case NotValidBeforeAttribute n:
ConsoleHelper.Info("", " Type: ", $"{n.Type}");
ConsoleHelper.Info("", " Height: ", $"{n.Height}");
break;
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.CLI/CLI/MainService.Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ private void OnCancelCommand(UInt256 txid, UInt160? sender = null, UInt160[]? si
return;
}

var conflict = new TransactionAttribute[] { new Conflicts() { Hash = txid } };
var conflict = new TransactionAttribute[] { new ConflictsAttribute() { Hash = txid } };
Signer[] signers = Array.Empty<Signer>();
if (sender != null)
{
Expand Down
12 changes: 6 additions & 6 deletions src/Neo/Ledger/MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ internal VerifyResult TryAdd(Transaction tx, DataCache snapshot)
VerificationContext.RemoveTransaction(conflict.Tx);
}
removedTransactions = conflictsToBeRemoved.Select(itm => itm.Tx).ToList();
foreach (var attr in tx.GetAttributes<Conflicts>())
foreach (var attr in tx.GetAttributes<ConflictsAttribute>())
{
if (!_conflicts.TryGetValue(attr.Hash, out var pooled))
{
Expand Down Expand Up @@ -371,7 +371,7 @@ private bool CheckConflicts(Transaction tx, out List<PoolItem> conflictsList)
}
}
// Step 2: check if unsorted transactions were in `tx`'s Conflicts attributes.
foreach (var hash in tx.GetAttributes<Conflicts>().Select(p => p.Hash))
foreach (var hash in tx.GetAttributes<ConflictsAttribute>().Select(p => p.Hash))
{
if (_unsortedTransactions.TryGetValue(hash, out var unsortedTx))
{
Expand Down Expand Up @@ -429,7 +429,7 @@ private bool TryRemoveVerified(UInt256 hash, [MaybeNullWhen(false)] out PoolItem
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void RemoveConflictsOfVerified(PoolItem item)
{
foreach (var h in item.Tx.GetAttributes<Conflicts>().Select(attr => attr.Hash))
foreach (var h in item.Tx.GetAttributes<ConflictsAttribute>().Select(attr => attr.Hash))
{
if (_conflicts.TryGetValue(h, out var conflicts))
{
Expand Down Expand Up @@ -483,7 +483,7 @@ internal void UpdatePoolForBlockPersisted(Block block, DataCache snapshot)
{
if (!TryRemoveVerified(tx.Hash, out _)) TryRemoveUnVerified(tx.Hash, out _);
var conflictingSigners = tx.Signers.Select(s => s.Account);
foreach (var h in tx.GetAttributes<Conflicts>().Select(a => a.Hash))
foreach (var h in tx.GetAttributes<ConflictsAttribute>().Select(a => a.Hash))
{
if (conflicts.TryGetValue(h, out var signersList))
{
Expand All @@ -501,7 +501,7 @@ internal void UpdatePoolForBlockPersisted(Block block, DataCache snapshot)
var stale = new List<UInt256>();
foreach (var item in _sortedTransactions)
{
if ((conflicts.TryGetValue(item.Tx.Hash, out var signersList) && signersList.Intersect(item.Tx.Signers.Select(s => s.Account)).Any()) || item.Tx.GetAttributes<Conflicts>().Select(a => a.Hash).Intersect(persisted).Any())
if ((conflicts.TryGetValue(item.Tx.Hash, out var signersList) && signersList.Intersect(item.Tx.Signers.Select(s => s.Account)).Any()) || item.Tx.GetAttributes<ConflictsAttribute>().Select(a => a.Hash).Intersect(persisted).Any())
{
stale.Add(item.Tx.Hash);
conflictingItems.Add(item.Tx);
Expand Down Expand Up @@ -569,7 +569,7 @@ private int ReverifyTransactions(SortedSet<PoolItem> verifiedSortedTxPool,
if (_unsortedTransactions.TryAdd(item.Tx.Hash, item))
{
verifiedSortedTxPool.Add(item);
foreach (var attr in item.Tx.GetAttributes<Conflicts>())
foreach (var attr in item.Tx.GetAttributes<ConflictsAttribute>())
{
if (!_conflicts.TryGetValue(attr.Hash, out var pooled))
{
Expand Down
6 changes: 3 additions & 3 deletions src/Neo/Ledger/TransactionVerificationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class TransactionVerificationContext
/// <param name="tx">The verified <see cref="Transaction"/>.</param>
public void AddTransaction(Transaction tx)
{
var oracle = tx.GetAttribute<OracleResponse>();
var oracle = tx.GetAttribute<OracleResponseAttribute>();
if (oracle != null) oracleResponses.Add(oracle.Id, tx.Hash);

if (senderFee.TryGetValue(tx.Sender, out var value))
Expand All @@ -65,7 +65,7 @@ public bool CheckTransaction(Transaction tx, IEnumerable<Transaction> conflictin
expectedFee -= (conflictTx.NetworkFee + conflictTx.SystemFee);
if (balance < expectedFee) return false;

var oracle = tx.GetAttribute<OracleResponse>();
var oracle = tx.GetAttribute<OracleResponseAttribute>();
if (oracle != null && oracleResponses.ContainsKey(oracle.Id))
return false;

Expand All @@ -80,7 +80,7 @@ public void RemoveTransaction(Transaction tx)
{
if ((senderFee[tx.Sender] -= tx.SystemFee + tx.NetworkFee) == 0) senderFee.Remove(tx.Sender);

var oracle = tx.GetAttribute<OracleResponse>();
var oracle = tx.GetAttribute<OracleResponseAttribute>();
if (oracle != null) oracleResponses.Remove(oracle.Id);
}
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
using Neo.Json;
using Neo.Persistence;
using Neo.SmartContract.Native;
using System;
using System.IO;

namespace Neo.Network.P2P.Payloads
{
[Obsolete("Use ConflictsAttribute instead")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create an issue to remove this obsolete code. This issue should be included into next release.

public class Conflicts : TransactionAttribute
Copy link
Member

@shargon shargon Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before remove maybe is easier to mantain if we share the code

Suggested change
public class Conflicts : TransactionAttribute
public class Conflicts : ConflictsAttribute

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shargon thats what i said here #3488 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before remove maybe is easier to mantain if we share the code

But code is already there, keep this pr this way as much easier isn't it~~~~

{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// ConflictsAttribute.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.IO;
using Neo.Json;
using Neo.Persistence;
using Neo.SmartContract.Native;
using System.IO;

namespace Neo.Network.P2P.Payloads
{
public class ConflictsAttribute : TransactionAttribute
{
/// <summary>
/// Indicates the conflict transaction hash.
/// </summary>
public UInt256 Hash;

public override TransactionAttributeType Type => TransactionAttributeType.Conflicts;

public override bool AllowMultiple => true;

public override int Size => base.Size + Hash.Size;

protected override void DeserializeWithoutType(ref MemoryReader reader)
{
Hash = reader.ReadSerializable<UInt256>();
}

protected override void SerializeWithoutType(BinaryWriter writer)
{
writer.Write(Hash);
}

public override JObject ToJson()
{
JObject json = base.ToJson();
json["hash"] = Hash.ToString();
return json;
}

public override bool Verify(DataCache snapshot, Transaction tx)
{
// Only check if conflicting transaction is on chain. It's OK if the
// conflicting transaction was in the Conflicts attribute of some other
// on-chain transaction.
return !NativeContract.Ledger.ContainsTransaction(snapshot, Hash);
}

public override long CalculateNetworkFee(DataCache snapshot, Transaction tx)
{
return tx.Signers.Length * base.CalculateNetworkFee(snapshot, tx);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
using Neo.Json;
using Neo.Persistence;
using Neo.SmartContract.Native;
using System;
using System.IO;

namespace Neo.Network.P2P.Payloads
{
[Obsolete("Use NotValidBeforeAttribute instead")]
public class NotValidBefore : TransactionAttribute
{
/// <summary>
Expand All @@ -29,7 +31,7 @@ public class NotValidBefore : TransactionAttribute
public override bool AllowMultiple => false;

public override int Size => base.Size +
sizeof(uint); // Height.
sizeof(uint); // Height.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this may cause by my dotnet format.


protected override void DeserializeWithoutType(ref MemoryReader reader)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// NotValidBeforeAttribute.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.IO;
using Neo.Json;
using Neo.Persistence;
using Neo.SmartContract.Native;
using System.IO;

namespace Neo.Network.P2P.Payloads
{
public class NotValidBeforeAttribute : TransactionAttribute
{
/// <summary>
/// Indicates that the transaction is not valid before this height.
/// </summary>
public uint Height;

public override TransactionAttributeType Type => TransactionAttributeType.NotValidBefore;

public override bool AllowMultiple => false;

public override int Size => base.Size +
sizeof(uint); // Height.

protected override void DeserializeWithoutType(ref MemoryReader reader)
{
Height = reader.ReadUInt32();
}

protected override void SerializeWithoutType(BinaryWriter writer)
{
writer.Write(Height);
}

public override JObject ToJson()
{
JObject json = base.ToJson();
json["height"] = Height;
return json;
}

public override bool Verify(DataCache snapshot, Transaction tx)
{
var block_height = NativeContract.Ledger.CurrentIndex(snapshot);
return block_height >= Height;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace Neo.Network.P2P.Payloads
/// <summary>
/// Indicates that the transaction is an oracle response.
/// </summary>
[Obsolete("Use OracleResponseAttribute instead")]
public class OracleResponse : TransactionAttribute
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// OracleResponseAttribute.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.IO;
using Neo.Json;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using System;
using System.IO;
using System.Linq;

namespace Neo.Network.P2P.Payloads
{
/// <summary>
/// Indicates that the transaction is an oracle response.
/// </summary>
public class OracleResponseAttribute : TransactionAttribute
{
/// <summary>
/// Indicates the maximum size of the <see cref="Result"/> field.
/// </summary>
public const int MaxResultSize = ushort.MaxValue;

/// <summary>
/// Represents the fixed value of the <see cref="Transaction.Script"/> field of the oracle responding transaction.
/// </summary>
public static readonly byte[] FixedScript;

/// <summary>
/// The ID of the oracle request.
/// </summary>
public ulong Id;

/// <summary>
/// The response code for the oracle request.
/// </summary>
public OracleResponseCode Code;

/// <summary>
/// The result for the oracle request.
/// </summary>
public ReadOnlyMemory<byte> Result;

public override TransactionAttributeType Type => TransactionAttributeType.OracleResponse;
public override bool AllowMultiple => false;

public override int Size => base.Size +
sizeof(ulong) + //Id
sizeof(OracleResponseCode) + //ResponseCode
Result.GetVarSize(); //Result

static OracleResponseAttribute()
{
using ScriptBuilder sb = new();
sb.EmitDynamicCall(NativeContract.Oracle.Hash, "finish");
FixedScript = sb.ToArray();
}

protected override void DeserializeWithoutType(ref MemoryReader reader)
{
Id = reader.ReadUInt64();
Code = (OracleResponseCode)reader.ReadByte();
if (!Enum.IsDefined(typeof(OracleResponseCode), Code))
throw new FormatException();
Result = reader.ReadVarMemory(MaxResultSize);
if (Code != OracleResponseCode.Success && Result.Length > 0)
throw new FormatException();
}

protected override void SerializeWithoutType(BinaryWriter writer)
{
writer.Write(Id);
writer.Write((byte)Code);
writer.WriteVarBytes(Result.Span);
}

public override JObject ToJson()
{
JObject json = base.ToJson();
json["id"] = Id;
json["code"] = Code;
json["result"] = Convert.ToBase64String(Result.Span);
return json;
}

public override bool Verify(DataCache snapshot, Transaction tx)
{
if (tx.Signers.Any(p => p.Scopes != WitnessScope.None)) return false;
if (!tx.Script.Span.SequenceEqual(FixedScript)) return false;
OracleRequest request = NativeContract.Oracle.GetRequest(snapshot, Id);
if (request is null) return false;
if (tx.NetworkFee + tx.SystemFee != request.GasForResponse) return false;
UInt160 oracleAccount = Contract.GetBFTAddress(NativeContract.RoleManagement.GetDesignatedByRole(snapshot, Role.Oracle, NativeContract.Ledger.CurrentIndex(snapshot) + 1));
return tx.Signers.Any(p => p.Account.Equals(oracleAccount));
}
}
}
Loading
Loading