Skip to content

Instantly share code, notes, and snippets.

@tuttlem
Created August 15, 2017 13:25
Show Gist options
  • Save tuttlem/c1d2611ec35f6228fd41a7091111b60d to your computer and use it in GitHub Desktop.
Save tuttlem/c1d2611ec35f6228fd41a7091111b60d to your computer and use it in GitHub Desktop.
Blockchain example
#include <chrono>
#include <sstream>
#include <string>
#include <memory>
#include <iostream>
#include <cstdlib>
#include "picosha2.h"
class block {
public:
block(const long int ts, const std::string &data, const std::string &prev_hash)
: _ts(ts), _data(data), _prev_hash(prev_hash) { }
std::string hash(void) const {
std::stringstream ss;
ss << _ts
<< _data
<< _prev_hash;
std::string src = ss.str();
std::vector<unsigned char> hash(32);
picosha2::hash256(src.begin(), src.end(), hash.begin(), hash.end());
return picosha2::bytes_to_hex_string(hash.begin(), hash.end());
}
static block create_seed(void) {
auto temp_ts = std::chrono::system_clock::now().time_since_epoch();
return block(
temp_ts.count(),
"Seed block",
""
);
}
static block create_next(const block &b, const std::string &data) {
auto temp_ts = std::chrono::system_clock::now().time_since_epoch();
return block(
temp_ts.count(),
data,
b.hash()
);
}
public:
const long ts() const { return _ts; }
const std::string& data() const { return _data; }
const std::string& prev_hash() const { return _prev_hash; }
private:
long _ts;
std::string _data;
std::string _prev_hash;
};
void print_chain(std::vector<block> chain) {
int i = 0;
for (block& b : chain) {
std::cout <<
"index: " << i << std::endl <<
"ts: " << b.ts() << std::endl <<
"data: " << b.data() << std::endl <<
"this: " << b.hash() << std::endl <<
"prev: " << b.prev_hash() << std::endl <<
"-------------------------------------" << std::endl;
i ++;
}
}
std::string make_data() {
std::stringstream ss;
int a = std::rand();
int b = std::rand();
int c = std::rand();
ss <<
"{ \"a\": " << a << "," <<
"\"b\": " << b << "," <<
"\"c\": " << c << " }";
return ss.str();
}
int main(int argc, char *argv[]) {
std::vector<block> chain = {
block::create_seed()
};
for (int i = 0; i < 5; i ++) {
// get the last block in the chain
auto last = chain[chain.size() - 1];
// create the next block
chain.push_back(block::create_next(last, make_data()));
}
print_chain(chain);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment