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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
@@ -1,6 +1,6 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// Conflicts.cs file belongs to the neo project and is free
// 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
Expand All @@ -17,7 +17,7 @@

namespace Neo.Network.P2P.Payloads
{
public class Conflicts : TransactionAttribute
public class ConflictsAttribute : TransactionAttribute
{
/// <summary>
/// Indicates the conflict transaction hash.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// NotValidBefore.cs file belongs to the neo project and is free
// 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
Expand All @@ -17,7 +17,7 @@

namespace Neo.Network.P2P.Payloads
{
public class NotValidBefore : TransactionAttribute
public class NotValidBeforeAttribute : TransactionAttribute
{
/// <summary>
/// Indicates that the transaction is not valid before this height.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// OracleResponse.cs file belongs to the neo project and is free
// 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
Expand All @@ -24,7 +24,7 @@ namespace Neo.Network.P2P.Payloads
/// <summary>
/// Indicates that the transaction is an oracle response.
/// </summary>
public class OracleResponse : TransactionAttribute
public class OracleResponseAttribute : TransactionAttribute
{
/// <summary>
/// Indicates the maximum size of the <see cref="Result"/> field.
Expand Down Expand Up @@ -59,7 +59,7 @@ public class OracleResponse : TransactionAttribute
sizeof(OracleResponseCode) + //ResponseCode
Result.GetVarSize(); //Result

static OracleResponse()
static OracleResponseAttribute()
{
using ScriptBuilder sb = new();
sb.EmitDynamicCall(NativeContract.Oracle.Hash, "finish");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Neo.Network.P2P.Payloads
{
/// <summary>
/// Represents the type of a <see cref="TransactionAttribute"/>.
/// Represents the type of <see cref="TransactionAttribute"/>.
/// </summary>
public enum TransactionAttributeType : byte
{
Expand All @@ -27,19 +27,19 @@ public enum TransactionAttributeType : byte
/// <summary>
/// Indicates that the transaction is an oracle response.
/// </summary>
[ReflectionCache(typeof(OracleResponse))]
[ReflectionCache(typeof(OracleResponseAttribute))]
OracleResponse = 0x11,

/// <summary>
/// Indicates that the transaction is not valid before <see cref="NotValidBefore.Height"/>.
/// Indicates that the transaction is not valid before <see cref="NotValidBeforeAttribute.Height"/>.
/// </summary>
[ReflectionCache(typeof(NotValidBefore))]
[ReflectionCache(typeof(NotValidBeforeAttribute))]
NotValidBefore = 0x20,

/// <summary>
/// Indicates that the transaction conflicts with <see cref="Conflicts.Hash"/>.
/// Indicates that the transaction conflicts with <see cref="ConflictsAttribute.Hash"/>.
/// </summary>
[ReflectionCache(typeof(Conflicts))]
[ReflectionCache(typeof(ConflictsAttribute))]
Conflicts = 0x21
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,14 @@ protected internal bool CheckWitnessInternal(UInt160 hash)
if (ScriptContainer is Transaction tx)
{
Signer[] signers;
OracleResponse response = tx.GetAttribute<OracleResponse>();
if (response is null)
OracleResponseAttribute responseAttribute = tx.GetAttribute<OracleResponseAttribute>();
if (responseAttribute is null)
{
signers = tx.Signers;
}
else
{
OracleRequest request = NativeContract.Oracle.GetRequest(SnapshotCache, response.Id);
OracleRequest request = NativeContract.Oracle.GetRequest(SnapshotCache, responseAttribute.Id);
signers = NativeContract.Ledger.GetTransaction(SnapshotCache, request.OriginalTxid).Signers;
}
Signer signer = signers.FirstOrDefault(p => p.Account.Equals(hash));
Expand Down
2 changes: 1 addition & 1 deletion src/Neo/SmartContract/Native/LedgerContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal override ContractTask OnPersistAsync(ApplicationEngine engine)

// Store transaction's conflicits.
var conflictingSigners = tx.Transaction.Signers.Select(s => s.Account);
foreach (var attr in tx.Transaction.GetAttributes<Conflicts>())
foreach (var attr in tx.Transaction.GetAttributes<ConflictsAttribute>())
{
engine.SnapshotCache.GetAndChange(CreateStorageKey(Prefix_Transaction).Add(attr.Hash), () => new StorageItem(new TransactionState())).FromReplica(new StorageItem(new TransactionState() { BlockIndex = engine.PersistingBlock.Index }));
foreach (var signer in conflictingSigners)
Expand Down
28 changes: 14 additions & 14 deletions src/Neo/SmartContract/Native/OracleContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,21 @@ private ContractTask Finish(ApplicationEngine engine)
if (engine.InvocationStack.Count != 2) throw new InvalidOperationException();
if (engine.GetInvocationCounter() != 1) throw new InvalidOperationException();
Transaction tx = (Transaction)engine.ScriptContainer;
OracleResponse response = tx.GetAttribute<OracleResponse>();
if (response == null) throw new ArgumentException("Oracle response was not found");
OracleRequest request = GetRequest(engine.SnapshotCache, response.Id);
OracleResponseAttribute responseAttribute = tx.GetAttribute<OracleResponseAttribute>();
if (responseAttribute == null) throw new ArgumentException("Oracle response was not found");
OracleRequest request = GetRequest(engine.SnapshotCache, responseAttribute.Id);
if (request == null) throw new ArgumentException("Oracle request was not found");
engine.SendNotification(Hash, "OracleResponse", new VM.Types.Array(engine.ReferenceCounter) { response.Id, request.OriginalTxid.ToArray() });
engine.SendNotification(Hash, "OracleResponse", new VM.Types.Array(engine.ReferenceCounter) { responseAttribute.Id, request.OriginalTxid.ToArray() });
StackItem userData = BinarySerializer.Deserialize(request.UserData, engine.Limits, engine.ReferenceCounter);
return engine.CallFromNativeContractAsync(Hash, request.CallbackContract, request.CallbackMethod, request.Url, userData, (int)response.Code, response.Result);
return engine.CallFromNativeContractAsync(Hash, request.CallbackContract, request.CallbackMethod, request.Url, userData, (int)responseAttribute.Code, responseAttribute.Result);
}

private UInt256 GetOriginalTxid(ApplicationEngine engine)
{
Transaction tx = (Transaction)engine.ScriptContainer;
OracleResponse response = tx.GetAttribute<OracleResponse>();
if (response is null) return tx.Hash;
OracleRequest request = GetRequest(engine.SnapshotCache, response.Id);
OracleResponseAttribute responseAttribute = tx.GetAttribute<OracleResponseAttribute>();
if (responseAttribute is null) return tx.Hash;
OracleRequest request = GetRequest(engine.SnapshotCache, responseAttribute.Id);
return request.OriginalTxid;
}

Expand Down Expand Up @@ -150,26 +150,26 @@ internal override async ContractTask PostPersistAsync(ApplicationEngine engine)
foreach (Transaction tx in engine.PersistingBlock.Transactions)
{
//Filter the response transactions
OracleResponse response = tx.GetAttribute<OracleResponse>();
if (response is null) continue;
OracleResponseAttribute responseAttribute = tx.GetAttribute<OracleResponseAttribute>();
if (responseAttribute is null) continue;

//Remove the request from storage
StorageKey key = CreateStorageKey(Prefix_Request).AddBigEndian(response.Id);
StorageKey key = CreateStorageKey(Prefix_Request).AddBigEndian(responseAttribute.Id);
OracleRequest request = engine.SnapshotCache.TryGet(key)?.GetInteroperable<OracleRequest>();
if (request == null) continue;
engine.SnapshotCache.Delete(key);

//Remove the id from IdList
key = CreateStorageKey(Prefix_IdList).Add(GetUrlHash(request.Url));
IdList list = engine.SnapshotCache.GetAndChange(key).GetInteroperable<IdList>();
if (!list.Remove(response.Id)) throw new InvalidOperationException();
if (!list.Remove(responseAttribute.Id)) throw new InvalidOperationException();
if (list.Count == 0) engine.SnapshotCache.Delete(key);

//Mint GAS for oracle nodes
nodes ??= RoleManagement.GetDesignatedByRole(engine.SnapshotCache, Role.Oracle, engine.PersistingBlock.Index).Select(p => (Contract.CreateSignatureRedeemScript(p).ToScriptHash(), BigInteger.Zero)).ToArray();
if (nodes.Length > 0)
{
int index = (int)(response.Id % (ulong)nodes.Length);
int index = (int)(responseAttribute.Id % (ulong)nodes.Length);
nodes[index].GAS += GetPrice(engine.SnapshotCache);
}
}
Expand Down Expand Up @@ -231,7 +231,7 @@ private async ContractTask Request(ApplicationEngine engine, string url, string
private bool Verify(ApplicationEngine engine)
{
Transaction tx = (Transaction)engine.ScriptContainer;
return tx?.GetAttribute<OracleResponse>() != null;
return tx?.GetAttribute<OracleResponseAttribute>() != null;
}

private class IdList : InteroperableList<ulong>
Expand Down
4 changes: 2 additions & 2 deletions src/Plugins/DBFTPlugin/Consensus/ConsensusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ private bool AddTransaction(Transaction tx, bool verify)
VerifyResult result;

// Firstly, check whether tx has Conlicts attribute with the hash of one of the context's transactions.
foreach (var h in tx.GetAttributes<Conflicts>().Select(attr => attr.Hash))
foreach (var h in tx.GetAttributes<ConflictsAttribute>().Select(attr => attr.Hash))
{
if (context.TransactionHashes.Contains(h))
{
Expand All @@ -280,7 +280,7 @@ private bool AddTransaction(Transaction tx, bool verify)
// After that, check whether context's transactions have Conflicts attribute with tx's hash.
foreach (var pooledTx in context.Transactions.Values)
{
if (pooledTx.GetAttributes<Conflicts>().Select(attr => attr.Hash).Contains(tx.Hash))
if (pooledTx.GetAttributes<ConflictsAttribute>().Select(attr => attr.Hash).Contains(tx.Hash))
{
result = VerifyResult.HasConflicts;
Log($"Rejected tx: {tx.Hash}, {result}{Environment.NewLine}{tx.ToArray().ToHexString()}", LogLevel.Warning);
Expand Down
Loading
Loading