Skip to content

Commit

Permalink
fix send cli bug
Browse files Browse the repository at this point in the history
  • Loading branch information
TheStarBoys committed Dec 29, 2019
1 parent f4e4875 commit 90f22c6
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions blockchain_v3/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,37 +99,37 @@ func NewBlockchain(nodeID string) *Blockchain {
}

// AddBlock 保存区块到区块链中
// 下面代码顺序不能变,因为会出现从最新块开始接收节点传来的区块数据
// 认为中心节点是受信任的,不对区块数据进行验证,直接添加
func (bc *Blockchain) AddBlock(block *Block) {
err := bc.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(blocksBucket))

blockInDb := b.Get(block.Hash)
// 不允许添加已存在的区块到数据库中
if blockInDb != nil {
// block already exist, just ignore
return nil
}

lastBlockHash := b.Get([]byte("l"))
lastBlockData := b.Get(lastBlockHash)
lastBlock := DeserializeBlock(lastBlockData)
if lastBlock.Height > block.Height {
return fmt.Errorf("block height error, lastBlockHeight: %v > blockHeight: %v", lastBlock.Height, block.Height)
}

err := b.Put(block.Hash, block.Serialize())
// 添加区块到数据库
blockData := block.Serialize()
err := b.Put(block.Hash, blockData)
if err != nil {
return err
log.Panic(err)
}
err = b.Put([]byte("l"), block.Hash)
if err != nil {
return err
// 获取最后一个区块
lastHash := b.Get([]byte("l"))
lastBlockData := b.Get(lastHash)
lastBlock := DeserializeBlock(lastBlockData)
// 更新存储最后一个区块的哈希
if block.Height > lastBlock.Height {
err = b.Put([]byte("l"), block.Hash)
if err != nil {
log.Panic(err)
}
bc.tip = block.Hash
}

bc.tip = block.Hash

return nil
})

if err != nil {
log.Panic(err)
}
Expand Down
Binary file modified blockchain_v3/blockchain_3000.db
Binary file not shown.
Binary file modified blockchain_v3/blockchain_3001.db
Binary file not shown.
Binary file modified blockchain_v3/blockchain_3002.db
Binary file not shown.
Binary file added blockchain_v3/wallet_3003.dat
Binary file not shown.

0 comments on commit 90f22c6

Please sign in to comment.