Last active
January 9, 2022 16:41
-
-
Save Nuhvi/b4ed3b2e346741df0ec8728b2d2613e6 to your computer and use it in GitHub Desktop.
Duplicitous hypercores
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Hypercore = require('hypercore'); | |
const ram = require('random-access-memory'); | |
const crypto = require('hypercore-crypto'); | |
(async () => { | |
const keyPair = crypto.keyPair(); | |
const a = new Hypercore(ram, { keyPair }); | |
const b = new Hypercore(ram, a.key, { keyPair: keyPair }); | |
await a.append(['a1', 'a2']); | |
await b.append(['a1', 'b2', 'b3']); | |
const c = new Hypercore(ram, a.key); | |
await c.ready(); | |
console.log(a.key === b.key && a.key === c.key); // true | |
console.log((await a.get(1)).toString()); // a2 | |
console.log((await b.get(1)).toString()); // b2 | |
const replicate = (x, y) => { | |
const s1 = x.replicate(true, { keepAlive: false }); | |
const s2 = y.replicate(false, { keepAlive: false }); | |
s1.pipe(s2).pipe(s1); | |
}; | |
replicate(a, c); | |
replicate(b, c); | |
const cLast = (await c.get(1)).toString(); | |
// {cLast: 'a2'} unless replicate(a,c) is commented out | |
console.log({ cLast }); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment