block chaining technology code example

Example: blockchain implementation

int hash();
void printStruct();
void printChain();
void printWallet();
void setLocation();
int Random();
int rand();
int getBlock();
void printNewChain();
struct Block{
int locationBefore;
int machineID;
int locationCurrent;
};
int Random(int lower, int upper)
{
int num = (rand() % (upper - lower + 1)) + lower;
return num;
}
// void setLocation(int previousLocation , int currentLocation){
// previousLocation = currentLocation;
// }
void main(){
//initialize
struct Block hashArray[100];
int walletMachine1 = 0, walletMachine2 = 0, z =0 , y =
0,machine1Code,machine2Code,check=1,manualCheck,automaticIterations,i=0,blocks=0;
//solution
int solution = Random(0,10);
printf("manual or automatic simulation?( 1:manual 0:automatic):");
scanf("%d",&manualCheck);
if(manualCheck == 0){
printf("please enter the number of iterations for the simulations:");
scanf("%d",&automaticIterations);
}
do{
//mining
machine1Code = Random(0,10);
machine2Code = Random(0,10);
if(machine1Code == solution){
//machine 1 has the code
walletMachine1++;
blocks++;
//set in block
hashArray[z].machineID = 1;
hashArray[z].locationBefore = y;
hashArray[z].locationCurrent = z;
//set previous location for next block
y=z;
//set hash for new block
//z now location of next block
z = Random(0,100);
z = hash(z,1,hashArray);
//new code
solution = Random(0,10);
}
if(machine2Code == solution){
//machine 2 has the code
walletMachine2++;
blocks++;
//set in block
hashArray[z].machineID = 2;
hashArray[z].locationBefore = y;
hashArray[z].locationCurrent = z;
//set previous location for next block
y=z;
//set hash for new block
//z now location of next block
z = Random(0,100);
z = hash(z,2,hashArray);
//new code
solution = Random(0,10);
}
if(manualCheck == 1){
printf("continue?( 1:yes , 0:no ):");
scanf("%d",&check);
}
i++;
if(i == automaticIterations){
check = 0;
}
}while(check == 1);
//print wallet for each machine
printWallet(walletMachine1,walletMachine2,i);
//to print blockChain
printf("\n\n");
printf("the blockchain has been depicted as shown:-\n");
printf("------------------------Block Chain-------------------------\n");
printf("------------------------------------------------------------\n");
//print chain
printChain(y,hashArray);
printf("\n\n");
printf("change a block?( 1:y 0:n):");
scanf("%d",&manualCheck);
if(manualCheck == 1){
do{
printf("please enter the block to be changed (from top):");
scanf("%d",&i);
}while(i > blocks);
i--;
//get block
int LocationNow = getBlock(i,y,hashArray);
int locationBefore = hashArray[LocationNow].locationBefore;
printf("\n");
printf("please enter the data for the block :");
scanf("%d",&i);
hashArray[LocationNow].machineID = i;
//getData
LocationNow = hash(LocationNow,i,hashArray);
//set data in block
hashArray[LocationNow].machineID = i;
hashArray[LocationNow].locationCurrent = LocationNow;
// int locationBefore = hashArray[LocationNow].locationBefore;
hashArray[LocationNow].locationBefore = locationBefore;
printStruct(hashArray[LocationNow]);
//to change data
printNewChain(i,y,hashArray);
}
}
void printNewChain(int num,int y,struct Block Array[100]){
do{
y = Array[y].locationBefore;
num--;
}while(num != 0);
printChain(y,Array);
}
int getBlock(int num,int y,struct Block Array[100]){
if(num == 0){
printStruct(Array[y]);
}
do{
y = Array[y].locationBefore;
num--;
}while(num != 0);
printStruct(Array[y]);
return y;
}
void printWallet(int a,int b,int iterations){
printf("wallet 1 has earned %d latinum after %d simulations\n",a,iterations);
printf("wallet 2 has earned %d latinum after %d simulations\n",b,iterations);
}
void printChain(int y,struct Block Array[100]){
if(Array[y].locationCurrent != 0){
printStruct(Array[y]);
printChain(Array[y].locationBefore,Array);
}
if (Array[y].locationCurrent == 0){
printStruct(Array[y]);
}
}
void printStruct(struct Block t){
printf("------------------------------------------------------------\n");
printf("locationCurrent :- %d\n",t.locationCurrent);
printf("machineID :- %d\n",t.machineID);
printf("locationBefore :- %d\n" , t.locationBefore);
}
int hash(int num,int data,struct Block Array[100]){
int location=0,i=0;
do{
location = (num%100 + i%100)%100 ;
i ++;
}while(Array[location].machineID == 1 || Array[location].machineID == 2 || i != 100);
return location;
}

Tags:

C Example