Documentation ¶
Index ¶
- Constants
- func GetProofOfStakeTarget(stake *big.Int) *big.Int
- func IsValidPoS(block *Block, stake *big.Int) bool
- type Block
- type Chain
- type Persistance
- func (p *Persistance) Get(key []byte) ([]byte, error)
- func (p *Persistance) GetLastHash() ([]byte, error)
- func (p *Persistance) Init(path string, genesisFn func() (Serializable, []byte)) ([]byte, error)
- func (p *Persistance) Iterate(prefix []byte, block Serializable, callback func(value []byte) error) error
- func (p *Persistance) SaveBlock(hash []byte, block Serializable) error
- type ProofOfStake
- type Serializable
Constants ¶
const Difficulty = int64(21)
Difficulty Implementation of the difficulty rate
const (
KeyLastHash = "last_hash"
)
Variables ¶
This section is empty.
Functions ¶
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
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
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) Deserialize ¶
type Chain ¶
func (*Chain) AddBlock ¶
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) GetLastBlock ¶
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 ¶
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 (*Chain) IterateLink ¶
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 ¶
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) 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 ¶
func (*ProofOfStake) Run ¶
func (pos *ProofOfStake) Run() (int64, []byte)