block

package
v0.0.0-...-f0b43a4 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2020 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package block contains the block struct and functions to transform a block

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractContractsFromBlock

func ExtractContractsFromBlock(b Block) ([]*contracts.Contract, error)

ExtractContractsFromBlock returns contract slice based on block data

func HashBlock

func HashBlock(b Block) []byte

Concatenate all the fields of the block header and return its SHA256 hash

func HashBlockHeader

func HashBlockHeader(b BlockHeader) []byte

Concatenate all the fields of the block header and return its SHA256 hash

Types

type Block

type Block struct {
	Version        uint16   // Version is the version of the software this block was created with
	Height         uint64   // Height is the distance from the bottom of the tree, with the genesis block starting with height 0
	Timestamp      int64    // Timestamp is the time of creation for this block
	PreviousHash   []byte   // PreviousHash is the hash of the previous block in the blockchain,
	MerkleRootHash []byte   // MerkleRootHash is the hash of the MerkleRoot of all inputs
	DataLen        uint16   // DataLen is the number of objects in the following Data variable
	Data           [][]byte // Data is an abritrary variable, holding the actual contents of this block
}

Block is a struct that represents a block in a blockchain.

func Deserialize

func Deserialize(block []byte) Block

Converts a block in byte form into a block struct, returns the struct

func New

func New(version uint16, height uint64, previousHash []byte, data []contracts.Contract) (Block, error)

func (*Block) Equals

func (block1 *Block) Equals(block2 Block) bool

Compares two block structs and returns true if all the fields in both blocks are equal, false otherwise

func (*Block) GetHeader

func (b *Block) GetHeader() BlockHeader

func (*Block) Marshal

func (b *Block) Marshal() JSONBlock

func (*Block) Serialize

func (b *Block) Serialize() []byte

Produces a byte string based on the block struct provided

Block Header Structure:

Bytes 0-2   : Version
Bytes 2-10  : Height
Bytes 10-18 : Timestamp
Bytes 18-50 : Previous Hash
Bytes 50-82 : Merkle Root Hash
Bytes 82-84 : Data Length

func (Block) ToString

func (b Block) ToString() string

Returns a string of the block

type BlockHeader

type BlockHeader struct {
	Version        uint16
	Height         uint64
	Timestamp      int64
	PreviousHash   []byte
	MerkleRootHash []byte
}

type JSONBlock

type JSONBlock struct {
	Version        uint16
	Height         uint64
	Timestamp      int64
	PreviousHash   string
	MerkleRootHash string
	DataLen        uint16
	Data           []string
}

Allows for easy Marshaling into a JSON string

func (*JSONBlock) Unmarshal

func (jB *JSONBlock) Unmarshal() (Block, error)

Unmarshal converts a JSONBlock to a Block

Jump to

Keyboard shortcuts

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