Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ protected void RunTest(BlockchainTest test, Stopwatch stopwatch = null)
_multiDb,
_stateProviders[test.Network],
_storageProviders[test.Network],
new TransactionStore(),
_logger);

IBlockchainProcessor blockchainProcessor = new BlockchainProcessor(
Expand Down
10 changes: 10 additions & 0 deletions src/Nevermind/Nevermind.Blockchain.Test.Runner/FileLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ public void Log(string text)
}
}

public void Debug(string text)
{
Log(text);
}

public void Error(string text, Exception ex = null)
{
Log(ex != null ? $"{text}, Exception: {ex}" : text);
}

public void Flush()
{
File.AppendAllText(_filePath, _buffer.ToString());
Expand Down
10 changes: 7 additions & 3 deletions src/Nevermind/Nevermind.Blockchain/BlockProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class BlockProcessor : IBlockProcessor
private readonly IStateProvider _stateProvider;
private readonly IStorageProvider _storageProvider;
private readonly ILogger _logger;
private readonly ITransactionStore _transactionStore;

private readonly IDifficultyCalculator _difficultyCalculator;

Expand All @@ -51,15 +52,15 @@ public BlockProcessor(
ITransactionProcessor transactionProcessor,
ISnapshotable db,
IStateProvider stateProvider,
IStorageProvider storageProvider,
ILogger logger = null)
IStorageProvider storageProvider, ITransactionStore transactionStore, ILogger logger = null)
{
_logger = logger;
_ethereumRelease = ethereumRelease;
_blockStore = blockStore;
_blockValidator = blockValidator;
_stateProvider = stateProvider;
_storageProvider = storageProvider;
_transactionStore = transactionStore;
_difficultyCalculator = difficultyCalculator;
_rewardCalculator = rewardCalculator;
_transactionProcessor = transactionProcessor;
Expand All @@ -75,13 +76,16 @@ private void ProcessTransactions(Block block, List<Transaction> transactions)
List<TransactionReceipt> receipts = new List<TransactionReceipt>(); // TODO: pool?
for (int i = 0; i < transactions.Count; i++)
{
var transaction = transactions[i];
if (block.Header.Number == 26)
{

}

_logger?.Log($"PROCESSING TRANSACTION {i}");
TransactionReceipt receipt = _transactionProcessor.Execute(transactions[i], block.Header);
_transactionStore.AddTransaction(transaction);
TransactionReceipt receipt = _transactionProcessor.Execute(transaction, block.Header);
_transactionStore.AddTransactionReceipt(transaction.Hash, receipt, block.Hash);
receipts.Add(receipt);
}

Expand Down
7 changes: 7 additions & 0 deletions src/Nevermind/Nevermind.Blockchain/BlockStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
*/
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using Nevermind.Core;
using Nevermind.Core.Crypto;

Expand Down Expand Up @@ -50,6 +52,11 @@ public Block FindBlock(Keccak blockHash, bool mainChainOnly)
return block;
}

public Block FindBlock(BigInteger blockNumber)
{
return _mainChain.Values.FirstOrDefault(x => x.Header?.Number == blockNumber);
}

public bool IsMainChain(Keccak blockHash)
{
return _mainChain.ContainsKey(blockHash);
Expand Down
4 changes: 4 additions & 0 deletions src/Nevermind/Nevermind.Blockchain/BlockchainProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public BlockchainProcessor(
}

public Block HeadBlock { get; private set; }
public Block SuggestedBlock { get; private set; }
public BigInteger TotalDifficulty { get; private set; }
public BigInteger TotalTransactions { get; private set; }

Expand Down Expand Up @@ -90,6 +91,7 @@ public void Process(Rlp blockRlp)
{
_logger?.Log("-------------------------------------------------------------------------------------");
Block suggestedBlock = Rlp.Decode<Block>(blockRlp);
SuggestedBlock = suggestedBlock;
BigInteger totalDifficulty = GetTotalDifficulty(suggestedBlock.Header);
BigInteger totalTransactions = GetTotalTransactions(suggestedBlock);
_logger?.Log($"TOTAL DIFFICULTY OF BLOCK {suggestedBlock.Header.Hash} ({suggestedBlock.Header.Number}) IS {totalDifficulty}");
Expand Down Expand Up @@ -172,9 +174,11 @@ public void Process(Rlp blockRlp)
// lower difficulty branch
_blockStore.AddBlock(suggestedBlock, false);
}
SuggestedBlock = null;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

finally instead

}
catch (InvalidBlockException ex)
{
SuggestedBlock = null;
throw;
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/Nevermind/Nevermind.Blockchain/IBlockStore.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

using System.Numerics;

/*
* Copyright (c) 2018 Demerzel Solutions Limited
* This file is part of the Nethermind library.
Expand All @@ -15,6 +18,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
*/

using Nevermind.Core;
using Nevermind.Core.Crypto;

Expand All @@ -24,6 +28,7 @@ public interface IBlockStore
{
void AddBlock(Block block, bool isMainChain);
Block FindBlock(Keccak blockHash, bool mainChainOnly);
Block FindBlock(BigInteger blockNumber);
bool IsMainChain(Keccak blockHash);
void MoveToMain(Keccak blockHash);
void MoveToBranch(Keccak blockHash);
Expand Down
2 changes: 2 additions & 0 deletions src/Nevermind/Nevermind.Blockchain/IBlockchainProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ namespace Nevermind.Blockchain
public interface IBlockchainProcessor
{
Block HeadBlock { get; }
//Currently processing block

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

use /// comments instead

Block SuggestedBlock { get; }
BigInteger TotalDifficulty { get; }
void Process(Rlp blockRlp); // TODO: potentially do not return anything
}
Expand Down
10 changes: 10 additions & 0 deletions src/Nevermind/Nevermind.Core/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,15 @@ public void Log(string text)
{
Console.WriteLine(text);
}

public void Debug(string text)
{
Log(text);
}

public void Error(string text, Exception ex = null)
{
Log(ex != null ? $"{text}, Exception: {ex}" : text);
}
}
}
12 changes: 9 additions & 3 deletions src/Nevermind/Nevermind.Core/Crypto/PrivateKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ public class PrivateKey
private const int PrivateKeyLengthInBytes = 32;
private PublicKey _publicKey;

public PrivateKey()
:this(Random.GeneratePrivateKey())
public PrivateKey() :this(Random.GeneratePrivateKey(), Guid.NewGuid())
{
}

public PrivateKey(Hex key)
public PrivateKey(Hex key) : this(key, Guid.NewGuid())
{
}

public PrivateKey(Hex key, Guid id)
{
if (key == null)
{
Expand All @@ -44,6 +47,7 @@ public PrivateKey(Hex key)
}

Hex = key;
Id = id;
}

public Hex Hex { get; }
Expand All @@ -57,6 +61,8 @@ private PublicKey ComputePublicKey()

public Address Address => PublicKey.Address;

public Guid Id { get; set; }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we will need to review how we pass private key around in memory (need to be protected and overwritten), separately - let us discuss why we need Guid - can't we store by public key? (or is it to cover all the scenarios when public key is same? - rare)


public override string ToString()
{
return Hex.ToString(true);
Expand Down
9 changes: 8 additions & 1 deletion src/Nevermind/Nevermind.Core/Crypto/Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ public static class Random

public static byte[] GeneratePrivateKey()
{
byte[] bytes = new byte[32];
var bytes = new byte[32];
SecureRandom.GetBytes(bytes);
return bytes;
}

public static byte[] GenerateRandomBytes(int lenght)
{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

if not behind interface then equally we can use SecureRandom.GetBytes directly, otherwise let us push it behind ISecureRandom so we can test with this class wherever used

var bytes = new byte[lenght];
SecureRandom.GetBytes(bytes);
return bytes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ internal Transaction Decode(object[] data)
transaction.Signature = signature;
}

transaction.RecomputeHash();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

any suggestion how to do that nicer? I did not like it in my code (for block header) and wanted to refactor

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

maybe some change tracking and getter for hash that would always recompute when onvoked and any changes were made...

return transaction;
}

Expand Down
5 changes: 5 additions & 0 deletions src/Nevermind/Nevermind.Core/Hex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public bool Equals(Hex obj)
return false;
}

public byte[] ToBytes()
{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

there is implicit conversion operator, not sure if we need it ever

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am not a great fan of the Hex class any more by the way, we will review when alpha version fiinished

return _bytes ?? (_bytes = ToBytes(_hexString));
}

public override string ToString()
{
return ToString(true);
Expand Down
5 changes: 5 additions & 0 deletions src/Nevermind/Nevermind.Core/ILogger.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

using System;

/*
* Copyright (c) 2018 Demerzel Solutions Limited
* This file is part of the Nethermind library.
Expand All @@ -21,5 +24,7 @@ namespace Nevermind.Core
public interface ILogger
{
void Log(string text);
void Debug(string text);
void Error(string text, Exception ex = null);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

maybe better Error(string text) and Error(Exception ex) separately?

}
}
7 changes: 7 additions & 0 deletions src/Nevermind/Nevermind.Core/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

using System.Numerics;
using Nevermind.Core.Crypto;
using Nevermind.Core.Encoding;

namespace Nevermind.Core
{
Expand All @@ -37,5 +38,11 @@ public class Transaction
public bool IsMessageCall => Data != null;
public bool IsTransfer => !IsContractCreation && !IsMessageCall;
public bool IsValid { get; set; }
public Keccak Hash { get; set; }

public void RecomputeHash()
{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

do we have a single test for this (not that I never neglected it)

Hash = Keccak.Compute(Rlp.Encode(this));
}
}
}
1 change: 1 addition & 0 deletions src/Nevermind/Nevermind.Core/TransactionReceipt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ public class TransactionReceipt
public long GasUsed { get; set; }
public Bloom Bloom { get; set; }
public LogEntry[] Logs { get; set; }
public Address Recipient { get; set; }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

need to learn more - this I guess is only in the network version but not in the one created by EVM

}
}
4 changes: 4 additions & 0 deletions src/Nevermind/Nevermind.Evm/ITransactionProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


/*
* Copyright (c) 2018 Demerzel Solutions Limited
* This file is part of the Nethermind library.
Expand All @@ -17,6 +19,8 @@
*/

using Nevermind.Core;
using System.Linq;
using System.Numerics;

namespace Nevermind.Evm
{
Expand Down
16 changes: 16 additions & 0 deletions src/Nevermind/Nevermind.Evm/ITransactionStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Nevermind.Core;
using Nevermind.Core.Crypto;

namespace Nevermind.Evm
{
public interface ITransactionStore
{
void AddTransaction(Transaction transaction);
void AddTransactionReceipt(Keccak transactionHash, TransactionReceipt transactionReceipt, Keccak blockhash);
Transaction GetTransaction(Keccak transactionHash);
TransactionReceipt GetTransactionReceipt(Keccak transactionHash);
bool WasProcessed(Keccak transactionHash);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

what does it mean? it may not be on the main chain even if it was added, maybe worth to clarify the API, let us discuss

//get hash of the block transaction was in
Keccak? GetBlockHash(Keccak transactionHash);
}
}
2 changes: 2 additions & 0 deletions src/Nevermind/Nevermind.Evm/Nevermind.Evm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<Compile Include="InvalidInstructionException.cs" />
<Compile Include="InvalidJumpDestinationException.cs" />
<Compile Include="ITransactionProcessor.cs" />
<Compile Include="ITransactionStore.cs" />
<Compile Include="IVirtualMachine.cs" />
<Compile Include="Precompiles\EcAddPrecompiledContract.cs" />
<Compile Include="Precompiles\EcMulPrecompiledContract.cs" />
Expand All @@ -98,6 +99,7 @@
<Compile Include="StatusCode.cs" />
<Compile Include="TransactionCollisionException.cs" />
<Compile Include="TransactionProcessor.cs" />
<Compile Include="TransactionStore.cs" />
<Compile Include="TransactionSubstate.cs" />
<Compile Include="VirtualMachine.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
5 changes: 3 additions & 2 deletions src/Nevermind/Nevermind.Evm/TransactionProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public TransactionReceipt Execute(
_stateProvider.Commit();

block.GasUsed += spentGas;
return BuildTransactionReceipt(statusCode, logEntries, block.GasUsed);
return BuildTransactionReceipt(statusCode, logEntries, block.GasUsed, recipient);
}

private long Refund(long gasLimit, long unspentGas, TransactionSubstate substate, Address sender, BigInteger gasPrice)
Expand All @@ -271,14 +271,15 @@ private long Refund(long gasLimit, long unspentGas, TransactionSubstate substate
return spentGas;
}

private TransactionReceipt BuildTransactionReceipt(byte statusCode, List<LogEntry> logEntries, long gasUsedSoFar)
private TransactionReceipt BuildTransactionReceipt(byte statusCode, List<LogEntry> logEntries, long gasUsedSoFar, Address recipient)
{
TransactionReceipt transactionReceipt = new TransactionReceipt();
transactionReceipt.Logs = logEntries.ToArray();
transactionReceipt.Bloom = BuildBloom(logEntries);
transactionReceipt.GasUsed = gasUsedSoFar;
transactionReceipt.PostTransactionState = _stateProvider.StateRoot;
transactionReceipt.StatusCode = statusCode;
transactionReceipt.Recipient = recipient;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

cool but I guess it was never tested so worth adding a test if not there yet

return transactionReceipt;
}

Expand Down
Loading