coin

package
v0.0.0-...-9802667 Latest Latest
Warning

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

Go to latest
Published: May 27, 2018 License: Apache-2.0 Imports: 8 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUint64MultOverflow is returned when multiplying uint64 values would overflow uint64
	ErrUint64MultOverflow = errors.New("uint64 multiplication overflow")
	// ErrUint64AddOverflow is returned when adding uint64 values would overflow uint64
	ErrUint64AddOverflow = errors.New("uint64 addition overflow")
	// ErrUint32AddOverflow is returned when adding uint32 values would overflow uint32
	ErrUint32AddOverflow = errors.New("uint32 addition overflow")
	// ErrUint64OverflowsInt64 is returned when converting a uint64 to an int64 would overflow int64
	ErrUint64OverflowsInt64 = errors.New("uint64 overflows int64")
	// ErrInt64UnderflowsUint64 is returned when converting an int64 to a uint64 would underflow uint64
	ErrInt64UnderflowsUint64 = errors.New("int64 underflows uint64")
)
View Source
var (
	// DebugLevel1 checks for extremely unlikely conditions (10e-40)
	DebugLevel1 = true
	// DebugLevel2 enable checks for impossible conditions
	DebugLevel2 = true
)
View Source
var ErrAddEarnedCoinHoursAdditionOverflow = errors.New("UxOut.CoinHours addition of earned coin hours overflow")

ErrAddEarnedCoinHoursAdditionOverflow is returned by UxOut.CoinHours() if during the addition of base coin hours to additional earned coin hours, the value would overflow a uint64. Callers may choose to ignore this errors and use 0 as the coinhours value instead. This affects one existing spent output, spent in block 13277.

Functions

func AddUint64

func AddUint64(a, b uint64) (uint64, error)

AddUint64 adds a and b, returning an error if the values would overflow

func Int64ToUint64

func Int64ToUint64(a int64) (uint64, error)

Int64ToUint64 converts an int64 to a uint64, returning an error if the int64 value underflows uint64

func Uint64ToInt64

func Uint64ToInt64(a uint64) (int64, error)

Uint64ToInt64 converts a uint64 to an int64, returning an error if the uint64 value overflows int64

func VerifyTransactionCoinsSpending

func VerifyTransactionCoinsSpending(uxIn UxArray, uxOut UxArray) error

VerifyTransactionCoinsSpending checks that coins are not destroyed or created by the transaction

func VerifyTransactionHoursSpending

func VerifyTransactionHoursSpending(headTime uint64, uxIn UxArray, uxOut UxArray) error

VerifyTransactionHoursSpending checks that hours are not created by the transaction

Types

type AddressUxOuts

type AddressUxOuts map[cipher.Address]UxArray

AddressUxOuts maps address with uxarray

func NewAddressUxOuts

func NewAddressUxOuts(uxs UxArray) AddressUxOuts

NewAddressUxOuts creates address uxouts map

func (AddressUxOuts) Add

func (auo AddressUxOuts) Add(other AddressUxOuts) AddressUxOuts

Add returns a new unspents, with merged unspents

func (AddressUxOuts) Flatten

func (auo AddressUxOuts) Flatten() UxArray

Flatten converts an AddressUxOuts map to a UxArray

func (AddressUxOuts) Keys

func (auo AddressUxOuts) Keys() []cipher.Address

Keys returns the Address keys

func (AddressUxOuts) Sub

func (auo AddressUxOuts) Sub(other AddressUxOuts) AddressUxOuts

Sub returns a new set of unspents, with unspents found in other removed. No address's unspent set will be empty Depreciate this: only visor uses it

type Block

type Block struct {
	Head BlockHeader
	Body BlockBody
}

Block represents the block struct

func NewBlock

func NewBlock(prev Block, currentTime uint64, uxHash cipher.SHA256, txns Transactions, calc FeeCalculator) (*Block, error)

NewBlock creates new block.

func NewGenesisBlock

func NewGenesisBlock(genesisAddr cipher.Address, genesisCoins, timestamp uint64) (*Block, error)

NewGenesisBlock creates genesis block

func (Block) GetTransaction

func (b Block) GetTransaction(txHash cipher.SHA256) (Transaction, bool)

GetTransaction looks up a Transaction by its Head.Hash. Returns the Transaction and whether it was found or not TODO -- build a private index on the block, or a global blockchain one mapping txns to their block + tx index TODO: Deprecate? Utility Function

func (Block) HashBody

func (b Block) HashBody() cipher.SHA256

HashBody return hash of block body.

func (Block) HashHeader

func (b Block) HashHeader() cipher.SHA256

HashHeader return hash of block head.

func (Block) PreHashHeader

func (b Block) PreHashHeader() cipher.SHA256

PreHashHeader return hash of prevous block.

func (Block) Seq

func (b Block) Seq() uint64

Seq return the head seq of the block.

func (Block) Size

func (b Block) Size() int

Size returns the size of the Block's Transactions, in bytes

func (Block) String

func (b Block) String() string

String return readable string of block.

func (Block) Time

func (b Block) Time() uint64

Time return the head time of the block.

type BlockBody

type BlockBody struct {
	Transactions Transactions
}

BlockBody represents the block body

func (BlockBody) Bytes

func (bb BlockBody) Bytes() []byte

Bytes serialize block body, and return the byte value.

func (BlockBody) Hash

func (bb BlockBody) Hash() cipher.SHA256

Hash returns the merkle hash of contained transactions

func (BlockBody) Size

func (bb BlockBody) Size() int

Size returns the size of Transactions, in bytes

type BlockHeader

type BlockHeader struct {
	Version uint32

	Time  uint64
	BkSeq uint64 // Increment every block
	Fee   uint64 // Fee in block

	PrevHash cipher.SHA256 // Hash of header of previous block
	BodyHash cipher.SHA256 // Hash of transaction block

	UxHash cipher.SHA256 // XOR of sha256 of elements in unspent output set
}

BlockHeader records the block header

func NewBlockHeader

func NewBlockHeader(prev BlockHeader, uxHash cipher.SHA256, currentTime, fee uint64, body BlockBody) BlockHeader

NewBlockHeader creates block header

func (BlockHeader) Bytes

func (bh BlockHeader) Bytes() []byte

Bytes serialize the blockheader and return the byte value.

func (BlockHeader) Hash

func (bh BlockHeader) Hash() cipher.SHA256

Hash return hash of block header

func (BlockHeader) String

func (bh BlockHeader) String() string

String return readable string of block header.

type FeeCalculator

type FeeCalculator func(*Transaction) (uint64, error)

FeeCalculator given a transaction, return its fee or an error if the fee cannot be calculated

type HashPair

type HashPair struct {
	Hash    cipher.SHA256
	PreHash cipher.SHA256
}

HashPair including current block hash and previous block hash.

type SignedBlock

type SignedBlock struct {
	Block
	Sig cipher.Sig
}

SignedBlock signed block

func (SignedBlock) VerifySignature

func (b SignedBlock) VerifySignature(pubkey cipher.PubKey) error

VerifySignature verifies that the block is signed by pubkey

type SortableTransactions

type SortableTransactions struct {
	Txns   Transactions
	Fees   []uint64
	Hashes []cipher.SHA256
}

SortableTransactions allows sorting transactions by fee & hash

func NewSortableTransactions

func NewSortableTransactions(txns Transactions, feeCalc FeeCalculator) SortableTransactions

NewSortableTransactions returns an array of txns that can be sorted by fee. On creation, fees are calculated, and if any txns have invalid fee, there are removed from consideration

func (SortableTransactions) Len

func (txns SortableTransactions) Len() int

Len returns length of transactions

func (SortableTransactions) Less

func (txns SortableTransactions) Less(i, j int) bool

Less default sorting is fees descending, hash ascending if fees equal

func (SortableTransactions) Sort

func (txns SortableTransactions) Sort()

Sort sorts by tx fee, and then by hash if fee equal

func (SortableTransactions) Swap

func (txns SortableTransactions) Swap(i, j int)

Swap swaps txns

type Transaction

type Transaction struct {
	Length    uint32        //length prefix
	Type      uint8         //transaction type
	InnerHash cipher.SHA256 //inner hash SHA256 of In[],Out[]

	Sigs []cipher.Sig        //list of signatures, 64+1 bytes each
	In   []cipher.SHA256     //ouputs being spent
	Out  []TransactionOutput //ouputs being created
}

Transaction transaction struct

func MustTransactionDeserialize

func MustTransactionDeserialize(b []byte) Transaction

MustTransactionDeserialize deserialize transaction, panics on error

func TransactionDeserialize

func TransactionDeserialize(b []byte) (Transaction, error)

TransactionDeserialize deserialize transaction

func (*Transaction) Hash

func (txn *Transaction) Hash() cipher.SHA256

Hash an entire Transaction struct, including the TransactionHeader

func (*Transaction) HashInner

func (txn *Transaction) HashInner() cipher.SHA256

HashInner hashes only the Transaction Inputs & Outputs This is what is signed Client hashes the inner hash with hash of output being spent and signs it with private key

func (*Transaction) OutputHours

func (txn *Transaction) OutputHours() (uint64, error)

OutputHours returns the coin hours sent as outputs. This does not include the fee.

func (*Transaction) PushInput

func (txn *Transaction) PushInput(uxOut cipher.SHA256) uint16

PushInput adds a UxArray to the Transaction given the hash of a UxOut. Returns the signature index for later signing

func (*Transaction) PushOutput

func (txn *Transaction) PushOutput(dst cipher.Address, coins, hours uint64)

PushOutput Adds a TransactionOutput, sending coins & hours to an Address

func (*Transaction) Serialize

func (txn *Transaction) Serialize() []byte

Serialize serialize the transaction

func (*Transaction) SignInputs

func (txn *Transaction) SignInputs(keys []cipher.SecKey)

SignInputs signs all inputs in the transaction

func (*Transaction) Size

func (txn *Transaction) Size() int

Size returns the encoded byte size of the transaction

func (*Transaction) SizeHash

func (txn *Transaction) SizeHash() (int, cipher.SHA256)

SizeHash returns the encoded size and the hash of it (avoids duplicate encoding)

func (*Transaction) TxID

func (txn *Transaction) TxID() []byte

TxID returns transaction ID as byte string

func (*Transaction) TxIDHex

func (txn *Transaction) TxIDHex() string

TxIDHex returns transaction ID as hex

func (*Transaction) UpdateHeader

func (txn *Transaction) UpdateHeader()

UpdateHeader saves the txn body hash to TransactionHeader.Hash

func (*Transaction) Verify

func (txn *Transaction) Verify() error

Verify attempts to determine if the transaction is well formed Verify cannot check transaction signatures, it needs the address from unspents Verify cannot check if outputs being spent exist Verify cannot check if the transaction would create or destroy coins or if the inputs have the required coin base

func (Transaction) VerifyInput

func (txn Transaction) VerifyInput(uxIn UxArray) error

VerifyInput verifies the input

type TransactionOutput

type TransactionOutput struct {
	Address cipher.Address //address to send to
	Coins   uint64         //amount to be sent in coins
	Hours   uint64         //amount to be sent in coin hours
}

TransactionOutput hash output/name is function of Hash

func (TransactionOutput) UxID

func (txOut TransactionOutput) UxID(txID cipher.SHA256) cipher.SHA256

UxID compute transaction output id

type Transactions

type Transactions []Transaction

Transactions transaction slice

func SortTransactions

func SortTransactions(txns Transactions, feeCalc FeeCalculator) Transactions

SortTransactions returns transactions sorted by fee per kB, and sorted by lowest hash if tied. Transactions that fail in fee computation are excluded.

func (Transactions) Fees

func (txns Transactions) Fees(calc FeeCalculator) (uint64, error)

Fees calculates all the fees in Transactions

func (Transactions) Hashes

func (txns Transactions) Hashes() []cipher.SHA256

Hashes caculate transactions hashes

func (Transactions) Size

func (txns Transactions) Size() int

Size returns the sum of contained Transactions' sizes. It is not the size if serialized, since that would have a length prefix.

func (Transactions) TruncateBytesTo

func (txns Transactions) TruncateBytesTo(size int) Transactions

TruncateBytesTo returns the first n transactions whose total size is less than or equal to size.

type UxArray

type UxArray []UxOut

UxArray Array of Outputs Used by unspent output pool, spent tests

func CreateUnspents

func CreateUnspents(bh BlockHeader, tx Transaction) UxArray

CreateUnspents creates the expected outputs for a transaction.

func (UxArray) Add

func (ua UxArray) Add(other UxArray) UxArray

Add returns a new UxArray with merged elements

func (UxArray) CoinHours

func (ua UxArray) CoinHours(headTime uint64) (uint64, error)

CoinHours returns the total coin hours

func (UxArray) Coins

func (ua UxArray) Coins() (uint64, error)

Coins returns the total coins

func (UxArray) HasDupes

func (ua UxArray) HasDupes() bool

HasDupes checks the UxArray for outputs which have the same hash

func (UxArray) Hashes

func (ua UxArray) Hashes() []cipher.SHA256

Hashes returns Array of hashes for the Ux in the UxArray

func (UxArray) Len

func (ua UxArray) Len() int

Len returns length of UxArray

func (UxArray) Less

func (ua UxArray) Less(i, j int) bool

Less checks if UxArray[i] < UxArray[j]

func (UxArray) Set

func (ua UxArray) Set() UxHashSet

Set returns the UxArray as a hash to struct{} map to be used as a set. Should only be used for membership detection

func (UxArray) Sort

func (ua UxArray) Sort()

Sort sorts UxArray

func (UxArray) Sub

func (ua UxArray) Sub(other UxArray) UxArray

Sub returns a new UxArray with elements in other removed from self Deprecate

func (UxArray) Swap

func (ua UxArray) Swap(i, j int)

Swap swaps value of UxArray[i] and UxArray[j]

type UxBody

type UxBody struct {
	SrcTransaction cipher.SHA256  // Inner Hash of Transaction
	Address        cipher.Address // Address of receiver
	Coins          uint64         // Number of coins
	Hours          uint64         // Coin hours
}

UxBody uxbody

func (*UxBody) Hash

func (ub *UxBody) Hash() cipher.SHA256

Hash returns hash of uxbody

type UxHashSet

type UxHashSet map[cipher.SHA256]struct{}

UxHashSet set mapping from UxHash to a placeholder value

type UxHead

type UxHead struct {
	Time  uint64 //time of block it was created in
	BkSeq uint64 //block it was created in, used to calculate depth

}

UxHead metadata (not hashed)

type UxOut

type UxOut struct {
	Head UxHead
	Body UxBody //hashed part

}

UxOut represents uxout

func CreateUnspent

func CreateUnspent(bh BlockHeader, tx Transaction, outIndex int) (UxOut, error)

CreateUnspent creates single unspent output

func (*UxOut) CoinHours

func (uo *UxOut) CoinHours(t uint64) (uint64, error)

CoinHours Calculate coinhour balance of output. t is the current unix utc time.

func (*UxOut) Hash

func (uo *UxOut) Hash() cipher.SHA256

Hash returns the hash of UxBody

func (*UxOut) SnapshotHash

func (uo *UxOut) SnapshotHash() cipher.SHA256

SnapshotHash returns hash of UxBody + UxHead

Jump to

Keyboard shortcuts

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