allmende-sim: latency and connection loss, attackers, seeds down, anti-entropy repair, 300 nodes
Findings: default libp2p connection limits strand nodes on a bootstrap node beyond ~190 peers, there is no peer scoring, and newcomers cannot find their region without DHT servers when their remembered peers are elsewhere. Documented in docs/PROTOCOL.md and SPECS.md. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
@@ -25,6 +25,7 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/libp2p/go-libp2p"
|
||||
"github.com/libp2p/go-libp2p/core/metrics"
|
||||
|
||||
"git.stinnesbeck.com/nils/allmende/internal/identity"
|
||||
@@ -49,6 +50,9 @@ type simNode struct {
|
||||
node *p2p.Node // nil while the node is down
|
||||
bw *metrics.BandwidthCounter
|
||||
regions []string
|
||||
// bootstrap, if set, is where this node looks for its first peers instead
|
||||
// of the seeds.
|
||||
bootstrap []string
|
||||
}
|
||||
|
||||
func (n *simNode) follows(region string) bool {
|
||||
@@ -69,6 +73,10 @@ type sim struct {
|
||||
discover time.Duration
|
||||
antiEnt time.Duration
|
||||
rng *rand.Rand
|
||||
extra []libp2p.Option // transport conditions (latency, connection loss)
|
||||
timeout time.Duration
|
||||
made []*listing.Listing
|
||||
seedConns int // raised connection limit of the seeds; 0 keeps libp2p's default
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -83,6 +91,12 @@ func main() {
|
||||
discover := flag.Duration("discover", 5*time.Second, "how often nodes look for region peers (a real node: 1m)")
|
||||
antiEntropy := flag.Duration("anti-entropy", 30*time.Minute, "anti-entropy interval (a real node: 30m)")
|
||||
timeout := flag.Duration("timeout", 60*time.Second, "how long to wait for each thing to spread")
|
||||
latency := flag.Duration("latency", 0, "delay added to every write on connections a node dials (one direction), with up to 50% jitter")
|
||||
lifetime := flag.Duration("lifetime", 0, "mean lifetime of a dialed connection before it is cut (0: never); redials and resyncs follow")
|
||||
seedConns := flag.Int("seed-conns", 0, "raise the seeds' inbound connection limit to this many (0: libp2p's default, which scales with RAM)")
|
||||
hostile := flag.Bool("hostile", false, "also run attackers: invalid listings, a message flood")
|
||||
killSeeds := flag.Bool("kill-seeds", false, "also shut the seeds down and see what the network and newcomers do")
|
||||
repair := flag.Bool("repair", false, "also test anti-entropy: a listing only one node has, nobody told (set -anti-entropy to a few seconds)")
|
||||
seed := flag.Uint64("seed", 1, "random seed, so a run can be repeated")
|
||||
flag.Parse()
|
||||
if *nregions < 1 || *nregions > len(allRegions) || *seeds < 1 || *nodes <= *seeds+1 {
|
||||
@@ -93,7 +107,12 @@ func main() {
|
||||
s := &sim{
|
||||
ctx: context.Background(), difficulty: 8, msgDiff: 6,
|
||||
discover: *discover, antiEnt: *antiEntropy,
|
||||
rng: rand.New(rand.NewPCG(*seed, 7)),
|
||||
rng: rand.New(rand.NewPCG(*seed, 7)), timeout: *timeout, seedConns: *seedConns,
|
||||
}
|
||||
var conditions *netConditions
|
||||
if *latency > 0 || *lifetime > 0 {
|
||||
conditions = &netConditions{latency: *latency, lifetime: *lifetime}
|
||||
s.extra = conditions.options()
|
||||
}
|
||||
regions := allRegions[:*nregions]
|
||||
report0 := newReport()
|
||||
@@ -153,6 +172,7 @@ func main() {
|
||||
// --- messages ------------------------------------------------------------
|
||||
fmt.Printf("sending %d buyer messages ...\n", *messages)
|
||||
pairs = nil
|
||||
refused := 0
|
||||
for i := 0; i < *messages; i++ {
|
||||
l := made[s.rng.IntN(len(made))]
|
||||
owner := s.byAuthor(l.Author)
|
||||
@@ -164,9 +184,15 @@ func main() {
|
||||
must(err)
|
||||
env, err := message.SealToOwner(l, tk, &message.Plain{Type: message.TypeText, N: 1, SentAt: time.Now().Unix(), Text: "Ist die Wohnung noch frei?"}, time.Now(), s.msgDiff)
|
||||
must(err)
|
||||
must(via.node.PublishMessage(s.ctx, listing.Region(l.Geohash), env))
|
||||
if err := via.node.PublishMessage(s.ctx, listing.Region(l.Geohash), env); err != nil {
|
||||
refused++ // the buyer's gateway does not hold the listing (yet)
|
||||
continue
|
||||
}
|
||||
pairs = append(pairs, &pair{start: time.Now(), check: s.ownerHas(owner, env)})
|
||||
}
|
||||
if refused > 0 {
|
||||
report0.row("message!", fmt.Sprintf("%d of %d messages were refused by the buyer's own node: it did not hold the listing", refused, *messages))
|
||||
}
|
||||
res = await(pairs, *timeout)
|
||||
report0.latency("message", "from a random node of the region to the listing's owner", res)
|
||||
|
||||
@@ -230,8 +256,23 @@ func main() {
|
||||
report0.latency("rejoin", "a node back from offline has every listing of its regions", res)
|
||||
made = append(made, extra...)
|
||||
}
|
||||
s.made = made
|
||||
|
||||
if *hostile {
|
||||
s.hostile(regions, report0)
|
||||
}
|
||||
if *repair {
|
||||
s.repair(report0)
|
||||
}
|
||||
if *killSeeds {
|
||||
s.killSeeds(regions, report0)
|
||||
}
|
||||
|
||||
// --- do all nodes agree? ---------------------------------------------------
|
||||
if conditions != nil {
|
||||
report0.row("network", fmt.Sprintf("latency %s (±50%%) per write on dialed connections, mean connection life %s: %d connections were cut",
|
||||
*latency, *lifetime, conditions.cuts.Load()))
|
||||
}
|
||||
report0.row("agreement", s.agreement(regions))
|
||||
report0.resources("at the end", s)
|
||||
report0.print(os.Stdout)
|
||||
@@ -265,11 +306,20 @@ func (s *sim) makeIdentity(n *simNode) error {
|
||||
|
||||
// start (re)starts a node's p2p side on its existing identity and store.
|
||||
func (s *sim) start(n *simNode) error {
|
||||
bootstrap := n.bootstrap
|
||||
if bootstrap == nil {
|
||||
bootstrap = s.seedAddrs
|
||||
}
|
||||
extra := s.extra
|
||||
if n.seed && s.seedConns > 0 {
|
||||
extra = append(append([]libp2p.Option(nil), extra...), seedLimits(s.seedConns))
|
||||
}
|
||||
node, err := p2p.New(s.ctx, p2p.Config{
|
||||
Device: n.dev,
|
||||
Store: n.st,
|
||||
ListenAddrs: []string{"/ip4/127.0.0.1/tcp/0"},
|
||||
Bootstrap: s.seedAddrs,
|
||||
Bootstrap: bootstrap,
|
||||
ExtraOptions: extra,
|
||||
Regions: n.regions,
|
||||
DHT: true,
|
||||
Public: n.seed,
|
||||
@@ -373,7 +423,28 @@ func (s *sim) waitFormed(regions []string, timeout time.Duration) string {
|
||||
}
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
}
|
||||
return "timed out: some nodes still lack regional peers"
|
||||
alone, short := 0, 0
|
||||
for _, n := range s.nodes {
|
||||
if n.seed {
|
||||
continue
|
||||
}
|
||||
if n.node.PeerCount() == 0 {
|
||||
alone++
|
||||
}
|
||||
for _, r := range n.regions {
|
||||
if len(n.node.RegionPeers(r)) < min(2, len(s.followers(r))-1) {
|
||||
short++
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
var seedPeers []int
|
||||
for _, n := range s.nodes {
|
||||
if n.seed {
|
||||
seedPeers = append(seedPeers, n.node.PeerCount())
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("timed out: %d nodes lack regional peers, %d of them have no peer at all; the seeds each hold %v connections", short, alone, seedPeers)
|
||||
}
|
||||
|
||||
// --- what a node has ---------------------------------------------------------------
|
||||
@@ -435,6 +506,9 @@ func (s *sim) agreement(regions []string) string {
|
||||
}
|
||||
reps := map[string]int{}
|
||||
for _, n := range s.nodes {
|
||||
if n.node == nil {
|
||||
continue // a node that is down holds what it held
|
||||
}
|
||||
d, err := n.st.ReportDigest(s.ctx)
|
||||
must(err)
|
||||
reps[fmt.Sprintf("%d/%s", d.Count, d.Root[:8])]++
|
||||
@@ -500,7 +574,10 @@ type reportTable struct {
|
||||
|
||||
func newReport() *reportTable { return &reportTable{} }
|
||||
|
||||
func (r *reportTable) row(k, v string) { r.rows = append(r.rows, [2]string{k, v}) }
|
||||
func (r *reportTable) row(k, v string) {
|
||||
r.rows = append(r.rows, [2]string{k, v})
|
||||
fmt.Printf(" %-10s %s\n", k, v) // live, so a crash later does not lose it
|
||||
}
|
||||
|
||||
func (r *reportTable) latency(k, what string, ps []*pair) {
|
||||
var d []time.Duration
|
||||
|
||||
Reference in New Issue
Block a user