Documentation
¶
Overview ¶
Package database contains blockchain models and database interfaces.
Index ¶
- Variables
- type Account
- type AccountID
- type Block
- type BlockData
- type BlockHeader
- type BlockTx
- type Database
- func (db *Database) ApplyMiningReward(block Block)
- func (db *Database) ApplyTransaction(block Block, tx BlockTx) error
- func (db *Database) Close()
- func (db *Database) Copy() map[AccountID]Account
- func (db *Database) ForEach() DatabaseIterator
- func (db *Database) GetBlock(num uint64) (Block, error)
- func (db *Database) HashState() (string, error)
- func (db *Database) LatestBlock() Block
- func (db *Database) Query(accountID AccountID) Account
- func (db *Database) Remove(accountID AccountID)
- func (db *Database) Reset() error
- func (db *Database) Update(account Account)
- func (db *Database) UpdateLatestBlock(block Block)
- func (db *Database) Write(block Block) error
- type DatabaseIterator
- type Iterator
- type SignedTx
- type Storage
- type Tx
Constants ¶
This section is empty.
Variables ¶
var ErrChainForked = errors.New("blockchain forked, start resync")
ErrChainForked signals a fork when another chain is 2+ blocks ahead.
Functions ¶
This section is empty.
Types ¶
type AccountID ¶
type AccountID string
AccountID represents an account/wallet identifier on the blockchain. The ID is hex-encoded string of the last 20 bytes of the account public key.
func AccountIDFromHex ¶
AccountIDFromHex takes a hex-encoded account ID, validates it and returns an AccountID.
func AccountIDFromPubKey ¶
AccountIDFromPubKey converts the public key to AccountID.
type Block ¶
type Block struct {
Header BlockHeader
MerkleTree *merkle.Tree[BlockTx]
}
Block represents a blockchain block.
func POW ¶
func POW( ctx context.Context, beneficiaryID AccountID, difficulty uint16, miningReward uint64, prevBlock Block, stateRoot string, txs []BlockTx, ) ( Block, error, )
POW mines a block by finding a valid solution.
type BlockData ¶
type BlockData struct {
Hash string `json:"hash"`
Header BlockHeader `json:"block"`
Trans []BlockTx `json:"trans"`
}
BlockData is a serializable block for network/disk.
func NewBlockData ¶
NewBlockData builds BlockData from a Block.
type BlockHeader ¶
type BlockHeader struct {
Number uint64 `json:"number"` // Block number.
PrevBlockHash string `json:"prev_block_hash"` // Previous block hash.
Timestamp uint64 `json:"timestamp"` // Block timestamp.
BeneficiaryID AccountID `json:"beneficiary"` // Fee/tip recipient.
Difficulty uint16 `json:"difficulty"` // Mining difficulty.
MiningReward uint64 `json:"mining_reward"` // Mining reward.
StateRoot string `json:"state_root"` // Accounts state Merkle root.
TransRoot string `json:"trans_root"` // Transactions Merkle root.
Solution uint64 `json:"solution"` // The mined block solution.
}
BlockHeader holds block metadata.
type BlockTx ¶
type BlockTx struct {
SignedTx
// The timestamp of when the transaction was received.
// Format: Unix timestamp in milliseconds.
Timestamp uint64 `json:"timestamp"`
// The price of one unit of gas.
GasPrice uint64 `json:"gas_price"`
// The number of units used in this transaction.
GasUnits uint64 `json:"gas_units"`
}
BlockTx represents a transaction inside a block.
func NewBlockTx ¶
NewBlockTx constructs a new block transaction.
func (BlockTx) Equals ¶
Equals implements the Merkle Hashable interface. It provides an equality check between two block transactions.
type Database ¶
type Database struct {
// contains filtered or unexported fields
}
Database manages the blockchain state.
func (*Database) ApplyMiningReward ¶
ApplyMiningReward credits the beneficiary with the mining reward.
func (*Database) ApplyTransaction ¶
ApplyTransaction applies a transaction to the database and does the accounting.
func (*Database) ForEach ¶
func (db *Database) ForEach() DatabaseIterator
ForEach returns an iterator that walks over the database blockchain starting from the first entry.
func (*Database) HashState ¶
HashState returns a hash of the current accounts state. The hash is computed from a sorted JSON representation of accounts.
func (*Database) LatestBlock ¶
LatestBlock returns the latest block.
func (*Database) Query ¶
Query returns the account for the given accountID. Returns an empty account if not found.
func (*Database) UpdateLatestBlock ¶
UpdateLatestBlock safely updates the latest block.
type DatabaseIterator ¶
type DatabaseIterator struct {
// contains filtered or unexported fields
}
DatabaseIterator provides support for iterating over the blockchain.
func (*DatabaseIterator) Done ¶
func (dbIter *DatabaseIterator) Done() bool
Done returns true if the iterator has reached the end of the blockchain.
func (*DatabaseIterator) Next ¶
func (dbIter *DatabaseIterator) Next() (Block, error)
Next returns the next block in the blockchain.
type Iterator ¶
Iterator represents a storage block iterator, it provides support to iterate over the blockchain blocks.
type SignedTx ¶
SignedTx is a signed transaction. This is the required format for clients and wallets to submit a transaction to the blockchain.
type Storage ¶
type Storage interface {
WriteBlock(blockData BlockData) error
GetBlock(num uint64) (BlockData, error)
ForEach() Iterator
Close() error
Reset() error
}
Storage represents a blockchain database storage interface.
type Tx ¶
type Tx struct {
// ChainID represents a unique identifier for this blockchain.
// It is used to run multiple blockchains in parallel
// without conflicting with each other.
//
// Current SPEC:
// ChainID 0: Development
// ChainID 1: Production
ChainID uint16 `json:"chain_id"`
// Nonce represents a unique identifier for this transaction.
// Currently it's an incremental number representing the transaction number.
Nonce uint64 `json:"nonce"`
From AccountID `json:"from"`
To AccountID `json:"to"`
Amount uint64 `json:"amount"`
// Tip is offered by the sender as an incentive to mine this transaction.
Tip uint64 `json:"tip"`
// Optional data attached to this transaction.
Data []byte `json:"data"`
}
Tx represents a transaction between two parties.
func NewTx ¶
func NewTx( chainID uint16, nonce uint64, from AccountID, to AccountID, amount uint64, tip uint64, data []byte, ) ( Tx, error, )
NewTx constructs a new transaction.