Documentation ¶
Overview ¶
Package database handles all the lower level support for maintaining the blockchain in storage and maintaining an in-memory databse 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) 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
- func (db *Database) LatestBlock() Block
- func (db *Database) Query(accountID AccountID) (Account, error)
- func (db *Database) Remove(accountID AccountID)
- func (db *Database) Reset() error
- 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 and is associated with transactions on the blockchain. This will be the last 20 bytes of the public key.
func PublicKeyToAccountID ¶
PublicKeyToAccountID converts the public key to an account value.
func ToAccountID ¶
ToAccountID converts a hex-encoded string to an account and validates the hex-encoded string is formatted correctly.
func (AccountID) IsAccountID ¶
IsAccountID verifies whether the underlying data represents a valid hex-encoded account.
type Block ¶
type Block struct { Header BlockHeader MerkleTree *merkle.Tree[BlockTx] }
Block represents a group of transactions batched together.
func POW ¶
POW constructs a new Block and performs the work to find a nonce that solves the cryptographic POW puzzel.
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.
func NewBlockData ¶
NewBlockData constructs block data from a block.
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 in the chain. 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: 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 a hash of the accounts and their balances. TransRoot string `json:"trans_root"` // Both: Represents the merkle tree root hash for the transactions in this block. 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 time the transaction was received. GasPrice uint64 `json:"gas_price"` // Ethereum: The price of one unit of gas to be paid for fees. GasUnits uint64 `json:"gas_units"` // Ethereum: The number of units of gas used for this transaction. }
BlockTx represents the transaction as it's recorded inside a block. This includes a timestamp and gas fees.
func NewBlockTx ¶
NewBlockTx constructs 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 and reads/writes the blockchain database on disk if a dbPath is provided.
func (*Database) ApplyMiningReward ¶
ApplyMiningReward gives the specififed 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 with block number 1.
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 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 set of arguments required to run POW.
type SignedTx ¶
type SignedTx struct { Tx V *big.Int `json:"v"` // Ethereum: Recovery identifier, either 29 or 30 with ardanID. R *big.Int `json:"r"` // Ethereum: First coordinate of the ECDSA signature. S *big.Int `json:"s"` // Ethereum: Second coordinate of the ECDSA signature. }
SignedTx is a signed version of the transaction. This is how clients like a wallet provide transactions for inclusion into the blockchain.
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 that is listed in the genesis file. Nonce uint64 `json:"nonce"` // Ethereum: Unique id for the transaction supplied by the user. FromID AccountID `json:"from"` // Ethereum: Account sending the transaction. Will be checked against signature. ToID AccountID `json:"to"` // Ethereum: Account receiving the benefit of the transaction. Value uint64 `json:"value"` // Ethereum: Monetary value received from this transaction. Tip uint64 `json:"tip"` // Ethereum: Tip offered by the sender as an incentive to mine this transaction. Data []byte `json:"data"` // Ethereum: Extra data related to the transaction. }
Tx is the transactional information between two parties.