我们都知道,HIVE上我们追随或者拉黑一个人,对应的操作是`follow`以及`mute`,相应的取消操作分别为`mute`与`unmute`,实际上在链上,这些都是通过同一个操作实现的,那就是`custom_json`。 ![image.png](https://images.hive.blog/DQmeXqRS5YJFSDQ58ztLeryhT52MbvKxjvkBDU8VqHCDuhX/image.png) (图源 :[pixabay](https://pixabay.com/)) 说到`custom_json`,那简直是一个百宝箱,可以配合链上/链下的应用实现好多功能,举例来说以前SE的好多功能都是用`custom_json`来实现的,还有现在的社区功能,也是基于`custom_json`的。 # 结构 `custom_json`的模板大致如下: ``` op_custom_json = ['custom_json', { 'required_auths': [], 'required_posting_auths': [], 'id': '', 'json': '' }] ``` 其中`required_auths`中需要填写需要ACTIVE授权的用户组,`required_posting_auths`填写需要POSTING权限的用户组,两者均可以留空,但是不能同时为空,相应的检查代码如下: >`FC_ASSERT( (required_auths.size() + required_posting_auths.size()) > 0, "at least one account must be specified" );` `id`这个名字起的比较怪,我看到`id`就会以为它是一个数字,其实不然,`id`是一个字符串。在代码注释中,说`id`长度必须小于32个字符: >`custom_id_type id; ///< must be less than 32 characters long` 但是实际上,检查代码如下: >`FC_ASSERT( id.size() <= STEEM_CUSTOM_OP_ID_MAX_LENGTH, "Operation ID length exceeded. Max: ${max} Current: ${n}", ("max", STEEM_CUSTOM_OP_ID_MAX_LENGTH)("n", id.size()) );` 而`STEEM_CUSTOM_OP_ID_MAX_LENGTH`定义为`32`,所以 `id`的最大长度实际上是`32`啦。 至于`json`就不用多说了,就是转化成UTF8编码字符串的JSON啦。 # 实现 知道了`custom_json`的定义,实现起来就比较简单了,下面是大致的实现逻辑: 给对应项赋值: >`op[1]['id'] = id` >`op[1]['json'] = json.dumps(json_data)` >`op[1]['required_auths'] = required_auths` >`op[1]['required_posting_auths'] = required_posting_auths` 追加进transaction,对其签名并广播: >`trx.append_op(op)` >`trx.sign_digest(wif)` >`ret = trx.broadcast()` # follow\unfollow\mute\unmute 有了`custom_json`,我们就可以实现`follow\unfollow\mute\unmute`功能了。 以@oflyhigh.test账户为例,几个操作对应JSON如下: >`json_follow = ["follow",{"follower":"oflyhigh.test","following":"oflyhigh","what":["blog"]}]` >`json_unfollow = ["follow",{"follower":"oflyhigh.test","following":"oflyhigh","what":[]}]` >`json_mute = ["follow",{"follower":"oflyhigh.test","following":"oflyhigh","what":["ignore"]}]` >`json_unmute = ["follow",{"follower":"oflyhigh.test","following":"oflyhigh","what":[]}]` 我们不难看出`unfollow`和`unmute`其实是一样的,都是把`what`中的内容清空。 将上述json内容放入我们实现的custom_json函数中,就会实现对应的功能,比如: >`client.custom_json("follow", json_data=json_follow, required_posting_auths=["oflyhigh.test"], wif=wif)` 我分别调用了一下几个操作,有趣的是,因为`json_unfollow`和`json_unmute`本质上操作相同,tapos以及超时时间又完全一样,会被认为是重复操作: >![image.png](https://images.hive.blog/DQmVi2qQVawccDGdshaSFzfnrMX5Kd5xE2zyrxwDNuQfGSY/image.png) 所以加一个超时,让`unfollow`和`unmute`的超时时间不同,就好了。 顺利执行后,在https://hiveblocks.com/上我们会看到类似如下的结果: >![image.png](https://images.hive.blog/DQmayfapV5vLHqnuypgv3fpu4SH7Eje8XXUn5vLyVRz72MS/image.png) # 结束语 本文就扯到这里啦,至于如何用`custom_json`实现其它功能,其实都是大同小异,留给大家自己探索了。 另外,文中代码仅为参考,大家可以用hive-python或者beem等去玩转`custom_json`,我的库还不成熟,先不放出来献丑了。
post_id | 86,229,899 |
---|---|
author | oflyhigh |
permlink | customjson-and-follow-unfollow-mute-unmute |
category | cn |
json_metadata | {"tags":["cn","cutehive","cn-programming","python","custom-json","follow","mute"],"users":["oflyhigh.test"],"image":["https:\/\/images.hive.blog\/DQmeXqRS5YJFSDQ58ztLeryhT52MbvKxjvkBDU8VqHCDuhX\/image.png","https:\/\/images.hive.blog\/DQmVi2qQVawccDGdshaSFzfnrMX5Kd5xE2zyrxwDNuQfGSY\/image.png","https:\/\/images.hive.blog\/DQmayfapV5vLHqnuypgv3fpu4SH7Eje8XXUn5vLyVRz72MS\/image.png"],"links":["https:\/\/pixabay.com\/","https:\/\/hiveblocks.com\/\u4e0a\u6211\u4eec\u4f1a\u770b\u5230\u7c7b\u4f3c\u5982\u4e0b\u7684\u7ed3\u679c\uff1a"],"app":"steemit\/0.2","format":"markdown"} |
created | 2020-06-04 03:27:03 |
last_update | 2020-06-04 03:27:03 |
depth | 0 |
children | 1 |
net_rshares | 31,081,649,298,194 |
last_payout | 2020-06-11 03:27:03 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 12.742 SBD |
curator_payout_value | 12.646 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 2,724 |
author_reputation | 1,148,153,621,496,884 |
root_title | "每天进步一点点:custom_json & follow\unfollow\mute\unmute" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
adm | 0 | 7,391,964,087,680 | 100% | ||
onealfa | 0 | 20,353,479,269 | 4.82% | ||
livingfree | 0 | 21,135,094,875 | 1% | ||
lemooljiang | 0 | 653,981,353,303 | 88% | ||
ace108 | 0 | 320,525,707,021 | 25% | ||
originate | 0 | 8,977,336,435 | 1% | ||
magicmonk | 0 | 492,518,182,591 | 100% | ||
skysunny | 0 | 144,964,719 | 88% | ||
midnightoil | 0 | 124,983,960,391 | 100% | ||
xiaohui | 0 | 942,729,802,642 | 100% | ||
oflyhigh | 0 | 384,735,719,175 | 100% | ||
helene | 0 | 122,598,466,171 | 100% | ||
ffcrossculture | 0 | 369,280,534,985 | 100% | ||
ethansteem | 0 | 32,396,724,916 | 100% | ||
sweetsssj | 0 | 17,028,544,768,950 | 50% | ||
shadowspub | 0 | 3,549,392,069 | 9% | ||
penguinpablo | 0 | 305,916,751,437 | 16% | ||
ebargains | 0 | 7,428,521,593 | 30% | ||
steemrising | 0 | 935,929,321 | 1% | ||
insights | 0 | 1,530,316,819 | 1% | ||
steemyoda | 0 | 2,357,719,411 | 1% | ||
eforucom | 0 | 3,698,680,899 | 20% | ||
created | 0 | 82,637,383,981 | 1% | ||
timool | 0 | 1,255,404,297 | 88% | ||
lucknie | 0 | 19,568,784,983 | 88% | ||
dumping | 0 | 2,018,631,288 | 88% | ||
improv | 0 | 7,969,030,072 | 3% | ||
exec | 0 | 46,783,588,621 | 100% | ||
eval | 0 | 858,028,442 | 100% | ||
ew-and-patterns | 0 | 71,200,878,060 | 16% | ||
techken | 0 | 602,390,844 | 1.6% | ||
gunthertopp | 0 | 347,687,775,327 | 7.5% | ||
karja | 0 | 56,739,304,891 | 3% | ||
kimzwarch | 0 | 28,585,398,872 | 12.91% | ||
accelerator | 0 | 20,366,506,142 | 4% | ||
buildawhale | 0 | 52,479,685,282 | 30% | ||
osm0sis | 0 | 4,316,714,015 | 30% | ||
rollsman | 0 | 5,940,262,413 | 100% | ||
metten | 0 | 10,473,548,582 | 88% | ||
cn-book | 0 | 1,091,854,571 | 88% | ||
windowglass | 0 | 216,265,327,409 | 30% | ||
awesome-gadgets | 0 | 1,828,909,931 | 100% | ||
cnbuddy | 0 | 1,614,214,503 | 0.1% | ||
cn-movie | 0 | 126,372,935 | 88% | ||
vivia | 0 | 3,363,442,095 | 88% | ||
sneakyninja | 0 | 14,308,598,937 | 100% | ||
cryptonized | 0 | 66,382,231,110 | 16% | ||
girolamomarotta | 0 | 90,205,339,893 | 99.9% | ||
mmmmkkkk311 | 0 | 598,397,171,913 | 100% | ||
yikloongye | 0 | 0 | 4% | ||
johnhooi | 0 | 0 | 4% | ||
heidimarie | 0 | 22,597,736,756 | 7% | ||
curx | 0 | 98,902,496,847 | 30% | ||
abandi | 0 | 767,631,710 | 100% | ||
xiaoli | 0 | 320,335,679 | 88% | ||
yumisee | 0 | 17,778,534 | 4% | ||
joeliew | 0 | 155,792,542 | 4% | ||
best-strategy | 0 | 10,263,817,805 | 100% | ||
hkit98 | 0 | 0 | 4% | ||
elvinmas001 | 0 | 0 | 4% | ||
watersoo | 0 | 84,751,709 | 4% | ||
vamos-amigo | 0 | 154,014,580 | 4% | ||
iipoh06 | 0 | 0 | 4% | ||
annabellenoelle | 0 | 7,956,926 | 4% | ||
riczlook | 0 | 0 | 4% | ||
andrewnoel | 0 | 0 | 4% | ||
josemalavem | 0 | 25,435,548,042 | 100% | ||
onepercentbetter | 0 | 29,919,382,439 | 12% | ||
andrewma | 0 | 5,552,520,329 | 20% | ||
sergino | 0 | 3,977,805,835 | 10% | ||
archisteem | 0 | 4,454,603,337 | 7.08% | ||
cn-malaysia | 0 | 1,136,685,743 | 4% | ||
merlion | 0 | 3,968,493,038 | 12% | ||
natur-pur | 0 | 861,590,024 | 100% | ||
pollygonewild | 0 | 0 | 4% | ||
foodblogresteem | 0 | 0 | 4% | ||
bluesniper | 0 | 26,662,358,569 | 75% | ||
jacoblai | 0 | 0 | 4% | ||
ctime | 0 | 662,203,360,610 | 12% | ||
bobdavids | 0 | 31,412,694 | 4% | ||
hungrybear | 0 | 2,145,893,623 | 12% | ||
kharma.scribbles | 0 | 237,108,657 | 6.75% | ||
apix | 0 | 21,719,413,413 | 100% | ||
steem-fund | 0 | 4,428,564,538 | 100% | ||
morwen | 0 | 28,684,238,291 | 24% | ||
diceshark | 0 | 81,860,040 | 50% | ||
map10k | 0 | 7,668,514,361 | 21% | ||
goldentouch | 0 | 67,042,634 | 50% | ||
online-24-7 | 0 | 1,333,401,680 | 100% | ||
mapxv | 0 | 6,793,529,375 | 15% | ||
botante | 0 | 63,706,107,192 | 20% | ||
maxuv | 0 | 930,874,294 | 12.7% | ||
maxuvd | 0 | 5,977,372,405 | 5.4% | ||
maxuve | 0 | 27,630,989,240 | 5.4% | ||
cnbuddy-reward | 0 | 154,790,723 | 0.05% | ||
steemjiang | 0 | 0 | 88% | ||
starnote | 0 | 231,335,364 | 88% | ||
coin-doubler | 0 | 17,275,674,254 | 100% | ||
moochain.net | 0 | 233,139,076 | 88% | ||
lemool | 0 | 0 | 88% | ||
moochain.com | 0 | 0 | 88% | ||
liebin | 0 | 0 | 88% | ||
bettyliu | 0 | 0 | 88% | ||
younggle | 0 | 0 | 88% | ||
dinggle | 0 | 0 | 88% | ||
moocer | 0 | 0 | 88% | ||
steem-zh | 0 | 0 | 88% | ||
coursechain | 0 | 0 | 88% | ||
youngoole | 0 | 0 | 88% | ||
imooc | 0 | 0 | 88% | ||
wikicoin | 0 | 0 | 88% | ||
starchain | 0 | 0 | 88% | ||
steemzh | 0 | 0 | 88% | ||
openbazzar | 0 | 0 | 88% | ||
steemipfs | 0 | 0 | 88% | ||
hive-224466 | 0 | 0 | 88% | ||
evahe | 0 | 1,519,101,205 | 61% |
代码看到我头晕(=_=)眼花,学网页设计的我,已经还给老师啦哈哈哈哈哈,今天写了两个文章,消耗大,我要去找吃的了 来自于 [WhereIn Android] (http://www.wherein.io)
post_id | 86,231,628 |
---|---|
author | evahe |
permlink | wherein-1591254941230 |
category | cn |
json_metadata | {} |
created | 2020-06-04 07:15:45 |
last_update | 2020-06-04 07:15:45 |
depth | 1 |
children | 0 |
net_rshares | 0 |
last_payout | 2020-06-11 07:15:45 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 SBD |
curator_payout_value | 0.000 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 103 |
author_reputation | 216,548,687,369,048 |
root_title | "每天进步一点点:custom_json & follow\unfollow\mute\unmute" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |