forked from neo-project/neo
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Erik Zhang
committed
Oct 20, 2016
1 parent
1db0a21
commit f786d5c
Showing
28 changed files
with
955 additions
and
2,334 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 28 additions & 28 deletions
56
Miner/Miner/Consensus/ChangeView.cs → AntSharesCore/Consensus/ChangeView.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
264 changes: 132 additions & 132 deletions
264
Miner/Miner/Consensus/ConsensusContext.cs → AntSharesCore/Consensus/ConsensusContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.