-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockchain-database-memory.js
More file actions
153 lines (128 loc) · 4.29 KB
/
Copy pathblockchain-database-memory.js
File metadata and controls
153 lines (128 loc) · 4.29 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
/**
* In-Memory Blockchain Database
* Fallback when Firestore is not available
* WARNING: Data is lost on server restart!
*/
class BlockchainDatabaseMemory {
constructor() {
this.blocks = [];
this.transactions = new Map();
this.pendingTransactions = [];
this.smartContracts = [];
this.minerBlockHistory = [];
this.miningRegistrations = new Map();
this.initialized = false;
}
async initialize() {
console.log('⚠️ Using IN-MEMORY database - data will be LOST on restart!');
this.initialized = true;
return true;
}
async saveBlock(block) {
const existing = this.blocks.findIndex(b => b.index === block.index);
if (existing >= 0) {
this.blocks[existing] = block;
} else {
this.blocks.push(block);
}
return block;
}
async getAllBlocks(skipTransactions = false) {
return this.blocks.sort((a, b) => a.index - b.index);
}
async getTransaction(idOrHash) {
if (!idOrHash) return null;
const q = String(idOrHash).trim().toLowerCase();
// Scan memory blocks for matching transaction
for (const block of this.blocks) {
const found = (block.transactions || []).find(tx => {
const txId = (tx.id || '').toLowerCase();
const txHash = (tx.hash || '').toLowerCase();
const docHash = (tx.data?.hash || '').toLowerCase();
const ethHash = (tx.data?.eth_hash || '').toLowerCase();
const dataTxHash = (tx.data?.txHash || '').toLowerCase();
return txId === q || txHash === q || docHash === q || ethHash === q || dataTxHash === q;
});
if (found) return found;
}
return null;
}
async getBlock(index) {
return this.blocks.find(b => b.index === index);
}
async deleteBlock(index) {
this.blocks = this.blocks.filter(b => b.index !== index);
return true;
}
async saveTransaction(tx, blockIndex = null) {
const key = tx.id || `tx-${Date.now()}-${Math.random()}`;
this.transactions.set(key, { ...tx, blockIndex });
return tx;
}
async getTransactionsByBlock(blockIndex) {
const txs = [];
this.transactions.forEach((tx, key) => {
if (tx.blockIndex === blockIndex) {
txs.push(tx);
}
});
return txs;
}
async getTransactionHistory(address) {
const txs = [];
this.transactions.forEach((tx) => {
if (tx.from === address || tx.to === address) {
txs.push(tx);
}
});
return txs;
}
async getPendingTransactions() {
return this.pendingTransactions;
}
async clearPendingTransactions() {
this.pendingTransactions = [];
return true;
}
async saveSmartContract(contract) {
this.smartContracts.push(contract);
return contract;
}
async getAllSmartContracts() {
return this.smartContracts;
}
async getMinerBlockHistory() {
return this.minerBlockHistory;
}
async saveMinerBlockHistory(record) {
this.minerBlockHistory.push(record);
return record;
}
// ==================== MINING REGISTRATION OPERATIONS ====================
async saveMiningRegistration(registration) {
const key = registration.walletAddress.toLowerCase();
this.miningRegistrations.set(key, registration);
return registration;
}
async getMiningRegistration(walletAddress, deviceId = null) {
const key = walletAddress.toLowerCase();
let registration = this.miningRegistrations.get(key);
if (!registration && deviceId) {
// Check by device ID
for (const reg of this.miningRegistrations.values()) {
if (reg.deviceId === deviceId) {
registration = reg;
break;
}
}
}
return registration;
}
async getAllMiningRegistrations() {
return Array.from(this.miningRegistrations.values());
}
async close() {
console.log('🔒 In-memory database closed');
}
}
module.exports = BlockchainDatabaseMemory;