blockchain

package
v0.0.0-...-77dcbbd Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package blockchain implements common blockchain concepts and logic to store them for the Lisk protocol. Block, Transaction, Asset, Event are handled in this package.

Index

Constants

View Source
const (
	EventMaxSizeBytes          uint32 = 1024
	EventTotalIndexLengthBytes uint32 = (eventIndexLengthBits + eventTopicIndexLengthBits) / 8
	EventIDLengthBytes         uint32 = 4 + EventTotalIndexLengthBytes
)
View Source
const (
	GenesisBlockVersion = 0
)
View Source
const (
	MaxTransactionParamsSize = 14 * 1024
)

Variables

View Source
var (
	AddressLength = 20
	IDLength      = 32
)
View Source
var (
	EventNameDefault       = "commandExecutionResult"
	EventMaxTopicsPerEvent = uint32(math.Pow(binaryBase, float64(eventTopicIndexLengthBits)))
	MaxEventsPerBlock      = uint32(math.Pow(binaryBase, float64(eventIndexLengthBits)))
)
View Source
var (
	TagBlockHeader = []byte("LSK_BH_")
	TagTransaction = []byte("LSK_TX_")
)

Functions

func CalculateEventRoot

func CalculateEventRoot(events []*Event) ([]byte, error)

func DBPrefixToBytes

func DBPrefixToBytes(prefix DBPrefix) []byte

DBPrefixToBytes aconverts prefix to slice.

func NewStandardTransactionEventData

func NewStandardTransactionEventData(success bool) []byte

func SortBlockByHeightAsc

func SortBlockByHeightAsc(blocks []*Block)

SortBlockByHeightAsc sorts block with height asc.

func SortBlockByHeightDesc

func SortBlockByHeightDesc(blocks []*Block)

SortBlockByHeightDesc sorts block with height desc.

func ValidateBlockSignature

func ValidateBlockSignature(publicKey, signature, chainID, signingBytes []byte) bool

Types

type AggregateCommit

type AggregateCommit struct {
	Height               uint32    `json:"height" fieldNumber:"1"`
	AggregationBits      codec.Hex `json:"aggregationBits" fieldNumber:"2"`
	CertificateSignature codec.Hex `json:"certificateSignature" fieldNumber:"3"`
}

func (*AggregateCommit) Decode

func (e *AggregateCommit) Decode(data []byte) error

func (*AggregateCommit) DecodeFromReader

func (e *AggregateCommit) DecodeFromReader(reader *codec.Reader) error

func (*AggregateCommit) DecodeStrict

func (e *AggregateCommit) DecodeStrict(data []byte) error

func (*AggregateCommit) DecodeStrictFromReader

func (e *AggregateCommit) DecodeStrictFromReader(reader *codec.Reader) error

func (*AggregateCommit) Empty

func (c *AggregateCommit) Empty() bool

func (*AggregateCommit) Encode

func (e *AggregateCommit) Encode() []byte

func (*AggregateCommit) MustDecode

func (e *AggregateCommit) MustDecode(data []byte)

type Block

type Block struct {
	Header       *BlockHeader   `fieldNumber:"1" json:"header"`
	Transactions []*Transaction `fieldNumber:"2" json:"transactions"`
	Assets       []*BlockAsset  `fieldNumber:"3" json:"assets"`
}

Block is an envelope for header and payload.

func NewBlock

func NewBlock(val []byte) (*Block, error)

NewBlock from val and assign ID.

func NewGenesisBlock

func NewGenesisBlock(height, timestamp uint32, previoudBlockID codec.Hex, assets BlockAssets) *Block

NewGenesisBlock returns a valid genesis block but without validatorsHash and stateRoot.

func (*Block) Decode

func (e *Block) Decode(data []byte) error

func (*Block) DecodeFromReader

func (e *Block) DecodeFromReader(reader *codec.Reader) error

func (*Block) DecodeStrict

func (e *Block) DecodeStrict(data []byte) error

func (*Block) DecodeStrictFromReader

func (e *Block) DecodeStrictFromReader(reader *codec.Reader) error

func (*Block) Encode

func (e *Block) Encode() []byte

func (*Block) Init

func (b *Block) Init()

Init the calculated value if decoded directly without NewBlock.

func (*Block) MustDecode

func (e *Block) MustDecode(data []byte)

func (*Block) Validate

func (b *Block) Validate() error

Validate a block.

func (*Block) ValidateGenesis

func (b *Block) ValidateGenesis() error

ValidateGenesis validates block specific for genesis.

type BlockAsset

type BlockAsset struct {
	Module string    `fieldNumber:"1" json:"module"`
	Data   codec.Hex `fieldNumber:"2" json:"data"`
}

func NewBlockAsset

func NewBlockAsset(val []byte) (*BlockAsset, error)

func (*BlockAsset) Decode

func (e *BlockAsset) Decode(data []byte) error

func (*BlockAsset) DecodeFromReader

func (e *BlockAsset) DecodeFromReader(reader *codec.Reader) error

func (*BlockAsset) DecodeStrict

func (e *BlockAsset) DecodeStrict(data []byte) error

func (*BlockAsset) DecodeStrictFromReader

func (e *BlockAsset) DecodeStrictFromReader(reader *codec.Reader) error

func (*BlockAsset) Encode

func (e *BlockAsset) Encode() []byte

func (*BlockAsset) MustDecode

func (e *BlockAsset) MustDecode(data []byte)

type BlockAssets

type BlockAssets []*BlockAsset

func (BlockAssets) GetAsset

func (a BlockAssets) GetAsset(module string) ([]byte, bool)

func (BlockAssets) GetRoot

func (a BlockAssets) GetRoot() []byte

func (*BlockAssets) SetAsset

func (a *BlockAssets) SetAsset(module string, data []byte)

func (*BlockAssets) Sort

func (a *BlockAssets) Sort()

func (BlockAssets) Valid

func (a BlockAssets) Valid() error

type BlockHeader

type BlockHeader struct {
	ID                 codec.Hex        `json:"id"`
	Version            uint32           `json:"version" fieldNumber:"1"`
	Timestamp          uint32           `json:"timestamp" fieldNumber:"2"`
	Height             uint32           `json:"height" fieldNumber:"3"`
	PreviousBlockID    codec.Hex        `json:"previousBlockID" fieldNumber:"4"`
	GeneratorAddress   codec.Lisk32     `json:"generatorAddress" fieldNumber:"5"`
	TransactionRoot    codec.Hex        `json:"transactionRoot" fieldNumber:"6"`
	AssetRoot          codec.Hex        `json:"assetRoot" fieldNumber:"7"`
	EventRoot          codec.Hex        `json:"eventRoot" fieldNumber:"8"`
	StateRoot          codec.Hex        `json:"stateRoot" fieldNumber:"9"`
	MaxHeightPrevoted  uint32           `json:"maxHeightPrevoted" fieldNumber:"10"`
	MaxHeightGenerated uint32           `json:"maxHeightGenerated" fieldNumber:"11"`
	ImpliesMaxPrevotes bool             `json:"impliesMaxPrevotes" fieldNumber:"12"`
	ValidatorsHash     codec.Hex        `json:"validatorsHash" fieldNumber:"13"`
	AggregateCommit    *AggregateCommit `json:"aggregateCommit" fieldNumber:"14"`
	Signature          codec.Hex        `json:"signature" fieldNumber:"15"`
}

BlockHeader holds general block header regardless of version.

func NewBlockHeader

func NewBlockHeader(value []byte) (*BlockHeader, error)

NewBlockHeader create blockheader instance from encoded value.

func NewBlockHeaderWithValues

func NewBlockHeaderWithValues(
	version uint32,
	timestamp uint32,
	height uint32,
	previousBlockID []byte,
	assetRoot []byte,
	stateRoot []byte,
	maxHeightPrevoted uint32,
	maxHeightPreviouslyForged uint32,
	transactionRoot []byte,
	generatorPublicKey []byte,
	validatorHash []byte,
	aggregateCommit *AggregateCommit,
	signature []byte,
) (*BlockHeader, error)

func (*BlockHeader) Decode

func (e *BlockHeader) Decode(data []byte) error

func (*BlockHeader) DecodeFromReader

func (e *BlockHeader) DecodeFromReader(reader *codec.Reader) error

func (*BlockHeader) DecodeStrict

func (e *BlockHeader) DecodeStrict(data []byte) error

func (*BlockHeader) DecodeStrictFromReader

func (e *BlockHeader) DecodeStrictFromReader(reader *codec.Reader) error

func (*BlockHeader) Encode

func (e *BlockHeader) Encode() []byte

func (*BlockHeader) Init

func (b *BlockHeader) Init()

func (*BlockHeader) MustDecode

func (e *BlockHeader) MustDecode(data []byte)

func (*BlockHeader) Readonly

func (b *BlockHeader) Readonly() SealedBlockHeader

func (*BlockHeader) Sign

func (b *BlockHeader) Sign(chainID, privateKey []byte)

func (*BlockHeader) SigningBytes

func (b *BlockHeader) SigningBytes() []byte

SigningBytes return signed part of the block header.

func (*BlockHeader) Validate

func (b *BlockHeader) Validate() error

Validate block header property.

func (*BlockHeader) ValidateGenesis

func (b *BlockHeader) ValidateGenesis() error

ValidateGenesis validates block header specific for genesis.

func (*BlockHeader) VerifySignature

func (b *BlockHeader) VerifySignature(chainID, publicKey []byte) bool

type Chain

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

Chain holds information of a particular chain.

func NewChain

func NewChain(cfg *ChainConfig) *Chain

NewChain returns new chain with config.

func (*Chain) AddBlock

func (c *Chain) AddBlock(batch *db.Batch, block *Block, events []*Event, finalizedHeight uint32, removeTemp bool) error

AddBlock adds a block on top of the current chain.

func (*Chain) ChainID

func (c *Chain) ChainID() []byte

ChainID is getter for network identifier for the chain.

func (*Chain) DataAccess

func (c *Chain) DataAccess() *DataAccess

DataAccess returns access to DB.

func (*Chain) GenesisBlockExist

func (c *Chain) GenesisBlockExist(block *Block) (bool, error)

GenesisBlockExist returns true if matching genesis block exist.

func (*Chain) GetLastNBlocks

func (c *Chain) GetLastNBlocks(n int) ([]*Block, error)

GetLastNBlock returns latest N blocks exists.

func (*Chain) Init

func (c *Chain) Init(genesisBlock *Block, db *db.DB)

Init the chain.

func (*Chain) LastBlock

func (c *Chain) LastBlock() *Block

LastBlock returns latest block on the chain.

func (*Chain) PrepareCache

func (c *Chain) PrepareCache() error

PrepareCache block cache.

func (*Chain) RemoveBlock

func (c *Chain) RemoveBlock(batch *db.Batch, saveTemp bool) error

RemoveBlock removes last block from the top of the current chain.

type ChainConfig

type ChainConfig struct {
	ChainID               codec.Hex
	MaxTransactionsLength uint32
	MaxBlockCache         int
	KeepEventsForHeights  int
}

ChainConfig is for config value of chain.

type DBPrefix

type DBPrefix uint8

DBPrefix is a type to define prefix for the data type

var (
	DBPrefixState     DBPrefix = 10
	DBPrefixStateDiff DBPrefix = 51
)

DBPrefix for all the low level common data.

type DataAccess

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

DataAccess gives access to the blockchain data.

func NewDataAccess

func NewDataAccess(db *db.DB, maxCacheSize, keepEventsForHeights int) *DataAccess

NewDataAccess returns new instance of data access.

func (*DataAccess) Cache

func (d *DataAccess) Cache(block *Block) error

Cache a new block.

func (*DataAccess) Cached

func (d *DataAccess) Cached(height uint32) bool

func (*DataAccess) CachedLastBlock

func (d *DataAccess) CachedLastBlock() *Block

CachedLastBlock returns the latest block without popping.

func (*DataAccess) ClearTempBlocks

func (d *DataAccess) ClearTempBlocks()

ClearTempBlocks removes all temp blocks.

func (*DataAccess) GetBlock

func (d *DataAccess) GetBlock(id []byte) (*Block, error)

GetBlock returns a block by id if exist.

func (*DataAccess) GetBlockByHeight

func (d *DataAccess) GetBlockByHeight(height uint32) (*Block, error)

GetBlockByHeight returns block by height.

func (*DataAccess) GetBlockHeader

func (d *DataAccess) GetBlockHeader(id []byte) (*BlockHeader, error)

GetBlockHeader returns block header if data exists.

func (*DataAccess) GetBlockHeaderByHeight

func (d *DataAccess) GetBlockHeaderByHeight(height uint32) (*BlockHeader, error)

GetBlockHeaderByHeight returns block header with height.

func (*DataAccess) GetBlockHeaders

func (d *DataAccess) GetBlockHeaders(ids [][]byte) ([]*BlockHeader, error)

GetBlockHeaders returns all block header with id if exist.

func (*DataAccess) GetBlockHeadersByHeights

func (d *DataAccess) GetBlockHeadersByHeights(heights []uint32) ([]*BlockHeader, error)

GetBlockHeadersByHeights returns all block header with heights.

func (*DataAccess) GetBlocksBetweenHeight

func (d *DataAccess) GetBlocksBetweenHeight(from, to uint32) ([]*Block, error)

GetBlocksBetweenHeight returns all block between the height.

func (*DataAccess) GetEvents

func (d *DataAccess) GetEvents(height uint32) ([]*Event, error)

func (*DataAccess) GetFinalizedHeight

func (d *DataAccess) GetFinalizedHeight() (uint32, error)

func (*DataAccess) GetLastBlock

func (d *DataAccess) GetLastBlock() (*Block, error)

GetLastBlock of the blockcchain.

func (*DataAccess) GetLastBlockHeader

func (d *DataAccess) GetLastBlockHeader() (*BlockHeader, error)

GetLastBlockHeader returns last block header.

func (*DataAccess) GetTempBlocks

func (d *DataAccess) GetTempBlocks() ([]*Block, error)

GetTempBlocks returns all temp block exisst.

func (*DataAccess) GetTransaction

func (d *DataAccess) GetTransaction(id []byte) (*Transaction, error)

GetTransaction return transaction by id.

func (*DataAccess) GetTransactions

func (d *DataAccess) GetTransactions(ids [][]byte) ([]*Transaction, error)

GetTransaction return transaction by id.

func (*DataAccess) RemoveCache

func (d *DataAccess) RemoveCache()

RemoveCache removes last block from cache.

type Event

type Event struct {
	ID     codec.Hex   `json:"id"`
	Module string      `fieldNumber:"1" json:"module"`
	Name   string      `fieldNumber:"2" json:"name"`
	Data   codec.Hex   `fieldNumber:"3" json:"data"`
	Topics []codec.Hex `fieldNumber:"4" json:"topics"`
	Height uint32      `fieldNumber:"5" json:"height"`
	Index  uint32      `fieldNumber:"6" json:"index"`
}

func NewEvent

func NewEvent(val []byte) (*Event, error)

func NewEventFromValues

func NewEventFromValues(module string, name string, data []byte, topics []codec.Hex, height, index uint32) *Event

func (*Event) Decode

func (e *Event) Decode(data []byte) error

func (*Event) DecodeFromReader

func (e *Event) DecodeFromReader(reader *codec.Reader) error

func (*Event) DecodeStrict

func (e *Event) DecodeStrict(data []byte) error

func (*Event) DecodeStrictFromReader

func (e *Event) DecodeStrictFromReader(reader *codec.Reader) error

func (*Event) Encode

func (e *Event) Encode() []byte

func (*Event) KeyPairs

func (e *Event) KeyPairs() []EventKeyPair

func (*Event) MustDecode

func (e *Event) MustDecode(data []byte)

func (*Event) UpdateID

func (e *Event) UpdateID() []byte

func (*Event) Validate

func (e *Event) Validate() error

type EventKeyPair

type EventKeyPair struct {
	Key   codec.Hex
	Value codec.Hex
}

type Events

type Events []*Event

func (*Events) UpdateIndex

func (e *Events) UpdateIndex()

type FrozenTransaction

type FrozenTransaction interface {
	Params() []byte
	Module() string
	Command() string
	Fee() uint64
	Nonce() uint64
	SenderAddress() codec.Lisk32
	SenderPublicKey() codec.Hex
	Signatures() [][]byte
	Size() int
	SigningBytes() []byte
}

type RawBlock

type RawBlock struct {
	Header       []byte   `fieldNumber:"1"`
	Transactions [][]byte `fieldNumber:"2"`
	Assets       [][]byte `fieldNumber:"3"`
}

RawBlock represents a envelope of a block header.

func (*RawBlock) Decode

func (e *RawBlock) Decode(data []byte) error

func (*RawBlock) DecodeFromReader

func (e *RawBlock) DecodeFromReader(reader *codec.Reader) error

func (*RawBlock) DecodeStrict

func (e *RawBlock) DecodeStrict(data []byte) error

func (*RawBlock) DecodeStrictFromReader

func (e *RawBlock) DecodeStrictFromReader(reader *codec.Reader) error

func (*RawBlock) Encode

func (e *RawBlock) Encode() []byte

func (*RawBlock) MustDecode

func (e *RawBlock) MustDecode(data []byte)

type ReadableBlockAssets

type ReadableBlockAssets interface {
	GetAsset(module string) ([]byte, bool)
}

type ReadableBlockHeader

type ReadableBlockHeader interface {
	Version() uint32
	Height() uint32
	Timestamp() uint32
	PreviousBlockID() codec.Hex
	GeneratorAddress() codec.Lisk32
	AggregateCommit() *AggregateCommit
	MaxHeightPrevoted() uint32
	MaxHeightGenerated() uint32
	ImpliesMaxPrevotes() bool
}

type SealedBlockHeader

type SealedBlockHeader interface {
	ReadableBlockHeader
	ID() []byte
	Signature() []byte
	SigningBytes() []byte
	TransactionRoot() []byte
	AssetRoot() []byte
	StateRoot() []byte
	ValidatorsHash() []byte
}

type SigningTransaction

type SigningTransaction struct {
	Module          string `fieldNumber:"1"`
	Command         string `fieldNumber:"2"`
	Nonce           uint64 `fieldNumber:"3"`
	Fee             uint64 `fieldNumber:"4"`
	SenderPublicKey []byte `fieldNumber:"5"`
	Params          []byte `fieldNumber:"6"`
}

SigningTransaction holds signed part of the transaction.

func (*SigningTransaction) Decode

func (e *SigningTransaction) Decode(data []byte) error

func (*SigningTransaction) DecodeFromReader

func (e *SigningTransaction) DecodeFromReader(reader *codec.Reader) error

func (*SigningTransaction) DecodeStrict

func (e *SigningTransaction) DecodeStrict(data []byte) error

func (*SigningTransaction) DecodeStrictFromReader

func (e *SigningTransaction) DecodeStrictFromReader(reader *codec.Reader) error

func (*SigningTransaction) Encode

func (e *SigningTransaction) Encode() []byte

func (*SigningTransaction) MustDecode

func (e *SigningTransaction) MustDecode(data []byte)

type StandardTransactionEvent

type StandardTransactionEvent struct {
	Success bool `fieldNumber:"1"`
}

func (*StandardTransactionEvent) Decode

func (e *StandardTransactionEvent) Decode(data []byte) error

func (*StandardTransactionEvent) DecodeFromReader

func (e *StandardTransactionEvent) DecodeFromReader(reader *codec.Reader) error

func (*StandardTransactionEvent) DecodeStrict

func (e *StandardTransactionEvent) DecodeStrict(data []byte) error

func (*StandardTransactionEvent) DecodeStrictFromReader

func (e *StandardTransactionEvent) DecodeStrictFromReader(reader *codec.Reader) error

func (*StandardTransactionEvent) Encode

func (e *StandardTransactionEvent) Encode() []byte

func (*StandardTransactionEvent) MustDecode

func (e *StandardTransactionEvent) MustDecode(data []byte)

type Transaction

type Transaction struct {
	ID codec.Hex `json:"id"`

	Module          string      `json:"module" fieldNumber:"1"`
	Command         string      `json:"commandID" fieldNumber:"2"`
	Nonce           uint64      `json:"nonce,string" fieldNumber:"3"`
	Fee             uint64      `json:"fee,string" fieldNumber:"4"`
	SenderPublicKey codec.Hex   `json:"senderPublicKey" fieldNumber:"5"`
	Params          codec.Hex   `json:"params" fieldNumber:"6"`
	Signatures      []codec.Hex `json:"signatures" fieldNumber:"7"`
	// contains filtered or unexported fields
}

Transaction holds general transaction.

func NewTransaction

func NewTransaction(value []byte) (*Transaction, error)

NewTransaction creates instance from decoded value.

func (*Transaction) Bytes

func (t *Transaction) Bytes() []byte

Bytes return transaction in bytes.

func (*Transaction) Copy

func (t *Transaction) Copy() *Transaction

func (*Transaction) Decode

func (e *Transaction) Decode(data []byte) error

func (*Transaction) DecodeFromReader

func (e *Transaction) DecodeFromReader(reader *codec.Reader) error

func (*Transaction) DecodeStrict

func (e *Transaction) DecodeStrict(data []byte) error

func (*Transaction) DecodeStrictFromReader

func (e *Transaction) DecodeStrictFromReader(reader *codec.Reader) error

func (*Transaction) Encode

func (e *Transaction) Encode() []byte

func (*Transaction) Freeze

func (t *Transaction) Freeze() FrozenTransaction

Bytes return transaction in bytes.

func (*Transaction) GetSignature

func (t *Transaction) GetSignature(chainID, privateKey []byte) []byte

func (*Transaction) Init

func (t *Transaction) Init()

func (*Transaction) MustDecode

func (e *Transaction) MustDecode(data []byte)

func (*Transaction) SenderAddress

func (t *Transaction) SenderAddress() codec.Lisk32

func (*Transaction) SigningBytes

func (t *Transaction) SigningBytes() []byte

SigningBytes return signed byte so the transaction.

func (*Transaction) Size

func (t *Transaction) Size() int

Size returns the transaction size.

func (*Transaction) Validate

func (t *Transaction) Validate() error

Validate transaction statically.

type WritableBlockAssets

type WritableBlockAssets interface {
	ReadableBlockAssets
	SetAsset(module string, data []byte)
}

Jump to

Keyboard shortcuts

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