chain

package
v0.0.0-...-27cef4d Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2019 License: LGPL-3.0 Imports: 11 Imported by: 2 modules, 12 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	DataFetchHeightDiffMax = 100
)

consts

Variables

View Source
var (
	ErrAlreadyConnected   = errors.New("already connected")
	ErrAlreadyGenesised   = errors.New("already genesised")
	ErrForkDetected       = errors.New("fork detected")
	ErrInvalidVersion     = errors.New("invalid version")
	ErrInvalidPrevHash    = errors.New("invalid prev hash")
	ErrInvalidHeight      = errors.New("invalid height")
	ErrInvalidBodyHash    = errors.New("invalid body hash")
	ErrInvalidGenesisHash = errors.New("invalid genesis hash")
	ErrChainClosed        = errors.New("chain closed")
)

errors

View Source
var (
	HeaderMessageType  = message.DefineType("manager.HeaderMessage")
	DataMessageType    = message.DefineType("manager.DataMessage")
	RequestMessageType = message.DefineType("manager.RequestMessage")
	StatusMessageType  = message.DefineType("manager.StatusMessage")
)

types

Functions

This section is empty.

Types

type Base

type Base struct {
	Version_   uint16
	Height_    uint32
	PrevHash_  hash.Hash256
	Timestamp_ uint64
}

Base implements common functions of a header

func (*Base) Height

func (ch *Base) Height() uint32

Height returns the height of the header

func (*Base) PrevHash

func (ch *Base) PrevHash() hash.Hash256

PrevHash returns the prev hash of the header

func (*Base) ReadFrom

func (ch *Base) ReadFrom(r io.Reader) (int64, error)

ReadFrom is a deserialization function

func (*Base) Timestamp

func (ch *Base) Timestamp() uint64

Timestamp returns the timestamp of the header

func (*Base) Version

func (ch *Base) Version() uint16

Version returns the version of the header

func (*Base) WriteTo

func (ch *Base) WriteTo(w io.Writer) (int64, error)

WriteTo is a serialization function

type Body

type Body interface {
	io.WriterTo
	io.ReaderFrom
}

Body includes chain informations

type Chain

type Chain interface {
	Provider() Provider
	Screening(cd *Data) error
	CheckFork(ch Header, sigs []common.Signature) error
	Process(cd *Data, UserData interface{}) error
	DebugLog(args ...interface{})
}

Chain validates and stores the chain data

type Data

type Data struct {
	Header     Header
	Body       Body
	Signatures []common.Signature
}

Data is a unit of the chain

func (*Data) ReadFrom

func (cd *Data) ReadFrom(r io.Reader) (int64, error)

ReadFrom is a deserialization function

func (*Data) WriteTo

func (cd *Data) WriteTo(w io.Writer) (int64, error)

WriteTo is a serialization function

type DataMessage

type DataMessage struct {
	Data *Data
}

DataMessage used to send a chain data to a peer

func (*DataMessage) ReadFrom

func (msg *DataMessage) ReadFrom(r io.Reader) (int64, error)

ReadFrom is a deserialization function

func (*DataMessage) Type

func (msg *DataMessage) Type() message.Type

Type returns message type

func (*DataMessage) WriteTo

func (msg *DataMessage) WriteTo(w io.Writer) (int64, error)

WriteTo is a serialization function

type Header interface {
	io.WriterTo
	io.ReaderFrom
	Version() uint16
	Height() uint32
	PrevHash() hash.Hash256
	Timestamp() uint64
	Hash() hash.Hash256
}

Header includes validation informations

type HeaderMessage

type HeaderMessage struct {
	Header     Header
	Signatures []common.Signature
}

HeaderMessage used to send a chain header to a peer

func (*HeaderMessage) ReadFrom

func (msg *HeaderMessage) ReadFrom(r io.Reader) (int64, error)

ReadFrom is a deserialization function

func (*HeaderMessage) Type

func (msg *HeaderMessage) Type() message.Type

Type returns message type

func (*HeaderMessage) WriteTo

func (msg *HeaderMessage) WriteTo(w io.Writer) (int64, error)

WriteTo is a serialization function

type Manager

type Manager struct {
	sync.Mutex
	Mesh mesh.Mesh
	// contains filtered or unexported fields
}

Manager synchronizes the chain via the mesh

func NewManager

func NewManager(cn Chain) *Manager

NewManager returns a Manager

func (*Manager) AddData

func (cm *Manager) AddData(cd *Data) error

AddData pushes a chain data to the chain queue

func (*Manager) BroadcastHeader

func (cm *Manager) BroadcastHeader(ch Header)

BroadcastHeader sends the header to all of its peers

func (*Manager) IsExistData

func (cm *Manager) IsExistData(Height uint32) bool

IsExistData returns the chain data is exist or not in the data queue or the chain

func (*Manager) OnConnected

func (cm *Manager) OnConnected(p mesh.Peer)

OnConnected is called after accepting a peer to the peer list

func (*Manager) OnDisconnected

func (cm *Manager) OnDisconnected(p mesh.Peer)

OnDisconnected is called when the peer is closed

func (*Manager) OnRecv

func (cm *Manager) OnRecv(p mesh.Peer, r io.Reader, t message.Type) error

OnRecv is called when a message is received from the peer

func (*Manager) OnTimerExpired

func (cm *Manager) OnTimerExpired(height uint32, ID string)

OnTimerExpired is called when request is expired

func (*Manager) Process

func (cm *Manager) Process(cd *Data, UserData interface{}) error

Process processes the chain data

func (*Manager) Provider

func (cm *Manager) Provider() Provider

Provider returns the provider of the chain

func (*Manager) Run

func (cm *Manager) Run()

Run is the main loop of Manager

type Provider

type Provider interface {
	CreateHeader() Header
	CreateBody() Body
	Version() uint16
	Height() uint32
	LastHash() hash.Hash256
	Hash(height uint32) (hash.Hash256, error)
	Header(height uint32) (Header, error)
	Data(height uint32) (*Data, error)
}

Provider is a interface to give a chain data

type RequestExpireHandler

type RequestExpireHandler interface {
	OnTimerExpired(height uint32, ID string)
}

RequestExpireHandler handles a request expire event

type RequestMessage

type RequestMessage struct {
	Height uint32
}

RequestMessage used to request a chain data to a peer

func (*RequestMessage) ReadFrom

func (msg *RequestMessage) ReadFrom(r io.Reader) (int64, error)

ReadFrom is a deserialization function

func (*RequestMessage) Type

func (msg *RequestMessage) Type() message.Type

Type returns message type

func (*RequestMessage) WriteTo

func (msg *RequestMessage) WriteTo(w io.Writer) (int64, error)

WriteTo is a serialization function

type RequestTimer

type RequestTimer struct {
	sync.Mutex
	// contains filtered or unexported fields
}

RequestTimer triggers a event when a request is expired

func NewRequestTimer

func NewRequestTimer(handler RequestExpireHandler) *RequestTimer

NewRequestTimer returns a RequestTimer

func (*RequestTimer) Add

func (rm *RequestTimer) Add(height uint32, t time.Duration, p interface{}, ID string)

Add adds the timer of the request

func (*RequestTimer) Exist

func (rm *RequestTimer) Exist(height uint32) bool

Exist returns the target height request exists or not

func (*RequestTimer) Remove

func (rm *RequestTimer) Remove(height uint32)

Remove removes the timer of the request

func (*RequestTimer) Run

func (rm *RequestTimer) Run()

Run is the main loop of RequestTimer

type Status

type Status struct {
	Version  uint16
	Height   uint32
	LastHash hash.Hash256
}

Status is a status of a peer

type StatusMessage

type StatusMessage struct {
	Version  uint16
	Height   uint32
	LastHash hash.Hash256
}

StatusMessage used to provide the chain information to a peer

func (*StatusMessage) ReadFrom

func (msg *StatusMessage) ReadFrom(r io.Reader) (int64, error)

ReadFrom is a deserialization function

func (*StatusMessage) Type

func (msg *StatusMessage) Type() message.Type

Type returns message type

func (*StatusMessage) WriteTo

func (msg *StatusMessage) WriteTo(w io.Writer) (int64, error)

WriteTo is a serialization function

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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