ledger

package
v0.0.0-...-180b776 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2017 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HashLength    = 32
	AddressLength = 20
)

Variables

This section is empty.

Functions

func BytesToBig

func BytesToBig(b []byte) *big.Int

* Other helpers

func PaddedBytes

func PaddedBytes(x *big.Int, n int) []byte

Returns byte representation of big int, left padded with 0's to be as long as n If length of x is larger than n, just returns n

Types

type Account

type Account struct {
	Nonce   AccountNonce
	Addr    Address
	Balance *big.Int
}

func GetAccount

func GetAccount(db meddb.Database, addr Address) (*Account, error)

Gets account object from database using account address as key

func NewAccount

func NewAccount() (*Account, *ecdsa.PrivateKey, error)

Returns an Account with a randomly generate private key/address

func (*Account) Write

func (a *Account) Write(db meddb.Database) error

Writes account object to database using account address as key

type AccountNonce

type AccountNonce BlockNonce

type Address

type Address [AddressLength]byte

func AddressFromPubKey

func AddressFromPubKey(pub []byte) Address

Uses HASH160 (SHA256 + RIPEMD160) to generate the address from given public key Makes no assumption about whether this is a compressed or uncompressed public key

func BigToAddress

func BigToAddress(i *big.Int) Address

func BytesToAddress

func BytesToAddress(b []byte) Address

* Address helper methods

func StringToAddress

func StringToAddress(s string) Address

func (Address) Big

func (address Address) Big() *big.Int

func (Address) Bytes

func (address Address) Bytes() []byte

func (*Address) SetBytes

func (address *Address) SetBytes(b []byte)

func (Address) String

func (address Address) String() string

type Block

type Block struct {
	Header *BlockHeader
	Body   *BlockBody
}

func GetBlock

func GetBlock(db meddb.Database, hash Hash, number uint64) (*Block, error)

Gets whole block from database

func GetHeadBlock

func GetHeadBlock(db meddb.Database) (*Block, error)

Gets whole head block from database

func GetOrCreateGenesisBlock

func GetOrCreateGenesisBlock(db meddb.Database) (*Block, error)

Gets and returns the genesis block if it exists in the database Otherwise, creates the genesis block, commits it to the database and returns it

func (*Block) AddTransaction

func (b *Block) AddTransaction(t *Transaction) error

Adds transaction to block body

func (*Block) Write

func (b *Block) Write(db meddb.Database) error

Writes block header and body to the database

func (*Block) WriteHead

func (b *Block) WriteHead(db meddb.Database) error

Writes block header and body to the database using the head block key

type BlockBody

type BlockBody struct {
	Transactions []*Transaction
}

func GetBlockBody

func GetBlockBody(db meddb.Database, hash Hash, number uint64) (*BlockBody, error)

func (*BlockBody) Write

func (b *BlockBody) Write(db meddb.Database, hash Hash, number uint64) error

Writes block to provided database in rlp format

type BlockHeader

type BlockHeader struct {
	ParentHash Hash
	Number     *big.Int
	Dt         *big.Int
	Nonce      BlockNonce
}

func GetBlockHeader

func GetBlockHeader(db meddb.Database, hash Hash, number uint64) (*BlockHeader, error)

Gets block header from database in rlp format, constructs object and returns

func GetHeadBlockHeader

func GetHeadBlockHeader(db meddb.Database) (*BlockHeader, error)

Gets head block header from database in rlp format, constructs objects and returns

func (*BlockHeader) Hash

func (h *BlockHeader) Hash() Hash

Returns Sha256 hash of block header in rlp format

func (*BlockHeader) Write

func (h *BlockHeader) Write(db meddb.Database) error

Writes block header to provided database in rlp format

func (*BlockHeader) WriteHead

func (h *BlockHeader) WriteHead(db meddb.Database) error

Writes head block header to provided database in rlp format

type BlockNonce

type BlockNonce [8]byte

func EncodeNonce

func EncodeNonce(i uint64) BlockNonce

* BlockNonce helper methods

func (BlockNonce) Uint64

func (nonce BlockNonce) Uint64() uint64

type Chain

type Chain struct {
	DB            meddb.Database
	Genesis       *Block
	HeadCandidate *Block
}

func NewChain

func NewChain(db meddb.Database) (*Chain, error)

Builds and returns a chain from the existing database If the chain does not yet exist, creates a chain containing only genesis

func (*Chain) AddTransaction

func (c *Chain) AddTransaction(t *Transaction) (*Transaction, error)

Adds transaction to head block in chain, gives reward to owner TODO: Basically redo when we have tables done

type Hash

type Hash [HashLength]byte

func BigToHash

func BigToHash(i *big.Int) Hash

func BytesToHash

func BytesToHash(b []byte) Hash

* Hash helper methods

func StringToHash

func StringToHash(s string) Hash

func (Hash) Big

func (hash Hash) Big() *big.Int

func (Hash) Bytes

func (hash Hash) Bytes() []byte

func (*Hash) SetBytes

func (hash *Hash) SetBytes(b []byte)

func (Hash) String

func (hash Hash) String() string

type InsufficientFundsError

type InsufficientFundsError struct {
	InputSum  *big.Int
	OutputSum *big.Int
}

Sum of transaction inputs is less than sum of transaction outputs

func (*InsufficientFundsError) Error

func (e *InsufficientFundsError) Error() string

type Ledger

type Ledger struct {
	// contains filtered or unexported fields
}

type MissingBodyError

type MissingBodyError struct {
	B *Block
}

Block is missing its body

func (*MissingBodyError) Error

func (e *MissingBodyError) Error() string

type State

type State struct {
}

type Transaction

type Transaction struct {
	AccountNonce uint64
	V            *big.Int
	R, S         *big.Int
	To           Address
	Amount       *big.Int
}

func SignTx

func SignTx(t *Transaction, priv *ecdsa.PrivateKey) (*Transaction, error)

Signs the transaction body and writes the corresponding values to V, R, S

func (*Transaction) From

func (t *Transaction) From() (Address, error)

Returns the From address for the transaction derived from the V, R, S signature

func (*Transaction) SigHash

func (t *Transaction) SigHash() Hash

Returns the hash to be used for signing the transaction

type TransactionBody

type TransactionBody struct {
	To     Address
	Amount *big.Int
}

type TxInput

type TxInput struct {
	Source *TxOutput // Maybe store this as a hash instead
}

type TxOutput

type TxOutput struct {
	Cubes  *big.Int
	PubKey []byte
}

func GetUnspentTxOutput

func GetUnspentTxOutput(db meddb.Database, hash Hash) (*TxOutput, error)

Gets a transaction output from the unspent pool

func (*TxOutput) DeleteUnspent

func (o *TxOutput) DeleteUnspent(db meddb.Database) error

Removes a transaction output from the unspent pool

func (*TxOutput) Hash

func (o *TxOutput) Hash() Hash

func (*TxOutput) WriteUnspent

func (o *TxOutput) WriteUnspent(db meddb.Database) error

Writes a transaction output to the unspent pool

Jump to

Keyboard shortcuts

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