File size: 4,839 Bytes
37f9067
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# CHAINSTATE Quick Start Guide

Get up and running with CHAINSTATE in 5 minutes.

## Prerequisites

- Python 3.11+
- Node.js 18+
- Git
- 8GB+ RAM
- (Optional) CUDA-capable GPU

## Installation

### 1. Clone Repository

```bash
git clone https://github.com/CPater/chainstate.git
cd chainstate
```

### 2. Install Python Dependencies

```bash
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
pip install -r requirements.txt
```

### 3. Install Node Dependencies

```bash
npm install
```

### 4. Configure Environment

```bash
cp .env.example .env
# Edit .env with your API keys
```

## Running Locally

### Start the API Server

```bash
python -m src.api.server
```

Server will start on `http://localhost:7860`

### Run Tests

```bash
pytest tests/ -v
```

### Launch HF Space UI

```bash
python -m http.server 7860
# Open http://localhost:7860/chainstate.html
```

## Deploying to Cloudflare Workers

### 1. Authenticate

```bash
npx wrangler login
```

### 2. Create KV Namespaces

```bash
npx wrangler kv:namespace create CHAINSTATE_CACHE
npx wrangler kv:namespace create CHAINSTATE_NODES
npx wrangler kv:namespace create CHAINSTATE_CONSENSUS
```

### 3. Update wrangler.toml

Replace the namespace IDs in `wrangler.toml` with your created namespaces.

### 4. Deploy

```bash
npm run deploy
```

## Deploying to Hugging Face Spaces

### 1. Create Space

Go to https://huggingface.co/spaces/CPater/chainstate

### 2. Upload Files

```bash
git clone https://huggingface.co/spaces/CPater/chainstate
cp chainstate.html chainstate/
cd chainstate
git add .
git commit -m "Initial CHAINSTATE deployment"
git push
```

## First Query

### Using curl

```bash
curl -X POST https://your-worker.your-subdomain.workers.dev/query \
  -H "Content-Type: application/json" \
  -d '{
    "query": "∫∂x → ?",
    "consensusDepth": 3,
    "swarmSize": 50
  }'
```

### Using Python

```python
import requests

response = requests.post(
    "https://your-worker.your-subdomain.workers.dev/query",
    json={
        "query": "☉☽☿ in alchemy",
        "consensusDepth": 3,
        "swarmSize": 50,
        "quantumOffload": "ibm"
    }
)

print(response.json())
```

### Using JavaScript

```javascript
const response = await fetch('https://your-worker.your-subdomain.workers.dev/query', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    query: '🧬🔬⚗ → ?',
    consensusDepth: 3,
    swarmSize: 50
  })
});

const result = await response.json();
console.log(result);
```

## Running a Swarm Node

### 1. Register Node

```python
from chainstate.swarm import SwarmNode
from chainstate.symbolic import UniversalSemioticEmbedding

# Initialize
embedding = UniversalSemioticEmbedding()
node = SwarmNode(
    node_id="my-node-001",
    embedding_model=embedding,
    inference_fn=my_inference_function,
    endpoint="https://my-node.example.com"
)

# Register with beacon
await node.register("https://chainstate-beacon.workers.dev")
```

### 2. Start Processing

```python
await node.start_listening()
```

## Development

### Project Structure

```
chainstate/
├── src/
│   ├── symbolic/          # Universal Semiotic Embedding
│   ├── consensus/         # Proof-of-Cognitive-Work
│   ├── chain/             # Blockchain logic
│   ├── quantum/           # Quantum integration
│   └── edge/              # Edge computing
├── workers/               # Cloudflare Workers
├── contracts/             # Smart contracts
├── tests/                 # Test suite
└── docs/                  # Documentation
```

### Running Tests

```bash
# Unit tests
pytest tests/unit/ -v

# Integration tests
pytest tests/integration/ -v

# With coverage
pytest --cov=src --cov-report=html
```

### Code Style

```bash
# Format code
black src/
isort src/

# Type checking
mypy src/

# Linting
flake8 src/
```

## Troubleshooting

### Issue: Out of memory

**Solution:** Reduce batch size or use CPU instead of GPU

```python
# In .env
DEVICE=cpu
BATCH_SIZE=16
```

### Issue: Worker deployment fails

**Solution:** Check wrangler.toml configuration

```bash
npx wrangler config list
npx wrangler whoami
```

### Issue: Query timeout

**Solution:** Increase timeout or reduce swarm size

```python
response = requests.post(
    url,
    json={"swarmSize": 10},  # Reduce from 50
    timeout=60  # Increase from default 30
)
```

## Next Steps

- Read the [Whitepaper](docs/whitepaper.md)
- Explore [API Documentation](docs/api.md)
- Join the [Discord](https://discord.gg/chainstate)
- Contribute on [GitHub](https://github.com/CPater/chainstate)

## Support

- GitHub Issues: https://github.com/CPater/chainstate/issues
- Email: chainstate@nwo.capital
- Telegram: @chainstate_dev

---

**Happy building!** 🚀⛓