Documentation ¶
Overview ¶
Package core implements core blockchain logic and datatypes.
Index ¶
- Constants
- Variables
- func ComputeDataHash(txx []*Transaction) (crypto.Hash, error)
- type Account
- type Block
- func (b *Block) AddTx(tx *Transaction) error
- func (b *Block) AddTxx(txx []*Transaction) error
- func (b *Block) Decode(dec Decoder[*Block]) error
- func (b *Block) Encode(enc Encoder[*Block]) error
- func (b *Block) HeaderHash(hasher Hasher[*Header]) crypto.Hash
- func (b *Block) InvalidateHeaderHash()
- func (b *Block) Sign(privKey crypto.PrivateKey) error
- func (b *Block) Signer() (crypto.PublicKey, error)
- func (b *Block) VerifyData() error
- type BlockHasher
- type Decoder
- type Encoder
- type GobBlockDecoder
- type GobBlockEncoder
- type GobTxDecoder
- type GobTxEncoder
- type Hasher
- type Header
- type LedgerState
- type Transaction
- func (tx *Transaction) Decode(dec Decoder[*Transaction]) error
- func (tx *Transaction) Encode(enc Encoder[*Transaction]) error
- func (tx *Transaction) Hash(hasher Hasher[*Transaction]) crypto.Hash
- func (tx *Transaction) InvalidateHash()
- func (tx *Transaction) Sign(privKey crypto.PrivateKey) error
- func (tx *Transaction) Signer() (crypto.PublicKey, error)
- type TxHasher
Constants ¶
const PROTOCOL_VERSION = 1
PROTOCOL_VERSION represents the version of the Block format.
Variables ¶
var (
BlockMissingSignature = errors.New("The verified block has no signature.")
)
var (
TxMissingSignature = errors.New("The verified transaction has no signature.")
)
Functions ¶
func ComputeDataHash ¶
func ComputeDataHash(txx []*Transaction) (crypto.Hash, error)
ComputeDataHash computes the Hash of all the Block Transactions.
Types ¶
type Block ¶
type Block struct { *Header Transactions []*Transaction Signature crypto.Signature // contains filtered or unexported fields }
A Block contains a set of Transactions and the Signature of the Validator.
func NewBlock ¶
func NewBlock(h *Header, txx []*Transaction) (*Block, error)
NewBlock returns a pointer to a Block given a complete Header and a slice of Transactions.
func NewBlockFromPrevHeader ¶
func NewBlockFromPrevHeader(prevHeader *Header, txx []*Transaction) (*Block, error)
NewBlockFromPrevHeader returns a Block initialized with the metadatas of the parent Block.
func (*Block) AddTx ¶
func (b *Block) AddTx(tx *Transaction) error
AddTx adds a single Transaction to the Block and recompute the DataHash. This function invalidates the Block Hash cached.
func (*Block) AddTxx ¶
func (b *Block) AddTxx(txx []*Transaction) error
AddTx adds multiple Transactions to the Block and recompute the DataHash. This function invalidates the Block Hash cached.
func (*Block) HeaderHash ¶
HeaderHash returns the Block Header Hash computed using the Hasher. It uses a cache and only recomputes the Hash if it is unset or was invalidated. Methods that mutates the Block should invalidate the Hash using InvalidateHash.
func (*Block) InvalidateHeaderHash ¶
func (b *Block) InvalidateHeaderHash()
InvalidateHash invalidates the Block Hash cache.
func (*Block) Sign ¶
func (b *Block) Sign(privKey crypto.PrivateKey) error
Sign computes the signature of the HeaderHash which certifies the content of the Block.
func (*Block) VerifyData ¶
VerifyData checks that the Block Transactions hash is matching the Header DataHash.
type BlockHasher ¶
type BlockHasher struct{}
BlockHasher implements the Hasher interface for Block Header.
type GobBlockDecoder ¶
type GobBlockDecoder struct {
// contains filtered or unexported fields
}
GobBlockDecoder implements Decoder for Block using encoding/gob.
func NewGobBlockDecoder ¶
func NewGobBlockDecoder(r io.Reader) *GobBlockDecoder
NewGobBlockDecoder returns a pointer to a GobBlockDecoder given an io.Reader.
func (*GobBlockDecoder) Decode ¶
func (dec *GobBlockDecoder) Decode(b *Block) error
Decode reads the gob encoding in io.Reader r in Block b.
type GobBlockEncoder ¶
type GobBlockEncoder struct {
// contains filtered or unexported fields
}
GobTxEncoder implements Encoder for Block using encoding/gob.
func NewGobBlockEncoder ¶
func NewGobBlockEncoder(w io.Writer) *GobBlockEncoder
NewGobBlockEncoder returns a pointer to a GobBlockEncoder given an io.Writer.
func (*GobBlockEncoder) Encode ¶
func (enc *GobBlockEncoder) Encode(b *Block) error
Encode writes the gob encoding of Block b in the io.Writer w.
type GobTxDecoder ¶
type GobTxDecoder struct {
// contains filtered or unexported fields
}
GobTxDecoder implements Decoder for Transaction using encoding/gob.
func NewGobTxDecoder ¶
func NewGobTxDecoder(r io.Reader) *GobTxDecoder
NewGobTxDecoder returns a pointer to a GobTxDecoder given an io.Reader.
func (*GobTxDecoder) Decode ¶
func (e *GobTxDecoder) Decode(tx *Transaction) error
Decode reads the gob encoding in io.Reader r in Transaction tx.
type GobTxEncoder ¶
type GobTxEncoder struct {
// contains filtered or unexported fields
}
GobTxEncoder implements Encoder for Transaction using encoding/gob.
func NewGobTxEncoder ¶
func NewGobTxEncoder(w io.Writer) *GobTxEncoder
NewGobTxEncoder returns a pointer to a GobTxEncoder given an io.Writer.
func (*GobTxEncoder) Encode ¶
func (e *GobTxEncoder) Encode(tx *Transaction) error
Encode writes the gob encoding of Transaction in the io.Writer w.
type Header ¶
type Header struct { Version uint32 DataHash crypto.Hash PrevBlockHash crypto.Hash Height uint32 Timestamp int64 }
A Header is storing a Block metadatas.
type LedgerState ¶
type LedgerState struct {
// contains filtered or unexported fields
}
The LedgerState is the datastructure storing and managing all Accounts.
func NewLedgerState ¶
func NewLedgerState() *LedgerState
NewLedgerState initializes the LedgerState.
func (*LedgerState) CreateAccount ¶
func (ls *LedgerState) CreateAccount(address crypto.Address) *Account
CreateAccount create a new Account in the LedgerState from an Address.
func (*LedgerState) GetAccount ¶
func (s *LedgerState) GetAccount(address crypto.Address) (*Account, error)
GetAccount returns the Account matching an Address in the LedgerState.
func (*LedgerState) GetBalance ¶
func (ls *LedgerState) GetBalance(address crypto.Address) (uint64, error)
GetBalance returns the Balance in the LedgerState for an Address.
type Transaction ¶
type Transaction struct { Data []byte To crypto.Address Value uint64 Signature crypto.Signature Nonce int64 // contains filtered or unexported fields }
A Transaction is the object consumed for every data or value modification in the Blockchain. A Transaction should be signed by the From sender and have the To receiver PublicKey.
func NewTransaction ¶
func NewTransaction(data []byte, to crypto.Address, value uint64) *Transaction
NewTransaction returns a Transaction with a random Nonce.
func (*Transaction) Decode ¶
func (tx *Transaction) Decode(dec Decoder[*Transaction]) error
Decode the Decoder into the Transaction.
func (*Transaction) Encode ¶
func (tx *Transaction) Encode(enc Encoder[*Transaction]) error
Encode the Transaction into the Encoder.
func (*Transaction) Hash ¶
func (tx *Transaction) Hash(hasher Hasher[*Transaction]) crypto.Hash
Hash returns the Transaction Hash computed using the Hasher. It uses a cache and only recomputes the Hash if it is unset or was invalidated. Methods that mutates the Transaction should invalidate the Hash using InvalidateHash.
func (*Transaction) InvalidateHash ¶
func (tx *Transaction) InvalidateHash()
InvalidateHash invalidates the Transaction Hash cache.
func (*Transaction) Sign ¶
func (tx *Transaction) Sign(privKey crypto.PrivateKey) error
Sign a Transaction by signing the Transaction Hash and set the From field.