blockchain

package
v0.0.0-...-8f01991 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2025 License: MIT Imports: 17 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 {
	Header       *Header
	Transactions []*Transaction
	Signature    []byte // Validator's signature of the block header
	// contains filtered or unexported fields
}

Block represents a block in the blockchain

func NewBlock

func NewBlock(
	height uint64,
	prevHash []byte,
	stateRoot []byte,
	gasLimit uint64,
	v *validator.Authority,
) (*Block, error)

NewBlock creates a new block

func (*Block) AddTransaction

func (b *Block) AddTransaction(tx *Transaction) error

AddTransaction adds a transaction to the block

func (*Block) GetBlockSize

func (b *Block) GetBlockSize() uint64

GetBlockSize returns the approximate size of the block in bytes

func (*Block) GetHash

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

GetHash returns the block hash, calculating it if necessary

func (*Block) GetHashString

func (b *Block) GetHashString() string

GetHashString returns the block hash as a hex string

func (*Block) GetTransactionByHash

func (b *Block) GetTransactionByHash(hash []byte) *Transaction

GetTransactionByHash returns a transaction by its hash

func (*Block) Sign

func (b *Block) Sign(v *validator.Authority) error

Sign signs the block with the given validator

func (*Block) Verify

func (b *Block) Verify(v *validator.Authority) bool

Verify verifies the block's signature

type Blockchain

type Blockchain struct {
	Blocks          []*Block
	PendingBlock    *Block
	Validators      map[string]*validator.Authority
	CurrentIndex    int
	LastBlockTime   time.Time
	ContractManager *contracts.Manager
	EventEmitter    *EventEmitter
	WebSocketServer *WebSocketServer
	// contains filtered or unexported fields
}

Blockchain represents the entire chain of blocks

func NewBlockchain

func NewBlockchain(v *validator.Authority) (*Blockchain, error)

NewBlockchain creates a new blockchain instance

func (*Blockchain) AddBlock

func (bc *Blockchain) AddBlock(block *Block) error

AddBlock adds a new block to the chain

func (*Blockchain) AddValidator

func (bc *Blockchain) AddValidator(authority *validator.Authority)

AddValidator adds a new validator to the blockchain

func (*Blockchain) DeployContract

func (bc *Blockchain) DeployContract(code []byte, owner common.Address, name, version string) (*contracts.Contract, error)

DeployContract deploys a new smart contract

func (*Blockchain) ExecuteContract

func (bc *Blockchain) ExecuteContract(address common.Address, input []byte) ([]byte, error)

ExecuteContract executes a smart contract

func (*Blockchain) GetActiveValidatorCount

func (bc *Blockchain) GetActiveValidatorCount() int

GetActiveValidatorCount aktif validator sayısını döndürür

func (*Blockchain) GetBlock

func (bc *Blockchain) GetBlock(hash []byte) *Block

GetBlock returns a block by its hash

func (*Blockchain) GetBlockByHeight

func (bc *Blockchain) GetBlockByHeight(height uint64) *Block

GetBlockByHeight returns a block by its height

func (*Blockchain) GetBlockCount

func (bc *Blockchain) GetBlockCount() uint64

GetBlockCount toplam blok sayısını döndürür

func (*Blockchain) GetBlockchainInfo

func (bc *Blockchain) GetBlockchainInfo() map[string]interface{}

GetBlockchainInfo zincir hakkında genel bilgileri döndürür

func (*Blockchain) GetContract

func (bc *Blockchain) GetContract(address common.Address) (*contracts.Contract, error)

GetContract returns a contract by address

func (*Blockchain) GetCurrentValidator

func (bc *Blockchain) GetCurrentValidator() (*validator.Authority, error)

GetCurrentValidator returns the current validator in the rotation

func (*Blockchain) GetLatestBlock

func (bc *Blockchain) GetLatestBlock() *Block

GetLatestBlock returns the latest block in the chain

func (*Blockchain) GetValidatorCount

func (bc *Blockchain) GetValidatorCount() int

GetValidatorCount toplam validator sayısını döndürür

func (*Blockchain) IsValid

func (bc *Blockchain) IsValid() bool

IsValid checks if the blockchain is valid

func (*Blockchain) ListContracts

func (bc *Blockchain) ListContracts() []*contracts.Contract

ListContracts returns all contracts

func (*Blockchain) RemoveValidator

func (bc *Blockchain) RemoveValidator(address string)

RemoveValidator removes a validator from the blockchain

func (*Blockchain) VerifyContract

func (bc *Blockchain) VerifyContract(address common.Address) error

VerifyContract verifies a deployed contract

type ContractEvent

type ContractEvent struct {
	Address     common.Address         `json:"address"`
	Name        string                 `json:"name"`
	Args        map[string]interface{} `json:"args"`
	BlockNumber uint64                 `json:"blockNumber"`
	TxHash      common.Hash            `json:"transactionHash"`
	Timestamp   int64                  `json:"timestamp"`
}

ContractEvent represents a smart contract event

type Event

type Event struct {
	Type      EventType              `json:"type"`
	Timestamp int64                  `json:"timestamp"`
	Data      map[string]interface{} `json:"data"`
}

Event represents a blockchain event

type EventEmitter

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

EventEmitter handles event emission and subscription

func NewEventEmitter

func NewEventEmitter() *EventEmitter

NewEventEmitter creates a new event emitter

func (*EventEmitter) Emit

func (e *EventEmitter) Emit(eventType EventType, data map[string]interface{})

Emit an event

func (*EventEmitter) EmitContractEvent

func (e *EventEmitter) EmitContractEvent(event ContractEvent)

EmitContractEvent emits a contract-specific event

func (*EventEmitter) Subscribe

func (e *EventEmitter) Subscribe(eventType EventType) chan Event

Subscribe to specific event type

func (*EventEmitter) Unsubscribe

func (e *EventEmitter) Unsubscribe(eventType EventType, ch chan Event)

Unsubscribe from specific event type

type EventType

type EventType string

EventType represents different types of blockchain events

const (
	// Contract related events
	EventContractDeployStarted EventType = "CONTRACT_DEPLOY_STARTED"
	EventContractDeploySuccess EventType = "CONTRACT_DEPLOY_SUCCESS"
	EventContractDeployFailed  EventType = "CONTRACT_DEPLOY_FAILED"
	EventContractVerified      EventType = "CONTRACT_VERIFIED"
)
type Header struct {
	Version          uint32    // Protocol version
	Timestamp        time.Time // Block creation time
	PrevHash         []byte    // Previous block hash
	Height           uint64    // Block height
	StateRoot        []byte    // State trie root hash
	TransactionRoot  []byte    // Transaction trie root hash
	ReceiptRoot      []byte    // Receipt trie root hash
	GasLimit         uint64    // Block gas limit
	GasUsed          uint64    // Total gas used by transactions
	ValidatorAddress string    // Block producer address
}

Header represents the block header

type Mempool

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

Mempool işlem havuzunu yönetir

func NewMempool

func NewMempool(maxSize uint64) *Mempool

NewMempool yeni bir işlem havuzu oluşturur

func (*Mempool) AddTransaction

func (mp *Mempool) AddTransaction(tx *Transaction) error

AddTransaction işlemi havuza ekler

func (*Mempool) Clear

func (mp *Mempool) Clear()

Clear havuzu temizler

func (*Mempool) GetBestTransactions

func (mp *Mempool) GetBestTransactions(gasLimit uint64) []*Transaction

GetBestTransactions en yüksek öncelikli işlemleri döndürür

func (*Mempool) GetPendingNonce

func (mp *Mempool) GetPendingNonce(address string) uint64

GetPendingNonce bir adres için bekleyen en yüksek nonce değerini döndürür

func (*Mempool) GetSize

func (mp *Mempool) GetSize() uint64

GetSize havuzun mevcut boyutunu döndürür

func (*Mempool) GetTransaction

func (mp *Mempool) GetTransaction(hash []byte) *Transaction

GetTransaction belirtilen hash'e sahip işlemi döndürür

func (*Mempool) RemoveTransaction

func (mp *Mempool) RemoveTransaction(hash []byte)

RemoveTransaction işlemi havuzdan kaldırır

type PriorityQueue

type PriorityQueue []*TransactionPriority

PriorityQueue işlem öncelik kuyruğu için heap implementasyonu

func (PriorityQueue) Len

func (pq PriorityQueue) Len() int

func (PriorityQueue) Less

func (pq PriorityQueue) Less(i, j int) bool

func (*PriorityQueue) Pop

func (pq *PriorityQueue) Pop() interface{}

func (*PriorityQueue) Push

func (pq *PriorityQueue) Push(x interface{})

func (PriorityQueue) Swap

func (pq PriorityQueue) Swap(i, j int)

type Transaction

type Transaction struct {
	Hash      []byte
	From      string
	To        string
	Value     *big.Int
	Data      []byte
	GasPrice  uint64
	GasLimit  uint64
	GasUsed   uint64
	Nonce     uint64
	Signature []byte
	Status    TxStatus
}

Transaction represents a blockchain transaction

func (*Transaction) GetSize

func (tx *Transaction) GetSize() uint64

GetSize işlemin yaklaşık boyutunu hesaplar

type TransactionPriority

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

TransactionPriority işlemlerin öncelik sırasını belirler

type TxStatus

type TxStatus int

TxStatus represents the status of a transaction

const (
	TxPending TxStatus = iota
	TxSuccess
	TxFailed
)

type WebSocketServer

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

WebSocketServer handles WebSocket connections

func NewWebSocketServer

func NewWebSocketServer(bc *Blockchain) *WebSocketServer

NewWebSocketServer creates a new WebSocket server

func (*WebSocketServer) BroadcastToAll

func (s *WebSocketServer) BroadcastToAll(event Event)

BroadcastToAll sends an event to all connected clients

func (*WebSocketServer) HandleWebSocket

func (s *WebSocketServer) HandleWebSocket(w http.ResponseWriter, r *http.Request)

HandleWebSocket handles WebSocket connections

Jump to

Keyboard shortcuts

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