OSDN Git Service

Merge branch 'mov' into fix_mov_store
[bytom/vapor.git] / protocol / mov.go
index 331a36a..af09b61 100644 (file)
@@ -1,6 +1,11 @@
 package protocol
 
 import (
+       "github.com/vapor/application/mov"
+       "github.com/vapor/config"
+       "github.com/vapor/consensus"
+       dbm "github.com/vapor/database/leveldb"
+       "github.com/vapor/errors"
        "github.com/vapor/protocol/bc"
        "github.com/vapor/protocol/bc/types"
 )
@@ -9,56 +14,63 @@ const (
        protocolName = "MOV"
 )
 
-type matchEnginer interface {
+type movCore interface {
        ApplyBlock(block *types.Block) error
-       BeforeProposalBlock(txs []*types.Tx) ([]*types.Tx, error)
-       ChainStatus() (uint64, *bc.Hash)
+       BeforeProposalBlock(capacity int) ([]*types.Tx, error)
+       ChainStatus() (uint64, *bc.Hash, error)
        DetachBlock(block *types.Block) error
        IsDust(tx *types.Tx) bool
-       ValidateBlock(block *bc.Block) error
-       ValidateTxs(txs []*bc.Tx) error
+       ValidateBlock(block *types.Block) error
+       ValidateTxs(txs []*types.Tx) error
 }
 
 type MOV struct {
-       engine matchEnginer
+       core movCore
 }
 
-func NewMOV() *MOV {
-       return &MOV{}
+func NewMOV(db dbm.DB, startPoint consensus.Checkpoint) (*MOV, error) {
+       if startPoint.Height == 0 {
+               startPoint.Hash = config.GenesisBlock().Hash()
+       }
+
+       movCore, err := mov.NewMovCore(db, startPoint.Height, &startPoint.Hash)
+       if err != nil {
+               return nil, errors.Wrap(err, "failed on create mov core")
+       }
+
+       return &MOV{
+               core: movCore,
+       }, nil
 }
 
 func (m MOV) ApplyBlock(block *types.Block) error {
-       return m.engine.ApplyBlock(block)
+       return m.core.ApplyBlock(block)
 }
 
-func (m MOV) BeforeProposalBlock(txs []*types.Tx) ([]*types.Tx, error) {
-       return m.engine.BeforeProposalBlock(txs)
+func (m MOV) BeforeProposalBlock(capacity int) ([]*types.Tx, error) {
+       return m.core.BeforeProposalBlock(capacity)
 }
 
-func (m MOV) ChainStatus() (uint64, *bc.Hash) {
-       return m.engine.ChainStatus()
+func (m MOV) ChainStatus() (uint64, *bc.Hash, error) {
+       return m.core.ChainStatus()
 }
 
 func (m MOV) DetachBlock(block *types.Block) error {
-       return m.engine.DetachBlock(block)
+       return m.core.DetachBlock(block)
 }
 
 func (m MOV) IsDust(tx *types.Tx) bool {
-       return m.engine.IsDust(tx)
+       return m.core.IsDust(tx)
 }
 
 func (m MOV) Name() string {
        return protocolName
 }
 
-func (m MOV) ValidateBlock(block *bc.Block) error {
-       return m.engine.ValidateBlock(block)
-}
-
-func (m MOV) ValidateTxs(txs []*bc.Tx) error {
-       return m.engine.ValidateTxs(txs)
+func (m MOV) ValidateBlock(block *types.Block) error {
+       return m.core.ValidateBlock(block)
 }
 
-func (m MOV) Status() (uint64, *bc.Hash) {
-       return m.engine.ChainStatus()
+func (m MOV) ValidateTxs(txs []*types.Tx) error {
+       return m.core.ValidateTxs(txs)
 }