Documentation ¶
Overview ¶
Package database handles all lower level support for maintaining the blockchain database. This includes the management of an in-memory database of account information.
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) Copy() map[AccountID]Account
- func (db *Database) ForEach() DatabaseIterator
- func (db *Database) GetBlock(num uint64) (Block, error)
- func (db *Database) HashState() string
- func (db *Database) LatestBlock() Block
- func (db *Database) Query(accountID AccountID) (Account, error)
- func (db *Database) Remove(accountID AccountID)
- func (db *Database) UpdateLatestBlock(block Block)
- func (db *Database) Write(block Block) error
- type DatabaseIterator
- type Iterator
- type POWArgs
- type SignedTx
- type Storage
- type Tx
Constants ¶
This section is empty.
Variables ¶
var ErrChainForked = errors.New("blockchain forked, start resync")
ErrChainForked is returned from validateNextBlock if another node's chain is two or more blocks ahead of ours.
Functions ¶
This section is empty.
Types ¶
type AccountID ¶
type AccountID string
AccountID represents an account ID that is used to sign transactions. It is associated with transactions on the blockchain. This is the last 20 bytes of the hash of the public key.
func PublicKeyToAccountID ¶
PublicKeyToAccountID converts a public key to an account ID.
func ToAccountID ¶
ToAccountID converts a hex-encoded string to an account ID and validates the string is formatted correctly.
func (AccountID) IsAccountID ¶
IsAccountID returns true if the account ID is valid.
type Block ¶
type Block struct { Header BlockHeader MerkleTree *merkle.Tree[BlockTx] }
Block represents a group of transactions bundled together.
func POW ¶
POW constructs a new Block and performs the work to find a nonce that solves the cryptographic hash puzzle.
type BlockData ¶
type BlockData struct { Hash string `json:"hash"` Header BlockHeader `json:"block"` Trans []BlockTx `json:"trans"` }
BlockData represents what can be serialized to disk and over the network.
type BlockHeader ¶
type BlockHeader struct { Number uint64 `json:"number"` // Ethereum: Block number in the chain. PrevBlockHash string `json:"prev_block_hash"` // Bitcoin: Hash of the previous block. TimeStamp uint64 `json:"timestamp"` // Bitcoin: Time the block was mined. BeneficiaryID AccountID `json:"beneficiary"` // Ethereum: The account who is receiving fees and tips. Difficulty uint16 `json:"difficulty"` // Ethereum: The number of 0's needed to solve the hash solution. MiningReward uint64 `json:"mining_reward"` // Ethereum: The reward for mining this block. StateRoot string `json:"state_root"` // Ethereum: Represents the hash of the accounts and their balances. TransRoot string `json:"trans_root"` // Both: Represents the merkle root hash for the transactions. Nonce uint64 `json:"nonce"` // Both: Value identified to solve the hash solution. }
BlockHeader represents common information required for each block.
type BlockTx ¶
type BlockTx struct { SignedTx TimeStamp uint64 `json:"timestamp"` // Ethereum: The timestamp of the block. GasPrice uint64 `json:"gas_price"` // Ethereum: The gas price in the block. GasUnits uint64 `json:"gas_units"` // Ethereum: The gas units in the block. }
BlockTx represents a transaction in a block, which includes the timestamp and gas fees.
func NewBlockTx ¶
NewBlockTx creates a new block transaction.
type Database ¶
type Database struct {
// contains filtered or unexported fields
}
Database manages data related to accounts who have transacted on the blockchain.
func New ¶
func New(genesis genesis.Genesis, storage Storage, evHandler func(v string, args ...any)) (*Database, error)
New constructs a new database and applies account genesis information. It reads/writes the blockchain database on disk if a dbPath is provided.
func (*Database) ApplyMiningReward ¶
ApplyMiningReward gives the specified account the mining reward.
func (*Database) ApplyTransaction ¶
ApplyTransaction performs the business logic for applying a transaction to the database.
func (*Database) ForEach ¶
func (db *Database) ForEach() DatabaseIterator
ForEach returns an iterator to walk through all the blocks starting from the genesis block.
func (*Database) GetBlock ¶
GetBlock searches the blockchain on disk to locate and return the contents of the specified block by number.
func (*Database) HashState ¶
HashState returns a hash based on the contents of the accounts and their balances. This is added to each block and checked by peers.
func (*Database) LatestBlock ¶
LatestBlock returns the latest block.
func (*Database) UpdateLatestBlock ¶
UpdateLatestBlock provides safe access to update the latest block.
type DatabaseIterator ¶
type DatabaseIterator struct {
// contains filtered or unexported fields
}
DatabaseIterator provides support for iterating over the blocks in the blockchain database using the configured storage option.
func (*DatabaseIterator) Done ¶
func (di *DatabaseIterator) Done() bool
Done returns the end of the chain value.
func (*DatabaseIterator) Next ¶
func (di *DatabaseIterator) Next() (Block, error)
Next retrieves the next block from disk.
type Iterator ¶
Iterator interface represents the behavior required to be implemented by any package providing support to iterate over the blocks.
type POWArgs ¶
type POWArgs struct { BeneficiaryID AccountID Difficulty uint16 MiningReward uint64 PrevBlock Block StateRoot string Trans []BlockTx EvHandler func(v string, args ...any) }
POWArgs represents the arguments required to solve the proof of work.
type SignedTx ¶
type SignedTx struct { Tx V *big.Int `json:"v"` // Ethereum: The recovery ID. R *big.Int `json:"r"` // Ethereum: The first 32 bytes of the ECDSA signature. S *big.Int `json:"s"` // Ethereum: The second 32 bytes of the ECDSA signature. }
SignedTx represents a signed transaction.
func (SignedTx) SignatureString ¶
SignatureString returns the signature as a string.
type Storage ¶
type Storage interface { Write(blockData BlockData) error GetBlock(num uint64) (BlockData, error) ForEach() Iterator Close() error Reset() error }
Storage interface represents the behavior required to be implemented by any package providing support for reading and writing the blockchain.
type Tx ¶
type Tx struct { ChainID uint16 `json:"chain_id"` // Ethereum: The chain ID in the genesis file. FromID AccountID `json:"from_id"` // Ethereum: The transaction sender. ToID AccountID `json:"to_id"` // Ethereum: The transaction recipient. Value uint64 `json:"value"` // Ethereum: The unit amount to transfer. Nonce uint64 `json:"nonce"` // Ethereum: Unique number for the transaction. Tip uint64 `json:"tip"` // Ethereum: The unit amount to tip the miner. Data []byte `json:"data"` // Ethereum: The input data for the transaction. }
Tx represents a transaction.