node

package
v0.0.0-...-24e5678 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2023 License: LGPL-3.0 Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const BlockTime = 500 * time.Millisecond

BlockTime defines the block generation interval

Variables

View Source
var (
	ErrInsufficientCandidateCount   = errors.New("insufficient candidate count")
	ErrExceedCandidateCount         = errors.New("exceed candidate count")
	ErrInvalidMaxBlocksPerGenerator = errors.New("invalid max blocks per generator")
	ErrInvalidObserverKey           = errors.New("invalid observer key")
	ErrInvalidTopAddress            = errors.New("invalid top address")
	ErrInvalidTopSignature          = errors.New("invalid top signature")
	ErrInvalidSignatureCount        = errors.New("invalid signature count")
	ErrInvalidPhase                 = errors.New("invalid phase")
	ErrExistAddress                 = errors.New("exist address")
	ErrFoundForkedBlockGen          = errors.New("found forked block gen")
	ErrInvalidVote                  = errors.New("invalid vote")
	ErrInvalidRoundState            = errors.New("invalid round state")
	ErrInvalidRequest               = errors.New("invalid request")
	ErrAlreadyVoted                 = errors.New("already voted")
	ErrNotExistObserverPeer         = errors.New("not exist observer peer")
	ErrNotExistGeneratorPeer        = errors.New("not exist generator peer")
	ErrActiveGeneratorTimout        = errors.New("timeout for active generator")
)

consensus errors

View Source
var (
	RoundVoteMessageType       = p2p.RegisterSerializableType(&RoundVoteMessage{})
	RoundVoteAckMessageType    = p2p.RegisterSerializableType(&RoundVoteAckMessage{})
	BlockReqMessageType        = p2p.RegisterSerializableType(&BlockReqMessage{})
	BlockGenMessageType        = p2p.RegisterSerializableType(&BlockGenMessage{})
	BlockVoteMessageType       = p2p.RegisterSerializableType(&BlockVoteMessage{})
	BlockObSignMessageType     = p2p.RegisterSerializableType(&BlockObSignMessage{})
	BlockGenRequestMessageType = p2p.RegisterSerializableType(&BlockGenRequestMessage{})
)
View Source
var (
	DEBUG = false
)
View Source
var RoundStates = [...]string{
	"EmptyState       ",
	"RoundVoteState   ",
	"RoundVoteAckState",
	"BlockWaitState   ",
	"BlockVoteState   ",
}

Functions

This section is empty.

Types

type BlockGenMessage

type BlockGenMessage struct {
	Block              *types.Block
	GeneratorSignature common.Signature
	IsReply            bool
}

BlockGenMessage is a message for a block generation

func (*BlockGenMessage) ReadFrom

func (s *BlockGenMessage) ReadFrom(r io.Reader) (int64, error)

func (*BlockGenMessage) TypeID

func (s *BlockGenMessage) TypeID() uint32

func (*BlockGenMessage) WriteTo

func (s *BlockGenMessage) WriteTo(w io.Writer) (int64, error)

type BlockGenRequestMessage

type BlockGenRequestMessage struct {
	ChainID      *big.Int
	LastHash     hash.Hash256
	TargetHeight uint32
	TimeoutCount uint32
	Generator    common.Address
	Timestamp    uint64
}

BlockGenRequestMessage is a message to request block gen

func (*BlockGenRequestMessage) ReadFrom

func (s *BlockGenRequestMessage) ReadFrom(r io.Reader) (int64, error)

func (*BlockGenRequestMessage) TypeID

func (s *BlockGenRequestMessage) TypeID() uint32

func (*BlockGenRequestMessage) WriteTo

func (s *BlockGenRequestMessage) WriteTo(w io.Writer) (int64, error)

type BlockObSignMessage

type BlockObSignMessage struct {
	TargetHeight       uint32
	BlockSign          *types.BlockSign
	ObserverSignatures []common.Signature
}

BlockObSignMessage is a message for a block observer signatures

func (*BlockObSignMessage) ReadFrom

func (s *BlockObSignMessage) ReadFrom(r io.Reader) (int64, error)

func (*BlockObSignMessage) TypeID

func (s *BlockObSignMessage) TypeID() uint32

func (*BlockObSignMessage) WriteTo

func (s *BlockObSignMessage) WriteTo(w io.Writer) (int64, error)

type BlockReqMessage

type BlockReqMessage struct {
	PrevHash     hash.Hash256
	TargetHeight uint32
	TimeoutCount uint32
	Generator    common.Address
}

BlockReqMessage is a message for a block request

func (*BlockReqMessage) ReadFrom

func (s *BlockReqMessage) ReadFrom(r io.Reader) (int64, error)

func (*BlockReqMessage) TypeID

func (s *BlockReqMessage) TypeID() uint32

func (*BlockReqMessage) WriteTo

func (s *BlockReqMessage) WriteTo(w io.Writer) (int64, error)

type BlockRound

type BlockRound struct {
	BlockVoteMap            map[common.PublicKey]*BlockVoteMessage
	BlockGenMessage         *BlockGenMessage
	Context                 *types.Context
	Receipts                types.Receipts
	BlockVoteMessageWaitMap map[common.PublicKey]*BlockVoteMessage
	BlockGenMessageWait     *BlockGenMessage
	LastBlockGenRequestTime uint64
}

BlockRound is data for the block round

func NewBlockRound

func NewBlockRound() *BlockRound

NewBlockRound returns a VoteRound

type BlockVoteMessage

type BlockVoteMessage struct {
	TargetHeight       uint32
	Header             *types.Header
	GeneratorSignature common.Signature
	ObserverSignature  common.Signature
	IsReply            bool
}

BlockVoteMessage is message for a block vote

func (*BlockVoteMessage) ReadFrom

func (s *BlockVoteMessage) ReadFrom(r io.Reader) (int64, error)

func (*BlockVoteMessage) TypeID

func (s *BlockVoteMessage) TypeID() uint32

func (*BlockVoteMessage) WriteTo

func (s *BlockVoteMessage) WriteTo(w io.Writer) (int64, error)

type GeneratorConfig

type GeneratorConfig struct {
	MaxTransactionsPerBlock int
}

GeneratorConfig defines configuration of the generator

type GeneratorNode

type GeneratorNode struct {
	sync.Mutex
	ChainID *big.Int
	Config  *GeneratorConfig
	// contains filtered or unexported fields
}

GeneratorNode procudes a block by the consensus

func NewGeneratorNode

func NewGeneratorNode(ChainID *big.Int, Config *GeneratorConfig, cn *chain.Chain, key key.Key, ndkey key.Key, NetAddressMap map[common.PublicKey]string, SeedNodeMap map[common.PublicKey]string, peerStorePath string) *GeneratorNode

NewGeneratorNode returns a GeneratorNode

func (*GeneratorNode) ActiveGenerators

func (fr *GeneratorNode) ActiveGenerators() ([]common.Address, error)

ActiveGenerators returns the received active(=connected) generators from observer wait 2 seconds for respective observer response

func (*GeneratorNode) AddTx

func (fr *GeneratorNode) AddTx(tx *types.Transaction, sig common.Signature) error

AddTx adds tx to txpool

func (*GeneratorNode) Close

func (fr *GeneratorNode) Close()

Close terminates the generator

func (*GeneratorNode) GetTxFromTXPool

func (fr *GeneratorNode) GetTxFromTXPool(TxHash hash.Hash256) *txpool.PoolItem

GetTxFromTXPool returned tx from txpool

func (*GeneratorNode) Init

func (fr *GeneratorNode) Init() error

Init initializes generator

func (*GeneratorNode) OnConnected

func (fr *GeneratorNode) OnConnected(p peer.Peer)

OnConnected is called after a new peer is connected

func (*GeneratorNode) OnDisconnected

func (fr *GeneratorNode) OnDisconnected(p peer.Peer)

OnDisconnected is called when the peer is disconnected

func (*GeneratorNode) OnItemExpired

func (fr *GeneratorNode) OnItemExpired(Interval time.Duration, Key string, Item interface{}, IsLast bool) queue.TxExpiredType

OnItemExpired is called when the item is expired

func (*GeneratorNode) OnObserverConnected

func (fr *GeneratorNode) OnObserverConnected(p peer.Peer)

OnObserverConnected is called after a new observer peer is connected

func (*GeneratorNode) OnObserverDisconnected

func (fr *GeneratorNode) OnObserverDisconnected(p peer.Peer)

OnObserverDisconnected is called when the observer peer is disconnected

func (*GeneratorNode) OnRecv

func (fr *GeneratorNode) OnRecv(p peer.Peer, bs []byte) error

OnRecv called when message received

func (*GeneratorNode) OnTimerExpired

func (fr *GeneratorNode) OnTimerExpired(height uint32, value string)

OnTimerExpired called when rquest expired

func (*GeneratorNode) PushTx

func (fr *GeneratorNode) PushTx(tx *types.Transaction, sig common.Signature) error

PushTx pushes transaction

func (*GeneratorNode) Run

func (fr *GeneratorNode) Run(BindAddress string)

Run runs the generator

func (*GeneratorNode) TxPoolList

func (fr *GeneratorNode) TxPoolList() []*txpool.PoolItem

TxPoolList returned tx list from txpool

type GeneratorNodeMesh

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

func NewGeneratorNodeMesh

func NewGeneratorNodeMesh(key key.Key, NetAddressMap map[common.PublicKey]string, fr *GeneratorNode) *GeneratorNodeMesh

func (*GeneratorNodeMesh) BroadcastPacket

func (ms *GeneratorNodeMesh) BroadcastPacket(bs []byte)

BroadcastPacket sends a packet to all peers

func (*GeneratorNodeMesh) Peers

func (ms *GeneratorNodeMesh) Peers() []peer.Peer

Peers returns peers of the generator mesh

func (*GeneratorNodeMesh) RemovePeer

func (ms *GeneratorNodeMesh) RemovePeer(ID string)

RemovePeer removes peers from the mesh

func (*GeneratorNodeMesh) Run

func (ms *GeneratorNodeMesh) Run()

Run starts the generator mesh

func (*GeneratorNodeMesh) SendTo

func (ms *GeneratorNodeMesh) SendTo(ID string, m p2p.Serializable) error

SendTo sends a message to the observer

type GeneratorService

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

GeneratorService provides connectivity with generators

func NewGeneratorService

func NewGeneratorService(ob *ObserverNode) *GeneratorService

NewGeneratorService returns a GeneratorService

func (*GeneratorService) ActiveGenerators

func (ms *GeneratorService) ActiveGenerators() []common.Address

Generators returns a Generator list slice

func (*GeneratorService) GeneratorMap

func (ms *GeneratorService) GeneratorMap() map[common.Address]bool

GeneratorMap returns a Generator list as a map

func (*GeneratorService) Peer

func (ms *GeneratorService) Peer(ID string) (peer.Peer, bool)

Peer returns the peer

func (*GeneratorService) PeerCount

func (ms *GeneratorService) PeerCount() int

PeerCount returns a number of the peer

func (*GeneratorService) Peers

func (ms *GeneratorService) Peers() []peer.Peer

Peers returns peer list

func (*GeneratorService) RemovePeer

func (ms *GeneratorService) RemovePeer(ID string)

RemovePeer removes peers from the mesh

func (*GeneratorService) Run

func (ms *GeneratorService) Run(BindAddress string)

Run provides a server

func (*GeneratorService) SendTo

func (ms *GeneratorService) SendTo(addr common.Address, bs []byte) error

SendTo sends a message to the generator

type ObserverNode

type ObserverNode struct {
	sync.Mutex
	ChainID *big.Int
	// contains filtered or unexported fields
}

ObserverNode observes a block by the consensus

func NewObserverNode

func NewObserverNode(ChainID *big.Int, key key.Key, NetAddressMap map[common.PublicKey]string, cn *chain.Chain, obID string) *ObserverNode

NewObserverNode returns a ObserverNode

func (*ObserverNode) Close

func (ob *ObserverNode) Close()

Close terminates the observer

func (*ObserverNode) Init

func (ob *ObserverNode) Init() error

Init initializes observer

func (*ObserverNode) OnGeneratorConnected

func (ob *ObserverNode) OnGeneratorConnected(p peer.Peer)

OnGeneratorConnected is called after a new generator peer is connected

func (*ObserverNode) OnGeneratorDisconnected

func (ob *ObserverNode) OnGeneratorDisconnected(p peer.Peer)

OnGeneratorDisconnected is called when the generator peer is disconnected

func (*ObserverNode) OnTimerExpired

func (ob *ObserverNode) OnTimerExpired(height uint32, value string)

OnTimerExpired called when rquest expired

func (*ObserverNode) ResetRound

func (ob *ObserverNode) ResetRound()

func (*ObserverNode) Run

func (ob *ObserverNode) Run(BindObserver string, BindGenerator string)

Run starts the pof consensus on the observer

type ObserverNodeMesh

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

func NewObserverNodeMesh

func NewObserverNodeMesh(key key.Key, NetAddressMap map[common.PublicKey]string, ob *ObserverNode) *ObserverNodeMesh

func (*ObserverNodeMesh) BroadcastPacket

func (ms *ObserverNodeMesh) BroadcastPacket(bs []byte)

BroadcastPacket sends a packet to all peers

func (*ObserverNodeMesh) Peers

func (ms *ObserverNodeMesh) Peers() []peer.Peer

Peers returns peers of the observer mesh

func (*ObserverNodeMesh) RemovePeer

func (ms *ObserverNodeMesh) RemovePeer(ID string)

RemovePeer removes peers from the mesh

func (*ObserverNodeMesh) Run

func (ms *ObserverNodeMesh) Run(BindAddress string)

Run starts the observer mesh

func (*ObserverNodeMesh) SendAnyone

func (ms *ObserverNodeMesh) SendAnyone(bs []byte) error

SendAnyone sends a message to the one of observers

func (*ObserverNodeMesh) SendTo

func (ms *ObserverNodeMesh) SendTo(PubKey common.PublicKey, bs []byte) error

SendTo sends a message to the observer

type RoundState

type RoundState int
const (
	EmptyState RoundState = iota
	RoundVoteState
	RoundVoteAckState
	BlockWaitState
	BlockVoteState
)

consts

func (RoundState) String

func (r RoundState) String() string

type RoundVoteAckMessage

type RoundVoteAckMessage struct {
	ChainID      *big.Int
	LastHash     hash.Hash256
	TargetHeight uint32
	TimeoutCount uint32
	Generator    common.Address
	PublicKey    common.PublicKey
	Timestamp    uint64
	IsReply      bool
}

RoundVoteAckMessage is a message for a round vote ack

func (*RoundVoteAckMessage) ReadFrom

func (s *RoundVoteAckMessage) ReadFrom(r io.Reader) (int64, error)

func (*RoundVoteAckMessage) TypeID

func (s *RoundVoteAckMessage) TypeID() uint32

func (*RoundVoteAckMessage) WriteTo

func (s *RoundVoteAckMessage) WriteTo(w io.Writer) (int64, error)

type RoundVoteMessage

type RoundVoteMessage struct {
	ChainID      *big.Int
	LastHash     hash.Hash256
	TargetHeight uint32
	TimeoutCount uint32
	Generator    common.Address
	PublicKey    common.PublicKey
	Timestamp    uint64
	IsReply      bool
}

RoundVoteMessage is a message for a round vote

func (*RoundVoteMessage) ReadFrom

func (s *RoundVoteMessage) ReadFrom(r io.Reader) (int64, error)

func (*RoundVoteMessage) TypeID

func (s *RoundVoteMessage) TypeID() uint32

func (*RoundVoteMessage) WriteTo

func (s *RoundVoteMessage) WriteTo(w io.Writer) (int64, error)

type VoteRound

type VoteRound struct {
	RoundState                 RoundState
	TargetHeight               uint32
	RoundVoteMessageMap        map[common.PublicKey]*RoundVoteMessage
	RoundVoteAckMessageMap     map[common.PublicKey]*RoundVoteAckMessage
	MinRoundVoteAck            *RoundVoteAckMessage
	BlockRoundMap              map[uint32]*BlockRound
	RoundVoteWaitMap           map[common.PublicKey]*RoundVoteMessage
	RoundVoteAckMessageWaitMap map[common.PublicKey]*RoundVoteAckMessage
	VoteFailCount              int
}

VoteRound is data for the voting round

func NewVoteRound

func NewVoteRound(TargetHeight uint32, MaxBlocksPerGenerator uint32) *VoteRound

NewVoteRound returns a VoteRound

Jump to

Keyboard shortcuts

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