Skip to content
Snippets Groups Projects
Select Git revision
  • exhange_documentation
  • master default protected
  • nebulous_master
  • jayrama-master-patch-44542
  • master_jaytest
  • master_genesis_fix
  • master_faustianagi
  • master_toastpool
  • master_logging
  • master_syncing
  • jay_test
  • master_fork
  • genesis_airdrop
  • dev_fund
  • update_ports
  • update_imports
  • hardfork
  • sighash
  • ndf-add-nodes
  • better-score-breakdown
  • v1.3.5.2
  • v1.3.5.1
  • v1.3.5
  • v1.3.4
  • v1.3.3
  • v1.3.2
  • v1.3.1
  • v1.3.0
  • v1.2.2
  • v1.2.1
  • v1.2.0
  • v1.1.2
  • v1.1.1
  • v1.1.0
  • v1.0.4
  • v1.0.4-lts
  • lts-v1.0.4
  • v1.0.3
  • v1.0.1
  • v1.0.0
40 results

block.go

Forked from NebulousLabs / Sia
18607 commits behind the upstream repository.
block.go 2.46 KiB
package types

// block.go defines the Block type for Sia, and provides some helper functions
// for working with blocks.

import (
	"bytes"

	"github.com/NebulousLabs/Sia/crypto"
)

type (
	// A Block is a summary of changes to the state that have occurred since the
	// previous block. Blocks reference the ID of the previous block (their
	// "parent"), creating the linked-list commonly known as the blockchain. Their
	// primary function is to bundle together transactions on the network. Blocks
	// are created by "miners," who collect transactions from other nodes, and
	// then try to pick a Nonce that results in a block whose BlockID is below a
	// given Target.
	Block struct {
		ParentID     BlockID
		Nonce        uint64
		Timestamp    Timestamp
		MinerPayouts []SiacoinOutput
		Transactions []Transaction
	}

	BlockHeight uint64
	BlockID     crypto.Hash
)

// CalculateCoinbase calculates the coinbase for a given height. The coinbase
// equation is:
//
//     coinbase := max(InitialCoinbase - height, MinimumCoinbase) * CoinbaseAugment
func CalculateCoinbase(height BlockHeight) (c Currency) {
	base := InitialCoinbase - uint64(height)
	if uint64(height) > InitialCoinbase || base < MinimumCoinbase {
		base = MinimumCoinbase
	}
	return NewCurrency64(base).Mul(NewCurrency(CoinbaseAugment))
}

// ID returns the ID of a Block, which is calculated by hashing the
// concatenation of the block's parent's ID, nonce, and the result of the
// b.MerkleRoot().
func (b Block) ID() BlockID {
	return BlockID(crypto.HashAll(
		b.ParentID,
		b.Nonce,
		b.MerkleRoot(),
	))
}

// CheckTarget returns true if the block's ID meets the given target.
func (b Block) CheckTarget(target Target) bool {
	blockHash := b.ID()
	return bytes.Compare(target[:], blockHash[:]) >= 0
}

// MerkleRoot calculates the Merkle root of a Block. The leaves of the Merkle
// tree are composed of the Timestamp, the miner outputs (one leaf per
// payout), and the transactions (one leaf per transaction).
func (b Block) MerkleRoot() crypto.Hash {
	tree := crypto.NewTree()
	tree.PushObject(b.Timestamp)
	for _, payout := range b.MinerPayouts {
		tree.PushObject(payout)
	}
	for _, txn := range b.Transactions {
		tree.PushObject(txn)
	}
	return tree.Root()
}

// MinerPayoutID returns the ID of the miner payout at the given index, which
// is calculated by hashing the concatenation of the BlockID and the payout
// index.
func (b Block) MinerPayoutID(i int) SiacoinOutputID {
	return SiacoinOutputID(crypto.HashAll(
		b.ID(),
		i,
	))
}