Xjail/Spartancoin Project

Check-in [45f336350a]
Login
Overview
Comment:upgrade to c++17 (std::auto_ptr was removed in c++17 (use std::unique_ptr); std::random_shuffle was removed in c++17 (use custom xjail::random_shuffle, not std::shuffle as no enough test))
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | refork-at-8ca3912a49
Files: files | file ages | folders
SHA3-256:45f336350af29da0491834d07fa1118bfa4825ceaf0af8a686cc739f0ccb4a3f
User & Date: xjail 2018-04-02 02:11:49
Context
2018-04-06
14:58
merge check-in: 3f22b893bf user: xjail tags: boost-1.66.0
2018-04-02
02:11
upgrade to c++17 (std::auto_ptr was removed in c++17 (use std::unique_ptr); std::random_shuffle was removed in c++17 (use custom xjail::random_shuffle, not std::shuffle as no enough test)) Closed-Leaf check-in: 45f336350a user: xjail tags: refork-at-8ca3912a49
01:23
Create new branch named "refork-at-8ca3912a49" check-in: 52db5e4447 user: xjail tags: refork-at-8ca3912a49
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to spartancoin-qt.pro.

5
6
7
8
9
10
11

12
13
14
15
16
17
18
INCLUDEPATH += src src/json src/qt
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
DEFINES += QT_GUI BOOST_THREAD_USE_LIB BOOST_SPIRIT_THREADSAFE
CONFIG += no_include_pwd
CONFIG += thread
CONFIG += static


# for boost 1.37, add -mt to the boost libraries
# use: qmake BOOST_LIB_SUFFIX=-mt
# for boost thread win32 with _win32 sufix
# use: BOOST_THREAD_LIB_SUFFIX=_win32-...
# or when linking against a specific BerkelyDB version: BDB_LIB_SUFFIX=-5.3








>







5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
INCLUDEPATH += src src/json src/qt
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
DEFINES += QT_GUI BOOST_THREAD_USE_LIB BOOST_SPIRIT_THREADSAFE
CONFIG += no_include_pwd
CONFIG += thread
CONFIG += static
QMAKE_CXXFLAGS += -std=c++17

# for boost 1.37, add -mt to the boost libraries
# use: qmake BOOST_LIB_SUFFIX=-mt
# for boost thread win32 with _win32 sufix
# use: BOOST_THREAD_LIB_SUFFIX=_win32-...
# or when linking against a specific BerkelyDB version: BDB_LIB_SUFFIX=-5.3

Changes to src/leveldb.cpp.

1

2
3
4
5
6
7
8
9
10
11
12
13
14

15
16
17
18
19
20
21
22
..
66
67
68
69
70
71
72

73
74
75
76
77
78
79
80
81
// Copyright (c) 2012 The Bitcoin developers

// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "leveldb.h"
#include "util.h"

#include <leveldb/env.h>
#include <leveldb/cache.h>
#include <leveldb/filter_policy.h>
#include <memenv/memenv.h>

#include <boost/filesystem.hpp>


void HandleError(const leveldb::Status &status) throw(leveldb_error) {
    if (status.ok())
        return;
    if (status.IsCorruption())
        throw leveldb_error("Database corrupted");
    if (status.IsIOError())
        throw leveldb_error("Database I/O error");
    if (status.IsNotFound())
................................................................................
    options.filter_policy = NULL;
    delete options.block_cache;
    options.block_cache = NULL;
    delete penv;
    options.env = NULL;
}


bool CLevelDB::WriteBatch(CLevelDBBatch &batch, bool fSync) throw(leveldb_error) {
    leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
    if (!status.ok()) {
        printf("LevelDB write failure: %s\n", status.ToString().c_str());
        HandleError(status);
        return false;
    }
    return true;
}

>













>
|







 







>
|








1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
..
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) 2012 The Bitcoin developers
// Copyright (c) 2017-2018 xjail.tiv.cc developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "leveldb.h"
#include "util.h"

#include <leveldb/env.h>
#include <leveldb/cache.h>
#include <leveldb/filter_policy.h>
#include <memenv/memenv.h>

#include <boost/filesystem.hpp>

// c++17: replace throw(leveldb_error) with noexcept(false)
void HandleError(const leveldb::Status &status) noexcept(false) {
    if (status.ok())
        return;
    if (status.IsCorruption())
        throw leveldb_error("Database corrupted");
    if (status.IsIOError())
        throw leveldb_error("Database I/O error");
    if (status.IsNotFound())
................................................................................
    options.filter_policy = NULL;
    delete options.block_cache;
    options.block_cache = NULL;
    delete penv;
    options.env = NULL;
}

// c++17: replace throw(leveldb_error) with noexcept(false)
bool CLevelDB::WriteBatch(CLevelDBBatch &batch, bool fSync) noexcept(false) {
    leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
    if (!status.ok()) {
        printf("LevelDB write failure: %s\n", status.ToString().c_str());
        HandleError(status);
        return false;
    }
    return true;
}

Changes to src/leveldb.h.

1

2
3
4
5
6
7
8
..
13
14
15
16
17
18
19

20
21
22
23
24
25
26
27
..
76
77
78
79
80
81
82

83
84
85
86
87
88
89
90
..
99
100
101
102
103
104
105

106
107
108
109
110
111

112
113
114
115
116
117
118
119
...
122
123
124
125
126
127
128

129
130
131
132
133
134

135
136
137
138
139
140
141
142

143
144
145
146
147
148
149
150
151
152
153
// Copyright (c) 2012 The Bitcoin developers

// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_LEVELDB_H
#define BITCOIN_LEVELDB_H

#include "serialize.h"

................................................................................

class leveldb_error : public std::runtime_error
{
public:
    leveldb_error(const std::string &msg) : std::runtime_error(msg) {}
};


void HandleError(const leveldb::Status &status) throw(leveldb_error);

// Batch of changes queued to be written to a CLevelDB
class CLevelDBBatch
{
    friend class CLevelDB;

private:
................................................................................
    // the database itself
    leveldb::DB *pdb;

public:
    CLevelDB(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory = false, bool fWipe = false);
    ~CLevelDB();


    template<typename K, typename V> bool Read(const K& key, V& value) throw(leveldb_error) {
        CDataStream ssKey(SER_DISK, CLIENT_VERSION);
        ssKey.reserve(ssKey.GetSerializeSize(key));
        ssKey << key;
        leveldb::Slice slKey(&ssKey[0], ssKey.size());

        std::string strValue;
        leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
................................................................................
            ssValue >> value;
        } catch(std::exception &e) {
            return false;
        }
        return true;
    }


    template<typename K, typename V> bool Write(const K& key, const V& value, bool fSync = false) throw(leveldb_error) {
        CLevelDBBatch batch;
        batch.Write(key, value);
        return WriteBatch(batch, fSync);
    }


    template<typename K> bool Exists(const K& key) throw(leveldb_error) {
        CDataStream ssKey(SER_DISK, CLIENT_VERSION);
        ssKey.reserve(ssKey.GetSerializeSize(key));
        ssKey << key;
        leveldb::Slice slKey(&ssKey[0], ssKey.size());

        std::string strValue;
        leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
................................................................................
                return false;
            printf("LevelDB read failure: %s\n", status.ToString().c_str());
            HandleError(status);
        }
        return true;
    }


    template<typename K> bool Erase(const K& key, bool fSync = false) throw(leveldb_error) {
        CLevelDBBatch batch;
        batch.Erase(key);
        return WriteBatch(batch, fSync);
    }


    bool WriteBatch(CLevelDBBatch &batch, bool fSync = false) throw(leveldb_error);

    // not available for LevelDB; provide for compatibility with BDB
    bool Flush() {
        return true;
    }

    bool Sync() throw(leveldb_error) {

        CLevelDBBatch batch;
        return WriteBatch(batch, true);
    }

    // not exactly clean encapsulation, but it's easiest for now
    leveldb::Iterator *NewIterator() {
        return pdb->NewIterator(iteroptions);
    }
};

#endif // BITCOIN_LEVELDB_H

>







 







>
|







 







>
|







 







>
|





>
|







 







>
|





>
|






|
>











1
2
3
4
5
6
7
8
9
..
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
..
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
...
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
...
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) 2012 The Bitcoin developers
// Copyright (c) 2017-2018 xjail.tiv.cc developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_LEVELDB_H
#define BITCOIN_LEVELDB_H

#include "serialize.h"

................................................................................

class leveldb_error : public std::runtime_error
{
public:
    leveldb_error(const std::string &msg) : std::runtime_error(msg) {}
};

// c++17: replace throw(leveldb_error) with noexcept(false);
void HandleError(const leveldb::Status &status) noexcept(false);

// Batch of changes queued to be written to a CLevelDB
class CLevelDBBatch
{
    friend class CLevelDB;

private:
................................................................................
    // the database itself
    leveldb::DB *pdb;

public:
    CLevelDB(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory = false, bool fWipe = false);
    ~CLevelDB();

	// c++17: replace throw(leveldb_error) with noexcept(false);
    template<typename K, typename V> bool Read(const K& key, V& value) noexcept(false) {
        CDataStream ssKey(SER_DISK, CLIENT_VERSION);
        ssKey.reserve(ssKey.GetSerializeSize(key));
        ssKey << key;
        leveldb::Slice slKey(&ssKey[0], ssKey.size());

        std::string strValue;
        leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
................................................................................
            ssValue >> value;
        } catch(std::exception &e) {
            return false;
        }
        return true;
    }

	// c++17: replace throw(leveldb_error) with noexcept(false);
    template<typename K, typename V> bool Write(const K& key, const V& value, bool fSync = false) noexcept(false) {
        CLevelDBBatch batch;
        batch.Write(key, value);
        return WriteBatch(batch, fSync);
    }

	// c++17: replace throw(leveldb_error) with noexcept(false);
    template<typename K> bool Exists(const K& key) noexcept(false) {
        CDataStream ssKey(SER_DISK, CLIENT_VERSION);
        ssKey.reserve(ssKey.GetSerializeSize(key));
        ssKey << key;
        leveldb::Slice slKey(&ssKey[0], ssKey.size());

        std::string strValue;
        leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
................................................................................
                return false;
            printf("LevelDB read failure: %s\n", status.ToString().c_str());
            HandleError(status);
        }
        return true;
    }

	// c++17: replace throw(leveldb_error) with noexcept(false);
    template<typename K> bool Erase(const K& key, bool fSync = false) noexcept(false) {
        CLevelDBBatch batch;
        batch.Erase(key);
        return WriteBatch(batch, fSync);
    }

	// c++17: replace throw(leveldb_error) with noexcept(false);
    bool WriteBatch(CLevelDBBatch &batch, bool fSync = false) noexcept(false);

    // not available for LevelDB; provide for compatibility with BDB
    bool Flush() {
        return true;
    }

	// c++17: replace throw(leveldb_error) with noexcept(false);
    bool Sync() noexcept(false) {
        CLevelDBBatch batch;
        return WriteBatch(batch, true);
    }

    // not exactly clean encapsulation, but it's easiest for now
    leveldb::Iterator *NewIterator() {
        return pdb->NewIterator(iteroptions);
    }
};

#endif // BITCOIN_LEVELDB_H

Changes to src/main.cpp.

4346
4347
4348
4349
4350
4351
4352

4353
4354
4355
4356
4357
4358
4359
4360
....
4714
4715
4716
4717
4718
4719
4720

4721
4722
4723
4724
4725
4726
4727
4728
        }
    }
};

CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
{
    // Create new block

    auto_ptr<CBlockTemplate> pblocktemplate(new CBlockTemplate());
    if(!pblocktemplate.get())
        return NULL;
    CBlock *pblock = &pblocktemplate->block; // pointer for convenience

    // Create coinbase tx
    CTransaction txNew;
    txNew.vin.resize(1);
................................................................................

        //
        // Create new block
        //
        unsigned int nTransactionsUpdatedLast = nTransactionsUpdated;
        CBlockIndex* pindexPrev = pindexBest;


        auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey));
        if (!pblocktemplate.get())
            return;
        CBlock *pblock = &pblocktemplate->block;
        IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);

        printf("Running SpartancoinMiner with %s transactions in block (%u bytes)\n", 
		std::to_string(pblock->vtx.size()).c_str(),







>
|







 







>
|







4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
....
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
        }
    }
};

CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
{
    // Create new block
	// auto_ptr => unique_ptr, std::auto_ptr was removed in c++17
    std::unique_ptr<CBlockTemplate> pblocktemplate(new CBlockTemplate());
    if(!pblocktemplate.get())
        return NULL;
    CBlock *pblock = &pblocktemplate->block; // pointer for convenience

    // Create coinbase tx
    CTransaction txNew;
    txNew.vin.resize(1);
................................................................................

        //
        // Create new block
        //
        unsigned int nTransactionsUpdatedLast = nTransactionsUpdated;
        CBlockIndex* pindexPrev = pindexBest;

		// auto_ptr => unique_ptr, std::auto_ptr was removed in c++17
        std::unique_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey));
        if (!pblocktemplate.get())
            return;
        CBlock *pblock = &pblocktemplate->block;
        IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);

        printf("Running SpartancoinMiner with %s transactions in block (%u bytes)\n", 
		std::to_string(pblock->vtx.size()).c_str(),

Changes to src/makefile.freebsd.

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

DEBUGFLAGS=-g

# CXXFLAGS can be specified on the make command line, so we use xCXXFLAGS that only
# adds some defaults in front. Unfortunately, CXXFLAGS=... $(CXXFLAGS) does not work.
xCXXFLAGS=-O2 -Wall -Wextra -Wformat -Wformat-security -Wno-unused-parameter \
    $(DEBUGFLAGS) $(DEFS) $(HARDENING) $(CXXFLAGS)
xCXXFLAGS += -stdlib=libc++ -std=c++14

# LDFLAGS can be specified on the make command line, so we use xLDFLAGS that only
# adds some defaults in front. Unfortunately, LDFLAGS=... $(LDFLAGS) does not work.
xLDFLAGS=$(LDHARDENING) $(LDFLAGS)
xLDFLAGS += -lthr

OBJS= \







|







108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

DEBUGFLAGS=-g

# CXXFLAGS can be specified on the make command line, so we use xCXXFLAGS that only
# adds some defaults in front. Unfortunately, CXXFLAGS=... $(CXXFLAGS) does not work.
xCXXFLAGS=-O2 -Wall -Wextra -Wformat -Wformat-security -Wno-unused-parameter \
    $(DEBUGFLAGS) $(DEFS) $(HARDENING) $(CXXFLAGS)
xCXXFLAGS += -stdlib=libc++ -std=c++17

# LDFLAGS can be specified on the make command line, so we use xLDFLAGS that only
# adds some defaults in front. Unfortunately, LDFLAGS=... $(LDFLAGS) does not work.
xLDFLAGS=$(LDHARDENING) $(LDFLAGS)
xLDFLAGS += -lthr

OBJS= \

Changes to src/makefile.unix.

1

2
3
4
5
6
7
8
...
108
109
110
111
112
113
114

115
116
117
118
119
120
121
# Copyright (c) 2009-2010 Satoshi Nakamoto

# Distributed under the MIT/X11 software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

# :=0 --> UPnP support turned off by default at runtime
# :=1 --> UPnP support turned on by default at runtime
# :=- --> No UPnP support - miniupnp not required
USE_UPNP:=0
................................................................................

DEBUGFLAGS=-g

# CXXFLAGS can be specified on the make command line, so we use xCXXFLAGS that only
# adds some defaults in front. Unfortunately, CXXFLAGS=... $(CXXFLAGS) does not work.
xCXXFLAGS=-O2 -pthread -Wall -Wextra -Wformat -Wformat-security -Wno-unused-parameter \
    $(DEBUGFLAGS) $(DEFS) $(HARDENING) $(CXXFLAGS)


# LDFLAGS can be specified on the make command line, so we use xLDFLAGS that only
# adds some defaults in front. Unfortunately, LDFLAGS=... $(LDFLAGS) does not work.
xLDFLAGS=$(LDHARDENING) $(LDFLAGS)

OBJS= \
    leveldb/libleveldb.a \

>







 







>







1
2
3
4
5
6
7
8
9
...
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Copyright (c) 2009-2010 Satoshi Nakamoto
# Copyright (c) 2017-2018 xjail.tiv.cc developers
# Distributed under the MIT/X11 software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

# :=0 --> UPnP support turned off by default at runtime
# :=1 --> UPnP support turned on by default at runtime
# :=- --> No UPnP support - miniupnp not required
USE_UPNP:=0
................................................................................

DEBUGFLAGS=-g

# CXXFLAGS can be specified on the make command line, so we use xCXXFLAGS that only
# adds some defaults in front. Unfortunately, CXXFLAGS=... $(CXXFLAGS) does not work.
xCXXFLAGS=-O2 -pthread -Wall -Wextra -Wformat -Wformat-security -Wno-unused-parameter \
    $(DEBUGFLAGS) $(DEFS) $(HARDENING) $(CXXFLAGS)
xCXXFLAGS += -std=c++17

# LDFLAGS can be specified on the make command line, so we use xLDFLAGS that only
# adds some defaults in front. Unfortunately, LDFLAGS=... $(LDFLAGS) does not work.
xLDFLAGS=$(LDHARDENING) $(LDFLAGS)

OBJS= \
    leveldb/libleveldb.a \

Changes to src/wallet.cpp.

1037
1038
1039
1040
1041
1042
1043


























1044
1045
1046
1047
1048
1049
1050
....
1051
1052
1053
1054
1055
1056
1057


1058
1059
1060
1061
1062
1063
1064
1065
                        vfIncluded[i] = false;
                    }
                }
            }
        }
    }
}



























bool CWallet::SelectCoinsMinConf(int64 nTargetValue, int nConfMine, int nConfTheirs, vector<COutput> vCoins,
                                 set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const
{
    setCoinsRet.clear();
    nValueRet = 0;

................................................................................
    // List of values less than target
    pair<int64, pair<const CWalletTx*,unsigned int> > coinLowestLarger;
    coinLowestLarger.first = std::numeric_limits<int64>::max();
    coinLowestLarger.second.first = NULL;
    vector<pair<int64, pair<const CWalletTx*,unsigned int> > > vValue;
    int64 nTotalLower = 0;



    random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt);

    for(COutput output: vCoins)
    {
        const CWalletTx *pcoin = output.tx;

        if (output.nDepth < (pcoin->IsFromMe() ? nConfMine : nConfTheirs))
            continue;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
|







1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
....
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
                        vfIncluded[i] = false;
                    }
                }
            }
        }
    }
}

////////////////////////////////////////////////////////////////////////
// ====> Temporary Code <====
// Custom Random Shuffle. The std::random_shuffle was removed
// in c++17. This random_shuffle is a modification of 
// std::random_shuffle in LLVM c++/v1/algorithm from 
// FreeBSD System Headers.
// The best solution is using std::shuffle, which will be 
// implemented in the future to write a custom URBG class.
// Copyright (c) xjail.tiv.cc developers.
namespace xjail {
	template <typename RandomIter, typename RandomFunc>
	void random_shuffle(RandomIter first, RandomIter last, 
					RandomFunc && rand_fn) {
		using DiffType = typename std::iterator_traits<RandomIter>
						::difference_type;
		DiffType max_setting = last - first;
		if (max_setting > 1) {
			for (--last; first < last; ++first, --max_setting) {
				DiffType rand_i = rand_fn(max_setting);
				std::swap(*first, *(first + rand_i));
			}
		}
	}
}
////////////////////////////////////////////////////////////////////////

bool CWallet::SelectCoinsMinConf(int64 nTargetValue, int nConfMine, int nConfTheirs, vector<COutput> vCoins,
                                 set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const
{
    setCoinsRet.clear();
    nValueRet = 0;

................................................................................
    // List of values less than target
    pair<int64, pair<const CWalletTx*,unsigned int> > coinLowestLarger;
    coinLowestLarger.first = std::numeric_limits<int64>::max();
    coinLowestLarger.second.first = NULL;
    vector<pair<int64, pair<const CWalletTx*,unsigned int> > > vValue;
    int64 nTotalLower = 0;

	// std::random_shuffle => xjail::random_shuffle
	// xjail::random_shuffle was defined above.
    xjail::random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt);

    for(COutput output: vCoins)
    {
        const CWalletTx *pcoin = output.tx;

        if (output.nDepth < (pcoin->IsFromMe() ? nConfMine : nConfTheirs))
            continue;