core

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2021 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const INDEX_BLOCK_HASH = "block_hash"
View Source
const INDEX_BLOCK_NUMBER = "block_no"
View Source
const MINING_ALGO_POW = "pow"

Variables

This section is empty.

Functions

func GetBlocksDbPath

func GetBlocksDbPath() string

func GetDataDir

func GetDataDir() string

func GetGenesisFilePath

func GetGenesisFilePath() string

func InitFS

func InitFS() error

func InitGenesis

func InitGenesis() error

func SetConfig

func SetConfig(config CoreConfig)

Types

type Block

type Block struct {
	Header       BlockHeader   `json:"header"`
	Transactions []Transaction `json:"transactions"`
	Data         []byte        `json:"data,omitempty"`
}

func NewBlock

func NewBlock(
	parentHash common.Hash,
	number uint64,
	time uint64,
	nonce uint64,
	miner common.Address,
	miningAlgo string,
	reward uint64,
	transactions []Transaction,
	data []byte,
) Block

func (Block) Hash

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

func (Block) IsBlockHashValid

func (b Block) IsBlockHashValid() (bool, error)

func (Block) IsEmpty

func (b Block) IsEmpty() bool

type BlockConfig

type BlockConfig struct {
	Reward     uint64 `yaml:"reward,omitempty"`
	Complexity uint64 `yaml:"complexity,omitempty"`
}

type BlockFS

type BlockFS struct {
	Hash  common.Hash `json:"hash"`
	Block Block       `json:"block"`
}

type BlockHeader

type BlockHeader struct {
	ParentHash       common.Hash    `json:"parentHash"`
	Timestamp        uint64         `json:"timestamp"`
	Number           uint64         `json:"number"`
	Nonce            uint64         `json:"nonce"`
	Miner            common.Address `json:"miner"`
	MiningAlgo       string         `json:"miningAlgo"`
	MiningComplexity uint64         `json:"miningComplexity"`
	Reward           uint64         `json:"reward"`
}

type CoreConfig

type CoreConfig struct {
	State StateConfig `yaml:"state,omitempty"`
	Block BlockConfig `yaml:"block,omitempty"`
}
var Config CoreConfig = Defaults()

func Defaults

func Defaults() CoreConfig

type FsProvider

type FsProvider interface {
	InitFS() error
	GetDataDir() string
	GetGenesisFilePath() string
	GetBlocksDbPath() string
}

type Genesis

type Genesis struct {
	Timestamp uint64                  `json:"timestamp"`
	Config    GenesisConfig           `json:"config"`
	Balances  map[common.Address]uint `json:"balances"`
}

func (Genesis) Hash

func (g Genesis) Hash() (common.Hash, error)

type GenesisConfig

type GenesisConfig struct {
	ChainId string `json:"chainId"`
}

type State

type State struct {
	Balances map[common.Address]uint
	// contains filtered or unexported fields
}

func LoadState

func LoadState() (*State, error)

1. Initializes empty state 2. Load Genesis block 3. Loads the state from local db

func NewState

func NewState() (*State, error)

func (*State) AddBlock

func (s *State) AddBlock(block Block) (common.Hash, error)

func (*State) AddTransaction

func (s *State) AddTransaction(tx Transaction) error

func (*State) Close

func (s *State) Close()

func (*State) Copy

func (s *State) Copy() State

func (*State) GetBlock

func (s *State) GetBlock(blockHash common.Hash) (BlockFS, error)

func (*State) GetBlockWithHashBytes

func (s *State) GetBlockWithHashBytes(hashBytes []byte) (BlockFS, error)

func (*State) GetBlocksAfter

func (s *State) GetBlocksAfter(hash common.Hash) ([]Block, error)

TODO : this needs to be fixed and changed to query from leveldb

func (*State) LatestBlock

func (s *State) LatestBlock() Block

func (*State) LatestBlockHash

func (s *State) LatestBlockHash() common.Hash

func (*State) NextBlockNumber

func (s *State) NextBlockNumber() uint64

func (*State) ValidateTxn

func (s *State) ValidateTxn(tx Transaction) error

type StateConfig

type StateConfig struct {
	DbFile string `yaml:"dbfile,omitempty"`
}

type Transaction

type Transaction struct {
	TxnContent TransactionContent `json:"txnContent"`
	TxnHash    common.Hash        `json:"txnHash"`
	Signature  common.Signature   `json:"signature"`
}

func NewTransaction

func NewTransaction(from common.Address, to common.Address, value uint, data string) Transaction

func (*Transaction) Cost

func (t *Transaction) Cost() uint

func (*Transaction) Data

func (t *Transaction) Data() string

func (*Transaction) Encode

func (t *Transaction) Encode() ([]byte, error)

func (*Transaction) Fee

func (t *Transaction) Fee() uint

func (*Transaction) From

func (t *Transaction) From() common.Address

func (*Transaction) Hash

func (t *Transaction) Hash() common.Hash

func (*Transaction) IsAuthentic

func (t *Transaction) IsAuthentic() (bool, error)

func (*Transaction) Sign

func (t *Transaction) Sign() error

func (*Transaction) To

func (t *Transaction) To() common.Address

func (*Transaction) Value

func (t *Transaction) Value() uint

func (*Transaction) WithSignature

func (t *Transaction) WithSignature(signature common.Signature)

type TransactionContent

type TransactionContent struct {
	TxnData   TransactionData `json:"txnData"`
	TxnFee    uint            `json:"txnFee"`
	Timestamp uint64          `json:"timestamp"`
}

type TransactionData

type TransactionData struct {
	From  common.Address `json:"from"`
	To    common.Address `json:"to"`
	Value uint           `json:"value"`
	Data  string         `json:"data"`
}

Jump to

Keyboard shortcuts

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