@@ -14,27 +14,26 @@ var LastKey = []byte("LAST")
1414type Storage interface {
1515 FindLastOrCreate (ctx context.Context ) block.Block
1616 Create (ctx context.Context , key string , b block.Block ) error
17- FindByHash (ctx context.Context , hash string ) block.Block
17+ FindByHash (ctx context.Context , hash string ) ( block.Block , error )
1818 FindLast (ctx context.Context ) (block.Block , error )
1919 UpdateLast (ctx context.Context , b block.Block ) error
2020}
2121
22- type BlockStore struct {
22+ type ChainStore struct {
2323 store * badger.DB
2424}
2525
26-
27- // findLastOrCreate used to get the last block in the chain or creates a new genesis block
26+ // FindLastOrCreate used to get the last block in the chain or creates a new genesis block
2827//
2928// Process:
3029// - Queries storage to get the block at the "LAST" position
3130// - If the block is not found then create the Genesis block for this chain, else returns the last found block
3231//
3332// Returns
3433// - last: The block at the "LAST" position
35- func (bs * BlockStore ) FindLastOrCreate () (last block.Block ) {
34+ func (cs * ChainStore ) FindLastOrCreate (ctx context. Context ) (last block.Block ) {
3635 // try to findLast the block with the key 'LAST'
37- last , err := bs .FindLast ()
36+ last , err := cs .FindLast (ctx )
3837 if err != nil {
3938 //TODO: standardize logging
4039 fmt .Printf ("error finding last block err: [%s]\n " , err .Error ())
@@ -46,7 +45,7 @@ func (bs *BlockStore) FindLastOrCreate() (last block.Block) {
4645 // if not found create the genesis block
4746 fmt .Printf ("creating new genesis block\n " )
4847 last = block .NewGenesisBlock ()
49- err = bs .Create (string (LastKey ), last )
48+ err = cs .Create (ctx , string (LastKey ), last )
5049 if err != nil {
5150 fmt .Printf ("error while creating last item %s" , err .Error ())
5251 }
@@ -67,8 +66,8 @@ func (bs *BlockStore) FindLastOrCreate() (last block.Block) {
6766//
6867// Returns
6968// - error: Returns the error during block creation process
70- func (bs * BlockStore ) Create (key string , b block.Block ) error {
71- err := bs .store .Update (func (txn * badger.Txn ) (err error ) {
69+ func (cs * ChainStore ) Create (ctx context. Context , key string , b block.Block ) error {
70+ err := cs .store .Update (func (txn * badger.Txn ) (err error ) {
7271 data , err := b .Serialize ()
7372 if err != nil {
7473 return err
@@ -82,7 +81,7 @@ func (bs *BlockStore) Create(key string, b block.Block) error {
8281 return err
8382}
8483
85- // findByHash finds a block by the given hash
84+ // FindByHash finds a block by the given hash
8685//
8786// Parameters:
8887// - hash(string): The hash of the block that we want to retrieve
@@ -94,9 +93,9 @@ func (bs *BlockStore) Create(key string, b block.Block) error {
9493// Returns
9594// - b(block): Returns the block just found
9695// - err(error): Returns the error during block creation process
97- func (bs * BlockStore ) FindByHash (hash string ) (b block.Block , err error ) {
96+ func (cs * ChainStore ) FindByHash (ctx context. Context , hash string ) (b block.Block , err error ) {
9897 b = block.Block {}
99- err = bs .store .View (func (txn * badger.Txn ) error {
98+ err = cs .store .View (func (txn * badger.Txn ) error {
10099 lastBlock , err := txn .Get ([]byte (hash ))
101100 if err != nil {
102101 return err
@@ -110,7 +109,7 @@ func (bs *BlockStore) FindByHash(hash string) (b block.Block, err error) {
110109 return b , err
111110}
112111
113- // findByHash finds a block by the given hash
112+ // FindLast finds a block by the given hash
114113//
115114// Process:
116115// - Retrieves the block in bytes if it exists, else returns error
@@ -119,9 +118,9 @@ func (bs *BlockStore) FindByHash(hash string) (b block.Block, err error) {
119118// Returns
120119// - block: Returns the block just found
121120// - error: Returns the error during block creation process
122- func (bs * BlockStore ) FindLast () (block.Block , error ) {
121+ func (cs * ChainStore ) FindLast (ctx context. Context ) (block.Block , error ) {
123122 b := & block.Block {}
124- err := bs .store .View (func (txn * badger.Txn ) error {
123+ err := cs .store .View (func (txn * badger.Txn ) error {
125124 lastBlock , err := txn .Get (LastKey )
126125 if err != nil {
127126 return err
@@ -143,8 +142,8 @@ func (bs *BlockStore) FindLast() (block.Block, error) {
143142//
144143// Returns
145144// - error: Returns the error during update process
146- func (bs * BlockStore ) UpdateLast (b block.Block ) (err error ) {
147- err = bs .store .Update (func (txn * badger.Txn ) error {
145+ func (cs * ChainStore ) UpdateLast (ctx context. Context , b block.Block ) (err error ) {
146+ err = cs .store .Update (func (txn * badger.Txn ) error {
148147 data , err := b .Serialize ()
149148 if err != nil {
150149 return err
0 commit comments