-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
415 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package repo | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
// Address holds information about unique PublicKey. | ||
type Address struct { | ||
ID primitive.ObjectID `json:"-" bson:"_id,omitempty"` | ||
PublicKey string `json:"public_key" bson:"public_key"` | ||
} | ||
|
||
// WriteAddress writes unique address to the database. | ||
func (db DataBase) WriteAddress(ctx context.Context, address Address) error { | ||
address.ID = primitive.NilObjectID | ||
if address.PublicKey == "" { | ||
return errors.New("public key is empty") | ||
} | ||
|
||
if _, err := db.inner.Collection(addressesCollection).InsertOne(ctx, address); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// CheckAddressExists checks if address exists in the database. | ||
func (db DataBase) CheckAddressExists(ctx context.Context, address string) (bool, error) { | ||
if address == "" { | ||
return false, errors.New("public key is empty") | ||
} | ||
|
||
res := db.inner.Collection(addressesCollection).FindOne(ctx, bson.M{"public_key": address}) | ||
if res.Err() != nil { | ||
if res.Err() == mongo.ErrNoDocuments { | ||
return false, nil | ||
} | ||
return false, res.Err() | ||
} | ||
|
||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package repo | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/bartossh/The-Accountant/block" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
// LastBlock returns last block from the database. | ||
func (db DataBase) LastBlock(ctx context.Context) (block.Block, error) { | ||
var b []block.Block | ||
|
||
opts := options.Find().SetSort(bson.M{"index": 1}).SetLimit(1) | ||
|
||
curs, err := db.inner.Collection(blocksCollection).Find(ctx, bson.M{}, opts) | ||
if err != nil { | ||
return block.Block{}, err | ||
} | ||
|
||
if err := curs.All(ctx, &b); err != nil { | ||
return block.Block{}, err | ||
} | ||
|
||
if len(b) == 0 { | ||
return block.Block{}, errors.New("unreachable code when querying last block") | ||
} | ||
|
||
return b[0], nil | ||
} | ||
|
||
// ReadBlockByHash returns block with given hash. | ||
func (db DataBase) ReadBlockByHash(ctx context.Context, hash [32]byte) (block.Block, error) { | ||
var b block.Block | ||
if err := db.inner.Collection(blocksCollection).FindOne(ctx, bson.M{"hash": hash}).Decode(&b); err != nil { | ||
return block.Block{}, err | ||
} | ||
return b, nil | ||
} | ||
|
||
// WriteBlock writes block to the database. | ||
func (db DataBase) WriteBlock(ctx context.Context, block block.Block) error { | ||
if _, err := db.inner.Collection(blocksCollection).InsertOne(ctx, block); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.