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

同步原作者的Master分支 #5

Merged
merged 3 commits into from
Dec 29, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion AntSharesCore/Core/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public abstract class Blockchain : IDisposable
ECPoint.DecodePoint("039dafd8571a641058ccc832c5e2111ea39b09c0bde36050914384f7a48bce9bf9".HexToBytes(), ECCurve.Secp256r1),
ECPoint.DecodePoint("038dddc06ce687677a53d54f096d2591ba2302068cf123c1f2d75c2dddc5425579".HexToBytes(), ECCurve.Secp256r1),
};
public static readonly Block GenesisBlock = "000000000000000000000000000000000000000000000000000000000000000000000000e9b80ae02a31a55454620f682f6e41f1c4ab825b619f112dd33ce6cda97344ed1d496856000000001dac2b7c00000000d5b21f2e11a9795a22a482f342f53634b5a8cf3afd450140218268890bffef0632d2cc07aee12a31ea715e90df682c49118042bffd58fc661100ae6270a1ac3492a1b0c35e89f41d5243647b2ef2ef917073290fdbe5148640657634ffc7841c6e293aff9d05a4c00bbf830cc75da584835f56d95f1d491818d0ab7139076ba5779b88bcfa2e755bd0364ba5008ce0af10f6c744b01ce8f42e405ebfe773daad02f76d3fe1af637df1632e5fb9ebeb480b11c0082a76fc7f7699c47d0d22c5a74eefda81e18893faffdf81842b92b16423b7e76ad1d877867b4b4034b5c455c3acb90872d990d11a032ebe1f9cca1ef2221e9c2aedbecbe70ee925107c07fa1c31800590f80457f7137b1509f8506a4ba8316a7798dd425133c0d840cfe42c54fb8b5d0497dbc7c4ad49b88cefc1b4ef95b6484d245680f193b2fd0d4c10e17c8cc93411f80882ad2e737ef0862f329bf8862b788e07b49d36175413ad53210209e7fd41dfb5c2f8dc72eb30358ac100ea8c72da18847befe06eade68cebfcb9210327da12b5c40200e9f65569476bbff2218da4f32548ff43b6387ec1416a231ee821026ce35b29147ad09e4afe4ec4a7319095f08198fa8babbe3c56e970b143528d2221038dddc06ce687677a53d54f096d2591ba2302068cf123c1f2d75c2dddc542557921039dafd8571a641058ccc832c5e2111ea39b09c0bde36050914384f7a48bce9bf955ae0200000000000000004000565b7b276c616e67273a277a682d434e272c276e616d65273a27e5b08fe89a81e882a128e6b58be8af9529277d2c7b276c616e67273a27656e272c276e616d65273a27416e74536861726528546573744e657429277d5d0000c16ff2862300d5b21f2e11a9795a22a482f342f53634b5a8cf3ad5b21f2e11a9795a22a482f342f53634b5a8cf3a000001fd4501409adbbd94f33abe4796861f904d3a3804c434e7795944fbff5241418c2a408f127691197f8dd3ea09d5dacd14cdcb5496d4803889dc6e36a739b2b88cea270473401f5c056b1bad0f92b97b471feebaeb277ece3abdd1478b723950aaeff6ae4202913ea7f9279600054df03c66cc889f6e924e76e57d9e00c706e0a3670b70008540908ea943aed5d1c78db36fcfe00d49d7aafe469a0f3afba16832c85f79641976631e5a89b2489f9c888e0e65a3cf8e1e4d1d207068c140ca56c33defb48dced040137416609c6c92663d741d86162f67b54e9523e1bbb36fe8f63fc15868bc88edc42a19851336b18336327d15607fa12a3bd483b83b9db95d48e2b60459fdf3aa40e6d6d8670e6687ba8751f4c5c241e6c2db038079b43ec201f68127f0b227fc99cf92037941ad2845c6beed85f683a5ba19e7400cc93ed824c16a967c471f2134ad53210209e7fd41dfb5c2f8dc72eb30358ac100ea8c72da18847befe06eade68cebfcb9210327da12b5c40200e9f65569476bbff2218da4f32548ff43b6387ec1416a231ee821026ce35b29147ad09e4afe4ec4a7319095f08198fa8babbe3c56e970b143528d2221038dddc06ce687677a53d54f096d2591ba2302068cf123c1f2d75c2dddc542557921039dafd8571a641058ccc832c5e2111ea39b09c0bde36050914384f7a48bce9bf955ae".HexToBytes().AsSerializable<Block>();
public static readonly Block GenesisBlock = "0000000000000000000000000000000000000000000000000000000000000000000000003e237594e6e2b0cdd92aea5beecab6662662820597f1424a935c4dfc362bfafc81fc7856000000001dac2b7c00000000d5b21f2e11a9795a22a482f342f53634b5a8cf3afd450140af61f3ede868743262d6efe56dec7fcf6f9c75f42e4289e9165d26a08b870fc65f8a3b2b72f52fed206b9f215f1b993ebb3fce0a27b70d8de9782edb900b12094027bec17f618362a3beb8da43850d192880ab7e77c6d0c7bd12a81669f8afc38fca431adc9c4603ae2a70390ab0945b3484fa6c405742b409be9eb952ada981e8408d4584f68a66962e5a6b4454847bab196776432331a8ee2e538de117f7b4d8371dbcf177581b9e02c7db07253fd653fb249b60597a737d565d6b1209805bbfce40f9a17482f956e0567378f444f2f5256b4c15d4a425302ff96da3530d4e07c47eb08d32ebdd104db01bfb09e4c34292a94d26cdc8b6efd3a86f6fbceff44bfb59404dacce9dda32ec14f17448d6422f95dab8876d75371004183909819090021c58d71c070c66d82591a2a21a40d5cd2af12121b33403280735de1bc6aaf234dd71ad53210209e7fd41dfb5c2f8dc72eb30358ac100ea8c72da18847befe06eade68cebfcb9210327da12b5c40200e9f65569476bbff2218da4f32548ff43b6387ec1416a231ee821026ce35b29147ad09e4afe4ec4a7319095f08198fa8babbe3c56e970b143528d2221038dddc06ce687677a53d54f096d2591ba2302068cf123c1f2d75c2dddc542557921039dafd8571a641058ccc832c5e2111ea39b09c0bde36050914384f7a48bce9bf955ae020000000000000000004000565b7b276c616e67273a277a682d434e272c276e616d65273a27e5b08fe89a81e882a128e6b58be8af9529277d2c7b276c616e67273a27656e272c276e616d65273a27416e74536861726528546573744e657429277d5d0000c16ff28623000327da12b5c40200e9f65569476bbff2218da4f32548ff43b6387ec1416a231ee8d5b21f2e11a9795a22a482f342f53634b5a8cf3a000000024140925b0d1dc68ceb21337886439f2cd40009746fe3332b9149d0214ae7c9d3c9d2056d4adb8d5d14d7ff07447b3c71032508b36b81b7014921c819a042dbfbb60a23210327da12b5c40200e9f65569476bbff2218da4f32548ff43b6387ec1416a231ee8acfd450140c85d5de0f90a7ae9e268ef2b092a63d39c9022c7fce44714a7c9171d58304712cbe90cca5718c210ccf6e99cc3a0a1ac27ea52a1f3b18c1d60b9c4842c88153c4049a8674b3637b7b72e2950b75b8333d0294be7d46fe44309e1cc9d47ffa1e55c3b2d9968267316b1425a54369e55c2103915f064a2d479cd34bf665f2ef0acba40c397b172d90401dd38e965489e36dcdf1129cc28c387dfc2d19d2a7d1c9c35e8344fda9bafc7b80be652a892f506ae98013111d2e332a6afe1bf2a98bb92aee34023e4565d362b9d8708c5a661c9b5f3973d4deaa24be18122a97c68286aed76523d79f13afc1ffd75b369afe799c6516ed25d3862c2d6dc9693f9bc360563410f40819abbd4c52c98360f0b54b67e62e1726a3a5704cf4d09ce0a270226ac22d4397158785d5c5b27b05a041a7b52cd993722afff807b9ed227799c06fe6d321e51ad53210209e7fd41dfb5c2f8dc72eb30358ac100ea8c72da18847befe06eade68cebfcb9210327da12b5c40200e9f65569476bbff2218da4f32548ff43b6387ec1416a231ee821026ce35b29147ad09e4afe4ec4a7319095f08198fa8babbe3c56e970b143528d2221038dddc06ce687677a53d54f096d2591ba2302068cf123c1f2d75c2dddc542557921039dafd8571a641058ccc832c5e2111ea39b09c0bde36050914384f7a48bce9bf955ae".HexToBytes().AsSerializable<Block>();
public static readonly RegisterTransaction AntShare = (RegisterTransaction)GenesisBlock.Transactions[1];
public static readonly RegisterTransaction AntCoin = new RegisterTransaction
{
Expand All @@ -39,6 +39,7 @@ public abstract class Blockchain : IDisposable
Amount = Fixed8.FromDecimal(100000000),
Issuer = ECCurve.Secp256r1.Infinity,
Admin = new UInt160(),
Attributes = new TransactionAttribute[0],
Inputs = new TransactionInput[0],
Outputs = new TransactionOutput[0],
Scripts = new Script[0]
Expand Down
35 changes: 1 addition & 34 deletions AntSharesCore/Core/ContractTransaction.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,10 @@
using AntShares.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace AntShares.Core
namespace AntShares.Core
{
public class ContractTransaction : Transaction
{
public TransactionAttribute[] Attributes;

public ContractTransaction()
: base(TransactionType.ContractTransaction)
{
}

protected override void DeserializeExclusiveData(BinaryReader reader)
{
this.Attributes = reader.ReadSerializableArray<TransactionAttribute>();
}

public override UInt160[] GetScriptHashesForVerifying()
{
HashSet<UInt160> hashes = new HashSet<UInt160>(base.GetScriptHashesForVerifying());
foreach (var group in Outputs.GroupBy(p => p.AssetId))
{
RegisterTransaction tx = Blockchain.Default.GetTransaction(group.Key) as RegisterTransaction;
if (tx == null) throw new InvalidOperationException();
if (tx.AssetType == AssetType.Share)
{
hashes.UnionWith(group.Select(p => p.ScriptHash));
}
}
return hashes.OrderBy(p => p).ToArray();
}

protected override void SerializeExclusiveData(BinaryWriter writer)
{
writer.Write(Attributes);
}
}
}
21 changes: 4 additions & 17 deletions AntSharesCore/Core/IssueTransaction.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,30 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace AntShares.Core
{
public class IssueTransaction : Transaction
{
public override Fixed8 SystemFee => Outputs.All(p => p.AssetId == Blockchain.AntShare.Hash) ? Fixed8.Zero : Fixed8.FromDecimal(500);
public override Fixed8 SystemFee => Outputs.All(p => p.AssetId == Blockchain.AntShare.Hash || p.AssetId == Blockchain.AntCoin.Hash) ? Fixed8.Zero : Fixed8.FromDecimal(500);

public IssueTransaction()
: base(TransactionType.IssueTransaction)
{
}

protected override void DeserializeExclusiveData(BinaryReader reader)
: base(TransactionType.IssueTransaction)
{
}

public override UInt160[] GetScriptHashesForVerifying()
{
HashSet<UInt160> hashes = new HashSet<UInt160>(base.GetScriptHashesForVerifying());
foreach (var group in Outputs.GroupBy(p => p.AssetId))
foreach (TransactionResult result in GetTransactionResults().Where(p => p.Amount < Fixed8.Zero))
{
RegisterTransaction tx = Blockchain.Default.GetTransaction(group.Key) as RegisterTransaction;
RegisterTransaction tx = Blockchain.Default.GetTransaction(result.AssetId) as RegisterTransaction;
if (tx == null) throw new InvalidOperationException();
hashes.Add(tx.Admin);
if (tx.AssetType == AssetType.Share)
{
hashes.UnionWith(group.Select(p => p.ScriptHash));
}
}
return hashes.OrderBy(p => p).ToArray();
}

protected override void SerializeExclusiveData(BinaryWriter writer)
{
}

public override bool Verify()
{
if (!base.Verify()) return false;
Expand Down
15 changes: 4 additions & 11 deletions AntSharesCore/Core/RegisterTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class RegisterTransaction : Transaction
public ECPoint Issuer;
public UInt160 Admin;

private static readonly string ShareName = "[{'lang':'zh-CN','name':'股权'},{'lang':'en','name':'Share'}]";

public override Fixed8 SystemFee => Fixed8.FromDecimal(10000);

public RegisterTransaction()
Expand Down Expand Up @@ -55,16 +57,10 @@ protected override void DeserializeExclusiveData(BinaryReader reader)
private Dictionary<CultureInfo, string> _names;
public string GetName(CultureInfo culture = null)
{
if (AssetType == AssetType.Share)
{
//TODO: 获取证书上的名称
//股权的名称由证书上的公司名称决定,不能自定义
//目前实名认证的相关设计还没有完全定型,所以暂时先不实现股权资产的名称查询
throw new NotImplementedException();
}
string name_str = AssetType == AssetType.Share ? ShareName : Name;
if (_names == null)
{
_names = ((JArray)JObject.Parse(Name)).ToDictionary(p => CultureInfo.GetCultureInfo(p["lang"].AsString()), p => p["name"].AsString());
_names = ((JArray)JObject.Parse(name_str)).ToDictionary(p => CultureInfo.GetCultureInfo(p["lang"].AsString()), p => p["name"].AsString());
}
if (culture == null) culture = CultureInfo.CurrentCulture;
if (_names.ContainsKey(culture))
Expand Down Expand Up @@ -98,9 +94,6 @@ protected override void SerializeExclusiveData(BinaryWriter writer)

public override string ToString()
{
//TODO: 在资产名称的后面加上发行者的名称
//如:CNY(由xxx公司发行)
//用以区分不同主体发行的相同名称的资产
return GetName();
}
}
Expand Down
23 changes: 20 additions & 3 deletions AntSharesCore/Core/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace AntShares.Core
public abstract class Transaction : Inventory, ISignable
{
public readonly TransactionType Type;
public TransactionAttribute[] Attributes;
public TransactionInput[] Inputs;
public TransactionOutput[] Outputs;
public Script[] Scripts;
Expand Down Expand Up @@ -71,7 +72,9 @@ public override void Deserialize(BinaryReader reader)
Scripts = reader.ReadSerializableArray<Script>();
}

protected abstract void DeserializeExclusiveData(BinaryReader reader);
protected virtual void DeserializeExclusiveData(BinaryReader reader)
{
}

public static Transaction DeserializeFrom(byte[] value)
{
Expand Down Expand Up @@ -104,6 +107,7 @@ void ISignable.DeserializeUnsigned(BinaryReader reader)
private void DeserializeUnsignedWithoutType(BinaryReader reader)
{
DeserializeExclusiveData(reader);
Attributes = reader.ReadSerializableArray<TransactionAttribute>();
Inputs = reader.ReadSerializableArray<TransactionInput>();
TransactionInput[] inputs = GetAllInputs().ToArray();
for (int i = 1; i < inputs.Length; i++)
Expand All @@ -121,7 +125,17 @@ public virtual IEnumerable<TransactionInput> GetAllInputs()
public virtual UInt160[] GetScriptHashesForVerifying()
{
if (References == null) throw new InvalidOperationException();
return Inputs.Select(p => References[p].ScriptHash).OrderBy(p => p).ToArray();
HashSet<UInt160> hashes = new HashSet<UInt160>(Inputs.Select(p => References[p].ScriptHash));
foreach (var group in Outputs.GroupBy(p => p.AssetId))
{
RegisterTransaction tx = Blockchain.Default.GetTransaction(group.Key) as RegisterTransaction;
if (tx == null) throw new InvalidOperationException();
if (tx.AssetType == AssetType.Share)
{
hashes.UnionWith(group.Select(p => p.ScriptHash));
}
}
return hashes.OrderBy(p => p).ToArray();
}

public IEnumerable<TransactionResult> GetTransactionResults()
Expand Down Expand Up @@ -152,12 +166,15 @@ public override void Serialize(BinaryWriter writer)
writer.Write(Scripts);
}

protected abstract void SerializeExclusiveData(BinaryWriter writer);
protected virtual void SerializeExclusiveData(BinaryWriter writer)
{
}

void ISignable.SerializeUnsigned(BinaryWriter writer)
{
writer.Write((byte)Type);
SerializeExclusiveData(writer);
writer.Write(Attributes);
writer.Write(Inputs);
writer.Write(Outputs);
}
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/Cryptography/ECC/ECPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ public bool Equals(ECPoint other)
{
if (ReferenceEquals(this, other)) return true;
if (ReferenceEquals(null, other)) return false;
if (IsInfinity && other.IsInfinity) return true;
if (IsInfinity || other.IsInfinity) return false;
return X.Equals(other.X) && Y.Equals(other.Y);
}

Expand Down
5 changes: 5 additions & 0 deletions AntSharesCore/Fixed8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ public override string ToString()
return ToDecimal().ToString();
}

public string ToString(string format)
{
return ToDecimal().ToString(format);
}

public string ToString(string format, IFormatProvider formatProvider)
{
return ToDecimal().ToString(format, formatProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public LevelDBBlockchain(string path)
Version version;
Slice value;
db = DB.Open(path);
if (db.TryGet(ReadOptions.Default, SliceBuilder.Begin(DataEntryPrefix.CFG_Version), out value) && Version.TryParse(value.ToString(), out version) && version >= Version.Parse("0.3"))
if (db.TryGet(ReadOptions.Default, SliceBuilder.Begin(DataEntryPrefix.CFG_Version), out value) && Version.TryParse(value.ToString(), out version) && version >= Version.Parse("0.3.5834.28142"))
{
ReadOptions options = new ReadOptions { FillCache = false };
value = db.Get(options, SliceBuilder.Begin(DataEntryPrefix.SYS_CurrentBlock));
Expand Down
3 changes: 0 additions & 3 deletions AntSharesCore/Implementations/Wallets/EntityFramework/Key.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
{
internal class Key
{
public const string MasterKey = "MasterKey";
public const string IV = "IV";

public string Name { get; set; }
public byte[] Value { get; set; }
}
Expand Down
9 changes: 7 additions & 2 deletions AntSharesCore/Network/LocalNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,13 @@ public void Start(int port = DEFAULT_PORT)
{
if (Interlocked.Exchange(ref started, 1) == 0)
{
IPHostEntry localhost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress address = localhost.AddressList.FirstOrDefault(p => p.AddressFamily == AddressFamily.InterNetwork && !IPAddress.IsLoopback(p) && !IsIntranetAddress(p));
IPHostEntry localhost = null;
try
{
localhost = Dns.GetHostEntry(Dns.GetHostName());
}
catch (SocketException) { }
IPAddress address = localhost?.AddressList.FirstOrDefault(p => p.AddressFamily == AddressFamily.InterNetwork && !IPAddress.IsLoopback(p) && !IsIntranetAddress(p));
if (address == null && UpnpEnabled && UPnP.Discover())
{
address = UPnP.GetExternalIP();
Expand Down
5 changes: 5 additions & 0 deletions AntSharesCore/Network/RemoteNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,11 @@ private void RunProtocol()
{
OnMessageReceived(message);
}
catch (EndOfStreamException)
{
Disconnect(true);
break;
}
catch (FormatException)
{
Disconnect(true);
Expand Down
Loading