Skip to content

Commit

Permalink
get more information from rpc server; move rpc uri_prefix into config…
Browse files Browse the repository at this point in the history
… file;
  • Loading branch information
Erik Zhang committed Oct 22, 2016
1 parent 1aa2344 commit 827b062
Show file tree
Hide file tree
Showing 39 changed files with 140 additions and 26 deletions.
2 changes: 2 additions & 0 deletions AntSharesCore/Consensus/ConsensusMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ internal abstract class ConsensusMessage : ISerializable
public readonly ConsensusMessageType Type;
public byte ViewNumber;

public int Size => sizeof(ConsensusMessageType) + sizeof(byte);

protected ConsensusMessage(ConsensusMessageType type)
{
this.Type = type;
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/Core/AgencyTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class AgencyTransaction : Transaction
/// </summary>
public SplitOrder SplitOrder;

public override int Size => base.Size + AssetId.Size + ValueAssetId.Size + Agent.Size + Orders.Length.GetVarSize() + Orders.Sum(p => p.SizeInTransaction) + SplitOrder.Size;

public AgencyTransaction()
: base(TransactionType.AgencyTransaction)
{
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/Core/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public Header Header
/// </summary>
InventoryType IInventory.InventoryType => InventoryType.Block;

public override int Size => base.Size + Transactions.Length.GetVarSize() + Transactions.Sum(p => p.Size);

public static Fixed8 CalculateNetFee(IEnumerable<Transaction> transactions)
{
Transaction[] ts = transactions.Where(p => p.Type != TransactionType.MinerTransaction && p.Type != TransactionType.ClaimTransaction).ToArray();
Expand Down
3 changes: 3 additions & 0 deletions AntSharesCore/Core/BlockBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Script[] ISignable.Scripts
}
}

public virtual int Size => sizeof(uint) + PrevBlock.Size + MerkleRoot.Size + sizeof(uint) + sizeof(uint) + sizeof(ulong) + NextMiner.Size + 1 + Script.Size;

public virtual void Deserialize(BinaryReader reader)
{
((ISignable)this).DeserializeUnsigned(reader);
Expand Down Expand Up @@ -118,6 +120,7 @@ public virtual JObject ToJson()
{
JObject json = new JObject();
json["hash"] = Hash.ToString();
json["size"] = Size;
json["version"] = Version;
json["previousblockhash"] = PrevBlock.ToString();
json["merkleroot"] = MerkleRoot.ToString();
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/Core/ClaimTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class ClaimTransaction : Transaction
/// </summary>
public TransactionInput[] Claims;

public override int Size => base.Size + Claims.Length.GetVarSize() + Claims.Sum(p => p.Size);

public ClaimTransaction()
: base(TransactionType.ClaimTransaction)
{
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/Core/EnrollmentTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public UInt160 Miner
}
}

public override int Size => base.Size + PublicKey.Size;

/// <summary>
/// 系统费用
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/Core/Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace AntShares.Core
{
public class Header : BlockBase, IEquatable<Header>
{
public override int Size => base.Size + 1;

public override void Deserialize(BinaryReader reader)
{
base.Deserialize(reader);
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/Core/MinerTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class MinerTransaction : Transaction
/// </summary>
public uint Nonce;

public override int Size => base.Size + sizeof(uint);

public MinerTransaction()
: base(TransactionType.MinerTransaction)
{
Expand Down
16 changes: 4 additions & 12 deletions AntSharesCore/Core/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,11 @@ public class Order : ISignable
/// <summary>
/// 用于验证该订单的脚本列表
/// </summary>
public Script[] Scripts;
public Script[] Scripts { get; set; }

Script[] ISignable.Scripts
{
get
{
return Scripts;
}
set
{
Scripts = value;
}
}
public int Size => AssetId.Size + ValueAssetId.Size + Agent.Size + SizeInTransaction;

public int SizeInTransaction => Amount.Size + Price.Size + Client.Size + Inputs.Length.GetVarSize() + Inputs.Sum(p => p.Size) + Scripts.Length.GetVarSize() + Scripts.Sum(p => p.Size);

void ISerializable.Deserialize(BinaryReader reader)
{
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/Core/PublishTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class PublishTransaction : Transaction
{
public byte[][] Contracts;

public override int Size => base.Size + Contracts.Length.GetVarSize() + Contracts.Sum(p => p.Length.GetVarSize() + p.Length);

public override Fixed8 SystemFee => Fixed8.FromDecimal(500 * Contracts.Length);

public PublishTransaction()
Expand Down
3 changes: 3 additions & 0 deletions AntSharesCore/Core/RegisterTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;

namespace AntShares.Core
{
Expand Down Expand Up @@ -42,6 +43,8 @@ public class RegisterTransaction : Transaction

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

public override int Size => base.Size + sizeof(AssetType) + Encoding.UTF8.GetByteCount(Name).GetVarSize() + Encoding.UTF8.GetByteCount(Name) + Amount.Size + sizeof(byte) + Issuer.Size + Admin.Size;

/// <summary>
/// 系统费用
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/Core/Scripts/Script.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class Script : ISerializable
public byte[] StackScript;
public byte[] RedeemScript;

public int Size => StackScript.Length.GetVarSize() + StackScript.Length + RedeemScript.Length.GetVarSize() + RedeemScript.Length;

void ISerializable.Deserialize(BinaryReader reader)
{
StackScript = reader.ReadVarBytes();
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/Core/SplitOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class SplitOrder : ISerializable
/// </summary>
public UInt160 Client;

public int Size => Amount.Size + Price.Size + Client.Size;

void ISerializable.Deserialize(BinaryReader reader)
{
this.Amount = reader.ReadSerializable<Fixed8>();
Expand Down
3 changes: 3 additions & 0 deletions AntSharesCore/Core/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public IReadOnlyDictionary<TransactionInput, TransactionOutput> References
}
}

public virtual int Size => sizeof(TransactionType) + sizeof(byte) + Attributes.Length.GetVarSize() + Attributes.Sum(p => p.Size) + Inputs.Length.GetVarSize() + Inputs.Sum(p => p.Size) + Outputs.Length.GetVarSize() + Outputs.Sum(p => p.Size) + Scripts.Length.GetVarSize() + Scripts.Sum(p => p.Size);

/// <summary>
/// 系统费用
/// </summary>
Expand Down Expand Up @@ -298,6 +300,7 @@ public virtual JObject ToJson()
{
JObject json = new JObject();
json["txid"] = Hash.ToString();
json["size"] = Size;
json["type"] = Type;
json["version"] = Version;
json["attributes"] = Attributes.Select(p => p.ToJson()).ToArray();
Expand Down
11 changes: 11 additions & 0 deletions AntSharesCore/Core/TransactionAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ public class TransactionAttribute : ISerializable
/// </summary>
public byte[] Data;

public int Size
{
get
{
if (Usage == TransactionAttributeUsage.ContractHash || Usage == TransactionAttributeUsage.ECDH02 || Usage == TransactionAttributeUsage.ECDH03 || Usage == TransactionAttributeUsage.Vote || (Usage >= TransactionAttributeUsage.Hash1 && Usage <= TransactionAttributeUsage.Hash15))
return sizeof(TransactionAttributeUsage) + 32;
else
return sizeof(TransactionAttributeUsage) + Data.Length.GetVarSize() + Data.Length;
}
}

void ISerializable.Deserialize(BinaryReader reader)
{
Usage = (TransactionAttributeUsage)reader.ReadByte();
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/Core/TransactionInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class TransactionInput : IEquatable<TransactionInput>, ISerializable
/// </summary>
public ushort PrevIndex;

public int Size => PrevHash.Size + sizeof(ushort);

void ISerializable.Deserialize(BinaryReader reader)
{
PrevHash = reader.ReadSerializable<UInt256>();
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/Core/TransactionOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class TransactionOutput : ISerializable
/// </summary>
public UInt160 ScriptHash;

public int Size => AssetId.Size + Value.Size + ScriptHash.Size;

void ISerializable.Deserialize(BinaryReader reader)
{
this.AssetId = reader.ReadSerializable<UInt256>();
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 @@ -20,6 +20,8 @@ public bool IsInfinity
get { return X == null && Y == null; }
}

public int Size => IsInfinity ? 1 : 33;

internal ECPoint(ECFieldElement x, ECFieldElement y, ECCurve curve)
{
if ((x != null && y == null) || (x == null && y != null))
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/Fixed8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public struct Fixed8 : IComparable<Fixed8>, IEquatable<Fixed8>, IFormattable, IS

public static readonly Fixed8 Zero = new Fixed8();

public int Size => sizeof(long);

public Fixed8(long data)
{
this.value = data;
Expand Down
10 changes: 10 additions & 0 deletions AntSharesCore/IO/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public static ISerializable AsSerializable(this byte[] value, Type type)
return serializable;
}

public static int GetVarSize(this int value)
{
if (value < 0xFD)
return sizeof(byte);
else if (value <= 0xFFFF)
return sizeof(byte) + sizeof(ushort);
else
return sizeof(byte) + sizeof(uint);
}

public static string ReadFixedString(this BinaryReader reader, int length)
{
byte[] data = reader.ReadBytes(length);
Expand Down
4 changes: 3 additions & 1 deletion AntSharesCore/IO/ISerializable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ namespace AntShares.IO
/// </summary>
public interface ISerializable
{
int Size { get; }

/// <summary>
/// 序列化
/// </summary>
/// <param name="writer">存放序列化后的结果</param>
void Serialize(BinaryWriter writer);

/// <summary>
/// 反序列化
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/Network/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ internal class Message : ISerializable
public uint Checksum;
public byte[] Payload;

public int Size => sizeof(uint) + 12 + sizeof(int) + sizeof(uint) + Payload.Length;

public static Message Create(string command, ISerializable payload = null)
{
return Create(command, payload == null ? new byte[0] : payload.ToArray());
Expand Down
3 changes: 3 additions & 0 deletions AntSharesCore/Network/Payloads/AddrPayload.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using AntShares.IO;
using System.IO;
using System.Linq;

namespace AntShares.Network.Payloads
{
internal class AddrPayload : ISerializable
{
public NetworkAddressWithTime[] AddressList;

public int Size => AddressList.Length.GetVarSize() + AddressList.Sum(p => p.Size);

public static AddrPayload Create(params NetworkAddressWithTime[] addresses)
{
return new AddrPayload
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/Network/Payloads/ConsensusPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Script[] ISignable.Scripts
}
}

public int Size => sizeof(uint) + PrevHash.Size + sizeof(uint) + sizeof(ushort) + sizeof(uint) + Data.Length.GetVarSize() + Data.Length + 1 + Script.Size;

void ISerializable.Deserialize(BinaryReader reader)
{
((ISignable)this).DeserializeUnsigned(reader);
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/Network/Payloads/FilterAddPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ internal class FilterAddPayload : ISerializable
{
public byte[] Data;

public int Size => Data.Length.GetVarSize() + Data.Length;

void ISerializable.Deserialize(BinaryReader reader)
{
Data = reader.ReadVarBytes(520);
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/Network/Payloads/FilterLoadPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ internal class FilterLoadPayload : ISerializable
public byte K;
public uint Tweak;

public int Size => Filter.Length.GetVarSize() + Filter.Length + sizeof(byte) + sizeof(uint);

public static FilterLoadPayload Create(BloomFilter filter)
{
byte[] buffer = new byte[filter.M / 8];
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/Network/Payloads/GetBlocksPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ internal class GetBlocksPayload : ISerializable
public UInt256[] HashStart;
public UInt256 HashStop;

public int Size => HashStart.Length.GetVarSize() + HashStart.Sum(p => p.Size) + HashStop.Size;

public static GetBlocksPayload Create(IEnumerable<UInt256> hash_start, UInt256 hash_stop = null)
{
return new GetBlocksPayload
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/Network/Payloads/HeadersPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ internal class HeadersPayload : ISerializable
{
public Header[] Headers;

public int Size => Headers.Length.GetVarSize() + Headers.Sum(p => p.Size);

public static HeadersPayload Create(IEnumerable<Header> headers)
{
return new HeadersPayload
Expand Down
3 changes: 3 additions & 0 deletions AntSharesCore/Network/Payloads/InvPayload.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using AntShares.IO;
using System;
using System.IO;
using System.Linq;

namespace AntShares.Network.Payloads
{
Expand All @@ -9,6 +10,8 @@ internal class InvPayload : ISerializable
public InventoryType Type;
public UInt256[] Hashes;

public int Size => sizeof(InventoryType) + Hashes.Length.GetVarSize() + Hashes.Sum(p => p.Size);

public static InvPayload Create(InventoryType type, params UInt256[] hashes)
{
return new InvPayload
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/Network/Payloads/MerkleBlockPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ internal class MerkleBlockPayload : BlockBase
public UInt256[] Hashes;
public byte[] Flags;

public override int Size => base.Size + sizeof(int) + Hashes.Length.GetVarSize() + Hashes.Sum(p => p.Size) + Flags.Length.GetVarSize() + Flags.Length;

public static MerkleBlockPayload Create(Block block, BitArray flags)
{
MerkleTree tree = new MerkleTree(block.Transactions.Select(p => p.Hash).ToArray());
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/Network/Payloads/NetworkAddressWithTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ internal class NetworkAddressWithTime : ISerializable
public ulong Services;
public IPEndPoint EndPoint;

public int Size => sizeof(uint) + sizeof(ulong) + 16 + sizeof(ushort);

public static NetworkAddressWithTime Create(IPEndPoint endpoint, ulong services, uint timestamp)
{
return new NetworkAddressWithTime
Expand Down
3 changes: 3 additions & 0 deletions AntSharesCore/Network/Payloads/VersionPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using AntShares.IO;
using System;
using System.IO;
using System.Text;

namespace AntShares.Network.Payloads
{
Expand All @@ -16,6 +17,8 @@ internal class VersionPayload : ISerializable
public uint StartHeight;
public bool Relay;

public int Size => sizeof(uint) + sizeof(ulong) + sizeof(uint) + sizeof(ushort) + sizeof(uint) + Encoding.UTF8.GetByteCount(UserAgent).GetVarSize() + Encoding.UTF8.GetByteCount(UserAgent) + sizeof(uint) + sizeof(bool);

public static VersionPayload Create(int port, uint nonce, string userAgent)
{
return new VersionPayload
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/UIntBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public abstract class UIntBase : IEquatable<UIntBase>, ISerializable
{
private byte[] data_bytes;

public int Size => data_bytes.Length;

protected UIntBase(int bytes, byte[] value)
{
if (value == null)
Expand Down
2 changes: 2 additions & 0 deletions AntSharesCore/Wallets/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public UInt160 ScriptHash
}
}

public int Size => PublicKeyHash.Size + ParameterList.Length.GetVarSize() + ParameterList.Length + RedeemScript.Length.GetVarSize() + RedeemScript.Length;

public static Contract Create(UInt160 publicKeyHash, ContractParameterType[] parameterList, byte[] redeemScript)
{
return new Contract
Expand Down
Loading

0 comments on commit 827b062

Please sign in to comment.