CovenantSQL: github.com/CovenantSQL/CovenantSQL/sqlchain Index | Files | Directories

package sqlchain

import "github.com/CovenantSQL/CovenantSQL/sqlchain"

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

Package Files

ackindex.go blockindex.go chain.go config.go doc.go errors.go mux.go node.go rpc.go runtime.go storageproof.go

Variables

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")
)

func CheckValid Uses

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.

type AdviseNewBlockReq Uses

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

AdviseNewBlockReq defines a request of the AdviseNewBlock RPC method.

type AdviseNewBlockResp Uses

type AdviseNewBlockResp struct {
}

AdviseNewBlockResp defines a response of the AdviseNewBlock RPC method.

type Answer Uses

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 Uses

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 Uses

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

NewAnswer generates an answer for storage proof.

type BlockID Uses

type BlockID string

BlockID is the hash of block content.

type Chain Uses

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

Chain represents a sql-chain.

func NewChain Uses

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

NewChain creates a new sql-chain struct.

func NewChainWithContext Uses

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

NewChainWithContext creates a new sql-chain struct with context.

func (*Chain) AddResponse Uses

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

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

func (*Chain) CheckAndPushNewBlock Uses

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

CheckAndPushNewBlock implements ChainRPCServer.CheckAndPushNewBlock.

func (*Chain) FetchBlock Uses

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

FetchBlock fetches the block at specified height from local cache.

func (*Chain) FetchBlockByCount Uses

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 Uses

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 Uses

func (c *Chain) SetLastBillingHeight(h int32)

SetLastBillingHeight sets the last billing height of this chain instance.

func (*Chain) Start Uses

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

Start starts the main process of the sql-chain.

func (*Chain) Stop Uses

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

Stop stops the main process of the sql-chain.

func (*Chain) UpdatePeers Uses

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

UpdatePeers updates peer list of the sql-chain.

func (*Chain) VerifyAndPushAckedQuery Uses

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

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

type ChainRPCService Uses

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

ChainRPCService defines a sql-chain RPC server.

func (*ChainRPCService) AdviseNewBlock Uses

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 Uses

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 Uses

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 Uses

type FetchBlockReq struct {
    Height int32
}

FetchBlockReq defines a request of the FetchBlock RPC method.

type FetchBlockResp Uses

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

FetchBlockResp defines a response of the FetchBlock RPC method.

type MuxAdviseNewBlockReq Uses

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

MuxAdviseNewBlockReq defines a request of the AdviseNewBlock RPC method.

type MuxAdviseNewBlockResp Uses

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

MuxAdviseNewBlockResp defines a response of the AdviseNewBlock RPC method.

type MuxFetchBlockReq Uses

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

MuxFetchBlockReq defines a request of the FetchBlock RPC method.

type MuxFetchBlockResp Uses

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

MuxFetchBlockResp defines a response of the FetchBlock RPC method.

type MuxService Uses

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

MuxService defines multiplexing service of sql-chain.

func NewMuxService Uses

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 Uses

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 Uses

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 Uses

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

StorageProofBlock records block's status.

Directories

PathSynopsis
adapter
adapter/apiPackage api defines the adapter api interface.
adapter/configPackage config defines the configuration objects for adapter.
adapter/storagePackage storage defines the abstraction layer.
mirror
observer
observer/statik

Package sqlchain imports 29 packages (graph) and is imported by 2 packages. Updated 2019-06-12. Refresh now. Tools for package owners.