Teranode P2P is a peer-to-peer networking component for the BSV Blockchain ecosystem. It now includes message persistence, a realtime websocket API, and a lightweight frontend for viewing/searching messages.
- Decentralized Networking: Connects to other nodes using a secure, private DHT (Distributed Hash Table).
- Configurable Topics: Subscribes to and handles key BSV Blockchain network topics (blocks, transactions, mining status, etc.).
- Message Persistence: All received messages are stored in a SQLite database (configurable path).
- Realtime Websocket: All new messages are broadcast to connected websocket clients.
- Searchable API: Query messages by topic or peer via a REST API.
- Lightweight Frontend: View and search messages in realtime at
/
(served fromfrontend/index.html
).
- Go 1.18 or later
- GORM and gorilla/websocket (declared in
go.mod
)
Clone the repository and ensure dependencies are installed:
git clone https://github.com/bsv-blockchain/teranode-p2p.git
cd teranode-p2p
go mod tidy
Create a config.yaml
in your project root. Example:
database:
path: "./messages.db"
p2p:
bootstrap_addresses:
- "address1"
- "address2"
shared_key: "your-shared-key"
dht_protocol_id: "your-protocol-id"
port: 12345
listen_addresses:
- "/ip4/0.0.0.0/tcp/12345"
advertise: true
use_private_dht: true
topics:
- "bitcoin/mainnet-block"
- "bitcoin/mainnet-tx"
- database.path: Path to your SQLite database file.
- topics: List of topics to subscribe to.
- Other
p2p
keys as needed for your network.
To start the P2P node and HTTP server:
go run main.go
- The HTTP server listens on port 8080 by default.
- The frontend is available at http://localhost:8080/
- Endpoint:
ws://localhost:8080/ws
- Description: Receives all new messages in realtime as JSON objects.
- Example JS usage:
const ws = new WebSocket("ws://localhost:8080/ws"); ws.onmessage = (event) => { const msg = JSON.parse(event.data); // Render or process msg };
- Endpoint:
GET /messages
- Query Parameters:
topic
(optional): Filter by topicpeer
(optional): Filter by sender/peerlimit
(optional): Max results (default 100, max 500)offset
(optional): Skip N results (default 0)
- Response: JSON array of messages, each with:
ID
,Topic
,Data
,Peer
,ReceivedAt
- Example:
GET /messages?topic=bitcoin/mainnet-block&limit=20
- Location: http://localhost:8080/
- Features:
- Realtime message feed (via websocket)
- Search/filter messages by topic or peer (via API)
cmd/main.go
: Entry point, creates and runs the P2P node, and wires up HTTP/websocket servers.pkg/http/server.go
: Implements the HTTP REST API for message search and serves the static frontend.pkg/websocket/broadcast.go
: Implements the websocket server and manages broadcasting messages to clients.pkg/model/message.go
: Message model for GORM/SQLite.pkg/p2p/node.go
,pkg/p2p/interface.go
,pkg/p2p/types.go
: P2P networking core and interfaces.frontend/index.html
: Lightweight UI for realtime and historical message viewing.config.yaml
: Configuration file (see above).go.mod
,go.sum
: Go module dependencies.
curl 'http://localhost:8080/messages?topic=bitcoin/mainnet-block'
curl 'http://localhost:8080/messages?peer=peer1'
curl 'http://localhost:8080/messages?limit=10&offset=20'
curl 'http://localhost:8080/messages?topic=bitcoin/mainnet-block&peer=peer1&limit=5'
const ws = new WebSocket("ws://localhost:8080/ws");
ws.onmessage = (event) => {
const msg = JSON.parse(event.data);
// handle msg
};
npx wscat -c ws://localhost:8080/ws
bitcoin/mainnet-bestblock
bitcoin/mainnet-block
bitcoin/mainnet-subtree
bitcoin/mainnet-mining_on
bitcoin/mainnet-handshake
bitcoin/mainnet-rejected_tx
This project is part of the BSV Blockchain initiative. See BSV Blockchain License for details.
- BSV Blockchain community