p2p、分布式,区块链条记:基于IPFS实现的数据库orbitdb条记 ...

打印 上一主题 下一主题

主题 1784|帖子 1784|积分 5352

orbitdb



  • orbitdb :Peer-to-Peer Databases for the Decentralized Web
特性阐明特点无服务器、分布式、p2p编程语言JavaScript对其他语言的支持A python client for the Orbitdb HTTP API,go-orbit-db, 让我们了解一下谁在使用 js-ipfs!是否为区块链不是区块链。使用强最终同等性模型,而非强同等性模型。许可MIT 软件许可证支持的数据库范例事件、文件、键/值、索引键/值底层分布式数据结构无辩论复制数据范例 (CRDT)。IPFS pubsub协议主动使各对等方的数据库保持最新。https://www.zxch3n.com/crdt-intro/crdt-intro/ 安装

node下令



  • 安装node
  1. $ npm init --yes # use npm defaults, you can edit this later
  2. $ npm install @orbitdb/core helia
  3. # 这是老版本的安装方式 $ npm install --save orbit-db ipfs     # --save 选项的作用是将安装的包添加到 package.json 文件的 dependencies 部分。在较新的 npm 版本中(从 npm 5.0.0 开始),--save 选项是默认的
  4. # Helia 是一种精简、模块化和现代的 IPFS TypeScript 实现,适用于多产的 JS 和浏览器环境。https://helia.io/
复制代码
cdn方式安装



  • OrbitDB 可以使用带有 tag 的分布式 js 文件在欣赏器中加载<script>/path/to/orbitdb.min.js</script>
  1. <script src="https://unpkg.com/ipfs@0.55.1/dist/index.min.js"></script>
  2. <script src="https://unpkg.com/orbit-db@0.26.1/dist/orbitdb.min.js"></script>
复制代码
创建数据库示例代码

安装所需的依赖

  1. npm install helia @orbitdb/core @chainsafe/libp2p-gossipsub @libp2p/identify libp2p
复制代码
确保 Node.js 支持 ES 模块



  • import 是 ES6(ECMAScript 2015)引入的模块导入语法,用于从一个模块中引入特定的功能、对象或变量。
  • type: "module" 是在 package.json 文件或 HTML 文件中指定 JavaScript 模块的方式。它告诉 JavaScript 引擎这个文件或所有相关文件应该被看成 ES6 模块来处理。
  • 在 Node.js 项目中,可在 package.json 文件中指定 "type": "module",如许就可以在项目中使用 ES6 模块语法(即 import 和 export,import 和 export 是 ES6 模块体系的焦点,实现在不同的 JavaScript 文件中共享代码。)。
  • 打开 package.json 文件,添加 "type": "module":
  1. {
  2.   "name": "my-project",
  3.   "version": "1.0.0",
  4.   "description": "",
  5.   "main": "index.js",
  6.   "type": "module",
  7.   "scripts": {
  8.     "test": "echo "Error: no test specified" && exit 1"
  9.   },
  10.   "author": "",
  11.   "license": "ISC",
  12.   "dependencies": {
  13.     "helia": "^0.1.0",
  14.     "@orbitdb/core": "^0.1.0",
  15.     "@chainsafe/libp2p-gossipsub": "^0.1.0",
  16.     "@libp2p/identify": "^0.1.0",
  17.     "libp2p": "^0.1.0"
  18.   }
  19. }
复制代码
创建 JavaScript 文件 index.js。

  1. // index.js
  2. import { createHelia } from 'helia';
  3. import { createOrbitDB } from '@orbitdb/core';
  4. import { gossipsub } from '@chainsafe/libp2p-gossipsub';
  5. import { identify } from '@libp2p/identify';
  6. import { createLibp2p } from 'libp2p';
  7. const Libp2pOptions = {
  8.   services: {
  9.     pubsub: gossipsub({
  10.       // necessary to run a single peer
  11.       allowPublishToZeroTopicPeers: true
  12.     }),
  13.     identify: identify()
  14.   }
  15. };
  16. (async function () {
  17.   const libp2p = await createLibp2p({ ...Libp2pOptions });
  18.   const ipfs = await createHelia({ libp2p });
  19.   const orbitdb = await createOrbitDB({ ipfs });
  20.   // Create / Open a database. Defaults to db type "events".  events类型的数据库定义详见https://github.com/orbitdb/orbitdb/blob/main/src/databases/events.js
  21.   const db = await orbitdb.open('hello');
  22.   const address = db.address;
  23.   console.log(address);
  24.   // "/orbitdb/zdpuAkstgbTVGHQmMi5TC84auhJ8rL5qoaNEtXo2d5PHXs2To"
  25.   // The above address can be used on another peer to open the same database
  26.   // Listen for updates from peers,任何更新都会触发此监听函数,再运行“Add an entry”部分时会触发
  27.   db.events.on('update', async entry => {
  28.     console.log(entry);
  29.     const all = await db.all();
  30.     console.log(all);
  31.   });
  32.   // Add an entry
  33.   const hash = await db.add('world');
  34.   console.log(hash);
  35.   // Query
  36.   for await (const record of db.iterator()) {
  37.     console.log(record);
  38.   }
  39.   await db.close();
  40.   await orbitdb.stop();
  41.   await ipfs.stop();
  42. })();
复制代码
自界说events范例的数据库



  • 数据库范例 Events,它继承了 Database 并实现了 OrbitDB 数据库接口。下面是功能总结:

    • add: 将一个事件添加到数据库中,并返回该事件的哈希值。
    • get: 根据给定的哈希值从数据库中获取事件的值。
    • iterator: 异步生成器函数,用于迭代事件。支持多种过滤条件(如哈希值范围和结果数量)。
    • all: 返回所有事件的数组,事件以哈希/值对的形式存储(通过iterator实现)。

运行代码:在终端中运行node index.js

运行结果


  1. PS C:\Users\kingchuxing\Documents\IPFS\orbitdb_enent> node index.js
  2. ## console.log(address);的输出
  3. /orbitdb/zdpuB3GgA87irrdJDyvrNvwDNTwQLRxXNWoLrioDEjNeiYvC4
  4. ## Listen for updates from peers 部分的输出
  5. {
  6.   id: '/orbitdb/zdpuB3GgA87irrdJDyvrNvwDNTwQLRxXNWoLrioDEjNeiYvC4',
  7.   payload: { op: 'ADD', key: null, value: 'world' },
  8.   next: [],
  9.   refs: [],
  10.   clock: {
  11.     id: '021c0174efab4162111ef8c77df934dbdb767cc96449fad014b46cdd9033a53ea1',
  12.     time: 1
  13.   },
  14.   v: 2,
  15.   key: '021c0174efab4162111ef8c77df934dbdb767cc96449fad014b46cdd9033a53ea1',
  16.   identity: 'zdpuApdyQ1Nfunra16qqtALnLb8KkPGxWeoMDZyXrKQij1QtT',
  17.   sig: '3045022100a5166de55326728dd33fccc770fcc117324f02a62e41d4217050af9ecba895ec02202927783b54c79146809351b58d3dba4d9c99fcfc294f1a261cbe3607435d41f9',
  18.   hash: 'zdpuAkydAnMRxSiyqHaXJgngdpXQK1HYWWNqRiqMtU273GZSe',
  19.   bytes: Uint8Array(476) [
  20.     169,  97, 118,   2,  98, 105, 100, 120,  58,  47, 111, 114,
  21.      98, 105, 116, 100,  98,  47, 122, 100, 112, 117,  66,  51,
  22.      71, 103,  65,  56,  55, 105, 114, 114, 100,  74,  68, 121,
  23.     118, 114,  78, 118, 119,  68,  78,  84, 119,  81,  76,  82,
  24.     120,  88,  78,  87, 111,  76, 114, 105, 111,  68,  69, 106,
  25.      78, 101, 105,  89, 118,  67,  52,  99, 107, 101, 121, 120,
  26.      66,  48,  50,  49,  99,  48,  49,  55,  52, 101, 102,  97,
  27.      98,  52,  49,  54,  50,  49,  49,  49, 101, 102,  56,  99,
  28.      55,  55, 100, 102,
  29.     ... 376 more items
  30.   ]
  31. }
  32. ## console.log(hash); 的输出
  33. zdpuAkydAnMRxSiyqHaXJgngdpXQK1HYWWNqRiqMtU273GZSe
  34. [
  35.   {
  36.     hash: 'zdpuAkydAnMRxSiyqHaXJgngdpXQK1HYWWNqRiqMtU273GZSe',
  37.     value: 'world'
  38.   }
  39. ]
  40. ## console.log(record) 的输出
  41. {
  42.   hash: 'zdpuAkydAnMRxSiyqHaXJgngdpXQK1HYWWNqRiqMtU273GZSe',
  43.   value: 'world'
  44. }
  45. PS C:\Users\kingchuxing\Documents\IPFS\orbitdb_enent>
复制代码
CG



  • 在 HTML 文件中,可通过 type="module" 属性来指定一个 <script> 标签内的 JavaScript 代码是ES模块代码。
  1. <script type="module">
  2.   import { myFunction } from './myModule.js';
  3.   myFunction();
  4. </script>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

惊落一身雪

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表