Skip to content

Commit

Permalink
Enhancement/voting (#130)
Browse files Browse the repository at this point in the history
* improve the votes counting algorithm

* change the method of voting
1. Add `StateTransaction`
2. Remove `Neo.Account.SetVotes` and `Neo.Validator.Register`

* fix tests

* consider unconfirmed transactions in `Blockchain.GetValidators`

* fix the bug of initializing `DbMetaDataCache`

* fix bug of `DbCache.FindInternal`

* fix bug of processing `AccountStateDescriptor`

* reject the voting to random

* allow the voting to standby validators

* change version to v2.6.0

* improve the performance of `WalletIndexer`
  • Loading branch information
erikzhang authored Jan 15, 2018
1 parent c424dda commit 115a402
Show file tree
Hide file tree
Showing 34 changed files with 894 additions and 391 deletions.
1 change: 0 additions & 1 deletion neo.UnitTests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@ Coverage
* TransactionResult.cs
* UnspentCoinState.cs
* ValidatorState.cs
* VoteState.cs
* Witness.cs
20 changes: 8 additions & 12 deletions neo.UnitTests/TestBlockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,14 @@ public override bool ContainsUnspent(UInt256 hash, ushort index)
throw new NotImplementedException();
}

public override DataCache<TKey, TValue> CreateCache<TKey, TValue>()
public override MetaDataCache<T> GetMetaData<T>()
{
throw new NotImplementedException();
return new TestMetaDataCache<T>();
}

public override DataCache<TKey, TValue> GetStates<TKey, TValue>()
{
return new TestDataCache<TKey, TValue>();
}

public override void Dispose()
Expand Down Expand Up @@ -120,7 +125,7 @@ public override Transaction GetTransaction(UInt256 hash, out int height)
{
height = 0;
// take part of the trans hash and use that for the scripthash of the testtransaction
return new TestTransaction(_assetId, TransactionType.ClaimTransaction, new UInt160(TestUtils.GetByteArray(20,hash.ToArray()[0])));
return new TestTransaction(_assetId, TransactionType.ClaimTransaction, new UInt160(TestUtils.GetByteArray(20, hash.ToArray()[0])));
}

public override Dictionary<ushort, SpentCoin> GetUnclaimed(UInt256 hash)
Expand All @@ -138,15 +143,6 @@ public override IEnumerable<TransactionOutput> GetUnspent(UInt256 hash)
throw new NotImplementedException();
}

public override IEnumerable<VoteState> GetVotes(IEnumerable<Transaction> others)
{
VoteState vs = new VoteState() { Count = Fixed8.FromDecimal(1), PublicKeys = TestUtils.StandbyValidators};
return new VoteState[]
{
vs
};
}

public override bool IsDoubleSpend(Transaction tx)
{
throw new NotImplementedException();
Expand Down
40 changes: 40 additions & 0 deletions neo.UnitTests/TestDataCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Neo.IO;
using Neo.IO.Caching;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Neo.UnitTests
{
public class TestDataCache<TKey, TValue> : DataCache<TKey, TValue>
where TKey : IEquatable<TKey>, ISerializable
where TValue : class, ICloneable<TValue>, ISerializable, new()
{
public override void DeleteInternal(TKey key)
{
}

protected override void AddInternal(TKey key, TValue value)
{
}

protected override IEnumerable<KeyValuePair<TKey, TValue>> FindInternal(byte[] key_prefix)
{
return Enumerable.Empty<KeyValuePair<TKey, TValue>>();
}

protected override TValue GetInternal(TKey key)
{
throw new NotImplementedException();
}

protected override TValue TryGetInternal(TKey key)
{
return null;
}

protected override void UpdateInternal(TKey key, TValue value)
{
}
}
}
18 changes: 18 additions & 0 deletions neo.UnitTests/TestMetaDataCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Neo.IO;
using Neo.IO.Caching;

namespace Neo.UnitTests
{
public class TestMetaDataCache<T> : MetaDataCache<T> where T : class, ISerializable, new()
{
public TestMetaDataCache()
: base(null)
{
}

protected override T TryGetInternal()
{
return null;
}
}
}
11 changes: 6 additions & 5 deletions neo.UnitTests/UT_AccountState.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using FluentAssertions;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Core;
using Neo.Cryptography.ECC;
using Neo.IO;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Neo.Core;

namespace Neo.UnitTests
{
Expand Down Expand Up @@ -41,7 +42,7 @@ public void IsFrozen_Get()

[TestMethod]
public void IsFrozen_Set()
{
{
uut.IsFrozen = true;
uut.IsFrozen.Should().Be(true);
}
Expand Down Expand Up @@ -81,7 +82,7 @@ public void Balances_Set()
[TestMethod]
public void Size_Get_0_Votes_0_Balances()
{
UInt160 val = new UInt160();
UInt160 val = new UInt160();
ECPoint[] array = new ECPoint[0];
Dictionary<UInt256, Fixed8> dict = new Dictionary<UInt256, Fixed8>();

Expand Down
10 changes: 5 additions & 5 deletions neo.UnitTests/UT_AssetState.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Neo.Core;
using Neo.Cryptography.ECC;
using FluentAssertions;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Core;
using Neo.Cryptography.ECC;
using Neo.IO;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;

namespace Neo.UnitTests
{
Expand Down Expand Up @@ -264,7 +264,7 @@ public void Size_Get_Default()

uut.Size.Should().Be(130); // 1 + 32 + 1 + 4 + 8 + 8 + 1 + 1 + 8 + 20 + 1 + 20 + 20 + 4 + 1
}

[TestMethod]
public void Clone()
{
Expand Down
2 changes: 1 addition & 1 deletion neo.UnitTests/UT_StorageItem.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Core;
using System.Collections.Generic;
using Neo.IO;
using System.IO;
using System.Text;

Expand Down
30 changes: 6 additions & 24 deletions neo.UnitTests/UT_ValidatorState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Core;
using Neo.Cryptography.ECC;
using Neo.IO.Json;
using System.Collections.Generic;
using System.IO;
using System.Text;

Expand Down Expand Up @@ -31,7 +29,7 @@ public void Items_Set()
{
ECPoint val = new ECPoint();
uut.PublicKey = val;
uut.PublicKey.Should().Be(val);
uut.PublicKey.Should().Be(val);
}

[TestMethod]
Expand All @@ -40,13 +38,13 @@ public void Size_Get()
ECPoint val = ECPoint.DecodePoint("03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c".HexToBytes(), ECCurve.Secp256r1);
uut.PublicKey = val;

uut.Size.Should().Be(34); // 1 + 33
uut.Size.Should().Be(43); // 1 + 33 + 1 + 8
}

[TestMethod]
public void Deserialize()
{
byte[] data = new byte[] { 0, 3, 178, 9, 253, 79, 83, 167, 23, 14, 164, 68, 78, 12, 176, 166, 187, 106, 83, 194, 189, 1, 105, 38, 152, 156, 248, 95, 155, 15, 186, 23, 167, 12 };
byte[] data = new byte[] { 0, 3, 178, 9, 253, 79, 83, 167, 23, 14, 164, 68, 78, 12, 176, 166, 187, 106, 83, 194, 189, 1, 105, 38, 152, 156, 248, 95, 155, 15, 186, 23, 167, 12, 1, 0, 0, 0, 0, 0, 0, 0, 0 };
int index = 0;
using (MemoryStream ms = new MemoryStream(data, index, data.Length - index, false))
{
Expand All @@ -64,16 +62,6 @@ public void Equals_SameObj()
uut.Equals(uut).Should().BeTrue();
}


[TestMethod]
public void Equals_SameKey()
{
ValidatorState newVs = new ValidatorState();
newVs.PublicKey = ECPoint.DecodePoint("03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c".HexToBytes(), ECCurve.Secp256r1);
uut.PublicKey = ECPoint.DecodePoint("03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c".HexToBytes(), ECCurve.Secp256r1);
uut.Equals(newVs).Should().BeTrue();
}

[TestMethod]
public void Equals_DifferentKey()
{
Expand All @@ -89,19 +77,13 @@ public void Equals_Null()
uut.Equals(null).Should().BeFalse();
}

[TestMethod]
public void GetHashCode_Get()
{
uut.PublicKey = ECPoint.DecodePoint("03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c".HexToBytes(), ECCurve.Secp256r1);
uut.GetHashCode().Should().Be(435035065);
}


[TestMethod]
public void Serialize()
{
ECPoint val = ECPoint.DecodePoint("03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c".HexToBytes(), ECCurve.Secp256r1);
uut.PublicKey = val;
uut.Registered = true;
uut.Votes = Fixed8.Zero;

byte[] data;
using (MemoryStream stream = new MemoryStream())
Expand All @@ -113,7 +95,7 @@ public void Serialize()
}
}

byte[] requiredData = new byte[] { 0, 3, 178, 9, 253, 79, 83, 167, 23, 14, 164, 68, 78, 12, 176, 166, 187, 106, 83, 194, 189, 1, 105, 38, 152, 156, 248, 95, 155, 15, 186, 23, 167, 12 };
byte[] requiredData = new byte[] { 0, 3, 178, 9, 253, 79, 83, 167, 23, 14, 164, 68, 78, 12, 176, 166, 187, 106, 83, 194, 189, 1, 105, 38, 152, 156, 248, 95, 155, 15, 186, 23, 167, 12, 1, 0, 0, 0, 0, 0, 0, 0, 0 };

data.Length.Should().Be(requiredData.Length);
for (int i = 0; i < requiredData.Length; i++)
Expand Down
51 changes: 0 additions & 51 deletions neo.UnitTests/UT_VoteState.cs

This file was deleted.

7 changes: 7 additions & 0 deletions neo/Core/AccountState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ void ICloneable<AccountState>.FromReplica(AccountState replica)
Balances = replica.Balances;
}

public Fixed8 GetBalance(UInt256 asset_id)
{
if (!Balances.TryGetValue(asset_id, out Fixed8 value))
value = Fixed8.Zero;
return value;
}

public override void Serialize(BinaryWriter writer)
{
base.Serialize(writer);
Expand Down
Loading

0 comments on commit 115a402

Please sign in to comment.