-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from bulwark-crypto/dev
v1.3.1
- Loading branch information
Showing
108 changed files
with
1,436 additions
and
2,245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
\.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright (c) 2009-2010 Satoshi Nakamoto | ||
// Copyright (c) 2009-2016 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include "addrdb.h" | ||
|
||
#include "addrman.h" | ||
#include "chainparams.h" | ||
#include "clientversion.h" | ||
#include "fs.h" | ||
#include "hash.h" | ||
#include "random.h" | ||
#include "streams.h" | ||
#include "tinyformat.h" | ||
#include "util.h" | ||
|
||
|
||
CBanDB::CBanDB() | ||
{ | ||
pathBanlist = GetDataDir() / "banlist.dat"; | ||
} | ||
|
||
bool CBanDB::Write(const banmap_t& banSet) | ||
{ | ||
// Generate random temporary filename | ||
unsigned short randv = 0; | ||
GetRandBytes((unsigned char*)&randv, sizeof(randv)); | ||
std::string tmpfn = strprintf("banlist.dat.%04x", randv); | ||
|
||
// serialize banlist, checksum data up to that point, then append csum | ||
CDataStream ssBanlist(SER_DISK, CLIENT_VERSION); | ||
ssBanlist << FLATDATA(Params().MessageStart()); | ||
ssBanlist << banSet; | ||
uint256 hash = Hash(ssBanlist.begin(), ssBanlist.end()); | ||
ssBanlist << hash; | ||
|
||
// open temp output file, and associate with CAutoFile | ||
fs::path pathTmp = GetDataDir() / tmpfn; | ||
FILE *file = fsbridge::fopen(pathTmp, "wb"); | ||
CAutoFile fileout(file, SER_DISK, CLIENT_VERSION); | ||
if (fileout.IsNull()) | ||
return error("%s: Failed to open file %s", __func__, pathTmp.string()); | ||
|
||
// Write and commit header, data | ||
try { | ||
fileout << ssBanlist; | ||
} | ||
catch (const std::exception& e) { | ||
return error("%s: Serialize or I/O error - %s", __func__, e.what()); | ||
} | ||
FileCommit(fileout.Get()); | ||
fileout.fclose(); | ||
|
||
// replace existing banlist.dat, if any, with new banlist.dat.XXXX | ||
if (!RenameOver(pathTmp, pathBanlist)) | ||
return error("%s: Rename-into-place failed", __func__); | ||
|
||
return true; | ||
} | ||
|
||
bool CBanDB::Read(banmap_t& banSet) | ||
{ | ||
// open input file, and associate with CAutoFile | ||
FILE *file = fsbridge::fopen(pathBanlist, "rb"); | ||
CAutoFile filein(file, SER_DISK, CLIENT_VERSION); | ||
if (filein.IsNull()) | ||
return error("%s: Failed to open file %s", __func__, pathBanlist.string()); | ||
|
||
// use file size to size memory buffer | ||
uint64_t fileSize = fs::file_size(pathBanlist); | ||
uint64_t dataSize = 0; | ||
// Don't try to resize to a negative number if file is small | ||
if (fileSize >= sizeof(uint256)) | ||
dataSize = fileSize - sizeof(uint256); | ||
std::vector<unsigned char> vchData; | ||
vchData.resize(dataSize); | ||
uint256 hashIn; | ||
|
||
// read data and checksum from file | ||
try { | ||
filein.read((char *)&vchData[0], dataSize); | ||
filein >> hashIn; | ||
} | ||
catch (const std::exception& e) { | ||
return error("%s: Deserialize or I/O error - %s", __func__, e.what()); | ||
} | ||
filein.fclose(); | ||
|
||
CDataStream ssBanlist(vchData, SER_DISK, CLIENT_VERSION); | ||
|
||
// verify stored checksum matches input data | ||
uint256 hashTmp = Hash(ssBanlist.begin(), ssBanlist.end()); | ||
if (hashIn != hashTmp) | ||
return error("%s: Checksum mismatch, data corrupted", __func__); | ||
|
||
unsigned char pchMsgTmp[4]; | ||
try { | ||
// de-serialize file header (network specific magic number) and .. | ||
ssBanlist >> FLATDATA(pchMsgTmp); | ||
|
||
// ... verify the network matches ours | ||
if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp))) | ||
return error("%s: Invalid network magic number", __func__); | ||
|
||
// de-serialize ban data | ||
ssBanlist >> banSet; | ||
} | ||
catch (const std::exception& e) { | ||
return error("%s: Deserialize or I/O error - %s", __func__, e.what()); | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright (c) 2009-2010 Satoshi Nakamoto | ||
// Copyright (c) 2009-2016 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef BITCOIN_ADDRDB_H | ||
#define BITCOIN_ADDRDB_H | ||
|
||
#include "fs.h" | ||
#include "serialize.h" | ||
|
||
#include <string> | ||
#include <map> | ||
|
||
class CSubNet; | ||
class CAddrMan; | ||
class CDataStream; | ||
|
||
typedef enum BanReason | ||
{ | ||
BanReasonUnknown = 0, | ||
BanReasonNodeMisbehaving = 1, | ||
BanReasonManuallyAdded = 2 | ||
} BanReason; | ||
|
||
class CBanEntry | ||
{ | ||
public: | ||
static const int CURRENT_VERSION = 1; | ||
int nVersion; | ||
int64_t nCreateTime; | ||
int64_t nBanUntil; | ||
uint8_t banReason; | ||
|
||
CBanEntry() | ||
{ | ||
SetNull(); | ||
} | ||
|
||
CBanEntry(int64_t nCreateTimeIn) | ||
{ | ||
SetNull(); | ||
nCreateTime = nCreateTimeIn; | ||
} | ||
|
||
ADD_SERIALIZE_METHODS; | ||
|
||
template <typename Stream, typename Operation> | ||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { | ||
READWRITE(this->nVersion); | ||
READWRITE(nCreateTime); | ||
READWRITE(nBanUntil); | ||
READWRITE(banReason); | ||
} | ||
|
||
void SetNull() | ||
{ | ||
nVersion = CBanEntry::CURRENT_VERSION; | ||
nCreateTime = 0; | ||
nBanUntil = 0; | ||
banReason = BanReasonUnknown; | ||
} | ||
|
||
std::string banReasonToString() | ||
{ | ||
switch (banReason) { | ||
case BanReasonNodeMisbehaving: | ||
return "node misbehaving"; | ||
case BanReasonManuallyAdded: | ||
return "manually added"; | ||
default: | ||
return "unknown"; | ||
} | ||
} | ||
}; | ||
|
||
typedef std::map<CSubNet, CBanEntry> banmap_t; | ||
|
||
/** Access to the banlist database (banlist.dat) */ | ||
class CBanDB | ||
{ | ||
private: | ||
fs::path pathBanlist; | ||
public: | ||
CBanDB(); | ||
bool Write(const banmap_t& banSet); | ||
bool Read(banmap_t& banSet); | ||
}; | ||
|
||
#endif // BITCOIN_ADDRDB_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.