Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Znort987 committed Nov 21, 2015
1 parent 115cec2 commit 42b247a
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 100 deletions.
1 change: 1 addition & 0 deletions TODO.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

TODO:
Make ^C global, not per callback
Separate tx hash computation and need for upstream

DONE:
Better progress in full parse (w/ ETA)
Expand Down
74 changes: 35 additions & 39 deletions callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,20 @@ Callback *Callback::find(
const char *name,
bool printList
) {

CBMap found;
size_t sz = strlen(name);
auto e = callbacks->end();
auto i = callbacks->begin();
while(i!=e) {
Callback *c = *(i++);
auto sz = strlen(name);
for(auto c:*callbacks) {

std::vector<const char*> names;
names.push_back(c->name());
c->aliases(names);

auto e = names.end();
auto i = names.begin();
while(i!=e) {
const char *nm = *(i++);
size_t x = std::min(sz, strlen(nm));
if(0==strncasecmp(nm, name, x)) found[(uintptr_t)c] = c;
for(const auto nm:names) {
auto x = std::min(sz, strlen(nm));
if(0==strncasecmp(nm, name, x)) {
found[(uintptr_t)c] = c;
}
}
}

Expand All @@ -54,16 +51,17 @@ Callback *Callback::find(
printf("\n");

if(!printList) {
warning("\"%s\": ambiguous command name, could be any of:\n", name);
warning(
"\"%s\": ambiguous command name, could be any of:\n",
name
);
}

int count = 0;
auto e = found.end();
auto i = found.begin();
while(i!=e) {
for(auto pair:found) {

std::vector<const char*> names;
Callback *c = (i++)->second;
auto c = pair.second;
c->aliases(names);

printf(
Expand All @@ -73,11 +71,8 @@ Callback *Callback::find(
0<names.size() ? " (aliases: " : ""
);

bool first = true;
auto e = names.end();
auto i = names.begin();
while(i!=e) {
const char *nm = *(i++);
auto first = true;
for(const auto nm:names) {
printf(
"%s%s",
first ? "" : ", ",
Expand All @@ -103,18 +98,6 @@ Callback *Callback::find(
exit(-1);
}

struct Cmp {
bool operator()(
const Callback *a,
const Callback *b
) {
const char *an = a->name();
const char *bn = b->name();
int n = strcmp(an, bn);
return n<0;
}
};

static void printHelp(
const Callback *c,
bool longHelp
Expand Down Expand Up @@ -143,15 +126,28 @@ void Callback::showHelpFor(
}
}

struct Cmp {
bool operator()(
const Callback *a,
const Callback *b
) {
auto an = a->name();
auto bn = b->name();
auto n = strcmp(an, bn);
return n<0;
}
};

void Callback::showAllHelps(
bool longHelp
) {
auto e = callbacks->end();
auto i = callbacks->begin();
std::sort(i, e, Cmp());

while(i!=e) {
printHelp(*(i++), longHelp);
std::sort(
callbacks->begin(),
callbacks->end(),
Cmp()
);
for(const auto c:*callbacks) {
printHelp(c, longHelp);
}
printf("\n");
}
Expand Down
20 changes: 15 additions & 5 deletions cb/closure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,22 @@

typedef uint160_t Addr;
static uint8_t gEmptyKey[kRIPEMD160ByteSize] = { 0x52 };
typedef GoogMap<Hash160, uint64_t, Hash160Hasher, Hash160Equal >::Map AddrMap;
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS> Graph;

struct Closure:public Callback
{
typedef GoogMap<
Hash160,
uint64_t,
Hash160Hasher,
Hash160Equal
>::Map AddrMap;

typedef boost::adjacency_list<
boost::vecS,
boost::vecS,
boost::undirectedS
> Graph;

struct Closure : public Callback {

optparse::OptionParser parser;

Graph graph;
Expand Down Expand Up @@ -70,7 +81,6 @@ struct Closure:public Callback
allAddrs.reserve(15 * 1000 * 1000);
info("Building address equivalence graph ...");
startTime = usecs();

return 0;
}

Expand Down
3 changes: 1 addition & 2 deletions cb/help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <option.h>
#include <callback.h>

struct Help:public Callback {
struct Help : public Callback {

optparse::OptionParser parser;

Expand Down Expand Up @@ -98,7 +98,6 @@ struct Help:public Callback {
Callback::showAllHelps(false);
}
}

return 0;
}
};
Expand Down
57 changes: 32 additions & 25 deletions cb/rewards.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include <string.h>
#include <callback.h>

struct Rewards:public Callback
{
struct Rewards : public Callback {

optparse::OptionParser parser;

bool fullDump;
Expand All @@ -19,8 +19,7 @@ struct Rewards:public Callback
uint64_t currBlock;
const uint8_t *currTXHash;

Rewards()
{
Rewards() {
parser
.usage("")
.version("")
Expand All @@ -42,8 +41,7 @@ struct Rewards:public Callback
virtual int init(
int argc,
const char *argv[]
)
{
) {
optparse::Values &values = parser.parse_args(argc, argv);
fullDump = values.get("full");

Expand All @@ -54,8 +52,7 @@ struct Rewards:public Callback
virtual void startBlock(
const Block *b,
uint64_t
)
{
) {
const uint8_t *p = b->chunk->getData();
SKIP(uint32_t, version, p);
SKIP(uint256_t, prevBlkHash, p);
Expand All @@ -68,29 +65,35 @@ struct Rewards:public Callback
virtual void startTX(
const uint8_t *p,
const uint8_t *hash
)
{
) {
currTXHash = hash;
}

virtual void startInputs(const uint8_t *p)
{
virtual void startInputs(
const uint8_t *p
) {
hasGenInput = false;
nbInputs = 0;
}

virtual void startInput(const uint8_t *p)
{
virtual void startInput(
const uint8_t *p
) {
static uint256_t gNullHash;
bool isGenInput = (0==memcmp(gNullHash.v, p, sizeof(gNullHash)));
if(isGenInput) hasGenInput = true;
if(isGenInput) {
hasGenInput = true;
}
++nbInputs;
}

virtual void endInputs(const uint8_t *p)
{
virtual void endInputs(
const uint8_t *p
) {
if(hasGenInput) {
if(1!=nbInputs) abort();
if(1!=nbInputs) {
abort();
}
}
}

Expand All @@ -101,9 +104,10 @@ struct Rewards:public Callback
uint64_t outputIndex,
const uint8_t *outputScript,
uint64_t outputScriptSize
)
{
if(!hasGenInput) return;
) {
if(!hasGenInput) {
return;
}

uint8_t addrType[3];
uint160_t pubKeyHash;
Expand All @@ -113,7 +117,9 @@ struct Rewards:public Callback
outputScriptSize,
addrType
);
if(unlikely(-2==type)) return;
if(unlikely(-2==type)) {
return;
}

if(unlikely(type<0) && 0!=value && fullDump) {
printf("============================\n");
Expand All @@ -127,7 +133,9 @@ struct Rewards:public Callback
}

reward += value;
if(!fullDump) return;
if(!fullDump) {
return;
}

printf("%7d ", (int)currBlock);
showHex(currTXHash);
Expand Down Expand Up @@ -170,8 +178,7 @@ struct Rewards:public Callback

virtual void endBlock(
const Block *b
)
{
) {
uint64_t baseReward = getBaseReward(currBlock);
int64_t feesEarned = reward - (int64_t)baseReward; // This sometimes goes <0 for some early, buggy blocks
printf(
Expand Down
Loading

0 comments on commit 42b247a

Please sign in to comment.