blockchain

package
v0.0.0-...-c7d00ea Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Block

type Block struct {
	Index        uint64
	Timestamp    time.Time
	Data         []byte
	PreviousHash []byte
	Hash         []byte
	Nonce        uint64
	Difficulty   uint64
	Proposer     string
	Proof        []byte
	Metadata     map[string]any
	// contains filtered or unexported fields
}

Block represents a Proof of Work block

func NewBlock

func NewBlock(index uint64, data []byte, previousHash []byte, difficulty uint64) *Block

NewBlock creates a new PoW block

func (*Block) CalculateHash

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

CalculateHash computes the block's hash based on its contents

func (*Block) Clone

func (b *Block) Clone() interfaces.Block

Clone creates a copy of the block

func (*Block) ComputeHash

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

ComputeHash calculates and returns the hash of the block

func (*Block) GetCachedHash

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

GetCachedHash returns the cached hash if available

func (*Block) GetData

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

GetData returns the block's data

func (*Block) GetDifficulty

func (b *Block) GetDifficulty() any

GetDifficulty returns the mining difficulty as any

func (*Block) GetDifficultyUint64

func (b *Block) GetDifficultyUint64() uint64

GetDifficultyUint64 returns the mining difficulty as uint64

func (*Block) GetHashAlgorithm

func (b *Block) GetHashAlgorithm() string

GetHashAlgorithm returns the preferred hash algorithm for this block

func (*Block) GetHashBytes

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

GetHashBytes returns the hash as bytes

func (*Block) GetHashString

func (b *Block) GetHashString() string

GetHashString is a helper method to get hash as hex string (not from interface)

func (*Block) GetHashableBytes

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

GetHashableBytes returns the byte representation for hashing

func (*Block) GetIndex

func (b *Block) GetIndex() uint64

GetIndex returns the block index/height

func (*Block) GetMetadata

func (b *Block) GetMetadata() map[string]any

GetMetadata returns additional block metadata

func (*Block) GetNonce

func (b *Block) GetNonce() uint64

GetNonce returns the current nonce value

func (*Block) GetPreviousHash

func (b *Block) GetPreviousHash() string

GetPreviousHash returns the previous block's hash as hex string

func (*Block) GetPreviousHashBytes

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

GetPreviousHashBytes returns the previous hash as bytes

func (*Block) GetProof

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

GetProof returns the consensus proof (the hash for PoW)

func (*Block) GetProposer

func (b *Block) GetProposer() string

GetProposer returns who proposed this block

func (*Block) GetSize

func (b *Block) GetSize() int

GetSize returns the size of the block in bytes

func (*Block) GetTimestamp

func (b *Block) GetTimestamp() time.Time

GetTimestamp returns when the block was created

func (*Block) GetTransactions

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

GetTransactions returns the transactions in the block

func (*Block) InvalidateCache

func (b *Block) InvalidateCache()

InvalidateCache clears the cached hash value

func (*Block) IsValid

func (b *Block) IsValid() bool

IsValid checks if the block meets the PoW requirements

func (*Block) Serialize

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

Serialize converts the block to bytes

func (*Block) SetData

func (b *Block) SetData(data []byte)

SetData sets the block's data

func (*Block) SetDifficulty

func (b *Block) SetDifficulty(difficulty any)

SetDifficulty sets the mining difficulty from any

func (*Block) SetHashBytes

func (b *Block) SetHashBytes(hash []byte)

SetHashBytes sets the hash directly from bytes

func (*Block) SetHashFromBytes

func (b *Block) SetHashFromBytes(hash []byte)

SetHashBytes sets the block's hash directly from bytes (not from interface) This is useful when loading blocks from storage

func (*Block) SetMetadata

func (b *Block) SetMetadata(metadata map[string]any)

SetMetadata sets additional block metadata

func (*Block) SetNonce

func (b *Block) SetNonce(nonce uint64)

SetNonce sets the nonce value

func (*Block) SetProof

func (b *Block) SetProof(proof []byte)

SetProof sets the consensus proof

func (*Block) SetProposer

func (b *Block) SetProposer(proposer string)

SetProposer sets who proposed this block

func (*Block) Validate

func (b *Block) Validate() error

Validate performs basic validation

func (*Block) Verify

func (b *Block) Verify(hash []byte) (bool, error)

Verify checks if a given hash matches this block's hash

Jump to

Keyboard shortcuts

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