Skip to content

Commit

Permalink
Merge branch 'testnet' into testnet-coreclr
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Zhang committed Oct 5, 2016
2 parents 3142b66 + 8030c49 commit 05a6242
Show file tree
Hide file tree
Showing 18 changed files with 146 additions and 98 deletions.
35 changes: 12 additions & 23 deletions AntSharesCore/Core/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ public abstract class Blockchain : IDisposable
{
AssetType = AssetType.AntShare,
#if TESTNET
Name = "[{\"lang\":\"zh-CN\",\"name\":\"小蚁股(测试)\"},{\"lang\":\"en\",\"name\":\"AntShare(TestNet)\"}]",
Name = "[{'lang':'zh-CN','name':'小蚁股(测试)'},{'lang':'en','name':'AntShare(TestNet)'}]",
#else
Name = "[{\"lang\":\"zh-CN\",\"name\":\"小蚁股\"},{\"lang\":\"en\",\"name\":\"AntShare\"}]",
Name = "[{'lang':'zh-CN','name':'小蚁股'},{'lang':'en','name':'AntShare'}]",
#endif
Amount = Fixed8.FromDecimal(100000000),
Precision = 0,
Issuer = ECCurve.Secp256r1.Infinity,
Issuer = ECPoint.DecodePoint((new[] { (byte)0x02 }).Concat(ECCurve.Secp256r1.G.EncodePoint(false).Skip(1).Sha256().Sha256()).ToArray(), ECCurve.Secp256r1),
Admin = (new[] { (byte)ScriptOp.OP_TRUE }).ToScriptHash(),
Attributes = new TransactionAttribute[0],
Inputs = new TransactionInput[0],
Outputs = new TransactionOutput[0]
Outputs = new TransactionOutput[0],
Scripts = new Script[0]
};

/// <summary>
Expand All @@ -86,17 +86,17 @@ public abstract class Blockchain : IDisposable
{
AssetType = AssetType.AntCoin,
#if TESTNET
Name = "[{\"lang\":\"zh-CN\",\"name\":\"小蚁币(测试)\"},{\"lang\":\"en\",\"name\":\"AntCoin(TestNet)\"}]",
Name = "[{'lang':'zh-CN','name':'小蚁币(测试)'},{'lang':'en','name':'AntCoin(TestNet)'}]",
#else
Name = "[{\"lang\":\"zh-CN\",\"name\":\"小蚁币\"},{\"lang\":\"en\",\"name\":\"AntCoin\"}]",
Name = "[{'lang':'zh-CN','name':'小蚁币'},{'lang':'en','name':'AntCoin'}]",
#endif
Amount = Fixed8.FromDecimal(MintingAmount.Sum(p => p * DecrementInterval)),
Precision = 8,
Issuer = ECCurve.Secp256r1.Infinity,
Issuer = ECPoint.DecodePoint((new[] { (byte)0x02 }).Concat(ECCurve.Secp256r1.G.EncodePoint(false).Skip(1).Sha256().Sha256()).ToArray(), ECCurve.Secp256r1),
Admin = (new[] { (byte)ScriptOp.OP_FALSE }).ToScriptHash(),
Attributes = new TransactionAttribute[0],
Inputs = new TransactionInput[0],
Outputs = new TransactionOutput[0]
Outputs = new TransactionOutput[0],
Scripts = new Script[0]
};

/// <summary>
Expand Down Expand Up @@ -128,6 +128,7 @@ public abstract class Blockchain : IDisposable
AntCoin,
new IssueTransaction
{
Nonce = 2083236893,
Attributes = new TransactionAttribute[0],
Inputs = new TransactionInput[0],
Outputs = new[]
Expand Down Expand Up @@ -182,18 +183,6 @@ public abstract class Blockchain : IDisposable

static Blockchain()
{
AntShare.Scripts = new[] { new Script { RedeemScript = Contract.CreateSignatureRedeemScript(AntShare.Issuer) } };
using (ScriptBuilder sb = new ScriptBuilder())
{
sb.Push(ECCurve.Secp256r1.G.EncodePoint(true).Skip(1).Concat(AntShare.GetHashForSigning()).ToArray());
AntShare.Scripts[0].StackScript = sb.ToArray();
}
AntCoin.Scripts = new[] { new Script { RedeemScript = Contract.CreateSignatureRedeemScript(AntCoin.Issuer) } };
using (ScriptBuilder sb = new ScriptBuilder())
{
sb.Push(ECCurve.Secp256r1.G.EncodePoint(true).Skip(1).Concat(AntCoin.GetHashForSigning()).ToArray());
AntCoin.Scripts[0].StackScript = sb.ToArray();
}
GenesisBlock.RebuildMerkleRoot();
}

Expand Down Expand Up @@ -349,7 +338,7 @@ public virtual IEnumerable<ECPoint> GetMiners(IEnumerable<Transaction> others)
Dictionary<UInt256, ECPoint> enrollments = GetEnrollments(others).ToDictionary(p => p.Hash, p => p.PublicKey);
foreach (var vote in votes)
{
foreach (UInt256 hash in vote.Enrollments.Take(miner_count))
foreach (UInt256 hash in vote.Enrollments)
{
if (!enrollments.ContainsKey(hash)) continue;
ECPoint pubkey = enrollments[hash];
Expand Down
24 changes: 24 additions & 0 deletions AntSharesCore/Core/IssueTransaction.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using AntShares.Network;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace AntShares.Core
Expand All @@ -10,6 +11,11 @@ namespace AntShares.Core
/// </summary>
public class IssueTransaction : Transaction
{
/// <summary>
/// 随机数
/// </summary>
public uint Nonce;

/// <summary>
/// 系统费用
/// </summary>
Expand All @@ -32,6 +38,15 @@ public IssueTransaction()
{
}

/// <summary>
/// 反序列化交易中的额外数据
/// </summary>
/// <param name="reader">数据来源</param>
protected override void DeserializeExclusiveData(BinaryReader reader)
{
this.Nonce = reader.ReadUInt32();
}

/// <summary>
/// 获取需要校验的脚本散列值
/// </summary>
Expand All @@ -48,6 +63,15 @@ public override UInt160[] GetScriptHashesForVerifying()
return hashes.OrderBy(p => p).ToArray();
}

/// <summary>
/// 序列化交易中的额外数据
/// </summary>
/// <param name="writer">存放序列化后的结果</param>
protected override void SerializeExclusiveData(BinaryWriter writer)
{
writer.Write(Nonce);
}

/// <summary>
/// 验证交易
/// </summary>
Expand Down
19 changes: 7 additions & 12 deletions AntSharesCore/Core/RegisterTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public class RegisterTransaction : Transaction
/// 2. 不限量模式:当Amount等于-1时,表示当前资产可以由创建者无限量发行。这种模式的自由度最大,但是公信力最低,不建议使用。
/// </summary>
public Fixed8 Amount;
public byte Precision;
/// <summary>
/// 发行者的公钥
/// </summary>
Expand All @@ -40,7 +39,7 @@ public class RegisterTransaction : Transaction
/// </summary>
public UInt160 Admin;

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

/// <summary>
/// 系统费用
Expand Down Expand Up @@ -70,20 +69,18 @@ public RegisterTransaction()
/// <param name="reader">数据来源</param>
protected override void DeserializeExclusiveData(BinaryReader reader)
{
AssetType = (AssetType)reader.ReadByte();
this.AssetType = (AssetType)reader.ReadByte();
if (!Enum.IsDefined(typeof(AssetType), AssetType) || AssetType == AssetType.CreditFlag || AssetType == AssetType.DutyFlag)
throw new FormatException();
Name = reader.ReadVarString();
Amount = reader.ReadSerializable<Fixed8>();
this.Name = reader.ReadVarString();
this.Amount = reader.ReadSerializable<Fixed8>();
if (Amount == Fixed8.Zero || Amount < -Fixed8.Satoshi) throw new FormatException();
if (AssetType == AssetType.Share && Amount <= Fixed8.Zero)
throw new FormatException();
if (AssetType == AssetType.Invoice && Amount != -Fixed8.Satoshi)
throw new FormatException();
Precision = reader.ReadByte();
if (Precision > 8) throw new FormatException();
Issuer = ECPoint.DeserializeFrom(reader, ECCurve.Secp256r1);
Admin = reader.ReadSerializable<UInt160>();
this.Issuer = ECPoint.DeserializeFrom(reader, ECCurve.Secp256r1);
this.Admin = reader.ReadSerializable<UInt160>();
}

private Dictionary<CultureInfo, string> _names;
Expand Down Expand Up @@ -125,7 +122,7 @@ public string GetName(CultureInfo culture = null)
public override UInt160[] GetScriptHashesForVerifying()
{
UInt160 issuer = Contract.CreateSignatureRedeemScript(Issuer).ToScriptHash();
return base.GetScriptHashesForVerifying().Union(new[] { issuer }).OrderBy(p => p).ToArray();
return base.GetScriptHashesForVerifying().Union(new UInt160[] { issuer, Admin }).OrderBy(p => p).ToArray();
}

protected override void OnDeserialized()
Expand All @@ -146,7 +143,6 @@ protected override void SerializeExclusiveData(BinaryWriter writer)
writer.Write((byte)AssetType);
writer.WriteVarString(Name);
writer.Write(Amount);
writer.Write(Precision);
writer.Write(Issuer);
writer.Write(Admin);
}
Expand All @@ -169,7 +165,6 @@ public override JObject ToJson()
json["asset"]["name"] = Name;
}
json["asset"]["amount"] = Amount.ToString();
json["asset"]["precision"] = Precision;
json["asset"]["high"] = Amount.GetData() >> 32;
json["asset"]["low"] = Amount.GetData() & 0xffffffff;
json["asset"]["issuer"] = Issuer.ToString();
Expand Down
14 changes: 4 additions & 10 deletions AntSharesCore/Core/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ public abstract class Transaction : IInventory
/// </summary>
public readonly TransactionType Type;
/// <summary>
/// 版本
/// </summary>
public const byte Version = 0;
/// <summary>
/// 该交易所具备的额外特性
/// </summary>
public TransactionAttribute[] Attributes;
Expand Down Expand Up @@ -164,8 +160,6 @@ void ISignable.DeserializeUnsigned(BinaryReader reader)

private void DeserializeUnsignedWithoutType(BinaryReader reader)
{
if (reader.ReadByte() != Version)
throw new FormatException();
DeserializeExclusiveData(reader);
Attributes = reader.ReadSerializableArray<TransactionAttribute>();
if (Attributes.Select(p => p.Usage).Distinct().Count() != Attributes.Length)
Expand All @@ -179,6 +173,10 @@ private void DeserializeUnsignedWithoutType(BinaryReader reader)
Outputs = reader.ReadSerializableArray<TransactionOutput>();
if (Outputs.Length > ushort.MaxValue + 1)
throw new FormatException();
if (Blockchain.AntShare != null)
foreach (TransactionOutput output in Outputs.Where(p => p.AssetId == Blockchain.AntShare.Hash))
if (output.Value.GetData() % 100000000 != 0)
throw new FormatException();
}

public bool Equals(Transaction other)
Expand Down Expand Up @@ -277,7 +275,6 @@ protected virtual void SerializeExclusiveData(BinaryWriter writer)
void ISignable.SerializeUnsigned(BinaryWriter writer)
{
writer.Write((byte)Type);
writer.Write(Version);
SerializeExclusiveData(writer);
writer.Write(Attributes);
writer.Write(Inputs);
Expand Down Expand Up @@ -315,9 +312,6 @@ public virtual bool Verify()
{
RegisterTransaction asset = Blockchain.Default.GetTransaction(group.Key) as RegisterTransaction;
if (asset == null) return false;
foreach (TransactionOutput output in group)
if (output.Value.GetData() % (long)Math.Pow(10, 8 - asset.Precision) != 0)
return false;
}
TransactionResult[] results = GetTransactionResults()?.ToArray();
if (results == null) return false;
Expand Down
2 changes: 1 addition & 1 deletion AntSharesCore/Network/InventoryType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// 定义清单中的对象类型
/// </summary>
public enum InventoryType : byte
public enum InventoryType : uint
{
/// <summary>
/// 交易
Expand Down
24 changes: 13 additions & 11 deletions AntSharesCore/Network/Payloads/InvPayload.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
using AntShares.IO;
using System;
using System.IO;
using System.Linq;

namespace AntShares.Network.Payloads
{
internal class InvPayload : ISerializable
{
public InventoryType Type;
public UInt256[] Hashes;
public InventoryVector[] Inventories;

public static InvPayload Create(InventoryVector[] vectors)
{
return new InvPayload
{
Inventories = vectors
};
}

public static InvPayload Create(InventoryType type, params UInt256[] hashes)
{
return new InvPayload
{
Type = type,
Hashes = hashes
Inventories = hashes.Select(p => new InventoryVector { Type = type, Hash = p }).ToArray()
};
}

void ISerializable.Deserialize(BinaryReader reader)
{
Type = (InventoryType)reader.ReadByte();
if (!Enum.IsDefined(typeof(InventoryType), Type))
throw new FormatException();
Hashes = reader.ReadSerializableArray<UInt256>();
Inventories = reader.ReadSerializableArray<InventoryVector>();
}

void ISerializable.Serialize(BinaryWriter writer)
{
writer.Write((byte)Type);
writer.Write(Hashes);
writer.Write(Inventories);
}
}
}
44 changes: 44 additions & 0 deletions AntSharesCore/Network/Payloads/InventoryVector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using AntShares.IO;
using System;
using System.IO;

namespace AntShares.Network.Payloads
{
internal class InventoryVector : IEquatable<InventoryVector>, ISerializable
{
public InventoryType Type;
public UInt256 Hash;

void ISerializable.Deserialize(BinaryReader reader)
{
Type = (InventoryType)reader.ReadUInt32();
if (!Enum.IsDefined(typeof(InventoryType), Type))
throw new FormatException();
Hash = reader.ReadSerializable<UInt256>();
}

public override bool Equals(object obj)
{
if (!(obj is InventoryVector)) return false;
return Equals((InventoryVector)obj);
}

public bool Equals(InventoryVector other)
{
if (ReferenceEquals(other, null)) return false;
if (ReferenceEquals(this, other)) return true;
return Hash == other.Hash;
}

public override int GetHashCode()
{
return Hash.GetHashCode();
}

void ISerializable.Serialize(BinaryWriter writer)
{
writer.Write((uint)Type);
writer.Write(Hash);
}
}
}
4 changes: 1 addition & 3 deletions AntSharesCore/Network/Payloads/VersionPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ internal class VersionPayload : ISerializable
public uint Nonce;
public string UserAgent;
public uint StartHeight;
public bool Relay;

public static VersionPayload Create(int port, uint nonce, string userAgent)
{
Expand All @@ -26,8 +25,7 @@ public static VersionPayload Create(int port, uint nonce, string userAgent)
Port = (ushort)port,
Nonce = nonce,
UserAgent = userAgent,
StartHeight = Blockchain.Default?.Height ?? 0,
Relay = true
StartHeight = Blockchain.Default?.Height ?? 0
};
}

Expand Down
Loading

0 comments on commit 05a6242

Please sign in to comment.