brain

package
v0.4.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 25, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package brain is a generic, mode-agnostic bot-orchestration layer: it allocates a slot on a Catscope/Solpipe validator pipeline, then uploads whatever WASM bot images are requested of it over time (via Request), handing back a Bot object per upload -- the running instance's handshake, an on-disk log file of its stderr output, and plain Go channels for its stdin/stdout messaging.

This package deliberately knows nothing about any specific trading strategy's wire protocol (message key flags, wallet funding, latency reports, etc.) -- that belongs to whatever code drives a given Bot's SendC/RecvC. See brain/arbv1, brain/testperpv1, and siblings for that.

Index

Constants

View Source
const KeyFlagWallet uint8 = 3

KeyFlagWallet is the one wire-protocol key flag every bot mode in this codebase already shares byte-for-byte (see brain/arbv1, brain/testperpv1, brain/testperplatencyv1, brain/perpfundingv1, brain/helloworldv1, brain/leveragedloopv1, and brain/multimodelv1's own message.go, each with an identical KeyFlagWallet = 3 and DoWallet of their own) -- unlike higher-numbered, per-strategy key flags, this one is universal: every WASM bot needs a signing key sent to it exactly this way before it can do anything real. Kept here, once, rather than making every caller of this package duplicate it again.

Variables

This section is empty.

Functions

func ChildID

func ChildID(index uint32) sgo.PublicKey

ChildID builds an sgo.PublicKey-shaped id from a plain uint32 index -- a copy of sgo.SystemProgramID (32 zero bytes) with index's 4 big-endian bytes written into the last 4 bytes, e.g. ChildID(1) is 27 zero bytes followed by 0x00 0x00 0x00 0x01.

Meant for common.DeriveChildKeyV2 (and for harness.Treasury.Child/ Budget, which key children by this same kind of id) wherever a caller wants one child wallet per small integer index -- e.g. one per bot uploaded via this package's own Request/UploadRequest, so concurrently-running bots never share a signing key.

Deliberately a different convention from common.DeriveChildKeyFromIndex's own id-construction (a little-endian uint64 written into the FIRST 8 bytes) -- this is not a drop-in replacement for that, just a second, explicit option.

func DoWallet

func DoWallet(key sgo.PrivateKey) catmsg.FixedPair

DoWallet builds the stdin message (send it via Bot.SendC) that gives a bot key as its own trading wallet -- see KeyFlagWallet's own doc comment. Panics on a malformed key, same as every per-mode DoWallet this mirrors: a wrong-length ed25519 private key here is a caller bug, not a runtime condition to recover from.

Types

type Bot

type Bot struct {

	// SendC delivers messages to the bot's stdin, in order -- send-only
	// from the caller's side. Keep sending until ErrC fires; there's no
	// need (and no way) to close SendC yourself.
	SendC chan<- catmsg.FixedPair
	// RecvC delivers every message the bot sends via stdout, in order.
	// Closed once the bot's connection ends (check ErrC for why).
	RecvC <-chan catmsg.FixedPair
	// ErrC receives exactly one value when the bot's connection ends --
	// nil for a clean shutdown (context cancelled), non-nil otherwise
	// (stdout subscription error or the bot process itself exiting).
	ErrC <-chan error
	// contains filtered or unexported fields
}

Bot represents one running WASM bot instance uploaded to a validator pipeline: its handshake, an on-disk log file of its real stderr output, and plain Go channels for its stdin/stdout messaging. Callers define their own wire protocol on top of the raw catmsg.FixedPair values flowing through SendC/RecvC -- this package doesn't know or care what any specific bot mode's key flags mean (see message.go in brain/arbv1 and siblings for that).

func (*Bot) Close

func (b *Bot) Close() error

Close closes the underlying bot connection and its log file. SendC/ RecvC/ErrC are left as-is (RecvC closes and ErrC fires on its own, same as any other connection end, once the close takes effect).

func (*Bot) Handshake

func (b *Bot) Handshake() mgrbot.Handshake

func (*Bot) LogPath

func (b *Bot) LogPath() string

func (*Bot) Mode

func (b *Bot) Mode() string

func (*Bot) Pipeline

func (b *Bot) Pipeline() sgo.PublicKey

func (*Bot) Wallet

func (b *Bot) Wallet() sgo.PublicKey

type Configuration

type Configuration struct {
	// LogDir is where each uploaded bot's stderr log file is created --
	// one file per upload, named "<mode>-<pipeline>-<unixnano>.log".
	// Defaults to os.TempDir() if empty.
	LogDir string
}

type Hook

type Hook interface {
	brain.Brain
	// Request submits req to the background upload dispatcher Init
	// starts. Non-blocking as long as the dispatcher isn't badly
	// backlogged (the request channel is buffered) -- req.ResultC
	// receives exactly one *UploadResult once the upload finishes
	// (success or failure). If req.ResultC is nil, Request allocates
	// one (buffered by 1) before submitting.
	Request(req *UploadRequest)
	// Bots returns a snapshot of every bot this Hook has successfully
	// uploaded and not yet closed, keyed by the pipeline it's running
	// on. Safe to call concurrently with Request/the dispatcher.
	Bots() map[sgo.PublicKey]*Bot
}

Hook is brain.Brain (what mothership.Create needs to drive this as a real bot-mode) plus Request/Bots -- the two ways callers actually drive uploads and observe what's running.

func Create

func Create(ctx context.Context, cancel context.CancelCauseFunc, parentKey sgo.PrivateKey, config *Configuration) Hook

Create builds a Hook. cancel is called (with the eventual error, if any) when the dispatcher loop exits -- same convention every other brain/* eventHook already uses via mothership.Create's own lifecycle.

type UploadRequest

type UploadRequest struct {
	// Mode is the MODE env var the WASM bot image dispatches on at
	// startup (e.g. "arbv1", "testperpv1", "testlatencylitev1") --
	// see catscope-rust-bot's brain::mod BotMode::from_env.
	Mode string
	// BotImagePath is a local path to compile from; empty downloads the
	// published default image instead (see downloadDefaultImage).
	BotImagePath string
	// Env is merged with {"MODE": Mode} and forwarded to the image at
	// load/compile time (e.g. TEST_PROTOCOL) -- may be nil.
	Env map[string]string
	// Pipeline selects which validator to upload to; the zero value
	// uses the pipeline Init allocated (see eventHook.defaultPipeline).
	Pipeline sgo.PublicKey
	// Timeout bounds the whole allocate-a-connection/upload/handshake
	// retry loop; <= 0 uses defaultUploadTimeout.
	Timeout time.Duration
	// ResultC receives exactly one *UploadResult. Request allocates
	// this (buffered by 1) if left nil.
	ResultC chan *UploadResult
}

UploadRequest describes one bot image to upload and run. Submit via Hook.Request; the result (a *Bot, or an error) arrives on ResultC.

func NewUploadRequest

func NewUploadRequest(mode string) *UploadRequest

NewUploadRequest builds a request with ResultC already allocated -- the common case (Env/Pipeline/Timeout left at their zero values).

type UploadResult

type UploadResult struct {
	Bot *Bot
	Err error
}

UploadResult is delivered on an UploadRequest's ResultC exactly once. Exactly one of Bot/Err is set.

Source Files

  • bot.go
  • brain.go
  • childid.go
  • eval.go
  • init.go
  • message.go
  • upload.go

Directories

Path Synopsis
Package arbv1 is the Go-side orchestrator for the arbv1 (arbitrage v1) bot mode.
Package arbv1 is the Go-side orchestrator for the arbv1 (arbitrage v1) bot mode.
Package helloworldv1 is the Go-side orchestrator for the helloworldv1 bot mode.
Package helloworldv1 is the Go-side orchestrator for the helloworldv1 bot mode.
Package leveragedloopv1 is the Go-side orchestrator for leveragedloopv1 -- Phase 2 of catscope-rust-bot's leveraged_yield_farming_plan.md: a single, conservative jitoSOL-collateral/USDC-debt leverage loop on Kamino, manual/explicit trigger only.
Package leveragedloopv1 is the Go-side orchestrator for leveragedloopv1 -- Phase 2 of catscope-rust-bot's leveraged_yield_farming_plan.md: a single, conservative jitoSOL-collateral/USDC-debt leverage loop on Kamino, manual/explicit trigger only.
Package marketwatchv1 is the Go-side orchestrator for the marketwatchv1 bot mode -- forked from optimizer/brain/arbv1, stripped of every trading-specific piece (no wallet, no latency-file, no bundler tip wiring).
Package marketwatchv1 is the Go-side orchestrator for the marketwatchv1 bot mode -- forked from optimizer/brain/arbv1, stripped of every trading-specific piece (no wallet, no latency-file, no bundler tip wiring).
loopInstance runs in its own goroutine after Init completes.
loopInstance runs in its own goroutine after Init completes.
Package perpfundingv1 is the Go-side orchestrator for the perpfundingv1 bot mode (inter-venue perpetual-futures funding-rate observation, Phoenix vs.
Package perpfundingv1 is the Go-side orchestrator for the perpfundingv1 bot mode (inter-venue perpetual-futures funding-rate observation, Phoenix vs.
Package testperplatencyv1 is the Go-side orchestrator for the testperplatencyv1 bot mode -- a real-transaction latency test, not a real trading strategy.
Package testperplatencyv1 is the Go-side orchestrator for the testperplatencyv1 bot mode -- a real-transaction latency test, not a real trading strategy.
Package testperpv1 is the Go-side orchestrator for the testperpv1 bot mode -- a real-transaction smoke test, not a real trading strategy.
Package testperpv1 is the Go-side orchestrator for the testperpv1 bot mode -- a real-transaction smoke test, not a real trading strategy.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL