【p2p、分布式,区块链笔记 DAM】GunDB特殊符号*、+、#、_、>、< 的作用

  • 在 GunDB 中,特殊符号用于表示数据结构内部状态和认证访问控制的标识符。

节点信息

_:表示节点的元数据(Metadata)

_ 对象包含了该节点的元数据,描述了与该节点相关的状态和引用信息。这部分内容对于 GunDB 的数据同步、历史版本控制以及并发处理至关重要。

例如,输出中的 _ 部分:

{
  '#': 'root',
  '>': {
    children: 1731823304497.004,
    id: 1731823304496,
    name: 1731823304496
  }
}

#:节点 ID

这个符号在 GunDB 中指代节点的唯一 ID,通常用于标识和引用一个节点。

例如,输出中的:

{
  '#': 'root'
}
  • #:这是该节点的 ID,用于标识节点。它表示的是当前节点(例如这里的 'root')的唯一标识符。在 GunDB 中,每个节点都有一个唯一的 ID(如果没有指定,GunDB 会自动生成一个)。
  • Deprecated! It was used to return data’s gun ID. Use data._[“#”] instead. NOTE! It works only if received data is an object, not a primitive value.
    • 获取节点 ID:应该使用 data._[“#”] 来访问 GunDB 节点的 ID。

    • 只适用于对象:这个方法 仅适用于对象类型的数据,而不适用于基本数据类型(如字符串、数字、布尔值等)。因为基本数据类型没有 _ 这个元数据属性,只有节点对象才会包含这个字段。

>:时间戳与版本控制

GunDB 内部通过版本控制来跟踪每个节点和属性的修改历史。这个 > 符号用来标记节点或属性的变化或版本。在 GunDB 中,如果你更新了一个节点,系统会生成一个新的时间戳,用来表示新版本的数据。

例如:

{
  '>': {
    children: 1731823304497.004,
    id: 1731823304496,
    name: 1731823304496
  }
}

这里 > 表示该节点的属性 childrenidname 对应的时间戳或版本号。GunDB 使用时间戳来确定数据的更新顺序,并确保多节点并发时的同步问题能够得到解决。

用于SEA.认证时的访问控制

  • 访问控制配置:
{"*": "notifications", "+": "*"} // Path must start with "notifications", then Path or Key must contain Certificant's Pub (it's just (path||key).indexOf(pub)!=-1)

{"#": {"*": "inbox"}} // Path must start with "inbox". "get('inbox').get('Alice').get('secret').put('abc', null, cert)" and "get('inbox').get('Bob').get('sensitive').put('something', null, cert)" ARE ALL OK.

{"#": {"*": "project"}, ".": {"*": "Bob"}, {"+": "*"}} // Path must start with "project" and Key must start with "Bob", then Path or Key must contain Certificant's Pub.

"inbox/Bob" // Path must equal "inbox/Bob", it is a LEX exact match {"=":"inbox/Bob"}

["inbox", {"*":"projects", "+": "*"}, {"*":"employees"}] // an Array of rules. If any matches, continue.
  • 应用示例:
var Alice = await SEA.pair()
var Bob = await SEA.pair()
var Dave = await SEA.pair()

// Alice wants to allow Bob and Dave to use write to her "inbox" and "stories" UNTIL TOMORROW
// On Alice's side:
var certificate = await SEA.certify([Bob.pub, Dave.pub], [{"*": "inbox", "+": "*"}, {"*": "stories"}], Alice, null, {expiry: Gun.state()+(60*60*24*1000)})

// Now on Bob/Dave's side, they can write to Alice's graph using gun.put:
gun.get('~'+Alice.pub).get('inbox').get('deeper'+Bob.pub).put('hello world', null, {opt: {cert: certificate}}) // {opt: {cert: certificate}} is how you use Certificate in gun.put
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值