Documentation
¶
Index ¶
- Constants
- Variables
- func CheckKeys(pub, priv string)
- func CompareHash(hash, target string) bool
- func ValidateNonce(txns []Transaction, lastNonce int64) (curentNonce int64, err error)
- func ValidateTransactionGroup(currentBalance int64, currentNonce int64, txns []Transaction) error
- type Block
- func (b *Block) CalculateHash() string
- func (b Block) DeepCopy() Block
- func (b *Block) GetTimestampTime() time.Time
- func (b *Block) MineWithWorkers(ctx context.Context, target string, workers int, solutionChan chan Block, ...)
- func (b *Block) RandomizeNonce()
- func (b *Block) RotateMiningValues() (hash string)
- func (b *Block) Serialize() []byte
- func (b *Block) SetStampNow()
- func (b *Block) SignHash()
- func (b Block) StartSolutionWorker(ctx context.Context, target string, solutionChan chan Block, ...)
- func (b *Block) ValidateHash() bool
- type BlockTimeDifficulty
- type BottleNeckQueue
- type Broombase
- func (bb *Broombase) AddAndSync(block *Block) error
- func (bb *Broombase) GetBlock(hash string, height int64) (block Block, found bool)
- func (bb *Broombase) GetFirstBlockByHeight(height int) (string, bool)
- func (bb *Broombase) GetHighestBlock() (height int64, hash string, err error)
- func (bb *Broombase) GetLedgerAt(hash string, height int64) (*Ledger, bool)
- func (bb *Broombase) ReceiveBlock(block Block) error
- func (bb *Broombase) StoreBlock(block Block) error
- func (bb *Broombase) StoreGivenLedger(ledger *Ledger) error
- func (bb *Broombase) StoreLedger(hash string, height int64) error
- func (bb *Broombase) SyncLedger(blockHeight int64, blockHash string) error
- func (bb *Broombase) ValidateBlockHeaders(block *Block) error
- type Executor
- func (ex *Executor) AtomicCopyMiningBlock() Block
- func (ex *Executor) BackupFromFile(filename string)
- func (ex *Executor) Cancel()
- func (ex *Executor) DownloadBackupFileFromPeer(peerAddress string)
- func (ex *Executor) EnableMiningPool(tax int64, privateKey string, poolNote string)
- func (ex *Executor) GetAddressTransactions(address string) []Transaction
- func (ex *Executor) GetNodeNonce(address string) int64
- func (ex *Executor) Mine(ctx context.Context, solutionChan chan Block, doneChan chan struct{}, ...)
- func (ex *Executor) NetworkSync(ctx context.Context)
- func (ex *Executor) NetworkSyncWithTracker(ctx context.Context) (syncRequired bool)
- func (ex *Executor) PayoutMiners(block Block)
- func (ex *Executor) RunBackup() error
- func (ex *Executor) RunMiningLoop(ctx context.Context, workers int)
- func (ex *Executor) RunNetworkSync(ctx context.Context) (caughtUp bool)
- func (ex *Executor) SetPort(port string)
- func (ex *Executor) SetupHttpServer()
- func (ex *Executor) Start(workers int, self string, seeds ...string)
- func (ex *Executor) ValidateIncomingPoolBlock(block Block) error
- func (ex *Executor) ValidateIncomingPoolBlockNoHash(block Block) error
- type Funnel
- type HashHeight
- type Ledger
- func (l *Ledger) Accumulate(b Block)
- func (l *Ledger) CalculateCurrentReward() int
- func (l *Ledger) CheckLedger(address string) (found bool, nonce int64, balance int64)
- func (l *Ledger) Clear()
- func (l *Ledger) GetAddressBalance(address string) (int64, bool)
- func (l *Ledger) GetAddressNonce(address string) (int64, bool)
- func (l *Ledger) GetCurrentMiningThreshold() string
- func (l *Ledger) SyncNextBlock(validatedBlock Block)
- func (l *Ledger) ValidateBlock(block Block) error
- func (l *Ledger) ValidateCoinbaseTxn(coinbaseTxn Transaction, currentBlock int64) error
- type Limiter
- type Message
- type MessageType
- type MiningPool
- func (mp *MiningPool) BackupWorkLog() error
- func (mp *MiningPool) ClearBlock(i int64)
- func (mp *MiningPool) LoadBackup() error
- func (mp *MiningPool) PayoutBlock(i int64)
- func (mp *MiningPool) PublishWorkProof(wp WorkProof)
- func (mp *MiningPool) Send(amount int64, address string, nonce int64)
- func (mp *MiningPool) StartListener()
- type Node
- func (n *Node) BalancePeers(hostNames []string)
- func (n *Node) DialPeer(address string) (*Peer, error)
- func (n *Node) DropRandomPeer() bool
- func (n *Node) GetAddressSample() []string
- func (n *Node) GossipPeers()
- func (n *Node) RacePeersForValidBlock(hash string, height int) (Block, error)
- func (n *Node) RunEgress()
- func (n *Node) SamplePeersBlock(path string, height int, hash string) (consensus Block, err error)
- func (n *Node) SamplePeersHighestBlock() (hash string, height int, err error)
- func (n *Node) Schedule(task func(), period time.Duration)
- func (n *Node) ScheduleUnbanPeer(peer string)
- func (n *Node) Seed()
- func (n *Node) StartListenIncomingMessages()
- type Peer
- type PendingBalance
- type QueueNode
- type RequestPeer
- type TempStorage
- type Transaction
- type TransactionPool
- type WorkLeaf
- type WorkProof
- type WorkTrie
Constants ¶
const ( THRESHOLD_A = "0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" THRESHOLD_B = "00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" THRESHOLD_C = "000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" THRESHOLD_POOL = "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" )
const ALPHA_FACTOR = 4
const BACKUP_DIR = "backup"
const BACKUP_FREQUENCY = 2 * 60 * 60 // every 2 hours
const BLOCK_SPEED_AVERAGE_WINDOW = 150
const BROOMBASE_DEFAULT_DIR = "broombase"
const CHANNEL_BUFFER_SIZE = 10
const COINBASE = "coinbase"
const COINBASE_VESTING_BLOCK_NUMBER = 10 // coinbase txns don't go out until a fork is hopefully resolved, these txn amounts are not spendable for this number of blocks
const DEFAULT_MINING_THRESHOLD = "0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
const EXECUTOR_WORKER_COUNT = 4
const EXPOSED_PORT = "80"
const GENESIS = "genesis"
const GENESIS_BLOCK_HEIGHT = 0
const LEDGER_DEFAULT_DIR = "ledger"
const MAX_BLOCK_SIZE = 1000
const MAX_PEER_CONNECTIONS = 200
const MINING_PAYOUT_LOOKBACK = 10
we want to resolve mining payout faster than our won blocks vest
const MINING_POOL_GOB = "mining_pool.gob"
const NO_PEERS_ERROR = "no peers found"
const PEER_CONNECTION_RETRIES = 5
const PEER_SAMPLE_SIZE = 30
const PROOF_LIMITER_RATE = time.Millisecond * 300
const PROTOCOL_MAX_SIZE = 30_000_000
const RETRY_TIME = 3 // in seconds
const STARTING_PAYOUT = 10_000
const SYNC_CHECK_DURATION = 2 * time.Minute
const TARGET_BLOCK_TIME = 60
const TCP_PORT = "4188"
Variables ¶
var GENESIS_HASH = "fd1cb7926a4447454a00a3c5c75126ad8ec053bc5ba61d706ca687428c8af5ac"
var GENESIS_TXN = Transaction{ Coinbase: false, To: "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEW7I9+SUHOW28jjABqnpO76tqwG/nCG/jMMPuUfIQryMPlCdxPwUrSP49ioqYZAf2kXrXQ7MQE891OXBTSpvlsA==", Note: "Matthew 5:16", Amount: STARTING_PAYOUT, }
var START_DELIMETER = []byte{0x01, 0x14}
Functions ¶
func CompareHash ¶
func ValidateNonce ¶
func ValidateNonce(txns []Transaction, lastNonce int64) (curentNonce int64, err error)
first txn nonce must be 1
func ValidateTransactionGroup ¶
func ValidateTransactionGroup(currentBalance int64, currentNonce int64, txns []Transaction) error
other block txns are needed to validate the current txn The current "txn to validate" can be added to the list of the rest of the txns,
Types ¶
type Block ¶
type Block struct {
Hash string `json:"hash"`
Timestamp int64 `json:"timestamp"`
Height int64 `json:"height"`
Nonce int64 `json:"nonce"`
PreviousBlockHash string `json:"previous"`
Transactions TransactionPool `json:"transactions"`
}
func (*Block) CalculateHash ¶
func (*Block) GetTimestampTime ¶
func (*Block) MineWithWorkers ¶
func (*Block) RandomizeNonce ¶
func (b *Block) RandomizeNonce()
func (*Block) RotateMiningValues ¶
func (*Block) SetStampNow ¶
func (b *Block) SetStampNow()
func (Block) StartSolutionWorker ¶
func (*Block) ValidateHash ¶
type BlockTimeDifficulty ¶
type BottleNeckQueue ¶
type BottleNeckQueue[T any] struct { // contains filtered or unexported fields }
func NewBottleNeckQueue ¶
func NewBottleNeckQueue[T any]() *BottleNeckQueue[T]
func (*BottleNeckQueue[T]) Add ¶
func (bn *BottleNeckQueue[T]) Add(value T)
func (*BottleNeckQueue[T]) Clear ¶
func (bn *BottleNeckQueue[T]) Clear()
func (*BottleNeckQueue[T]) HasNext ¶
func (bn *BottleNeckQueue[T]) HasNext() bool
func (*BottleNeckQueue[T]) Pop ¶
func (bn *BottleNeckQueue[T]) Pop() (value T, found bool)
type Broombase ¶
type Broombase struct {
Ledger *Ledger
// contains filtered or unexported fields
}
func InitBroombaseWithDir ¶
func (*Broombase) AddAndSync ¶
DOES NOT USE SNAPSHOTS ONLY USED ON GENESIS
func (*Broombase) GetFirstBlockByHeight ¶
used for testing only and copied by TempStorage
func (*Broombase) GetHighestBlock ¶
reconciles forks, does not TRACK them
func (*Broombase) GetLedgerAt ¶
func (*Broombase) ReceiveBlock ¶
not ledger safe,
func (*Broombase) StoreBlock ¶
func (*Broombase) StoreGivenLedger ¶
func (*Broombase) SyncLedger ¶
TDO: implement snapshots where we can jump to pick like n blocks ago
func (*Broombase) ValidateBlockHeaders ¶
type Executor ¶
type Executor struct {
Node *Node
Database *Broombase
MsgChan chan []byte
BlockChan chan Block
TxnChan chan Transaction
Mempool map[string]Transaction
MiningBlock *Block
MiningBlockMutex *sync.RWMutex
EgressBlockChan chan Block
EgressTxnChan chan Transaction
Port string
// Mining Pool Config
MiningPoolEnabled bool
PoolTaxPercent int64 // ex: 8 = %8
PoolNote string
PrivateKey string
Pool *MiningPool
SolutionTargetOperators map[string]func(b Block)
ProofLimiter *Limiter[WorkProof]
// contains filtered or unexported fields
}
func NewExecutor ¶
func (*Executor) AtomicCopyMiningBlock ¶
func (*Executor) BackupFromFile ¶
func (*Executor) DownloadBackupFileFromPeer ¶
func (*Executor) EnableMiningPool ¶
call this from the cli before running the node
func (*Executor) GetAddressTransactions ¶
func (ex *Executor) GetAddressTransactions(address string) []Transaction
func (*Executor) GetNodeNonce ¶
func (*Executor) NetworkSync ¶
func (*Executor) NetworkSyncWithTracker ¶
func (*Executor) PayoutMiners ¶
func (*Executor) RunMiningLoop ¶
func (*Executor) RunNetworkSync ¶
func (*Executor) SetupHttpServer ¶
func (ex *Executor) SetupHttpServer()
func (*Executor) ValidateIncomingPoolBlock ¶
func (*Executor) ValidateIncomingPoolBlockNoHash ¶
type Funnel ¶
type HashHeight ¶
type Ledger ¶
type Ledger struct {
Mut *sync.RWMutex `json:"-"`
MiningThresholdHash string `json:"miningThreshold"`
BlockHeight int64 `json:"blockHeight"`
BlockHash string `json:"blockHash"`
Balances map[string]int64 `json:"balances"` // track receiver balances
Nonces map[string]int64 `json:"nonces"` // track sender nonce values of transactions
BlockTimeDifficulty []BlockTimeDifficulty `json:"blockTime"`
Pending []PendingBalance `json:"pending"`
}
func (*Ledger) Accumulate ¶
need to remove validity checking, assumes we have a valid block
func (*Ledger) CalculateCurrentReward ¶
func (*Ledger) CheckLedger ¶
func (*Ledger) GetCurrentMiningThreshold ¶
func (*Ledger) SyncNextBlock ¶
Pass in a previously validated block, use l.ValidateBlock() CurrentLedger
func (*Ledger) ValidateBlock ¶
block must validate against current ledger TODO: Validate Mining difficulty
func (*Ledger) ValidateCoinbaseTxn ¶
func (l *Ledger) ValidateCoinbaseTxn(coinbaseTxn Transaction, currentBlock int64) error
Add halving rule, or gambling rules
type Limiter ¶
func NewLimiter ¶
type Message ¶
type Message struct {
Action MessageType `json:"action"`
HostNames []string `json:"hostnames"`
Block Block `json:"block"`
Txn Transaction `json:"txn"`
}
type MessageType ¶
type MessageType string
const ( Ping MessageType = "Ping" PeerBroadcast MessageType = "PeerBroadcast" TxnMsg MessageType = "TxnMsg" BlockMsg MessageType = "BlockMsg" )
type MiningPool ¶
type MiningPool struct {
TaxPercent int64
BlockPayout int64
NodeAddress string
NodePrivateKey string
TxnChan chan Transaction
WorkLog map[int64]WorkTrie
GetCurrentNonce func(address string) int64
PoolNote string
ListenerChan chan WorkProof
}
stores proof groups for specific heights
func InitMiningPool ¶
func InitMiningPool( taxPercent int64, blockPayout int64, nodeMiningAddress string, txnChan chan Transaction, poolNote string, privateKey string, getNonce func(address string) int64, ) *MiningPool
func (*MiningPool) BackupWorkLog ¶
func (mp *MiningPool) BackupWorkLog() error
func (*MiningPool) ClearBlock ¶
func (mp *MiningPool) ClearBlock(i int64)
func (*MiningPool) LoadBackup ¶
func (mp *MiningPool) LoadBackup() error
func (*MiningPool) PayoutBlock ¶
func (mp *MiningPool) PayoutBlock(i int64)
func (*MiningPool) PublishWorkProof ¶
func (mp *MiningPool) PublishWorkProof(wp WorkProof)
func (*MiningPool) StartListener ¶
func (mp *MiningPool) StartListener()
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
func ActivateNode ¶
func (*Node) BalancePeers ¶
func (*Node) DropRandomPeer ¶
func (*Node) GetAddressSample ¶
func (*Node) GossipPeers ¶
func (n *Node) GossipPeers()
func (*Node) RacePeersForValidBlock ¶
func (*Node) SamplePeersBlock ¶
func (*Node) SamplePeersHighestBlock ¶
func (*Node) ScheduleUnbanPeer ¶
func (*Node) StartListenIncomingMessages ¶
func (n *Node) StartListenIncomingMessages()
type Peer ¶
type Peer struct {
// contains filtered or unexported fields
}
func (*Peer) ListenProtocol ¶
type PendingBalance ¶
type QueueNode ¶
type QueueNode[T any] struct { // contains filtered or unexported fields }
func NewRootQueueNode ¶
type RequestPeer ¶
type RequestPeer struct {
// contains filtered or unexported fields
}
func (*RequestPeer) SendMsg ¶
func (r *RequestPeer) SendMsg(msg []byte) error
type TempStorage ¶
type TempStorage struct {
// contains filtered or unexported fields
}
func NewTempStorage ¶
func NewTempStorage(dir string) *TempStorage
func (*TempStorage) DeleteBlock ¶
func (ts *TempStorage) DeleteBlock(hash string, height int64) error
func (*TempStorage) GetBlock ¶
func (ts *TempStorage) GetBlock(hash string, height int64) (Block, bool)
func (*TempStorage) GetFirstBlockByHeight ¶
func (ts *TempStorage) GetFirstBlockByHeight(height int) (string, bool)
func (*TempStorage) StoreBlock ¶
func (ts *TempStorage) StoreBlock(block Block) error
type Transaction ¶
type Transaction struct {
Sig string `json:"sig"`
Coinbase bool `json:"coinbase"`
Note string `json:"note"`
Nonce int64 `json:"nonce"`
To string `json:"to"`
From string `json:"from"`
Amount int64 `json:"amount"`
}
func (*Transaction) Serialize ¶
func (t *Transaction) Serialize() []byte
func (*Transaction) Sign ¶
func (t *Transaction) Sign(privateKey string) error
func (*Transaction) ValidateSig ¶
func (t *Transaction) ValidateSig() (bool, error)
func (*Transaction) ValidateSize ¶
func (t *Transaction) ValidateSize() bool
type TransactionPool ¶
type TransactionPool map[string]Transaction