Skip to content

Commit

Permalink
Merge branch 'master' into fixes/syscalls
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang committed Apr 8, 2020
2 parents 9acce87 + 2fad68a commit 8d2d524
Show file tree
Hide file tree
Showing 22 changed files with 212 additions and 223 deletions.
4 changes: 2 additions & 2 deletions src/neo/SmartContract/BinarySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static StackItem Deserialize(BinaryReader reader, uint maxArraySize, uin
case StackItemType.Integer:
deserialized.Push(new BigInteger(reader.ReadVarBytes(Integer.MaxSize)));
break;
case StackItemType.ByteArray:
case StackItemType.ByteString:
deserialized.Push(reader.ReadVarBytes((int)maxItemSize));
break;
case StackItemType.Buffer:
Expand Down Expand Up @@ -142,7 +142,7 @@ private static void Serialize(StackItem item, BinaryWriter writer, uint maxSize)
case Integer integer:
writer.WriteVarBytes(integer.Span);
break;
case ByteArray bytes:
case ByteString bytes:
writer.WriteVarBytes(bytes.Span);
break;
case Buffer buffer:
Expand Down
4 changes: 2 additions & 2 deletions src/neo/SmartContract/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static JObject Serialize(StackItem item)
{
return array.Select(p => Serialize(p)).ToArray();
}
case ByteArray buffer:
case ByteString buffer:
{
return Convert.ToBase64String(buffer.GetSpan());
}
Expand Down Expand Up @@ -87,7 +87,7 @@ public static byte[] SerializeToByteArray(StackItem item, uint maxSize)
case JsonTokenType.EndArray:
writer.WriteEndArray();
break;
case ByteArray buffer:
case ByteString buffer:
writer.WriteStringValue(Convert.ToBase64String(buffer.GetSpan()));
break;
case Integer num:
Expand Down
2 changes: 1 addition & 1 deletion src/neo/VM/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ private static ContractParameter ToParameter(StackItem item, List<(StackItem, Co
Value = item.ToBoolean()
};
break;
case ByteArray array:
case ByteString array:
parameter = new ContractParameter
{
Type = ContractParameterType.ByteArray,
Expand Down
46 changes: 45 additions & 1 deletion src/neo/Wallets/NEP6/NEP6Account.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using Neo.IO.Json;
using System;
using System.Threading;

namespace Neo.Wallets.NEP6
{
internal class NEP6Account : WalletAccount
{
private readonly NEP6Wallet wallet;
private readonly string nep2key;
private string nep2key;
private string nep2KeyNew = null;
private KeyPair key;
public JObject Extra;

Expand Down Expand Up @@ -83,5 +85,47 @@ public bool VerifyPassword(string password)
return false;
}
}

/// <summary>
/// Cache draft nep2key during wallet password changing process. Should not be called alone for a single account
/// </summary>
internal bool ChangePasswordPrepare(string password_old, string password_new)
{
if (WatchOnly) return true;
KeyPair keyTemplate = key;
if (nep2key == null)
{
if (keyTemplate == null)
{
return true;
}
}
else
{
try
{
keyTemplate = new KeyPair(Wallet.GetPrivateKeyFromNEP2(nep2key, password_old, wallet.Scrypt.N, wallet.Scrypt.R, wallet.Scrypt.P));
}
catch
{
return false;
}
}
nep2KeyNew = keyTemplate.Export(password_new, wallet.Scrypt.N, wallet.Scrypt.R, wallet.Scrypt.P);
return true;
}

internal void ChangePasswordCommit()
{
if (nep2KeyNew != null)
{
nep2key = Interlocked.Exchange(ref nep2KeyNew, null);
}
}

internal void ChangePasswordRoolback()
{
nep2KeyNew = null;
}
}
}
30 changes: 30 additions & 0 deletions src/neo/Wallets/NEP6/NEP6Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using UserWallet = Neo.Wallets.SQLite.UserWallet;

namespace Neo.Wallets.NEP6
Expand Down Expand Up @@ -300,5 +301,34 @@ public override bool VerifyPassword(string password)
}
}
}

public bool ChangePassword(string password_old, string password_new)
{
bool succeed = true;
lock (accounts)
{
Parallel.ForEach(accounts.Values, (account, state) =>
{
if (!account.ChangePasswordPrepare(password_old, password_new))
{
state.Stop();
succeed = false;
}
});
}
if (succeed)
{
foreach (NEP6Account account in accounts.Values)
account.ChangePasswordCommit();
if (password != null)
password = password_new;
}
else
{
foreach (NEP6Account account in accounts.Values)
account.ChangePasswordRoolback();
}
return succeed;
}
}
}
4 changes: 2 additions & 2 deletions src/neo/neo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Akka" Version="1.3.16" />
<PackageReference Include="Akka" Version="1.4.2" />
<PackageReference Include="K4os.Compression.LZ4" Version="1.1.11" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.WebSockets" Version="2.2.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.1" />
<PackageReference Include="Neo.VM" Version="3.0.0-CI00211" />
<PackageReference Include="Neo.VM" Version="3.0.0-CI00214" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public static string Symbol(this NativeContract contract)
engine.Execute().Should().Be(VMState.HALT);

var result = engine.ResultStack.Pop();
result.Should().BeOfType(typeof(VM.Types.ByteArray));
result.Should().BeOfType(typeof(VM.Types.ByteString));

return result.GetString();
}
Expand All @@ -186,7 +186,7 @@ public static string Name(this NativeContract contract)
engine.Execute().Should().Be(VMState.HALT);

var result = engine.ResultStack.Pop();
result.Should().BeOfType(typeof(VM.Types.ByteArray));
result.Should().BeOfType(typeof(VM.Types.ByteString));

return result.GetString();
}
Expand Down
1 change: 0 additions & 1 deletion tests/neo.UnitTests/Network/P2P/UT_RemoteNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
namespace Neo.UnitTests.Network.P2P
{
[TestClass]
[NotReRunnable]
public class UT_RemoteNode : TestKit
{
private static NeoSystem testBlockchain;
Expand Down
1 change: 0 additions & 1 deletion tests/neo.UnitTests/Network/P2P/UT_RemoteNodeMailbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Neo.UnitTests.Network.P2P
{
[TestClass]
[NotReRunnable]
public class UT_RemoteNodeMailbox : TestKit
{
private static readonly Random TestRandom = new Random(1337); // use fixed seed for guaranteed determinism
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class UT_PrimitiveWrapper
[TestMethod]
public void TestGeneratorAndDispose()
{
ByteArrayWrapper arrayWrapper = new ByteArrayWrapper(new ByteArray(new byte[0]));
ByteArrayWrapper arrayWrapper = new ByteArrayWrapper(new ByteString(new byte[0]));
Assert.IsNotNull(arrayWrapper);
Action action = () => arrayWrapper.Dispose();
action.Should().NotThrow<Exception>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public void TestKeyAndValueAndNext()
list.Add((storageKey, storageItem));
StorageIterator storageIterator = new StorageIterator(list.GetEnumerator());
storageIterator.Next();
Assert.AreEqual(new ByteArray(new byte[1]), storageIterator.Key());
Assert.AreEqual(new ByteArray(new byte[1]), storageIterator.Value());
Assert.AreEqual(new ByteString(new byte[1]), storageIterator.Key());
Assert.AreEqual(new ByteString(new byte[1]), storageIterator.Value());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ internal static void CheckBalance(byte[] account, DataCache<StorageKey, StorageI
var st = (VM.Types.Struct)BinarySerializer.Deserialize(trackable.Item.Value, 16, 32);

st.Count.Should().Be(3);
st.Select(u => u.GetType()).ToArray().Should().BeEquivalentTo(new Type[] { typeof(VM.Types.Integer), typeof(VM.Types.Integer), typeof(VM.Types.ByteArray) }); // Balance
st.Select(u => u.GetType()).ToArray().Should().BeEquivalentTo(new Type[] { typeof(VM.Types.Integer), typeof(VM.Types.Integer), typeof(VM.Types.ByteString) }); // Balance

st[0].GetBigInteger().Should().Be(balance); // Balance
st[1].GetBigInteger().Should().Be(height); // BalanceHeight
Expand Down
4 changes: 2 additions & 2 deletions tests/neo.UnitTests/SmartContract/Native/UT_NativeContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ public void TestInvoke()
sb2.EmitSysCall("test".ToInteropMethodHash());
engine2.LoadScript(sb2.ToArray());

ByteArray method1 = new ByteArray(System.Text.Encoding.Default.GetBytes("wrongMethod"));
ByteString method1 = new ByteString(System.Text.Encoding.Default.GetBytes("wrongMethod"));
VMArray args1 = new VMArray();
engine2.CurrentContext.EvaluationStack.Push(args1);
engine2.CurrentContext.EvaluationStack.Push(method1);
testNativeContract.Invoke(engine2).Should().BeFalse();

ByteArray method2 = new ByteArray(System.Text.Encoding.Default.GetBytes("onPersist"));
ByteString method2 = new ByteString(System.Text.Encoding.Default.GetBytes("onPersist"));
VMArray args2 = new VMArray();
engine2.CurrentContext.EvaluationStack.Push(args2);
engine2.CurrentContext.EvaluationStack.Push(method2);
Expand Down
2 changes: 1 addition & 1 deletion tests/neo.UnitTests/SmartContract/UT_BinarySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void TestSerialize()
[TestMethod]
public void TestDeserializeStackItem()
{
StackItem stackItem1 = new ByteArray(new byte[5]);
StackItem stackItem1 = new ByteString(new byte[5]);
byte[] byteArray1 = BinarySerializer.Serialize(stackItem1, MaxItemSize);
StackItem result1 = BinarySerializer.Deserialize(byteArray1, 2048, (uint)byteArray1.Length);
Assert.AreEqual(stackItem1, result1);
Expand Down
2 changes: 1 addition & 1 deletion tests/neo.UnitTests/SmartContract/UT_InteropService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ public void TestBlockchain_GetContract()
InteropService.Invoke(engine, InteropService.Blockchain.GetContract).Should().BeTrue();
var stackItems = ((VM.Types.Array)engine.CurrentContext.EvaluationStack.Pop()).ToArray();
stackItems.Length.Should().Be(3);
stackItems[0].GetType().Should().Be(typeof(ByteArray));
stackItems[0].GetType().Should().Be(typeof(ByteString));
stackItems[0].GetSpan().ToHexString().Should().Be(state.Script.ToHexString());
stackItems[1].ToBoolean().Should().BeFalse();
stackItems[2].ToBoolean().Should().BeFalse();
Expand Down
14 changes: 7 additions & 7 deletions tests/neo.UnitTests/SmartContract/UT_Syscalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void System_Blockchain_GetBlock()

Assert.AreEqual(engine.Execute(), VMState.HALT);
Assert.AreEqual(1, engine.ResultStack.Count);
Assert.IsInstanceOfType(engine.ResultStack.Peek(), typeof(ByteArray));
Assert.IsInstanceOfType(engine.ResultStack.Peek(), typeof(ByteString));
Assert.AreEqual(engine.ResultStack.Pop().GetSpan().ToHexString(),
"5b22366b4139757552614430373634585358466c706674686b436b5954702f6e34623878715057476c6a6659303d222c332c22414141414141414141414141414141414141414141414141414141414141414141414141414141414141413d222c22414141414141414141414141414141414141414141414141414141414141414141414141414141414141413d222c322c302c224141414141414141414141414141414141414141414141414141413d222c315d");
Assert.AreEqual(0, engine.ResultStack.Count);
Expand Down Expand Up @@ -194,11 +194,11 @@ public void Json_Serialize()
Assert.AreEqual(engine.Execute(), VMState.HALT);
Assert.AreEqual(5, engine.ResultStack.Count);

Assert.IsTrue(engine.ResultStack.TryPop<ByteArray>(out var m) && m.GetString() == "{\"key\":\"dmFsdWU=\"}");
Assert.IsTrue(engine.ResultStack.TryPop<ByteArray>(out var n) && n.GetString() == "null");
Assert.IsTrue(engine.ResultStack.TryPop<ByteArray>(out var s) && s.GetString() == "\"dGVzdA==\"");
Assert.IsTrue(engine.ResultStack.TryPop<ByteArray>(out var b) && b.GetString() == "true");
Assert.IsTrue(engine.ResultStack.TryPop<ByteArray>(out var i) && i.GetString() == "5");
Assert.IsTrue(engine.ResultStack.TryPop<ByteString>(out var m) && m.GetString() == "{\"key\":\"dmFsdWU=\"}");
Assert.IsTrue(engine.ResultStack.TryPop<ByteString>(out var n) && n.GetString() == "null");
Assert.IsTrue(engine.ResultStack.TryPop<ByteString>(out var s) && s.GetString() == "\"dGVzdA==\"");
Assert.IsTrue(engine.ResultStack.TryPop<ByteString>(out var b) && b.GetString() == "true");
Assert.IsTrue(engine.ResultStack.TryPop<ByteString>(out var i) && i.GetString() == "5");
}
}

Expand Down Expand Up @@ -259,7 +259,7 @@ public void System_ExecutionEngine_GetScriptContainer()

Assert.AreEqual(engine.Execute(), VMState.HALT);
Assert.AreEqual(1, engine.ResultStack.Count);
Assert.IsInstanceOfType(engine.ResultStack.Peek(), typeof(ByteArray));
Assert.IsInstanceOfType(engine.ResultStack.Peek(), typeof(ByteString));
Assert.AreEqual(engine.ResultStack.Pop().GetSpan().ToHexString(),
@"5b225c75303032426b53415959527a4c4b69685a676464414b50596f754655737a63544d7867445a6572584a3172784c37303d222c362c342c222f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f383d222c332c322c352c2241513d3d225d");
Assert.AreEqual(0, engine.ResultStack.Count);
Expand Down
Loading

0 comments on commit 8d2d524

Please sign in to comment.