summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
d5fbf5b)
* dispatch signature when proccess block
* remove unuse code
* remove unuse code
* add event dispatcher to bbft
12 files changed:
store := database.NewStore(testDB)
txPool := protocol.NewTxPool(store, dispatcher)
store := database.NewStore(testDB)
txPool := protocol.NewTxPool(store, dispatcher)
- chain, err := protocol.NewChain(store, txPool)
+ chain, err := protocol.NewChain(store, txPool, dispatcher)
if err != nil {
t.Fatal(err)
}
if err != nil {
t.Fatal(err)
}
+// SaveChainNodeStatus update the best node and irreversible node
+func (s *Store) SaveChainNodeStatus(bestNode, irreversibleNode *state.BlockNode) error {
+ bytes, err := json.Marshal(protocol.BlockStoreState{
+ Height: bestNode.Height,
+ Hash: &bestNode.Hash,
+ IrreversibleHeight: irreversibleNode.Height,
+ IrreversibleHash: &irreversibleNode.Hash,
+ })
+ if err != nil {
+ return err
+ }
+
+ s.db.Set(blockStoreKey, bytes)
+ return nil
+}
+
// saveVoteResult update the voting results generated by each irreversible block
func saveVoteResult(batch dbm.Batch, voteMap map[uint64]*state.VoteResult) error {
for _, vote := range voteMap {
// saveVoteResult update the voting results generated by each irreversible block
func saveVoteResult(batch dbm.Batch, voteMap map[uint64]*state.VoteResult) error {
for _, vote := range voteMap {
log "github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
+ "github.com/vapor/protocol/bc"
"github.com/vapor/protocol/bc/types"
)
"github.com/vapor/protocol/bc/types"
)
type NewMinedBlockEvent struct{ Block types.Block }
type NewMinedBlockEvent struct{ Block types.Block }
+type BlockSignatureEvent struct {
+ BlockHash bc.Hash
+ Signature []byte
+}
+
// TypeMuxEvent is a time-tagged notification pushed to subscribers.
type TypeMuxEvent struct {
Time time.Time
// TypeMuxEvent is a time-tagged notification pushed to subscribers.
type TypeMuxEvent struct {
Time time.Time
dispatcher := event.NewDispatcher()
txPool := protocol.NewTxPool(store, dispatcher)
dispatcher := event.NewDispatcher()
txPool := protocol.NewTxPool(store, dispatcher)
- chain, err := protocol.NewChain(store, txPool)
+ chain, err := protocol.NewChain(store, txPool, dispatcher)
if err != nil {
cmn.Exit(cmn.Fmt("Failed to create chain structure: %v", err))
}
if err != nil {
cmn.Exit(cmn.Fmt("Failed to create chain structure: %v", err))
}
"github.com/vapor/crypto/ed25519"
"github.com/vapor/crypto/ed25519/chainkd"
"github.com/vapor/errors"
"github.com/vapor/crypto/ed25519"
"github.com/vapor/crypto/ed25519/chainkd"
"github.com/vapor/errors"
+ "github.com/vapor/event"
"github.com/vapor/math/checked"
"github.com/vapor/protocol/bc"
"github.com/vapor/protocol/bc/types"
"github.com/vapor/math/checked"
"github.com/vapor/protocol/bc"
"github.com/vapor/protocol/bc/types"
consensusNodeManager *consensusNodeManager
orphanManage *OrphanManage
signatureCache *lru.Cache
consensusNodeManager *consensusNodeManager
orphanManage *OrphanManage
signatureCache *lru.Cache
+ eventDispatcher *event.Dispatcher
-func newBbft(store Store, blockIndex *state.BlockIndex, orphanManage *OrphanManage) *bbft {
+func newBbft(store Store, blockIndex *state.BlockIndex, orphanManage *OrphanManage, eventDispatcher *event.Dispatcher) *bbft {
return &bbft{
orphanManage: orphanManage,
consensusNodeManager: newConsensusNodeManager(store, blockIndex),
signatureCache: lru.New(maxSignatureCacheSize),
return &bbft{
orphanManage: orphanManage,
consensusNodeManager: newConsensusNodeManager(store, blockIndex),
signatureCache: lru.New(maxSignatureCacheSize),
+ eventDispatcher: eventDispatcher,
}
// SignBlock signing the block if current node is consensus node
}
// SignBlock signing the block if current node is consensus node
-func (b *bbft) SignBlock(block *types.Block) error {
+func (b *bbft) SignBlock(block *types.Block) ([]byte, error) {
var xprv chainkd.XPrv
xpub := [64]byte(xprv.XPub())
node, err := b.consensusNodeManager.getConsensusNode(block.Height, hex.EncodeToString(xpub[:]))
if err != nil && err != errNotFoundConsensusNode {
var xprv chainkd.XPrv
xpub := [64]byte(xprv.XPub())
node, err := b.consensusNodeManager.getConsensusNode(block.Height, hex.EncodeToString(xpub[:]))
if err != nil && err != errNotFoundConsensusNode {
- block.Witness[node.order] = xprv.Sign(block.Hash().Bytes())
- return nil
+ signature := xprv.Sign(block.Hash().Bytes())
+ block.Witness[node.order] = signature
+ return signature, nil
}
// UpdateConsensusNodes used to update consensus node after each round of voting
}
// UpdateConsensusNodes used to update consensus node after each round of voting
log "github.com/sirupsen/logrus"
"github.com/vapor/errors"
log "github.com/sirupsen/logrus"
"github.com/vapor/errors"
+ "github.com/vapor/event"
"github.com/vapor/protocol/bc"
"github.com/vapor/protocol/bc/types"
"github.com/vapor/protocol/state"
"github.com/vapor/protocol/bc"
"github.com/vapor/protocol/bc/types"
"github.com/vapor/protocol/state"
return errors.Sub(ErrBadBlock, err)
}
return errors.Sub(ErrBadBlock, err)
}
- if err := c.bbft.SignBlock(block); err != nil {
+ signature, err := c.bbft.SignBlock(block)
+ if err != nil {
return errors.Sub(ErrBadBlock, err)
}
return errors.Sub(ErrBadBlock, err)
}
+ if len(signature) != 0 {
+ if err := c.txPool.eventDispatcher.Post(event.BlockSignatureEvent{BlockHash: block.Hash(), Signature: signature}); err != nil {
+ return err
+ }
+ }
+
bcBlock := types.MapBlock(block)
if err := c.store.SaveBlock(block, bcBlock.TransactionStatus); err != nil {
return err
bcBlock := types.MapBlock(block)
if err := c.store.SaveBlock(block, bcBlock.TransactionStatus); err != nil {
return err
+
+func (c *Chain) processBlockSignature(signature, pubkey []byte, blockHeight uint64, blockHash *bc.Hash) error {
+ isBestIrreversible, err := c.bbft.ProcessBlockSignature(signature, pubkey, blockHeight, blockHash)
+ if err != nil {
+ return err
+ }
+
+ if isBestIrreversible {
+ bestIrreversibleNode := c.index.GetNode(blockHash)
+ if err := c.store.SaveChainNodeStatus(c.bestNode, bestIrreversibleNode); err != nil {
+ return err
+ }
+
+ c.bestIrreversibleNode = bestIrreversibleNode
+ }
+ return nil
+}
log "github.com/sirupsen/logrus"
"github.com/vapor/config"
log "github.com/sirupsen/logrus"
"github.com/vapor/config"
+ "github.com/vapor/event"
"github.com/vapor/protocol/bc"
"github.com/vapor/protocol/bc/types"
"github.com/vapor/protocol/state"
"github.com/vapor/protocol/bc"
"github.com/vapor/protocol/bc/types"
"github.com/vapor/protocol/state"
}
// NewChain returns a new Chain using store as the underlying storage.
}
// NewChain returns a new Chain using store as the underlying storage.
-func NewChain(store Store, txPool *TxPool) (*Chain, error) {
+func NewChain(store Store, txPool *TxPool, eventDispatcher *event.Dispatcher) (*Chain, error) {
c := &Chain{
orphanManage: NewOrphanManage(),
txPool: txPool,
c := &Chain{
orphanManage: NewOrphanManage(),
txPool: txPool,
c.bestNode = c.index.GetNode(storeStatus.Hash)
c.index.SetMainChain(c.bestNode)
c.bestNode = c.index.GetNode(storeStatus.Hash)
c.index.SetMainChain(c.bestNode)
- c.bbft = newBbft(store, c.index, c.orphanManage)
+ c.bbft = newBbft(store, c.index, c.orphanManage, eventDispatcher)
go c.blockProcesser()
return c, nil
}
go c.blockProcesser()
return c, nil
}
LoadBlockIndex(uint64) (*state.BlockIndex, error)
SaveBlock(*types.Block, *bc.TransactionStatus) error
SaveChainStatus(*state.BlockNode, *state.BlockNode, *state.UtxoViewpoint, map[uint64]*state.VoteResult) error
LoadBlockIndex(uint64) (*state.BlockIndex, error)
SaveBlock(*types.Block, *bc.TransactionStatus) error
SaveChainStatus(*state.BlockNode, *state.BlockNode, *state.UtxoViewpoint, map[uint64]*state.VoteResult) error
+ SaveChainNodeStatus(*state.BlockNode, *state.BlockNode) error
}
// BlockStoreState represents the core's db status
}
// BlockStoreState represents the core's db status
func (s *mockStore) LoadBlockIndex(uint64) (*state.BlockIndex, error) { return nil, nil }
func (s *mockStore) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
func (s *mockStore) SaveChainStatus(*state.BlockNode, *state.BlockNode, *state.UtxoViewpoint, map[uint64]*state.VoteResult) error { return nil }
func (s *mockStore) LoadBlockIndex(uint64) (*state.BlockIndex, error) { return nil, nil }
func (s *mockStore) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
func (s *mockStore) SaveChainStatus(*state.BlockNode, *state.BlockNode, *state.UtxoViewpoint, map[uint64]*state.VoteResult) error { return nil }
+func (s *mockStore) SaveChainNodeStatus(*state.BlockNode, *state.BlockNode) error { return nil }
func TestAddOrphan(t *testing.T) {
cases := []struct {
func TestAddOrphan(t *testing.T) {
cases := []struct {
func (s *mockStore1) LoadBlockIndex(uint64) (*state.BlockIndex, error) { return nil, nil }
func (s *mockStore1) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
func (s *mockStore1) SaveChainStatus(*state.BlockNode, *state.BlockNode, *state.UtxoViewpoint, map[uint64]*state.VoteResult) error { return nil }
func (s *mockStore1) LoadBlockIndex(uint64) (*state.BlockIndex, error) { return nil, nil }
func (s *mockStore1) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
func (s *mockStore1) SaveChainStatus(*state.BlockNode, *state.BlockNode, *state.UtxoViewpoint, map[uint64]*state.VoteResult) error { return nil }
+func (s *mockStore1) SaveChainNodeStatus(*state.BlockNode, *state.BlockNode) error { return nil }
func TestProcessTransaction(t *testing.T) {
txPool := &TxPool{
func TestProcessTransaction(t *testing.T) {
txPool := &TxPool{
store := database.NewStore(testDB)
dispatcher := event.NewDispatcher()
txPool := protocol.NewTxPool(store, dispatcher)
store := database.NewStore(testDB)
dispatcher := event.NewDispatcher()
txPool := protocol.NewTxPool(store, dispatcher)
- chain, err := protocol.NewChain(store, txPool)
+ chain, err := protocol.NewChain(store, txPool, dispatcher)
if err != nil {
return nil, nil, nil, err
}
if err != nil {
return nil, nil, nil, err
}
store := database.NewStore(testDB)
dispatcher := event.NewDispatcher()
txPool := protocol.NewTxPool(store, dispatcher)
store := database.NewStore(testDB)
dispatcher := event.NewDispatcher()
txPool := protocol.NewTxPool(store, dispatcher)
- chain, err := protocol.NewChain(store, txPool)
+ chain, err := protocol.NewChain(store, txPool, dispatcher)
return chain, store, txPool, err
}
return chain, store, txPool, err
}
dispatcher := event.NewDispatcher()
txPool := protocol.NewTxPool(store, dispatcher)
dispatcher := event.NewDispatcher()
txPool := protocol.NewTxPool(store, dispatcher)
- chain, err := protocol.NewChain(store, txPool)
+ chain, err := protocol.NewChain(store, txPool, dispatcher)
if err != nil {
t.Fatal(err)
}
if err != nil {
t.Fatal(err)
}
store := database.NewStore(testDB)
dispatcher := event.NewDispatcher()
txPool := protocol.NewTxPool(store, dispatcher)
store := database.NewStore(testDB)
dispatcher := event.NewDispatcher()
txPool := protocol.NewTxPool(store, dispatcher)
- chain, err := protocol.NewChain(store, txPool)
+ chain, err := protocol.NewChain(store, txPool, dispatcher)
if err != nil {
t.Fatal(err)
}
if err != nil {
t.Fatal(err)
}
dispatcher := event.NewDispatcher()
txPool := protocol.NewTxPool(store, dispatcher)
dispatcher := event.NewDispatcher()
txPool := protocol.NewTxPool(store, dispatcher)
- chain, err := protocol.NewChain(store, txPool)
+ chain, err := protocol.NewChain(store, txPool, dispatcher)
if err != nil {
t.Fatal(err)
}
if err != nil {
t.Fatal(err)
}