Building Ethereum with C language
just like bitcoin, Ethereum is neither controlled nor owned by anyone - it is an open source project created by many people around the world. Different from bitcoin protocol, Ethereum's design is very flexible and adaptable. It is very easy to create new applications on the Ethereum platform. With the release of homestead, anyone can use the applications on this platform safely
Ethereum is a programmable blockchain. It does not give users a series of preset operations, but allows users to create complex operations according to their own wishes. In this way, it can be used as a platform for many types of decentralized blockchain applications
in a narrow sense, Ethereum refers to a series of protocols that define a decentralized application platform. The core of Ethereum is Ethereum virtual machine (EVM), which can encode any complex algorithm. In computer science terminology, Ethereum is "Turing complete.". Developers can use the existing JavaScript and python as the model of other friendly programming languages to create applications running on the Ethereum simulator.
Ethereum is an open source public blockchain platform with smart contract function. It provides decentralized virtual machine (Ethereum virtual machine) to process point-to-point contract through its special cryptocurrency ether (also known as "Ethereum")
The token on theblockchain is called ether, and the code is eth. It can be traded in many foreign exchange markets of cryptocurrency, and it is also the medium used to pay transaction fees and computing services on Ethereum
the concept of Ethereum was first proposed by vitalik buterin, a programmer, from 2013 to 2014, inspired by bitcoin, with the general meaning of "next generation cryptocurrency and decentralized application platform", and began to develop through ICO crowdfunding in 2014. As of February 2018, Ethernet is the second highest cryptocurrency in market value, second only to bitcoin
extended data:
Ethereum platform has no characteristics and value. Like programming languages, it's up to entrepreneurs and developers to decide what to use. However, it is clear that some application types benefit more from the functions of Ethereum than others. Ethereum is especially suitable for those applications that automatically interact directly between points or promote group coordination activities across networks
for example, coordinate the application of point-to-point market, or the automation of complex financial contracts. Bitcoin enables indivials to exchange money without the help of financial institutions, banks or governments. The impact of Ethereum may be more profound
in theory, any complex financial activities or transactions can be automatically and reliably carried out on Ethereum with coding. In addition to financial applications, any application scenario with high requirements for trust, security and persistence, such as asset registration, voting, management and Internet of things, will be affected by Ethereum platform on a large scale
make: * * [geth] error 1
there is no such directory, or the directory has insufficient permissions
Why develop digital currency? From the perspective of the central bank, there are six advantages:
first, to improve the convenience and transparency of economic transactions; second, to rece the high cost of the issuance and circulation of traditional paper money; third, to better support economic and social development; fourth, to help the comprehensive realization of Inclusive Finance; fifth, to rece money laundering, to promote the development of the economy and society Sixth, improve the central bank's control over money supply and circulation
steps of digital currency development:
the first step,
first, we need to download the source code of a blockchain system from git, such as choosing the backbone code of bitcoin to download the relevant source code
At the same time, prepare the corresponding compiling environment (c + + is recommended in Linux) and install the corresponding development environment and toolsthe second step and
code all need to be compiled, so you need to prepare the compilation environment and tools, download the environment compilation tools, configure
system environment variables, QT environment and other files, and compile commands are described in detail in the files in ITC source code
however, the construction of the system and development environment, program compilation and other processes are cumbersome, so it is not recommended for ordinary users to make their own. For developers, it may take 2-3 days to install and configure for the first time
the third step,
take bitcoin development as an example, it is the development environment of Q. after downloading the source code and configuring the environment, open the source code of the bitcoin core in qtcreator, configure relevant files and compilers, and start to try to compile the client of the bitcoin core
Step 4,
transform into your own digital currency, open each source file, find the corresponding local adjustment parameters, such as adjusting
the number of coins in each block, the total output, the adjustment difficulty, and so on. Then the most critical point is to change the name to your own currency
name as you want, and don't forget to replace the related icons in the resource folder. If all goes well, after re compiling, your new coin will be invented successfully
for the development of this digital currency, the technology is relatively professional, so it is better to have a professional team to assist
framework for digital currency development:
1. Build Ethereum private chain test environment and public chain node environment configuration
2. Ethereum transaction and confirmation principle
3. Ethereum JSON RPC interface
4. Ethereum transfer and withdrawal principle
5. The server connects Ethereum public chain interface, and its own server stores business data, Public chain storage transaction anonymous data
6, private key security processing
the following is the development code example:
for example, the common digital currency wallets in the market are:
app class: kcash Imtokenweb: myethereumwallet Google browser plug-in: metamask
one of the most commonly used is imtoken
blockchain transaction technology concept:
let's take a look at how blockchain transaction is handled with bitcoin as an example. In order to send a certain amount of bitcoin to another wallet, you need the following information: the address to send the funds to your wallet, the amount of cryptocurrency you want to send
the ID of the recipient's wallet
each transaction is signed with a unique confidential private key. Once the payment is signed by the sender, it becomes publicly available. The transaction still needs to be confirmed so that the payee can get the money. In order to confirm the transaction, it is necessary to generate a new chain block
these blocks are generated by complex mathematical calculation to find the unique key. It takes 10 minutes to create a new block, and the person who finds the key gets a certain amount of coins as a reward. Once a new block of the chain is created, it is not possible to remove it from the database or change the information in some way. Therefore, blockchain transaction is final and irreversible
three core advantages of digital currency:
first, digital currency is a fair currency
digital currency has no specific issuing institution, is not issued by a country, only depends on a specific algorithm, which means that digital currency can not be manipulated by manipulating the number of issues, so digital currency is a free, non-state currency
we can see that there are many countries that directly recognize the virtual currency, so if there is a demand, it needs a trading platform
many investors who want to build a virtual currency trading platform now, why can't they go to these areas to build a trading platform? Building a virtual currency trading platform is a good business opportunity, isn't it
Second, the security factor of digital currency is higher.
although the emergence of paper money facilitates the transactions in our daily life, there are risks of being stolen and received. Although e-money can avoid these risks, there will be new problems such as theft and swiping
digital currency can avoid the above problems. And broadcast every transaction record on the network. Yes, all nodes save all the currency circulation information, so that any node can easily find the currency circulation before the transaction
Thirdly, the transaction of digital currency can realize anonymous transaction. Although you can query the flow information of each account according to the local complete transaction recordsbut we can't know who the owner of this account is, and no one has the ability to manipulate the digital currency on other people's accounts, which protects the privacy of users
if you also hold trading digital currency, foreign exchange gold, crude oil, contract Futures:
system("~/ myprogram/test.sh");} The results are as follows: xiakeyou@ubuntu :~/myprogram$ gcc systemtest.c -o
systemtestxiakeyou@ubuntu :~/myprogram$ ./systemtest/home/d/e/ xiakeyouxiakeyou@ubuntu ~/myprogram$2) Popen (char *command, char *type) execution process: Popen () calls fork () to generate the child process, and then invokes /bin/sh
-c from the sub process to execute the instruction of parameter command. The parameter
type can use "R" for reading and "W" for writing. According to this type value, popen() will establish a pipeline to the standard output device or standard input device of the child process, and then return a file
pointer. Then the process can use this file pointer to read the output device of the child process or write to the standard input device of the child process. In addition, all functions that use the file pointer (file *) operation can also be used
except fclose(). Return value: if successful, the file pointer will be returned; otherwise, null will be returned. The error reason is stored in errno
note: when writing a program with suid / sgid permission, please try to avoid using Popen (), which inherits environment variables and may cause system security problems through environment variables. For example, the content of C program popentest. C is as follows: # include & lt; stdio.h> main(){FILE * fp; charbuffer[80]; fp=popen(“~/myprogram/test.sh”,”r”); fgets(buffer,sizeof(buffer),fp); printf(“%s”,buffer); pclose(fp);} The results are as follows: xiakeyou@ubuntu :~/myprogram$ vim popentest. cxiakeyou@ubuntu :~/myprogram$ gcc popentest.c -o popentestxiakeyou@ubuntu :~/myprogram$ ./popentest/home/d/e/ xiakeyouxiakeyou@ubuntu : ~ / myprogram $
it's just that my ability may be a little limited and I don't understand it very well. It's a script to use system () directly, but the return value is a mess. I tried many times, but I didn't find any rules. I can't help but look at the above blog post and get some inspiration, which can be realized as follows: first, use echo & gt; to return the script value; Xxxxx output to a local file, when you need this return value is, but through the C language file operation function to read directly from the file, later on, this should be the Popen implementation method! There are three ways for C program to call shell script: system (), Poppen (), exec series function system ()
you don't need to generate a process by yourself, it has been encapsulated, you need to fork your own process to directly add your own command exec, and then the exec's own command Poppen () can also execute your command, which is less than the system cost. 1) system (shell command or shell script path); System() will call fork() to generate a subprocess, and the subprocess will call / bin / SH-C string to perform the command represented by the
parameter string string. After the command is performed, the original calling process will be returned immediately. The sigchld
signal will be temporarily suspended while the SIGINT and sigquit signals will be ignored ring the call to system()
return value: if system() fails to call / bin / sh, 127 will be returned, and - 1 will be returned for other failure reasons. If the parameter string is null, a non-zero value is returned
if the system() call is successful, the return value after the
shell command is fulfilled will be returned. However, this return value may also be the 127 returned when the system() call / bin / sh fails. Therefore, it is better to reflect on errno
to confirm the successful fulfillment. System command is widely used because of its simple and efficient function. Here is an example: there is a shell script test.sh in ~ / test / directory, and the content is #! Bin / bash#test.shecho Hello create a new C file system in the same layer directory_ Test. C, the content is: # include & lt; stdlib.h> int main(){system("~/ test/test.sh");} The effect of performance is as follows:[ root@localhost test]$gcc system_ test.c -o system_ test[ root@localhost test]$./system_ testhello[ root@localhost Test]$2) Popen (char *command, char *type) Popen () calls fork () to generate the child process, and then invokes /bin/sh -c from the sub process to perform the instruction of
parameter /bin/sh. The parameter type can be applied with "R" for read and "W" for write. Following this type value, popen() creates a
pipeline to the standard output device or standard input device of the child process, and then returns a file pointer. Then the process can use this file pointer to read the output device of the sub process
or write it to the standard input device of the sub process. In addition,
can be applied to all functions that apply file pointer (file *), except fclose(). Return value: if successful, the file pointer will be returned; otherwise, null will be returned; the error
reason is stored in errno. Note: when you write a program with suid / sgid permission, please try to avoid using Popen (), which will inherit environment variables, which may cause system security problems. For example, the content of C program popentest. C is as follows: # include & lt; stdio.h> main{FILE * fp; charbuffer[80]; fp=popen(“~/myprogram/test.sh”,”r”); fgets(buffer,sizeof(buffer),fp); printf(“%s”,buffer); pclose(fp);} The effect of performance is as follows:[ root@localhost test]$ vim popentest.c[ root@localhost test]$ gcc popentest.c -o popentest[ root@localhost test]$ ./popentest/root/test[ root@localhost test]$
view plain to clipboardprint?
01.QString GetLocalIp()
02.{
03.
04. int sock_get_ip;
05. char ipaddr[50];
06.
07. struct sockaddr_in *sin;
08. struct ifreq ifr_ip;
09.
10. if ((sock_get_ip=socket(AF_INET, SOCK_STREAM, 0)) == -1)
11. {
12. printf("socket create failse...GetLocalIp!\n");
13. return "";
14. }
15.
16. memset(&ifr_ip, 0, sizeof(ifr_ip));
17. strncpy(ifr_ip.ifr_name, "eth0", sizeof(ifr_ip.ifr_name) - 1);
18.
19. if( ioctl( sock_get_ip, SIOCGIFADDR, &ifr_ip) < 0 )
20. {
21. return "";
22. }
23. sin = (struct sockaddr_in *)&ifr_ip.ifr_addr;
24. strcpy(ipaddr,inet_ntoa(sin->sin_addr));
25.
26. printf("local ip:%s \n",ipaddr);
27. close( sock_get_ip );
28.
29. return QString( ipaddr );
30.}
QString GetLocalIp()
{
int sock_get_ip;
char ipaddr[50];
struct sockaddr_in *sin;
struct ifreq ifr_ip;
if ((sock_get_ip=socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("socket create failse...GetLocalIp!\n");
return "";
}
memset(&ifr_ip, 0, sizeof(ifr_ip));
strncpy(ifr_ip.ifr_name, "eth0", sizeof(ifr_ip.ifr_name) - 1);
if( ioctl( sock_get_ip, SIOCGIFADDR, &ifr_ip) < 0 )
{
return "";
}
sin = (struct sockaddr_in *)&ifr_ip.ifr_addr;
strcpy(ipaddr,inet_ntoa(sin->sin_addr));
printf("local ip:%s \n",ipaddr);
close( sock_get_ip );
return QString( ipaddr );
}
//修改本机IP地址的函数
int SetLocalIp( const char *ipaddr )
{
int sock_set_ip;
struct sockaddr_in sin_set_ip;
struct ifreq ifr_set_ip;
bzero( &ifr_set_ip,sizeof(ifr_set_ip));
if( ipaddr == NULL )
return -1;
if(sock_set_ip = socket( AF_INET, SOCK_STREAM, 0 ) == -1);
{
perror("socket create failse...SetLocalIp!\n");
return -1;
}
memset( &sin_set_ip, 0, sizeof(sin_set_ip));
strncpy(ifr_set_ip.ifr_name, "eth0", sizeof(ifr_set_ip.ifr_name)-1);
sin_set_ip.sin_family = AF_INET;
sin_set_ip.sin_addr.s_addr = inet_addr(ipaddr);
memcpy( &ifr_set_ip.ifr_addr, &sin_set_ip, sizeof(sin_set_ip));
if( ioctl( sock_set_ip, SIOCSIFADDR, &ifr_set_ip) < 0 )
{
perror( "Not setup interface\n");
return -1;
}
//设置激活标志
ifr_set_ip.ifr_flags |= IFF_UP |IFF_RUNNING;
//get the status of the device
if( ioctl( sock_set_ip, SIOCSIFFLAGS, &ifr_set_ip ) < 0 )
{
perror("SIOCSIFFLAGS");
return -1;
}
close( sock_set_ip );
return 0;
}