sqlchain

package
v0.0.0-...-020e20f Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2022 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Overview

Package sqlchain provides a blockchain implementation for database state tracking.

Bucket hierarchical structure is as following:

[ root ] | +-- [ sql-chain meta ] | | | +-- [ state ]: State | | | +-- [ block ] | | | | | +--- [ height1+hash1 ]: Block1 | | +--- [ height2+hash2 ]: Block2 | | +--- [ height3+hash3 ]: Block3 | | | | | +--- ... | | | +-- [ query ] | | | +--- ... | | | +--- [ height 7 ] | | | | | +--- [ request ] | | | | | | | +--- [ header hash1 ]: Request1 | | | +--- [ header hash2 ]: Request2 | | | +--- [ header hash3 ]: Request3 | | | | | | | +--- ... | | | | | +--- [ response ] | | | | | | | +--- [ header hash1 ]: Response1 | | | +--- [ header hash2 ]: Response2 | | | +--- [ header hash3 ]: Response3 | | | | | | | +--- ... | | | | | +--- [ ack ] | | | | | +--- [ header hash1 ]: Ack1 | | +--- [ header hash2 ]: Ack2 | | +--- [ header hash3 ]: Ack3 | | | | | +--- ... | | | | | +--- [ height 8 ] | | | | | +--- [ request ] | | | | | | | +--- ... | | | | | +--- [ response ] | | | | | | | +--- ... | | | | | +--- [ ack ] | | | | | +--- ... | | | +--- [ height 9 ] | | | | | +--- [ request ] | | | | | | | +--- ... | | | | | +--- [ response ] | | | | | | | +--- ... | | | | | +--- [ ack ] | | | | | +--- ... | | | +--- ... | |-- [ other bucket ] |-- [ other bucket ] |-- [ other bucket ] | +-- ...

Block producing:

    blocks: Block-0               Block-1               Block-2               ...
           (Genesis)              |                     |
            |                     +-{q1, q2}            +-{q3, q5}            ...
            |                     |                     |
            +---------------------+---------------------+-------------------
            |                     |                     |
   queries: | q1       q2    q3   |        q4 q5    q6  |     q7              ...
   \        | |\       |\    |\   |        |  |     |   |     |
    \       | | \      | |   | +--|--+     |  +     +   +     +
(responses) | |  \     | |   |    |   \    |  |\    |\   \    |\
      \     | |   |    | |   |    +    \   |  | +   | \   \   | \
       \    | |   |    | |   |     \    \  |  | |   |  \   \  |  \
      acks: | |   a1   | a2  |      +    a3|  | a5  |   a6  + |   a7          ...
            | |   |    | |   |      |    | |  | |   |    \  | |    \
            | |   |    | |   |      |    | |  | |   |     + | |     +
            | |   |    | |   |      |    | |  | |   |     | | |     |
    height: 0 +---+----+-+---+--- 1 +----+-+--+-+---+-- 2 +-+-+-----+-------- ...
            |                     |                     |
            [ ---- period-0 ---- )[ ---- period-1 ---- )[ ---- period-2 ---- )...

Note: q4 will expire after `config.QueryTTL` blocks; q5 and q6, which are acknowledged in period-2, will be included in the next block.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrParentNotFound indicates an error failing to find parent node during a chain reloading.
	ErrParentNotFound = errors.New("could not find parent node")
	// ErrInvalidBlock indicates an invalid block which does not extend the best chain while
	// pushing new blocks.
	ErrInvalidBlock = errors.New("invalid block")
	// ErrMultipleAckOfSeqNo indicates that multiple acknowledgements for a same sequence number is
	// detected.
	ErrMultipleAckOfSeqNo = errors.New("multiple acknowledgements of same sequence number")
	// ErrUnknownMuxRequest indicates that the multiplexing request endpoint is not found.
	ErrUnknownMuxRequest = errors.New("unknown multiplexing request")
	// ErrQueryExpired indicates that a received query Response/Ack has expired.
	ErrQueryExpired = errors.New("query has expired")
	// ErrUnknownProducer indicates that the block has an unknown producer.
	ErrUnknownProducer = errors.New("unknown block producer")
	// ErrInvalidProducer indicates that the block has an invalid producer.
	ErrInvalidProducer = errors.New("invalid block producer")
	// ErrQueryNotFound indicates that a query is not found in the index.
	ErrQueryNotFound = errors.New("query not found")
	// ErrResponseSeqNotMatch indicates that a response sequence id doesn't match the original one
	// in the index.
	ErrResponseSeqNotMatch = errors.New("response sequence id doesn't match")
	// ErrInitiating indicates that a sqlchain is in initiate state and is not available for sync
	// requests.
	ErrInitiating = errors.New("sqlchain is in initiate")
)

Functions

func CheckValid

func CheckValid(answers []Answer) bool

CheckValid returns whether answers is valid Checkvalid checks answers as follows: 1. len(answers) == len(nodes) - 1 2. answers[i].nodeID's answer is the same as the hash of verifier.

Types

type AdviseNewBlockReq

type AdviseNewBlockReq struct {
	Block *types.Block
	Count int32
}

AdviseNewBlockReq defines a request of the AdviseNewBlock RPC method.

type AdviseNewBlockResp

type AdviseNewBlockResp struct {
}

AdviseNewBlockResp defines a response of the AdviseNewBlock RPC method.

type Answer

type Answer struct {
	// The block id that the question belongs to
	PreviousBlockID BlockID
	// The node id that provides this answer
	NodeID proto.NodeID
	// The answer for the question
	Answer hash.Hash
}

Answer is responded by node to confirm other nodes that the node stores data correctly.

func GenerateAnswer

func GenerateAnswer(answers []Answer, previousBlock StorageProofBlock, node proto.Node) (*Answer, error)

GenerateAnswer will select specified record for proving. In order to generate a unique answer which is different with other nodes' answer, we hash(record + nodeID) as the answer.

func NewAnswer

func NewAnswer(previousBlockID BlockID, nodeID proto.NodeID, answer hash.Hash) *Answer

NewAnswer generates an answer for storage proof.

type BlockID

type BlockID string

BlockID is the hash of block content.

type Chain

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

Chain represents a sql-chain.

func NewChain

func NewChain(c *Config) (chain *Chain, err error)

NewChain creates a new sql-chain struct.

func NewChainWithContext

func NewChainWithContext(ctx context.Context, c *Config) (chain *Chain, err error)

NewChainWithContext creates a new sql-chain struct with context.

func (*Chain) AddResponse

func (c *Chain) AddResponse(resp *types.SignedResponseHeader) (err error)

AddResponse addes a response to the ackIndex, awaiting for acknowledgement.

func (*Chain) CheckAndPushNewBlock

func (c *Chain) CheckAndPushNewBlock(block *types.Block) (err error)

CheckAndPushNewBlock implements ChainRPCServer.CheckAndPushNewBlock.

func (*Chain) FetchBlock

func (c *Chain) FetchBlock(height int32) (b *types.Block, err error)

FetchBlock fetches the block at specified height from local cache.

func (*Chain) FetchBlockByCount

func (c *Chain) FetchBlockByCount(count int32) (b *types.Block, realCount int32, height int32, err error)

FetchBlockByCount fetches the block at specified count from local cache.

func (*Chain) Query

func (c *Chain) Query(
	req *types.Request, isLeader bool) (tracker *x.QueryTracker, resp *types.Response, err error,
)

Query queries req from local chain state and returns the query results in resp.

func (*Chain) SetLastBillingHeight

func (c *Chain) SetLastBillingHeight(h int32)

SetLastBillingHeight sets the last billing height of this chain instance.

func (*Chain) Start

func (c *Chain) Start() (err error)

Start starts the main process of the sql-chain.

func (*Chain) Stop

func (c *Chain) Stop() (err error)

Stop stops the main process of the sql-chain.

func (*Chain) UpdatePeers

func (c *Chain) UpdatePeers(peers *proto.Peers) error

UpdatePeers updates peer list of the sql-chain.

func (*Chain) VerifyAndPushAckedQuery

func (c *Chain) VerifyAndPushAckedQuery(ack *types.SignedAckHeader) (err error)

VerifyAndPushAckedQuery verifies a acknowledged and signed query, and pushed it if valid.

type ChainRPCService

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

ChainRPCService defines a sql-chain RPC server.

func (*ChainRPCService) AdviseNewBlock

func (s *ChainRPCService) AdviseNewBlock(req *AdviseNewBlockReq, resp *AdviseNewBlockResp) (
	err error)

AdviseNewBlock is the RPC method to advise a new produced block to the target server.

func (*ChainRPCService) FetchBlock

func (s *ChainRPCService) FetchBlock(req *FetchBlockReq, resp *FetchBlockResp) (err error)

FetchBlock is the RPC method to fetch a known block from the target server.

type Config

type Config struct {
	DatabaseID      proto.DatabaseID
	ChainFilePrefix string
	DataFile        string

	Genesis *types.Block
	Period  time.Duration
	Tick    time.Duration

	MuxService *MuxService
	Peers      *proto.Peers
	Server     proto.NodeID

	// QueryTTL sets the unacknowledged query TTL in block periods.
	QueryTTL      int32
	BlockCacheTTL int32

	// DBAccount info
	TokenType         types.TokenType
	GasPrice          uint64
	UpdatePeriod      uint64
	LastBillingHeight int32
	IsolationLevel    int
}

Config represents a sql-chain config.

type FetchBlockReq

type FetchBlockReq struct {
	Height int32
}

FetchBlockReq defines a request of the FetchBlock RPC method.

type FetchBlockResp

type FetchBlockResp struct {
	Height int32
	Block  *types.Block
}

FetchBlockResp defines a response of the FetchBlock RPC method.

type MuxAdviseNewBlockReq

type MuxAdviseNewBlockReq struct {
	proto.Envelope
	proto.DatabaseID
	AdviseNewBlockReq
}

MuxAdviseNewBlockReq defines a request of the AdviseNewBlock RPC method.

type MuxAdviseNewBlockResp

type MuxAdviseNewBlockResp struct {
	proto.Envelope
	proto.DatabaseID
	AdviseNewBlockResp
}

MuxAdviseNewBlockResp defines a response of the AdviseNewBlock RPC method.

type MuxFetchBlockReq

type MuxFetchBlockReq struct {
	proto.Envelope
	proto.DatabaseID
	FetchBlockReq
}

MuxFetchBlockReq defines a request of the FetchBlock RPC method.

type MuxFetchBlockResp

type MuxFetchBlockResp struct {
	proto.Envelope
	proto.DatabaseID
	FetchBlockResp
}

MuxFetchBlockResp defines a response of the FetchBlock RPC method.

type MuxService

type MuxService struct {
	ServiceName string
	// contains filtered or unexported fields
}

MuxService defines multiplexing service of sql-chain.

func NewMuxService

func NewMuxService(serviceName string, server *rpc.Server) (service *MuxService, err error)

NewMuxService creates a new multiplexing service and registers it to rpc server.

func (*MuxService) AdviseNewBlock

func (s *MuxService) AdviseNewBlock(req *MuxAdviseNewBlockReq, resp *MuxAdviseNewBlockResp) error

AdviseNewBlock is the RPC method to advise a new produced block to the target server.

func (*MuxService) FetchBlock

func (s *MuxService) FetchBlock(req *MuxFetchBlockReq, resp *MuxFetchBlockResp) (err error)

FetchBlock is the RPC method to fetch a known block from the target server.

type StorageProofBlock

type StorageProofBlock struct {
	// Block id
	ID BlockID
	// Nodes with index in the SQL chain.
	Nodes []proto.Node
}

StorageProofBlock records block's status.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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