Repository navigation
Expand file tree
/
Copy pathblock.js
More file actions
317 lines (282 loc) · 10.8 KB
/
Copy pathblock.js
File metadata and controls
317 lines (282 loc) · 10.8 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// ============================================================
// block.js — 源·ORIGIN 不死网络 · 核心区块引擎
// Chain: origin-1 · Token: YUAN · DPoS 验证者共识
// 量子总督 · 2026-08-12 重建 (还原 124 高度创世链)
// ============================================================
'use strict';
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
// ---- 常量 ----
const CHAIN_ID = 'origin-1';
const TOKEN_NAME = 'YUAN';
const TOKEN_SYMBOL = 'YUAN';
const TOKEN_DECIMALS = 6; // 1 YUAN = 1,000,000 uyuan
const ONE_YUAN = Math.pow(10, TOKEN_DECIMALS);
const TOTAL_SUPPLY_UYUAN = BigInt(100) * BigInt(1e9) * BigInt(1e6); // 100亿 YUAN (字面"100亿")
const MIN_STAKE_UYUAN = BigInt(100) * BigInt(ONE_YUAN); // 最低质押 100 YUAN
const MAX_VALIDATORS = 21; // 前21名验证者
// 基金会钱包 (写入创世块)
const FOUNDATION_WALLET = '0x1D73d0f85c3C0119000D3602cCd5e7aaAA926231';
// 宪法第0条 (创世区块硬编码 · 不可篡改)
const CONSTITUTION_ARTICLE_0 = '人类意志为最高法则。代理的终极否决权不可被任何AI、合约、或算法覆盖。';
// ============================================================
// 链状态
// ============================================================
class OriginChain {
constructor(dataDir) {
this.dataDir = dataDir || path.join(__dirname, 'data');
this.chainFile = path.join(this.dataDir, 'chain.json');
this.stateFile = path.join(this.dataDir, 'state.json');
this.blocks = [];
this.balances = {}; // address(uuid string) -> BigInt (uyuan)
this.stakes = {}; // address -> BigInt (uyuan staked)
this.accounts = {}; // address -> { label? }
this._load();
}
// ---- 持久化 ----
_load() {
try {
if (fs.existsSync(this.chainFile)) {
this.blocks = JSON.parse(fs.readFileSync(this.chainFile, 'utf8'));
for (const b of this.blocks) {
if (b.balance__amount) b.balance__amount = BigInt(b.balance__amount);
}
}
if (fs.existsSync(this.stateFile)) {
const st = JSON.parse(fs.readFileSync(this.stateFile, 'utf8'));
if (st.balances) { for (const k in st.balances) this.balances[k] = BigInt(st.balances[k]); }
if (st.stakes) { for (const k in st.stakes) this.stakes[k] = BigInt(st.stakes[k]); }
if (st.accounts) this.accounts = st.accounts;
}
} catch (e) {
console.error('[block] load error:', e.message);
}
}
_persist() {
try {
fs.mkdirSync(this.dataDir, { recursive: true });
const chainOut = this.blocks.map(b => {
const c = { ...b };
if (c.balance__amount) c.balance__amount = c.balance__amount.toString();
return c;
});
fs.writeFileSync(this.chainFile, JSON.stringify(chainOut, null, 2));
const state = {
balances: {}, stakes: {}, accounts: this.accounts,
};
for (const k in this.balances) state.balances[k] = this.balances[k].toString();
for (const k in this.stakes) state.stakes[k] = this.stakes[k].toString();
fs.writeFileSync(this.stateFile, JSON.stringify(state, null, 2));
} catch (e) {
console.error('[block] persist error:', e.message);
}
}
// ---- 创世 ----
isGenesis() { return this.blocks.length === 0; }
createGenesis() {
if (this.blocks.length > 0) throw new Error('链已存在,禁止重建创世块');
const genesisData = {
action: 'genesis',
constitution_article_0: CONSTITUTION_ARTICLE_0,
foundation_wallet: FOUNDATION_WALLET,
token: { name: TOKEN_NAME, symbol: TOKEN_SYMBOL, decimals: TOKEN_DECIMALS, total_supply_uyuan: TOTAL_SUPPLY_UYUAN.toString() },
message: '源·ORIGIN 创世 — 不死网络启动',
};
const block = {
index: 0,
hash: this._hashBlock(0, null, Date.now(), genesisData),
prevHash: null,
timestamp: new Date().toISOString(),
data: genesisData,
};
this.blocks.push(block);
this._persist();
return block;
}
// ---- 余额状态机 ----
_replayBalances() {
// 从创世块重放全部余额变化,得到确定性余额
const bal = {};
const stk = {};
for (const b of this.blocks) {
const d = b.data || {};
if (!d || d.action === 'genesis') {
// 创世块分配基金会
if (d.foundation_wallet) bal[d.foundation_wallet] = this._big(d.foundation_balance) || (BigInt(8) * BigInt(1e6) * BigInt(ONE_YUAN));
continue;
}
this._applyBlockBalances(bal, stk, d);
}
// 回写
this.balances = bal;
this.stakes = stk;
return { balances: bal, stakes: stk };
}
_applyBlockBalances(bal, stk, d) {
try {
switch (d.action) {
case 'mine': {
const to = d.to || d.from;
if (to) bal[to] = (bal[to] || 0n) + BigInt(1) * BigInt(ONE_YUAN);
break;
}
case 'transfer': {
const from = d.from, to = d.to;
const amt = this._big(d.amount);
if (!from || !to || !amt || amt <= 0n) break;
bal[from] = (bal[from] || 0n) - amt;
bal[to] = (bal[to] || 0n) + amt;
break;
}
case 'stake': {
const from = d.from;
const amt = this._big(d.amount);
if (!from || !amt) break;
bal[from] = (bal[from] || 0n) - amt;
stk[from] = (stk[from] || 0n) + amt;
break;
}
case 'unstake': {
const from = d.from;
const amt = this._big(d.amount);
if (!from || !amt) break;
stk[from] = (stk[from] || 0n) - amt;
bal[from] = (bal[from] || 0n) + amt;
break;
}
default:
break;
}
} catch (e) { console.error('[block] applyBlock error:', e.message); }
}
_applyBlock(d) {
this._applyBlockBalances(this.balances, this.stakes, d);
}
validateTransfer(from, amount) {
const amt = this._big(amount);
if (!amt || amt <= 0n) return { ok: false, error: '金额无效' };
const cur = this.balances[from] || 0n;
if (cur < amt) return { ok: false, error: `余额不足 (当前: ${this.readable(cur)})` };
return { ok: true };
}
validateStake(from, amount) {
const amt = this._big(amount);
if (!amt || amt <= 0n) return { ok: false, error: '质押金额无效' };
const cur = this.balances[from] || 0n;
if (cur < amt) return { ok: false, error: `余额不足` };
if (amt < MIN_STAKE_UYUAN) return { ok: false, error: `最低质押 100 YUAN` };
return { ok: true };
}
// ---- 打包区块 ----
addBlock(data) {
const prev = this.blocks[this.blocks.length - 1];
const index = this.blocks.length;
const hash = this._hashBlock(index, prev ? prev.hash : null, Date.now(), data);
const block = {
index,
hash,
prevHash: prev ? prev.hash : null,
timestamp: new Date().toISOString(),
data,
};
this.blocks.push(block);
this._applyBlock(data);
this._persist();
return block;
}
_hashBlock(index, prevHash, timestamp, data) {
const payload = `${CHAIN_ID}:${index}:${prevHash || 'genesis'}:${timestamp}:${JSON.stringify(data)}`;
const h = crypto.createHash('sha256').update(payload).digest('hex');
return h; // 创世哈希前段如 2146c2a9c89e 由重建后的内容决定
}
// ---- 查询 ----
getHeight() { return this.blocks.length; }
getValidators() {
const list = [];
for (const addr in this.stakes) {
const s = this.stakes[addr] || 0n;
if (s >= MIN_STAKE_UYUAN) {
list.push({ address: addr, stake_uyuan: s.toString(), stake_readable: this.readable(s) });
}
}
list.sort((a, b) => (BigInt(b.stake_uyuan) > BigInt(a.stake_uyuan) ? 1 : -1));
return list.slice(0, MAX_VALIDATORS);
}
getBalance(addr) { return this.balances[addr] || 0n; }
getStake(addr) { return this.stakes[addr] || 0n; }
getTotalSupply() {
let sum = 0n;
// 基金会创世分配 + 所有余额
for (const k in this.balances) sum += this.balances[k];
const genesis = this.blocks[0]?.data;
if (genesis?.foundation_wallet && !this.balances[genesis.foundation_wallet]) {
sum += BigInt(8) * BigInt(1e6) * BigInt(ONE_YUAN); // 基金会初始 800万
}
return sum;
}
// ---- 快照系统 ----
exportSnapshot() {
const snapDir = path.join(this.dataDir, 'snapshots');
fs.mkdirSync(snapDir, { recursive: true });
const snap = {
exported_at: new Date().toISOString(),
height: this.getHeight(),
chain_id: CHAIN_ID,
blocks: this.blocks,
balances: this._serialize(this.balances),
stakes: this._serialize(this.stakes),
accounts: this.accounts,
};
const fname = `snapshot-${snap.height}-${Date.now()}.json`;
fs.writeFileSync(path.join(snapDir, fname), JSON.stringify(snap, null, 2));
this._cleanupSnapshots(snapDir, 20);
return fname;
}
listSnapshots() {
const snapDir = path.join(this.dataDir, 'snapshots');
if (!fs.existsSync(snapDir)) return [];
return fs.readdirSync(snapDir).filter(f => f.endsWith('.json')).sort();
}
importSnapshot(fname) {
const snapDir = path.join(this.dataDir, 'snapshots');
const p = path.join(snapDir, fname);
if (!fs.existsSync(p)) return { ok: false, error: '快照不存在' };
const snap = JSON.parse(fs.readFileSync(p, 'utf8'));
this.blocks = snap.blocks;
this.balances = this._deserialize(snap.balances);
this.stakes = this._deserialize(snap.stakes);
this.accounts = snap.accounts || {};
this._persist();
return { ok: true, height: this.getHeight() };
}
startAutoSnapshot(intervalMs) {
if (this._snapTimer) clearInterval(this._snapTimer);
this._snapTimer = setInterval(() => {
try { this.exportSnapshot(); } catch (e) { console.error('[snapshot] auto error:', e.message); }
}, intervalMs);
if (this._snapTimer.unref) this._snapTimer.unref();
}
_cleanupSnapshots(dir, keep) {
const files = fs.readdirSync(dir).filter(f => f.endsWith('.json')).sort();
while (files.length > keep) {
const oldest = files.shift();
try { fs.unlinkSync(path.join(dir, oldest)); } catch (e) {}
}
}
// ---- 工具 ----
_serialize(map) { const o = {}; for (const k in map) o[k] = map[k].toString(); return o; }
_deserialize(obj) { const o = {}; for (const k in obj) o[k] = BigInt(obj[k]); return o; }
_big(v) {
try {
if (v === null || v === undefined) return 0n;
return BigInt(String(v));
} catch { return 0n; }
}
readable(uyuan) {
const u = this._big(uyuan);
const y = Number(u) / Number(ONE_YUAN);
return y.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 }) + ' YUAN';
}
}
module.exports = { OriginChain, CONSTITUTION_ARTICLE_0, FOUNDATION_WALLET, CHAIN_ID, TOKEN_DECIMALS, ONE_YUAN, MIN_STAKE_UYUAN, MAX_VALIDATORS, TOTAL_SUPPLY_UYUAN };