35 lines
589 B
Go
35 lines
589 B
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/boltdb/bolt"
|
|
)
|
|
|
|
// BlockchainIterator is used to iterate over blockchain blocks
|
|
type BlockchainIterator struct {
|
|
currentHash []byte
|
|
db *bolt.DB
|
|
}
|
|
|
|
// Next returns next block starting from the tip
|
|
func (i *BlockchainIterator) Next() *Block {
|
|
var block *Block
|
|
|
|
err := i.db.View(func(tx *bolt.Tx) error {
|
|
b := tx.Bucket([]byte(blocksBucket))
|
|
encodedBlock := b.Get(i.currentHash)
|
|
block = DeserializeBlock(encodedBlock)
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
i.currentHash = block.PrevBlockHash
|
|
|
|
return block
|
|
}
|