block

package
v0.0.0-...-3b62c27 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2021 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const MineReward = 100

MineReward is the reward you get from mining a block

Variables

View Source
var (
	// ErrNotEnoughFunds is an error returned when a sender
	// does not have enough money to make a transaction
	ErrNotEnoughFunds = errors.New("not enough funds")

	// ErrBlockNotMined is the error returned when a block has not had
	// enough work done one it or it has not been mined (these are usually not
	// mutually exclusive).
	ErrBlockNotMined = errors.New("block has not been mined or done sufficient work")
)
View Source
var ErrInvalidSignature = errors.New("invalid signature")

ErrInvalidSignature is the error value given when a transaction has an invalid signature

View Source
var File_block_proto protoreflect.FileDescriptor

Functions

func HasDoneWork

func HasDoneWork(block MabyeProved) bool

HasDoneWork returns true of the hash has been run through the proof of work algorithm.

func IsDefaultGenesis

func IsDefaultGenesis(b *Block) bool

IsDefaultGenesis will return true if the block given is the default genesis block

func IsGenisis

func IsGenisis(b *Block) bool

IsGenisis will return true if the block given is the genisis block.

func ProofOfWork

func ProofOfWork(block Provable) (nonce int64, hash []byte)

ProofOfWork runs the Proof of Work algorithm for a provable block.

Types

type Block

type Block struct {
	Data         []byte         `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"`
	Nonce        int64          `protobuf:"varint,2,opt,name=nonce,proto3" json:"nonce,omitempty"`
	Hash         []byte         `protobuf:"bytes,3,opt,name=hash,proto3" json:"hash,omitempty"`
	PrevHash     []byte         `protobuf:"bytes,4,opt,name=prevHash,proto3" json:"prevHash,omitempty"`
	Transactions []*Transaction `protobuf:"bytes,5,rep,name=transactions,proto3" json:"transactions,omitempty"`
	// contains filtered or unexported fields
}

func DefaultGenesis

func DefaultGenesis() *Block

DefaultGenesis block with pre-computed nonce and hash

func Genisis

func Genisis(coinbase *Transaction) *Block

Genisis creates the first block of the chain

func New

func New(txs []*Transaction, prev []byte) *Block

New creates a new block from a list of transactions and the previous hash

func (*Block) CreateNext

func (b *Block) CreateNext(data []byte) *Block

CreateNext will create a new block using the data given and the hash of the current block.

func (*Block) Descriptor deprecated

func (*Block) Descriptor() ([]byte, []int)

Deprecated: Use Block.ProtoReflect.Descriptor instead.

func (*Block) GetData

func (x *Block) GetData() []byte

func (*Block) GetHash

func (x *Block) GetHash() []byte

func (*Block) GetNonce

func (x *Block) GetNonce() int64

func (*Block) GetPrevHash

func (x *Block) GetPrevHash() []byte

func (*Block) GetTransactions

func (x *Block) GetTransactions() []*Transaction

func (*Block) ProtoMessage

func (*Block) ProtoMessage()

func (*Block) ProtoReflect

func (x *Block) ProtoReflect() protoreflect.Message

func (*Block) Reset

func (x *Block) Reset()

func (*Block) String

func (x *Block) String() string

type Chain

type Chain interface {
	Iter() Iterator
	Head() (*Block, error)
}

Chain is an interface that defines what a blockchain is.

type Hashable

type Hashable interface {
	Hash() []byte
}

Hashable is an interface that defines type that can be hashed.

type Iterator

type Iterator interface {
	// returns the next block in the chain, value will
	// be nil if there are no more blocks. This should
	// return the genisis block as the last block
	Next() *Block
}

Iterator is an interface that defines a block iterator

type MabyeProved

type MabyeProved interface {
	Provable
	GetNonce() int64
}

MabyeProved is an interface that has been previously run through the proof of work algorithm

type Provable

type Provable interface {
	GetTransactions() []*Transaction
	GetPrevHash() []byte
	GetData() []byte
}

Provable is defines an interface for block hashing

type Store

type Store interface {
	Chain
	Get([]byte) (*Block, error)
	Push(*Block) error
}

Store defines an interface for objects that can store blocks

type Transaction

type Transaction struct {
	ID []byte `protobuf:"bytes,1,opt,name=ID,json=iD,proto3" json:"ID,omitempty"`
	// Transaction lock timestamp
	Lock *timestamp.Timestamp `protobuf:"bytes,2,opt,name=lock,proto3" json:"lock,omitempty"`
	// list of transaction inputs
	Inputs []*TxInput `protobuf:"bytes,3,rep,name=inputs,proto3" json:"inputs,omitempty"`
	// list of transaction outputs
	Outputs []*TxOutput `protobuf:"bytes,4,rep,name=outputs,proto3" json:"outputs,omitempty"`
	// contains filtered or unexported fields
}

Trasaction is a blockchain transaction which contains a list of both inputs and outputs.

Inputs are references to funds that have been used by the owner on the input's public key.

Outputs are references to funds that are being credited to the owner of the output's public key hash.

func Coinbase

func Coinbase(to key.Address) *Transaction

Coinbase will create a coinbase transaction.

func CreateTx

func CreateTx(stats UTXOSet, from key.Sender, recv []TxDesc) (*Transaction, error)

func NewTransaction

func NewTransaction() *Transaction

NewTransaction creates a new transaction. The new transaction will not be added to the chain.

func (*Transaction) Append

func (tx *Transaction) Append(utxo UTXOSet, d TxDesc) error

func (*Transaction) Descriptor deprecated

func (*Transaction) Descriptor() ([]byte, []int)

Deprecated: Use Transaction.ProtoReflect.Descriptor instead.

func (*Transaction) Fee

func (tx *Transaction) Fee(finder TxFinder) uint64

GetFee will get the transaction fee for the transaction The transaction fee is defined as the total input value minus the total output value of a transaction.

func (*Transaction) Finalize

func (tx *Transaction) Finalize()

func (*Transaction) GetID

func (x *Transaction) GetID() []byte

func (*Transaction) GetInputs

func (x *Transaction) GetInputs() []*TxInput

func (*Transaction) GetLock

func (x *Transaction) GetLock() *timestamp.Timestamp

func (*Transaction) GetOutputs

func (x *Transaction) GetOutputs() []*TxOutput

func (*Transaction) IsCoinbase

func (tx *Transaction) IsCoinbase() bool

IsCoinbase will return true of the transaction is a coinbase transaction.

func (*Transaction) ProtoMessage

func (*Transaction) ProtoMessage()

func (*Transaction) ProtoReflect

func (x *Transaction) ProtoReflect() protoreflect.Message

func (*Transaction) Reset

func (x *Transaction) Reset()

func (*Transaction) Sign

func (tx *Transaction) Sign(priv *ecdsa.PrivateKey, finder TxFinder) error

Sign signs the transaction

func (*Transaction) StrID

func (tx *Transaction) StrID() string

StrID will return a hex encoded version of the transaction ID

func (*Transaction) String

func (x *Transaction) String() string

func (*Transaction) VerifySig

func (tx *Transaction) VerifySig(find TxFinder) error

VerifySig will verify that a transaction has been correctly signed

type TransactionBuilder

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

func (*TransactionBuilder) Append

func (txb *TransactionBuilder) Append(utxo UTXOSet, d TxDesc) error

func (*TransactionBuilder) BuildTransactions

func (txb *TransactionBuilder) BuildTransactions() ([]*Transaction, error)

func (*TransactionBuilder) Reset

func (txb *TransactionBuilder) Reset()

type TxDesc

type TxDesc struct {
	From   key.Sender
	To     key.Receiver
	Amount uint64
}

TxDesc describes a transaction at a high level

type TxFinder

type TxFinder interface {
	// Transaction looks for a Transaction by ID
	// and returns nil if no transaction was found
	Transaction(id []byte) *Transaction
}

TxFinder defines an interface for objects that can find transactions.

type TxInput

type TxInput struct {

	// TxID is the hash of transaction where
	// the unspent output is stored.
	TxID []byte `protobuf:"bytes,1,opt,name=txID,proto3" json:"txID,omitempty"`
	// OutIndex gives the index of the output refrenced
	// with respect to the transaction's list of
	// outputs (0 being the transaction's first output and so on...)
	OutIndex int32 `protobuf:"varint,2,opt,name=outIndex,proto3" json:"outIndex,omitempty"`
	// Signature is the digital signature of
	// the sender
	Signature []byte `protobuf:"bytes,3,opt,name=signature,proto3" json:"signature,omitempty"`
	// Public key of the sender
	PubKey []byte `protobuf:"bytes,4,opt,name=pubKey,proto3" json:"pubKey,omitempty"`
	// contains filtered or unexported fields
}

func (*TxInput) Descriptor deprecated

func (*TxInput) Descriptor() ([]byte, []int)

Deprecated: Use TxInput.ProtoReflect.Descriptor instead.

func (*TxInput) GetOutIndex

func (x *TxInput) GetOutIndex() int32

func (*TxInput) GetPubKey

func (x *TxInput) GetPubKey() []byte

func (*TxInput) GetSignature

func (x *TxInput) GetSignature() []byte

func (*TxInput) GetTxID

func (x *TxInput) GetTxID() []byte

func (*TxInput) ProtoMessage

func (*TxInput) ProtoMessage()

func (*TxInput) ProtoReflect

func (x *TxInput) ProtoReflect() protoreflect.Message

func (*TxInput) Reset

func (x *TxInput) Reset()

func (*TxInput) String

func (x *TxInput) String() string

type TxOutput

type TxOutput struct {

	// Amount holds the amount
	// of coins in the output
	Amount uint64 `protobuf:"varint,1,opt,name=amount,proto3" json:"amount,omitempty"`
	// Hash of the recipient's public key
	// which can be derived from a wallet
	// address (see wallet package).
	PubKeyHash []byte `protobuf:"bytes,2,opt,name=pubKeyHash,proto3" json:"pubKeyHash,omitempty"`
	// Types that are assignable to Payload:
	//	*TxOutput_Token
	//	*TxOutput_Document
	//	*TxOutput_Data
	Payload isTxOutput_Payload `protobuf_oneof:"payload"`
	// contains filtered or unexported fields
}

TxOutput is a transaction output. If any given output is not referenced by a transaction input somewhere else in the chain, then the amount field is credited to the address which matches the public key hash.

func (*TxOutput) Descriptor deprecated

func (*TxOutput) Descriptor() ([]byte, []int)

Deprecated: Use TxOutput.ProtoReflect.Descriptor instead.

func (*TxOutput) GetAmount

func (x *TxOutput) GetAmount() uint64

func (*TxOutput) GetData

func (x *TxOutput) GetData() []byte

func (*TxOutput) GetDocument

func (x *TxOutput) GetDocument() string

func (*TxOutput) GetPayload

func (m *TxOutput) GetPayload() isTxOutput_Payload

func (*TxOutput) GetPubKeyHash

func (x *TxOutput) GetPubKeyHash() []byte

func (*TxOutput) GetToken

func (x *TxOutput) GetToken() uint64

func (*TxOutput) ProtoMessage

func (*TxOutput) ProtoMessage()

func (*TxOutput) ProtoReflect

func (x *TxOutput) ProtoReflect() protoreflect.Message

func (*TxOutput) Reset

func (x *TxOutput) Reset()

func (*TxOutput) String

func (x *TxOutput) String() string

type TxOutput_Data

type TxOutput_Data struct {
	// Raw data
	Data []byte `protobuf:"bytes,5,opt,name=data,proto3,oneof"`
}

type TxOutput_Document

type TxOutput_Document struct {
	// A document
	Document string `protobuf:"bytes,4,opt,name=document,proto3,oneof"`
}

type TxOutput_Token

type TxOutput_Token struct {
	// Token amount
	Token uint64 `protobuf:"varint,3,opt,name=token,proto3,oneof"`
}

type UTXO

type UTXO struct {
	// The actual transaction output that
	// has not been spent
	*TxOutput
	// contains filtered or unexported fields
}

UTXO is an unspent transaction output This is the same as a TxOutput except it holds useful information for creating transaction inputs like the output index and the transaction ID.

func FindOutputsToSpend

func FindOutputsToSpend(
	set UTXOSet,
	sender key.Address,
	amountNeeded uint64,
) []*UTXO

FindOutputsToSpend will find unspent transaction outputs needed for a transaction. Guaranteed to return at least one UTXO and a combined value greater than or equal to the amount needed (given as an argument). Assumes that the balance has already been checked and the publickeyhash owns enough outputs.

This function assumes that the sender has enough funds to meet the quota.

type UTXOSet

type UTXOSet interface {
	Bal(key.Address) uint64
	Unspent(key.Address) []*UTXO
	Update(*Transaction) error
	ReIndex(Iterator) error
}

UTXOSet holds unspent transaction outputs.

func BuildUTXOSet

func BuildUTXOSet(it Iterator) UTXOSet

BuildUTXOSet will traverse the entire chain to build an indexed unspent transaction set

Jump to

Keyboard shortcuts

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