Pages:
Author

Topic: [ANN] cbitcoin 2.0 - A Bitcoin Library in C (Read 17203 times)

legendary
Activity: 1190
Merit: 1004
January 09, 2015, 02:55:38 PM
Hi Andrew,

I have worked on full validation on cbitcoin, though I decided to scrap it in favour of more primitive bitcoin functions only, because of the time and work involved in getting it fully functional and tested. cbitcoin however would give a set of tools to help with creating a fully validating node, and the CBNetworkCommunicator code goes some way to implementing a node (handshakes, pings, peer discovery etc.). For blockchain validation you could look at the old CBValidator code, though I removed it. The library still has things such as address encoding, BIP0032 HD Wallets, message structures inc. serialisation, basic validation functions (merkle tree functions, proof of work stuff etc.), script creation/execution, and transaction creation/signing.

However to create an alt-coin from it would be an unnecessary difficulty in my opinion, though it could make an interesting project for sure, and cbitcoin may be useful in projects, though I can't guarantee that it's 100% correct and bug free. I am personally using the HD wallet code in another project with some modifications. I also use the example programs quite a lot: https://github.com/MatthewLM/cbitcoin/tree/master/examples

Regards,

Matthew
member
Activity: 73
Merit: 10
Hi Matt,

Thanks for your work on this, I'm very impressed by the simplicity of it - Bitcoin code that is easy to follow, what a novelty !

I want to do some experimentation with a slightly different blockchain implementation i.e. yet another worthless altcoin and thought that cbitcoin might be a good starting point. Is there a rough example on how to use cbitcoin to create a basic full validating node as I've looked through all the classes and can't find a 'main' function or a basic entry point anywhere ? None of the examples seem to cover this basic case, unless I've missed it.

Thanks

Andrew
legendary
Activity: 1190
Merit: 1004
November 16, 2014, 07:39:06 PM
Can we push it into student's syllabus learning C ? If yes, how ? Any idea ?

There was one university project using cbitcoin. I'm not sure how much they would be happy for me to disclose about that though.

I changed cbitcoin significantly in a new commit. I removed a lot of incomplete code so that what is left is done to a higher degree of completeness. It has code for HD wallets, transaction construction, script execution/construction, network messages, bitcoin addresses, WIF addresses, general base58 encoding/decoding, basic network communication, proof of work validation and more.
legendary
Activity: 2380
Merit: 1209
The revolution will be digital
Can we push it into student's syllabus learning C ? If yes, how ? Any idea ?
legendary
Activity: 1190
Merit: 1004
cbitcoin is now using the MIT license.
legendary
Activity: 1190
Merit: 1004
You can now donate via Paypal or Charitycoin:

Paypal: Click here!

Charitycoin: CSU54ZAa4VuhiVwzgyAudePmn7eJigkKU5
legendary
Activity: 1190
Merit: 1004
Thanks very much linuxdoctor. I've added your fixes, except for the second one which is not included in my latest source code. There are several bugs I'm working on at once, I'll hopefully push them to github later or sometime soon.
newbie
Activity: 6
Merit: 0
I've been away doing other projects but spent a few hours playing with cbitcoin this evening and found a few more bugs and possible solutions. I've compiled some unified diffs. The diffs are between the current git repository  and a fork (named cbitcoin-2014-03-21).

1. Function CBNewBlockChainStorage() crashes when trying to free self->database which is never initialized at this point. At this point, the function is doing cleanup on an error but should just free self since everything before it has already been freed.
Code:
--- ./library/dependencies/storage/CBBlockChainStorage.c        2014-03-21 19:53:02.744321264 -0400
+++ ../cbitcoin-2014-03-21/./library/dependencies/storage/CBBlockChainStorage.c 2014-03-21 01:17:12.000000000 -0400
@@ -38,7 +38,7 @@
                CBFreeIndex(self->blockHashIndex);
        }
        CBLogError("Could not load one of the block chain storage indices.");
-       CBFreeDatabase(self->database);
+       free(self);
        return false;
 }
 void CBFreeBlockChainStorage(CBDepObject self){

2. Function CBOnUpToDate is never declared (nor do I know how to use it) nor is CBCallBacks callbacks initialized to point to it. This then  crashes cbitcoin example server whenever some routine tries to access callbacks->updatetodate. Simple fix.
Code:
--- ./client-server/src/main.c  2014-03-21 19:53:02.738321373 -0400
+++ ../cbitcoin-2014-03-21/./client-server/src/main.c   2014-03-21 18:36:45.608202593 -0400
@@ -66,6 +66,9 @@
 void CBOnTransactionUnconfirmed(CBNode * node, uint8_t * txHash){
        
 }
+void CBOnUpToDate(CBNode * node, bool truefalse){
+
+}
 CBNetworkAddress * CBReadNetworkAddress(char * ipStr, bool isPublic){
        CBSocketAddress saddr = {NULL, 8333};
        char * portStart;
@@ -286,7 +289,8 @@
                CBOnNewTransaction,
                CBOnTransactionConfirmed,
                CBOnDoubleSpend,
-               CBOnTransactionUnconfirmed
+               CBOnTransactionUnconfirmed,
+               CBOnUpToDate
        };
        CBNodeFull * node = CBNewNodeFull(database, nodeFlags, otherTxsSizeLimit, callbacks);
        if (!node) {

3. This is a tricky one. in CBEcdsaVerify() if o2i_ECPublicKey() fails it writes a NULL into key which EC_KEY then tries to free. Result: crash. This is a work around which creates two copies of the new EC_KEY. If key gets stomped on then we can free the copy in eckey. Interestingly, ECDSA_verify harmlessly passes over any NULL value in key (although I wouldn't count on any future version doing so).
Code:
--- ./library/dependencies/crypto/CBOpenSSLCrypto.c     2014-03-21 19:53:02.741321318 -0400
+++ ../cbitcoin-2014-03-21/./library/dependencies/crypto/CBOpenSSLCrypto.c      2014-03-21 19:44:44.181601752 -0400
@@ -84,9 +84,13 @@
        RIPEMD160(data, len, output);
 }
 bool CBEcdsaVerify(uint8_t * signature, uint8_t sigLen, uint8_t * hash, uint8_t * pubKey, uint8_t keyLen){
-       EC_KEY * key = EC_KEY_new_by_curve_name(NID_secp256k1);
+       EC_KEY *eckey, * key;
+       int res = 0;
+
+       eckey = key = EC_KEY_new_by_curve_name(NID_secp256k1);
        o2i_ECPublicKey(&key, (const unsigned char **)&pubKey, keyLen);
-       int res = ECDSA_verify(0, hash, 32, signature, sigLen, key);
-       EC_KEY_free(key);
+       if ( key != NULL )
+               res = ECDSA_verify(0, hash, 32, signature, sigLen, key);
+       EC_KEY_free(eckey);
        return res == 1;
 }
legendary
Activity: 1890
Merit: 1086
Ian Knowles - CIYAM Lead Developer
February 11, 2014, 10:36:25 AM
It becomes a race between running out of memory or running out of file descriptors.

Typically the # of file descriptors is quite small compared to available memory so I'd think the latter would be the main problem but still a memory leak nonetheless.

Glad to see this project is still going (has been a while since I'd followed this topic).
legendary
Activity: 1190
Merit: 1004
February 11, 2014, 10:31:51 AM
Thank you linuxdoctor. I've added a fix and it will be available in the next commit. If you want to know anything about the library make sure to get in touch.
newbie
Activity: 6
Merit: 0
February 10, 2014, 11:53:44 PM
cbitcoin now has a fully validating node implementation. There is still much work to do. There are a lot of memory leaks that have not been resolved yet for a start.

Yes, I've found one. This is from dependencies/random/CBRand.c:

Code:
bool CBGet32RandomBytes(uint8_t * bytes){
FILE * f = fopen("/dev/random", "r");
return fread(bytes, 1, 32, f) == 32;
}

I'm looking at the cbitcoin library for my own edification and possibly integrating it into something. I'm new to bitcoin in general and before I begin to use it (if I decide to use it) I want to know everything I can about it. Reading the code of various implementations of different parts of bitcoin (servers, miners, clients, etc.) has been an education.

Anyway, in looking at this file (quite by accident incidentally) I discovered that you did not close the file after reading from it. Which means every time you get some RandomBytes you will use a new file descriptor. This will leak a lot of memory and waste file descriptors to boot. It becomes a race between running out of memory or running out of file descriptors.
legendary
Activity: 1190
Merit: 1004
I've fixed the address generation example and also added a (basic) generator for all upper-case and digit addresses such as this one I just generated: 17BK2E43VFA57KE6X5GQUAH12ECE7WL227.

cbitcoin now supports HD keys (BIP0032), it can properly construct and sign transactions of the standard types, various other things have been improved and there is a client on the way.
legendary
Activity: 1190
Merit: 1004
December 11, 2013, 04:45:20 PM
Thanks. I've not actually tested it running on the bitcoin network yet so let's hope it goes smoothly!  Smiley
sr. member
Activity: 378
Merit: 325
hivewallet.com
December 11, 2013, 04:39:40 PM
cbitcoin now has a fully validating node implementation. There is still much work to do. There are a lot of memory leaks that have not been resolved yet for a start.

Once again, thanks to people who have donated. The donations are now worth a good amount of money. I'm still open for more bitcoin donations (1D5A1q5d192j5gYuWiP3CSE5fcaaZxe6E9), and I would be happy to receive any offers for code contribution.

I haven't implemented a headers-only node as of yet. Before I do that I will like to test cbitcoin over the real bitcoin network by implementing a basic client. Now that should not be too difficult (I hope) since I can extend an interface on-top of the node software.

Wow Matthew, congratulations! That's a massive achievement!
legendary
Activity: 1190
Merit: 1004
Thanks. The library may be useful for embedded devices, though there will need to be at least a partial implementation of the C standard library, and to use all of the features you will to implement things like the cryptography, threading and network functions. At the moment cbitcoin has a core library which is in C99 only, and it has separated libraries which use POSIX libraries, libevent and OpenSSL. These separated libraries can be replaced by alternative code or ignored if they are not needed. So for instance if you want to use cbitcoin but don't want networking, the networking functions do not need to be implemented and thus there is no need to link to libevent.
sr. member
Activity: 378
Merit: 325
hivewallet.com
Wow, nice work. This could be great for embedded devices!
rlh
hero member
Activity: 804
Merit: 1004
September 04, 2013, 11:46:20 AM
I have this in my Watchlist, but somehow I've missed the additional discussion.  Thanks for the update.  I'll be keeping my eye on this project once it near, fully implemented.
legendary
Activity: 1190
Merit: 1004
Hi, the license for the most up-to-date code is the GPLv3 with additional permissions and can be found here: http://cbitcoin.com/license.html and here: https://github.com/MatthewLM/cbitcoin/blob/accountingAndNodes/LICENSE

I'll make sure to add it to the OP.
legendary
Activity: 1456
Merit: 1081
I may write code in exchange for bitcoins.
Thanks for this, I may just try it out!  I didn't see what kind of licensing in your OP?  Is it GPL?
legendary
Activity: 1190
Merit: 1004
Hi. I was busy for quite a few months earlier in the year, but I'm now able to put more work towards this.

The most up-to-date branch is this one: https://github.com/MatthewLM/cbitcoin/tree/accountingAndNodes

I've finished the accounting code now which keeps track of balances, transactions and unspent outputs for "accounts". These accounts are calculated with a list of watched outputs, so you can watch bitcoin addresses for wallets or whatever, and store the account information. It was designed so that you could have multiple accounts.

Now I'm working on finishing off the node code (both full validation and SPV). Progress has not been so bad for that. I'm currently working on producing features for the protocol version 60000, but other things such as the bloom filtering can come later.
Pages:
Jump to: