Skip to content

nitetrik/blockchaingo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Blockchain Implementation

This is a basic implementation of a blockchain using the Go programming language. The code defines the structures and functions necessary to create a blockchain, add blocks to it, and validate the blocks.

Prerequisites

  • Go programming language (version 1.16 or later)

Getting Started

  1. Clone the repository or download the source code files.

  2. Open a terminal or command prompt and navigate to the project directory.

  3. Build and run the application using the following command:

    shellCopy code

    go run main.go

Code Structure

The main components of the code are as follows:

  • SmartContract struct: Represents a smart contract with its code.

  • Transaction struct: Represents a transaction with sender, receiver, and amount.

  • Block struct: Represents a block in the blockchain with index, data (transactions), contracts (smart contracts), hash, previous hash, timestamp, and nonce.

  • Blockchain struct: Represents the blockchain with a slice of blocks and difficulty level.

  • NewBlockchain() function: Creates a new blockchain with a genesis block.

  • NewGenesisBlock() function: Creates a new genesis block (the first block in the blockchain).

  • NewBlock() function: Creates a new block with the provided data, contracts, and previous hash.

  • MineBlock() method: Mines the block by repeatedly calculating the hash and checking its validity based on the difficulty level.

  • CalculateHash() method: Calculates the hash of the block.

  • IsValid() method: Checks if the block's hash meets the difficulty requirement.

  • AddBlock() method: Adds a new block to the blockchain.

  • GetLatestBlock() method: Retrieves the latest block in the blockchain.

  • Print() method: Prints the entire blockchain.

  • Validator struct: Represents a block validator with a reference to the blockchain.

  • NewValidator() function: Creates a new block validator with the given blockchain.

  • Validate() method: Validates a block by checking its hash, previous hash, data, and nonce.

  • TestCreateBlockchain() function: Tests the creation of a new blockchain.

  • TestAddBlock() function: Tests the addition of a new block to the blockchain.

  • TestMineBlock() function: Tests the mining of a block.

  • TestValidateBlock() function: Tests the validation of a block.

  • TestMain() function: Runs all the tests.

  • PrintJSON() method: Prints the blockchain in JSON format.

Usage

The main function demonstrates the usage of the blockchain implementation. It creates a new blockchain, adds transactions and smart contracts to a new block, mines the block, adds it to the blockchain, and performs block validation. The blockchain and the validation result are then printed.

License

This project is licensed under the MIT License.

About

A example blockchain in go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages