Bitcoin mining in C doing one example very basic

Hi i was researching more about some bitcoin mining to learn more about how works. But at the moment i’m not sure if i am the right way so i share the code working that already i have. Code:

#ifdef fail     #!/bin/bash     # NOTE you can chmod 0755 this file and then execute it to compile (or just copy and paste)     gcc -o hashblock hashblock.c -lssl     exit 0 #endif  #include <openssl/sha.h> #include <stdio.h> #include <string.h> #include <stdlib.h>  // this is the block header, it is 80 bytes long (steal this code) typedef struct block_header {     unsigned int    version;     // dont let the "char" fool you, this is binary data not the human readable version     unsigned char   prev_block[32];     unsigned char   merkle_root[32];     unsigned int    timestamp;     unsigned int    bits;     unsigned int    nonce; } block_header;   // we need a helper function to convert hex to binary, this function is unsafe and slow, but very readable (write something better) void hex2bin(unsigned char* dest, const char* src) {     unsigned char bin;     int c, pos;     char buf[3];      pos=0;     c=0;     buf[2] = 0;     while(c < strlen(src))     {         // read in 2 characaters at a time         buf[0] = src[c++];         buf[1] = src[c++];         // convert them to a interger and recast to a char (uint8)         dest[pos++] = (unsigned char)strtol(buf, NULL, 16);     }  }  // this function is mostly useless in a real implementation, were only using it for demonstration purposes void hexdump(unsigned char* data, int len) {     int c;      c=0;     while(c < len)     {         printf("%.2x", data[c++]);     }     printf("\n"); }  // this function swaps the byte ordering of binary data, this code is slow and bloated (write your own) void byte_swap(unsigned char* data, int len) {     int c;     unsigned char tmp[len];      c=0;     while(c<len)     {         tmp[c] = data[len-(c+1)];         c++;     }      c=0;     while(c<len)     {         data[c] = tmp[c];         c++;     } }  int main() {     // start with a block header struct     block_header header;      // we need a place to store the checksums     unsigned char hash1[SHA256_DIGEST_LENGTH];     unsigned char hash2[SHA256_DIGEST_LENGTH];      // you should be able to reuse these, but openssl sha256 is slow, so your probbally not going to implement this anyway     SHA256_CTX sha256_pass1, sha256_pass2;       // we are going to supply the block header with the values from the generation block 0     header.version =    1;     hex2bin(header.prev_block,      "0000000000000000000000000000000000000000000000000000000000000000");     hex2bin(header.merkle_root,     "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b");     header.timestamp =  1231006505;     header.bits =       486604799;     header.nonce =      2083236893;      // the endianess of the checksums needs to be little, this swaps them form the big endian format you normally see in block explorer     byte_swap(header.prev_block, 32);     byte_swap(header.merkle_root, 32);      // dump out some debug data to the terminal     printf("sizeof(block_header) = %d\n", sizeof(block_header));     printf("Block header (in human readable hexadecimal representation): ");     hexdump((unsigned char*)&header, sizeof(block_header));      // Use SSL's sha256 functions, it needs to be initialized     SHA256_Init(&sha256_pass1);     // then you 'can' feed data to it in chuncks, but here were just making one pass cause the data is so small     SHA256_Update(&sha256_pass1, (unsigned char*)&header, sizeof(block_header));     // this ends the sha256 session and writes the checksum to hash1     SHA256_Final(hash1, &sha256_pass1);      // to display this, we want to swap the byte order to big endian     byte_swap(hash1, SHA256_DIGEST_LENGTH);     printf("Useless First Pass Checksum: ");     hexdump(hash1, SHA256_DIGEST_LENGTH);      // but to calculate the checksum again, we need it in little endian, so swap it back     byte_swap(hash1, SHA256_DIGEST_LENGTH);      //same as above     SHA256_Init(&sha256_pass2);     SHA256_Update(&sha256_pass2, hash1, SHA256_DIGEST_LENGTH);     SHA256_Final(hash2, &sha256_pass2);      byte_swap(hash2, SHA256_DIGEST_LENGTH);     printf("Target Second Pass Checksum: ");     hexdump(hash2, SHA256_DIGEST_LENGTH);      return 0; } 

My question is all is right. I’m complying all steps. Some idea or some suggestion good received. thx . It’s to do one example in c more useful using the normal structure of one bitcoin mining including sha256 encryption and big endian format.

Recent Questions – Bitcoin Stack Exchange

Indonesian Journalist: Follow Japan’s Example and Support Bitcoin

Indonesia should follow Japan and support bitcoin, according to an article in okezone.com, an online portal of news and entertainment that focuses on Indonesian readers both in the country and living abroad. The article, by journalist Kurniasih Miftakhul Jannah, calls for the country to take action to make bitcoin more acceptable in his country, in […]

The post Indonesian Journalist: Follow Japan’s Example and Support Bitcoin appeared first on CCN: Financial Bitcoin & Cryptocurrency News.

News – CCN: Financial Bitcoin & Cryptocurrency News

BigchainDB: a prime example of blockchain bullshit

Intro

BigchainDB is "a scalable blockchain database" (which falls under the category of so-called "private" or "permissioned" "blockchains", I believe) which was released in February 2016. It generated a lot of hype due to impressive scalability benchmarks: it was able to process a million transactions per second.

BigchainDB got a lot of praise from private blockchain enthusiasts. It entered many partnerships, e.g. with Eris Industries, Everledger, Capgemini, etc.

But BigchainDB's architecture is fundamentally flawed. All BigchainDB nodes, by design, connect to a single RethinkDB cluster. (RethinkDB is a distributed NoSQL database, kinda like MongoDB but better.) If something bad happens to that RethinkDB cluster, the whole blockchains goes down.

E.g. suppose one node is hijacked by an attacker and executes "dropTable" on the RethinkDB cluster it is connected to. All other nodes will go down because they don't have an independent storage.

I think it's a beautiful illustration to Andreas Antonopolous's argument that private blockchains will end up less secure:

In the end, it also lags behind in terms of security because it’s not exposed to the kind of robust peer review that an open Internet-based system has to have to survive. While bitcoin is getting stronger and stronger with security, these things will actually wither.

The idea being that intranets end up being these really insecure places where you’re running Outlook and FrontPage and old versions of Apache that haven’t been patched. Whereas on the Internet, if you’re Facebook, if you’re Google, if you’re Apple and you’re operating Internet applications, you have to be robust. You have to respond to vulnerability. You have to make systems that are antifragile, resilient to attack and they’re constantly evolving and they’ve become very robust.

I used to think that this argument is weird, but we've got a confirmation: a system which is fundamentally flawed gets all the praises and people start building apps on top of it before it is thoroughly analyzed.

Proof

You might think I'm just making this up or exaggerating. It's hard to believe that all 9 people working on BigchainDB (that's how many co-authors its whitepaper has) don't see a problem with using a single shared RethinkDB cluster. I though that maybe I'm missing something, so I asked this question in their gitter:

killerstorm @killerstorm May 01 23:03 Hey people, do you I understand it correctly that all nodes are connected to a single rethinkdb cluster? what if one of nodes will execute r.dropDb(…)?

Rodolphe Marques @r-marques May 02 12:56 @killerstorm yes all federation nodes are connected to a single rethinkdb cluster. We are working on a consensus protocol that requires a majority of the nodes to agree in any change done in the database (voting of blocks is part of that consensus protocol). For data deletion we are listening to changefeeds on the data and revert any change that should not happen. Regarding more admin tasks like table and db drops we are trying to leverage rethinkdb permissions and replication to make sure no single node is able to just drop the database (still work in progress).

killerstorm @killerstorm May 02 15:39 Sorry, I don't see how changefeed can protect against data loss. Where would data come from after it's deleted? Is there a backup?

Rodolphe Marques @r-marques May 02 15:41 backups are a planned feature. Changefeeds can be used to restore data when documents are deleted. When documents are deleted the changedfeed will return the documents that were deleted Although changefeeds are not enough to prevent dataloss in case a node uses db drop.

So they confirmed that a single db drop can destroy this "blockchain database".

But they say that some time in future they will start working on securing their system. A day after I asked a question about dropDb, they added an issue: How to lock down the RethinkDB "admin" account?:

@killerstorm pointed out that anyone with admin access to RethinkDB could just drop the database. What can be done to prevent that and similar things (e.g. dropping tables)?

So, basically, 8 months after they started the project and 3 months after they released it publicly they became aware that this problem exists. Wow, just wow.

The mindset

Obviously, this isn't the only potential attack vector. It's just the simplest thing which came to my mind. There are probably thousands of serious attack vectors. The fundamental issue is that RethinkDB is not Byzantine fault tolerant, it is not designed as such, it's just an ordinary database. And so if you implement a "blockchain" as a thin layer on top of RethinkDB you get it exposed to potential attackers.

It's hard to imagine that none of people working on BigchainDB weren't aware of the problem, that would imply that all 9 people working on it are pants-on-head stupid.

No, they know that BigchainDB isn't "Byzantine fault tolerant", it is mentioned in the white paper.

They just don't see a problem with it. It is a startup mindset: make an MVP, "fake it till you make it", generate buzz… Security is literally their last priority. They believe that they will add security after they get a lot of money from partners, investors, etc. And, meanwhile, it's just a prototype, bro.

The problem with it is that it's highly misleading. Here's what BigchainDB paper says about security:

Byzantine faults: In order to operate in a trustless network, BigchainDB incorporates measures against malicious or unpredictable behavior of nodes in the system. These include mechanisms for voting upon transaction and block validation. Efforts to achieve full Byzantine tolerance are on the roadmap and will be tested with regular security audits.

It incorporates measures, you see. Mechanisms. Business people reading this will understand that it's "mostly secure".

While in reality it's like not secure at all. It can be destroyed with a single line of code.

The mechanisms they are talking about are hashes and signatures. It's similar to what Bitcoin does, but by themselves hashes and signatures do not make things secure. You need to use proper architecture and protocols. After all, hashes and public key cryptography existed for almost 50 years, but we've got Bitcoin only 7 years ago.

An apt analogy for BigchainDB would be a cargo cult of a blockchain tech: it has hashes like in a real one, it has blocks, etc. There is superficial similarity. But it doesn't "fly" like a real one, because superficial similarity doesn't define object properties.

And BTW that 1 M tps benchmark they didn't isn't impressive: they were, basically, testing RethinkDB IO speed (as they say themselves). Yep, sure, RethinkDB is great. BigchainDB, on the other hand…

The lack of focus on security is understandable when if you take into account that there are no serious uses for "private blockchains" yet. People are just experimenting with stuff, doing proof-of-concepts and whatnot. Everybody understands that you aren't going to have real adversaries when you run an internal test. So these make-believe blockchains are OK.

Maybe in future this stuff will mature app. BigchainDB might become secure if RethinkDB becomes Byzantine fault tolerant, for example. But, personally, I'm rather disappointed with the "security last" approach and deceptive marketing.

Disclaimer

I work in a company which might be, potentially, a BigchainDB competitor. This isn't a coincidence: obviously, "blockchain tech" people review blockchain tech software.

So this post might be seen as "shitting on your competitor" kind of a post, I understand that. But I'm writing this on my own, in my spare time. And when I mentioned BigchainDB to my colleagues, they recommended against writing any articles.

If you dig through my comment history, you can see that I've been critical to many things: Mastercoin, alt-coins, 21 inc Bitcoin computer, sidechains and so on. This is just what I do.

submitted by /u/killerstorm
[link] [comments]
Bitcoin