database

package
v0.0.0-...-7ccffc4 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2023 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const BlockReward = 100
View Source
const TxFee = uint(50)

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 GetBlocksAfter

func GetBlocksAfter(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) Hash

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

type BlockFS

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

func GetBlockByHeightOrHash

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

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

func GetBlockByHeightOrHashByFileName

func GetBlockByHeightOrHashByFileName(state *State, height uint64, hash, filename string) (BlockFS, error)

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
	Account2Nonce map[common.Address]uint

	// position of block in file db
	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) 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) GetBlocks

func (s *State) GetBlocks() ([]Block, error)

func (*State) GetForkedBlock

func (s *State) GetForkedBlock(peerBlocks []Block) (Block, error)

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

func (*State) RemoveBlocks

func (s *State) RemoveBlocks(fromBlock Block) error

type Tx

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

func NewTx

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

func (Tx) Cost

func (t Tx) Cost() uint

func (Tx) Encode

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

func (Tx) Hash

func (t Tx) Hash() (Hash, error)

func (Tx) IsReward

func (t Tx) IsReward() bool

func (Tx) MarshalJSON

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

Jump to

Keyboard shortcuts

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