Skip to content

Commit

Permalink
cli wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Zhang committed Oct 20, 2016
1 parent 1db0a21 commit f786d5c
Show file tree
Hide file tree
Showing 28 changed files with 955 additions and 2,334 deletions.
6 changes: 0 additions & 6 deletions AntShares.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AntSharesCore", "AntSharesC
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AntSharesUI", "AntSharesUI\AntSharesUI.csproj", "{CFC243F0-F4E3-4754-8E21-8BA17E1C1788}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Miner", "Miner\Miner.csproj", "{58870D2E-9DAE-41EF-9766-17C39238ED0F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AntSharesDaemon", "AntSharesDaemon\AntSharesDaemon.csproj", "{3E7DD63A-6AD5-41F1-837D-42C11694246C}"
EndProject
Global
Expand All @@ -25,10 +23,6 @@ Global
{CFC243F0-F4E3-4754-8E21-8BA17E1C1788}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CFC243F0-F4E3-4754-8E21-8BA17E1C1788}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CFC243F0-F4E3-4754-8E21-8BA17E1C1788}.Release|Any CPU.Build.0 = Release|Any CPU
{58870D2E-9DAE-41EF-9766-17C39238ED0F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{58870D2E-9DAE-41EF-9766-17C39238ED0F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{58870D2E-9DAE-41EF-9766-17C39238ED0F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{58870D2E-9DAE-41EF-9766-17C39238ED0F}.Release|Any CPU.Build.0 = Release|Any CPU
{3E7DD63A-6AD5-41F1-837D-42C11694246C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3E7DD63A-6AD5-41F1-837D-42C11694246C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E7DD63A-6AD5-41F1-837D-42C11694246C}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
8 changes: 8 additions & 0 deletions AntSharesCore/AntSharesCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Consensus\ChangeView.cs" />
<Compile Include="Consensus\ConsensusContext.cs" />
<Compile Include="Consensus\ConsensusMessage.cs" />
<Compile Include="Consensus\ConsensusMessageType.cs" />
<Compile Include="Consensus\ConsensusState.cs" />
<Compile Include="Consensus\ConsensusService.cs" />
<Compile Include="Consensus\PerpareRequest.cs" />
<Compile Include="Consensus\PerpareResponse.cs" />
<Compile Include="Core\AgencyTransaction.cs" />
<Compile Include="Core\Block.cs" />
<Compile Include="Core\BlockBase.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
using System;
using System.IO;

namespace AntShares.Miner.Consensus
{
internal class ChangeView : ConsensusMessage
{
public byte NewViewNumber;

public ChangeView()
: base(ConsensusMessageType.ChangeView)
{
}

public override void Deserialize(BinaryReader reader)
{
base.Deserialize(reader);
NewViewNumber = reader.ReadByte();
if (NewViewNumber == 0) throw new FormatException();
}

public override void Serialize(BinaryWriter writer)
{
base.Serialize(writer);
writer.Write(NewViewNumber);
}
}
}
using System;
using System.IO;

namespace AntShares.Consensus
{
internal class ChangeView : ConsensusMessage
{
public byte NewViewNumber;

public ChangeView()
: base(ConsensusMessageType.ChangeView)
{
}

public override void Deserialize(BinaryReader reader)
{
base.Deserialize(reader);
NewViewNumber = reader.ReadByte();
if (NewViewNumber == 0) throw new FormatException();
}

public override void Serialize(BinaryWriter writer)
{
base.Serialize(writer);
writer.Write(NewViewNumber);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,132 +1,132 @@
using AntShares.Core;
using AntShares.Cryptography;
using AntShares.Cryptography.ECC;
using AntShares.IO;
using AntShares.Network.Payloads;
using AntShares.Wallets;
using System.Collections.Generic;
using System.Linq;

namespace AntShares.Miner.Consensus
{
internal class ConsensusContext
{
public const uint Version = 0;
public ConsensusState State;
public UInt256 PrevHash;
public uint Height;
public byte ViewNumber;
public ECPoint[] Miners;
public int MinerIndex;
public uint PrimaryIndex;
public uint Timestamp;
public ulong Nonce;
public UInt160 NextMiner;
public UInt256[] TransactionHashes;
public Dictionary<UInt256, Transaction> Transactions;
public byte[][] Signatures;
public byte[] ExpectedView;

public int M => Miners.Length - (Miners.Length - 1) / 3;

public void ChangeView(byte view_number)
{
int p = ((int)Height - view_number) % Miners.Length;
State &= ConsensusState.SignatureSent;
ViewNumber = view_number;
PrimaryIndex = p >= 0 ? (uint)p : (uint)(p + Miners.Length);
if (State == ConsensusState.Initial)
{
TransactionHashes = null;
Signatures = new byte[Miners.Length][];
}
_header = null;
}

public ConsensusPayload MakeChangeView()
{
return MakePayload(new ChangeView
{
NewViewNumber = ExpectedView[MinerIndex]
});
}

private Block _header = null;
public Block MakeHeader()
{
if (TransactionHashes == null) return null;
if (_header == null)
{
_header = new Block
{
Version = Version,
PrevBlock = PrevHash,
MerkleRoot = MerkleTree.ComputeRoot(TransactionHashes),
Timestamp = Timestamp,
Height = Height,
Nonce = Nonce,
NextMiner = NextMiner,
Transactions = new Transaction[0]
};
}
return _header;
}

private ConsensusPayload MakePayload(ConsensusMessage message)
{
message.ViewNumber = ViewNumber;
return new ConsensusPayload
{
Version = Version,
PrevHash = PrevHash,
Height = Height,
MinerIndex = (ushort)MinerIndex,
Timestamp = Timestamp,
Data = message.ToArray()
};
}

public ConsensusPayload MakePerpareRequest()
{
return MakePayload(new PerpareRequest
{
Nonce = Nonce,
NextMiner = NextMiner,
TransactionHashes = TransactionHashes,
MinerTransaction = (MinerTransaction)Transactions[TransactionHashes[0]],
Signature = Signatures[MinerIndex]
});
}

public ConsensusPayload MakePerpareResponse(byte[] signature)
{
return MakePayload(new PerpareResponse
{
Signature = signature
});
}

public void Reset(Wallet wallet)
{
State = ConsensusState.Initial;
PrevHash = Blockchain.Default.CurrentBlockHash;
Height = Blockchain.Default.Height + 1;
ViewNumber = 0;
Miners = Blockchain.Default.GetMiners();
MinerIndex = -1;
PrimaryIndex = Height % (uint)Miners.Length;
TransactionHashes = null;
Signatures = new byte[Miners.Length][];
ExpectedView = new byte[Miners.Length];
for (int i = 0; i < Miners.Length; i++)
{
if (wallet.ContainsAccount(Miners[i]))
{
MinerIndex = i;
break;
}
}
_header = null;
}
}
}
using AntShares.Core;
using AntShares.Cryptography;
using AntShares.Cryptography.ECC;
using AntShares.IO;
using AntShares.Network.Payloads;
using AntShares.Wallets;
using System.Collections.Generic;
using System.Linq;

namespace AntShares.Consensus
{
internal class ConsensusContext
{
public const uint Version = 0;
public ConsensusState State;
public UInt256 PrevHash;
public uint Height;
public byte ViewNumber;
public ECPoint[] Miners;
public int MinerIndex;
public uint PrimaryIndex;
public uint Timestamp;
public ulong Nonce;
public UInt160 NextMiner;
public UInt256[] TransactionHashes;
public Dictionary<UInt256, Transaction> Transactions;
public byte[][] Signatures;
public byte[] ExpectedView;

public int M => Miners.Length - (Miners.Length - 1) / 3;

public void ChangeView(byte view_number)
{
int p = ((int)Height - view_number) % Miners.Length;
State &= ConsensusState.SignatureSent;
ViewNumber = view_number;
PrimaryIndex = p >= 0 ? (uint)p : (uint)(p + Miners.Length);
if (State == ConsensusState.Initial)
{
TransactionHashes = null;
Signatures = new byte[Miners.Length][];
}
_header = null;
}

public ConsensusPayload MakeChangeView()
{
return MakePayload(new ChangeView
{
NewViewNumber = ExpectedView[MinerIndex]
});
}

private Block _header = null;
public Block MakeHeader()
{
if (TransactionHashes == null) return null;
if (_header == null)
{
_header = new Block
{
Version = Version,
PrevBlock = PrevHash,
MerkleRoot = MerkleTree.ComputeRoot(TransactionHashes),
Timestamp = Timestamp,
Height = Height,
Nonce = Nonce,
NextMiner = NextMiner,
Transactions = new Transaction[0]
};
}
return _header;
}

private ConsensusPayload MakePayload(ConsensusMessage message)
{
message.ViewNumber = ViewNumber;
return new ConsensusPayload
{
Version = Version,
PrevHash = PrevHash,
Height = Height,
MinerIndex = (ushort)MinerIndex,
Timestamp = Timestamp,
Data = message.ToArray()
};
}

public ConsensusPayload MakePerpareRequest()
{
return MakePayload(new PerpareRequest
{
Nonce = Nonce,
NextMiner = NextMiner,
TransactionHashes = TransactionHashes,
MinerTransaction = (MinerTransaction)Transactions[TransactionHashes[0]],
Signature = Signatures[MinerIndex]
});
}

public ConsensusPayload MakePerpareResponse(byte[] signature)
{
return MakePayload(new PerpareResponse
{
Signature = signature
});
}

public void Reset(Wallet wallet)
{
State = ConsensusState.Initial;
PrevHash = Blockchain.Default.CurrentBlockHash;
Height = Blockchain.Default.Height + 1;
ViewNumber = 0;
Miners = Blockchain.Default.GetMiners();
MinerIndex = -1;
PrimaryIndex = Height % (uint)Miners.Length;
TransactionHashes = null;
Signatures = new byte[Miners.Length][];
ExpectedView = new byte[Miners.Length];
for (int i = 0; i < Miners.Length; i++)
{
if (wallet.ContainsAccount(Miners[i]))
{
MinerIndex = i;
break;
}
}
_header = null;
}
}
}
Loading

0 comments on commit f786d5c

Please sign in to comment.