chain

package
v0.8.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const Difficulty = int64(21)

Difficulty Implementation of the difficulty rate

View Source
const (
	KeyLastHash = "last_hash"
)

Variables

This section is empty.

Functions

func GetProofOfStakeTarget added in v0.8.3

func GetProofOfStakeTarget(stake *big.Int) *big.Int

func IsValidPoS

func IsValidPoS(block *Block, stake *big.Int) bool

Types

type Block

type Block struct {
	Block uint64
	Data  []byte //	this block's data
	Hash  []byte //	this block's hash
	Link  []byte //	the hash of the last block in the chain
	Nonce int64  //	the nonce used to sign the block for verification
}

func GetBlockByHash added in v0.5.1

func GetBlockByHash(c *Chain, hash []byte) (*Block, error)

GetBlockByHash retrieves a specific block from the blockchain using its hash.

Parameters:

  • c: A pointer to the Chain instance.
  • hash: A byte slice representing the hash of the block to retrieve.

Returns:

  • *Block: A pointer to the retrieved Block if found.
  • error: An error if the block retrieval fails, nil otherwise.

This function attempts to fetch the block data from storage using the provided hash, then deserializes the data into a Block struct. If any step fails, an error is returned.

func GetBlockchain added in v0.5.1

func GetBlockchain(c *Chain) []*Block

GetBlockchain retrieves all blocks from the blockchain.

Parameters:

  • c: A pointer to the Chain instance.

Returns:

  • []*Block: A slice of pointers to Block, representing all blocks in the chain.

This function iterates through the entire blockchain, collecting all blocks into a slice. If an error occurs during iteration, it logs the error and returns nil. The blocks are returned in the order they are stored in the chain.

func (*Block) Build

func (b *Block) Build(data []byte, link []byte, stake *big.Int, block uint64)

func (*Block) Deserialize

func (b *Block) Deserialize(data []byte) error

func (*Block) Print

func (b *Block) Print()

func (*Block) Serialize

func (b *Block) Serialize() ([]byte, error)

type Chain

type Chain struct {
	LastHash []byte

	CurrentBlock uint64
	// contains filtered or unexported fields
}

func (*Chain) AddBlock

func (c *Chain) AddBlock(data []byte) error

AddBlock adds a new block to the blockchain with the given data.

This function: 1. Updates the last hash of the chain. 2. Creates a new block with the provided data, the last hash, and a nonce of 1. 3. Validates the new block using Proof of Stake (PoS). 4. Saves the new block to storage if valid. 5. Updates the LastHash of the chain to the hash of the new block.

Parameters:

  • data: The data to be included in the new block.

Returns:

  • error: An error if any step fails, nil otherwise.

func (*Chain) GetBlock

func (c *Chain) GetBlock(hash []byte) (*Block, error)

func (*Chain) GetLastBlock

func (c *Chain) GetLastBlock() (*Block, error)

GetLastBlock retrieves the most recent block in the blockchain.

This function: 1. Updates the last hash of the chain to ensure it's current. 2. Retrieves the block corresponding to the last hash.

Returns:

  • *Block: A pointer to the last block in the chain.
  • error: An error if the retrieval fails, nil otherwise.

func (*Chain) Init

func (c *Chain) Init(path string) error

Init initializes the blockchain.

This function performs the following tasks: 1. Creates a data directory for storing blocks if it doesn't exist. 2. Initializes the storage for the blockchain. 3. Creates and stores the genesis block if the blockchain is empty.

Returns:

  • error: An error if any step in the initialization process fails, nil otherwise.
func (c *Chain) IterateLink(each func(b *Block), pre, post func()) error

IterateLink iterates through the blockchain, executing provided functions at specific points.

This function: 1. Updates the last hash of the chain. 2. Executes a pre-iteration function. 3. Iterates through each block in the chain, starting from the last block. 4. For each block, it executes a provided function. 5. After iteration, executes a post-iteration function.

Parameters:

  • each: A function to be executed for each block in the chain.
  • pre: A function to be executed before the iteration begins.
  • post: A function to be executed after the iteration completes.

Returns:

  • error: An error if any operation fails during iteration, nil otherwise.

func (*Chain) UpdateLastHash

func (c *Chain) UpdateLastHash() error

UpdateLastHash updates the LastHash field of the Chain struct with the most recent hash from storage.

This function: 1. Retrieves the last hash from the storage. 2. Updates the LastHash field of the Chain struct with the retrieved hash.

Returns:

  • error: An error if retrieving the last hash from storage fails, nil otherwise.

type Persistance

type Persistance struct {
	// contains filtered or unexported fields
}

func (*Persistance) Get

func (p *Persistance) Get(key []byte) ([]byte, error)

func (*Persistance) GetLastHash

func (p *Persistance) GetLastHash() ([]byte, error)

func (*Persistance) Init

func (p *Persistance) Init(path string, genesisFn func() (Serializable, []byte)) ([]byte, error)

func (*Persistance) Iterate

func (p *Persistance) Iterate(prefix []byte, block Serializable, callback func(value []byte) error) error

func (*Persistance) SaveBlock

func (p *Persistance) SaveBlock(hash []byte, block Serializable) error

type ProofOfStake

type ProofOfStake struct {
	Block  *Block
	Target *big.Int
	Stake  *big.Int
}

func (*ProofOfStake) Run

func (pos *ProofOfStake) Run() (int64, []byte)

type Serializable

type Serializable interface {
	Serialize() ([]byte, error)
	Deserialize(data []byte) error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL