Skip to content

Commit

Permalink
More optimizations about Span (neo-project#1352)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored and Tommo-L committed Jun 22, 2020
1 parent a586c57 commit 8ec8a20
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 29 deletions.
8 changes: 4 additions & 4 deletions src/neo/Cryptography/Crypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public static byte[] Sign(byte[] message, byte[] prikey, byte[] pubkey)
}
}

public static bool VerifySignature(ReadOnlySpan<byte> message, ReadOnlySpan<byte> signature, byte[] pubkey)
public static bool VerifySignature(ReadOnlySpan<byte> message, ReadOnlySpan<byte> signature, ReadOnlySpan<byte> pubkey)
{
if (pubkey.Length == 33 && (pubkey[0] == 0x02 || pubkey[0] == 0x03))
{
try
{
pubkey = ECC.ECPoint.DecodePoint(pubkey, ECC.ECCurve.Secp256r1).EncodePoint(false)[1..];
pubkey = ECC.ECPoint.DecodePoint(pubkey, ECC.ECCurve.Secp256r1).EncodePoint(false).AsSpan(1);
}
catch
{
Expand All @@ -58,8 +58,8 @@ public static bool VerifySignature(ReadOnlySpan<byte> message, ReadOnlySpan<byte
Curve = ECCurve.NamedCurves.nistP256,
Q = new ECPoint
{
X = pubkey[..32],
Y = pubkey[32..]
X = pubkey[..32].ToArray(),
Y = pubkey[32..].ToArray()
}
}))
{
Expand Down
4 changes: 2 additions & 2 deletions src/neo/SmartContract/InteropService.NEO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private static bool Crypto_ECDsaVerify(ApplicationEngine engine)
Null _ => engine.ScriptContainer.GetHashData(),
_ => item0.GetSpan()
};
byte[] pubkey = engine.CurrentContext.EvaluationStack.Pop().GetSpan().ToArray();
ReadOnlySpan<byte> pubkey = engine.CurrentContext.EvaluationStack.Pop().GetSpan();
ReadOnlySpan<byte> signature = engine.CurrentContext.EvaluationStack.Pop().GetSpan();
try
{
Expand Down Expand Up @@ -162,7 +162,7 @@ private static bool Crypto_ECDsaCheckMultiSig(ApplicationEngine engine)

private static bool Account_IsStandard(ApplicationEngine engine)
{
UInt160 hash = new UInt160(engine.CurrentContext.EvaluationStack.Pop().GetSpan().ToArray());
UInt160 hash = new UInt160(engine.CurrentContext.EvaluationStack.Pop().GetSpan());
ContractState contract = engine.Snapshot.Contracts.TryGet(hash);
bool isStandard = contract is null || contract.Script.IsStandardContract();
engine.CurrentContext.EvaluationStack.Push(isStandard);
Expand Down
25 changes: 11 additions & 14 deletions src/neo/SmartContract/InteropService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using System.Numerics;
using System.Text;
using Array = Neo.VM.Types.Array;
using Boolean = Neo.VM.Types.Boolean;

namespace Neo.SmartContract
{
Expand Down Expand Up @@ -77,10 +76,8 @@ private static bool CheckItemForNotification(StackItem state)
items_unchecked.Enqueue(item);
}
break;
case Boolean _:
case ByteArray _:
case Integer _:
size += state.GetByteLength();
case PrimitiveType primitive:
size += primitive.GetByteLength();
break;
case Null _:
break;
Expand Down Expand Up @@ -231,7 +228,7 @@ private static bool Runtime_CheckWitness(ApplicationEngine engine)
ReadOnlySpan<byte> hashOrPubkey = engine.CurrentContext.EvaluationStack.Pop().GetSpan();
bool result;
if (hashOrPubkey.Length == 20)
result = CheckWitness(engine, new UInt160(hashOrPubkey.ToArray()));
result = CheckWitness(engine, new UInt160(hashOrPubkey));
else if (hashOrPubkey.Length == 33)
result = CheckWitness(engine, ECPoint.DecodePoint(hashOrPubkey, ECCurve.Secp256r1));
else
Expand All @@ -250,7 +247,7 @@ private static bool Runtime_Notify(ApplicationEngine engine)

private static bool Runtime_Log(ApplicationEngine engine)
{
byte[] state = engine.CurrentContext.EvaluationStack.Pop().GetSpan().ToArray();
ReadOnlySpan<byte> state = engine.CurrentContext.EvaluationStack.Pop().GetSpan();
if (state.Length > MaxNotificationSize) return false;
string message = Encoding.UTF8.GetString(state);
engine.SendLog(engine.CurrentScriptHash, message);
Expand Down Expand Up @@ -287,7 +284,7 @@ private static bool Runtime_GetNotifications(ApplicationEngine engine)
IEnumerable<NotifyEventArgs> notifications = engine.Notifications;
if (!item.IsNull) // must filter by scriptHash
{
var hash = new UInt160(item.GetSpan().ToArray());
var hash = new UInt160(item.GetSpan());
notifications = notifications.Where(p => p.ScriptHash == hash);
}

Expand Down Expand Up @@ -334,7 +331,7 @@ private static bool Blockchain_GetHeight(ApplicationEngine engine)

private static bool Blockchain_GetBlock(ApplicationEngine engine)
{
byte[] data = engine.CurrentContext.EvaluationStack.Pop().GetSpan().ToArray();
ReadOnlySpan<byte> data = engine.CurrentContext.EvaluationStack.Pop().GetSpan();
UInt256 hash;
if (data.Length <= 5)
hash = Blockchain.Singleton.GetBlockHash((uint)new BigInteger(data));
Expand All @@ -353,7 +350,7 @@ private static bool Blockchain_GetBlock(ApplicationEngine engine)

private static bool Blockchain_GetTransaction(ApplicationEngine engine)
{
byte[] hash = engine.CurrentContext.EvaluationStack.Pop().GetSpan().ToArray();
ReadOnlySpan<byte> hash = engine.CurrentContext.EvaluationStack.Pop().GetSpan();
Transaction tx = engine.Snapshot.GetTransaction(new UInt256(hash));
if (tx == null)
engine.CurrentContext.EvaluationStack.Push(StackItem.Null);
Expand All @@ -364,15 +361,15 @@ private static bool Blockchain_GetTransaction(ApplicationEngine engine)

private static bool Blockchain_GetTransactionHeight(ApplicationEngine engine)
{
byte[] hash = engine.CurrentContext.EvaluationStack.Pop().GetSpan().ToArray();
ReadOnlySpan<byte> hash = engine.CurrentContext.EvaluationStack.Pop().GetSpan();
var tx = engine.Snapshot.Transactions.TryGet(new UInt256(hash));
engine.CurrentContext.EvaluationStack.Push(tx != null ? new BigInteger(tx.BlockIndex) : BigInteger.MinusOne);
return true;
}

private static bool Blockchain_GetTransactionFromBlock(ApplicationEngine engine)
{
byte[] data = engine.CurrentContext.EvaluationStack.Pop().GetSpan().ToArray();
ReadOnlySpan<byte> data = engine.CurrentContext.EvaluationStack.Pop().GetSpan();
UInt256 hash;
if (data.Length <= 5)
hash = Blockchain.Singleton.GetBlockHash((uint)new BigInteger(data));
Expand Down Expand Up @@ -402,7 +399,7 @@ private static bool Blockchain_GetTransactionFromBlock(ApplicationEngine engine)

private static bool Blockchain_GetContract(ApplicationEngine engine)
{
UInt160 hash = new UInt160(engine.CurrentContext.EvaluationStack.Pop().GetSpan().ToArray());
UInt160 hash = new UInt160(engine.CurrentContext.EvaluationStack.Pop().GetSpan());
ContractState contract = engine.Snapshot.Contracts.TryGet(hash);
if (contract == null)
engine.CurrentContext.EvaluationStack.Push(StackItem.Null);
Expand Down Expand Up @@ -470,7 +467,7 @@ private static bool Contract_Call(ApplicationEngine engine)
{
StackItem contractHash = engine.CurrentContext.EvaluationStack.Pop();

ContractState contract = engine.Snapshot.Contracts.TryGet(new UInt160(contractHash.GetSpan().ToArray()));
ContractState contract = engine.Snapshot.Contracts.TryGet(new UInt160(contractHash.GetSpan()));
if (contract is null) return false;

StackItem method = engine.CurrentContext.EvaluationStack.Pop();
Expand Down
4 changes: 2 additions & 2 deletions src/neo/SmartContract/Native/PolicyContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private StackItem SetFeePerByte(ApplicationEngine engine, Array args)
private StackItem BlockAccount(ApplicationEngine engine, Array args)
{
if (!CheckValidators(engine)) return false;
UInt160 account = new UInt160(args[0].GetSpan().ToArray());
UInt160 account = new UInt160(args[0].GetSpan());
StorageKey key = CreateStorageKey(Prefix_BlockedAccounts);
StorageItem storage = engine.Snapshot.Storages[key];
SortedSet<UInt160> accounts = new SortedSet<UInt160>(storage.Value.AsSerializableArray<UInt160>());
Expand All @@ -159,7 +159,7 @@ private StackItem BlockAccount(ApplicationEngine engine, Array args)
private StackItem UnblockAccount(ApplicationEngine engine, Array args)
{
if (!CheckValidators(engine)) return false;
UInt160 account = new UInt160(args[0].GetSpan().ToArray());
UInt160 account = new UInt160(args[0].GetSpan());
StorageKey key = CreateStorageKey(Prefix_BlockedAccounts);
StorageItem storage = engine.Snapshot.Storages[key];
SortedSet<UInt160> accounts = new SortedSet<UInt160>(storage.Value.AsSerializableArray<UInt160>());
Expand Down
4 changes: 2 additions & 2 deletions src/neo/SmartContract/Native/Tokens/NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ protected override bool OnPersist(ApplicationEngine engine)
[ContractMethod(0_03000000, ContractParameterType.Integer, ParameterTypes = new[] { ContractParameterType.Hash160, ContractParameterType.Integer }, ParameterNames = new[] { "account", "end" }, SafeMethod = true)]
private StackItem UnclaimedGas(ApplicationEngine engine, Array args)
{
UInt160 account = new UInt160(args[0].GetSpan().ToArray());
UInt160 account = new UInt160(args[0].GetSpan());
uint end = (uint)args[1].GetBigInteger();
return UnclaimedGas(engine.Snapshot, account, end);
}
Expand Down Expand Up @@ -153,7 +153,7 @@ private bool RegisterValidator(StoreView snapshot, ECPoint pubkey)
[ContractMethod(5_00000000, ContractParameterType.Boolean, ParameterTypes = new[] { ContractParameterType.Hash160, ContractParameterType.Array }, ParameterNames = new[] { "account", "pubkeys" })]
private StackItem Vote(ApplicationEngine engine, Array args)
{
UInt160 account = new UInt160(args[0].GetSpan().ToArray());
UInt160 account = new UInt160(args[0].GetSpan());
ECPoint[] pubkeys = ((Array)args[1]).Select(p => p.GetSpan().AsSerializable<ECPoint>()).ToArray();
if (!InteropService.CheckWitness(engine, account)) return false;
StorageKey key_account = CreateAccountKey(account);
Expand Down
6 changes: 3 additions & 3 deletions src/neo/SmartContract/Native/Tokens/Nep5Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public virtual BigInteger TotalSupply(StoreView snapshot)
[ContractMethod(0_01000000, ContractParameterType.Integer, ParameterTypes = new[] { ContractParameterType.Hash160 }, ParameterNames = new[] { "account" }, SafeMethod = true)]
protected StackItem BalanceOf(ApplicationEngine engine, Array args)
{
return BalanceOf(engine.Snapshot, new UInt160(args[0].GetSpan().ToArray()));
return BalanceOf(engine.Snapshot, new UInt160(args[0].GetSpan()));
}

public virtual BigInteger BalanceOf(StoreView snapshot, UInt160 account)
Expand All @@ -163,8 +163,8 @@ public virtual BigInteger BalanceOf(StoreView snapshot, UInt160 account)
[ContractMethod(0_08000000, ContractParameterType.Boolean, ParameterTypes = new[] { ContractParameterType.Hash160, ContractParameterType.Hash160, ContractParameterType.Integer }, ParameterNames = new[] { "from", "to", "amount" })]
protected StackItem Transfer(ApplicationEngine engine, Array args)
{
UInt160 from = new UInt160(args[0].GetSpan().ToArray());
UInt160 to = new UInt160(args[1].GetSpan().ToArray());
UInt160 from = new UInt160(args[0].GetSpan());
UInt160 to = new UInt160(args[1].GetSpan());
BigInteger amount = args[2].GetBigInteger();
return Transfer(engine, from, to, amount);
}
Expand Down
2 changes: 1 addition & 1 deletion src/neo/UInt160.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public UInt160()
{
}

public unsafe UInt160(byte[] value)
public unsafe UInt160(ReadOnlySpan<byte> value)
{
fixed (ulong* p = &value1)
{
Expand Down
2 changes: 1 addition & 1 deletion src/neo/Wallets/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static UInt160 ToScriptHash(this string address)
throw new FormatException();
if (data[0] != ProtocolSettings.Default.AddressVersion)
throw new FormatException();
return new UInt160(data[1..]);
return new UInt160(data.AsSpan(1));
}

internal static byte[] XOR(byte[] x, byte[] y)
Expand Down

0 comments on commit 8ec8a20

Please sign in to comment.