Skip to content

Commit

Permalink
3.2.1 (#2694)
Browse files Browse the repository at this point in the history
* Devhawk/getAddressVersion2 (#2689)

* Fix fee in NativeContract.Invoke (#2692)

* GetTransaction signers (#2685)

* Allow the committee to block a member from the committee (#2695)
  • Loading branch information
erikzhang committed Apr 18, 2022
1 parent 41055be commit ed68274
Show file tree
Hide file tree
Showing 20 changed files with 174 additions and 17 deletions.
9 changes: 9 additions & 0 deletions src/neo/Network/P2P/Payloads/Conditions/AndCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using Neo.IO;
using Neo.IO.Json;
using Neo.SmartContract;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -58,5 +60,12 @@ public override JObject ToJson()
json["expressions"] = Expressions.Select(p => p.ToJson()).ToArray();
return json;
}

public override StackItem ToStackItem(ReferenceCounter referenceCounter)
{
var result = (VM.Types.Array)base.ToStackItem(referenceCounter);
result.Add(new VM.Types.Array(referenceCounter, Expressions.Select(p => p.ToStackItem(referenceCounter))));
return result;
}
}
}
9 changes: 9 additions & 0 deletions src/neo/Network/P2P/Payloads/Conditions/BooleanCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

using Neo.IO.Json;
using Neo.SmartContract;
using Neo.VM;
using Neo.VM.Types;
using System.IO;

namespace Neo.Network.P2P.Payloads.Conditions
Expand Down Expand Up @@ -50,5 +52,12 @@ public override JObject ToJson()
json["expression"] = Expression;
return json;
}

public override StackItem ToStackItem(ReferenceCounter referenceCounter)
{
var result = (Array)base.ToStackItem(referenceCounter);
result.Add(Expression);
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using Neo.IO;
using Neo.IO.Json;
using Neo.SmartContract;
using Neo.VM;
using Neo.VM.Types;
using System.IO;

namespace Neo.Network.P2P.Payloads.Conditions
Expand Down Expand Up @@ -51,5 +53,12 @@ public override JObject ToJson()
json["hash"] = Hash.ToString();
return json;
}

public override StackItem ToStackItem(ReferenceCounter referenceCounter)
{
var result = (Array)base.ToStackItem(referenceCounter);
result.Add(Hash.ToArray());
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using Neo.IO.Json;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.VM.Types;
using System.IO;
using System.Linq;

Expand Down Expand Up @@ -56,5 +58,12 @@ public override JObject ToJson()
json["group"] = Group.ToString();
return json;
}

public override StackItem ToStackItem(ReferenceCounter referenceCounter)
{
var result = (Array)base.ToStackItem(referenceCounter);
result.Add(Group.ToArray());
return result;
}
}
}
9 changes: 9 additions & 0 deletions src/neo/Network/P2P/Payloads/Conditions/GroupCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using Neo.IO.Json;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.VM.Types;
using System.IO;
using System.Linq;

Expand Down Expand Up @@ -56,5 +58,12 @@ public override JObject ToJson()
json["group"] = Group.ToString();
return json;
}

public override StackItem ToStackItem(ReferenceCounter referenceCounter)
{
var result = (Array)base.ToStackItem(referenceCounter);
result.Add(Group.ToArray());
return result;
}
}
}
9 changes: 9 additions & 0 deletions src/neo/Network/P2P/Payloads/Conditions/NotCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using Neo.IO;
using Neo.IO.Json;
using Neo.SmartContract;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.IO;

Expand Down Expand Up @@ -56,5 +58,12 @@ public override JObject ToJson()
json["expression"] = Expression.ToJson();
return json;
}

public override StackItem ToStackItem(ReferenceCounter referenceCounter)
{
var result = (VM.Types.Array)base.ToStackItem(referenceCounter);
result.Add(Expression.ToStackItem(referenceCounter));
return result;
}
}
}
9 changes: 9 additions & 0 deletions src/neo/Network/P2P/Payloads/Conditions/OrCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using Neo.IO;
using Neo.IO.Json;
using Neo.SmartContract;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -58,5 +60,12 @@ public override JObject ToJson()
json["expressions"] = Expressions.Select(p => p.ToJson()).ToArray();
return json;
}

public override StackItem ToStackItem(ReferenceCounter referenceCounter)
{
var result = (VM.Types.Array)base.ToStackItem(referenceCounter);
result.Add(new VM.Types.Array(referenceCounter, Expressions.Select(p => p.ToStackItem(referenceCounter))));
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using Neo.IO;
using Neo.IO.Json;
using Neo.SmartContract;
using Neo.VM;
using Neo.VM.Types;
using System.IO;

namespace Neo.Network.P2P.Payloads.Conditions
Expand Down Expand Up @@ -51,5 +53,12 @@ public override JObject ToJson()
json["hash"] = Hash.ToString();
return json;
}

public override StackItem ToStackItem(ReferenceCounter referenceCounter)
{
var result = (Array)base.ToStackItem(referenceCounter);
result.Add(Hash.ToArray());
return result;
}
}
}
14 changes: 13 additions & 1 deletion src/neo/Network/P2P/Payloads/Conditions/WitnessCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
using Neo.IO.Caching;
using Neo.IO.Json;
using Neo.SmartContract;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.IO;

namespace Neo.Network.P2P.Payloads.Conditions
{
public abstract class WitnessCondition : ISerializable
public abstract class WitnessCondition : IInteroperable, ISerializable
{
private const int MaxSubitems = 16;
internal const int MaxNestingDepth = 2;
Expand Down Expand Up @@ -119,5 +121,15 @@ public virtual JObject ToJson()
["type"] = Type
};
}

void IInteroperable.FromStackItem(StackItem stackItem)
{
throw new NotSupportedException();
}

public virtual StackItem ToStackItem(ReferenceCounter referenceCounter)
{
return new VM.Types.Array(referenceCounter, new StackItem[] { (byte)Type });
}
}
}
22 changes: 21 additions & 1 deletion src/neo/Network/P2P/Payloads/Signer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
using Neo.IO;
using Neo.IO.Json;
using Neo.Network.P2P.Payloads.Conditions;
using Neo.SmartContract;
using Neo.VM;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -22,7 +24,7 @@ namespace Neo.Network.P2P.Payloads
/// <summary>
/// Represents a signer of a <see cref="Transaction"/>.
/// </summary>
public class Signer : ISerializable
public class Signer : IInteroperable, ISerializable
{
// This limits maximum number of AllowedContracts or AllowedGroups here
private const int MaxSubitems = 16;
Expand Down Expand Up @@ -176,5 +178,23 @@ public JObject ToJson()
json["rules"] = Rules.Select(p => p.ToJson()).ToArray();
return json;
}

void IInteroperable.FromStackItem(VM.Types.StackItem stackItem)
{
throw new NotSupportedException();
}

VM.Types.StackItem IInteroperable.ToStackItem(ReferenceCounter referenceCounter)
{
return new VM.Types.Array(referenceCounter, new VM.Types.StackItem[]
{
this.ToArray(),
Account.ToArray(),
(byte)Scopes,
new VM.Types.Array(AllowedContracts.Select(u => new VM.Types.ByteString(u.ToArray()))),
new VM.Types.Array(AllowedGroups.Select(u => new VM.Types.ByteString(u.ToArray()))),
new VM.Types.Array(Rules.Select(u => u.ToStackItem(referenceCounter)))
});
}
}
}
19 changes: 18 additions & 1 deletion src/neo/Network/P2P/Payloads/WitnessRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
using Neo.IO;
using Neo.IO.Json;
using Neo.Network.P2P.Payloads.Conditions;
using Neo.SmartContract;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.IO;

Expand All @@ -19,7 +22,7 @@ namespace Neo.Network.P2P.Payloads
/// <summary>
/// The rule used to describe the scope of the witness.
/// </summary>
public class WitnessRule : ISerializable
public class WitnessRule : IInteroperable, ISerializable
{
/// <summary>
/// Indicates the action to be taken if the current context meets with the rule.
Expand Down Expand Up @@ -73,5 +76,19 @@ public JObject ToJson()
["condition"] = Condition.ToJson()
};
}

void IInteroperable.FromStackItem(StackItem stackItem)
{
throw new NotSupportedException();
}

public StackItem ToStackItem(ReferenceCounter referenceCounter)
{
return new VM.Types.Array(referenceCounter, new StackItem[]
{
(byte)Action,
Condition.ToStackItem(referenceCounter)
});
}
}
}
2 changes: 1 addition & 1 deletion src/neo/SmartContract/ApplicationEngine.Crypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected internal bool CheckMultisig(byte[][] pubkeys, byte[][] signatures)
byte[] message = ScriptContainer.GetSignData(ProtocolSettings.Network);
int m = signatures.Length, n = pubkeys.Length;
if (n == 0 || m == 0 || m > n) throw new ArgumentException();
AddGas(CheckSigPrice * n * exec_fee_factor);
AddGas(CheckSigPrice * n * ExecFeeFactor);
try
{
for (int i = 0, j = 0; i < m && j < n;)
Expand Down
16 changes: 16 additions & 0 deletions src/neo/SmartContract/ApplicationEngine.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ partial class ApplicationEngine
/// </summary>
public static readonly InteropDescriptor System_Runtime_GetNetwork = Register("System.Runtime.GetNetwork", nameof(GetNetwork), 1 << 3, CallFlags.None);

/// <summary>
/// The <see cref="InteropDescriptor"/> of System.Runtime.GetAddressVersion.
/// Gets the address version of the current network.
/// </summary>
public static readonly InteropDescriptor System_Runtime_GetAddressVersion = Register("System.Runtime.GetAddressVersion", nameof(GetAddressVersion), 1 << 3, CallFlags.None);

/// <summary>
/// The <see cref="InteropDescriptor"/> of System.Runtime.GetTrigger.
/// Gets the trigger of the execution.
Expand Down Expand Up @@ -150,6 +156,16 @@ internal protected uint GetNetwork()
return ProtocolSettings.Network;
}

/// <summary>
/// The implementation of System.Runtime.GetAddressVersion.
/// Gets the address version of the current network.
/// </summary>
/// <returns>The address version of the current network.</returns>
internal protected byte GetAddressVersion()
{
return ProtocolSettings.AddressVersion;
}

/// <summary>
/// The implementation of System.Runtime.GetTime.
/// Gets the timestamp of the current block.
Expand Down
8 changes: 4 additions & 4 deletions src/neo/SmartContract/ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public partial class ApplicationEngine : ExecutionEngine
private List<IDisposable> disposables;
private readonly Dictionary<UInt160, int> invocationCounter = new();
private readonly Dictionary<ExecutionContext, ContractTaskAwaiter> contractTasks = new();
private readonly uint exec_fee_factor;
internal readonly uint ExecFeeFactor;
internal readonly uint StoragePrice;
private byte[] nonceData;

Expand Down Expand Up @@ -153,7 +153,7 @@ protected unsafe ApplicationEngine(TriggerType trigger, IVerifiable container, D
this.ProtocolSettings = settings;
this.gas_amount = gas;
this.Diagnostic = diagnostic;
this.exec_fee_factor = snapshot is null || persistingBlock?.Index == 0 ? PolicyContract.DefaultExecFeeFactor : NativeContract.Policy.GetExecFeeFactor(Snapshot);
this.ExecFeeFactor = snapshot is null || persistingBlock?.Index == 0 ? PolicyContract.DefaultExecFeeFactor : NativeContract.Policy.GetExecFeeFactor(Snapshot);
this.StoragePrice = snapshot is null || persistingBlock?.Index == 0 ? PolicyContract.DefaultStoragePrice : NativeContract.Policy.GetStoragePrice(Snapshot);
this.nonceData = container is Transaction tx ? tx.Hash.ToArray()[..16] : new byte[16];
if (persistingBlock is not null)
Expand Down Expand Up @@ -479,7 +479,7 @@ protected override void OnSysCall(uint method)
protected virtual void OnSysCall(InteropDescriptor descriptor)
{
ValidateCallFlags(descriptor.RequiredCallFlags);
AddGas(descriptor.FixedPrice * exec_fee_factor);
AddGas(descriptor.FixedPrice * ExecFeeFactor);

object[] parameters = new object[descriptor.Parameters.Count];
for (int i = 0; i < parameters.Length; i++)
Expand All @@ -493,7 +493,7 @@ protected virtual void OnSysCall(InteropDescriptor descriptor)
protected override void PreExecuteInstruction()
{
if (CurrentContext.InstructionPointer < CurrentContext.Script.Length)
AddGas(exec_fee_factor * OpCodePrices[CurrentContext.CurrentInstruction.OpCode]);
AddGas(ExecFeeFactor * OpCodePrices[CurrentContext.CurrentInstruction.OpCode]);
}

private static Block CreateDummyBlock(DataCache snapshot, ProtocolSettings settings)
Expand Down
6 changes: 6 additions & 0 deletions src/neo/SmartContract/Native/LedgerContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ private Transaction GetTransactionForContract(ApplicationEngine engine, UInt256
return state.Transaction;
}

[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.ReadStates)]
private Signer[] GetTransactionSigners(DataCache snapshot, UInt256 hash)
{
return GetTransactionState(snapshot, hash)?.Transaction.Signers;
}

[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.ReadStates)]
private VMState GetTransactionVMState(ApplicationEngine engine, UInt256 hash)
{
Expand Down
2 changes: 1 addition & 1 deletion src/neo/SmartContract/Native/NativeContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ internal async void Invoke(ApplicationEngine engine, byte version)
ExecutionContextState state = context.GetState<ExecutionContextState>();
if (!state.CallFlags.HasFlag(method.RequiredCallFlags))
throw new InvalidOperationException($"Cannot call this method with the flag {state.CallFlags}.");
engine.AddGas(method.CpuFee * Policy.GetExecFeeFactor(engine.Snapshot) + method.StorageFee * Policy.GetStoragePrice(engine.Snapshot));
engine.AddGas(method.CpuFee * engine.ExecFeeFactor + method.StorageFee * engine.StoragePrice);
List<object> parameters = new();
if (method.NeedApplicationEngine) parameters.Add(engine);
if (method.NeedSnapshot) parameters.Add(engine.Snapshot);
Expand Down
6 changes: 5 additions & 1 deletion src/neo/SmartContract/Native/NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,11 @@ public ECPoint[] ComputeNextBlockValidators(DataCache snapshot, ProtocolSettings
var candidates = GetCandidates(snapshot);
if (voterTurnout < EffectiveVoterTurnout || candidates.Length < settings.CommitteeMembersCount)
return settings.StandbyCommittee.Select(p => (p, candidates.FirstOrDefault(k => k.PublicKey.Equals(p)).Votes));
return candidates.OrderByDescending(p => p.Votes).ThenBy(p => p.PublicKey).Take(settings.CommitteeMembersCount);
return candidates
.OrderByDescending(p => p.Votes)
.ThenBy(p => p.PublicKey)
.Where(p => !Policy.IsBlocked(snapshot, Contract.CreateSignatureRedeemScript(p.PublicKey).ToScriptHash()))
.Take(settings.CommitteeMembersCount);
}

[ContractMethod(CpuFee = 1 << 16, RequiredCallFlags = CallFlags.ReadStates)]
Expand Down
Loading

0 comments on commit ed68274

Please sign in to comment.