Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<feat>(transaction): simplify sending transactions when cross fabric chains #543

Merged
merged 5 commits into from
Jun 29, 2022

Conversation

leo201313
Copy link

@leo201313 leo201313 commented May 26, 2022

feature_fabricDefault

在涉及多个fabric链时,需要频繁切换Fabric的默认账户。需要一种方案来解决该问题。

方案讨论

WeCross-Console本地管理

最理想的情况是在WeCross-Console端建立一个本地的default映射,在调用sendTransaction时将对应的默认账号ID附上。遗憾的是WeCross在执行sendTransaction时是向WeCross-Account-Manager请求的默认账号用于发送交易信息,采用该方案需要修改整套WeCross方案。

使用数据库管理fabricDefault数据

该方案是在WeCross-Account-Manager数据库中建立一新的对象fabricDefault,如管理ChainAccount一般管理,因为现在的逻辑是WeCross-Account-Manager每次取UA时会在数据库中遍历寻找它关联的ChainAccount,那么也可以在取UA时遍历寻找它相关的fabricDefault。

该方案的好处是较为安全对现有代码逻辑几乎没有影响,但是需要重新构造一整个fabricDefault的运作方式并附加到UA的维护中,故也没有采用。

ChainAccount增加fabricDefault属性

在现有的ChainAccount基础上,增加一fabricDefault属性,指明该ChainAccount是哪一个fabric链的默认账户,如果不是默认账户则该项置空。该方案实现简单同时符合现在的WeCross与WeCross-Account-Manager的运作逻辑。选取该方案。

实现

在WeCross-Console中增加setDefaultFabricAccount指令,命令使用方法如下:

setDefaultFabricAccount payment.fabric-mychannel 1

其中payment.fabric-mychannel为chainName,1为账户的keyID。仿照setDefaultAccount写底层逻辑,在WeCross-Java-SDK中增加setDefaultFabricAccount的调用函数,以及修改listAccount的打印方式来显示fabric账户的fabricDefault属性。

在WeCross中增加/auth/setDefaultFabricAccount的路由,同时修改UA的构造方法,更改asynSendTransaction和asynCall中获取默认账户的方法。

在WeCross-Account-Manager中为ChainAccount增加fabricDefault属性,修改相应的对象构造方法以及数据库更新方法。

Consideration

由于修改了fabric链的sendTransaction与call的默认账户获取方式,不知道对WeCrossHub、HTLC、XA相关有没有影响(目测感觉没有)。

Future

  • customcommand中如fabric资源部署实例化相关应该也适用,可以简化fabric的资源部署过程。
  • 适配Web操作。

Want

需要WeCross的跨多条Fabric链的一键部署脚本,否则开发测试过于困难。

Test

使用WeCross-Demo的混合场景进行调试(实在是没找到两条链的fabric演示场景,所以在确认方案是否有效时是通过查日志实现的,太麻烦了)

在没有找到fabricDefault时使用当前默认Fabric账号发送交易

>> login
>> setDefaultAccount Fabric1.4 2
Result: success
Universal Account info has been changed, now auto-login again.
Result: success
=============================================================================================
Universal Account:
username: org1-admin
pubKey  : 3059301306...
uaID    : 3059301306...
>> listAccount
Universal Account:
username: org1-admin
pubKey  : 3059301306...
uaID    : 3059301306...
chainAccounts: [
	BCOS2.0 Account:
	keyID    : 0
	type     : BCOS2.0
	address  : 0x6f0674e82089a1e1ce147597a02d19878e18e59a
	isDefault: true
	----------
	GM_BCOS2.0 Account:
	keyID    : 4
	type     : GM_BCOS2.0
	address  : 0xda786dcc436164fa1d0a114c01786e74c8ec999f
	isDefault: true
	----------
	Fabric1.4 Account:
	keyID    : 2
	type     : Fabric1.4
	MembershipID : Org2MSP
	isDefault: true
	fabricDefault: 
	----------
	Fabric1.4 Account:
	keyID    : 3
	type     : Fabric1.4
	MembershipID : Org1MSP
	isDefault: false
	fabricDefault: 
	----------
	Fabric1.4 Account:
	keyID    : 1
	type     : Fabric1.4
	MembershipID : Org1MSP
	isDefault: false
	fabricDefault: 
	----------
]
>> sendTransaction payment.fabric-mychannel.sacc set a 666
Txhash  : 5ad6b31e1e372808c3de4d677b5919623b540800c3e31656ff0c70c2801055df
BlockNum: 6
Result  : [666]

验证到当chainName没有设置fabricDefault时,sendTransaction将采用当前默认的Fabric账户进行交易发送。

设置fabricDefault并通过fabricDefault进行交易发送

>> login
>> setDefaultAccount Fabric1.4 1
>> setDefaultFabricAccount payment.fabric-mychannel 2
>> listAccount
Universal Account:
username: org1-admin
pubKey  : 3059301306...
uaID    : 3059301306...
chainAccounts: [
	BCOS2.0 Account:
	keyID    : 0
	type     : BCOS2.0
	address  : 0x6f0674e82089a1e1ce147597a02d19878e18e59a
	isDefault: true
	----------
	GM_BCOS2.0 Account:
	keyID    : 4
	type     : GM_BCOS2.0
	address  : 0xda786dcc436164fa1d0a114c01786e74c8ec999f
	isDefault: true
	----------
	Fabric1.4 Account:
	keyID    : 1
	type     : Fabric1.4
	MembershipID : Org1MSP
	isDefault: true
	fabricDefault: 
	----------
	Fabric1.4 Account:
	keyID    : 3
	type     : Fabric1.4
	MembershipID : Org1MSP
	isDefault: false
	fabricDefault: 
	----------
	Fabric1.4 Account:
	keyID    : 2
	type     : Fabric1.4
	MembershipID : Org2MSP
	isDefault: false
	fabricDefault: payment.fabric-mychannel
	----------
]
>> sendTransaction payment.fabric-mychannel.sacc set b 777
Txhash  : 7c49ce50cde534d6c4bcd193f394246c42c7ce4d19fefd85c808299d04e0676c
BlockNum: 7
Result  : [777]
>> call payment.fabric-mychannel.sacc get a
Result  : [666]

查阅Router 127.0.0.1-8250-25500的日志(我在WeCross中增加了相应的loginfo便于debug)得:

  • 2022-05-26 16:16:26.155 [http-callback4] INFO Response() - asynSendTransaction use account by key: 2, path: payment.fabric-mychannel.sacc
  • 2022-05-26 16:16:37.320 [http-callback5] INFO Response() - asynCall use account by key: 2, path: payment.fabric-mychannel.sacc

说明此时确实是使用的keyID为2的Fabric账户进行的sendTransaction,同理call也是正常的。

设置已有的fabricDefault

>> login
>> listAccount
Universal Account:
username: org1-admin
pubKey  : 3059301306...
uaID    : 3059301306...
chainAccounts: [
	BCOS2.0 Account:
	keyID    : 0
	type     : BCOS2.0
	address  : 0xacbf658f8f89a0a45b08949cf6f91c94c815e867
	isDefault: true
	----------
	GM_BCOS2.0 Account:
	keyID    : 4
	type     : GM_BCOS2.0
	address  : 0x11cb6c74251f4eb60cf4c6fc0db820a3997cdf35
	isDefault: true
	----------
	Fabric1.4 Account:
	keyID    : 1
	type     : Fabric1.4
	MembershipID : Org1MSP
	isDefault: true
	fabricDefault: 
	----------
	Fabric1.4 Account:
	keyID    : 3
	type     : Fabric1.4
	MembershipID : Org1MSP
	isDefault: false
	fabricDefault: 
	----------
	Fabric1.4 Account:
	keyID    : 2
	type     : Fabric1.4
	MembershipID : Org2MSP
	isDefault: false
	fabricDefault: payment.fabric-mychannel
	----------
]
>> setDefaultFabricAccount payment.fabric-mychannel 1
>> listAccount
Universal Account:
username: org1-admin
pubKey  : 3059301306...
uaID    : 3059301306...
chainAccounts: [
	BCOS2.0 Account:
	keyID    : 0
	type     : BCOS2.0
	address  : 0xacbf658f8f89a0a45b08949cf6f91c94c815e867
	isDefault: true
	----------
	GM_BCOS2.0 Account:
	keyID    : 4
	type     : GM_BCOS2.0
	address  : 0x11cb6c74251f4eb60cf4c6fc0db820a3997cdf35
	isDefault: true
	----------
	Fabric1.4 Account:
	keyID    : 1
	type     : Fabric1.4
	MembershipID : Org1MSP
	isDefault: true
	fabricDefault: payment.fabric-mychannel
	----------
	Fabric1.4 Account:
	keyID    : 3
	type     : Fabric1.4
	MembershipID : Org1MSP
	isDefault: false
	fabricDefault: 
	----------
	Fabric1.4 Account:
	keyID    : 2
	type     : Fabric1.4
	MembershipID : Org2MSP
	isDefault: false
	fabricDefault: 
	----------
]

可以看到在设置keyID为1的账户的fabricDefault时,keyID为2的账户的fabricDefault刷掉,始终保持唯一性。

@leo201313
Copy link
Author

@leo201313 leo201313 changed the title add feature fabricDefault <feat>(transaction): simplify sending transactions when cross fabric chains May 27, 2022
@JimmyShi22
Copy link
Collaborator

赞!方案很完整,完成度很高!

@JimmyShi22
Copy link
Collaborator

感觉已经非常接近完美了! 👍
在WeCross的设计里面,除开stub和console,链类型对其它的组件是透明的。如Router和Account Manager。感觉本方案可以再通用一些,把fabricDefault这个字段通用化。让任意的链类型都可以这样操作,在发交易前,先根据chainName查询到相应的ChainAccount,查询到了就用这个去发交易,如果查询不到,就走原来的default账户逻辑。

@leo201313
Copy link
Author

Got it :)

@morebtcg
Copy link
Contributor

ci还有问题,麻烦看一下

@leo201313
Copy link
Author

ci还有问题,麻烦看一下

正在根据意见修改

@leo201313
Copy link
Author

特性通用化

现将fabricDefault属性修改为chainDefault,这样可以为每条链(任意类型)设置一专门的默认账号。

命令修改

WeCross-Console中增加setDefaultChainAccount命令(代替setDefaultFabricAccount),用于为一特定链指定默认账户。用法如下。

Set the chain account to be the default account to send transaction in a certain [chain]
Usage: setDefaultChainAccount [chainName][keyID]
chainName -- the full name of the chain
    chainName is like: payment.fabric-mychannel
keyID -- the primary key stand for chain account
[note]: you can do command listAccount to check chain account's keyID

sendTransaction命令将会优先使用资源所在链的默认账号执行,当未寻找到所在链的默认账号时使用链类型的默认账号执行。当chainName为NULL或null时,执行撤销命令,指定账号的chainDefault属性置空。

listChainAccount在非详细打印条件下只有Fabric账号中打印chainDefault属性(bcos账号通常只有一个就行)。在详细打印时(listAccount -d),所有类型的账号都将打印chainDefault属性。当然也可以通过修改WeCross-Java-SDK自定化更改打印函数。

Test

使用WeCross-Demo的混合场景进行调试

在没有找到fabricDefault时使用当前默认Fabric账号发送交易

>> login
>> setDefaultAccount Fabric1.4 2
Result: success
Universal Account info has been changed, now auto-login again.
Result: success
=============================================================================================
Universal Account:
username: org1-admin
pubKey  : 3059301306...
uaID    : 3059301306...
>> listAccount
Universal Account:
username: org1-admin
pubKey  : 3059301306...
uaID    : 3059301306...
chainAccounts: [
	BCOS2.0 Account:
	keyID    : 0
	type     : BCOS2.0
	address  : 0xbea904b99ce04ee10962a1d2eb1ed2715703851b
	isDefault: true
	----------
	GM_BCOS2.0 Account:
	keyID    : 4
	type     : GM_BCOS2.0
	address  : 0xc8e45ab4ccc414148b142f946d539891df018d65
	isDefault: true
	----------
	Fabric1.4 Account:
	keyID    : 2
	type     : Fabric1.4
	MembershipID : Org2MSP
	isDefault: true
	chainDefault: 
	----------
	Fabric1.4 Account:
	keyID    : 3
	type     : Fabric1.4
	MembershipID : Org1MSP
	isDefault: false
	chainDefault: 
	----------
	Fabric1.4 Account:
	keyID    : 1
	type     : Fabric1.4
	MembershipID : Org1MSP
	isDefault: false
	chainDefault: 
	----------
]
>> sendTransaction payment.fabric-mychannel.sacc set a 666
Txhash  : 30e03f3894487849d6719a53d7d4706f30ffdb5dad0ed93d63a90a3d41beeffd
BlockNum: 6
Result  : [666]

验证到当chainName没有设置fabricDefault时,sendTransaction将采用当前默认的Fabric账户进行交易发送。

设置fabricDefault并通过fabricDefault进行交易发送

>> login
>> setDefaultAccount Fabric1.4 1
>> setDefaultChainAccount payment.fabric-mychannel 2
>> listAccount
Universal Account:
username: org1-admin
pubKey  : 3059301306...
uaID    : 3059301306...
chainAccounts: [
	BCOS2.0 Account:
	keyID    : 0
	type     : BCOS2.0
	address  : 0xbea904b99ce04ee10962a1d2eb1ed2715703851b
	isDefault: true
	----------
	GM_BCOS2.0 Account:
	keyID    : 4
	type     : GM_BCOS2.0
	address  : 0xc8e45ab4ccc414148b142f946d539891df018d65
	isDefault: true
	----------
	Fabric1.4 Account:
	keyID    : 1
	type     : Fabric1.4
	MembershipID : Org1MSP
	isDefault: true
	chainDefault: 
	----------
	Fabric1.4 Account:
	keyID    : 3
	type     : Fabric1.4
	MembershipID : Org1MSP
	isDefault: false
	chainDefault: 
	----------
	Fabric1.4 Account:
	keyID    : 2
	type     : Fabric1.4
	MembershipID : Org2MSP
	isDefault: false
	chainDefault: payment.fabric-mychannel
	----------
]
>> sendTransaction payment.fabric-mychannel.sacc set b 777
Txhash  : cff46024601b0d49ca09245c5288f2961d10145e4f4299af5952c3e47c2656dd
BlockNum: 7
Result  : [777]
>> call payment.fabric-mychannel.sacc get a
Result  : [666]
>> call payment.fabric-mychannel.sacc get b
Result  : [777]

查阅Router 127.0.0.1-8250-25500的日志(我在WeCross中增加了相应的loginfo便于debug)得:

  • 2022-06-10 18:22:36.277 [http-callback3] INFO Response() - asynSendTransaction use account by key: 2, path: payment.fabric-mychannel.sacc
  • 2022-06-10 18:27:17.035 [http-callback6] INFO Response() - asynCall use account by key: 2, path: payment.fabric-mychannel.sacc
  • 2022-06-10 18:27:23.758 [http-callback7] INFO Response() - asynCall use account by key: 2, path: payment.fabric-mychannel.sacc

说明此时确实是使用的keyID为2的Fabric账户进行的sendTransaction,同理call也是正常的。

设置已有的fabricDefault

>> login
>> listAccount
Universal Account:
username: org1-admin
pubKey  : 3059301306...
uaID    : 3059301306...
chainAccounts: [
	BCOS2.0 Account:
	keyID    : 0
	type     : BCOS2.0
	address  : 0xbea904b99ce04ee10962a1d2eb1ed2715703851b
	isDefault: true
	----------
	GM_BCOS2.0 Account:
	keyID    : 4
	type     : GM_BCOS2.0
	address  : 0xc8e45ab4ccc414148b142f946d539891df018d65
	isDefault: true
	----------
	Fabric1.4 Account:
	keyID    : 1
	type     : Fabric1.4
	MembershipID : Org1MSP
	isDefault: true
	chainDefault: 
	----------
	Fabric1.4 Account:
	keyID    : 3
	type     : Fabric1.4
	MembershipID : Org1MSP
	isDefault: false
	chainDefault: 
	----------
	Fabric1.4 Account:
	keyID    : 2
	type     : Fabric1.4
	MembershipID : Org2MSP
	isDefault: false
	chainDefault: payment.fabric-mychannel
	----------
]
>> setDefaultChainAccount payment.fabric-mychannel 1
>> listAccount
Universal Account:
username: org1-admin
pubKey  : 3059301306...
uaID    : 3059301306...
chainAccounts: [
	BCOS2.0 Account:
	keyID    : 0
	type     : BCOS2.0
	address  : 0xbea904b99ce04ee10962a1d2eb1ed2715703851b
	isDefault: true
	----------
	GM_BCOS2.0 Account:
	keyID    : 4
	type     : GM_BCOS2.0
	address  : 0xc8e45ab4ccc414148b142f946d539891df018d65
	isDefault: true
	----------
	Fabric1.4 Account:
	keyID    : 1
	type     : Fabric1.4
	MembershipID : Org1MSP
	isDefault: true
	chainDefault: payment.fabric-mychannel
	----------
	Fabric1.4 Account:
	keyID    : 3
	type     : Fabric1.4
	MembershipID : Org1MSP
	isDefault: false
	chainDefault: 
	----------
	Fabric1.4 Account:
	keyID    : 2
	type     : Fabric1.4
	MembershipID : Org2MSP
	isDefault: false
	chainDefault: 
	----------
]

可以看到在设置keyID为1的账户的fabricDefault时,keyID为2的账户的fabricDefault刷掉,始终保持唯一性。

撤销设置chainDefault

>> login
>> listAccount
chainAccounts: [
	BCOS2.0 Account:
	keyID    : 0
	type     : BCOS2.0
	address  : 0xbea904b99ce04ee10962a1d2eb1ed2715703851b
	isDefault: true
	----------
	GM_BCOS2.0 Account:
	keyID    : 4
	type     : GM_BCOS2.0
	address  : 0xc8e45ab4ccc414148b142f946d539891df018d65
	isDefault: true
	----------
	Fabric1.4 Account:
	keyID    : 1
	type     : Fabric1.4
	MembershipID : Org1MSP
	isDefault: true
	chainDefault: payment.fabric-mychannel
	----------
	Fabric1.4 Account:
	keyID    : 3
	type     : Fabric1.4
	MembershipID : Org1MSP
	isDefault: false
	chainDefault: 
	----------
	Fabric1.4 Account:
	keyID    : 2
	type     : Fabric1.4
	MembershipID : Org2MSP
	isDefault: false
	chainDefault: 
	----------
]
>> setDefaultChainAccount NULL 1
>> listAccount
chainAccounts: [
	BCOS2.0 Account:
	keyID    : 0
	type     : BCOS2.0
	address  : 0xbea904b99ce04ee10962a1d2eb1ed2715703851b
	isDefault: true
	----------
	GM_BCOS2.0 Account:
	keyID    : 4
	type     : GM_BCOS2.0
	address  : 0xc8e45ab4ccc414148b142f946d539891df018d65
	isDefault: true
	----------
	Fabric1.4 Account:
	keyID    : 1
	type     : Fabric1.4
	MembershipID : Org1MSP
	isDefault: true
	chainDefault:
	----------
	Fabric1.4 Account:
	keyID    : 3
	type     : Fabric1.4
	MembershipID : Org1MSP
	isDefault: false
	chainDefault: 
	----------
	Fabric1.4 Account:
	keyID    : 2
	type     : Fabric1.4
	MembershipID : Org2MSP
	isDefault: false
	chainDefault: 
	----------
]

可以看到通过设置chainName为NULL(null)可以置空chainDefault属性。

测试其他类型账号能否设置chainDefault

>> login
>> setDefaultChainAccount payment.example 0
>> listAccount -d
Universal Account:
username: org1-admin
pubKey  : 3059301306072a8648ce3d020106082a811ccf5501822d0342000462cc166a8a47ef4450707586835f41b8b3806890d51ef2fef6d26a7a3b9da210fb260f4d1c7f60a669039d2bb940965068176b0ff264a31cf201b43a2d5719af
uaID    : 3059301306072a8648ce3d020106082a811ccf5501822d0342000462cc166a8a47ef4450707586835f41b8b3806890d51ef2fef6d26a7a3b9da210fb260f4d1c7f60a669039d2bb940965068176b0ff264a31cf201b43a2d5719af
chainAccounts: [
	BCOS2.0 Account:
	keyID    : 0
	type     : BCOS2.0
	pubKey   : -----BEGIN PUBLIC KEY-----
MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAE6yR3yBfsLtx7gcK4e2+0Ja2odPQBIv0S
izz+LER2wFnzlaml6LHKsT6x3cNS6PPxCtl9PT04Of39TBKOrK+DnA==
-----END PUBLIC KEY-----

	address  : 0x2628ea27f91ad57579ea2734103699dfd30c839e
	isDefault: true
	chainDefault: payment.example

可以看到BCOS2.0的账号也成功设置了默认账号。

To minimize the effect when an odd user set a chainName to a wrong type account.
@leo201313
Copy link
Author

chainDefault在设置时是没有判断chainDefault是否满足语法规则以及账户类型是否与链类型对应(这一步可以在webpage和weconsole进行检测),WeCross在使用该属性时是会进行检查的。所以当前情况下是允许出现将不存在的chain或者其它类型的chain设置给账户。

现阶段默认用户都是正常的,但是不排除一些奇怪的用户可能会错误地给某一账户设置非对应类型的chainDefault。
假如一fabric账户已设置chainDefault为payment.fabric-mychannel,如果用户设置另一bcos账户的chainDefault也为payment.fabric-mychannel,那么此时两个账号的chainDefault相同,都显示为payment.fabric-mychannel(fabric账户的chainDefault不会被刷掉置空,因为在WeCross-Account-Manager的逻辑中在设置新的chainDefault时只检查同一类型账户是否已存在相同值的账户并进行置空)。

已更新代码,使得当这种情况发生时,可以继续正常使用SendTransaction(即不会使用bcos账户对payment.fabric-mychannel链进行访问)。

@JimmyShi22
Copy link
Collaborator

完美了!

Copy link
Collaborator

@JimmyShi22 JimmyShi22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@JimmyShi22
Copy link
Collaborator

CI failed.
Please use the command to format the code and push again 😄

bash gradlew goJF 

@leo201313
Copy link
Author

I've run gradlew goJF already and push the code again. However, I think ci still cannot succeed as other prs in WeCross-Account-Manager, WeCross-Java-SDK, and WeCross-Consle are not involved.

@JimmyShi22 JimmyShi22 merged commit c5b961b into WeBankBlockchain:dev Jun 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants