blockchain

package
v0.0.0-...-b567ec7 Latest Latest
Warning

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

Go to latest
Published: May 25, 2022 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxTransactionsPerBlock = 512
)

Variables

View Source
var (
	ErrTransactionAlreadyPending = errors.New("Transaction is already pending!")
	ErrTransactionNotFound       = errors.New("Transaction not found!")
	ErrBlockNotFound             = errors.New("Block not found!")
	ErrChildrenNotFound          = errors.New("Children not found!")
	ErrParentBlockNotFound       = errors.New("Parent block not found!")
	ErrAccountHasNoStake         = errors.New("Account has no stake!")
)
View Source
var (
	// The initial block height.
	GenesisHeight uint64 = 0

	// The initial block target in the network.
	GenesisTarget uint64 = 100_000

	// The initial block difficulty in the network.
	GenesisDifficulty uint64 = 0

	// The initial block creation time in the network.
	GenesisTimeUnixNano int64 = time.Unix(0, 0).UnixNano()

	// The genesis account that created the genesis block.
	GenesisKeyPair = &secp256k1.KeyPair{
		PublicKey:  "0308f3ee0280f67151bd0d1a468205279d7df016805213be6c89f7bb8168835d0c",
		PrivateKey: "484ff6fe0382d9f0c201d3f7a7e65e2a4f86845ccc47bc5b8617b31666ddf408",
	}

	// The initial transactions in the genesis block.
	GenesisTransactions = []Transaction{}

	GenesisChallenge encryption.SHA256HexString = encryption.ZeroSHA256HexString()

	GenesisAddress encryption.SHA256HexString = encryption.ZeroSHA256HexString()

	// The genesis block.
	GenesisBlock *Block

	Stakeholders = map[string]uint64{

		"0372689db204d56d9bb7122497eef4732cce308b73f3923fc076aed3c2dfa4ad04": uint64(100_000),

		"03f1f2fbd80b49b8ffc8194ac0a0e0b7cf0c7e21bca2482c5fba7adf67db41dec5": uint64(100_000),
	}
)
View Source
var (
	ErrEmptyBlockRepo = errors.New("The block repository is empty!")
)
View Source
var (
	ErrProofHitAboveUpperBound = errors.New("Hit is above the upper bound!")
)

Functions

func BroadcastNewBlock

func BroadcastNewBlock(b *Block)

func BroadcastNewTransaction

func BroadcastNewTransaction(t *Transaction)

func BroadcastResolveBlockRequest

func BroadcastResolveBlockRequest(id *encryption.SHA256HexString)

func BroadcastResolveBlockResponse

func BroadcastResolveBlockResponse(b *Block)

func InitChain

func InitChain(keyPair *secp256k1.KeyPair)

Initiate a new blockchain with the genesis block. The blockchain is accessible under `Instance`.

func InitRepo

func InitRepo()

func ReactToPeerMessages

func ReactToPeerMessages()

Bind the blockchain to new messages from the peer.

func Routes

func Routes() (router *Router)

Types

type AccountTransactionInfo

type AccountTransactionInfo struct {
	PendingTransactions   []Transaction
	PersistedTransactions []Transaction
}

type Block

type Block struct {
	// The random id of the block.
	ID encryption.SHA256HexString `json:"id" sign:"yes" pg:",pk,unique,notnull"`

	// The id of the parent block.
	// This is only "nil" for the genesis block.
	ParentID *encryption.SHA256HexString `json:"parentID" sign:"yes"`

	// The height of the block.
	// The genesis block has height 0.
	Height uint64 `json:"height" sign:"yes" pg:",notnull,use_zero"`

	// The timestamp of the block creation.
	// For the genesis block, this is 0.
	TimeUnixNano int64 `json:"timeUnixNano" sign:"yes" pg:"time_unix_nano,notnull,use_zero"`

	// The transactions that are included in the block.
	// This includes regular transactions from clients
	// and a special reward transaction at the block end.
	Transactions []Transaction `json:"transactions" sign:"yes" pg:",rel:has-many,join_fk:block_id"`

	// The address of the block creator.
	Creator secp256k1.PublicKeyHexString `json:"creator" sign:"yes" pg:",notnull"`

	// The target value of this block which has to be met
	// by the block creator.
	Target uint64 `json:"target" sign:"yes" pg:",notnull"`

	// The challenge is created by signing the parent block challenge
	// with the block creator public keyand hashing it with the
	// SHA256 hashing algorithm. The challenge is used to
	// determine if an account is eligible to create a new block.
	Challenge encryption.SHA256HexString `json:"challenge" sign:"yes" pg:",notnull"`

	// The cumulative difficulty of this block increases
	// over the chain length with regards of the base target.
	// It is used to determine which chain to use when
	// there are two chains with equal maximum heights.
	// For the genesis block, this is 0.
	CumulativeDifficulty uint64 `json:"cumulativeDifficulty" sign:"yes" pg:",notnull,use_zero"`

	// The signature of the block.
	Signature *secp256k1.SignatureHexString `json:"signature" sign:"no" pg:",notnull"`
}

A block as the main constituent of the blockchain.

func (*Block) GetSender

func (b *Block) GetSender() secp256k1.PublicKeyHexString

func (*Block) GetSignString

func (b *Block) GetSignString() string

type BlockRepo

type BlockRepo struct {
	DB *pg.DB
}

A block repository interface to the database.

var Repo *BlockRepo

The blockchain repo instance.

func (*BlockRepo) AddBlockIfNotExists

func (r *BlockRepo) AddBlockIfNotExists(b *Block) error

func (*BlockRepo) ContainsBlockByID

func (r *BlockRepo) ContainsBlockByID(id encryption.SHA256HexString) bool

func (*BlockRepo) ContainsMainChainTransactionByID

func (r *BlockRepo) ContainsMainChainTransactionByID(id encryption.SHA256HexString) bool

func (*BlockRepo) GetBlockByID

func (r *BlockRepo) GetBlockByID(id encryption.SHA256HexString) (*Block, error)

func (*BlockRepo) GetBlockChildren

func (r *BlockRepo) GetBlockChildren(id encryption.SHA256HexString) (*[]Block, error)

func (*BlockRepo) GetBlockCount

func (r *BlockRepo) GetBlockCount() (*int, error)

func (*BlockRepo) GetBlockCountByCreator

func (r *BlockRepo) GetBlockCountByCreator(creator secp256k1.PublicKeyHexString) (*int, error)

func (*BlockRepo) GetChainToBlock

func (r *BlockRepo) GetChainToBlock(b Block) (*[]Block, error)

func (*BlockRepo) GetMainChainEndpoint

func (r *BlockRepo) GetMainChainEndpoint() (*Block, error)

func (*BlockRepo) GetMainChainTransactionByID

func (r *BlockRepo) GetMainChainTransactionByID(id encryption.SHA256HexString) (*Transaction, error)

func (*BlockRepo) GetMainChainTransactionsForAccount

func (r *BlockRepo) GetMainChainTransactionsForAccount(account secp256k1.PublicKeyHexString) (*[]Transaction, error)

func (*BlockRepo) GetMaxNLastBlocks

func (r *BlockRepo) GetMaxNLastBlocks(n int) (*[]Block, error)

func (*BlockRepo) GetMaxNLastBlocksByCreator

func (r *BlockRepo) GetMaxNLastBlocksByCreator(n int, creator secp256k1.PublicKeyHexString) (*[]Block, error)

func (*BlockRepo) GetMaxNLastMainChainTransactions

func (r *BlockRepo) GetMaxNLastMainChainTransactions(n int) (*[]Transaction, error)

func (*BlockRepo) StakeUntilBlockWithID

func (r *BlockRepo) StakeUntilBlockWithID(
	p secp256k1.PublicKeyHexString,
	blockID encryption.SHA256HexString,
) (*int64, error)

Compute the stake of an account until a block id.

type Blockchain

type Blockchain struct {
	// The currently pending transactions that were
	// sent to the node (by clients or other nodes)
	// and not yet included in the blockchain.
	PendingTransactions *[]Transaction

	// The blocks that were received or generated
	// but are not yet integrated into the head.
	PendingBlocks *[]Block
	// contains filtered or unexported fields
}
var Instance *Blockchain

The main instance of the blockchain. This instance is `nil` until `Init(keyPair)` is called.

func (*Blockchain) AddPendingTransaction

func (chain *Blockchain) AddPendingTransaction(t *Transaction) error

Add a given transaction to the pending transactions.

func (*Blockchain) CalculateProof

func (chain *Blockchain) CalculateProof(b *Block) (*Proof, error)

func (*Blockchain) ContainsPendingBlockByID

func (chain *Blockchain) ContainsPendingBlockByID(id encryption.SHA256HexString) bool

Check if the blockchain has a pending block with the given id.

func (*Blockchain) ContainsPendingTransactionByID

func (chain *Blockchain) ContainsPendingTransactionByID(id encryption.SHA256HexString) bool

func (*Blockchain) GetPendingTransactionByID

func (chain *Blockchain) GetPendingTransactionByID(id encryption.SHA256HexString) (*Transaction, error)

Get a pending transaction of the blockchain.

func (*Blockchain) GetTransactionInfo

func (chain *Blockchain) GetTransactionInfo(account secp256k1.PublicKeyHexString) (*AccountTransactionInfo, error)

func (*Blockchain) GetTransactionsToMint

func (chain *Blockchain) GetTransactionsToMint(endpointBlock Block) (*[]Transaction, error)

Get max. 512 transactions from the pending transactions which should be minted into a new block, on top of the given endpoint block.

func (*Blockchain) MigrateBlock

func (chain *Blockchain) MigrateBlock(b *Block, syncmode bool)

func (*Blockchain) MintBlock

func (chain *Blockchain) MintBlock() (*Block, error)

Mint a new block. This returns a block, if the proof of stake is successful, otherwise this will return `nil` and an error.

func (*Blockchain) PublicKey

func (chain *Blockchain) PublicKey() secp256k1.PublicKeyHexString

func (*Blockchain) RecommendedTransactionFee

func (chain *Blockchain) RecommendedTransactionFee() int

Get a recommended transaction fee based on the pending transactions.

func (*Blockchain) RemovePendingTransaction

func (chain *Blockchain) RemovePendingTransaction(t *Transaction)

func (*Blockchain) RunContinuousMinting

func (chain *Blockchain) RunContinuousMinting()

Run a scheduled concurrent block creation loop.

func (*Blockchain) Sync

func (chain *Blockchain) Sync(remote string)

func (*Blockchain) ThreadSafe

func (chain *Blockchain) ThreadSafe(execution func())

func (*Blockchain) ValidateBlock

func (chain *Blockchain) ValidateBlock(b *Block) (*Proof, error)

func (*Blockchain) ValidateTransaction

func (chain *Blockchain) ValidateTransaction(t *Transaction) error

type CreateTransactionRequest

type CreateTransactionRequest struct {
	Transaction *Transaction `json:"transaction"`
}

The request format for the `postTransaction` method.

type CreateTransactionResponse

type CreateTransactionResponse struct {
	Transaction *Transaction `json:"transaction"`
}

The response format for the `postTransaction` method.

type GetAccountBalanceResponse

type GetAccountBalanceResponse struct {
	Balance *int64 `json:"balance"`
}

The response format for the `getAccountBalance` method.

type GetAccountTransactionsResponse

type GetAccountTransactionsResponse struct {
	// The requested transaction.
	Transactions *[]Transaction `json:"transactions"`
}

The response format for the `getAccountTransactions` method.

type GetChildrenResponse

type GetChildrenResponse struct {
	Children *[]Block `json:"children"`
}

The response format for the `getTransaction` method.

type GetRecommendedTransactionFeeResponse

type GetRecommendedTransactionFeeResponse struct {
	Fee *int `json:"fee"`
}

type GetTransactionResponse

type GetTransactionResponse struct {
	// The requested transaction.
	Transaction *Transaction `json:"transaction"`
}

The response format for the `getTransaction` method.

type NewBlockMessage

type NewBlockMessage struct {
	NewBlock *Block `json:"newBlock"`
}

type NewTransactionMessage

type NewTransactionMessage struct {
	NewTransaction *Transaction `json:"newTransaction"`
}

type Proof

type Proof struct {
	Challenge            encryption.SHA256HexString
	Hit                  uint64
	UpperBound           big.Int
	Target               uint64
	CumulativeDifficulty uint64
	Stake                int64
	NanoSeconds          int64
}

A block proof of stake.

func (*Proof) Validate

func (proof *Proof) Validate() error

Check if a calculated proof is valid.

type ResolveBlockRequest

type ResolveBlockRequest struct {
	BlockID *encryption.SHA256HexString `json:"blockID"`
}

type ResolveBlockResponse

type ResolveBlockResponse struct {
	ResolvedBlock *Block `json:"resolvedBlock"`
}

type Transaction

type Transaction struct {
	// The random id of this transaction, as a unique key.
	ID encryption.SHA256HexString `json:"id" sign:"yes" pg:",pk,unique,notnull"`

	// The sender of this transaction, by address.
	Sender secp256k1.PublicKeyHexString `json:"sender" sign:"yes" pg:",notnull"`

	// The receiver of this transaction, by address.
	Receiver secp256k1.PublicKeyHexString `json:"receiver" sign:"yes" pg:",notnull"`

	// The transferred account balance from the sender
	// to the receiver.
	Balance uint64 `json:"balance" sign:"yes" pg:",notnull,use_zero"`

	// The timestamp of the transaction creation.
	// For the genesis transactions, this is the
	// start of Unix time.
	TimeUnixNano int64 `json:"timeUnixNano" sign:"yes" pg:",notnull,use_zero"`

	// The included transaction data.
	Data *[]byte `json:"data,omitempty" sign:"yes"`

	// The transaction fee.
	Fee uint64 `json:"fee" sign:"yes" pg:",notnull,use_zero"`

	// The signature of the transaction.
	Signature *secp256k1.SignatureHexString `json:"signature" sign:"no" pg:",notnull"`

	// The block id of the block where this transaction is included.
	// This field is `nil` until the transaction is included into
	// a block.
	BlockID *encryption.SHA256HexString `json:"blockID,omitempty" sign:"no" pg:",pk,notnull"`
}

A transaction in the blockchain. Transactions are obtained via the http interfaces and forged into blocks to persist them in the blockchain.

func (*Transaction) GetSender

func (t *Transaction) GetSender() secp256k1.PublicKeyHexString

func (*Transaction) GetSignString

func (t *Transaction) GetSignString() string

Jump to

Keyboard shortcuts

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