Documentation ¶
Overview ¶
Package sunyata defines the basic types and functions of the sunyata system.
Index ¶
- Constants
- Variables
- func CurrentTimestamp() time.Time
- func EncodedLen(v interface{}) int
- type Address
- func (a *Address) DecodeFrom(d *Decoder)
- func (a Address) EncodeTo(e *Encoder)
- func (a Address) MarshalJSON() ([]byte, error)
- func (a Address) MarshalText() ([]byte, error)
- func (a Address) String() string
- func (a *Address) UnmarshalJSON(b []byte) (err error)
- func (a *Address) UnmarshalText(b []byte) (err error)
- type Block
- type BlockHeader
- type BlockID
- func (id *BlockID) DecodeFrom(d *Decoder)
- func (id BlockID) EncodeTo(e *Encoder)
- func (bid BlockID) MarshalJSON() ([]byte, error)
- func (bid BlockID) MarshalText() ([]byte, error)
- func (bid BlockID) MeetsTarget(t BlockID) bool
- func (bid BlockID) String() string
- func (bid *BlockID) UnmarshalJSON(b []byte) error
- func (bid *BlockID) UnmarshalText(b []byte) error
- type ChainIndex
- type Currency
- func (c Currency) Add(v Currency) Currency
- func (c Currency) AddWithOverflow(v Currency) (Currency, bool)
- func (c Currency) Big() *big.Int
- func (c Currency) Cmp(v Currency) int
- func (c *Currency) DecodeFrom(d *Decoder)
- func (c Currency) Div(v Currency) Currency
- func (c Currency) Div64(v uint64) Currency
- func (c Currency) EncodeTo(e *Encoder)
- func (c Currency) Equals(v Currency) bool
- func (c Currency) ExactString() string
- func (c Currency) Format(f fmt.State, v rune)
- func (c Currency) IsZero() bool
- func (c Currency) MarshalJSON() ([]byte, error)
- func (c Currency) Mul64(v uint64) Currency
- func (c Currency) String() string
- func (c Currency) Sub(v Currency) Currency
- func (c Currency) SubWithUnderflow(v Currency) (Currency, bool)
- func (c *Currency) UnmarshalJSON(b []byte) (err error)
- type Decoder
- func (d *Decoder) Err() error
- func (d *Decoder) Read(p []byte) (int, error)
- func (d *Decoder) ReadBool() bool
- func (d *Decoder) ReadBytes() []byte
- func (d *Decoder) ReadPrefix() int
- func (d *Decoder) ReadString() string
- func (d *Decoder) ReadTime() time.Time
- func (d *Decoder) ReadUint64() uint64
- func (d *Decoder) ReadUint8() uint8
- func (d *Decoder) SetErr(err error)
- type DecoderFrom
- type ElementID
- type Encoder
- func (e *Encoder) Flush() error
- func (e *Encoder) Write(p []byte) (int, error)
- func (e *Encoder) WriteBool(b bool)
- func (e *Encoder) WriteBytes(b []byte)
- func (e *Encoder) WritePrefix(i int)
- func (e *Encoder) WriteString(s string)
- func (e *Encoder) WriteTime(t time.Time)
- func (e *Encoder) WriteUint64(u uint64)
- func (e *Encoder) WriteUint8(u uint8)
- type EncoderTo
- type Hash256
- func (h *Hash256) DecodeFrom(d *Decoder)
- func (h Hash256) EncodeTo(e *Encoder)
- func (h Hash256) MarshalJSON() ([]byte, error)
- func (h Hash256) MarshalText() ([]byte, error)
- func (h Hash256) String() string
- func (h *Hash256) UnmarshalJSON(b []byte) error
- func (h *Hash256) UnmarshalText(b []byte) error
- type Hasher
- type Input
- type Output
- type OutputElement
- type PrivateKey
- type PublicKey
- func (pk PublicKey) Address() Address
- func (pk *PublicKey) DecodeFrom(d *Decoder)
- func (pk PublicKey) EncodeTo(e *Encoder)
- func (pk PublicKey) MarshalJSON() ([]byte, error)
- func (pk PublicKey) MarshalText() ([]byte, error)
- func (pk PublicKey) String() string
- func (pk *PublicKey) UnmarshalJSON(b []byte) error
- func (pk *PublicKey) UnmarshalText(b []byte) error
- func (pk PublicKey) VerifyHash(h Hash256, s Signature) bool
- type Signature
- func (s *Signature) DecodeFrom(d *Decoder)
- func (s Signature) EncodeTo(e *Encoder)
- func (sig Signature) MarshalJSON() ([]byte, error)
- func (sig Signature) MarshalText() ([]byte, error)
- func (sig Signature) String() string
- func (sig *Signature) UnmarshalJSON(b []byte) error
- func (sig *Signature) UnmarshalText(b []byte) error
- type StateElement
- type Transaction
- type TransactionID
- func (id *TransactionID) DecodeFrom(d *Decoder)
- func (id TransactionID) EncodeTo(e *Encoder)
- func (tid TransactionID) MarshalJSON() ([]byte, error)
- func (tid TransactionID) MarshalText() ([]byte, error)
- func (tid TransactionID) String() string
- func (tid *TransactionID) UnmarshalJSON(b []byte) error
- func (tid *TransactionID) UnmarshalText(b []byte) error
- type Work
- func (w Work) Add(v Work) Work
- func (w Work) Cmp(v Work) int
- func (w *Work) DecodeFrom(d *Decoder)
- func (w Work) Div64(v uint64) Work
- func (w Work) EncodeTo(e *Encoder)
- func (w Work) MarshalJSON() ([]byte, error)
- func (w Work) MarshalText() ([]byte, error)
- func (w Work) Mul64(v uint64) Work
- func (w Work) String() string
- func (w Work) Sub(v Work) Work
- func (w *Work) UnmarshalJSON(b []byte) error
- func (w *Work) UnmarshalText(b []byte) error
Constants ¶
const EphemeralLeafIndex = math.MaxUint64
EphemeralLeafIndex is used as the LeafIndex of StateElements that are created and spent within the same block. Such elements do not require a proof of existence. They are, however, assigned a proper index and are incorporated into the state accumulator when the block is processed.
Variables ¶
var BaseUnitsPerCoin = NewCurrency64(1e9)
BaseUnitsPerCoin is the number of base units in a single coin.
Functions ¶
func CurrentTimestamp ¶
CurrentTimestamp returns the current time, rounded to the nearest second.
func EncodedLen ¶
func EncodedLen(v interface{}) int
EncodedLen returns the length of v when encoded.
Types ¶
type Address ¶
type Address Hash256
An Address is the hash of a public key.
var VoidAddress Address
VoidAddress is an address whose signing key does not exist. Sending coins to this address ensures that they will never be recoverable by anyone.
func ParseAddress ¶
ParseAddress parses an address from a prefixed hex encoded string.
func (*Address) DecodeFrom ¶
DecodeFrom implements sunyata.DecoderFrom.
func (Address) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (Address) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Address) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Address) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Block ¶
type Block struct { Header BlockHeader Transactions []Transaction }
A Block is a set of transactions grouped under a header.
func (*Block) ID ¶
ID returns a hash that uniquely identifies a block. It is equivalent to b.Header.ID().
func (*Block) Index ¶
func (b *Block) Index() ChainIndex
Index returns the block's chain index. It is equivalent to b.Header.Index().
func (*Block) MinerOutputID ¶
MinerOutputID returns the output ID of the miner payout.
type BlockHeader ¶
type BlockHeader struct { Height uint64 ParentID BlockID Nonce uint64 Timestamp time.Time MinerAddress Address Commitment Hash256 }
A BlockHeader contains a Block's non-transaction data.
func (*BlockHeader) DecodeFrom ¶
func (h *BlockHeader) DecodeFrom(d *Decoder)
DecodeFrom implements sunyata.DecoderFrom.
func (BlockHeader) EncodeTo ¶
func (h BlockHeader) EncodeTo(e *Encoder)
EncodeTo implements sunyata.EncoderTo.
func (BlockHeader) ID ¶
func (h BlockHeader) ID() BlockID
ID returns a hash that uniquely identifies a block.
func (BlockHeader) Index ¶
func (h BlockHeader) Index() ChainIndex
Index returns the header's chain index.
func (BlockHeader) ParentIndex ¶
func (h BlockHeader) ParentIndex() ChainIndex
ParentIndex returns the index of the header's parent.
type BlockID ¶
type BlockID Hash256
A BlockID uniquely identifies a block.
func HashRequiringWork ¶
HashRequiringWork returns the best BlockID that the given amount of Work would be expected to produce. Note that many different BlockIDs may require the same amount of Work; this function returns the lowest of them.
func (*BlockID) DecodeFrom ¶
DecodeFrom implements sunyata.DecoderFrom.
func (BlockID) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (BlockID) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (BlockID) MeetsTarget ¶
MeetsTarget returns true if bid is not greater than t.
func (*BlockID) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*BlockID) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type ChainIndex ¶
A ChainIndex pairs a block's height with its ID.
func ParseChainIndex ¶
func ParseChainIndex(s string) (ci ChainIndex, err error)
ParseChainIndex parses a chain index from a string.
func (*ChainIndex) DecodeFrom ¶
func (index *ChainIndex) DecodeFrom(d *Decoder)
DecodeFrom implements sunyata.DecoderFrom.
func (ChainIndex) EncodeTo ¶
func (index ChainIndex) EncodeTo(e *Encoder)
EncodeTo implements sunyata.EncoderTo.
func (ChainIndex) MarshalText ¶
func (ci ChainIndex) MarshalText() ([]byte, error)
MarshalText implements encoding.TextMarshaler.
func (*ChainIndex) UnmarshalText ¶
func (ci *ChainIndex) UnmarshalText(b []byte) (err error)
UnmarshalText implements encoding.TextUnmarshaler.
type Currency ¶
type Currency struct {
Lo, Hi uint64
}
Currency represents a quantity of hastings as an unsigned 128-bit number.
var ZeroCurrency Currency
ZeroCurrency represents zero base units.
func NewCurrency ¶
NewCurrency returns the Currency value (lo,hi).
func NewCurrency64 ¶
NewCurrency64 converts c to a Currency value.
func ParseCurrency ¶
ParseCurrency parses s as a Currency value. The format of s should match one of the representations provided by (Currency).Format.
func (Currency) Add ¶
Add returns c+v. If the result would overflow, Add panics.
It is safe to use Add in any context where the sum cannot exceed the total supply of Currency (such as when calculating the balance of a wallet). In less-trusted contexts (such as when validating a transaction), AddWithOverflow should be used instead.
func (Currency) AddWithOverflow ¶
AddWithOverflow returns c+v, along with a boolean indicating whether the result overflowed.
func (*Currency) DecodeFrom ¶
DecodeFrom implements sunyata.DecoderFrom.
func (Currency) Equals ¶
Equals returns true if c == v.
Currency values can be compared directly with ==, but use of the Equals method is preferred for consistency.
func (Currency) ExactString ¶
ExactString returns the base-10 representation of c as a string.
func (Currency) Format ¶
Format implements fmt.Formatter. It accepts the following formats:
d: raw integer (equivalent to ExactString()) s: rounded integer with unit suffix (equivalent to String()) v: same as s
func (Currency) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (Currency) Mul64 ¶
Mul64 returns c*v. If the result would overflow, Mul64 panics.
Note that it is safe to multiply any two Currency values that are below 2^64.
func (Currency) String ¶
String returns base-10 representation of c with a unit suffix. The value may be rounded. To avoid loss of precision, use ExactString.
func (Currency) SubWithUnderflow ¶
SubWithUnderflow returns c-v, along with a boolean indicating whether the result underflowed.
func (*Currency) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
A Decoder reads values from an underlying stream. Callers MUST check (*Decoder).Err before using any decoded values.
func NewBufDecoder ¶
NewBufDecoder returns a Decoder for the provided byte slice.
func NewDecoder ¶
func NewDecoder(lr io.LimitedReader) *Decoder
NewDecoder returns a Decoder that wraps the provided stream.
func (*Decoder) Read ¶
Read implements the io.Reader interface. It always returns an error if fewer than len(p) bytes were read.
func (*Decoder) ReadPrefix ¶
ReadPrefix reads a length prefix from the underlying stream. If the length exceeds the number of bytes remaining in the stream, ReadPrefix sets d.Err and returns 0.
func (*Decoder) ReadString ¶
ReadString reads a length-prefixed string from the underlying stream.
func (*Decoder) ReadUint64 ¶
ReadUint64 reads a uint64 value from the underlying stream.
type DecoderFrom ¶
type DecoderFrom interface {
DecodeFrom(d *Decoder)
}
A DecoderFrom can decode itself from a stream via a Decoder.
type ElementID ¶
An ElementID uniquely identifies a StateElement.
func (*ElementID) DecodeFrom ¶
DecodeFrom implements sunyata.DecoderFrom.
func (ElementID) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*ElementID) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
An Encoder writes objects to an underlying stream.
func NewEncoder ¶
NewEncoder returns an Encoder that wraps the provided stream.
func (*Encoder) Flush ¶
Flush writes any pending data to the underlying stream. It returns the first error encountered by the Encoder.
func (*Encoder) WriteBytes ¶
WriteBytes writes a length-prefixed []byte to the underlying stream.
func (*Encoder) WritePrefix ¶
WritePrefix writes a length prefix to the underlying stream.
func (*Encoder) WriteString ¶
WriteString writes a length-prefixed string to the underlying stream.
func (*Encoder) WriteUint64 ¶
WriteUint64 writes a uint64 value to the underlying stream.
func (*Encoder) WriteUint8 ¶
WriteUint8 writes a uint8 value to the underlying stream.
type EncoderTo ¶
type EncoderTo interface {
EncodeTo(e *Encoder)
}
An EncoderTo can encode itself to a stream via an Encoder.
type Hash256 ¶
type Hash256 [32]byte
A Hash256 is a generic 256-bit cryptographic hash.
func (*Hash256) DecodeFrom ¶
DecodeFrom implements sunyata.DecoderFrom.
func (Hash256) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (Hash256) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Hash256) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Hash256) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Hasher ¶
type Hasher struct { E *Encoder // contains filtered or unexported fields }
A Hasher streams objects into an instance of the sunyata hash function.
type Input ¶
type Input struct { Parent OutputElement PublicKey PublicKey Signature Signature }
An Input spends its parent Output by revealing its public key and signing the transaction.
func (*Input) DecodeFrom ¶
DecodeFrom implements sunyata.DecoderFrom.
type Output ¶
An Output is a volume of currency that is created and spent as an atomic unit.
func (*Output) DecodeFrom ¶
DecodeFrom implements sunyata.DecoderFrom.
type OutputElement ¶
type OutputElement struct { StateElement Output MaturityHeight uint64 }
An OutputElement is a volume of currency that is created and spent as an atomic unit.
func (*OutputElement) DecodeFrom ¶
func (oe *OutputElement) DecodeFrom(d *Decoder)
DecodeFrom implements sunyata.DecoderFrom.
func (OutputElement) EncodeTo ¶
func (oe OutputElement) EncodeTo(e *Encoder)
EncodeTo implements sunyata.EncoderTo.
type PrivateKey ¶
type PrivateKey []byte
A PrivateKey is an Ed25519 private key.
func GeneratePrivateKey ¶
func GeneratePrivateKey() PrivateKey
GeneratePrivateKey creates a new private key from a secure entropy source.
func NewPrivateKeyFromSeed ¶
func NewPrivateKeyFromSeed(seed []byte) PrivateKey
NewPrivateKeyFromSeed calculates a private key from a seed.
func (PrivateKey) PublicKey ¶
func (priv PrivateKey) PublicKey() (pk PublicKey)
PublicKey returns the PublicKey corresponding to priv.
func (PrivateKey) SignHash ¶
func (priv PrivateKey) SignHash(h Hash256) (s Signature)
SignHash signs h with priv, producing a Signature.
type PublicKey ¶
type PublicKey [32]byte
A PublicKey is an Ed25519 public key.
func (*PublicKey) DecodeFrom ¶
DecodeFrom implements sunyata.DecoderFrom.
func (PublicKey) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (PublicKey) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*PublicKey) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*PublicKey) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Signature ¶
type Signature [64]byte
A Signature is an Ed25519 signature.
func (*Signature) DecodeFrom ¶
DecodeFrom implements sunyata.DecoderFrom.
func (Signature) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (Signature) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Signature) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Signature) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type StateElement ¶
A StateElement is a generic element within the state accumulator.
func (*StateElement) DecodeFrom ¶
func (se *StateElement) DecodeFrom(d *Decoder)
DecodeFrom implements sunyata.DecoderFrom.
func (StateElement) EncodeTo ¶
func (se StateElement) EncodeTo(e *Encoder)
EncodeTo implements sunyata.EncoderTo.
type Transaction ¶
A Transaction transfers value by consuming existing Outputs and creating new Outputs.
func (*Transaction) DecodeFrom ¶
func (txn *Transaction) DecodeFrom(d *Decoder)
DecodeFrom implements sunyata.DecoderFrom.
func (*Transaction) DeepCopy ¶
func (txn *Transaction) DeepCopy() Transaction
DeepCopy returns a copy of txn that does not alias any of its memory.
func (Transaction) EncodeTo ¶
func (txn Transaction) EncodeTo(e *Encoder)
EncodeTo implements sunyata.EncoderTo.
func (*Transaction) EphemeralOutputElement ¶
func (txn *Transaction) EphemeralOutputElement(i int) OutputElement
EphemeralOutputElement returns txn.Outputs[i] as an ephemeral OutputElement.
func (*Transaction) ID ¶
func (txn *Transaction) ID() TransactionID
ID returns the "semantic hash" of the transaction, covering all of the transaction's effects, but not incidental data such as signatures or Merkle proofs. This ensures that the ID will remain stable (i.e. non-malleable).
To hash all of the data in a transaction, use the EncodeTo method.
func (*Transaction) OutputID ¶
func (txn *Transaction) OutputID(i int) ElementID
OutputID returns the ID of the output at index i.
type TransactionID ¶
type TransactionID Hash256
A TransactionID uniquely identifies a transaction.
func (*TransactionID) DecodeFrom ¶
func (id *TransactionID) DecodeFrom(d *Decoder)
DecodeFrom implements sunyata.DecoderFrom.
func (TransactionID) EncodeTo ¶
func (id TransactionID) EncodeTo(e *Encoder)
EncodeTo implements sunyata.EncoderTo.
func (TransactionID) MarshalJSON ¶
func (tid TransactionID) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler.
func (TransactionID) MarshalText ¶
func (tid TransactionID) MarshalText() ([]byte, error)
MarshalText implements encoding.TextMarshaler.
func (TransactionID) String ¶
func (tid TransactionID) String() string
String implements fmt.Stringer.
func (*TransactionID) UnmarshalJSON ¶
func (tid *TransactionID) UnmarshalJSON(b []byte) error
UnmarshalJSON implements json.Unmarshaler.
func (*TransactionID) UnmarshalText ¶
func (tid *TransactionID) UnmarshalText(b []byte) error
UnmarshalText implements encoding.TextUnmarshaler.
type Work ¶
type Work struct { // The representation is the expected number of hashes required to produce a // given hash, in big-endian order. NumHashes [32]byte }
Work represents a quantity of work.
func WorkRequiredForHash ¶
WorkRequiredForHash estimates how much work was required to produce the given id. Note that the mapping is not injective; many different ids may require the same expected amount of Work.
func (*Work) DecodeFrom ¶
DecodeFrom implements sunyata.DecoderFrom.
func (Work) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (Work) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Work) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Work) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
Directories ¶
Path | Synopsis |
---|---|
Package consensus implements the sunyata consensus algorithms.
|
Package consensus implements the sunyata consensus algorithms. |
internal
|
|
Package miner provides a basic miner for sunyata, suitable for testing and as a basis for more sophisticated implementations.
|
Package miner provides a basic miner for sunyata, suitable for testing and as a basis for more sophisticated implementations. |