diff --git a/persist/sqlite/peers.go b/persist/sqlite/peers.go index 3a5e0f3..3ba38ac 100644 --- a/persist/sqlite/peers.go +++ b/persist/sqlite/peers.go @@ -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 diff --git a/persist/sqlite/peers_test.go b/persist/sqlite/peers_test.go index 16b0732..62fe6ad 100644 --- a/persist/sqlite/peers_test.go +++ b/persist/sqlite/peers_test.go @@ -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 @@ -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) {