Browse Source

Merge branch 'master' of https://github.com/programmerTim/cpp-ethereum into programmerTim-master

Conflicts:
	libethereum/BlockInfo.cpp
	libethereum/Trie.h
	test/main.cpp
cl-refactor
Gav Wood 11 years ago
parent
commit
4f810b0ec8
  1. 7
      .gitignore
  2. 68
      Ethereum.sln
  3. 184
      eth/Ethereum.vcxproj
  4. 1
      libethereum/BlockChain.h
  5. 3
      libethereum/BlockInfo.cpp
  6. 9
      libethereum/Common.cpp
  7. 10
      libethereum/Common.h
  8. 104
      libethereum/Dagger.cpp
  9. 17
      libethereum/Dagger.h
  10. 19
      libethereum/LibEthereum.props
  11. 175
      libethereum/LibEthereum.vcxproj
  12. 32
      libethereum/LibEthereum.vcxproj.filters
  13. 20
      libethereum/RLP.cpp
  14. 4
      libethereum/RLP.h
  15. 28
      libethereum/State.cpp
  16. 3
      libethereum/State.h
  17. 2
      libethereum/Transaction.cpp
  18. 12
      libethereum/Trie.cpp
  19. 27
      libethereum/Trie.h
  20. 24
      libethereum/vector_ref.h
  21. 184
      test/Test.vcxproj
  22. 19
      test/main.cpp

7
.gitignore

@ -12,4 +12,11 @@
*.la
*.a
# VS stuff
build
ipch
*.sdf
*.opensdf
*.suo
*.user

68
Ethereum.sln

@ -0,0 +1,68 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Express 2013 for Windows Desktop
VisualStudioVersion = 12.0.21005.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LibEthereum", "libethereum\LibEthereum.vcxproj", "{7050C7CF-7551-48BE-8E57-92235906C13A}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TestMining", "test\TestMining.vcxproj", "{02DC8A9B-DEF7-403B-8AE3-EF9680937D96}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Test", "test\Test.vcxproj", "{3F3E389B-88DE-41D5-A73B-4F6036E18B36}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cryptlib", "..\cryptopp562\cryptlib.vcxproj", "{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Ethereum", "eth\Ethereum.vcxproj", "{C60C065C-2135-4B2B-AFD4-35FD7AC56B40}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Debug|x64 = Debug|x64
Release|Win32 = Release|Win32
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7050C7CF-7551-48BE-8E57-92235906C13A}.Debug|Win32.ActiveCfg = Debug|Win32
{7050C7CF-7551-48BE-8E57-92235906C13A}.Debug|Win32.Build.0 = Debug|Win32
{7050C7CF-7551-48BE-8E57-92235906C13A}.Debug|x64.ActiveCfg = Debug|x64
{7050C7CF-7551-48BE-8E57-92235906C13A}.Debug|x64.Build.0 = Debug|x64
{7050C7CF-7551-48BE-8E57-92235906C13A}.Release|Win32.ActiveCfg = Release|Win32
{7050C7CF-7551-48BE-8E57-92235906C13A}.Release|Win32.Build.0 = Release|Win32
{7050C7CF-7551-48BE-8E57-92235906C13A}.Release|x64.ActiveCfg = Release|x64
{7050C7CF-7551-48BE-8E57-92235906C13A}.Release|x64.Build.0 = Release|x64
{02DC8A9B-DEF7-403B-8AE3-EF9680937D96}.Debug|Win32.ActiveCfg = Debug|Win32
{02DC8A9B-DEF7-403B-8AE3-EF9680937D96}.Debug|Win32.Build.0 = Debug|Win32
{02DC8A9B-DEF7-403B-8AE3-EF9680937D96}.Debug|x64.ActiveCfg = Debug|x64
{02DC8A9B-DEF7-403B-8AE3-EF9680937D96}.Debug|x64.Build.0 = Debug|x64
{02DC8A9B-DEF7-403B-8AE3-EF9680937D96}.Release|Win32.ActiveCfg = Release|Win32
{02DC8A9B-DEF7-403B-8AE3-EF9680937D96}.Release|Win32.Build.0 = Release|Win32
{02DC8A9B-DEF7-403B-8AE3-EF9680937D96}.Release|x64.ActiveCfg = Release|x64
{02DC8A9B-DEF7-403B-8AE3-EF9680937D96}.Release|x64.Build.0 = Release|x64
{3F3E389B-88DE-41D5-A73B-4F6036E18B36}.Debug|Win32.ActiveCfg = Debug|Win32
{3F3E389B-88DE-41D5-A73B-4F6036E18B36}.Debug|Win32.Build.0 = Debug|Win32
{3F3E389B-88DE-41D5-A73B-4F6036E18B36}.Debug|x64.ActiveCfg = Debug|x64
{3F3E389B-88DE-41D5-A73B-4F6036E18B36}.Debug|x64.Build.0 = Debug|x64
{3F3E389B-88DE-41D5-A73B-4F6036E18B36}.Release|Win32.ActiveCfg = Release|Win32
{3F3E389B-88DE-41D5-A73B-4F6036E18B36}.Release|Win32.Build.0 = Release|Win32
{3F3E389B-88DE-41D5-A73B-4F6036E18B36}.Release|x64.ActiveCfg = Release|x64
{3F3E389B-88DE-41D5-A73B-4F6036E18B36}.Release|x64.Build.0 = Release|x64
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.Debug|Win32.ActiveCfg = Debug|Win32
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.Debug|Win32.Build.0 = Debug|Win32
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.Debug|x64.ActiveCfg = Debug|x64
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.Debug|x64.Build.0 = Debug|x64
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.Release|Win32.ActiveCfg = Release|Win32
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.Release|Win32.Build.0 = Release|Win32
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.Release|x64.ActiveCfg = Release|x64
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.Release|x64.Build.0 = Release|x64
{C60C065C-2135-4B2B-AFD4-35FD7AC56B40}.Debug|Win32.ActiveCfg = Debug|Win32
{C60C065C-2135-4B2B-AFD4-35FD7AC56B40}.Debug|Win32.Build.0 = Debug|Win32
{C60C065C-2135-4B2B-AFD4-35FD7AC56B40}.Debug|x64.ActiveCfg = Debug|x64
{C60C065C-2135-4B2B-AFD4-35FD7AC56B40}.Debug|x64.Build.0 = Debug|x64
{C60C065C-2135-4B2B-AFD4-35FD7AC56B40}.Release|Win32.ActiveCfg = Release|Win32
{C60C065C-2135-4B2B-AFD4-35FD7AC56B40}.Release|Win32.Build.0 = Release|Win32
{C60C065C-2135-4B2B-AFD4-35FD7AC56B40}.Release|x64.ActiveCfg = Release|x64
{C60C065C-2135-4B2B-AFD4-35FD7AC56B40}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

184
eth/Ethereum.vcxproj

@ -0,0 +1,184 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<Keyword>Win32Proj</Keyword>
<RootNamespace>Ethereum</RootNamespace>
<ProjectName>Ethereum</ProjectName>
<ProjectGuid>{C60C065C-2135-4B2B-AFD4-35FD7AC56B40}</ProjectGuid>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\libethereum\LibEthereum.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\libethereum\LibEthereum.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\libethereum\LibEthereum.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\libethereum\LibEthereum.props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>../libethereum</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<IntrinsicFunctions>true</IntrinsicFunctions>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
<OpenMPSupport>true</OpenMPSupport>
<DisableSpecificWarnings>4351</DisableSpecificWarnings>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<LargeAddressAware>true</LargeAddressAware>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>../libethereum</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<IntrinsicFunctions>true</IntrinsicFunctions>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
<OpenMPSupport>true</OpenMPSupport>
<DisableSpecificWarnings>4351</DisableSpecificWarnings>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>../libethereum</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<StringPooling>true</StringPooling>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
<OpenMPSupport>true</OpenMPSupport>
<DisableSpecificWarnings>4351</DisableSpecificWarnings>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<LargeAddressAware>true</LargeAddressAware>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>../libethereum</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<StringPooling>true</StringPooling>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
<OpenMPSupport>true</OpenMPSupport>
<DisableSpecificWarnings>4351</DisableSpecificWarnings>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ProjectReference Include="..\..\cryptopp562\cryptlib.vcxproj">
<Project>{3423ec9a-52e4-4a4d-9753-edebc38785ef}</Project>
</ProjectReference>
<ProjectReference Include="..\libethereum\LibEthereum.vcxproj">
<Project>{7050c7cf-7551-48be-8e57-92235906c13a}</Project>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

1
libethereum/BlockChain.h

@ -22,7 +22,6 @@
#pragma once
#include "Common.h"
#include <leveldb/db.h>
namespace ldb = leveldb;
namespace eth

3
libethereum/BlockInfo.cpp

@ -91,8 +91,7 @@ void BlockInfo::populate(bytesConstRef _block)
}
// check it hashes according to proof of work.
Dagger d(headerHashWithoutNonce());
if (d.eval(nonce) >= (u256)(((bigint)1 << 256) / difficulty))
if (!Dagger::verify(headerHashWithoutNonce(), nonce, difficulty))
throw InvalidNonce();
}

9
libethereum/Common.cpp

@ -19,7 +19,14 @@
* @date 2014
*/
#if WIN32
#pragma warning(push)
#pragma warning(disable:4244)
#endif
#include <sha3.h>
#if WIN32
#pragma warning(pop)
#endif
#include <random>
#include "Common.h"
#include "Exceptions.h"
@ -79,7 +86,7 @@ bytes eth::fromUserHex(std::string const& _s)
std::vector<uint8_t> ret;
ret.reserve((_s.size() - s) / 2);
for (uint i = s; i < _s.size(); i += 2)
ret.push_back(fromHex(_s[i]) * 16 + fromHex(_s[i + 1]));
ret.push_back((byte)(fromHex(_s[i]) * 16 + fromHex(_s[i + 1])));
return ret;
}

10
libethereum/Common.h

@ -23,6 +23,11 @@
#pragma once
// way to many uint to size_t warnings in 32 bit build
#ifdef _M_IX86
#pragma warning(disable:4244)
#endif
#include <array>
#include <map>
#include <set>
@ -200,9 +205,8 @@ bytes toHex(std::string const& _s);
template <class _T, class _Out>
inline void toBigEndian(_T _val, _Out& o_out)
{
auto s = o_out.size();
for (uint i = 0; i < s; ++i, _val >>= 8)
o_out[s - 1 - i] = (typename _Out::value_type)(uint8_t)_val;
for (auto i = o_out.size(); i-- != 0; _val >>= 8)
o_out[i] = (typename _Out::value_type)(uint8_t)_val;
}
/// Converts a big-endian byte-stream represented on a templated collection to a templated integer value.

104
libethereum/Dagger.cpp

@ -1,15 +1,20 @@
#include <boost/detail/endian.hpp>
#include <chrono>
#include <array>
#pragma warning(push)
#pragma warning(disable:4244)
#include <sha3.h>
#pragma warning(pop)
#include <random>
#include "Common.h"
#include "Dagger.h"
using namespace std;
using namespace std::chrono;
using namespace eth;
Dagger::Dagger(h256 _hash): m_hash(_hash)
namespace eth
{
Dagger::Dagger()
{
}
@ -17,79 +22,106 @@ Dagger::~Dagger()
{
}
u256 Dagger::bound(u256 _diff)
u256 Dagger::bound(u256 const& _difficulty)
{
return (u256)((bigint(1) << 256) / _difficulty);
}
bool Dagger::verify(h256 const& _root, u256 const& _nonce, u256 const& _difficulty)
{
return (u256)((bigint(1) << 256) / _diff);
return eval(_root, _nonce) < bound(_difficulty);
}
u256 Dagger::search(uint _msTimeout, u256 _diff)
bool Dagger::mine(u256& o_solution, h256 const& _root, u256 const& _difficulty, uint _msTimeout)
{
static mt19937_64 s_engine((std::random_device())());
u256 b = bound(_diff);
// restart search if root has changed
if (m_root != _root)
{
m_root = _root;
m_nonce = 0;
}
auto start = steady_clock::now();
// compute bound
u256 const b = bound(_difficulty);
while (steady_clock::now() - start < milliseconds(_msTimeout))
for (uint sp = std::uniform_int_distribution<uint>()(s_engine), j = 0; j < 1000; ++j, ++sp)
if (eval(sp) < b)
return sp;
return 0;
// evaluate until we run out of time
for (auto startTime = steady_clock::now(); (steady_clock::now() - startTime) < milliseconds(_msTimeout); m_nonce += 1)
{
if (eval(_root, m_nonce) < b)
{
o_solution = m_nonce;
return true;
}
}
return false;
}
template <class _T, class _U>
inline void update(_T& _sha, _U const& _value)
template <class _T>
inline void update(_T& _sha, u256 const& _value)
{
int i = 0;
for (_U v = _value; v; ++i, v >>= 8) {}
bytes buf(i);
toBigEndian(_value, buf);
_sha.Update(buf.data(), buf.size());
for (u256 v = _value; v; ++i, v >>= 8) {}
byte buf[32];
bytesRef bufRef(buf, i);
toBigEndian(_value, bufRef);
_sha.Update(buf, i);
}
template <class _T>
inline u256 get(_T& _sha)
inline void update(_T& _sha, h256 const& _value)
{
byte buf[32];
_sha.TruncatedFinal(buf, 32);
return fromBigEndian<u256>(bytesConstRef(buf, 32));
int i = 0;
byte const* data = _value.data();
for (; i != 32 && data[i] == 0; ++i);
_sha.Update(data + i, 32 - i);
}
u256 Dagger::node(uint_fast32_t _L, uint_fast32_t _i) const
template <class _T>
inline h256 get(_T& _sha)
{
h256 ret;
_sha.TruncatedFinal(&ret[0], 32);
return ret;
}
h256 Dagger::node(h256 const& _root, h256 const& _xn, uint_fast32_t _L, uint_fast32_t _i)
{
if (_L == _i)
return m_hash;
return _root;
u256 m = (_L == 9) ? 16 : 3;
CryptoPP::SHA3_256 bsha;
for (uint_fast32_t k = 0; k < m; ++k)
{
CryptoPP::SHA3_256 sha;
update(sha, m_hash);
update(sha, m_xn);
update(sha, _root);
update(sha, _xn);
update(sha, (u256)_L);
update(sha, (u256)_i);
update(sha, (u256)k);
uint_fast32_t pk = (uint_fast32_t)get(sha) & ((1 << ((_L - 1) * 3)) - 1);
auto u = node(_L - 1, pk);
uint_fast32_t pk = (uint_fast32_t)(u256)get(sha) & ((1 << ((_L - 1) * 3)) - 1);
auto u = node(_root, _xn, _L - 1, pk);
update(bsha, u);
}
return get(bsha);
}
u256 Dagger::eval(u256 _N)
h256 Dagger::eval(h256 const& _root, u256 const& _nonce)
{
m_xn = _N >> 26; // with xn = floor(n / 2^26) -> assuming this is with xn = floor(N / 2^26)
h256 extranonce = _nonce >> 26; // with xn = floor(n / 2^26) -> assuming this is with xn = floor(N / 2^26)
CryptoPP::SHA3_256 bsha;
for (uint_fast32_t k = 0; k < 4; ++k)
{
//sha256(D || xn || i || k) -> sha256(D || xn || k) - there's no 'i' here!
CryptoPP::SHA3_256 sha;
update(sha, m_hash);
update(sha, m_xn);
update(sha, _N);
update(sha, _root);
update(sha, extranonce);
update(sha, _nonce);
update(sha, (u256)k);
uint_fast32_t pk = (uint_fast32_t)get(sha) & 0x1ffffff; // mod 8^8 * 2 [ == mod 2^25 ?! ] [ == & ((1 << 25) - 1) ] [ == & 0x1ffffff ]
auto u = node(9, pk);
uint_fast32_t pk = (uint_fast32_t)(u256)get(sha) & 0x1ffffff; // mod 8^8 * 2 [ == mod 2^25 ?! ] [ == & ((1 << 25) - 1) ] [ == & 0x1ffffff ]
auto u = node(_root, extranonce, 9, pk);
update(bsha, u);
}
return get(bsha);
}
}

17
libethereum/Dagger.h

@ -9,18 +9,21 @@ namespace eth
class Dagger
{
public:
Dagger(h256 _hash);
Dagger();
~Dagger();
u256 node(uint_fast32_t _L, uint_fast32_t _i) const;
u256 eval(u256 _N);
u256 search(uint _msTimeout, u256 _diff);
static u256 bound(u256 const& _difficulty);
static h256 eval(h256 const& _root, u256 const& _nonce);
static bool verify(h256 const& _root, u256 const& _nonce, u256 const& _difficulty);
static u256 bound(u256 _diff);
bool mine(u256& o_solution, h256 const& _root, u256 const& _difficulty, uint _msTimeout = 100);
private:
u256 m_hash;
u256 m_xn;
static h256 node(h256 const& _root, h256 const& _xn, uint_fast32_t _L, uint_fast32_t _i);
h256 m_root;
u256 m_nonce;
};
}

19
libethereum/LibEthereum.props

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<IncludePath>$(IncludePath);../../boost_1_55_0;../../leveldb-1.15.0/include;../../cryptopp562;../../secp256k1/include</IncludePath>
<OutDir>..\build\$(ProjectName)\$(Platform)_$(Configuration)\</OutDir>
<IntDir>..\build\$(ProjectName)\$(Platform)_$(Configuration)\</IntDir>
</PropertyGroup>
<ItemDefinitionGroup>
<ClCompile>
<DisableSpecificWarnings>4100;4127;4505;4512;</DisableSpecificWarnings>
<WarningLevel>Level4</WarningLevel>
<TreatWarningAsError>true</TreatWarningAsError>
<MinimalRebuild>false</MinimalRebuild>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup />
</Project>

175
libethereum/LibEthereum.vcxproj

@ -0,0 +1,175 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClInclude Include="AddressState.h" />
<ClInclude Include="BlockChain.h" />
<ClInclude Include="BlockInfo.h" />
<ClInclude Include="Common.h" />
<ClInclude Include="Dagger.h" />
<ClInclude Include="Exceptions.h" />
<ClInclude Include="Instruction.h" />
<ClInclude Include="PeerNetwork.h" />
<ClInclude Include="RLP.h" />
<ClInclude Include="State.h" />
<ClInclude Include="Transaction.h" />
<ClInclude Include="TransactionQueue.h" />
<ClInclude Include="Trie.h" />
<ClInclude Include="vector_ref.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="AddressState.cpp" />
<ClCompile Include="BlockChain.cpp" />
<ClCompile Include="BlockInfo.cpp" />
<ClCompile Include="Common.cpp" />
<ClCompile Include="Dagger.cpp" />
<ClCompile Include="PeerNetwork.cpp" />
<ClCompile Include="RLP.cpp" />
<ClCompile Include="State.cpp" />
<ClCompile Include="Transaction.cpp" />
<ClCompile Include="TransactionQueue.cpp" />
<ClCompile Include="Trie.cpp" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{7050C7CF-7551-48BE-8E57-92235906C13A}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>LibEthereum</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="LibEthereum.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="LibEthereum.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="LibEthereum.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="LibEthereum.props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<OutDir>..\build\$(ProjectName)\$(Platform)_$(Configuration)\</OutDir>
<IntDir>..\build\$(ProjectName)\$(Platform)_$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<OutDir>..\build\$(ProjectName)\$(Platform)_$(Configuration)\</OutDir>
<IntDir>..\build\$(ProjectName)\$(Platform)_$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<OutDir>..\build\$(ProjectName)\$(Platform)_$(Configuration)\</OutDir>
<IntDir>..\build\$(ProjectName)\$(Platform)_$(Configuration)\</IntDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MinimalRebuild>false</MinimalRebuild>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<TreatWarningAsError>true</TreatWarningAsError>
<MinimalRebuild>false</MinimalRebuild>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<TreatWarningAsError>true</TreatWarningAsError>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<TreatWarningAsError>true</TreatWarningAsError>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

32
libethereum/LibEthereum.vcxproj.filters

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClInclude Include="TransactionQueue.h" />
<ClInclude Include="Trie.h" />
<ClInclude Include="vector_ref.h" />
<ClInclude Include="AddressState.h" />
<ClInclude Include="BlockChain.h" />
<ClInclude Include="BlockInfo.h" />
<ClInclude Include="Common.h" />
<ClInclude Include="Dagger.h" />
<ClInclude Include="Exceptions.h" />
<ClInclude Include="Instruction.h" />
<ClInclude Include="PeerNetwork.h" />
<ClInclude Include="RLP.h" />
<ClInclude Include="State.h" />
<ClInclude Include="Transaction.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Trie.cpp" />
<ClCompile Include="AddressState.cpp" />
<ClCompile Include="BlockChain.cpp" />
<ClCompile Include="BlockInfo.cpp" />
<ClCompile Include="Common.cpp" />
<ClCompile Include="Dagger.cpp" />
<ClCompile Include="PeerNetwork.cpp" />
<ClCompile Include="RLP.cpp" />
<ClCompile Include="State.cpp" />
<ClCompile Include="Transaction.cpp" />
<ClCompile Include="TransactionQueue.cpp" />
</ItemGroup>
</Project>

20
libethereum/RLP.cpp

@ -124,7 +124,7 @@ eth::uint RLP::items() const
RLPStream& RLPStream::appendString(bytesConstRef _s)
{
if (_s.size() < 0x38)
m_out.push_back(_s.size() | 0x40);
m_out.push_back((byte)(_s.size() | 0x40));
else
pushCount(_s.size(), 0x40);
uint os = m_out.size();
@ -136,7 +136,7 @@ RLPStream& RLPStream::appendString(bytesConstRef _s)
RLPStream& RLPStream::appendString(string const& _s)
{
if (_s.size() < 0x38)
m_out.push_back(_s.size() | 0x40);
m_out.push_back((byte)(_s.size() | 0x40));
else
pushCount(_s.size(), 0x40);
uint os = m_out.size();
@ -156,7 +156,7 @@ RLPStream& RLPStream::appendRaw(bytesConstRef _s)
RLPStream& RLPStream::appendList(uint _count)
{
if (_count < 0x38)
m_out.push_back(_count | 0x80);
m_out.push_back((byte)(_count | 0x80));
else
pushCount(_count, 0x80);
return *this;
@ -165,11 +165,11 @@ RLPStream& RLPStream::appendList(uint _count)
RLPStream& RLPStream::append(uint _i)
{
if (_i < 0x18)
m_out.push_back(_i);
m_out.push_back((byte)_i);
else
{
auto br = bytesRequired(_i);
m_out.push_back(br + 0x17); // max 8 bytes.
m_out.push_back((byte)(br + 0x17)); // max 8 bytes.
pushInt(_i, br);
}
return *this;
@ -182,7 +182,7 @@ RLPStream& RLPStream::append(u160 _i)
else
{
auto br = bytesRequired(_i);
m_out.push_back(br + 0x17); // max 8 bytes.
m_out.push_back((byte)(br + 0x17)); // max 8 bytes.
pushInt(_i, br);
}
return *this;
@ -195,7 +195,7 @@ RLPStream& RLPStream::append(u256 _i)
else
{
auto br = bytesRequired(_i);
m_out.push_back(br + 0x17); // max 8 bytes.
m_out.push_back((byte)(br + 0x17)); // max 8 bytes.
pushInt(_i, br);
}
return *this;
@ -209,11 +209,11 @@ RLPStream& RLPStream::append(bigint _i)
{
uint br = bytesRequired(_i);
if (br <= 32)
m_out.push_back(bytesRequired(_i) + 0x17); // max 32 bytes.
m_out.push_back((byte)(bytesRequired(_i) + 0x17)); // max 32 bytes.
else
{
auto brbr = bytesRequired(br);
m_out.push_back(0x37 + brbr);
m_out.push_back((byte)(0x37 + brbr));
pushInt(br, brbr);
}
pushInt(_i, br);
@ -224,7 +224,7 @@ RLPStream& RLPStream::append(bigint _i)
void RLPStream::pushCount(uint _count, byte _base)
{
auto br = bytesRequired(_count);
m_out.push_back(br + 0x37 + _base); // max 8 bytes.
m_out.push_back((byte)(br + 0x37 + _base)); // max 8 bytes.
pushInt(_count, br);
}

4
libethereum/RLP.h

@ -223,7 +223,7 @@ public:
else{}
_N ret;
unsigned s = std::min<unsigned>(_N::size, items());
size_t s = std::min((size_t)_N::size, (size_t)items());
memcpy(ret.data() + _N::size - s, payload().data(), s);
return ret;
}
@ -348,7 +348,7 @@ private:
for (unsigned i = 0; i < _N && !*d; ++i, --s, ++d) {}
if (s < 0x38)
m_out.push_back(s | 0x40);
m_out.push_back((byte)(s | 0x40));
else
pushCount(s, 0x40);
uint os = m_out.size();

28
libethereum/State.cpp

@ -20,9 +20,16 @@
*/
#include <secp256k1.h>
#if WIN32
#pragma warning(push)
#pragma warning(disable:4244)
#endif
#include <sha.h>
#include <sha3.h>
#include <ripemd.h>
#if WIN32
#pragma warning(pop)
#endif
#include <time.h>
#include <random>
#include "Trie.h"
@ -132,7 +139,9 @@ void State::sync(TransactionQueue& _tq)
// TRANSACTIONS
auto ts = _tq.transactions();
for (auto const& i: ts)
{
if (!m_transactions.count(i.first))
{
// don't have it yet! Execute it now.
try
{
@ -150,6 +159,8 @@ void State::sync(TransactionQueue& _tq)
// Something else went wrong - drop it.
_tq.drop(i.first);
}
}
}
}
u256 State::playback(bytesConstRef _block)
@ -247,10 +258,7 @@ bool State::mine(uint _msTimeout)
m_currentBlock.difficulty = m_currentBlock.calculateDifficulty(m_previousBlock);
// TODO: Miner class that keeps dagger between mine calls (or just non-polling mining).
Dagger d(m_currentBlock.headerHashWithoutNonce());
m_currentBlock.nonce = d.search(_msTimeout, m_currentBlock.difficulty);
if (m_currentBlock.nonce)
if (m_dagger.mine(/*out*/m_currentBlock.nonce, m_currentBlock.headerHashWithoutNonce(), m_currentBlock.difficulty, _msTimeout))
{
// Got it! Compile block:
RLPStream ret;
@ -690,9 +698,9 @@ void State::execute(Address _myAddress, Address _txSender, u256 _txValue, u256 _
bytes x = toBigEndian(stack.back());
stack.pop_back();
if (secp256k1_ecdsa_pubkey_verify(pub.data(), pub.size())) // TODO: Check both are less than P.
if (secp256k1_ecdsa_pubkey_verify(pub.data(), (int)pub.size())) // TODO: Check both are less than P.
{
secp256k1_ecdsa_pubkey_tweak_mul(pub.data(), pub.size(), x.data());
secp256k1_ecdsa_pubkey_tweak_mul(pub.data(), (int)pub.size(), x.data());
stack.push_back(fromBigEndian<u256>(bytesConstRef(&pub).cropped(1, 32)));
stack.push_back(fromBigEndian<u256>(bytesConstRef(&pub).cropped(33, 32)));
}
@ -720,9 +728,9 @@ void State::execute(Address _myAddress, Address _txSender, u256 _txValue, u256 _
stack.pop_back();
stack.pop_back();
if (secp256k1_ecdsa_pubkey_verify(pub.data(), pub.size()) && secp256k1_ecdsa_pubkey_verify(tweak.data(), tweak.size()))
if (secp256k1_ecdsa_pubkey_verify(pub.data(),(int) pub.size()) && secp256k1_ecdsa_pubkey_verify(tweak.data(),(int) tweak.size()))
{
secp256k1_ecdsa_pubkey_tweak_add(pub.data(), pub.size(), tweak.data());
secp256k1_ecdsa_pubkey_tweak_add(pub.data(), (int)pub.size(), tweak.data());
stack.push_back(fromBigEndian<u256>(bytesConstRef(&pub).cropped(1, 32)));
stack.push_back(fromBigEndian<u256>(bytesConstRef(&pub).cropped(33, 32)));
}
@ -767,7 +775,7 @@ void State::execute(Address _myAddress, Address _txSender, u256 _txValue, u256 _
byte pubkey[65];
int pubkeylen = 65;
if (secp256k1_ecdsa_recover_compact(msg.data(), msg.size(), sig.data(), pubkey, &pubkeylen, 0, v - 27))
if (secp256k1_ecdsa_recover_compact(msg.data(), (int)msg.size(), sig.data(), pubkey, &pubkeylen, 0, v - 27))
{
stack.push_back(0);
stack.push_back(0);
@ -788,7 +796,7 @@ void State::execute(Address _myAddress, Address _txSender, u256 _txValue, u256 _
stack.pop_back();
stack.pop_back();
stack.back() = secp256k1_ecdsa_pubkey_verify(pub.data(), pub.size()) ? 1 : 0;
stack.back() = secp256k1_ecdsa_pubkey_verify(pub.data(), (int)pub.size()) ? 1 : 0;
break;
}
case Instruction::SHA3:

3
libethereum/State.h

@ -32,6 +32,7 @@
#include "AddressState.h"
#include "Transaction.h"
#include "Trie.h"
#include "Dagger.h"
namespace eth
{
@ -168,6 +169,8 @@ private:
Address m_ourAddress; ///< Our address (i.e. the address to which fees go).
Dagger m_dagger;
/// The fee structure. Values yet to be agreed on...
static const u256 c_stepFee;
static const u256 c_dataFee;

2
libethereum/Transaction.cpp

@ -63,7 +63,7 @@ void Transaction::sign(PrivateKey _priv)
if (!secp256k1_ecdsa_sign_compact(msg.data(), 32, sig, _priv.data(), kFromMessage(msg, _priv).data(), &v))
throw InvalidSignature();
vrs.v = v + 27;
vrs.v = (byte)(v + 27);
vrs.r = fromBigEndian<u256>(bytesConstRef(sig, 32));
vrs.s = fromBigEndian<u256>(bytesConstRef(&(sig[32]), 32));
}

12
libethereum/Trie.cpp

@ -59,7 +59,7 @@ std::string hexPrefixEncode(bytes const& _hexVector, bool _terminated, int _begi
uint begin = _begin;
uint end = _end < 0 ? _hexVector.size() + 1 + _end : _end;
bool termed = _terminated;
bool odd = (end - begin) % 2;
bool odd = ((end - begin) % 2) != 0;
std::string ret(1, ((termed ? 2 : 0) | (odd ? 1 : 0)) * 16);
if (odd)
@ -101,7 +101,7 @@ void hash256rlp(HexMap const& _s, HexMap::const_iterator _begin, HexMap::const_i
uint c = 0;
for (auto i = std::next(_begin); i != _end && sharedPre; ++i, ++c)
{
uint x = std::min(sharedPre, std::min(_begin->first.size(), i->first.size()));
uint x = std::min(sharedPre, std::min((uint)_begin->first.size(), (uint)i->first.size()));
uint shared = _preLen;
for (; shared < x && _begin->first[shared] == i->first[shared]; ++shared) {}
sharedPre = std::min(shared, sharedPre);
@ -113,8 +113,8 @@ void hash256rlp(HexMap const& _s, HexMap::const_iterator _begin, HexMap::const_i
if (g_hashDebug)
std::cerr << s_indent << asHex(bytesConstRef(_begin->first.data() + _preLen, sharedPre), 1) << ": " << std::endl;
#endif
_rlp.appendList(2) << hexPrefixEncode(_begin->first, false, _preLen, sharedPre);
hash256aux(_s, _begin, _end, sharedPre, _rlp);
_rlp.appendList(2) << hexPrefixEncode(_begin->first, false, _preLen, (int)sharedPre);
hash256aux(_s, _begin, _end, (unsigned)sharedPre, _rlp);
#if ENABLE_DEBUG_PRINT
if (g_hashDebug)
std::cerr << s_indent << "= " << hex << sha3(_rlp.out()) << std::endl;
@ -517,7 +517,7 @@ byte TrieBranchNode::activeBranch() const
if (m_nodes[i] != nullptr)
{
if (n == (byte)-1)
n = i;
n = (byte)i;
else
return 16;
}
@ -535,7 +535,7 @@ TrieNode* TrieInfixNode::insert(bytesConstRef _key, std::string const& _value)
}
else
{
int prefix = commonPrefix(_key, m_ext);
uint prefix = commonPrefix(_key, m_ext);
if (prefix)
{
// one infix becomes two infixes, then insert into the second

27
libethereum/Trie.h

@ -22,7 +22,6 @@
#pragma once
#include <map>
#include <leveldb/db.h>
#include "RLP.h"
namespace ldb = leveldb;
@ -59,23 +58,6 @@ private:
TrieNode* m_root;
};
/*class HashDBFace
{
public:
virtual void insert(h256 _key, bytesConstRef _value) = 0;
virtual void remove(h256 _key) = 0;
virtual std::string at(h256 _key) const = 0;
};
class HashDBOverlay
{
public:
virtual void insert(h256 _key, bytesConstRef _value) = 0;
virtual void remove(h256 _key) = 0;
virtual std::string at(h256 _key) const = 0;
};*/
inline byte nibble(bytesConstRef _data, uint _i)
{
return (_i & 1) ? (_data[_i / 2] & 15) : (_data[_i / 2] >> 4);
@ -163,7 +145,10 @@ private:
ldb::WriteOptions m_writeOptions;
};
#if WIN32
#pragma warning(push)
#pragma warning(disable:4100) // disable warnings so it compiles
#endif
/**
* @brief Merkle Patricia Tree "Trie": a modifed base-16 Radix tree.
@ -264,6 +249,10 @@ private:
DB* m_db = nullptr;
};
#if WIN32
#pragma warning(pop)
#endif
template <class KeyType, class DB>
class TrieDB: public GenericTrieDB<DB>
{

24
libethereum/vector_ref.h

@ -4,7 +4,15 @@
#include <cassert>
#include <vector>
#include <string>
#if WIN32
#pragma warning(push)
#pragma warning(disable: 4267)
#endif
#include <leveldb/db.h>
#if WIN32
#pragma warning(pop)
#endif
namespace eth
{
@ -17,7 +25,7 @@ public:
typedef _T element_type;
vector_ref(): m_data(nullptr), m_count(0) {}
vector_ref(_T* _data, unsigned _count): m_data(_data), m_count(_count) {}
vector_ref(_T* _data, size_t _count): m_data(_data), m_count(_count) {}
vector_ref(std::string* _data): m_data((_T*)_data->data()), m_count(_data->size() / sizeof(_T)) {}
vector_ref(typename std::conditional<std::is_const<_T>::value, std::vector<typename std::remove_const<_T>::type> const*, std::vector<_T>*>::type _data): m_data(_data->data()), m_count(_data->size()) {}
vector_ref(typename std::conditional<std::is_const<_T>::value, std::string const&, std::string&>::type _data): m_data((_T*)_data.data()), m_count(_data.size() / sizeof(_T)) {}
@ -31,11 +39,11 @@ public:
template <class _T2> operator vector_ref<_T2>() const { assert(m_count * sizeof(_T) / sizeof(_T2) * sizeof(_T2) / sizeof(_T) == m_count); return vector_ref<_T2>((_T2*)m_data, m_count * sizeof(_T) / sizeof(_T2)); }
_T* data() const { return m_data; }
unsigned count() const { return m_count; }
unsigned size() const { return m_count; }
unsigned empty() const { return !m_count; }
size_t count() const { return m_count; }
size_t size() const { return m_count; }
bool empty() const { return !m_count; }
vector_ref<_T> next() const { return vector_ref<_T>(m_data + m_count, m_count); }
vector_ref<_T> cropped(unsigned _begin, int _count = -1) const { if (m_data && _begin + std::max(0, _count) <= m_count) return vector_ref<_T>(m_data + _begin, _count < 0 ? m_count - _begin : _count); else return vector_ref<_T>(); }
vector_ref<_T> cropped(size_t _begin, size_t _count = ~size_t(0)) const { if (m_data && _begin + std::max(size_t(0), _count) <= m_count) return vector_ref<_T>(m_data + _begin, _count == ~size_t(0) ? m_count - _begin : _count); else return vector_ref<_T>(); }
void retarget(_T const* _d, size_t _s) { m_data = _d; m_count = _s; }
void retarget(std::vector<_T> const& _t) { m_data = _t.data(); m_count = _t.size(); }
@ -44,8 +52,8 @@ public:
_T const* begin() const { return m_data; }
_T const* end() const { return m_data + m_count; }
_T& operator[](unsigned _i) { assert(m_data); assert(_i < m_count); return m_data[_i]; }
_T const& operator[](unsigned _i) const { assert(m_data); assert(_i < m_count); return m_data[_i]; }
_T& operator[](size_t _i) { assert(m_data); assert(_i < m_count); return m_data[_i]; }
_T const& operator[](size_t _i) const { assert(m_data); assert(_i < m_count); return m_data[_i]; }
bool operator==(vector_ref<_T> const& _cmp) const { return m_data == _cmp.m_data && m_count == _cmp.m_count; }
bool operator!=(vector_ref<_T> const& _cmp) const { return !operator==(); }
@ -56,7 +64,7 @@ public:
private:
_T* m_data;
unsigned m_count;
size_t m_count;
};
template<class _T> vector_ref<_T const> ref(_T const& _t) { return vector_ref<_T const>(&_t, 1); }

184
test/Test.vcxproj

@ -0,0 +1,184 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<Keyword>Win32Proj</Keyword>
<RootNamespace>Test</RootNamespace>
<ProjectName>Test</ProjectName>
<ProjectGuid>{3F3E389B-88DE-41D5-A73B-4F6036E18B36}</ProjectGuid>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\libethereum\LibEthereum.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\libethereum\LibEthereum.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\libethereum\LibEthereum.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\libethereum\LibEthereum.props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>../libethereum</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<IntrinsicFunctions>true</IntrinsicFunctions>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
<OpenMPSupport>true</OpenMPSupport>
<DisableSpecificWarnings>4351</DisableSpecificWarnings>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<LargeAddressAware>true</LargeAddressAware>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>../libethereum</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<IntrinsicFunctions>true</IntrinsicFunctions>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
<OpenMPSupport>true</OpenMPSupport>
<DisableSpecificWarnings>4351</DisableSpecificWarnings>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>../libethereum</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<StringPooling>true</StringPooling>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
<OpenMPSupport>true</OpenMPSupport>
<DisableSpecificWarnings>4351</DisableSpecificWarnings>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<LargeAddressAware>true</LargeAddressAware>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>../libethereum</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<StringPooling>true</StringPooling>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
<OpenMPSupport>true</OpenMPSupport>
<DisableSpecificWarnings>4351</DisableSpecificWarnings>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ProjectReference Include="..\..\cryptopp562\cryptlib.vcxproj">
<Project>{3423ec9a-52e4-4a4d-9753-edebc38785ef}</Project>
</ProjectReference>
<ProjectReference Include="..\libethereum\LibEthereum.vcxproj">
<Project>{7050c7cf-7551-48be-8e57-92235906c13a}</Project>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

19
test/main.cpp

@ -39,22 +39,23 @@ int main()
/*
// Test dagger
{
Dagger d((h256)0);
Dagger d;
auto s = steady_clock::now();
cout << hex << d.eval(0);
cout << hex << d.eval((h256)1, 0);
cout << " " << dec << duration_cast<milliseconds>(steady_clock::now() - s).count() << " ms" << endl;
cout << hex << d.eval(1);
cout << hex << d.eval((h256)1, 1);
cout << " " << dec << duration_cast<milliseconds>(steady_clock::now() - s).count() << " ms" << endl;
}
{
Dagger d((h256)1);
Dagger d;
auto s = steady_clock::now();
cout << hex << d.eval(0);
cout << hex << d.eval((h256)1, 0);
cout << " " << dec << duration_cast<milliseconds>(steady_clock::now() - s).count() << " ms" << endl;
cout << hex << d.eval(1);
cout << hex << d.eval((h256)1, 1);
cout << " " << dec << duration_cast<milliseconds>(steady_clock::now() - s).count() << " ms" << endl;
}
*/
//*/
/*
// Test transaction.
bytes tx = fromUserHex("88005401010101010101010101010101010101010101011f0de0b6b3a76400001ce8d4a5100080181c373130a009ba1f10285d4e659568bfcfec85067855c5a3c150100815dad4ef98fd37cf0593828c89db94bd6c64e210a32ef8956eaa81ea9307194996a3b879441f5d");
@ -239,7 +240,6 @@ int main()
remove("do");
remove("doge");
remove("doe");
for (int a = 0; a < 20; ++a)
{
StringMap m;
@ -274,7 +274,8 @@ int main()
assert(asString(rlp("dog")) == "\x43""dog");
// 2-item list
RLP twoItemList((byte const*)"\x82\x0f\x43""dog", 6);
string twoItemListString = "\x82\x0f\x43""dog";
RLP twoItemList(twoItemListString);
assert(twoItemList.itemCount() == 2);
assert(twoItemList[0] == 15);
assert(twoItemList[1] == "dog");

Loading…
Cancel
Save