block

package
v1.0.0-release Latest Latest
Warning

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

Go to latest
Published: May 19, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComputeHash

func ComputeHash(block *Block) []byte

calculateHash calculates the hash of a block

func IntToHex

func IntToHex(num int64) []byte

IntToHex converts an int64 to a byte array

func IsGenesisBlock

func IsGenesisBlock(b *Block) bool

IsGenesisBlock checks if the block is the genesis block

func VerifyBlock

func VerifyBlock(currentBlock *Block) bool

VerifyBlock checks if the block signature is valid

Types

type Block

type Block struct {
	Header
	Transactions []transaction.Transaction `json:"transactions"`
}

func DeserializeBlock

func DeserializeBlock(data []byte) (*Block, error)

DeserializeBlock converts a byte slice back into a Block

func NewBlock

func NewBlock(transactions []transaction.Transaction, prevBlockHash []byte, height uint32, minerAddr ecdsa.KeyPair) *Block

NewBlock creates a new block using the provided transactions and the previous block hash

func (*Block) ComputeMerkleRoot

func (b *Block) ComputeMerkleRoot() []byte

calculateMerkleRoot computes the Merkle root of the transactions in the block

func (*Block) GetTransactionIDsMap

func (b *Block) GetTransactionIDsMap() map[string]transaction.Transaction

func (*Block) Serialize

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

Serialize converts the block into a byte slice

func (*Block) SignBlock

func (b *Block) SignBlock(privateKey ecdsa.KeyPair) error

SignBlock signs the block with the given private key and adds the signature to the block header

type Header struct {
	Version    uint32 `json:"version"`     // Blockchain version
	PrevBlock  []byte `json:"prev_block"`  // 256-bit hash of the previous block header
	MerkleRoot []byte `json:"merkle_root"` // 256-bit hash based on all of the transactions in the block
	TargetBits uint32 `json:"target_bits"` // Target bits is a way to set the mining difficulty
	Timestamp  int64  `json:"timestamp"`   // Current timestamp as seconds since 1970-01-01T00:00 UTC
	Height     uint32 `json:"height"`      // Block height
	Nonce      uint32 `json:"nonce"`       // 32-bit number (starts at 0) used to generate the required hash
	MinerAddr  []byte `json:"miner_addr"`  // ECDSA public key of the miner
	Signature  []byte `json:"signature"`   // ECDSA signature of the block header
}

Jump to

Keyboard shortcuts

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