Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MINOR] Logging + Hardening #39

Merged
merged 2 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions blocker/blocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,14 @@ func (bl *Blocker) managedBlock() error {
ctx, cancel := context.WithTimeout(context.Background(), database.MongoDefaultTimeout)
defer cancel()

bl.staticLogger.Debugf("managedBlock blocking hashes from %v", from)

// Fetch hashes to block
hashes, err := bl.staticDB.HashesToBlock(ctx, from)
if err != nil {
return err
}
bl.staticLogger.Debugf("managedBlock found %d hashes", len(hashes))
if len(hashes) == 0 {
return nil
}
Expand Down
5 changes: 4 additions & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ func (db *DB) BlockedHashes(ctx context.Context, sort, skip, limit int) ([]Block
opts.SetSort(bson.D{{"timestamp_added", sort}})

// fetch the documents
docs, err := db.find(ctx, bson.M{"invalid": bson.M{"$ne": true}}, opts)
docs, err := db.find(ctx, bson.M{
"invalid": bson.M{"$ne": true},
"hash": bson.M{"$exists": true},
}, opts)
if err != nil {
return nil, false, err
}
Expand Down
67 changes: 45 additions & 22 deletions database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ func TestDatabase(t *testing.T) {
name string
test func(t *testing.T)
}{

{
name: "Ping",
test: testPing,
name: "BlockedHashes",
test: testBlockedHashes,
},

{
name: "CreateBlockedSkylink",
test: testCreateBlockedSkylink,
Expand Down Expand Up @@ -83,38 +85,30 @@ func TestDatabase(t *testing.T) {
name: "MarkInvalid",
test: testMarkInvalid,
},
{
name: "Ping",
test: testPing,
},
}
for _, test := range tests {
t.Run(test.name, test.test)
}
}

// testPing is a unit test for the database's Ping method.
func testPing(t *testing.T) {
// testBlockedHashes tests fetching blocked hashes from the database
func testBlockedHashes(t *testing.T) {
// create context
ctx, cancel := context.WithTimeout(context.Background(), MongoDefaultTimeout)
defer cancel()

// create test database
db := newTestDB(t.Name())

// ping should succeed
err := db.Ping(ctx)
if err != nil {
t.Fatal(err)
}

// close it
err = db.Close(ctx)
if err != nil {
t.Fatal(err)
}

// ping should fail
err = db.Ping(ctx)
if err == nil {
t.Fatal("should fail")
}
defer func() {
err := db.Close(ctx)
if err != nil {
t.Fatal(err)
}
}()
}

// testCreateBlockedSkylink tests creating and fetching a blocked skylink from
Expand Down Expand Up @@ -573,6 +567,35 @@ func testMarkInvalid(t *testing.T) {
}
}


// testPing is a unit test for the database's Ping method.
func testPing(t *testing.T) {
// create context
ctx, cancel := context.WithTimeout(context.Background(), MongoDefaultTimeout)
defer cancel()

// create test database
db := newTestDB(t.Name())

// ping should succeed
err := db.Ping(ctx)
if err != nil {
t.Fatal(err)
}

// close it
err = db.Close(ctx)
if err != nil {
t.Fatal(err)
}

// ping should fail
err = db.Ping(ctx)
if err == nil {
t.Fatal("should fail")
}
}

// define a helper function to decode a skylink as string into a skylink obj
func skylinkFromString(skylink string) (sl skymodules.Skylink) {
err := sl.LoadString(skylink)
Expand Down