Skip to content

Commit

Permalink
Optimize the serialization of ConsensusContext
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang committed Feb 21, 2019
1 parent 75c81d9 commit becf0f5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
20 changes: 10 additions & 10 deletions neo.UnitTests/UT_Consensus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,20 +178,20 @@ public void TestSerializeAndDeserializeConsensusContext()
{
var consensusContext = new ConsensusContext(null);
consensusContext.State = ConsensusState.CommitSent;
consensusContext.PrevHash = UInt256.Parse("3333333377777777333333337777777733333333777777773333333377777777");
consensusContext.BlockIndex = 1337;
consensusContext.PrevHash = UInt256.Parse("0xd42561e3d30e15be6400b6df2f328e02d2bf6354c41dce433bc57687c82144bf");
consensusContext.BlockIndex = 1;
consensusContext.ViewNumber = 2;
consensusContext.Validators = new ECPoint[7]
{
TestUtils.StandbyValidators[0],
ECPoint.Multiply(TestUtils.StandbyValidators[0], new BigInteger(2)),
ECPoint.Multiply(TestUtils.StandbyValidators[0], new BigInteger(3)),
ECPoint.Multiply(TestUtils.StandbyValidators[0], new BigInteger(4)),
ECPoint.Multiply(TestUtils.StandbyValidators[0], new BigInteger(5)),
ECPoint.Multiply(TestUtils.StandbyValidators[0], new BigInteger(6)),
ECPoint.Multiply(TestUtils.StandbyValidators[0], new BigInteger(7)),
ECPoint.Parse("02486fd15702c4490a26703112a5cc1d0923fd697a33406bd5a1c00e0013b09a70", Cryptography.ECC.ECCurve.Secp256r1),
ECPoint.Parse("024c7b7fb6c310fccf1ba33b082519d82964ea93868d676662d4a59ad548df0e7d", Cryptography.ECC.ECCurve.Secp256r1),
ECPoint.Parse("02aaec38470f6aad0042c6e877cfd8087d2676b0f516fddd362801b9bd3936399e", Cryptography.ECC.ECCurve.Secp256r1),
ECPoint.Parse("02ca0e27697b9c248f6f16e085fd0061e26f44da85b58ee835c110caa5ec3ba554", Cryptography.ECC.ECCurve.Secp256r1),
ECPoint.Parse("02df48f60e8f3e01c48ff40b9b7f1310d7a8b2a193188befe1c2e3df740e895093", Cryptography.ECC.ECCurve.Secp256r1),
ECPoint.Parse("03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c", Cryptography.ECC.ECCurve.Secp256r1),
ECPoint.Parse("03b8d9d5771d8f513aa0869b9cc8d50986403b78c6da36890638c3d46a5adce04a", Cryptography.ECC.ECCurve.Secp256r1)
};
consensusContext.MyIndex = 3;
consensusContext.MyIndex = -1;
consensusContext.PrimaryIndex = 6;
consensusContext.Timestamp = 4244941711;
consensusContext.Nonce = UInt64.MaxValue;
Expand Down
21 changes: 8 additions & 13 deletions neo/Consensus/ConsensusContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ namespace Neo.Consensus
internal class ConsensusContext : IConsensusContext
{
public const uint Version = 0;
public ConsensusState State { get; set; }
public UInt256 PrevHash { get; set; }
public uint BlockIndex { get; set; }
public UInt256 PrevHash { get; set; }
public byte ViewNumber { get; set; }
public ECPoint[] Validators { get; set; }
public int MyIndex { get; set; }
Expand All @@ -33,6 +32,7 @@ internal class ConsensusContext : IConsensusContext
public ConsensusPayload[] CommitPayloads { get; set; }
public ConsensusPayload[] ChangeViewPayloads { get; set; }
public ConsensusPayload[] LastChangeViewPayloads { get; set; }
public ConsensusState State { get; set; }
public Snapshot Snapshot { get; private set; }
private KeyPair keyPair;
private readonly Wallet wallet;
Expand Down Expand Up @@ -66,13 +66,10 @@ public Block CreateBlock()

public void Deserialize(BinaryReader reader)
{
if (reader.ReadUInt32() != Version) return;
State = (ConsensusState)reader.ReadByte();
PrevHash = reader.ReadSerializable<UInt256>();
BlockIndex = reader.ReadUInt32();
Reset(0);
if (reader.ReadUInt32() != Version) throw new FormatException();
if (reader.ReadUInt32() != BlockIndex) throw new InvalidOperationException();
ViewNumber = reader.ReadByte();
Validators = reader.ReadSerializableArray<ECPoint>();
MyIndex = reader.ReadInt32();
PrimaryIndex = reader.ReadUInt32();
Timestamp = reader.ReadUInt32();
Nonce = reader.ReadUInt64();
Expand Down Expand Up @@ -105,6 +102,7 @@ public void Deserialize(BinaryReader reader)
LastChangeViewPayloads = new ConsensusPayload[reader.ReadVarInt()];
for (int i = 0; i < LastChangeViewPayloads.Length; i++)
LastChangeViewPayloads[i] = reader.ReadBoolean() ? reader.ReadSerializable<ConsensusPayload>() : null;
State = (ConsensusState)reader.ReadByte();
}

public void Dispose()
Expand Down Expand Up @@ -251,7 +249,7 @@ public void Reset(byte viewNumber)
keyPair = null;
for (int i = 0; i < Validators.Length; i++)
{
WalletAccount account = wallet.GetAccount(Validators[i]);
WalletAccount account = wallet?.GetAccount(Validators[i]);
if (account?.HasKey != true) continue;
MyIndex = i;
keyPair = account.GetKey();
Expand Down Expand Up @@ -279,12 +277,8 @@ public void Reset(byte viewNumber)
public void Serialize(BinaryWriter writer)
{
writer.Write(Version);
writer.Write((byte)State);
writer.Write(PrevHash);
writer.Write(BlockIndex);
writer.Write(ViewNumber);
writer.Write(Validators);
writer.Write(MyIndex);
writer.Write(PrimaryIndex);
writer.Write(Timestamp);
writer.Write(Nonce);
Expand Down Expand Up @@ -323,6 +317,7 @@ public void Serialize(BinaryWriter writer)
if (!hasPayload) continue;
writer.Write(payload);
}
writer.Write((byte)State);
}

public void Fill()
Expand Down
2 changes: 1 addition & 1 deletion neo/Consensus/ConsensusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ private void OnStart(Start options)
Log("OnStart");
started = true;
bool loadedState = !options.IgnoreRecoveryLogs && context.Load(store);
if (loadedState && context.State.HasFlag(ConsensusState.CommitSent) && Blockchain.Singleton.Height + 1 == context.BlockIndex)
if (loadedState && context.State.HasFlag(ConsensusState.CommitSent))
{
CheckPreparations();
return;
Expand Down
19 changes: 11 additions & 8 deletions neo/Consensus/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,29 @@ public static void Save(this IConsensusContext context, Store store)
store.PutSync(CN_Context, new byte[0], context.ToArray());
}

public static bool Load(this IConsensusContext context, Store store, bool shouldReset = true)
public static bool Load(this IConsensusContext context, Store store)
{
byte[] data = store.Get(CN_Context, new byte[0]);
if (data != null)
if (data is null) return false;
using (MemoryStream ms = new MemoryStream(data, false))
using (BinaryReader reader = new BinaryReader(ms))
{
if (shouldReset) context.Reset(0);
using (MemoryStream ms = new MemoryStream(data, false))
using (BinaryReader reader = new BinaryReader(ms))
try
{
context.Deserialize(reader);
return true;
}
catch
{
return false;
}
return true;
}
return false;
}

public static IEnumerable<Transaction> RetreiveTransactionsFromSavedConsensusContext(Store consensusStore)
{
IConsensusContext context = new ConsensusContext(null);
context.Load(consensusStore, false);
context.Load(consensusStore);
return context.Transactions?.Values ?? (IEnumerable<Transaction>)new Transaction[0];
}
}
Expand Down

1 comment on commit becf0f5

@jsolman
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

Please sign in to comment.