Bitcoin Forum
March 19, 2024, 06:35:27 AM *
News: Latest Bitcoin Core release: 26.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1] 2 »  All
  Print  
Author Topic: Proposal for a C bitcoin library.  (Read 8357 times)
MatthewLM (OP)
Legendary
*
Offline Offline

Activity: 1190
Merit: 1004


View Profile
June 09, 2011, 02:50:21 PM
 #1

Hello.  Smiley

I was talking about bitcoin on the development irc channel last night. I was asking about various things relating to making a bitcoin client for mobile devices. People on the channel seemed to support my thoughts that the bitcoin source code needs to be improved a lot.

I like bitcoin and I would like it to spread. For it to be spread, it needs to be easily accessible for programmers to use and apply it to various devices. I have a proposal for a new way of organising the bitcoin software using a C library.

The current bitcoin source code contains the code for the entire bitcoin client. The back-end needs to be completely separated from the UI and platform specific parts. Using a library which programmers can include in their projects and easily attach to their UI code, makes much more sense.

Bitcoin requires platform specific code. I'm not fully aware of what it requires exactly. Obviously, it needs access to the internet through irc sockets.

I suggest the library does not implement platform specific code and instead requires the user to implement functions. The user can send function pointers to bitcoin which do some of the platform specific work. For example, bitcoin may need to send a message though an IRC socket. The programmer can implement a function which takes an ID argument and a data argument and sends it over the socket (defined by the ID allowing multiple socket objects, if needed) using the platform specific APIs. This way bitcoin is as portable as possible without anyone needing to modify the library, only understand how to use it. Function pointers are a sensible way to do this. Function pointers could also be used for event handling (Events for receiving bit coins etc.).

Bitcoin is currently written in C++. I think this is a poor choice. C is more popular than C++ according to surveys (http://langpop.com/) and is generally more portable. C++ programmers can easily use it and programmers using other C based languages like Objective-C and C# can also integrate C. C can easily be accessed by other languages on many platforms. While C++ is also widely portable, C is a better alternative offering ultimate portability. C is widely used on many processor architectures. If bitcoin was ever to make it's way into a variety of devices and products, C is the best way to go.

A C++ wrapper would be lovely but the fundamental library should be in C. The library should be done in standard C of-course, only using libraries that are portable with standard c.

To conclude my proposal is to separate the back-end code for bit-coin into a C library which can be used by programmers to easily create bitcoin applications, removing all UI and platform specific code from the core library.

What do people think about this?
Bitcoin addresses contain a checksum, so it is very unlikely that mistyping an address will cause you to lose money.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1710830127
Hero Member
*
Offline Offline

Posts: 1710830127

View Profile Personal Message (Offline)

Ignore
1710830127
Reply with quote  #2

1710830127
Report to moderator
1710830127
Hero Member
*
Offline Offline

Posts: 1710830127

View Profile Personal Message (Offline)

Ignore
1710830127
Reply with quote  #2

1710830127
Report to moderator
1710830127
Hero Member
*
Offline Offline

Posts: 1710830127

View Profile Personal Message (Offline)

Ignore
1710830127
Reply with quote  #2

1710830127
Report to moderator
arturh
Jr. Member
*
Offline Offline

Activity: 59
Merit: 10



View Profile
June 09, 2011, 02:55:19 PM
 #2

Now that mining is a miner's thing, I think that what we need is an official Haskell / ocaml / scala library and client. Something where checking what's going on is a bit easier. Also this way we wouldn't have to worry about a worm wiping clean the bitcoin network through a stupid buffer overflow. I only run the client when necessary because of this.

C bindings can be created from these.
MatthewLM (OP)
Legendary
*
Offline Offline

Activity: 1190
Merit: 1004


View Profile
June 09, 2011, 03:30:30 PM
 #3

You still need to compile or interpret the code on different platforms. C is widely accepted among platforms. In the future there may even be bitcoin software on microcontrollers where people can make bitcoin payments with card readers where their wallet information is encrypted on a card (You wouldn't store all the money on a card in case you lost it of-course and this is just an example).

Also, I was looking into making an objective-c implementation of bitcoin but I can't do that with the current state of the software. If there was a c library, I could make objective-c classes for bitcoin and use it to make Mac and iOS bitcoin clients with Apple's APIs.

I'm not clever enough to convert the bitcoin code and don't use C++. I wouldn't be mentally equipped to make a c library, only propose it as an idea.
arturh
Jr. Member
*
Offline Offline

Activity: 59
Merit: 10



View Profile
June 09, 2011, 03:42:19 PM
 #4

I fail to see why you would want to embed the library. However Ocaml and Haskell can also be compiled to whatever architucture you need, and you can't beat JCM for portability.

I'm just saying it would help trust the official client if it used a statically typed, funcitonal programming language. A more "mathematical" one, if you  like.  For me, in the official client at least, it should be about using the safest technologies.
MatthewLM (OP)
Legendary
*
Offline Offline

Activity: 1190
Merit: 1004


View Profile
June 09, 2011, 03:56:08 PM
 #5

Quote
However Ocaml and Haskell can also be compiled to whatever architucture you need

More so than C? And there's no reason why you can't make safe code in C. You could (and perhaps should) have multiple implementations of bit coin. I suggest having a C implementation would be beneficial, especially more so than C++.

Why would you want to embed the library? So you can easily spread bitcoin over many devices and encourage innovation in the use of bitcoin.
arturh
Jr. Member
*
Offline Offline

Activity: 59
Merit: 10



View Profile
June 09, 2011, 04:11:02 PM
 #6

What I don't see is the micrcontroller thing.

Safe code is easier to write in some languages than others. C's original purpose was to write programming languages. I agree that multiple implementations is good but I'm talking about the "official" one, the "reference" implementation against which others must be tested, the executable documentation.
MatthewLM (OP)
Legendary
*
Offline Offline

Activity: 1190
Merit: 1004


View Profile
June 09, 2011, 04:14:54 PM
 #7

Quote
C's original purpose was to write programming languages

I thought the purpose was to use it for the unix operating system and find a better way to program operating systems in general.

Quote
What I don't see is the micrcontroller thing.

That was an example. If bitcoin would ever become a properly used currency it needs to be more than a thing you use on your desktop computer.

It's hard to even make clients for mobile devices at the moment. That's what led me here, I wanted to make an iOS client for bitcoin but I can't when it's like how it is today.
arturh
Jr. Member
*
Offline Offline

Activity: 59
Merit: 10



View Profile
June 09, 2011, 04:33:59 PM
 #8

Sorry, I meant a language to write operating systems.

I'm talking about a reference implementation, so I think there's no conflict between us.
MatthewLM (OP)
Legendary
*
Offline Offline

Activity: 1190
Merit: 1004


View Profile
June 09, 2011, 04:40:02 PM
 #9

Your idea to use Haskell or ocaml for a reference implementation is probably a good idea.  Wink

But I would still like to see a c library very much indeed. So much could be done with a simple c library.
bluecmd
Newbie
*
Offline Offline

Activity: 14
Merit: 0


View Profile
June 09, 2011, 04:46:21 PM
 #10

I would be interested in developing such a library. I have my pet-project https://github.com/blueCommand/libbitcoin which was supposed to be something like that but as can be seen, it is not that far gone :-)

If we can gather a couple of people to write such a library with a common goal for a lightweight libbitcoin that would be awesome.
mrenouf
Newbie
*
Offline Offline

Activity: 17
Merit: 0


View Profile
June 09, 2011, 05:18:20 PM
 #11

For what it's worth, I'm actively porting the BitCoin source to Java.

I plan to produce an Android BitCoin app, but this will also make it easy to develop additional webapps and other services. I know this doesn't help the iOS people, sorry.

On the other hand, a C or C++ client library could be used on Android via NDK (native code support), but I'd prefer to port the existing source for:

a) robustness, alternate implementation
b) to learn how it works
c) clean it up and help add new features

The existing C++ code is terrible for learning and understanding what's happening. Hopefully once I'm done with the Java port it could be ported back to C++ or even over to Obj-C without too much difficulty.
MatthewLM (OP)
Legendary
*
Offline Offline

Activity: 1190
Merit: 1004


View Profile
June 09, 2011, 05:39:04 PM
 #12

@bluecmd: Very nice. I took a look at what has been done. The one conflict with my idea is the platform incompatibilities. You have so far been implementing the network side of bitcoin which is platform-specific.

I do think it would be best to create a core library which is standard c and then allow the programmer using the core library to add platform-specific code with delegate functions. There could be UNIX, Windows, whatever libraries built on top of the core library. A layered approach would increase the portability of the underlying layers.

From what I can tell, the main things which standard c can't provide and that bit coin need are threads and sockets. Am I right? It can't be too difficult for the user of the core library to add delegate functions which provide basic interface to threads and sockets.

Unfortunately the help I can give is limited since I do not understand bitcoin technically and doubt I ever will. Being good with C++ would help me understand the current source but I'm not good with C++. I may be able to aid in certain areas.

@mrenouf: A Java implementation would be good. Of-course a C library could still be used on Android with the NDK and the machine code should handle the performance intensive parts of bitcoin better. But a Java implementation in general would be great for many people I'm sure. Not for my iOS apps since I use Objective-C for that. Please don't port it back to C++ though!  Smiley

I will be glad to make Objective-C classes based on a C library if one was made, as I said. Then I can easily make bitcoin applications for Mac and iOS.
blap
Newbie
*
Offline Offline

Activity: 51
Merit: 0


View Profile
June 09, 2011, 06:24:56 PM
 #13

just walking in the topic...
 Grin
Mike Hearn
Legendary
*
expert
Offline Offline

Activity: 1526
Merit: 1078


View Profile
June 09, 2011, 06:37:04 PM
 #14

Reimplementing Bitcoin in C or Objective-C isn't necessary for an iOS version. You could reuse the existing code with some pretty hefty modifications (eg, to not store the chain headers in RAM).

However this is a fairly pointless project right now because Apple have rejected Bitcoin in the past. It'd be a large amount of work to build a nicely running mobile client and there's a high risk of arbitrary rejection. The efforts are better spent elsewhere.

Proposals are, I'm afraid, fairly cheap when it comes to Bitcoin. What matters is working code. Don't underestimate the size of the task. Bitcoin doesn't look like a large codebase, but it's larger than you think due to the way the code is laid out and the code is extremely complex.

There's not much point in making a direct transcript into another language, it'd have to work in some reasonably different way to be useful, so you need a fluent understanding of Bitcoin. Unfortunately it isn't really possible to get fluency today unless you already know C++. The documentation is much better than it used to be, but some details are still non-obvious without reading the code.

You might want to discuss concrete implementations in the "alternative clients" section.
MatthewLM (OP)
Legendary
*
Offline Offline

Activity: 1190
Merit: 1004


View Profile
June 09, 2011, 07:08:57 PM
 #15

I don't use Objective-C++, I hate the existing code and wouldn't know where to start implementing an alternative UI with the tangle of the current code.

A C library would do wonders for helping programmers make use of it.
arturh
Jr. Member
*
Offline Offline

Activity: 59
Merit: 10



View Profile
June 10, 2011, 02:21:49 PM
 #16

I still don't see the use case for the embedded C library, but I'd recommend that you start with a list of platforms you are interested in running it on (and willing to build and test against) then implement, instead of "writing it in stadard C".
MatthewLM (OP)
Legendary
*
Offline Offline

Activity: 1190
Merit: 1004


View Profile
June 10, 2011, 02:46:28 PM
 #17

As I said, I personally would be willing to implement it in objective-C. Objective-C is based on C and you can use C99, ANSI etc. with it perfectly.

Can't do that as the source code is now.

My point is that a standard C library would make bitcoin more portable for programming. It would make implementing bit coin on more platforms easier.
wumpus
Hero Member
*****
qt
Offline Offline

Activity: 812
Merit: 1022

No Maps for These Territories


View Profile
June 10, 2011, 03:03:46 PM
Last edit: June 10, 2011, 09:11:02 PM by John Smith
 #18

Don't most platforms that support C also support C++ these days?

Then again, feel free to make another client implementation.

Edit: maybe it would be an idea to write the "business logic"/rules of bitcoin in a very high-level language, that can be interpreted in or converted to  any language you want. This would make it easier to implement bitcoin in other languages, and at the same time make it possible to have a common spec.

Edit2: Another advantage of this would be that you'd be able to support multiple different chains with different rules, switching to new chains potentially without recompiling.

Bitcoin Core developer [PGP] Warning: For most, coin loss is a larger risk than coin theft. A disk can die any time. Regularly back up your wallet through FileBackup Wallet to an external storage or the (encrypted!) cloud. Use a separate offline wallet for storing larger amounts.
MatthewLM (OP)
Legendary
*
Offline Offline

Activity: 1190
Merit: 1004


View Profile
June 10, 2011, 06:32:42 PM
 #19

I'm sure bitcoin is complex enough for someone to write a book on it.  Cheesy

Lots of platforms and architectures support C++ but not as much with microcontrollers. And it's harder for C programmers to make use of C++ code. C is one of the most popular languages. C++ programmers can easily take C code, doesn't work the other way around.

Once again, I would love to make a C library but I don't like C++ and there isn't a specification for bitcoin I could use without reading the source code, is there?

Also, I think I'm out of my comfort zone with bit coin.
PLATO
Sr. Member
****
Offline Offline

Activity: 493
Merit: 250


Don't trust "BBOD The Best Futures Exchange"


View Profile
June 10, 2011, 06:35:35 PM
 #20

For anyone who isn't aware of the Java bitcoin libraries:
http://code.google.com/p/bitcoinj/updates/list

All posts by me after 2012 were a compromised account. Probably by "BBOD The Best Futures Exchange". SORRY Y'ALL
Pages: [1] 2 »  All
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!