From e9d3e9414fbae3f06adb7d6bf22ed3f4d899c03e Mon Sep 17 00:00:00 2001 From: paladz <453256728@qq.com> Date: Tue, 26 Sep 2017 16:46:52 +0800 Subject: [PATCH] log error when sync tx fail --- blockchain/reactor.go | 6 ++---- blockchain/txdb/store.go | 45 +++++++++++++++++++++------------------------ 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/blockchain/reactor.go b/blockchain/reactor.go index cd4fb668..fb1d08e7 100644 --- a/blockchain/reactor.go +++ b/blockchain/reactor.go @@ -338,12 +338,11 @@ func (bcR *BlockchainReactor) Receive(chID byte, src *p2p.Peer, msgBytes []byte) // queue is full, just ignore. } } else { - fmt.Println("skip sent the block response due to block is nil") + bcR.Logger.Info("skip sent the block response due to block is nil") // TODO peer is asking for things we don't have. } case *bcBlockResponseMessage: // Got a block. - //fmt.Printf("receive block %v \n", msg.Block) bcR.pool.AddBlock(src.Key, msg.GetBlock(), len(msgBytes)) case *bcStatusRequestMessage: // Send peer our state. @@ -353,13 +352,12 @@ func (bcR *BlockchainReactor) Receive(chID byte, src *p2p.Peer, msgBytes []byte) } case *bcStatusResponseMessage: // Got a peer status. Unverified. - //fmt.Printf("reveive peer high is %d \n", msg.Height) bcR.pool.SetPeerHeight(src.Key, msg.Height) case *bcTransactionMessage: tx := msg.GetTransaction() if err := bcR.chain.ValidateTx(tx); err != nil { - return + bcR.Logger.Error("fail to sync transaction to txPool", "err", err) } default: bcR.Logger.Error(cmn.Fmt("Unknown message type %v", reflect.TypeOf(msg))) diff --git a/blockchain/txdb/store.go b/blockchain/txdb/store.go index 4d95722c..0725fb64 100644 --- a/blockchain/txdb/store.go +++ b/blockchain/txdb/store.go @@ -2,14 +2,14 @@ package txdb import ( "context" - "fmt" "encoding/json" + "fmt" "github.com/bytom/errors" "github.com/bytom/protocol/bc/legacy" "github.com/bytom/protocol/state" - dbm "github.com/tendermint/tmlibs/db" . "github.com/tendermint/tmlibs/common" + dbm "github.com/tendermint/tmlibs/db" ) // A Store encapsulates storage for blockchain validation. @@ -21,7 +21,6 @@ type Store struct { cache blockCache } - //var _ protocol.Store = (*Store)(nil) func calcBlockHeadKey(height uint64) []byte { @@ -33,14 +32,13 @@ func calcBlockKey(height uint64) []byte { } func LoadBlock(db dbm.DB, height uint64) *legacy.Block { - var block *legacy.Block = &legacy.Block{} - bytez := db.Get(calcBlockKey(height)) - if bytez == nil { - return nil - } - - fmt.Printf("------LoadBlock height:%v, byte:%v", height, bytez) - block.UnmarshalText(bytez) + var block *legacy.Block = &legacy.Block{} + bytez := db.Get(calcBlockKey(height)) + if bytez == nil { + return nil + } + + block.UnmarshalText(bytez) return block } @@ -73,7 +71,6 @@ func LoadBlockStoreStateJSON(db dbm.DB) BlockStoreStateJSON { return bsj } - // NewStore creates and returns a new Store object. // // For testing purposes, it is usually much faster @@ -81,18 +78,18 @@ func LoadBlockStoreStateJSON(db dbm.DB) BlockStoreStateJSON { // instead. func NewStore(db dbm.DB) *Store { cache := newBlockCache(func(height uint64) *legacy.Block { - return LoadBlock(db, height) - }) + return LoadBlock(db, height) + }) return &Store{ - db: db, + db: db, cache: cache, - } + } } // Height returns the height of the blockchain. func (s *Store) Height() uint64 { heightJson := LoadBlockStoreStateJSON(s.db) - return heightJson.Height + return heightJson.Height } // GetBlock looks up the block with the provided block height. @@ -105,7 +102,7 @@ func (s *Store) GetBlock(height uint64) (*legacy.Block, error) { func (s *Store) GetRawBlock(height uint64) ([]byte, error) { bytez := s.db.Get(calcBlockKey(height)) if bytez == nil { - return nil , errors.New("querying blocks from the db null") + return nil, errors.New("querying blocks from the db null") } return bytez, nil } @@ -140,12 +137,12 @@ func (s *Store) SaveBlock(block *legacy.Block) error { s.cache.add(block) height := block.Height - binaryBlock, err := block.MarshalText() - if err != nil { - PanicCrisis(Fmt("Error Marshal block meta: %v", err)) - } + binaryBlock, err := block.MarshalText() + if err != nil { + PanicCrisis(Fmt("Error Marshal block meta: %v", err)) + } fmt.Printf("------SaveBlock height:%v, byte:%v", height, binaryBlock) - s.db.Set(calcBlockKey(height), binaryBlock) + s.db.Set(calcBlockKey(height), binaryBlock) // Save new BlockStoreStateJSON descriptor BlockStoreStateJSON{Height: height}.Save(s.db) @@ -163,6 +160,6 @@ func (s *Store) SaveSnapshot(ctx context.Context, height uint64, snapshot *state } func (s *Store) FinalizeBlock(ctx context.Context, height uint64) error { -// _, err := s.db.ExecContext(ctx, `SELECT pg_notify('newblock', $1)`, height) + // _, err := s.db.ExecContext(ctx, `SELECT pg_notify('newblock', $1)`, height) return nil } -- 2.11.0