mirror of
https://github.com/luxfi/dex.git
synced 2026-09-24 00:26:31 +00:00
`go test ./...` failed three ways on the runner. Two were build failures that
cannot happen on a mac, and one was a race that only loses under load.
pkg/dpdk — `undefined: syscall.SO_NOSIGPIPE`. DarwinOptimizations guarded itself
with `if runtime.GOOS != "darwin" { return }`, but SO_NOSIGPIPE is a COMPILE-time
constant that exists only on Darwin; a runtime check cannot save a reference the
linux build still has to resolve. Split into _darwin.go / _nodarwin.go. Verified
by cross-compiling both ways -- it failed for GOOS=linux before, both pass now.
pkg/orderbook — the cgo preamble uses uint64_t while including only <stdlib.h>.
macOS pulls stdint.h in transitively and glibc does not, so this built here and
never on the runner. Added the include.
pkg/price — TestXChainSourceStalePrice: "Expected price to be stale".
Not a flaky test; Close() was not a barrier. It closed the done channel and
returned WITHOUT waiting for the polling goroutine. A loop selecting on done
against a ticker picks a ready case AT RANDOM, so a poll could still be running
after Close() returned, and poll writes last[symbol] = time.Now(). The test
closes the source, backdates that timestamp by 10s, and asserts staleness -- an
in-flight poll overwrote it. Wide window on a loaded runner, near-zero on a
laptop, which is why it reproduces 0/30 here.
All EIGHT sources had it, so the fix is one shared `lifecycle` (embedded, so
`s.done` still resolves) whose stop() joins before returning and is safe to call
twice. Oracle had the same defect written as
// Give time for goroutines to exit
time.Sleep(100 * time.Millisecond)
which could not work either -- calcAverages blocks on a 1s ticker, so it ran up
to a full second after Stop() returned. Now on the same lifecycle, and its loops
select on done instead of polling a flag, so they leave promptly rather than
after one more tick. Nothing restarts a stopped source (checked: 0 tests), and
the other sources already could not.
go build ./... OK
GOOS=linux go build ./... OK (failed before)
go vet price/dpdk/orderbook OK
go test ./pkg/price/ -race ok, 3/3 runs
go test -run StalePrice -count=30 ok
Co-authored-by: Hanzo Dev <[email protected]>
202 lines
4.1 KiB
Go
202 lines
4.1 KiB
Go
package price
|
|
|
|
import (
|
|
"context"
|
|
"math"
|
|
"math/big"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// ChainlinkSource provides prices from Chainlink oracles.
|
|
// Decentralized and highly reliable for reference prices.
|
|
type ChainlinkSource struct {
|
|
feeds map[string]string
|
|
prices map[string]*Data
|
|
last map[string]time.Time
|
|
|
|
interval time.Duration
|
|
timeout time.Duration
|
|
|
|
healthy bool
|
|
failures int
|
|
|
|
mu sync.RWMutex
|
|
lifecycle
|
|
polling bool
|
|
}
|
|
|
|
// NewChainlinkSource creates a Chainlink price source.
|
|
func NewChainlinkSource() *ChainlinkSource {
|
|
return &ChainlinkSource{
|
|
feeds: chainlinkFeeds(),
|
|
prices: make(map[string]*Data),
|
|
last: make(map[string]time.Time),
|
|
interval: 2 * time.Second,
|
|
timeout: 5 * time.Second,
|
|
healthy: true,
|
|
lifecycle: newLifecycle(),
|
|
}
|
|
}
|
|
|
|
func chainlinkFeeds() map[string]string {
|
|
return map[string]string{
|
|
"BTC-USD": "0xF4030086522a5bEEa4988F8cA5B36dbC97BeE88c",
|
|
"ETH-USD": "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419",
|
|
"SOL-USD": "0x4ffC43a60e009B551865A93d232E33Fce9f01507",
|
|
"AVAX-USD": "0xFF3EEb22B5E3dE6e705b44749C2559d704923FD7",
|
|
"LUX-USD": "0x0000000000000000000000000000000000000001", // Placeholder
|
|
}
|
|
}
|
|
|
|
// Start begins polling Chainlink feeds.
|
|
func (s *ChainlinkSource) Start() error {
|
|
s.mu.Lock()
|
|
if s.polling {
|
|
s.mu.Unlock()
|
|
return nil
|
|
}
|
|
s.polling = true
|
|
s.mu.Unlock()
|
|
|
|
s.run(s.loop)
|
|
s.poll()
|
|
return nil
|
|
}
|
|
|
|
func (s *ChainlinkSource) loop() {
|
|
ticker := time.NewTicker(s.interval)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-s.done:
|
|
return
|
|
case <-ticker.C:
|
|
s.poll()
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *ChainlinkSource) poll() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), s.timeout)
|
|
defer cancel()
|
|
|
|
var wg sync.WaitGroup
|
|
for symbol, addr := range s.feeds {
|
|
wg.Add(1)
|
|
go func(sym, a string) {
|
|
defer wg.Done()
|
|
s.fetch(ctx, sym, a)
|
|
}(symbol, addr)
|
|
}
|
|
wg.Wait()
|
|
}
|
|
|
|
func (s *ChainlinkSource) fetch(ctx context.Context, symbol, addr string) {
|
|
// In production: call latestRoundData() on the aggregator contract
|
|
// For now, simulate realistic prices
|
|
price := s.simulate(symbol)
|
|
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
s.prices[symbol] = &Data{
|
|
Symbol: symbol,
|
|
Price: price,
|
|
Confidence: 0.99, // Chainlink is highly reliable
|
|
Timestamp: time.Now(),
|
|
Source: "chainlink",
|
|
}
|
|
s.last[symbol] = time.Now()
|
|
s.failures = 0
|
|
s.healthy = true
|
|
}
|
|
|
|
func (s *ChainlinkSource) simulate(symbol string) float64 {
|
|
base := map[string]float64{
|
|
"BTC-USD": 50000.0,
|
|
"ETH-USD": 3000.0,
|
|
"SOL-USD": 100.0,
|
|
"AVAX-USD": 35.0,
|
|
"LUX-USD": 10.0,
|
|
}
|
|
|
|
b, ok := base[symbol]
|
|
if !ok {
|
|
return 0
|
|
}
|
|
|
|
// Small variation
|
|
v := (math.Sin(float64(time.Now().UnixNano())/1e9) * 0.001) + 1.0
|
|
return b * v
|
|
}
|
|
|
|
// Price returns the latest price for a symbol.
|
|
func (s *ChainlinkSource) Price(symbol string) (*Data, error) {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
|
|
p, ok := s.prices[symbol]
|
|
if !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
|
|
if time.Since(s.last[symbol]) > 60*time.Second {
|
|
copy := *p
|
|
copy.Stale = true
|
|
return ©, nil
|
|
}
|
|
|
|
copy := *p
|
|
return ©, nil
|
|
}
|
|
|
|
// Prices returns prices for multiple symbols.
|
|
func (s *ChainlinkSource) Prices(symbols []string) (map[string]*Data, error) {
|
|
result := make(map[string]*Data)
|
|
for _, sym := range symbols {
|
|
if p, err := s.Price(sym); err == nil {
|
|
result[sym] = p
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// Subscribe is a no-op (uses polling).
|
|
func (s *ChainlinkSource) Subscribe(symbol string) error { return nil }
|
|
|
|
// Unsubscribe is a no-op.
|
|
func (s *ChainlinkSource) Unsubscribe(symbol string) error { return nil }
|
|
|
|
// Healthy returns health status.
|
|
func (s *ChainlinkSource) Healthy() bool {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
return s.healthy && s.failures < 3
|
|
}
|
|
|
|
// Name returns "chainlink".
|
|
func (s *ChainlinkSource) Name() string { return "chainlink" }
|
|
|
|
// Weight returns 1.0 (external oracle - reference only).
|
|
func (s *ChainlinkSource) Weight() float64 { return 1.0 }
|
|
|
|
// Close stops the source.
|
|
func (s *ChainlinkSource) Close() error {
|
|
s.stop()
|
|
s.mu.Lock()
|
|
s.polling = false
|
|
s.mu.Unlock()
|
|
return nil
|
|
}
|
|
|
|
// ChainlinkRound represents a price feed response.
|
|
type ChainlinkRound struct {
|
|
RoundID *big.Int
|
|
Answer *big.Int
|
|
StartedAt *big.Int
|
|
UpdatedAt *big.Int
|
|
Decimals uint8
|
|
}
|