blockchain

package
v0.0.0-...-06e810e Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateMerkleProof

func GenerateMerkleProof(transactions []*Transaction, index int) ([][]byte, error)

func HashData

func HashData(data []byte) []byte

func HashTransaction

func HashTransaction(tx *Transaction) []byte

func PutIntoDb

func PutIntoDb(bc *Blockchain) error

func VerifyTransaction

func VerifyTransaction(rootHash []byte, tx *Transaction, proof [][]byte) bool

Types

type Block

type Block struct {
	Number       int            `json:"number"`
	PrevHash     string         `json:"prevHash"`
	Timestamp    int64          `json:"timestamp"`
	Nonce        int            `json:"nonce"`
	Transactions []*Transaction `json:"transactions"`
	MerkleRoot   string         `json:"merkle_root"`
}

func NewBlock

func NewBlock(prevHash string, blockHeight int, nonce int, txns []*Transaction) *Block

func (*Block) AddTransactionToTheBlock

func (b *Block) AddTransactionToTheBlock(txn *Transaction)

func (*Block) Hash

func (b *Block) Hash() string

func (*Block) ToJson

func (b *Block) ToJson() string

type BlockHeightTxIndex

type BlockHeightTxIndex struct {
	BlockHeight int `json:"block_height"`
	TxIndex     int `json:"tx_index"`
}

type Blockchain

type Blockchain struct {
	TransactionPool  []*Transaction `json:"transaction_pool"`
	Blocks           []*Block       `json:"block_chain"`
	WalletIndex      *WalletIndex
	TransactionIndex *TransactionIndex
}

func NewBlockchain

func NewBlockchain(genesisBlock *Block) *Blockchain

func ReadFromDb

func ReadFromDb() (Blockchain, error)

func (*Blockchain) AddBlock

func (bc *Blockchain) AddBlock(nonce int, txns []*Transaction)

func (*Blockchain) AddTransactionToTransactionPool

func (bc *Blockchain) AddTransactionToTransactionPool(txn *Transaction)

func (*Blockchain) Airdrop

func (bc *Blockchain) Airdrop(address string)

func (*Blockchain) CalculateBalance

func (bc *Blockchain) CalculateBalance(address string) int

func (*Blockchain) CopyTransactionPool

func (bc *Blockchain) CopyTransactionPool() []*Transaction

func (*Blockchain) LastBlock

func (bc *Blockchain) LastBlock() *Block

func (*Blockchain) Mining

func (bc *Blockchain) Mining() bool

func (*Blockchain) ProofOfWork

func (bc *Blockchain) ProofOfWork() (int, []*Transaction)

func (*Blockchain) ToJSON

func (bc *Blockchain) ToJSON() string

func (*Blockchain) ValidProof

func (bc *Blockchain) ValidProof(nonce int, previousHash string, transactions []*Transaction, difficulty int) bool

type Node

type Node struct {
	Left  *Node
	Right *Node
	Hash  []byte
}

func BuildMerkleTree

func BuildMerkleTree(leaves []*Node) *Node

func CreateLeafNodes

func CreateLeafNodes(transactions []*Transaction) []*Node

type Transaction

type Transaction struct {
	From           string `json:"from"`
	To             string `json:"to"`
	Value          uint64 `json:"value"`
	Data           []byte `json:"data"`
	Status         string `json:"status"`
	TransactioHash string `json:"transaction_hash"`
}

func NewTransaction

func NewTransaction(from string, to string, value uint64, data []byte) *Transaction

func (*Transaction) Hash

func (t *Transaction) Hash() string

func (*Transaction) ToJSON

func (t *Transaction) ToJSON() string

func (*Transaction) VerifyTransaction

func (t *Transaction) VerifyTransaction() bool

type TransactionIndex

type TransactionIndex struct {
	TxBlockHeight map[string]*BlockHeightTxIndex `json:"transactions"`
}

func NewTransactionIndex

func NewTransactionIndex() *TransactionIndex

func (*TransactionIndex) AddTransaction

func (ti *TransactionIndex) AddTransaction(txHash string, txIndex int, blockHeight int)

func (*TransactionIndex) GetTransactionMetadata

func (ti *TransactionIndex) GetTransactionMetadata(txHash string) *BlockHeightTxIndex

type WalletIndex

type WalletIndex struct {
	Transactions map[string][]*WalletTransactionIndex `json:"transactions"`
}

func NewWalletIndex

func NewWalletIndex() *WalletIndex

func (*WalletIndex) AddTransaction

func (w *WalletIndex) AddTransaction(address string, blockIndex int, txIndex int, transaction *Transaction)

func (*WalletIndex) GetWalletTransactions

func (w *WalletIndex) GetWalletTransactions(address string) []*WalletTransactionIndex

type WalletTransactionIndex

type WalletTransactionIndex struct {
	BlockIndex       int          `json:"block_index"`
	TransactionIndex int          `json:"transaction_index"`
	Transaction      *Transaction `json:"transactions"`
}

Jump to

Keyboard shortcuts

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