文章目录
简介
Swarm开发团队已将其精力转移到构建新的Bee客户端上,这是Swarm的全新实现。进行此切换的主要原因是使用更成熟的网络层(libp2p)。以太坊swarm Bee是第二个正式的以太坊swarm实现。不能提供与第一个以太坊Swarm实现的网络层兼容性,这主要是因为底层网络协议从devp2p更改为libp2p。这意味着Bee节点无法加入第一个Swarm网络,反之亦然。
根据源码构建bee
git clone https://github.com/ethersphere/bee
cd bee
git describe --tags
git checkout v0.6.1
make binary
dist/bee version
sudo cp dist/bee /usr/local/bin/bee
启动
需要输入两次key密码(重要,需要记住)
bee start --verbosity 5 --swap-endpoint https://rpc.slock.it/goerli --debug-api-enable
将会输出
WARN[2021-04-21T18:21:02+08:00] cannot continue until there is at least 10 BZZ available on 5763e8fea803caa7b2d6c18709ef36b1b54fc1cf
WARN[2021-04-21T18:21:02+08:00] get your Goerli ETH and Goerli BZZ now via the bzzaar at https://bzz.ethswarm.org/?transaction=buy&amount=10&slippage=30&receiver=0x5763e8fea803caa7b2d6c18709ef36b1b54fc1cf
最后得receiver为我们钱包地址,提示我们没有ETH和GZZ,先CTRL+C终止
从水龙头获取gEth
https://faucet.goerli.mudit.blog/
http://goerli-faucet.slock.it/
https://faucet.ethswarm.org/
从水龙头获取代币,建议使用第一个最高可以获取87.5个gEth,需要从twitter发个包含地址推文
查看bee私钥
需要专门得工具exportSwarmKey
wget https://github.com/ethersphere/exportSwarmKey/releases/download/v0.1.0/export-swarm-key-linux-386
./export-swarm-key-linux-386 /root/.bee/keys/ 你的密码
导入到metamask
导入账户-》私钥-》选择我们得~/.bee/keys/目录下得swarm.key
将gEth转换成gBzz币(已失效)
浏览器打开
https://bzz.ethswarm.org/?transaction=buy&amount=10&slippage=30&receiver=0x5763e8fea803caa7b2d6c18709ef36b1b54fc
,选择Connect to your MetaMask Wallet,选择连接我们刚刚导入账户,输出要转换得gBzz数量,点击左下角得Get G-ETH ,再点击EXCAHNGE,
去以太坊浏览器查看交易情况
https://goerli.etherscan.io/address/0x5763e8fea803caa7b2d6c18709ef36b1b54fc1cf
最新获取gBzz的方法
加入https://discord.com/invite/kfKvmZfVfe
为您的节点钱包接收水龙头测试令牌,请使用新的接收测试令牌指令 /faucet sprinkle command! 🐝
操作方式:为您的钱包地址接收测试令牌,输入正斜杠(直接复制和粘贴指令不起作用),在弹出的命令提示框中选择 /faucet sprinkle addresses
接下来复制粘贴你的节点钱包地址,如下所示:
/faucet sprinkle addresses:0xabeecd123 …
检查您的水龙头使用状态(操作方式均为 输入正斜杠选择相关命令执行)
/faucet pending:返回正在处理接收测试令牌的钱包地址的状态,接水仍在进行中,测试代币稍后会发放到您的钱包地址里。
示例:The following addresses are still pending: 0x36Fb… 0xd82A…(返回已经在接水中的钱包地址)
/faucet allowance:返回最多接收水龙头测试令牌的次数。
示例:You can have 3 pending sprinkles at most(你最多可以接水3次)
/faucet remaining:返回剩余接收水龙头测试牌的次数。
示例:You have 3 sprinkles left.(你有3次剩余接水次数)
再次启动
0.6 必须启用-full-node
swap-endpoint默认值为ws://localhost:8546,如果你没有搭建自己的以太坊节点,原来的https://rpc.slock.it/goerli已经不能使用,可以使用https://infura.io/提供的服务,注册并创建项目即可以获取到url:wss://goerli.infura.io/ws/v3/your-api-key
nohup bee start \
--full-node \
--p2p-quic-enable \
--p2p-ws-enable \
--swap-endpoint wss://goerli.infura.io/ws/v3/your-api-key \
--debug-api-enable \
--verbosity 5 \
--swap-enable \
--api-addr=:1633 \
--debug-api-enable \
--debug-api-addr=:1635 \
--p2p-addr=:1634 \
--resolver-options "https://cloudflare-eth.com" \
--password your-password > bee.log 2>&1 &
基本用法
主要通过http接口发送请求,API请参考
peer数量
curl -s http://localhost:1635/peers | jq '.peers | length'
网络拓扑
curl -X GET http://localhost:1635/topology | jq
上传文件
echo "hello" >test.txt
curl -F file=@test.txt http://localhost:1633/files
{"reference":"303ce5f9864fd18ee5234c40d5a8df8671db22d68ecc1d4788b11b769fd58792"}
下载文件
curl -OJ http://localhost:1633/files/303ce5f9864fd18ee5234c40d5a8df8671db22d68ecc1d4788b11b769fd58792
通过公共网关查看文件
https://gateway.ethswarm.org/files/303ce5f9864fd18ee5234c40d5a8df8671db22d68ecc1d4788b11b769fd58792
加密和上传文件
curl -F file=@test.txt -H "Swarm-Encrypt: true" http://localhost:1633/files
{"reference":"b75426db4bebf679abe0e3989617481ffabb955f9d655b37835b7a616090375197f03d3564edc54e9d848db85aea8cb93aa9472ccaa27c47f2fde298d80b6462"}
返回得128个字符hash,前64就是原来得reference,后64个字符是密钥
下载和解密文件
curl -OJ http://localhost:1633/files/b75426db4bebf679abe0e3989617481ffabb955f9d655b37835b7a616090375197f03d3564edc54e9d848db85aea8cb93aa9472ccaa27c47f2fde298d80b6462
高级用法
本地pin
上传时pin住
curl -H "swarm-pin: true" --data-binary @test.txt localhost:1633/files\?test.txt
{"reference":"32eb13ba65fe78ba98e5d96f65cdb7cfea93d8e3b4ba1c7cc34bc71c8bb15b67"}
取消pin
curl -XDELETE http://localhost:1633/pin/files/32eb13ba65fe78ba98e5d96f65cdb7cfea93d8e3b4ba1c7cc34bc71c8bb15b67
{"message":"OK","code":200}
再次执行
curl -XDELETE http://localhost:1633/pin/files/32eb13ba65fe78ba98e5d96f65cdb7cfea93d8e3b4ba1c7cc34bc71c8bb15b67
{"message":"cannot unpin","code":500}
pin住已经上传的内容
curl -XPOST http://localhost:1633/pin/files/32eb13ba65fe78ba98e5d96f65cdb7cfea93d8e3b4ba1c7cc34bc71c8bb15b67
curl http://localhost:1633/pin/chunks/32eb13ba65fe78ba98e5d96f65cdb7cfea93d8e3b4ba1c7cc34bc71c8bb15b67
全局pin
本地固定可确保您自己的节点不会删除上传的文件。但是其他存储您的数据的节点(因为它们属于其职责范围内)可能已删除了最近尚未访问的内容,以便腾出空间.
启动节点时带上–global-pinning-enable标志
bee start\
--verbosity 5 \
--swap-endpoint https://rpc.slock.it/goerli \
--global-pinning-enable \
--debug-api-enable
和本地pin一样
curl -H "swarm-pin: true" --data-binary @test.txt localhost:1633/files\?test.txt
{"reference":"32eb13ba65fe78ba98e5d96f65cdb7cfea93d8e3b4ba1c7cc34bc71c8bb15b67"}
如果请求一个已经由其存储节点进行垃圾回收的数据(chunk),则存储节点将使用PSS向该前缀所定义的Swarm邻域节点发送消息
先找出我们的目标前缀
curl -s http://localhost:1635/addresses | jq .overlay
"aeae412998fef1d9c8b8e93df53aaa572fc909ca745a2a78103782ab68b838ad
获取overlay地址的前两个字节aeae,并在查看文件时将其带在target参数中.
curl http://localhost:1633/files/32eb13ba65fe78ba98e5d96f65cdb7cfea93d8e3b4ba1c7cc34bc71c8bb15b67?target=aeae
现在,即使我们的数据被删除,它们也将由我们的本地Bee节点在网络中修复,并且将始终对整个集群可用!
结算
启动bee时设置–swap-enable 和 --swap-endpoint标志
bee start \
--verbosity 5 \
--swap-enable \
--swap-endpoint https://rpc.slock.it/goerli \
--debug-api-enable
生成文件并上传
dd if=/dev/urandom of=/tmp/test.txt bs=1M count=20
curl -F file=@/tmp/test.txt http://localhost:1633/files
查看支票薄账户余额
curl localhost:1635/chequebook/balance | jq
查看每个peer的余额
curl localhost:1635/balances | jq
查看支票接收和发送数量
curl localhost:1635/settlements | jq
{
"totalreceived": 1304000000000,
"totalsent": 8983000000000,
"settlements": [
{
"peer": "200853fc86af3ffdd5c134d3a81dc8a957989cf4af770feeee7cda1c302787c7",
"received": 0,
"sent": 8983000000000
},
{
"peer": "ae700c55b4f491901f7055506892efa12241792b512256c52fc078a7d0ac9a94",
"received": 1120000000000,
"sent": 0
},
{
"peer": "ae226dc2e13470a478ee9daf7cff451b10ccce67949bf838627ccee31852b3da",
"received": 184000000000,
"sent": 0
}
]
}
查看支票薄详情
curl localhost:1635/chequebook/cheque | jq
{
"lastcheques": [
{
"peer": "200853fc86af3ffdd5c134d3a81dc8a957989cf4af770feeee7cda1c302787c7",
"lastreceived": null,
"lastsent": {
"beneficiary": "0x647b459c7dA0942A72F8DE7243F61d9873d1a6bd",
"chequebook": "0xfE47B51C5D21C4709020d81fd5182551e3B094B6",
"payout": 8983000000000
}
},
{
"peer": "ae700c55b4f491901f7055506892efa12241792b512256c52fc078a7d0ac9a94",
"lastreceived": {
"beneficiary": "0x5763e8fea803CAa7b2D6C18709Ef36b1b54fc1Cf",
"chequebook": "0x322F631f336f4fEbFdA1e5a2a5CDF81FbB6bbAD6",
"payout": 1120000000000
},
"lastsent": null
},
{
"peer": "ae226dc2e13470a478ee9daf7cff451b10ccce67949bf838627ccee31852b3da",
"lastreceived": {
"beneficiary": "0x5763e8fea803CAa7b2D6C18709Ef36b1b54fc1Cf",
"chequebook": "0xcfBCa130f7bAf4e4A08480d6Fb5E6Bee5b0a1C7e",
"payout": 184000000000
},
"lastsent": null
}
]
}
兑现(POST请求)
curl -XPOST http://localhost:1635/chequebook/cashout/ae700c55b4f491901f7055506892efa12241792b512256c52fc078a7d0ac9a94
查看兑现状态(GET请求)
curl http://localhost:1635/chequebook/cashout/ae700c55b4f491901f7055506892efa12241792b512256c52fc078a7d0ac9a94 | jq
将token从合约提现到个人账户
curl -XPOST http://localhost:1635/chequebook/withdraw\?amount\=金额 | jq