Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consensus: finding the best way to create a history of block executions results #835

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/DBFTPlugin/Consensus/ConsensusService.OnMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@

using Akka.Actor;
using Neo.Cryptography;
using Neo.IO;
using Neo.Ledger;
using Neo.Network.P2P;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.Wallets;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Neo.Consensus
Expand Down Expand Up @@ -76,6 +75,12 @@ private void OnConsensusPayload(ExtensiblePayload payload)

private void OnPrepareRequestReceived(ExtensiblePayload payload, PrepareRequest message)
{
if (!File.Exists("prestatehash") || File.ReadAllText("prestatehash") != message.PreStateHash.ToString())
{
Log($"Invalid request: previous state hash does not match", LogLevel.Warning);
return;
}

if (context.RequestSentOrReceived || context.NotAcceptingPayloadsDueToViewChanging) return;
if (message.ValidatorIndex != context.Block.PrimaryIndex || message.ViewNumber != context.ViewNumber) return;
if (message.Version != context.Block.Version || message.PrevHash != context.Block.PrevHash) return;
Expand Down
21 changes: 21 additions & 0 deletions src/DBFTPlugin/DBFTPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using System.Collections.Generic;
using System;
using System.IO;
using System.Security.Cryptography;
using Akka.Actor;
using Neo.ConsoleService;
using System.Text.Json;
using Neo.Ledger;
using Neo.Network.P2P;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.Plugins;
using Neo.Wallets;

Expand All @@ -30,6 +37,7 @@ public class DBFTPlugin : Plugin
public DBFTPlugin()
{
RemoteNode.MessageReceived += RemoteNode_MessageReceived;
Blockchain.Committing += OnCommitting;
}

public DBFTPlugin(Settings settings) : this()
Expand Down Expand Up @@ -96,5 +104,18 @@ private bool RemoteNode_MessageReceived(NeoSystem system, Message message)
}
return true;
}

private static void OnCommitting(NeoSystem system, Block block, DataCache snapshot, IReadOnlyList<Blockchain.ApplicationExecuted> applicationExecutedList)
{
var serializedData = JsonSerializer.SerializeToUtf8Bytes(applicationExecutedList);
var hash = ComputeSha256Hash(serializedData);
Copy link
Member

Choose a reason for hiding this comment

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

Here, you are proposing the Sha256 (of all Executed List) on every payload for PrepareRequest.

File.WriteAllText("prestatehash", hash);
}

private static string ComputeSha256Hash(byte[] rawData)
{
var bytes = SHA256.HashData(rawData);
return BitConverter.ToString(bytes).Replace("-", "").ToLower();
}
}
}
6 changes: 6 additions & 0 deletions src/DBFTPlugin/Messages/PrepareRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ public class PrepareRequest : ConsensusMessage
{
public uint Version;
public UInt256 PrevHash;
// The execution result hash of the previous block transactions
// since we do not need to update it or search it, no merkel tree is needed
Copy link
Member

Choose a reason for hiding this comment

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

This is correct, in this case, it is not a storage, it is a protocol communication level that ensure nodes will not proceed.

public UInt256 PreStateHash;
public ulong Timestamp;
public ulong Nonce;
public UInt256[] TransactionHashes;

public override int Size => base.Size
+ sizeof(uint) //Version
+ UInt256.Length //PrevHash
+ UInt256.Length //PreStateHash
+ sizeof(ulong) //Timestamp
+ sizeof(ulong) // Nonce
+ TransactionHashes.GetVarSize(); //TransactionHashes
Expand All @@ -37,6 +41,7 @@ public override void Deserialize(ref MemoryReader reader)
base.Deserialize(ref reader);
Version = reader.ReadUInt32();
PrevHash = reader.ReadSerializable<UInt256>();
PreStateHash = reader.ReadSerializable<UInt256>();
Timestamp = reader.ReadUInt64();
Nonce = reader.ReadUInt64();
TransactionHashes = reader.ReadSerializableArray<UInt256>(ushort.MaxValue);
Expand All @@ -55,6 +60,7 @@ public override void Serialize(BinaryWriter writer)
base.Serialize(writer);
writer.Write(Version);
writer.Write(PrevHash);
writer.Write(PreStateHash);
writer.Write(Timestamp);
writer.Write(Nonce);
writer.Write(TransactionHashes);
Expand Down