core

package
v0.0.0-...-e39e356 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const BlockReward = 100
View Source
const TxFee = uint(50)
View Source
const TxGas = 1
View Source
const TxGasPriceDefault = 1

Variables

This section is empty.

Functions

func ApplyTx

func ApplyTx(tx SignedTx, s *State) error

func InitDataDirIfNotExists

func InitDataDirIfNotExists(dataDir string, genesis []byte) error

func IsBlockHashValid

func IsBlockHashValid(hash Hash, miningDifficulty uint) bool

func NewAccount

func NewAccount(value string) common.Address

func ValidateTx

func ValidateTx(tx SignedTx, s *State) error

Types

type Block

type Block struct {
	Header BlockHeader `json:"header"`
	Txs    []SignedTx  `json:"payload"`
}

func GetBlockAfter

func GetBlockAfter(blockHash Hash, dataDir string) ([]Block, error)

func NewBlock

func NewBlock(parent Hash,
	number uint64, nonce uint32,
	time uint64, miner common.Address,
	txs []SignedTx) Block

func (Block) GasReward

func (b Block) GasReward() uint

func (Block) Hash

func (b Block) Hash() (Hash, error)

type BlockFS

type BlockFS struct {
	Key   Hash  `json:"key"`
	Value Block `json:"hash"`
}

func GetBlockByHeightOrHash

func GetBlockByHeightOrHash(state *State, height uint64, hash, dataDir string) (BlockFS, error)

GetBlocksByHeightOrHash returns the requested block by height or hash It uses cached data in the State struct (HeightCache / HashCache)

type BlockHeader

type BlockHeader struct {
	Parent Hash           `json:"parent"`
	Number uint64         `json:"number"`
	Nonce  uint32         `json:"nonce"`
	Time   uint64         `json:"time"`
	Miner  common.Address `json:"miner"`
}

type Genesis

type Genesis struct {
	Balances map[common.Address]uint `json:"balances"`
	Symbol   string                  `json:"symbol"`
}

type Hash

type Hash [32]byte

func (Hash) Hex

func (h Hash) Hex() string

func (Hash) IsEmpty

func (h Hash) IsEmpty() bool

func (Hash) MarshalText

func (h Hash) MarshalText() ([]byte, error)

func (*Hash) UnmarshalText

func (h *Hash) UnmarshalText(data []byte) error

type SignedTx

type SignedTx struct {
	Tx
	Sig []byte `json:"signature"`
}

func NewSignedTx

func NewSignedTx(tx Tx, sig []byte) SignedTx

func (SignedTx) Hash

func (t SignedTx) Hash() (Hash, error)

func (SignedTx) IsAuthentic

func (t SignedTx) IsAuthentic() (bool, error)

func (SignedTx) MarshalJSON

func (t SignedTx) MarshalJSON() ([]byte, error)

type State

type State struct {
	Balances       map[common.Address]uint
	AccountToNonce map[common.Address]uint

	HashCache   map[string]int64
	HeightCache map[uint64]int64
	// contains filtered or unexported fields
}

func NewStateFromDisk

func NewStateFromDisk(dataDir string, miningDifficulty uint) (*State, error)

func (*State) AddBlock

func (s *State) AddBlock(b Block) (Hash, error)

func (*State) AddBlocks

func (s *State) AddBlocks(blocks []Block) error

func (*State) ChangeMiningDifficulty

func (s *State) ChangeMiningDifficulty(newDifficulty uint)

func (*State) Close

func (s *State) Close() error

func (*State) Copy

func (s *State) Copy() State

func (*State) GetNextAccountNonce

func (s *State) GetNextAccountNonce(account common.Address) uint

func (*State) LatestBlock

func (s *State) LatestBlock() Block

func (*State) LatestBlockHash

func (s *State) LatestBlockHash() Hash

func (*State) NextBlockNumber

func (s *State) NextBlockNumber() uint64

type Tx

type Tx struct {
	From     common.Address `json:"from"`
	To       common.Address `json:"to"`
	Gas      uint           `json:"gas"`
	GasPrice uint           `json:"gas_price"`
	Value    uint           `json:"value"`
	Nonce    uint           `json:"nonce"`
	Data     string         `json:"data"`
	Time     uint64         `json:"time"`
}

func NewBaseTx

func NewBaseTx(from, to common.Address, value, nonce uint, data string) Tx

func NewTx

func NewTx(from, to common.Address, gas uint, gasPrice uint, value, nonce uint, data string) Tx

func (Tx) Cost

func (tx Tx) Cost() uint

func (Tx) Encode

func (tx Tx) Encode() ([]byte, error)

func (Tx) GasCost

func (tx Tx) GasCost() uint

func (Tx) Hash

func (tx Tx) Hash() (Hash, error)

func (Tx) IsReward

func (tx Tx) IsReward() bool

func (Tx) MarshalJSON

func (t Tx) MarshalJSON() ([]byte, error)

MarshalJson is the source of truth when it comes to encoding a transactionfor hash calculations

Jump to

Keyboard shortcuts

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