Skip to content

Commit

Permalink
sqlite: fix Peers method
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl committed Mar 18, 2024
1 parent 04b22a4 commit 9b20927
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion persist/sqlite/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s *Store) AddPeer(peer string) error {
// Peers returns the addresses of all known peers.
func (s *Store) Peers() (peers []syncer.PeerInfo, _ error) {
err := s.transaction(func(tx *txn) error {
const query = `SELECT peer_address FROM syncer_peers`
const query = `SELECT peer_address, first_seen, last_connect, synced_blocks, sync_duration FROM syncer_peers`
rows, err := tx.Query(query)
if err != nil {
return err
Expand Down
19 changes: 18 additions & 1 deletion persist/sqlite/peers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestAddPeer(t *testing.T) {
t.Fatal(err)
}

lastConnect := time.Now().Truncate(time.Second) // stored as unix milliseconds
lastConnect := time.Now().UTC().Truncate(time.Second) // stored as unix milliseconds
syncedBlocks := uint64(15)
syncDuration := 5 * time.Second

Expand All @@ -51,6 +51,23 @@ func TestAddPeer(t *testing.T) {
if info.SyncDuration != 5*time.Second {
t.Errorf("expected SyncDuration = %s; got %s", syncDuration, info.SyncDuration)
}

peers, err := db.Peers()
if err != nil {
t.Fatal(err)
} else if len(peers) != 1 {
t.Fatalf("expected 1 peer; got %d", len(peers))
} else if peerInfo := peers[0]; peerInfo.Address != peer {
t.Errorf("expected peer address = %q; got %q", peer, peerInfo.Address)
} else if peerInfo.LastConnect != lastConnect {
t.Errorf("expected LastConnect = %v; got %v", lastConnect, peerInfo.LastConnect)
} else if peerInfo.SyncedBlocks != syncedBlocks {
t.Errorf("expected SyncedBlocks = %d; got %d", syncedBlocks, peerInfo.SyncedBlocks)
} else if peerInfo.SyncDuration != syncDuration {
t.Errorf("expected SyncDuration = %s; got %s", syncDuration, peerInfo.SyncDuration)
} else if peerInfo.FirstSeen.IsZero() {
t.Errorf("expected FirstSeen to be non-zero; got %v", peerInfo.FirstSeen)
}
}

func TestBanPeer(t *testing.T) {
Expand Down

0 comments on commit 9b20927

Please sign in to comment.