core

package
v0.0.0-...-50d17a8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2022 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package core implements core blockchain logic and datatypes.

Index

Constants

View Source
const PROTOCOL_VERSION = 1

PROTOCOL_VERSION represents the version of the Block format.

Variables

View Source
var (
	BlockMissingSignature = errors.New("The verified block has no signature.")
)
View Source
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 Account

type Account struct {
	Address crypto.Address
	Balance uint64
}

An Account is an entry in the LedgerState.

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) Decode

func (b *Block) Decode(dec Decoder[*Block]) error

Decode the Decoder into the Block.

func (*Block) Encode

func (b *Block) Encode(enc Encoder[*Block]) error

Encode the Block into the Encoder.

func (*Block) HeaderHash

func (b *Block) HeaderHash(hasher Hasher[*Header]) crypto.Hash

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) Signer

func (b *Block) Signer() (crypto.PublicKey, error)

Signer returns the PublicKey of the Block Signature signer.

func (*Block) VerifyData

func (b *Block) VerifyData() error

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.

func (BlockHasher) Hash

func (BlockHasher) Hash(b *Header) crypto.Hash

Hash returns a Block Header Hash computed using blake2b 256bits.

type Decoder

type Decoder[T any] interface {
	Decode(T) error
}

A Decoder is used to decode objects of type T.

type Encoder

type Encoder[T any] interface {
	Encode(T) error
}

An Encoder is used to encode objects of type T.

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 Hasher

type Hasher[T any] interface {
	Hash(T) crypto.Hash
}

A Hasher is used to compute Hash objects for a type T.

type Header struct {
	Version       uint32
	DataHash      crypto.Hash
	PrevBlockHash crypto.Hash
	Height        uint32
	Timestamp     int64
}

A Header is storing a Block metadatas.

func (*Header) Bytes

func (h *Header) Bytes() []byte

Bytes returns the byte slice representation of the Header.

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.

func (*LedgerState) Transfer

func (ls *LedgerState) Transfer(from, to crypto.Address, amount uint64) error

Transfer transfers a funds amount from one Address to another.

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.

func (*Transaction) Signer

func (tx *Transaction) Signer() (crypto.PublicKey, error)

Signer returns the PublicKey of the Transaction signer.

type TxHasher

type TxHasher struct{}

TxHasher implements the Hasher interface for Transaction.

func (TxHasher) Hash

func (TxHasher) Hash(tx *Transaction) crypto.Hash

Hash returns a Transaction Hash computed using blake2b 256bits.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL