feat(chain): remove channels, add social feed (Phase A of v2.0.0)

Replaces the channel/membership model with a VK/Twitter-style feed:
public posts, follow graph, likes. Views are deliberately off-chain
(counted by the hosting relay, Phase B).

Removed
  - EventCreateChannel, EventAddMember
  - CreateChannelPayload, AddMemberPayload, ChannelMember
  - prefixChannel, prefixChanMember
  - chain.Channel(), chain.ChannelMembers()
  - node/api_channels.go
  - GetChannel, GetChannelMembers on ExplorerQuery

Added
  - Events: CREATE_POST, DELETE_POST, FOLLOW, UNFOLLOW, LIKE_POST, UNLIKE_POST
  - Payloads: CreatePostPayload, DeletePostPayload, FollowPayload,
    UnfollowPayload, LikePostPayload, UnlikePostPayload
  - Stored shape: PostRecord (author, size, hash, hosting relay, timestamp,
    reply/quote refs, soft-delete flag, fee paid)
  - State prefixes: post:, postbyauthor:, follow:, followin:, like:, likecount:
  - Queries: Post(), PostsByAuthor(), Following(), Followers(),
    LikeCount(), HasLiked()
  - Cached like counter via bumpLikeCount helper

Pricing
  - BasePostFee = 1000 µT (aligned with MinFee block-validation floor)
  - PostByteFee = 1 µT/byte of compressed content
  - Total fee credited in full to HostingRelay pub (storage compensation)
  - MaxPostSize = 256 KiB

Integrity
  - CREATE_POST validates content_hash length (32 B) and size range
  - DELETE_POST restricted to post.Author
  - Duplicate FOLLOW / LIKE rejected
  - reply_to and quote_of mutually exclusive

Tests
  - TestFeedCreatePost: post stored, indexed, host credited
  - TestFeedInsufficientFee: underpaid post is skipped
  - TestFeedFollowUnfollow: follow graph round-trips via forward + inbound indices
  - TestFeedLikeUnlike: like toggles with dedup, counter stays accurate
  - TestFeedDeletePostByOther: non-author deletion rejected

This is Phase A (chain-layer). Phase B adds the relay feed-mailbox
(post bodies + gossipsub) and HTTP endpoints. Phase C adds the client
Feed tab.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
vsecoder
2026-04-18 18:36:00 +03:00
parent f2cb5586ca
commit 88848efa63
6 changed files with 695 additions and 168 deletions

View File

@@ -1,102 +0,0 @@
// Package node — channel endpoints.
//
// `/api/channels/:id/members` returns every Ed25519 pubkey registered as a
// channel member together with their current X25519 pubkey (from the
// identity registry). Clients sealing a message to a channel iterate this
// list and call relay.Seal once per recipient — that's the "fan-out"
// group-messaging model (R1 in the roadmap).
//
// Why enrich with X25519 here rather than making the client do it?
// - One HTTP round trip vs N. At 10+ members the latency difference is
// significant over mobile networks.
// - The server already holds the identity state; no extra DB hops.
// - Clients get a stable, already-joined view — if a member hasn't
// published an X25519 key yet, we return them with `x25519_pub_key=""`
// so the caller knows to skip or retry later.
package node
import (
"fmt"
"net/http"
"strings"
"go-blockchain/blockchain"
"go-blockchain/wallet"
)
func registerChannelAPI(mux *http.ServeMux, q ExplorerQuery) {
// GET /api/channels/{id} → channel metadata
// GET /api/channels/{id}/members → enriched member list
//
// One HandleFunc deals with both by sniffing the path suffix.
mux.HandleFunc("/api/channels/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
jsonErr(w, fmt.Errorf("method not allowed"), 405)
return
}
path := strings.TrimPrefix(r.URL.Path, "/api/channels/")
path = strings.Trim(path, "/")
if path == "" {
jsonErr(w, fmt.Errorf("channel id required"), 400)
return
}
switch {
case strings.HasSuffix(path, "/members"):
id := strings.TrimSuffix(path, "/members")
handleChannelMembers(w, q, id)
default:
handleChannelInfo(w, q, path)
}
})
}
func handleChannelInfo(w http.ResponseWriter, q ExplorerQuery, channelID string) {
if q.GetChannel == nil {
jsonErr(w, fmt.Errorf("channel queries not configured"), 503)
return
}
ch, err := q.GetChannel(channelID)
if err != nil {
jsonErr(w, err, 500)
return
}
if ch == nil {
jsonErr(w, fmt.Errorf("channel %s not found", channelID), 404)
return
}
jsonOK(w, ch)
}
func handleChannelMembers(w http.ResponseWriter, q ExplorerQuery, channelID string) {
if q.GetChannelMembers == nil {
jsonErr(w, fmt.Errorf("channel queries not configured"), 503)
return
}
pubs, err := q.GetChannelMembers(channelID)
if err != nil {
jsonErr(w, err, 500)
return
}
out := make([]blockchain.ChannelMember, 0, len(pubs))
for _, pub := range pubs {
member := blockchain.ChannelMember{
PubKey: pub,
Address: wallet.PubKeyToAddress(pub),
}
// Best-effort X25519 lookup — skip silently on miss so a member
// who hasn't published their identity yet doesn't prevent the
// whole list from returning. The sender will just skip them on
// fan-out and retry later (after that member does register).
if q.IdentityInfo != nil {
if info, err := q.IdentityInfo(pub); err == nil && info != nil {
member.X25519PubKey = info.X25519Pub
}
}
out = append(out, member)
}
jsonOK(w, map[string]any{
"channel_id": channelID,
"count": len(out),
"members": out,
})
}

View File

@@ -79,12 +79,6 @@ type ExplorerQuery struct {
GetNFTs func() ([]blockchain.NFTRecord, error)
NFTsByOwner func(ownerPub string) ([]blockchain.NFTRecord, error)
// Channel group-messaging lookups (R1). GetChannel returns metadata;
// GetChannelMembers returns the Ed25519 pubkey of every current member.
// Both may be nil on nodes that don't expose channel state (tests).
GetChannel func(channelID string) (*blockchain.CreateChannelPayload, error)
GetChannelMembers func(channelID string) ([]string, error)
// Events is the SSE hub for the live event stream. Optional — if nil the
// /api/events endpoint returns 501 Not Implemented.
Events *SSEHub
@@ -127,7 +121,6 @@ func RegisterExplorerRoutes(mux *http.ServeMux, q ExplorerQuery, flags ...Explor
registerUpdateCheckAPI(mux, q)
registerOnboardingAPI(mux, q)
registerTokenAPI(mux, q)
registerChannelAPI(mux, q)
if !f.DisableSwagger {
registerSwaggerRoutes(mux)
}