Skip to content

Commit

Permalink
Merge pull request #2 from MatthewLM/master
Browse files Browse the repository at this point in the history
First alpha release to alpha branch
  • Loading branch information
MatthewLM committed Sep 13, 2012
2 parents 708d927 + ec3ea26 commit 3e8ec2e
Show file tree
Hide file tree
Showing 72 changed files with 959 additions and 944 deletions.
25 changes: 18 additions & 7 deletions BUILD.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,19 @@ def compile(flags,output,source,clean):
# Link object files into dynamic library
print "LINKING " + targetPath
subprocess.check_call("gcc " + lflags + " -o " + targetPath + objects, shell=True)
# OpenSSL information
opensslFlags = " -lssl -lcrypto -L" + OPENSSL_LOCATION + "lib/ " + ADDITIONAL_OPENSSL_LFLAGS
opensslCFlags = " -I" + OPENSSL_LOCATION + "include/"
opensslReq = ["testCBAddress", "testCBAddressManager", "testCBAlert", "testCBTransaction", "testCBBase58", "testCBBlock", "testCBNetworkCommunicator", "testCBNetworkCommunicatorLibEv","testCBScript","testValidation"]
opensslOutput = os.path.join(objPath,"CBOpenSSLCrypto.o")
opensslSource = os.path.join(currentDir,"dependencies", "crypto", "CBOpenSSLCrypto.c")
# Build and run tests if specified
if test:
# Script tests
shutil.copyfile(os.path.join(testPath,"scriptCases.txt"),os.path.join(binPath,"scriptCases.txt"))
os.chdir(binPath)
# OpenSSL dependency
opensslFlags = " -lssl -lcrypto -L" + OPENSSL_LOCATION + "lib/ " + ADDITIONAL_OPENSSL_LFLAGS
opensslCFlags = " -I" + OPENSSL_LOCATION + "include/"
opensslReq = ["testCBAddress", "testCBAddressManager", "testCBAlert", "testCBTransaction", "testCBBase58", "testCBBlock", "testCBNetworkCommunicator", "testCBNetworkCommunicatorLibEv","testCBScript","testValidation"]
source = os.path.join(currentDir,"dependencies", "crypto", "CBOpenSSLCrypto.c")
opensslOutput = os.path.join(objPath,"CBOpenSSLCrypto.o")
compile(cflags + opensslCFlags,opensslOutput,source,clean)
compile(cflags + opensslCFlags,opensslOutput,opensslSource,clean)
# PRNG dependency
randReq = ["testCBAddressManager", "testCBNetworkCommunicator", "testCBNetworkCommunicatorLibEv"]
source = os.path.join(currentDir,"dependencies","random","CBRand.c")
Expand Down Expand Up @@ -193,4 +194,14 @@ def compile(flags,output,source,clean):
subprocess.check_call(targetPath,shell=True)
# Build example if specified
if sys.argv[-1] == "addressGenerator":
subprocess.check_call("gcc " + execflags + " -o " + os.path.join(binPath,"addressGenerator") + " " + os.path.join(examplesPath,"addressGenerator.c"), shell=True)
compile(cflags + opensslCFlags,opensslOutput,opensslSource,clean)
output = os.path.join(objPath,"addressGenerator.o")
compile(cflags + opensslCFlags,output,os.path.join(examplesPath,"addressGenerator.c"),clean)
targetPath = os.path.join(binPath,"addressGenerator")
print "LINKING " + targetPath
linkerFlags = " -L" + binPath + " -lcbitcoin"
if platform.system() == 'Linux':
linkerFlags += " -Wl,-rpath," + binPath
linkerFlags += ":" + OPENSSL_LOCATION + "lib/"
linkerFlags += opensslFlags
subprocess.check_call("gcc " + output + " " + opensslOutput + linkerFlags + " -o " + targetPath, shell=True)
4 changes: 2 additions & 2 deletions Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ PROJECT_LOGO =
# If a relative path is entered, it will be relative to the location
# where doxygen was started. If left blank the current directory will be used.

OUTPUT_DIRECTORY = "./documentation"
OUTPUT_DIRECTORY = ./documentation

# If the CREATE_SUBDIRS tag is set to YES, then doxygen will create
# 4096 sub-directories (in 2 levels) under the output directory of each output
Expand Down Expand Up @@ -671,7 +671,7 @@ WARN_LOGFILE =
# directories like "/usr/src/myproject". Separate the files or directories
# with spaces.

INPUT = "/Users/matt/Programming/BitEagle Projects/cbitcoin/cbitcoin/src"
INPUT = src

# This tag can be used to specify the character encoding of the source files
# that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is
Expand Down
4 changes: 2 additions & 2 deletions dependencies/sockets/CBLibEventSockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

#include "CBDependencies.h" // cbitcoin dependencies to implement
#include <pthread.h> // POSIX threads
#include <event2/event.h> // libevent events
#include <event2/event.h> // libevent onErrorReceived
#include <string.h>
#include <unistd.h>
#include <errno.h>
Expand Down Expand Up @@ -85,7 +85,7 @@ union CBOnEvent{
};

typedef struct{
CBEventLoop * loop; /**< For getting timeout events */
CBEventLoop * loop; /**< For getting timeout onErrorReceived */
struct event * event; /**< libevent event. */
union CBOnEvent onEvent;
void * peer;
Expand Down
4 changes: 1 addition & 3 deletions examples/addressGenerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ void getLine(char * ptr) {
}

int main(){
CBEvents events;
events.onErrorReceived = err;
printf("OpenSSL version: %s\n",OPENSSL_VERSION_TEXT);
printf("Enter the number of keys: ");
fflush(stdout);
Expand Down Expand Up @@ -95,7 +93,7 @@ int main(){
}
SHA256(pubKey, pubSize, shaHash);
RIPEMD160(shaHash, 32, ripemdHash);
CBAddress * address = CBNewAddressFromRIPEMD160Hash(ripemdHash, CB_PRODUCTION_NETWORK_BYTE, false, &events);
CBAddress * address = CBNewAddressFromRIPEMD160Hash(ripemdHash, CB_PRODUCTION_NETWORK_BYTE, false, err);
CBByteArray * string = CBVersionChecksumBytesGetString(CBGetVersionChecksumBytes(address));
CBReleaseObject(address);
bool match = true;
Expand Down
32 changes: 16 additions & 16 deletions generators/message_maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@
@brief Creates a new CB"+name+" object.\n\
@returns A new CB"+name+" object.\n\
*/\n\
CB"+name+" * CBNew"+name+"(CBEvents * events);\n\
CB"+name+" * CBNew"+name+"(void (*onErrorReceived)(CBError error,char *,...));\n\
/**\n\
@brief Creates a new CB"+name+" object from serialised data.\n\
@param data Serialised CB"+name+" data.\n\
@returns A new CB"+name+" object.\n\
*/\n\
CB"+name+" * CBNew"+name+"FromData(CBByteArray * data,CBEvents * events);\n\
CB"+name+" * CBNew"+name+"FromData(CBByteArray * data,void (*onErrorReceived)(CBError error,char *,...));\n\
\n\
/**\n\
@brief Gets a CB"+name+" from another object. Use this to avoid casts.\n\
Expand All @@ -107,14 +107,14 @@
@param self The CB"+name+" object to initialise\n\
@returns true on success, false on failure.\n\
*/\n\
bool CBInit"+name+"(CB"+name+" * self,CBEvents * events);\n\
bool CBInit"+name+"(CB"+name+" * self,void (*onErrorReceived)(CBError error,char *,...));\n\
/**\n\
@brief Initialises a CB"+name+" object from serialised data\n\
@param self The CB"+name+" object to initialise\n\
@param data The serialised data.\n\
@returns true on success, false on failure.\n\
*/\n\
bool CBInit"+name+"FromData(CB"+name+" * self,CBByteArray * data,CBEvents * events);\n\
bool CBInit"+name+"FromData(CB"+name+" * self,CBByteArray * data,void (*onErrorReceived)(CBError error,char *,...));\n\
\n\
/**\n\
@brief Frees a CB"+name+" object.\n\
Expand Down Expand Up @@ -167,26 +167,26 @@
\n\
// Constructors\n\
\n\
CB"+name+" * CBNew"+name+"(CBEvents * events){\n\
CB"+name+" * CBNew"+name+"(void (*onErrorReceived)(CBError error,char *,...)){\n\
\tCB"+name+" * self = malloc(sizeof(*self));\n\
\tif (NOT self) { \n\
\t\tevents->onErrorReceived(CB_ERROR_OUT_OF_MEMORY,\"Cannot allocate %i bytes of memory in CBNew"+name+"\n\",sizeof(*self));\n\
\t\tonErrorReceived(CB_ERROR_OUT_OF_MEMORY,\"Cannot allocate %i bytes of memory in CBNew"+name+"\n\",sizeof(*self));\n\
\t\treturn NULL;\n\
\t}\n\
\tCBGetObject(self)->free = CBFree"+name+";\n\
\tif(CBInit"+name+"(self,events))\n\
\tif(CBInit"+name+"(self,onErrorReceived))\n\
\t\treturn self;\n\
\tfree(self);\n\
\treturn NULL;\n\
}\n\
CB"+name+" * CBNew"+name+"FromData(CBByteArray * data,CBEvents * events){\n\
CB"+name+" * CBNew"+name+"FromData(CBByteArray * data,void (*onErrorReceived)(CBError error,char *,...)){\n\
\tCB"+name+" * self = malloc(sizeof(*self));\n\
\tif (NOT self) { \n\
\t\tevents->onErrorReceived(CB_ERROR_OUT_OF_MEMORY,\"Cannot allocate %i bytes of memory in CBNew"+name+"FromData\n\",sizeof(*self));\n\
\t\tonErrorReceived(CB_ERROR_OUT_OF_MEMORY,\"Cannot allocate %i bytes of memory in CBNew"+name+"FromData\n\",sizeof(*self));\n\
\t\treturn NULL;\n\
\t}\n\
\tCBGetObject(self)->free = CBFree"+name+";\n\
\tif(CBInit"+name+"FromData(self,data,events))\n\
\tif(CBInit"+name+"FromData(self,data,onErrorReceived))\n\
\t\treturn self;\n\
\tfree(self);\n\
\treturn NULL;\n\
Expand All @@ -200,13 +200,13 @@
\n\
// Initialisers\n\
\n\
bool CBInit"+name+"(CB"+name+" * self,CBEvents * events){\n\
\tif (NOT CBInitMessageByObject(CBGetMessage(self), events))\n\
bool CBInit"+name+"(CB"+name+" * self,void (*onErrorReceived)(CBError error,char *,...)){\n\
\tif (NOT CBInitMessageByObject(CBGetMessage(self), onErrorReceived))\n\
\t\treturn false;\n\
\treturn true;\n\
}\n\
bool CBInit"+name+"FromData(CB"+name+" * self,CBByteArray * data,CBEvents * events){\n\
\tif (NOT CBInitMessageByData(CBGetMessage(self), data, events))\n\
bool CBInit"+name+"FromData(CB"+name+" * self,CBByteArray * data,void (*onErrorReceived)(CBError error,char *,...)){\n\
\tif (NOT CBInitMessageByData(CBGetMessage(self), data, onErrorReceived))\n\
\t\treturn false;\n\
\treturn true;\n\
}\n\
Expand All @@ -222,14 +222,14 @@
uint32_t CB"+name+"Deserialise(CB"+name+" * self){\n\
\tCBByteArray * bytes = CBGetMessage(self)->bytes;\n\
\tif (NOT bytes) {\n\
\t\tCBGetMessage(self)->events->onErrorReceived(CB_ERROR_MESSAGE_DESERIALISATION_NULL_BYTES,\"Attempting to deserialise a CB"+name+" with no bytes.\");\n\
\t\tCBGetMessage(self)->onErrorReceived(CB_ERROR_MESSAGE_DESERIALISATION_NULL_BYTES,\"Attempting to deserialise a CB"+name+" with no bytes.\");\n\
\t\treturn 0;\n\
\t}\n\
}\n\
uint32_t CB"+name+"Serialise(CB"+name+" * self){\n\
\tCBByteArray * bytes = CBGetMessage(self)->bytes;\n\
\tif (NOT bytes) {\n\
\t\tCBGetMessage(self)->events->onErrorReceived(CB_ERROR_MESSAGE_SERIALISATION_NULL_BYTES,\"Attempting to serialise a CB"+name+" with no bytes.\");\n\
\t\tCBGetMessage(self)->onErrorReceived(CB_ERROR_MESSAGE_SERIALISATION_NULL_BYTES,\"Attempting to serialise a CB"+name+" with no bytes.\");\n\
\t\treturn 0;\n\
\t}\n\
}\n\
Expand Down
18 changes: 9 additions & 9 deletions src/CBDependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

/**
@file
@brief File for weak linked functions for dependency injection. All these functions are unimplemented. The functions include the crytography functions which are key for the functioning of bitcoin. Sockets must be non-blocking and use an asynchronous events-type system. The use of the sockets is designed to be compatible with libevent. The random number functions should be cryptographically secure. See the dependecies folder for implementations.
@brief File for weak linked functions for dependency injection. All these functions are unimplemented. The functions include the crytography functions which are key for the functioning of bitcoin. Sockets must be non-blocking and use an asynchronous onErrorReceived-type system. The use of the sockets is designed to be compatible with libevent. The random number functions should be cryptographically secure. See the dependecies folder for implementations.
*/

#ifndef CBDEPENDENCIESH
Expand Down Expand Up @@ -146,25 +146,25 @@ bool CBSocketListen(uint64_t socketID,uint16_t maxConnections);
*/
bool CBSocketAccept(uint64_t socketID,uint64_t * connectionSocketID);
/**
@brief Starts a event loop for socket events on a seperate thread. Access to the loop id should be thread safe.
@brief Starts a event loop for socket onErrorReceived on a seperate thread. Access to the loop id should be thread safe.
@param loopID A uint64_t storing an integer or pointer representation of the new event loop.
@param onError If the event loop fails during execution of the thread, this function should be called.
@param onDidTimeout The function to call for timeout events. The second argument is for the peer given by events. The third is for the timeout type. For receiving data, the timeout should be CB_TIMEOUT_RECEIVE. The CBNetworkCommunicator will determine if it should be changed to CB_TIMEOUT_RESPONSE.
@param onDidTimeout The function to call for timeout onErrorReceived. The second argument is for the peer given by onErrorReceived. The third is for the timeout type. For receiving data, the timeout should be CB_TIMEOUT_RECEIVE. The CBNetworkCommunicator will determine if it should be changed to CB_TIMEOUT_RESPONSE.
@param communicator A CBNetworkCommunicator to pass to all event functions (first parameter), including "onError" and "onDidTimeout"
@returns true on success, false on failure.
*/
bool CBNewEventLoop(uint64_t * loopID,void (*onError)(void *),void (*onDidTimeout)(void *,void *,CBTimeOutType),void * communicator);
/**
@brief Creates an event where a listening socket is available for accepting a connection. The event should be persistent and not issue timeouts.
@param loopID The loop id for socket events.
@param loopID The loop id for socket onErrorReceived.
@param socketID The socket id
@param onCanAccept The function to call for the event. Accepts "onEventArg" and the socket ID.
@returns true on success, false on failure.
*/
bool CBSocketCanAcceptEvent(uint64_t * eventID,uint64_t loopID,uint64_t socketID,void (*onCanAccept)(void *,uint64_t));
/**
@brief Sets a function pointer for the event where a socket has connected. The event only needs to fire once on the successful connection or timeout.
@param loopID The loop id for socket events.
@param loopID The loop id for socket onErrorReceived.
@param socketID The socket id
@param onDidConnect The function to call for the event.
@param peer The peer to send to the "onDidConnect" or "onDidTimeout" function.
Expand All @@ -173,7 +173,7 @@ bool CBSocketCanAcceptEvent(uint64_t * eventID,uint64_t loopID,uint64_t socketID
bool CBSocketDidConnectEvent(uint64_t * eventID,uint64_t loopID,uint64_t socketID,void (*onDidConnect)(void *,void *),void * peer);
/**
@brief Sets a function pointer for the event where a socket is available for sending data. This should be persistent.
@param loopID The loop id for socket events.
@param loopID The loop id for socket onErrorReceived.
@param socketID The socket id
@param onCanSend The function to call for the event.
@param peer The peer to send to the "onCanSend" or "onDidTimeout" function.
Expand All @@ -182,7 +182,7 @@ bool CBSocketDidConnectEvent(uint64_t * eventID,uint64_t loopID,uint64_t socketI
bool CBSocketCanSendEvent(uint64_t * eventID,uint64_t loopID,uint64_t socketID,void (*onCanSend)(void *,void *),void * peer);
/**
@brief Sets a function pointer for the event where a socket is available for receiving data. This should be persistent.
@param loopID The loop id for socket events.
@param loopID The loop id for socket onErrorReceived.
@param socketID The socket id
@param onCanReceive The function to call for the event.
@param peer The peer to send to the "onCanReceive" or "onDidTimeout" function.
Expand All @@ -197,7 +197,7 @@ bool CBSocketCanReceiveEvent(uint64_t * eventID,uint64_t loopID,uint64_t socketI
*/
bool CBSocketAddEvent(uint64_t eventID,uint32_t timeout);
/**
@brief Removes an event so no more events are made.
@brief Removes an event so no more onErrorReceived are made.
@param eventID The event ID to remove
@returns true if sucessful, false otherwise.
*/
Expand Down Expand Up @@ -225,7 +225,7 @@ int32_t CBSocketSend(uint64_t socketID,uint8_t * data,uint32_t len);
int32_t CBSocketReceive(uint64_t socketID,uint8_t * data,uint32_t len);
/**
@brief Calls a callback every "time" seconds, until the timer is ended.
@param loopID The loop id for events.
@param loopID The loop id for onErrorReceived.
@param timer The timer sent by reference to be set.
@param time The number of milliseconds between each call of the callback.
@param callback The callback function.
Expand Down
2 changes: 1 addition & 1 deletion src/CBNetworkFunctions.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ CBIPType CBGetIPType(uint8_t * IP){
}
if (x == 16)
return CB_IP_INVALID;
if(NOT x && CBIsTor(IP)) // Check x first to see that the first byte was not zero. Just prevents uneccessary memory comparisons.
if(NOT x && CBIsTor(IP)) // Check x first to see that the first byte was not zero. Just pronErrorReceived uneccessary memory comparisons.
return CB_IP_TOR;
if(NOT x && CBIsI2P(IP))
return CB_IP_I2P;
Expand Down
2 changes: 1 addition & 1 deletion src/CBValidationFunctions.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ CBPrevOut * CBTransactionValidateBasic(CBTransaction * tx, bool coinbase, bool *
}
for (uint32_t x = 0; x < tx->inputNum; x++) {
for (uint32_t y = 0; y < x; y++)
if (CBByteArrayEquals(prevOutputs[y].hash, tx->inputs[x]->prevOut.hash)
if (CBByteArrayCompare(prevOutputs[y].hash, tx->inputs[x]->prevOut.hash) == CB_COMPARE_EQUAL
&& prevOutputs[y].index == tx->inputs[x]->prevOut.index) {
// Duplicate previous output
free(prevOutputs);
Expand Down
2 changes: 1 addition & 1 deletion src/CBValidationFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

/**
@file
@brief Here are functions for doing parts of bitcoin validation. cbitcoin is designed to make a broad variety of bitcoin development easier and is not designed to implement a client node. Therefore, cbitcoin does not provide provide functions for validating block chains as there are different methods for doing so. So wether you want to use SPV, block-chain pruning, full validation or whatever, you can use these functions to help. Also see @see CBScript.c and @see CBMerkleNode.c for validation. @see CBNetworkCommunicator.c has message size checks and message deserialisation functions check messages for syntax and sizes.
@brief Here are functions for doing parts of bitcoin validation. cbitcoin is designed to make a broad variety of bitcoin development easier and is not designed to implement a client node. Therefore, cbitcoin does not provide provide functions for validating block chains as there are different methods for doing so. So wether you want to use SPV, block-chain pruning, full validation or whatever, you can use these functions to help. Also see CBScript.h and CBMerkleNode.h for validation. CBNetworkCommunicator.h has message size checks and message deserialisation functions check messages for syntax and sizes.
*/

#ifndef CBVALIDATIONFUNCTIONSH
Expand Down
35 changes: 35 additions & 0 deletions src/mainpage.dox
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
\mainpage
Refer to the following list of files for cbitcoin features:

CBAddress.h - An extension on CBVersionChecksumBytes for bitcoin addresses.\n
CBAddressBroadcast.h - This is the "addr" message structure with functions.\n
CBAddressManager.h - A structure with functions for managing CBNetworkAddress objects and CBPeer objects.\n
CBAlert.h - This is the "alert" message structure with functions.\n
CBBase58.h - Base-58 encoding and decoding, used for bitcoin addresses.\n
CBBigInt.h - Basic bignum functionality required by the library and also exposed by the library.\n
CBBlock.h - This is the "block" message structure with functions.\n
CBBlockHeaders.h - This is the "headers" message structure with functions.\n
CBByteArray.h - A structure with functions for creating references to byte data.\n
CBChainDescriptor.h - This is a structure refered to as a block locator elsewhere which is used for CBGetBlocks.\n
CBConstants.h - Constants and enumeration types used throughout the library.\n
CBDependencies.h - Weak-linked function prototypes. You may need to implement some of these funtions to use the parts of cbitcoin you want. Look in the dependencies folder for some implementations already available for you.\n
CBGetBlocks.h - This is a structure with functions for the "getblocks" and "getheaders" messages.\n
CBInventoryBroadcast.h - This is a structure with functions for the "inv" and "getdata" messages.\n
CBMerkleNode.h - Graph node structure for merkle trees and functions for building and using merkle trees.\n
CBMessage.h - The base structure for bitcoin messages which contain data to be passed over the bitcoin network.\n
CBNetworkAddress.h - A network address structure with functions used to represent a socket address in the bitcoin network.\n
CBNetworkCommunicator.h - A structure with functions for interfacing with the bitcoin network.\n
CBNetworkFunctions.h - Basic IP functions.\n
CBObject.h - The base structure for other cbitcoin structures which use reference-counting.\n
CBPeer.h - An extention to CBNetworkAddress for connected peers.\n
CBPingPong.h - This is a structure with functions for the "ping" and "pong" messages.\n
CBScript.h - Contains a bitcoin script interpreter and compiler (compiles text format into byte-code).\n
CBTransaction.h - This is the "tx" message structure with functions.\n
CBTransactionInput.h - Transaction input structure and functions for CBTransaction.\n
CBTransactionOutput.h - Transaction output structure and functions for CBTransaction.\n
CBValidationFunctions.h - Contains basic validation functions useful for validating transactions and blocks.\n
CBVarInt.h - A structure for representing bitcoin varints.\n
CBVersion.h - This is the "version" message structure with functions.\n
CBVersionChecksumBytes.h - A structure with functions for base-58 encoded data in a format used by bitcoin.\n
*/
Loading

0 comments on commit 3e8ec2e

Please sign in to comment.