OSDN Git Service

merge bvm branch with master
[bytom/bytom-spv.git] / protocol / protocol.go
index 574ec0d..0973476 100644 (file)
@@ -1,43 +1,3 @@
-/*
-Package protocol provides the logic to tie together
-storage and validation for a Chain Protocol blockchain.
-
-This comprises all behavior that's common to every full
-node, as well as other functions that need to operate on the
-blockchain state.
-
-Here are a few examples of typical full node types.
-
-Generator
-
-A generator has two basic jobs: collecting transactions from
-other nodes and putting them into blocks.
-
-To add a new block to the blockchain, call GenerateBlock,
-sign the block (possibly collecting signatures from other
-parties), and call CommitBlock.
-
-Signer
-
-A signer validates blocks generated by the Generator and signs
-at most one block at each height.
-
-Participant
-
-A participant node in a network may select outputs for spending
-and compose transactions.
-
-To publish a new transaction, prepare your transaction
-(select outputs, and compose and sign the tx) and send the
-transaction to the network's generator. To wait for
-confirmation, call BlockWaiter on successive block heights
-and inspect the blockchain state until you find that the
-transaction has been either confirmed or rejected. Note
-that transactions may be malleable if there's no commitment
-to TXSIGHASH.
-
-To ingest a block, call ValidateBlock and CommitBlock.
-*/
 package protocol
 
 import (
@@ -48,7 +8,7 @@ import (
        "github.com/golang/groupcache/lru"
 
        "github.com/bytom/errors"
-       //"github.com/blockchain/log"
+       "github.com/bytom/log"
        "github.com/bytom/protocol/bc"
        "github.com/bytom/protocol/bc/legacy"
        "github.com/bytom/protocol/state"
@@ -73,11 +33,11 @@ var (
 type Store interface {
        Height(context.Context) (uint64, error)
        GetBlock(context.Context, uint64) (*legacy.Block, error)
-//     LatestSnapshot(context.Context) (*state.Snapshot, uint64, error)
+       LatestSnapshot(context.Context) (*state.Snapshot, uint64, error)
 
-//     SaveBlock(context.Context, *legacy.Block) error
+       SaveBlock(context.Context, *legacy.Block) error
        FinalizeBlock(context.Context, uint64) error
-//     SaveSnapshot(context.Context, uint64, *state.Snapshot) error
+       SaveSnapshot(context.Context, uint64, *state.Snapshot) error
 }
 
 // Chain provides a complete, minimal blockchain database. It
@@ -119,11 +79,7 @@ func NewChain(ctx context.Context, initialBlockHash bc.Hash, store Store, height
        }
        c.state.cond.L = new(sync.Mutex)
 
-       var err error
-       c.state.height, err = store.Height(ctx)
-       if err != nil {
-               return nil, errors.Wrap(err, "looking up blockchain height")
-       }
+       c.state.height, _ = store.Height(ctx)
 
        // Note that c.height.n may still be zero here.
        if heights != nil {
@@ -139,12 +95,11 @@ func NewChain(ctx context.Context, initialBlockHash bc.Hash, store Store, height
                        select {
                        case <-ctx.Done():
                                return
-                       //case ps := <-c.pendingSnapshots:
-                               /*err = store.SaveSnapshot(ctx, ps.height, ps.snapshot)
+                       case ps := <-c.pendingSnapshots:
+                               err := store.SaveSnapshot(ctx, ps.height, ps.snapshot)
                                if err != nil {
                                        log.Error(ctx, err, "at", "saving snapshot")
                                }
-                */
                        }
                }
        }()
@@ -152,6 +107,10 @@ func NewChain(ctx context.Context, initialBlockHash bc.Hash, store Store, height
        return c, nil
 }
 
+func (c *Chain) GetStore() *Store {
+       return &(c.store)
+}
+
 // Height returns the current height of the blockchain.
 func (c *Chain) Height() uint64 {
        c.state.cond.L.Lock()