-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_simple_commit.mjs
More file actions
56 lines (43 loc) · 1.55 KB
/
Copy pathtest_simple_commit.mjs
File metadata and controls
56 lines (43 loc) · 1.55 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
import DHT from './index.js'
import crypto from 'crypto'
console.log('🔍 Testing with simple { commit: true }...')
const node = new DHT({ ephemeral: true, bootstrap: ['localhost:10001'] })
await node.fullyBootstrapped()
const val = Buffer.from('Testing simple commit')
const target = crypto.createHash('sha256').update(val).digest()
console.log('📝 Trying with { commit: true }...')
try {
const q = node.query(
{ target, command: 0, value: val },
{ commit: true } // Use simple commit instead of custom function
)
await q.finished()
console.log('✅ SUCCESS! Inserted with simple commit')
console.log('🔑 Hash:', target.toString('hex'))
} catch (err) {
console.log('❌ FAILED with simple commit:', err.message)
}
console.log('📝 Now trying with original commit function...')
function sha256(val) {
return crypto.createHash('sha256').update(val).digest()
}
async function commit(reply) {
console.log('🔐 Custom commit called for:', reply.from.host + ':' + reply.from.port)
await node.request(
{ token: reply.token, target, command: 0, value: val },
reply.from
)
console.log('✅ Custom commit successful for this node')
}
try {
const q2 = node.query(
{ target, command: 0, value: val },
{ commit } // Use custom commit function like insert.mjs
)
await q2.finished()
console.log('✅ SUCCESS! Inserted with custom commit function')
} catch (err) {
console.log('❌ FAILED with custom commit function:', err.message)
console.log('🔍 This might be the issue with insert.mjs')
}
process.exit(0)