core

package
v1.4.4-rc.4 Latest Latest
Warning

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

Go to latest
Published: May 19, 2021 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Block

type Block struct {
	Metadata
	PrntID ids.ID `serialize:"true" json:"parentID"` // parent's ID
	Hght   uint64 `serialize:"true" json:"height"`   // This block's height. The genesis block is at height 0.
	VM     *SnowmanVM
}

Block contains fields and methods common to block's in a Snowman blockchain. Block is meant to be a building-block (pun intended). When you write a VM, your blocks can (and should) embed a core.Block to take care of some bioler-plate code. Block's methods can be over-written by structs that embed this struct.

func NewBlock

func NewBlock(parentID ids.ID, height uint64) *Block

NewBlock returns a new *Block

func (*Block) Accept

func (b *Block) Accept() error

Accept sets this block's status to Accepted and sets lastAccepted to this block's ID and saves this info to b.vm.DB Recall that b.vm.DB.Commit() must be called to persist to the DB

func (*Block) Height added in v0.8.0

func (b *Block) Height() uint64

Height returns this block's height. The genesis block has height 0.

func (*Block) Initialize

func (b *Block) Initialize(bytes []byte, vm *SnowmanVM)

Initialize sets [b.bytes] to bytes, sets [b.id] to hash([b.bytes]) Checks if [b]'s status is already stored in state. If so, [b] gets that status. Otherwise [b]'s status is Unknown.

func (*Block) Parent

func (b *Block) Parent() snowman.Block

Parent returns [b]'s parent

func (*Block) ParentID

func (b *Block) ParentID() ids.ID

ParentID returns [b]'s parent's ID

func (*Block) Reject

func (b *Block) Reject() error

Reject sets this block's status to Rejected and saves the status in state Recall that b.vm.DB.Commit() must be called to persist to the DB

func (*Block) Status

func (b *Block) Status() choices.Status

Status returns the status of this block

func (*Block) Verify

func (b *Block) Verify() (bool, error)

Verify returns: 1) true if the block is accepted 2) nil if this block is valid

type Metadata

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

Metadata contains the data common to all blocks and transactions

func (*Metadata) Bytes

func (i *Metadata) Bytes() []byte

Bytes returns the byte repr. of this block/transaction

func (*Metadata) ID

func (i *Metadata) ID() ids.ID

ID returns the ID of this block/transaction

func (*Metadata) Initialize

func (i *Metadata) Initialize(bytes []byte)

Initialize sets [i.bytes] to bytes, sets [i.id] to a hash of [i.bytes] and sets [i.status] to choices.Processing

func (*Metadata) SetStatus

func (i *Metadata) SetStatus(status choices.Status)

SetStatus sets the status of this block/transaction

func (*Metadata) Status

func (i *Metadata) Status() choices.Status

Status returns the status of this block/transaction

type SnowmanState

type SnowmanState interface {
	state.State
	GetBlock(database.Database, ids.ID) (snowman.Block, error)
	PutBlock(database.Database, snowman.Block) error
	GetLastAccepted(database.Database) (ids.ID, error)
	PutLastAccepted(database.Database, ids.ID) error
}

SnowmanState is a wrapper around state.State In additions to the methods exposed by state.State, SnowmanState exposes a few methods needed for managing state in a snowman vm

func NewSnowmanState

func NewSnowmanState(unmarshalBlockFunc func([]byte) (snowman.Block, error)) (SnowmanState, error)

NewSnowmanState returns a new SnowmanState

type SnowmanVM

type SnowmanVM struct {
	State SnowmanState

	// VersionDB on top of underlying database
	// Important note: In order for writes to [DB] to be persisted,
	// DB.Commit() must be called
	// We use a versionDB here so user can do atomic commits as they see fit
	DB *versiondb.Database

	// The context of this vm
	Ctx *snow.Context

	// ID of the last accepted block
	LastAcceptedID ids.ID

	// channel to send messages to the consensus engine
	ToEngine chan<- common.Message
	// contains filtered or unexported fields
}

SnowmanVM provides the core functionality shared by most snowman vms

func (*SnowmanVM) Bootstrapped added in v0.8.0

func (svm *SnowmanVM) Bootstrapped() error

Bootstrapped marks this VM as bootstrapped

func (*SnowmanVM) Bootstrapping added in v0.8.0

func (svm *SnowmanVM) Bootstrapping() error

Bootstrapping marks this VM as bootstrapping

func (*SnowmanVM) DBInitialized

func (svm *SnowmanVM) DBInitialized() bool

DBInitialized returns true iff [svm]'s database has values in it already

func (*SnowmanVM) GetBlock

func (svm *SnowmanVM) GetBlock(id ids.ID) (snowman.Block, error)

GetBlock returns the block with ID [ID]

func (*SnowmanVM) Initialize

func (svm *SnowmanVM) Initialize(
	ctx *snow.Context,
	db database.Database,
	unmarshalBlockFunc func([]byte) (snowman.Block, error),
	toEngine chan<- common.Message,
) error

Initialize this vm. If there is data in [db], sets [svm.lastAccepted] using data in the database, and sets [svm.preferred] to the last accepted block.

func (*SnowmanVM) LastAccepted

func (svm *SnowmanVM) LastAccepted() (ids.ID, error)

LastAccepted returns the block most recently accepted

func (*SnowmanVM) NewHandler

func (svm *SnowmanVM) NewHandler(name string, service interface{}, lockOption ...common.LockOption) (*common.HTTPHandler, error)

NewHandler returns a new Handler for a service where:

  • The handler's functionality is defined by [service] [service] should be a gorilla RPC service (see https://www.gorillatoolkit.org/pkg/rpc/v2)
  • The name of the service is [name]
  • The LockOption is the first element of [lockOption] By default the LockOption is WriteLock [lockOption] should have either 0 or 1 elements. Elements beside the first are ignored.

func (*SnowmanVM) NotifyBlockReady

func (svm *SnowmanVM) NotifyBlockReady()

NotifyBlockReady tells the consensus engine that a new block is ready to be created

func (*SnowmanVM) ParseBlock

func (svm *SnowmanVM) ParseBlock(bytes []byte) (snowman.Block, error)

ParseBlock parses bytes to a block

func (*SnowmanVM) Preferred

func (svm *SnowmanVM) Preferred() ids.ID

Preferred returns the ID of the preferred block

func (*SnowmanVM) SaveBlock

func (svm *SnowmanVM) SaveBlock(db database.Database, block snowman.Block) error

SaveBlock saves [block] to state

func (*SnowmanVM) SetDBInitialized

func (svm *SnowmanVM) SetDBInitialized() error

SetDBInitialized marks the database as initialized

func (*SnowmanVM) SetPreference

func (svm *SnowmanVM) SetPreference(id ids.ID) error

SetPreference sets the block with ID [ID] as the preferred block

func (*SnowmanVM) Shutdown

func (svm *SnowmanVM) Shutdown() error

Shutdown this vm

Jump to

Keyboard shortcuts

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