Browse Source

Where do I start?

peercoin
Intel 11 years ago
parent
commit
c9745b41ac
  1. 6
      CMakeLists.txt
  2. 23
      cmake/macros/FindGMP.cmake
  3. 5
      src/server/poolserver/CMakeLists.txt
  4. 9
      src/server/poolserver/Server/Server.cpp
  5. 22
      src/server/poolserver/Stratum/Server.h
  6. 8
      src/server/shared/BigNum.h
  7. 7
      src/server/shared/Bitcoin/Bitcoin.h
  8. 45
      src/server/shared/Bitcoin/Block.h
  9. 65
      src/server/shared/Bitcoin/Script.h
  10. 42
      src/server/shared/Bitcoin/Transaction.h
  11. 5
      src/server/shared/CMakeLists.txt
  12. 2
      src/server/shared/Common.h
  13. 34
      src/server/shared/Crypto.h
  14. 2
      src/server/shared/MySQL/PreparedStatement.h
  15. 43
      src/server/shared/Util.cpp
  16. 7
      src/server/shared/Util.h

6
CMakeLists.txt

@ -38,6 +38,12 @@ if( MYSQL ) @@ -38,6 +38,12 @@ if( MYSQL )
find_package(MySQL REQUIRED)
endif()
# GMP
find_package(GMP REQUIRED)
# OpenSSL
find_package(OpenSSL REQUIRED)
# Print options
include(cmake/showoptions.cmake)

23
cmake/macros/FindGMP.cmake

@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
# Try to find the GMP librairies
# GMP_FOUND - system has GMP lib
# GMP_INCLUDE_DIR - the GMP include directory
# GMP_LIBRARIES - Libraries needed to use GMP
# Copyright (c) 2006, Laurent Montel, <montel@kde.org>
#
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
if (GMP_INCLUDE_DIR AND GMP_LIBRARIES)
# Already in cache, be silent
set(GMP_FIND_QUIETLY TRUE)
endif (GMP_INCLUDE_DIR AND GMP_LIBRARIES)
find_path(GMP_INCLUDE_DIR NAMES gmp.h )
find_library(GMP_LIBRARIES NAMES gmp libgmp )
find_library(GMPXX_LIBRARIES NAMES gmpxx libgmpxx )
MESSAGE(STATUS "GMP libs: " ${GMP_LIBRARIES} " " ${GMPXX_LIBRARIES} )
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(GMP DEFAULT_MSG GMP_INCLUDE_DIR GMP_LIBRARIES)
mark_as_advanced(GMP_INCLUDE_DIR GMP_LIBRARIES)

5
src/server/poolserver/CMakeLists.txt

@ -22,8 +22,10 @@ include_directories( @@ -22,8 +22,10 @@ include_directories(
${CMAKE_SOURCE_DIR}/src/server/shared/Logging
${CMAKE_SOURCE_DIR}/src/server/shared/JSON
${CMAKE_SOURCE_DIR}/src/server/shared/JSONRPC
${CMAKE_SOURCE_DIR}/src/server/shared/Bitcoin
${Boost_INCLUDE_DIR}
${MYSQL_INCLUDE_DIR}
${GMP_INCLUDE_DIR}
)
# Create executable
@ -36,6 +38,9 @@ target_link_libraries(poolserver @@ -36,6 +38,9 @@ target_link_libraries(poolserver
shared
${Boost_LIBRARIES}
${MYSQL_LIBRARY}
${GMP_LIBRARIES}
${GMPXX_LIBRARIES}
${OPENSSL_LIBRARIES}
)
# Install

9
src/server/poolserver/Server/Server.cpp

@ -4,10 +4,12 @@ @@ -4,10 +4,12 @@
#include "Log.h"
#include "Stratum/Server.h"
#include "ServerDatabaseEnv.h"
#include "Crypto.h"
#include <boost/thread.hpp>
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <algorithm>
Server::Server() : serverLoops(0)
{
@ -32,6 +34,13 @@ int Server::Run() @@ -32,6 +34,13 @@ int Server::Run()
//InitDatabase();
//sLog.Info(LOG_SERVER, "Hash: %s", Util::BinToASCII(Crypto::SHA256("labas")).c_str());
std::vector<byte> trans = Util::ASCIIToBin("010000002e0fb65201eb031afcccb670d0abbb94e0d2a97c2c81c8dd3eac02a92780578e06d1c5d067000000006c493046022100fdac85d847568c37edb07101ba0e7a32b47491c7cd81798190cbc190cbc439e5022100ab6145c93a822d861ae76482506425f05016bac7b901c08aa58dff8cd7d8e0cb012102421c601634e066fcc4d8aa768dfad475ae9eccc5daf3169522572468550483dbffffffff02a08fb502000000001976a9142ef2b6de3e3a269c5883a0717948e257814b6ba688ac8cf51300000000001976a9144221049afbb6ddb1886a12d7d0300365e8769f2088ac00000000");
//std::vector<byte> trans = Util::ASCIIToBin("abcd");
//std::reverse(trans.begin(), trans.end());
sLog.Info(LOG_SERVER, "Trans: %s", Util::BinToASCII(trans).c_str());
sLog.Info(LOG_SERVER, "Hash: %s", Util::BinToASCII(Util::Reverse(Crypto::SHA256D(trans))).c_str());
// Main io service
asio::io_service io_service;

22
src/server/poolserver/Stratum/Server.h

@ -7,6 +7,8 @@ @@ -7,6 +7,8 @@
#include "Log.h"
#include "JSON.h"
#include "JSONRPC.h"
#include "Bitcoin.h"
#include "Util.h"
#include <boost/asio.hpp>
#include <boost/bind.hpp>
@ -85,6 +87,16 @@ namespace Stratum @@ -85,6 +87,16 @@ namespace Stratum
_StartAccept();
}
void _UpdateWork(bool reset)
{
JSON response = _bitcoinrpc->Query("getblocktemplate");
Bitcoin::Block* block = new Bitcoin::Block();
block->version = response.Get<uint32>("version");
block->prevBlockHash = Util::ASCIIToBin(response.Get<std::string>("previousblockhash"));
}
void _CheckBlocks()
{
sLog.Debug(LOG_STRATUM, "Checking for new blocks...");
@ -92,13 +104,10 @@ namespace Stratum @@ -92,13 +104,10 @@ namespace Stratum
JSON response = _bitcoinrpc->Query("getinfo");
uint32 curBlock = response.Get<uint32>("blocks");
// Initializing server
if (_blockHeight == 0) {
_blockHeight = curBlock;
} else if (curBlock > _blockHeight) {
if (curBlock > _blockHeight) {
sLog.Debug(LOG_STRATUM, "New block on network! Height: %u", curBlock);
_blockHeight = curBlock;
// do some crazy stuff
_UpdateWork(true);
}
_blockCheckTimer.expires_from_now(boost::posix_time::milliseconds(sConfig.Get<uint32>("StratumBlockCheckTime")));
@ -116,6 +125,9 @@ namespace Stratum @@ -116,6 +125,9 @@ namespace Stratum
// Bitcoin info
asio::deadline_timer _blockCheckTimer;
uint32 _blockHeight;
// Work
Bitcoin::Block* _currentWork;
};
}

8
src/server/shared/BigNum.h

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
#ifndef BIGNUM_H_
#define BIGNUM_H_
#include <gmpxx.h>
typedef mpz_class BigNum;
#endif

7
src/server/shared/Bitcoin/Bitcoin.h

@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
#ifndef BITCOIN_H_
#define BITCOIN_H_
#include "Block.h"
#include "Transaction.h"
#endif

45
src/server/shared/Bitcoin/Block.h

@ -1,40 +1,33 @@ @@ -1,40 +1,33 @@
#ifndef BITCOIN_H_
#define BITCOIN_H_
#ifndef BITCOIN_BLOCK_H_
#define BITCOIN_BLOCK_H_
#include "Common.h"
#include "Transaction.h"
#include <vector>
namespace Bitcoin
{
class TxIn
{
};
class TxOut
{
};
class Transaction
{
uint32 Version;
std::vector<TxIn> in;
std::vector<TxOut> out;
uint32 LockTime;
};
class BlockHeader
{
uint32 Version;
std::array<char, 32> PrevBlockHash;
std::array<char, 32> MerkleRootHash;
uint32 Time;
uint32 Bits;
uint32 Nonce;
public:
uint32 version;
std::vector<byte> prevBlockHash;
std::vector<byte> merkleRootHash;
uint32 time;
uint32 bits;
uint32 nonce;
};
class Block : public BlockHeader
{
public:
std::vector<Transaction> tx;
std::vector<byte> GenerateMerkle()
{
}
};
class BlockTemplate
}
#endif

65
src/server/shared/Bitcoin/Script.h

@ -0,0 +1,65 @@ @@ -0,0 +1,65 @@
#ifndef BITCOIN_SCRIPT_H_
#define BITCOIN_SCRIPT_H_
#include "Common.h"
#include <boost/range/join.hpp>
#include <vector>
namespace Bitcoin
{
enum ScriptOPCode
{
OP_0 = 0x00,
OP_FALSE = OP_0,
OP_PUSHDATA1 = 0x4C,
OP_PUSHDATA2 = 0x4D,
OP_PUSHDATA4 = 0x4E,
OP_CHECKSIG = 0xAC,
};
class Script
{
public:
Script() {}
Script(std::vector<byte> data) : script(data) {}
std::vector<byte> script;
const Script operator+(const Script& other)
{
std::vector<byte> tmp = script;
tmp.insert(tmp.end(), other.script.begin(), other.script.end());
return Script(tmp);
}
const Script operator+(const std::vector<byte> data)
{
Script tmp(script);
size_t size = data.size();
if (size >= 1 && size <= 75) {
// We know size!
tmp.script.resize(size+1);
// Push data size
tmp.script.push_back((byte)size);
// We start from 1 because index 0 is size opcode
for (uint16 i = 1; i <= size; ++i)
tmp.script[i] = (byte)data[i];
}
return tmp;
}
const Script operator+(const ScriptOPCode op)
{
Script tmp(script);
tmp.script.push_back((byte)op);
return tmp;
}
};
}
#endif

42
src/server/shared/Bitcoin/Transaction.h

@ -0,0 +1,42 @@ @@ -0,0 +1,42 @@
#ifndef BITCOIN_TRANSACTION_H_
#define BITCOIN_TRANSACTION_H_
#include "Common.h"
#include "BigNum.h"
#include "Script.h"
namespace Bitcoin
{
class OutPoint
{
std::vector<byte> hash;
uint32 n;
};
class InPoint
{
};
class TxIn
{
OutPoint prevout;
Script script;
uint32 n;
};
class TxOut
{
int64 value;
Script scriptPubKey;
};
class Transaction
{
uint32 version;
std::vector<TxIn> in;
std::vector<TxOut> out;
uint32 lockTime;
};
}
#endif

5
src/server/shared/CMakeLists.txt

@ -4,6 +4,7 @@ file(GLOB_RECURSE sources_MySQL MySQL/*.cpp MySQL/*.h) @@ -4,6 +4,7 @@ file(GLOB_RECURSE sources_MySQL MySQL/*.cpp MySQL/*.h)
file(GLOB_RECURSE sources_Logging Logging/*.cpp Logging/*.h)
file(GLOB_RECURSE sources_JSON JSON/*.cpp JSON/*.h)
file(GLOB_RECURSE sources_JSONRPC JSONRPC/*.cpp JSONRPC/*.h)
file(GLOB_RECURSE sources_Bitcoin Bitcoin/*.cpp Bitcoin/*.h)
file(GLOB sources_localdir *.cpp *.h)
@ -13,6 +14,7 @@ set(sources_Shared @@ -13,6 +14,7 @@ set(sources_Shared
${sources_Logging}
${sources_JSON}
${sources_JSONRPC}
${sources_Bitcoin}
${sources_localdir}
)
@ -23,8 +25,11 @@ include_directories( @@ -23,8 +25,11 @@ include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/Logging
${CMAKE_CURRENT_SOURCE_DIR}/JSON
${CMAKE_CURRENT_SOURCE_DIR}/JSONRPC
${CMAKE_CURRENT_SOURCE_DIR}/Bitcoin
${Boost_INCLUDE_DIR}
${MYSQL_INCLUDE_DIR}
${GMP_INCLUDE_DIR}
${OPENSSL_INCLUDE_DIR}
)
add_library(shared STATIC

2
src/server/shared/Common.h

@ -13,4 +13,6 @@ typedef int16_t int16; @@ -13,4 +13,6 @@ typedef int16_t int16;
typedef int32_t int32;
typedef int64_t int64;
typedef uint8_t byte;
#endif

34
src/server/shared/Crypto.h

@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
#ifndef CRYPTO_H_
#define CRYPTO_H_
#include "Common.h"
#include "Util.h"
#include <string>
#include <vector>
#include <openssl/sha.h>
namespace Crypto
{
std::vector<byte> SHA256(std::vector<byte> data)
{
std::vector<byte> hash;
hash.resize(SHA256_DIGEST_LENGTH);
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, &data[0], data.size());
SHA256_Final(&hash[0], &sha256);
return std::vector<byte>(hash.begin(), hash.end());
}
std::vector<byte> SHA256(std::string data)
{
return SHA256(std::vector<byte>(data.begin(), data.end()));
}
std::vector<byte> SHA256D(std::vector<byte> data)
{
return SHA256(SHA256(data));
}
}
#endif

2
src/server/shared/MySQL/PreparedStatement.h

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
#ifndef PREPARED_STATEMENT_MYSQL_H_
#define PREPARED_STATEMENT_MYSQL_H_
#include <Common.h>
#include "Common.h"
#include <boost/variant.hpp>
#include <mysql.h>

43
src/server/shared/Util.cpp

@ -42,3 +42,46 @@ std::string Util::FromBase64(std::string input) @@ -42,3 +42,46 @@ std::string Util::FromBase64(std::string input)
result.erase(result.end() - paddChars, result.end()); // erase padding '\0' characters
return result;
}
uint8 Util::ASCIIToHex(char ch)
{
if (ch > 47 && ch < 58)
return ch - 48;
else if (ch > 64 && ch < 71)
return ch - 55;
else if (ch > 96 && ch < 103)
return ch - 87;
else
return 0; // Invalid
}
std::vector<byte> Util::ASCIIToBin(std::string str)
{
std::vector<byte> data;
data.resize(str.size()/2, 0);
for (uint64 i = 0; i < str.size(); ++i) {
if (i%2)
data[i/2] += ASCIIToHex(str[i]);
else
data[i/2] += ASCIIToHex(str[i])*16;
}
return data;
}
std::string Util::BinToASCII(std::vector<byte> data)
{
std::string str;
for (uint64 i = 0; i < data.size(); ++i)
{
str += "0123456789abcdef"[data[i]/16];
str += "0123456789abcdef"[data[i]%16];
}
return str;
}
std::vector<byte> Util::Reverse(std::vector<byte> data)
{
std::vector<byte> out = data;
std::reverse(out.begin(), out.end());
return out;
}

7
src/server/shared/Util.h

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
#ifndef UTIL_H_
#define UTIL_H_
#include <Common.h>
#include "Common.h"
#include <sstream>
#include <iostream>
@ -103,6 +103,11 @@ namespace Util @@ -103,6 +103,11 @@ namespace Util
std::string ToBase64(std::string input, bool linebreaks = true);
std::string FromBase64(std::string input);
uint8 ASCIIToHex(char ch);
std::vector<byte> ASCIIToBin(std::string str);
std::string BinToASCII(std::vector<byte> data);
std::vector<byte> Reverse(std::vector<byte> data);
}
#endif

Loading…
Cancel
Save