mirror of
https://gitee.com/BDWare/cp.git
synced 2025-06-12 10:44:02 +00:00
Compare commits
99 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
a17925a5f1 | ||
|
4087202c24 | ||
|
533c92f843 | ||
|
93ccf7410b | ||
|
b50f891f02 | ||
|
9204992153 | ||
|
b5470af3c5 | ||
|
79cd1d1781 | ||
|
46b67d200e | ||
|
94db480dc2 | ||
|
de617542e9 | ||
|
8ffaa3f535 | ||
|
35e39f3dd9 | ||
|
ac62f7bbf7 | ||
|
eb91d17982 | ||
|
7e8076e075 | ||
2180260a20 | |||
2384f6cc59 | |||
a794bd3af4 | |||
c0c580090e | |||
0b253a4625 | |||
5326f41ff7 | |||
2a59f577fc | |||
6dbb0e161c | |||
|
78d068d39b | ||
|
e757264a38 | ||
|
0e5f92e51e | ||
|
587310c899 | ||
|
16f1e3a2cd | ||
|
9e55d56e85 | ||
|
50991a89d0 | ||
|
d76f816e92 | ||
|
76f564c777 | ||
|
fd1690c97b | ||
|
723c9c7a06 | ||
|
dc0671cfbd | ||
|
841f567a88 | ||
|
2514980e8b | ||
|
491cbadc4d | ||
|
042cfae39b | ||
|
16f2d4fae1 | ||
|
cd87b1f014 | ||
|
0d7424cd7f | ||
|
849e8b6601 | ||
|
1a8cb9c2a7 | ||
|
18b01e3ab0 | ||
|
d040028463 | ||
|
fb8305031e | ||
|
a117ef4a41 | ||
|
0daa4b6d8c | ||
|
fdb652880b | ||
|
838b2e77c8 | ||
|
098854fd46 | ||
|
29f81e582a | ||
|
7f81dba895 | ||
|
6e25abbef0 | ||
|
1a4bf502bb | ||
|
51ee18928b | ||
|
a1b59cfbfd | ||
|
76a8e3ca58 | ||
|
14e5fffe1d | ||
|
9477ff8a31 | ||
|
05e301d9e9 | ||
|
24c0673b9b | ||
|
9686de6ba9 | ||
|
37ad6c35d7 | ||
|
65aa9fd78d | ||
|
07174bb3b4 | ||
|
a35ca147e3 | ||
|
9317385bd9 | ||
|
64cdfecd67 | ||
|
56d140a2c7 | ||
|
363a20049a | ||
|
c077d425ab | ||
|
ad8de7a92c | ||
|
639d8dc6cb | ||
|
2770908f2b | ||
|
14db4a8f6c | ||
|
df94bedbae | ||
|
4652d731c4 | ||
|
e5157df523 | ||
|
e5e16a5e1d | ||
|
5f42ed082c | ||
|
1229c9ce4c | ||
|
c9b5a32e46 | ||
|
30d5ca5ace | ||
|
4318162fa8 | ||
|
6064882843 | ||
|
cf097ce7fd | ||
|
bac80bbf31 | ||
|
fa729d80a0 | ||
|
4b96899653 | ||
|
8db5c8e2d7 | ||
62679528e4 | |||
|
ea7eaf5db4 | ||
8095f9735f | |||
|
cb13076423 | ||
|
b5ec0caccb | ||
|
afad80ee2b |
1
.gitignore
vendored
1
.gitignore
vendored
@ -4,3 +4,4 @@ debugconf.json
|
||||
/ContractDB/
|
||||
/defaultLog/
|
||||
/log/
|
||||
.DS_Store
|
352
LICENSE
352
LICENSE
@ -1,339 +1,127 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
木兰宽松许可证, 第2版
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
木兰宽松许可证, 第2版
|
||||
2020年1月 http://license.coscl.org.cn/MulanPSL2
|
||||
|
||||
Preamble
|
||||
|
||||
The licenses for most software are designed to take away your
|
||||
freedom to share and change it. By contrast, the GNU General Public
|
||||
License is intended to guarantee your freedom to share and change free
|
||||
software--to make sure the software is free for all its users. This
|
||||
General Public License applies to most of the Free Software
|
||||
Foundation's software and to any other program whose authors commit to
|
||||
using it. (Some other Free Software Foundation software is covered by
|
||||
the GNU Lesser General Public License instead.) You can apply it to
|
||||
your programs, too.
|
||||
您对“软件”的复制、使用、修改及分发受木兰宽松许可证,第2版(“本许可证”)的如下条款的约束:
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
this service if you wish), that you receive source code or can get it
|
||||
if you want it, that you can change the software or use pieces of it
|
||||
in new free programs; and that you know you can do these things.
|
||||
0. 定义
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid
|
||||
anyone to deny you these rights or to ask you to surrender the rights.
|
||||
These restrictions translate to certain responsibilities for you if you
|
||||
distribute copies of the software, or if you modify it.
|
||||
“软件”是指由“贡献”构成的许可在“本许可证”下的程序和相关文档的集合。
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must give the recipients all the rights that
|
||||
you have. You must make sure that they, too, receive or can get the
|
||||
source code. And you must show them these terms so they know their
|
||||
rights.
|
||||
“贡献”是指由任一“贡献者”许可在“本许可证”下的受版权法保护的作品。
|
||||
|
||||
We protect your rights with two steps: (1) copyright the software, and
|
||||
(2) offer you this license which gives you legal permission to copy,
|
||||
distribute and/or modify the software.
|
||||
“贡献者”是指将受版权法保护的作品许可在“本许可证”下的自然人或“法人实体”。
|
||||
|
||||
Also, for each author's protection and ours, we want to make certain
|
||||
that everyone understands that there is no warranty for this free
|
||||
software. If the software is modified by someone else and passed on, we
|
||||
want its recipients to know that what they have is not the original, so
|
||||
that any problems introduced by others will not reflect on the original
|
||||
authors' reputations.
|
||||
“法人实体”是指提交贡献的机构及其“关联实体”。
|
||||
|
||||
Finally, any free program is threatened constantly by software
|
||||
patents. We wish to avoid the danger that redistributors of a free
|
||||
program will individually obtain patent licenses, in effect making the
|
||||
program proprietary. To prevent this, we have made it clear that any
|
||||
patent must be licensed for everyone's free use or not licensed at all.
|
||||
“关联实体”是指,对“本许可证”下的行为方而言,控制、受控制或与其共同受控制的机构,此处的控制是指有受控方或共同受控方至少50%直接或间接的投票权、资金或其他有价证券。
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
1. 授予版权许可
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的版权许可,您可以复制、使用、修改、分发其“贡献”,不论修改与否。
|
||||
|
||||
0. This License applies to any program or other work which contains
|
||||
a notice placed by the copyright holder saying it may be distributed
|
||||
under the terms of this General Public License. The "Program", below,
|
||||
refers to any such program or work, and a "work based on the Program"
|
||||
means either the Program or any derivative work under copyright law:
|
||||
that is to say, a work containing the Program or a portion of it,
|
||||
either verbatim or with modifications and/or translated into another
|
||||
language. (Hereinafter, translation is included without limitation in
|
||||
the term "modification".) Each licensee is addressed as "you".
|
||||
2. 授予专利许可
|
||||
|
||||
Activities other than copying, distribution and modification are not
|
||||
covered by this License; they are outside its scope. The act of
|
||||
running the Program is not restricted, and the output from the Program
|
||||
is covered only if its contents constitute a work based on the
|
||||
Program (independent of having been made by running the Program).
|
||||
Whether that is true depends on what the Program does.
|
||||
每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的(根据本条规定撤销除外)专利许可,供您制造、委托制造、使用、许诺销售、销售、进口其“贡献”或以其他方式转移其“贡献”。前述专利许可仅限于“贡献者”现在或将来拥有或控制的其“贡献”本身或其“贡献”与许可“贡献”时的“软件”结合而将必然会侵犯的专利权利要求,不包括对“贡献”的修改或包含“贡献”的其他结合。如果您或您的“关联实体”直接或间接地,就“软件”或其中的“贡献”对任何人发起专利侵权诉讼(包括反诉或交叉诉讼)或其他专利维权行动,指控其侵犯专利权,则“本许可证”授予您对“软件”的专利许可自您提起诉讼或发起维权行动之日终止。
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Program's
|
||||
source code as you receive it, in any medium, provided that you
|
||||
conspicuously and appropriately publish on each copy an appropriate
|
||||
copyright notice and disclaimer of warranty; keep intact all the
|
||||
notices that refer to this License and to the absence of any warranty;
|
||||
and give any other recipients of the Program a copy of this License
|
||||
along with the Program.
|
||||
3. 无商标许可
|
||||
|
||||
You may charge a fee for the physical act of transferring a copy, and
|
||||
you may at your option offer warranty protection in exchange for a fee.
|
||||
“本许可证”不提供对“贡献者”的商品名称、商标、服务标志或产品名称的商标许可,但您为满足第4条规定的声明义务而必须使用除外。
|
||||
|
||||
2. You may modify your copy or copies of the Program or any portion
|
||||
of it, thus forming a work based on the Program, and copy and
|
||||
distribute such modifications or work under the terms of Section 1
|
||||
above, provided that you also meet all of these conditions:
|
||||
4. 分发限制
|
||||
|
||||
a) You must cause the modified files to carry prominent notices
|
||||
stating that you changed the files and the date of any change.
|
||||
您可以在任何媒介中将“软件”以源程序形式或可执行形式重新分发,不论修改与否,但您必须向接收者提供“本许可证”的副本,并保留“软件”中的版权、商标、专利及免责声明。
|
||||
|
||||
b) You must cause any work that you distribute or publish, that in
|
||||
whole or in part contains or is derived from the Program or any
|
||||
part thereof, to be licensed as a whole at no charge to all third
|
||||
parties under the terms of this License.
|
||||
5. 免责声明与责任限制
|
||||
|
||||
c) If the modified program normally reads commands interactively
|
||||
when run, you must cause it, when started running for such
|
||||
interactive use in the most ordinary way, to print or display an
|
||||
announcement including an appropriate copyright notice and a
|
||||
notice that there is no warranty (or else, saying that you provide
|
||||
a warranty) and that users may redistribute the program under
|
||||
these conditions, and telling the user how to view a copy of this
|
||||
License. (Exception: if the Program itself is interactive but
|
||||
does not normally print such an announcement, your work based on
|
||||
the Program is not required to print an announcement.)
|
||||
“软件”及其中的“贡献”在提供时不带任何明示或默示的担保。在任何情况下,“贡献者”或版权所有者不对任何人因使用“软件”或其中的“贡献”而引发的任何直接或间接损失承担责任,不论因何种原因导致或者基于何种法律理论,即使其曾被建议有此种损失的可能性。
|
||||
|
||||
These requirements apply to the modified work as a whole. If
|
||||
identifiable sections of that work are not derived from the Program,
|
||||
and can be reasonably considered independent and separate works in
|
||||
themselves, then this License, and its terms, do not apply to those
|
||||
sections when you distribute them as separate works. But when you
|
||||
distribute the same sections as part of a whole which is a work based
|
||||
on the Program, the distribution of the whole must be on the terms of
|
||||
this License, whose permissions for other licensees extend to the
|
||||
entire whole, and thus to each and every part regardless of who wrote it.
|
||||
6. 语言
|
||||
“本许可证”以中英文双语表述,中英文版本具有同等法律效力。如果中英文版本存在任何冲突不一致,以中文版为准。
|
||||
|
||||
Thus, it is not the intent of this section to claim rights or contest
|
||||
your rights to work written entirely by you; rather, the intent is to
|
||||
exercise the right to control the distribution of derivative or
|
||||
collective works based on the Program.
|
||||
条款结束
|
||||
|
||||
In addition, mere aggregation of another work not based on the Program
|
||||
with the Program (or with a work based on the Program) on a volume of
|
||||
a storage or distribution medium does not bring the other work under
|
||||
the scope of this License.
|
||||
如何将木兰宽松许可证,第2版,应用到您的软件
|
||||
|
||||
3. You may copy and distribute the Program (or a work based on it,
|
||||
under Section 2) in object code or executable form under the terms of
|
||||
Sections 1 and 2 above provided that you also do one of the following:
|
||||
如果您希望将木兰宽松许可证,第2版,应用到您的新软件,为了方便接收者查阅,建议您完成如下三步:
|
||||
|
||||
a) Accompany it with the complete corresponding machine-readable
|
||||
source code, which must be distributed under the terms of Sections
|
||||
1 and 2 above on a medium customarily used for software interchange; or,
|
||||
1, 请您补充如下声明中的空白,包括软件名、软件的首次发表年份以及您作为版权人的名字;
|
||||
|
||||
b) Accompany it with a written offer, valid for at least three
|
||||
years, to give any third party, for a charge no more than your
|
||||
cost of physically performing source distribution, a complete
|
||||
machine-readable copy of the corresponding source code, to be
|
||||
distributed under the terms of Sections 1 and 2 above on a medium
|
||||
customarily used for software interchange; or,
|
||||
2, 请您在软件包的一级目录下创建以“LICENSE”为名的文件,将整个许可证文本放入该文件中;
|
||||
|
||||
c) Accompany it with the information you received as to the offer
|
||||
to distribute corresponding source code. (This alternative is
|
||||
allowed only for noncommercial distribution and only if you
|
||||
received the program in object code or executable form with such
|
||||
an offer, in accord with Subsection b above.)
|
||||
3, 请将如下声明文本放入每个源文件的头部注释中。
|
||||
|
||||
The source code for a work means the preferred form of the work for
|
||||
making modifications to it. For an executable work, complete source
|
||||
code means all the source code for all modules it contains, plus any
|
||||
associated interface definition files, plus the scripts used to
|
||||
control compilation and installation of the executable. However, as a
|
||||
special exception, the source code distributed need not include
|
||||
anything that is normally distributed (in either source or binary
|
||||
form) with the major components (compiler, kernel, and so on) of the
|
||||
operating system on which the executable runs, unless that component
|
||||
itself accompanies the executable.
|
||||
Copyright (c) [Year] [name of copyright holder]
|
||||
[Software Name] is licensed under Mulan PSL v2.
|
||||
You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
You may obtain a copy of Mulan PSL v2 at:
|
||||
http://license.coscl.org.cn/MulanPSL2
|
||||
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
See the Mulan PSL v2 for more details.
|
||||
|
||||
If distribution of executable or object code is made by offering
|
||||
access to copy from a designated place, then offering equivalent
|
||||
access to copy the source code from the same place counts as
|
||||
distribution of the source code, even though third parties are not
|
||||
compelled to copy the source along with the object code.
|
||||
|
||||
4. You may not copy, modify, sublicense, or distribute the Program
|
||||
except as expressly provided under this License. Any attempt
|
||||
otherwise to copy, modify, sublicense or distribute the Program is
|
||||
void, and will automatically terminate your rights under this License.
|
||||
However, parties who have received copies, or rights, from you under
|
||||
this License will not have their licenses terminated so long as such
|
||||
parties remain in full compliance.
|
||||
Mulan Permissive Software License,Version 2
|
||||
|
||||
5. You are not required to accept this License, since you have not
|
||||
signed it. However, nothing else grants you permission to modify or
|
||||
distribute the Program or its derivative works. These actions are
|
||||
prohibited by law if you do not accept this License. Therefore, by
|
||||
modifying or distributing the Program (or any work based on the
|
||||
Program), you indicate your acceptance of this License to do so, and
|
||||
all its terms and conditions for copying, distributing or modifying
|
||||
the Program or works based on it.
|
||||
Mulan Permissive Software License,Version 2 (Mulan PSL v2)
|
||||
January 2020 http://license.coscl.org.cn/MulanPSL2
|
||||
|
||||
6. Each time you redistribute the Program (or any work based on the
|
||||
Program), the recipient automatically receives a license from the
|
||||
original licensor to copy, distribute or modify the Program subject to
|
||||
these terms and conditions. You may not impose any further
|
||||
restrictions on the recipients' exercise of the rights granted herein.
|
||||
You are not responsible for enforcing compliance by third parties to
|
||||
this License.
|
||||
Your reproduction, use, modification and distribution of the Software shall be subject to Mulan PSL v2 (this License) with the following terms and conditions:
|
||||
|
||||
7. If, as a consequence of a court judgment or allegation of patent
|
||||
infringement or for any other reason (not limited to patent issues),
|
||||
conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot
|
||||
distribute so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you
|
||||
may not distribute the Program at all. For example, if a patent
|
||||
license would not permit royalty-free redistribution of the Program by
|
||||
all those who receive copies directly or indirectly through you, then
|
||||
the only way you could satisfy both it and this License would be to
|
||||
refrain entirely from distribution of the Program.
|
||||
0. Definition
|
||||
|
||||
If any portion of this section is held invalid or unenforceable under
|
||||
any particular circumstance, the balance of the section is intended to
|
||||
apply and the section as a whole is intended to apply in other
|
||||
circumstances.
|
||||
Software means the program and related documents which are licensed under this License and comprise all Contribution(s).
|
||||
|
||||
It is not the purpose of this section to induce you to infringe any
|
||||
patents or other property right claims or to contest validity of any
|
||||
such claims; this section has the sole purpose of protecting the
|
||||
integrity of the free software distribution system, which is
|
||||
implemented by public license practices. Many people have made
|
||||
generous contributions to the wide range of software distributed
|
||||
through that system in reliance on consistent application of that
|
||||
system; it is up to the author/donor to decide if he or she is willing
|
||||
to distribute software through any other system and a licensee cannot
|
||||
impose that choice.
|
||||
Contribution means the copyrightable work licensed by a particular Contributor under this License.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to
|
||||
be a consequence of the rest of this License.
|
||||
Contributor means the Individual or Legal Entity who licenses its copyrightable work under this License.
|
||||
|
||||
8. If the distribution and/or use of the Program is restricted in
|
||||
certain countries either by patents or by copyrighted interfaces, the
|
||||
original copyright holder who places the Program under this License
|
||||
may add an explicit geographical distribution limitation excluding
|
||||
those countries, so that distribution is permitted only in or among
|
||||
countries not thus excluded. In such case, this License incorporates
|
||||
the limitation as if written in the body of this License.
|
||||
Legal Entity means the entity making a Contribution and all its Affiliates.
|
||||
|
||||
9. The Free Software Foundation may publish revised and/or new versions
|
||||
of the General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
Affiliates means entities that control, are controlled by, or are under common control with the acting entity under this License, ‘control’ means direct or indirect ownership of at least fifty percent (50%) of the voting power, capital or other securities of controlled or commonly controlled entity.
|
||||
|
||||
Each version is given a distinguishing version number. If the Program
|
||||
specifies a version number of this License which applies to it and "any
|
||||
later version", you have the option of following the terms and conditions
|
||||
either of that version or of any later version published by the Free
|
||||
Software Foundation. If the Program does not specify a version number of
|
||||
this License, you may choose any version ever published by the Free Software
|
||||
Foundation.
|
||||
1. Grant of Copyright License
|
||||
|
||||
10. If you wish to incorporate parts of the Program into other free
|
||||
programs whose distribution conditions are different, write to the author
|
||||
to ask for permission. For software which is copyrighted by the Free
|
||||
Software Foundation, write to the Free Software Foundation; we sometimes
|
||||
make exceptions for this. Our decision will be guided by the two goals
|
||||
of preserving the free status of all derivatives of our free software and
|
||||
of promoting the sharing and reuse of software generally.
|
||||
Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable copyright license to reproduce, use, modify, or distribute its Contribution, with modification or not.
|
||||
|
||||
NO WARRANTY
|
||||
2. Grant of Patent License
|
||||
|
||||
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
||||
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
||||
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
||||
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
||||
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
||||
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
||||
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
||||
REPAIR OR CORRECTION.
|
||||
Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable (except for revocation under this Section) patent license to make, have made, use, offer for sale, sell, import or otherwise transfer its Contribution, where such patent license is only limited to the patent claims owned or controlled by such Contributor now or in future which will be necessarily infringed by its Contribution alone, or by combination of the Contribution with the Software to which the Contribution was contributed. The patent license shall not apply to any modification of the Contribution, and any other combination which includes the Contribution. If you or your Affiliates directly or indirectly institute patent litigation (including a cross claim or counterclaim in a litigation) or other patent enforcement activities against any individual or entity by alleging that the Software or any Contribution in it infringes patents, then any patent license granted to you under this License for the Software shall terminate as of the date such litigation or activity is filed or taken.
|
||||
|
||||
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
||||
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
||||
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
||||
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
||||
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
||||
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGES.
|
||||
3. No Trademark License
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
No trademark license is granted to use the trade names, trademarks, service marks, or product names of Contributor, except as required to fulfill notice requirements in Section 4.
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
4. Distribution Restriction
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
You may distribute the Software in any medium with or without modification, whether in source or executable forms, provided that you provide recipients with a copy of this License and retain copyright, patent, trademark and disclaimer statements in the Software.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
convey the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
5. Disclaimer of Warranty and Limitation of Liability
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
THE SOFTWARE AND CONTRIBUTION IN IT ARE PROVIDED WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED. IN NO EVENT SHALL ANY CONTRIBUTOR OR COPYRIGHT HOLDER BE LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO ANY DIRECT, OR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING FROM YOUR USE OR INABILITY TO USE THE SOFTWARE OR THE CONTRIBUTION IN IT, NO MATTER HOW IT’S CAUSED OR BASED ON WHICH LEGAL THEORY, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
6. Language
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
THIS LICENSE IS WRITTEN IN BOTH CHINESE AND ENGLISH, AND THE CHINESE VERSION AND ENGLISH VERSION SHALL HAVE THE SAME LEGAL EFFECT. IN THE CASE OF DIVERGENCE BETWEEN THE CHINESE AND ENGLISH VERSIONS, THE CHINESE VERSION SHALL PREVAIL.
|
||||
|
||||
You should have received a copy of the GNU General Public License along
|
||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
END OF THE TERMS AND CONDITIONS
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
How to Apply the Mulan Permissive Software License,Version 2 (Mulan PSL v2) to Your Software
|
||||
|
||||
If the program is interactive, make it output a short notice like this
|
||||
when it starts in an interactive mode:
|
||||
To apply the Mulan PSL v2 to your work, for easy identification by recipients, you are suggested to complete following three steps:
|
||||
|
||||
Gnomovision version 69, Copyright (C) year name of author
|
||||
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
i Fill in the blanks in following statement, including insert your software name, the year of the first publication of your software, and your name identified as the copyright owner;
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, the commands you use may
|
||||
be called something other than `show w' and `show c'; they could even be
|
||||
mouse-clicks or menu items--whatever suits your program.
|
||||
ii Create a file named “LICENSE” which contains the whole context of this License in the first directory of your software package;
|
||||
|
||||
You should also get your employer (if you work as a programmer) or your
|
||||
school, if any, to sign a "copyright disclaimer" for the program, if
|
||||
necessary. Here is a sample; alter the names:
|
||||
iii Attach the statement to the appropriate annotated syntax at the beginning of each source file.
|
||||
|
||||
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
||||
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
||||
|
||||
<signature of Ty Coon>, 1 April 1989
|
||||
Ty Coon, President of Vice
|
||||
|
||||
This General Public License does not permit incorporating your program into
|
||||
proprietary programs. If your program is a subroutine library, you may
|
||||
consider it more useful to permit linking proprietary applications with the
|
||||
library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License.
|
||||
Copyright (c) [Year] [name of copyright holder]
|
||||
[Software Name] is licensed under Mulan PSL v2.
|
||||
You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
You may obtain a copy of Mulan PSL v2 at:
|
||||
http://license.coscl.org.cn/MulanPSL2
|
||||
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
See the Mulan PSL v2 for more details.
|
||||
|
151
build.gradle
151
build.gradle
@ -1,10 +1,23 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'application'
|
||||
id 'java-library'
|
||||
id 'maven-publish'
|
||||
id 'signing'
|
||||
}
|
||||
|
||||
mainClassName = 'org.bdware.sc.ContractProcess'
|
||||
apply from: '../spotless.gradle'
|
||||
|
||||
group = "org.bdware.sc"
|
||||
version = "1.10.5"
|
||||
tasks.withType(JavaCompile) {
|
||||
// options.compilerArgs << '-Xlint:none'
|
||||
// options.compilerArgs << '-Xlint:deprecation' << "-Werror"
|
||||
options.compilerArgs << '-parameters'
|
||||
}
|
||||
repositories {
|
||||
mavenCentral()
|
||||
mavenLocal()
|
||||
}
|
||||
sourceSets {
|
||||
main {
|
||||
java {
|
||||
@ -25,51 +38,165 @@ sourceSets {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation project(":common")
|
||||
implementation project(":mockjava")
|
||||
api project(":common")
|
||||
api project(":mockjava")
|
||||
implementation 'org.bdware:delta-crdts:1.2.0'
|
||||
implementation 'org.apache.commons:commons-lang3:3.0'
|
||||
implementation 'com.atlassian.commonmark:commonmark:0.17.0'
|
||||
implementation 'com.idealista:format-preserving-encryption:1.0.0'
|
||||
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
|
||||
implementation 'com.sun.mail:javax.mail:1.6.2'
|
||||
implementation 'org.apache.commons:commons-math3:3.6.1'
|
||||
implementation 'org.codehaus.groovy:groovy-all:3.0.8'
|
||||
implementation 'io.grpc:grpc-all:1.64.0'
|
||||
implementation 'org.jsoup:jsoup:1.14.2'
|
||||
implementation 'org.apache.httpcomponents:httpclient:4.5.13'
|
||||
implementation 'com.sun.mail:javax.mail:1.6.2'
|
||||
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
|
||||
implementation 'org.bdware.bdcontract:sdk-java:1.0.2'
|
||||
implementation 'org.bdware.doip:doip-audit-tool:1.5.9'
|
||||
implementation 'org.bdware.doip:doip-sdk:1.5.9'
|
||||
implementation 'org.apache.logging.log4j:log4j-layout-template-json:2.17.2'
|
||||
implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.3'
|
||||
implementation fileTree(dir: 'lib', include: '*.jar')
|
||||
implementation 'io.grpc:grpc-all:1.41.0'
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
}
|
||||
tasks.jar.setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)
|
||||
task classJar(type: Jar, dependsOn: classes) {
|
||||
classifier = "jar"
|
||||
}
|
||||
|
||||
jar {
|
||||
String libs = ''
|
||||
configurations.runtimeClasspath.each {
|
||||
libs = libs + " libs/" + it.name
|
||||
}
|
||||
from {
|
||||
// uncomment this when publish,
|
||||
// while develop at local use "false"
|
||||
configurations.runtimeClasspath.filter {
|
||||
// it.getAbsolutePath().contains("/lib/")
|
||||
false
|
||||
}.collect {
|
||||
it.isDirectory() ? it : zipTree(it)
|
||||
}
|
||||
}
|
||||
|
||||
manifest {
|
||||
attributes 'Manifest-Version': project.version
|
||||
attributes 'Main-Class': mainClassName
|
||||
attributes 'Main-Class': 'org.bdware.sc.ContractProcess'
|
||||
attributes 'Class-Path': libs
|
||||
}
|
||||
}
|
||||
|
||||
tasks.processResources.setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)
|
||||
tasks.processTestResources.setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)
|
||||
tasks.withType(Copy).all {
|
||||
duplicatesStrategy = DuplicatesStrategy.INCLUDE
|
||||
}
|
||||
tasks.withType(Jar).all {
|
||||
duplicatesStrategy = DuplicatesStrategy.INCLUDE
|
||||
}
|
||||
|
||||
|
||||
task copyLibs(type: Copy, dependsOn: ":common:jar") {
|
||||
into "./build/output/libs/"
|
||||
from configurations.runtimeClasspath
|
||||
}
|
||||
|
||||
task copyJar(type: Copy, dependsOn: ":cp:jar") {
|
||||
task copyJar(type: Copy, dependsOn: [":cp:jar", ":cp:copyLog4jProp"]) {
|
||||
into "./build/output/"
|
||||
from "./build/libs/$project.name-${version}.jar"
|
||||
rename { String fileName -> "yjs.jar" }
|
||||
}
|
||||
task copyLog4jProp(type: Copy) {
|
||||
into "./build/output/"
|
||||
from "./src/main/resources/log4j2.debug.properties"
|
||||
from "./src/main/resources/log4j2.properties"
|
||||
}
|
||||
|
||||
task buildBundle(dependsOn: [":cp:copyLibs", ":cp:copyJar"]) {
|
||||
doLast {
|
||||
println "buildBundle in ./build/output/ successfully"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
task sourceJar(type: Jar, dependsOn: classes) {
|
||||
archiveClassifier = "sources"
|
||||
classifier = "sources"
|
||||
from sourceSets.main.allSource
|
||||
}
|
||||
tasks.withType(Javadoc) {
|
||||
options.addStringOption('Xdoclint:none', '-quiet')
|
||||
}
|
||||
|
||||
task javadocJar(type: Jar, dependsOn: javadoc) {
|
||||
archiveClassifier = 'javadoc'
|
||||
classifier = "javadoc"
|
||||
exclude { details -> details.file.getAbsolutePath().contains("/gm/")
|
||||
}
|
||||
from javadoc.destinationDir
|
||||
}
|
||||
//shadowJar {
|
||||
// // classifier = "jar"
|
||||
// dependencies {
|
||||
// include(dependency(fileTree(dir: 'lib', include: '*.jar')))
|
||||
//// exclude(dependency('com.google.code.gson:gson:2.8.6'))
|
||||
//// relocate 'com.google.code.gson', 'irs.com.google.code.gson'
|
||||
// }
|
||||
//}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
mavenJava(MavenPublication) {
|
||||
groupId project.group
|
||||
artifactId "cp"
|
||||
version "${version}"
|
||||
from components.java
|
||||
artifact sourceJar
|
||||
artifact javadocJar
|
||||
artifact classJar
|
||||
// artifact customFatJar
|
||||
pom {
|
||||
name = "bdware-cp"
|
||||
description = "cp"
|
||||
url = "https://gitee.com/BDWare/cp"
|
||||
licenses {
|
||||
license {
|
||||
name = "GPL v2"
|
||||
url = "https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt"
|
||||
}
|
||||
}
|
||||
developers {
|
||||
developer {
|
||||
id = "dataware"
|
||||
email = "caihq@pku.edu.cn"
|
||||
}
|
||||
}
|
||||
scm {
|
||||
connection = "scm:git:https://gitee.com/BDWare/cp.git"
|
||||
developerConnection = "scm:git:https://gitee.com/BDWare/cp.git"
|
||||
url = "https://gitee.com/BDWare/cp"
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
repositories {
|
||||
maven {
|
||||
name 'bdwareSnapshotRepository'
|
||||
url 'https://oss.sonatype.org/content/repositories/snapshots'
|
||||
credentials {
|
||||
username = "${NEXUS_USERNAME}"
|
||||
password = "${NEXUS_PASSWORD}"
|
||||
}
|
||||
}
|
||||
maven {
|
||||
name 'bdwareRepository'
|
||||
url 'https://oss.sonatype.org/service/local/staging/deploy/maven2'
|
||||
credentials {
|
||||
username = "${NEXUS_USERNAME}"
|
||||
password = "${NEXUS_PASSWORD}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
signing {
|
||||
sign publishing.publications.mavenJava
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
{
|
||||
"agentHttpAddr": "127.0.0.1:18000",
|
||||
"script": "/Users/huaqiancai/BDWare/datanet/datanet-gateway-bundle/datanet-gateway-backend/build/gateway.ypk",
|
||||
"pubKey": "04d1924329f72ced148f6f333fb985ccbaa31b1e3aacf10be5f43d4a4ff5ad88899a005e79e37fc06993e1d66ada8cf8b711cb36f59538bb7d3e39e70fa9360ddd",
|
||||
"privKey": "589d94ee5688358a1c5c18430dd9c75097ddddebf769f139da36a807911d20f8"
|
||||
"agentAddress": "127.0.0.1:18000",
|
||||
"ypkPath": "/Users/huaqiancai/BDWare/datanet/datanet-gateway-bundle/datanet-gateway-backend/build/gateway.ypk",
|
||||
"privateKey": "04d1924329f72ced148f6f333fb985ccbaa31b1e3aacf10be5f43d4a4ff5ad88899a005e79e37fc06993e1d66ada8cf8b711cb36f59538bb7d3e39e70fa9360ddd",
|
||||
"publicKey": "589d94ee5688358a1c5c18430dd9c75097ddddebf769f139da36a807911d20f8",
|
||||
"killBeforeStart":"ContractName",
|
||||
"createParam":{}
|
||||
}
|
BIN
lib/bdledger-java-sdk-dev-201104.2ce17584.jar.back
Normal file
BIN
lib/bdledger-java-sdk-dev-201104.2ce17584.jar.back
Normal file
Binary file not shown.
BIN
lib/bdledger-java-sdk-dev-221113-all.jar.backup
Normal file
BIN
lib/bdledger-java-sdk-dev-221113-all.jar.backup
Normal file
Binary file not shown.
BIN
lib/bdledger-java-sdk-dev-250321.jar
Normal file
BIN
lib/bdledger-java-sdk-dev-250321.jar
Normal file
Binary file not shown.
@ -16,8 +16,6 @@ import com.alibaba.datax.plugin.rdbms.util.DBUtil;
|
||||
import com.alibaba.datax.plugin.rdbms.util.DBUtilErrorCode;
|
||||
import com.alibaba.datax.plugin.rdbms.util.DataBaseType;
|
||||
import com.alibaba.datax.plugin.rdbms.util.RdbmsException;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
@ -56,8 +54,10 @@ public class MaskingJob {
|
||||
jdbcUrl = readerPara.getString(Key.JDBC_URL);
|
||||
table = readerPara.getString(Key.TABLE);
|
||||
buffer = new ArrayList<>();
|
||||
System.out.println("maskConf11"+maskConf.getConfiguration(CoreConstant.DATAX_JOB_CONTENT + "[0]"));
|
||||
transformerExecs = TransformerUtil.buildTransformerInfo(maskConf.getConfiguration(CoreConstant.DATAX_JOB_CONTENT + "[0]"));
|
||||
System.out.println(
|
||||
"maskConf11" + maskConf.getConfiguration(CoreConstant.DATAX_JOB_CONTENT + "[0]"));
|
||||
transformerExecs = TransformerUtil.buildTransformerInfo(
|
||||
maskConf.getConfiguration(CoreConstant.DATAX_JOB_CONTENT + "[0]"));
|
||||
}
|
||||
|
||||
public String buildQuerySql() {
|
||||
@ -65,7 +65,8 @@ public class MaskingJob {
|
||||
// String column = readerPara.getString(Key.COLUMN);
|
||||
String table = readerPara.getString(Key.TABLE);
|
||||
String where = readerPara.getString(Key.WHERE, null);
|
||||
//String querySql = SingleTableSplitUtil.buildQuerySql(column, table, where) + " limit 100";
|
||||
// String querySql = SingleTableSplitUtil.buildQuerySql(column, table, where) + " limit
|
||||
// 100";
|
||||
String querySql = SingleTableSplitUtil.buildQuerySql(column, table, where) + " limit 100";
|
||||
|
||||
return querySql;
|
||||
@ -113,8 +114,7 @@ public class MaskingJob {
|
||||
return new JsonPrimitive(res);
|
||||
}
|
||||
|
||||
private Record transportOneRecord(ResultSet rs, ResultSetMetaData metaData,
|
||||
int columnNumber) {
|
||||
private Record transportOneRecord(ResultSet rs, ResultSetMetaData metaData, int columnNumber) {
|
||||
Record record = buildRecord(rs, metaData, columnNumber);
|
||||
sendToWriter(record);
|
||||
return record;
|
||||
@ -138,8 +138,8 @@ public class MaskingJob {
|
||||
return record;
|
||||
}
|
||||
|
||||
ClassLoaderSwapper classLoaderSwapper = ClassLoaderSwapper
|
||||
.newCurrentThreadClassLoaderSwapper();
|
||||
ClassLoaderSwapper classLoaderSwapper =
|
||||
ClassLoaderSwapper.newCurrentThreadClassLoaderSwapper();
|
||||
|
||||
Record result = record;
|
||||
|
||||
@ -148,17 +148,19 @@ public class MaskingJob {
|
||||
for (TransformerExecution transformerInfoExec : transformerExecs) {
|
||||
System.out.println("transformerExecs" + transformerInfoExec.getTransformerName());
|
||||
if (transformerInfoExec.getClassLoader() != null) {
|
||||
classLoaderSwapper.setCurrentThreadClassLoader(transformerInfoExec.getClassLoader());
|
||||
classLoaderSwapper
|
||||
.setCurrentThreadClassLoader(transformerInfoExec.getClassLoader());
|
||||
}
|
||||
|
||||
/**
|
||||
* 延迟检查transformer参数的有效性,直接抛出异常,不作为脏数据
|
||||
* 不需要在插件中检查参数的有效性。但参数的个数等和插件相关的参数,在插件内部检查
|
||||
* 延迟检查transformer参数的有效性,直接抛出异常,不作为脏数据 不需要在插件中检查参数的有效性。但参数的个数等和插件相关的参数,在插件内部检查
|
||||
*/
|
||||
if (!transformerInfoExec.isChecked()) {
|
||||
|
||||
if (transformerInfoExec.getColumnIndex() != null && transformerInfoExec.getColumnIndex() >= record.getColumnNumber()) {
|
||||
throw DataXException.asDataXException(TransformerErrorCode.TRANSFORMER_ILLEGAL_PARAMETER,
|
||||
if (transformerInfoExec.getColumnIndex() != null
|
||||
&& transformerInfoExec.getColumnIndex() >= record.getColumnNumber()) {
|
||||
throw DataXException.asDataXException(
|
||||
TransformerErrorCode.TRANSFORMER_ILLEGAL_PARAMETER,
|
||||
String.format("columnIndex[%s] out of bound[%s]. name=%s",
|
||||
transformerInfoExec.getColumnIndex(), record.getColumnNumber(),
|
||||
transformerInfoExec.getTransformerName()));
|
||||
@ -167,10 +169,11 @@ public class MaskingJob {
|
||||
}
|
||||
|
||||
try {
|
||||
result = transformerInfoExec.getTransformer().evaluate(result, transformerInfoExec.gettContext(), transformerInfoExec.getFinalParas());
|
||||
result = transformerInfoExec.getTransformer().evaluate(result,
|
||||
transformerInfoExec.gettContext(), transformerInfoExec.getFinalParas());
|
||||
} catch (Exception e) {
|
||||
errorMsg = String.format("transformer(%s) has Exception(%s)", transformerInfoExec.getTransformerName(),
|
||||
e.getMessage());
|
||||
errorMsg = String.format("transformer(%s) has Exception(%s)",
|
||||
transformerInfoExec.getTransformerName(), e.getMessage());
|
||||
failed = true;
|
||||
// LOG.error(errorMsg, e);
|
||||
// transformerInfoExec.addFailedRecords(1);
|
||||
@ -199,8 +202,7 @@ public class MaskingJob {
|
||||
|
||||
}
|
||||
|
||||
protected Record buildRecord(ResultSet rs, ResultSetMetaData metaData,
|
||||
int columnNumber) {
|
||||
protected Record buildRecord(ResultSet rs, ResultSetMetaData metaData, int columnNumber) {
|
||||
final byte[] EMPTY_CHAR_ARRAY = new byte[0];
|
||||
Record record = new DefaultRecord();
|
||||
try {
|
||||
@ -276,13 +278,10 @@ public class MaskingJob {
|
||||
break;
|
||||
|
||||
default:
|
||||
throw DataXException
|
||||
.asDataXException(
|
||||
DBUtilErrorCode.UNSUPPORTED_TYPE,
|
||||
throw DataXException.asDataXException(DBUtilErrorCode.UNSUPPORTED_TYPE,
|
||||
String.format(
|
||||
"您的配置文件中的列配置信息有误. 因为DataX 不支持数据库读取这种字段类型. 字段名:[%s], 字段名称:[%s], 字段Java类型:[%s]. 请尝试使用数据库函数将其转换datax支持的类型 或者不同步该字段 .",
|
||||
metaData.getColumnName(i),
|
||||
metaData.getColumnType(i),
|
||||
metaData.getColumnName(i), metaData.getColumnType(i),
|
||||
metaData.getColumnClassName(i)));
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,10 @@
|
||||
package org.bdware.sc.debugger;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import org.apache.log4j.LogManager;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.client.SmartContractClient;
|
||||
import org.bdware.sc.ContractProcess;
|
||||
import org.bdware.sc.bean.Contract;
|
||||
import org.bdware.sc.bean.ContractExecType;
|
||||
@ -11,50 +13,104 @@ import org.bdware.sc.get.GetMessage;
|
||||
import org.bdware.sc.http.HttpUtil;
|
||||
import org.bdware.sc.util.FileUtil;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
import org.zz.gmhelper.SM2KeyPair;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class DebugMain {
|
||||
static Logger LOGGER = LogManager.getLogger(DebugMain.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
String content = FileUtil.getFileContent("./debugconf.json");
|
||||
public static void runWithConf(String configPath) {
|
||||
String content = FileUtil.getFileContent(configPath);
|
||||
DebugConfig config = JsonUtil.fromJson(content, DebugConfig.class);
|
||||
inject(config);
|
||||
ContractProcess.main(new String[]{"-port=" + config.port, "-cmi=" + config.cmi, "-disablePID"});
|
||||
ResultCallback printCallback
|
||||
= new ResultCallback() {
|
||||
String keyPairStr = "{\"publicKey\":\"%s\",\"privateKey\":\"%s\"}";
|
||||
SM2KeyPair pair =
|
||||
SM2KeyPair.fromJson(String.format(keyPairStr, config.publicKey, config.privateKey));
|
||||
String uriFormat = "ws://%s/SCIDE/SCExecutor";
|
||||
if (config.killBeforeStart != null && config.killBeforeStart.length() > 0) {
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
|
||||
SmartContractClient client =
|
||||
new SmartContractClient(String.format(uriFormat, config.agentAddress), pair) {
|
||||
public void onLogin(JsonObject obj) {
|
||||
counter.incrementAndGet();
|
||||
}
|
||||
|
||||
public void onKillContractProcess(JsonObject obj) {
|
||||
counter.incrementAndGet();
|
||||
}
|
||||
|
||||
};
|
||||
client.waitForConnect();
|
||||
client.login();
|
||||
try {
|
||||
for (; counter.get() == 0;)
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
client.sendMsg("{\"action\":\"killContractProcess\",\"name\":\""
|
||||
+ config.killBeforeStart + "\"}");
|
||||
try {
|
||||
for (; counter.get() == 1;)
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
config.contract.setCreateParam(config.createParam);
|
||||
ContractProcess
|
||||
.main(new String[] {"-port=" + config.port, "-cmi=" + config.cmi, "-disablePID"});
|
||||
ResultCallback printCallback = new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
LOGGER.info(str);
|
||||
if (str.contains("Error")) {
|
||||
LOGGER.error("Some error happens: " + str);
|
||||
}
|
||||
LOGGER.info("[PrintCB] " + str);
|
||||
}
|
||||
};
|
||||
ContractProcess.instance.handler.setDBInfo(wrap("", config.dbPath), printCallback);
|
||||
ContractProcess.instance.handler.registerMangerPort(wrap("", Integer.valueOf(config.cPort)), printCallback);
|
||||
ContractProcess.instance.handler.setContractBundle(wrap("", config.contract), printCallback);
|
||||
String urlFormat = ("http://%s/SCIDE/SCManager?action=reconnectAll&owner=%s");
|
||||
ContractProcess.instance.handler.registerMangerPort(wrap("", Integer.valueOf(config.cPort)),
|
||||
printCallback);
|
||||
|
||||
String url = String.format(urlFormat, config.agentHttpAddr, config.pubKey);
|
||||
ContractProcess.instance.handler.setContractBundle(wrap("", config.contract),
|
||||
printCallback);
|
||||
|
||||
String urlFormat = "http://%s/SCIDE/SCManager?action=reconnectPort&owner=%s&port=%d%s";
|
||||
String cpHost = "";
|
||||
if (config.cpHost != null && config.cpHost.length() > 0)
|
||||
cpHost = "&host=" + config.cpHost;
|
||||
String url = String.format(urlFormat, config.agentAddress, config.publicKey,
|
||||
ContractProcess.instance.server.mainPort.get(), cpHost);
|
||||
Map<String, Object> resp = HttpUtil.httpGet(url);
|
||||
|
||||
String data = (String) resp.get("response");
|
||||
LOGGER.info(JsonUtil.toPrettyJson(JsonUtil.parseStringAsJsonObject(data)));
|
||||
LOGGER.info("start done!");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
runWithConf("./debugconf.json");
|
||||
}
|
||||
|
||||
private static void inject(DebugConfig config) {
|
||||
String urlFormat = ("http://%s/SCIDE/SCManager?action=%s&arg=%s");
|
||||
String url = String.format(urlFormat, config.agentHttpAddr, "getAgentConfig", "");
|
||||
String url = String.format(urlFormat, config.agentAddress, "getAgentConfig", "");
|
||||
Map<String, Object> resp = HttpUtil.httpGet(url);
|
||||
String data = (String
|
||||
) resp.get("response");
|
||||
String data = (String) resp.get("response");
|
||||
assert (int) resp.get("responseCode") == 200;
|
||||
JsonObject jsonObject = JsonUtil.parseStringAsJsonObject(data);
|
||||
config.cmi = jsonObject.get("cmi").getAsString();
|
||||
config.dbPath = jsonObject.get("dbPath").getAsString();
|
||||
config.cPort = jsonObject.get("cPort").getAsInt();
|
||||
config.port = jsonObject.get("port").getAsInt();
|
||||
JsonObject ownerAndScript = new JsonObject();
|
||||
String arg = "abc&owner=" + config.pubKey + "&script=" + config.script;
|
||||
url = String.format(urlFormat, config.agentHttpAddr, "allocateKeyPair", arg);
|
||||
String arg = "abc&owner=" + config.publicKey + "&script=" + config.ypkPath
|
||||
+ "&doipStartPort=" + config.doipStartPort;
|
||||
url = String.format(urlFormat, config.agentAddress, "allocateKeyPair", arg);
|
||||
resp = HttpUtil.httpGet(url);
|
||||
LOGGER.info(url);
|
||||
String contractStr = (String) resp.get("response");
|
||||
@ -76,12 +132,15 @@ public class DebugMain {
|
||||
}
|
||||
|
||||
static class DebugConfig {
|
||||
String script;
|
||||
String agentHttpAddr;
|
||||
String pubKey;
|
||||
String privKey;
|
||||
String agentAddress;
|
||||
public JsonElement createParam;
|
||||
String publicKey;
|
||||
String privateKey;
|
||||
String killBeforeStart;
|
||||
String ypkPath;
|
||||
String cpHost;
|
||||
int doipStartPort;
|
||||
// AutoAppend
|
||||
|
||||
int port;
|
||||
String cmi;
|
||||
String dbPath;
|
||||
|
@ -1,8 +1,6 @@
|
||||
package org.bdware.sc;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import com.google.gson.*;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.google.gson.stream.MalformedJsonException;
|
||||
import org.apache.logging.log4j.Level;
|
||||
@ -18,11 +16,11 @@ import org.bdware.analysis.gas.PPCount;
|
||||
import org.bdware.analysis.taint.TaintBB;
|
||||
import org.bdware.analysis.taint.TaintCFG;
|
||||
import org.bdware.analysis.taint.TaintResult;
|
||||
import org.bdware.doip.audit.EndpointConfig;
|
||||
import org.bdware.sc.ContractResult.Status;
|
||||
import org.bdware.sc.bean.*;
|
||||
import org.bdware.sc.boundry.JavaScriptEntry;
|
||||
import org.bdware.sc.boundry.Resources;
|
||||
import org.bdware.sc.boundry.utils.FileUtil;
|
||||
import org.bdware.sc.boundry.utils.RocksDBUtil;
|
||||
import org.bdware.sc.boundry.utils.UtilRegistry;
|
||||
import org.bdware.sc.compiler.YJSCompiler;
|
||||
@ -30,21 +28,28 @@ import org.bdware.sc.conn.ByteUtil;
|
||||
import org.bdware.sc.conn.ServiceServer;
|
||||
import org.bdware.sc.conn.SocketGet;
|
||||
import org.bdware.sc.engine.DesktopEngine;
|
||||
import org.bdware.sc.engine.JSONTool;
|
||||
import org.bdware.sc.engine.hook.*;
|
||||
import org.bdware.sc.handler.ContractHandler;
|
||||
import org.bdware.sc.handler.DOOPRequestHandler;
|
||||
import org.bdware.sc.index.TimeSerialIndex;
|
||||
import org.bdware.sc.node.*;
|
||||
import org.bdware.sc.server.DoipClusterServer;
|
||||
import org.bdware.sc.trace.ProgramPointCounter;
|
||||
import org.bdware.sc.util.FileUtil;
|
||||
import org.bdware.sc.util.HashUtil;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
import org.objectweb.asm.ClassReader;
|
||||
import org.objectweb.asm.tree.ClassNode;
|
||||
import org.objectweb.asm.tree.MethodNode;
|
||||
import org.zz.gmhelper.SM2KeyPair;
|
||||
import org.zz.gmhelper.SM2Util;
|
||||
|
||||
import javax.script.ScriptContext;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptException;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
@ -78,6 +83,7 @@ public class ContractProcess {
|
||||
private TimeSerialIndex logIndex;
|
||||
private RocksDBUtil edion;
|
||||
private String pid;
|
||||
public DOOPRequestHandler doopRequestHandler;
|
||||
|
||||
public ContractProcess(int port, String cmi) {
|
||||
handler = new ContractHandler(this);
|
||||
@ -107,15 +113,15 @@ public class ContractProcess {
|
||||
Scanner sc = new Scanner(pidInput);
|
||||
for (String str; sc.hasNextLine();) {
|
||||
str = sc.nextLine();
|
||||
System.out.println("[CP From STDIN] " + str);
|
||||
LOGGER.info("[CP From STDIN] " + str);
|
||||
if (str.contains("CP PID:")) {
|
||||
int pid = Integer.parseInt(str.replace("CP PID:", ""));
|
||||
System.setProperty("io.netty.processId", pid + "");
|
||||
System.out.println("[CP SET PID DONE] " + str);
|
||||
LOGGER.info("[CP SET PID DONE] " + str);
|
||||
break;
|
||||
}
|
||||
}
|
||||
System.out.println("[Create CP]");
|
||||
LOGGER.info("[Create CP]");
|
||||
instance = new ContractProcess(port, cmi);
|
||||
}
|
||||
|
||||
@ -133,8 +139,7 @@ public class ContractProcess {
|
||||
byte[] buffer = new byte[4];
|
||||
int length = input.read(buffer, 0, 4);
|
||||
if (length == 4) {
|
||||
isArchive =
|
||||
(Arrays.equals(ZIP_HEADER_1, buffer))
|
||||
isArchive = (Arrays.equals(ZIP_HEADER_1, buffer))
|
||||
|| (Arrays.equals(ZIP_HEADER_2, buffer));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
@ -190,9 +195,8 @@ public class ContractProcess {
|
||||
|
||||
cn = czb.mergeContractNode();
|
||||
} else {
|
||||
cn =
|
||||
compiler.compile(
|
||||
new ByteArrayInputStream(script.getBytes()), "contract_main.yjs");
|
||||
cn = compiler.compile(new ByteArrayInputStream(script.getBytes()),
|
||||
"contract_main.yjs");
|
||||
}
|
||||
DesktopEngine engine = new DesktopEngine(); // engine.loadJar(zf);
|
||||
engine.loadContract(c, cn, ret.isInsnLimit);
|
||||
@ -212,8 +216,8 @@ public class ContractProcess {
|
||||
|
||||
MethodNode mn = methods.get(fn.functionName);
|
||||
if (mn != null) {
|
||||
System.out.println(
|
||||
"[ContractManager] getMethodNode, verify:" + fn.functionName);
|
||||
System.out
|
||||
.println("[ContractManager] getMethodNode, verify:" + fn.functionName);
|
||||
TaintResult.nLocals = mn.maxLocals;
|
||||
TaintResult.nStack = mn.maxStack;
|
||||
TaintCFG cfg = new TaintCFG(mn);
|
||||
@ -274,17 +278,12 @@ public class ContractProcess {
|
||||
* CFGraph cfg = new CFGraph(mn) {
|
||||
*
|
||||
* @Override public BasicBlock getBasicBlock(int id) { return new
|
||||
* BasicBlock(id); } }; FrontCF frontCF = new FrontCF(graph); String[]
|
||||
data =
|
||||
* fn.plainText().split("\n"); for (int i = 0; i <
|
||||
graph.getBasicBlockSize();
|
||||
* i++) { BasicBlock bb = graph.getBasicBlockAt(i); String decompiled =
|
||||
""; if
|
||||
* BasicBlock(id); } }; FrontCF frontCF = new FrontCF(graph); String[] data =
|
||||
* fn.plainText().split("\n"); for (int i = 0; i < graph.getBasicBlockSize();
|
||||
* i++) { BasicBlock bb = graph.getBasicBlockAt(i); String decompiled = ""; if
|
||||
* (bb.lineNum - 1 < data.length && bb.lineNum > 0) { decompiled =
|
||||
* data[bb.lineNum - 1]; } frontCF.addBB(bb, decompiled); Set<BasicBlock>
|
||||
suc =
|
||||
* graph.getSucBlocks(bb); for (BasicBlock sucBB : suc)
|
||||
frontCF.addEdge(bb,
|
||||
* data[bb.lineNum - 1]; } frontCF.addBB(bb, decompiled); Set<BasicBlock> suc =
|
||||
* graph.getSucBlocks(bb); for (BasicBlock sucBB : suc) frontCF.addEdge(bb,
|
||||
* sucBB); }
|
||||
*/
|
||||
TaintResult.nLocals = mn.maxLocals;
|
||||
@ -307,7 +306,8 @@ public class ContractProcess {
|
||||
List<Integer> ids = map.get(i);
|
||||
frontCF.addBB(bb, decompiled, ids, cfg);
|
||||
Set<BasicBlock> suc = cfg.getSucBlocks(bb);
|
||||
for (BasicBlock sucBB : suc) frontCF.addEdge(bb, sucBB);
|
||||
for (BasicBlock sucBB : suc)
|
||||
frontCF.addEdge(bb, sucBB);
|
||||
}
|
||||
// get result
|
||||
// TaintBB lastBlock = cfg.getLastBlock();
|
||||
@ -400,10 +400,8 @@ public class ContractProcess {
|
||||
String yancloud_desktop = "";
|
||||
isOpen.put(pmList[0], pmList[1]);
|
||||
yancloud_desktop += UtilRegistry.getInitStr(pmList[0], pmList[1].equals("open"));
|
||||
engine.getNashornEngine()
|
||||
.getContext()
|
||||
.setAttribute(
|
||||
ScriptEngine.FILENAME, yancloud_desktop, ScriptContext.ENGINE_SCOPE);
|
||||
engine.getNashornEngine().getContext().setAttribute(ScriptEngine.FILENAME,
|
||||
yancloud_desktop, ScriptContext.ENGINE_SCOPE);
|
||||
engine.getNashornEngine().eval(yancloud_desktop);
|
||||
} catch (ScriptException e) {
|
||||
e.printStackTrace();
|
||||
@ -425,24 +423,19 @@ public class ContractProcess {
|
||||
// 判断是否满足Oracle和Contact的执行要求
|
||||
public String verifyOracleAndContractPermission(Contract contract) {
|
||||
// 权限校验 如果是Oracle 启动方式只能是Sole 否则报错
|
||||
if (cn.getYjsType() == YjsType.Oracle && contract.getType() != ContractExecType.Sole) {
|
||||
if (cn.getYjsType() == YjsType.Oracle && contract.getType() != ContractExecType.Sole
|
||||
&& contract.getType() != ContractExecType.Sharding) {
|
||||
LOGGER.info("Oracle only support Sole ContractType!");
|
||||
return JsonUtil.toJson(
|
||||
new ContractResult(
|
||||
Status.Error,
|
||||
return JsonUtil.toJson(new ContractResult(Status.Error,
|
||||
new JsonPrimitive("Oracle only support Sole ContractType!")));
|
||||
}
|
||||
// 权限校验 如果是contract 申请了MySQL等权限 报错
|
||||
if (cn.getYjsType() == YjsType.Contract) {
|
||||
for (Permission per : cn.getPermission()) {
|
||||
if (per == Permission.SQL
|
||||
|| per == Permission.Http
|
||||
|| per == Permission.RocksDB
|
||||
if (per == Permission.SQL || per == Permission.Http || per == Permission.RocksDB
|
||||
|| per == Permission.MongoDB) {
|
||||
LOGGER.debug("Contract can not have permissions of IO!");
|
||||
return JsonUtil.toJson(
|
||||
new ContractResult(
|
||||
Status.Error,
|
||||
return JsonUtil.toJson(new ContractResult(Status.Error,
|
||||
new JsonPrimitive("Contract can not have permissions of IO|")));
|
||||
}
|
||||
}
|
||||
@ -450,54 +443,49 @@ public class ContractProcess {
|
||||
return "";
|
||||
}
|
||||
|
||||
public String setMembers(List<String> members) {
|
||||
JavaScriptEntry.members = members;
|
||||
if (members != null)
|
||||
return members.size() + "";
|
||||
else
|
||||
return "0";
|
||||
}
|
||||
|
||||
public String setContractBundle(Contract contract) {
|
||||
try {
|
||||
fixContractIfNecessary(contract);
|
||||
// long start = System.currentTimeMillis();
|
||||
// long start0 = start;
|
||||
this.contract = contract;
|
||||
|
||||
JavaScriptEntry.random = new Random();
|
||||
JavaScriptEntry.invokeID = 0L;
|
||||
JavaScriptEntry.random.setSeed(Integer.parseInt(contract.getID()));
|
||||
JavaScriptEntry.numOfCopies = this.contract.getNumOfCopies();
|
||||
JavaScriptEntry.shardingID =
|
||||
this.contract.getShardingId(); // 设置javaScriptEntry中的shardingID
|
||||
JavaScriptEntry.shardingID = this.contract.getShardingId(); // 设置javaScriptEntry中的shardingID
|
||||
// JavaScriptEntry
|
||||
// contract.getCreateParam().get("repoId").getAsString()-->去获取repoId。
|
||||
// 知道自己的BDOID --> repo的id。+contract.getID()
|
||||
String zipPath = contract.getScriptStr();
|
||||
if (isArchiveFile(new File(zipPath))) {
|
||||
ZipFile zf = new ZipFile(zipPath);
|
||||
ContractZipBundle zipBundle = new YJSCompiler().compile(zf);
|
||||
cn = zipBundle.mergeContractNode();
|
||||
// check functionNodes
|
||||
List<FunctionNode> functionNodes = cn.getFunctions();
|
||||
LOGGER.debug(
|
||||
"functionNodes jointInfo: "
|
||||
+ JsonUtil.toPrettyJson(
|
||||
functionNodes.stream()
|
||||
.map(
|
||||
x -> {
|
||||
JoinInfo joinInfo = x.getJoinInfo();
|
||||
return null == joinInfo
|
||||
? "null"
|
||||
: joinInfo;
|
||||
})
|
||||
.toArray()));
|
||||
injectHandlers();
|
||||
|
||||
this.contract.setYjsType(cn.getYjsType());
|
||||
memorySet = cn.memorySet;
|
||||
this.contract.sourcePath = zipBundle.getManifest().sourcePath;
|
||||
|
||||
LOGGER.debug(
|
||||
"check sourcePath\n\tin-contract="
|
||||
+ this.contract.sourcePath
|
||||
+ "\n\tin-manifest="
|
||||
+ zipBundle.getManifest().sourcePath);
|
||||
LOGGER.debug("check sourcePath\n\tin-contract=" + this.contract.sourcePath
|
||||
+ "\n\tin-manifest=" + zipBundle.getManifest().sourcePath);
|
||||
// zhanghongwei
|
||||
|
||||
/* if (ret.getManifest().getInsnLimit() != 0) {
|
||||
gasLimit=ret.getManifest().getInsnLimit();
|
||||
isInsnLimit = true;
|
||||
}*/
|
||||
/*
|
||||
* if (ret.getManifest().getInsnLimit() != 0) {
|
||||
* gasLimit=ret.getManifest().getInsnLimit(); isInsnLimit = true; }
|
||||
*/
|
||||
String ver = verifyOracleAndContractPermission(contract);
|
||||
if (!ver.isEmpty()) {
|
||||
return ver;
|
||||
@ -508,38 +496,52 @@ public class ContractProcess {
|
||||
}
|
||||
|
||||
handleLog();
|
||||
|
||||
// System.out.println("[ret.getManifest().getInsnLimit()]" +
|
||||
// ret.getManifest().getInsnLimit());
|
||||
|
||||
engine = new DesktopEngine(zipBundle.getManifest(), zipPath, contract);
|
||||
engine.loadJar(zf);
|
||||
engine.registerResource(new Resources(zf, engine.getClassLoad()));
|
||||
String retStr =
|
||||
JsonUtil.toJson(
|
||||
engine.loadContract(contract, cn, cn.getInstrumentBranch()));
|
||||
invokeOnCreate();
|
||||
LOGGER.debug("result: " + retStr);
|
||||
return retStr;
|
||||
ContractResult result = engine.loadContract(contract, cn, cn.getInstrumentBranch());
|
||||
JsonObject jo = new JsonObject();
|
||||
ContractResult onCreate = invokeOnCreate(contract.getCreateParam());
|
||||
jo.add("onCreate", JsonUtil.parseObject(onCreate));
|
||||
jo.add("loadContract", JsonUtil.parseObject(result));
|
||||
jo.addProperty("status", result.status.merge(onCreate.status).toString());
|
||||
LOGGER.debug("result: " + jo.toString());
|
||||
// doipModule的话,拉起DoipServer服务端口
|
||||
if (cn.hasDoipModule()) {
|
||||
// 只有一台机器去更新Router中的repoInfo就可以了
|
||||
updateRepoInfo(contract.getCreateParam());
|
||||
invokeOnStartingDoipServer(cn, contract.getCreateParam(), jo);
|
||||
}
|
||||
return jo.toString();
|
||||
} else {
|
||||
contract.setScript(FileUtil.getContent(zipPath));
|
||||
contract.setScript(FileUtil.getFileContent(zipPath));
|
||||
return setContract(contract);
|
||||
}
|
||||
|
||||
} catch (MalformedJsonException | JsonSyntaxException e) {
|
||||
return JsonUtil.toJson(
|
||||
new ContractResult(
|
||||
Status.Error,
|
||||
return JsonUtil.toJson(new ContractResult(Status.Error,
|
||||
new JsonPrimitive("parse manifest.json error, not json format!")));
|
||||
} catch (Exception e) {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
e.printStackTrace(new PrintStream(bo));
|
||||
return JsonUtil.toJson(
|
||||
new ContractResult(Status.Error, new JsonPrimitive(bo.toString())));
|
||||
return JsonUtil
|
||||
.toJson(new ContractResult(Status.Error, new JsonPrimitive(bo.toString())));
|
||||
}
|
||||
}
|
||||
|
||||
private void injectHandlers() {
|
||||
private void fixContractIfNecessary(Contract contract) {
|
||||
if (contract.getID() == null)
|
||||
contract.setID(contract.getScriptStr().hashCode() + "");
|
||||
if (contract.getPublicKey() == null) {
|
||||
SM2KeyPair pair = SM2Util.generateSM2KeyPair();
|
||||
contract.setKey(pair.getPrivateKeyStr());
|
||||
contract.setPublicKey(pair.getPublicKeyStr());
|
||||
}
|
||||
}
|
||||
|
||||
private void injectHandlers() throws Exception {
|
||||
// 正式启动
|
||||
if (!this.contract.isDebug()) {
|
||||
// this.engine.getResources().loadAsString("/maskConfig.json");
|
||||
@ -552,12 +554,31 @@ public class ContractProcess {
|
||||
}
|
||||
}
|
||||
}
|
||||
DOOPBeforeExecHandler doopBeforeExecHandler = null;
|
||||
DOOPAfterExecHandler doopAfterExecHandler = null;
|
||||
doopRequestHandler = null;
|
||||
for (FunctionNode fun : cn.getFunctions()) {
|
||||
if (fun.isConfidential()) {
|
||||
fun.appendBeforeInvokeHandler(new ConfidentialHandler(fun));
|
||||
}
|
||||
ArgSchemaHandler argSchemaHandler = createHandlerIfExist(fun, ArgSchemaHandler.class);
|
||||
if (argSchemaHandler != null) {
|
||||
fun.appendBeforeInvokeHandler(argSchemaHandler);
|
||||
}
|
||||
|
||||
|
||||
if (fun.isExport()) {
|
||||
// if(fun.annotations...)
|
||||
AccessHandler accessHandler = createHandlerIfExist(fun, AccessHandler.class);
|
||||
if (accessHandler != null) {
|
||||
fun.appendBeforeInvokeHandler(accessHandler);
|
||||
}
|
||||
fun.appendAfterInvokeHandler(new ObjToJsonHandler());
|
||||
ResultSchemaHandler resultSchemaHandler =
|
||||
createHandlerIfExist(fun, ResultSchemaHandler.class);
|
||||
if (resultSchemaHandler != null) {
|
||||
fun.appendAfterInvokeHandler(resultSchemaHandler);
|
||||
}
|
||||
// fun.appendBeforeInvokeHandler(new ReadMeHandler());
|
||||
// Mask是用于返回真正结果之后,做一些偏移,以保护数据隐私。
|
||||
// if (fun.isMask()) {
|
||||
@ -566,7 +587,7 @@ public class ContractProcess {
|
||||
// engine.getResources().loadAsString("/maskConfig.json");
|
||||
// System.out.println("injectMask"+maskConfig);
|
||||
// System.out.println("injectMask"+this.contract.Mask);
|
||||
fun.appendAfterInvokeHandler(new MaskHandler());
|
||||
// fun.appendAfterInvokeHandler(new MaskHandler());
|
||||
// }
|
||||
}
|
||||
if (fun.isHomomorphicEncrypt()) {
|
||||
@ -576,7 +597,36 @@ public class ContractProcess {
|
||||
if (fun.isHomomorphicDecrypt()) {
|
||||
fun.appendAfterInvokeHandler(new HomomorphicDecryptHandler(fun));
|
||||
}
|
||||
|
||||
if (fun.isDoipOperation()) {
|
||||
if (doopRequestHandler == null) {
|
||||
doopRequestHandler = new DOOPRequestHandler();
|
||||
}
|
||||
fun.appendBeforeInvokeHandler(
|
||||
new DOOPBeforeExecHandler(fun.getDoipOperationInfo().operation));
|
||||
fun.appendAfterInvokeHandler(
|
||||
new DOOPAfterExecHandler(fun.getDoipOperationInfo().operation));
|
||||
doopRequestHandler.addDoipOperation(fun);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
<T extends AnnotationHook> T createHandlerIfExist(FunctionNode function, Class<T> clz) {
|
||||
YJSAnnotation annotation = clz.getAnnotation(YJSAnnotation.class);
|
||||
if (annotation == null)
|
||||
return null;
|
||||
try {
|
||||
AnnotationNode node = function.getAnnotation(annotation.name());
|
||||
if (node == null)
|
||||
return null;
|
||||
Method m = clz.getDeclaredMethod("fromAnnotationNode", FunctionNode.class,
|
||||
AnnotationNode.class);
|
||||
T result = (T) m.invoke(null, function, node);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String changeDumpPeriod(String period) {
|
||||
@ -592,8 +642,12 @@ public class ContractProcess {
|
||||
|
||||
public String setContract(Contract contract) {
|
||||
try {
|
||||
fixContractIfNecessary(contract);
|
||||
JavaScriptEntry.random = new Random();
|
||||
JavaScriptEntry.invokeID = 0L;
|
||||
if (contract.getID() == null)
|
||||
JavaScriptEntry.random.setSeed(contract.getScriptStr().hashCode());
|
||||
else
|
||||
JavaScriptEntry.random.setSeed(Integer.parseInt(contract.getID()));
|
||||
JavaScriptEntry.numOfCopies = contract.getNumOfCopies();
|
||||
// TODO Optimize, 4 seconds takes to create an Engine.
|
||||
@ -610,16 +664,106 @@ public class ContractProcess {
|
||||
return ver;
|
||||
}
|
||||
handleLog();
|
||||
String ret =
|
||||
JsonUtil.toJson(engine.loadContract(contract, cn, cn.getInstrumentBranch()));
|
||||
invokeOnCreate();
|
||||
LOGGER.info("load script, contract:" + JsonUtil.toJson(contract.getScriptStr()));
|
||||
LOGGER.info("load cn:" + JsonUtil.toJson(cn));
|
||||
ContractResult ret = engine.loadContract(contract, cn, cn.getInstrumentBranch());
|
||||
ContractResult onCreate = invokeOnCreate(contract.getCreateParam());
|
||||
JsonObject jo = new JsonObject();
|
||||
jo.add("onCreate", JsonUtil.parseObject(onCreate));
|
||||
jo.add("loadContract", JsonUtil.parseObject(ret));
|
||||
jo.addProperty("status", ret.status.merge(onCreate.status).toString());
|
||||
LOGGER.debug("result: " + jo.toString());
|
||||
|
||||
return ret;
|
||||
// doipModule的话,拉起DoipServer服务端口
|
||||
if (cn.getYjsType() == YjsType.DoipModule) {
|
||||
updateRepoInfo(contract.getCreateParam());
|
||||
invokeOnStartingDoipServer(cn, contract.getCreateParam(), jo);
|
||||
}
|
||||
|
||||
return jo.toString();
|
||||
} catch (Exception e) {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
e.printStackTrace(new PrintStream(bo));
|
||||
return JsonUtil.toJson(
|
||||
new ContractResult(Status.Error, new JsonPrimitive(bo.toString())));
|
||||
return JsonUtil
|
||||
.toJson(new ContractResult(Status.Error, new JsonPrimitive(bo.toString())));
|
||||
}
|
||||
}
|
||||
|
||||
public void updateRepoInfo(JsonElement arg) throws Exception {
|
||||
// 只有0号节点需要初始化IRP连接去updateRepoInfo
|
||||
// just disable this part
|
||||
if (JavaScriptEntry.shardingID == -1) {
|
||||
// DOOP relevant logic
|
||||
DoipClusterServer server = DoipClusterServer.getDOOPServerInstance();
|
||||
if (server == null) {
|
||||
JsonObject createParams = arg.getAsJsonObject();
|
||||
if (createParams.has("router")) {
|
||||
JsonElement routerInfo = createParams.get("router");
|
||||
if (!routerInfo.isJsonObject())
|
||||
throw new Exception(
|
||||
"Provide wrong router info in create params to DoipModule");
|
||||
else {
|
||||
EndpointConfig endpointConfig = JsonUtil.GSON
|
||||
.fromJson(routerInfo.getAsJsonObject(), EndpointConfig.class);
|
||||
DoipClusterServer.createDOOPServerInstance(endpointConfig);
|
||||
}
|
||||
} else {
|
||||
throw new Exception("DoipModule should provide router info in create params");
|
||||
}
|
||||
|
||||
server = DoipClusterServer.getDOOPServerInstance();
|
||||
}
|
||||
// 只有一台机器去更新Router中的repoInfo就可以了
|
||||
|
||||
server.updateRepoInfo(contract, cn);
|
||||
}
|
||||
}
|
||||
|
||||
public void invokeOnStartingDoipServer(ContractNode cn, JsonElement arg,
|
||||
JsonObject returnValue) {
|
||||
ContractRequest onStartingDoipServer = new ContractRequest();
|
||||
onStartingDoipServer.setAction("onServerStart");
|
||||
if (arg == null) {
|
||||
if (engine != null && engine.getManifest() != null
|
||||
&& engine.getManifest().createParam != null)
|
||||
arg = engine.getManifest().createParam;
|
||||
else
|
||||
arg = new JsonPrimitive("");
|
||||
}
|
||||
onStartingDoipServer.setArg(arg);
|
||||
LOGGER.debug(
|
||||
"invoke onStartingDoipServer, param:" + onStartingDoipServer.getArg().toString());
|
||||
onStartingDoipServer.setRequester(contract.getOwner());
|
||||
FunctionNode funNode = cn.getFunction("onServerStart");
|
||||
|
||||
try {
|
||||
JsonElement onStartingDoipServerRes = invoke(onStartingDoipServer, funNode).result;
|
||||
returnValue.add("doipModuleStartResult", onStartingDoipServerRes);
|
||||
int startPort = ContractProcess.instance.server.getPort() + 1;
|
||||
if (arg.isJsonObject() && arg.getAsJsonObject().has("doipStartPort")) {
|
||||
startPort = arg.getAsJsonObject().get("doipStartPort").getAsInt();
|
||||
}
|
||||
LOGGER.info("Fetch the onStartingDoipServerRes from router successfully, the result is "
|
||||
+ onStartingDoipServerRes);
|
||||
int doipListenPort =
|
||||
DoipClusterServer.startDoipServer(startPort, onStartingDoipServerRes);
|
||||
returnValue.addProperty("doipListenPort", doipListenPort);
|
||||
this.contract.setDoipPort(doipListenPort);
|
||||
returnValue.addProperty("doipStartPort", startPort);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(
|
||||
"DoipLocalSingleton cannot starts properly, plz check the onServerStart function");
|
||||
e.printStackTrace();
|
||||
}
|
||||
funNode = cn.getFunction("onInitSharableVars");
|
||||
|
||||
if (funNode != null) {
|
||||
ContractRequest requestForInitVar = new ContractRequest();
|
||||
requestForInitVar.setAction("onInitSharableVars");
|
||||
requestForInitVar.setArg(onStartingDoipServer.getArg());
|
||||
requestForInitVar.setRequester(onStartingDoipServer.getRequester());
|
||||
JsonElement onInitSharableVars = invoke(requestForInitVar, funNode).result;
|
||||
returnValue.add("onInitSharableVars", onInitSharableVars);
|
||||
}
|
||||
}
|
||||
|
||||
@ -643,7 +787,8 @@ public class ContractProcess {
|
||||
}
|
||||
}
|
||||
|
||||
if (fun.getLogToBDContract()) detail.append("bdcontract;");
|
||||
if (fun.getLogToBDContract())
|
||||
detail.append("bdcontract;");
|
||||
if (fun.getLogToNamedLedger()) {
|
||||
for (String str : fun.getLedgerNames()) {
|
||||
detail.append("bdledger:").append(str).append(";");
|
||||
@ -666,7 +811,7 @@ public class ContractProcess {
|
||||
return r.totalMemory() - r.freeMemory();
|
||||
}
|
||||
|
||||
private void invokeOnCreate() {
|
||||
private ContractResult invokeOnCreate(JsonElement arg) {
|
||||
long start = System.currentTimeMillis();
|
||||
logIndex = new TimeSerialIndex("./ContractDB/" + cn.getContractName() + ".index");
|
||||
LOGGER.debug("timeSerialIndex: " + (System.currentTimeMillis() - start));
|
||||
@ -681,9 +826,6 @@ public class ContractProcess {
|
||||
}
|
||||
|
||||
JavaScriptEntry.setSM2KeyPair(contract.getPublicKey(), contract.getKey());
|
||||
if (null != contract.getDOI() && !contract.getDOI().isEmpty()) {
|
||||
JavaScriptEntry.doi = contract.getDOI();
|
||||
}
|
||||
if (null != contract.getAuthInfoPersistDOI()
|
||||
&& !contract.getAuthInfoPersistDOI().isEmpty()) {
|
||||
JavaScriptEntry.authInfoPersistDOI = contract.getAuthInfoPersistDOI();
|
||||
@ -691,50 +833,46 @@ public class ContractProcess {
|
||||
JavaScriptEntry.isDebug = contract.isDebug();
|
||||
ContractRequest onCreate = new ContractRequest();
|
||||
onCreate.setAction("onCreate");
|
||||
onCreate.setArg("null");
|
||||
onCreate.setRequester(contract.getOwner());
|
||||
if (contract.getDoipFlag() && null != contract.getDOI() && !contract.getDOI().isEmpty()) {
|
||||
onCreate.setRequesterDOI(contract.getDOI());
|
||||
} else {
|
||||
onCreate.setRequesterDOI("empty");
|
||||
if (arg == null) {
|
||||
if (engine != null && engine.getManifest() != null
|
||||
&& engine.getManifest().createParam != null)
|
||||
arg = engine.getManifest().createParam;
|
||||
else
|
||||
arg = new JsonPrimitive("");
|
||||
}
|
||||
onCreate.setArg(arg);
|
||||
LOGGER.debug("invoke onCreate, param:" + onCreate.getArg().toString());
|
||||
onCreate.setRequester(contract.getOwner());
|
||||
FunctionNode funNode = cn.getFunction("onCreate");
|
||||
invoke(start, onCreate, funNode);
|
||||
return invoke(onCreate, funNode);
|
||||
}
|
||||
|
||||
private void invokeOnRecover() {
|
||||
long start = System.currentTimeMillis();
|
||||
public void resetContractName(String name) {
|
||||
if (name != null)
|
||||
cn.resetContractName(name);
|
||||
}
|
||||
|
||||
private ContractResult invokeOnRecover(JsonElement arg) {
|
||||
ContractRequest onRecover = new ContractRequest();
|
||||
onRecover.setAction("onRecover");
|
||||
if (arg == null)
|
||||
onRecover.setArg("null");
|
||||
else
|
||||
onRecover.setArg(arg);
|
||||
onRecover.setRequester(contract.getOwner());
|
||||
if (contract.getDoipFlag()
|
||||
&& (contract.getDOI() != null)
|
||||
&& (contract.getDOI().length() > 0)) {
|
||||
onRecover.setRequesterDOI(contract.getDOI());
|
||||
} else {
|
||||
onRecover.setRequesterDOI("empty");
|
||||
}
|
||||
FunctionNode funNode = cn.getFunction("onRecover");
|
||||
invoke(start, onRecover, funNode);
|
||||
return invoke(onRecover, funNode);
|
||||
}
|
||||
|
||||
private void invoke(long start, ContractRequest onRecover, FunctionNode funNode) {
|
||||
// start = System.currentTimeMillis();
|
||||
private ContractResult invoke(ContractRequest onRecover, FunctionNode funNode) {
|
||||
if (funNode != null) {
|
||||
LOGGER.debug(
|
||||
"getFunction:" + (System.currentTimeMillis() - start) + " " + funNode.functionName);
|
||||
long start1 = System.currentTimeMillis();
|
||||
funNode.setIsExport(true);
|
||||
engine.executeContract(onRecover);
|
||||
LOGGER.debug("executeOnCreate:" + (System.currentTimeMillis() - start1));
|
||||
start1 = System.currentTimeMillis();
|
||||
ContractResult result = engine.executeContract(onRecover);
|
||||
funNode.setIsExport(false);
|
||||
engine.getTracePS().clean();
|
||||
LOGGER.debug("clearTrace:" + (System.currentTimeMillis() - start1));
|
||||
// start1 = System.currentTimeMillis();
|
||||
|
||||
return result;
|
||||
}
|
||||
return new ContractResult(Status.Success, new JsonPrimitive("no funNode found"));
|
||||
}
|
||||
|
||||
// public String executeBundle(ContractZipBundle czb, String arg) {
|
||||
@ -770,8 +908,7 @@ public class ContractProcess {
|
||||
List<Long> hashes = logIndex.request(offset, size);
|
||||
List<Map<String, String>> jo = new ArrayList<>();
|
||||
|
||||
TypeToken<Map<String, String>> token = new TypeToken<Map<String, String>>() {
|
||||
};
|
||||
TypeToken<Map<String, String>> token = new TypeToken<Map<String, String>>() {};
|
||||
for (Long hash : hashes)
|
||||
try {
|
||||
Map<String, String> obj =
|
||||
@ -786,8 +923,7 @@ public class ContractProcess {
|
||||
public String requestLast(int count) {
|
||||
List<Long> hashes = logIndex.requestLast(count);
|
||||
List<Map<String, String>> jo = new ArrayList<>();
|
||||
TypeToken<Map<String, String>> token = new TypeToken<Map<String, String>>() {
|
||||
};
|
||||
TypeToken<Map<String, String>> token = new TypeToken<Map<String, String>>() {};
|
||||
String log;
|
||||
for (Long hash : hashes)
|
||||
try {
|
||||
@ -797,8 +933,8 @@ public class ContractProcess {
|
||||
}
|
||||
Map<String, String> obj = JsonUtil.fromJson(log, token.getType());
|
||||
if (obj == null) {
|
||||
System.out.println(
|
||||
"[ContractProcess] requestLast, parseJsonError:" + log + "==");
|
||||
System.out
|
||||
.println("[ContractProcess] requestLast, parseJsonError:" + log + "==");
|
||||
continue;
|
||||
}
|
||||
obj.put("hash", hash + "");
|
||||
@ -813,6 +949,25 @@ public class ContractProcess {
|
||||
return logIndex.size();
|
||||
}
|
||||
|
||||
public String executeFunctionWithoutLimit(String arg) {
|
||||
try {
|
||||
JsonObject body = JsonUtil.parseString(arg).getAsJsonObject();
|
||||
String funcName = body.get("funcName").getAsString();
|
||||
JsonArray arr = body.getAsJsonArray("funcArgs");
|
||||
Object[] funcArgs = new Object[arr.size()];
|
||||
for (int i = 0; i < arr.size(); i++) {
|
||||
funcArgs[i] = JSONTool.convertJsonElementToMirror(arr.get(i));
|
||||
}
|
||||
Object result = engine.invokeFunction(funcName, funcArgs);
|
||||
result = JSONTool.convertMirrorToJson(result);
|
||||
return JsonUtil.toJson(result);
|
||||
} catch (Exception e) {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
e.printStackTrace(new PrintStream(bo));
|
||||
return bo.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public String executeContract(String arg) {
|
||||
// TODO
|
||||
// eventCenter.pub(new EventMsg("executeContract", arg));
|
||||
@ -822,13 +977,13 @@ public class ContractProcess {
|
||||
try {
|
||||
request = JsonUtil.fromJson(arg, ContractRequest.class);
|
||||
} catch (Exception ignored) {
|
||||
result =
|
||||
new ContractResult(
|
||||
ContractResult.Status.Error, new JsonPrimitive("Illegal Arguments!"));
|
||||
result = new ContractResult(ContractResult.Status.Error,
|
||||
new JsonPrimitive("Illegal Arguments!"));
|
||||
return JsonUtil.toJson(result);
|
||||
}
|
||||
String reqID = request.getRequestID();
|
||||
if (cachedRequests.contains(reqID)) {
|
||||
LOGGER.info("[Hit Cache]:" + reqID);
|
||||
try {
|
||||
String cachedResult = edion.get(reqID);
|
||||
if (cachedResult != null && !cachedResult.isEmpty()) {
|
||||
@ -844,9 +999,9 @@ public class ContractProcess {
|
||||
engine.redirectTracePS(new Logger(bo, this));
|
||||
result = engine.executeContract(request);
|
||||
result.analysis = bo.toString();
|
||||
System.out.println(
|
||||
"[ContractProcess] result.analysis = "
|
||||
+ result.analysis); // 动态分析bug null pointer
|
||||
System.out.println("[ContractProcess] result.analysis = " + result.analysis); // 动态分析bug
|
||||
// null
|
||||
// pointer
|
||||
// branchResult = JsonUtil.toJson(result);
|
||||
// branchTrace = result.analysis;
|
||||
engine.redirectTracePS(previous);
|
||||
@ -865,23 +1020,11 @@ public class ContractProcess {
|
||||
evaluatesAnalysis(request.getAction());
|
||||
}
|
||||
|
||||
engine.redirectTracePS(
|
||||
new ProgramPointCounter(
|
||||
bo,
|
||||
this,
|
||||
gasLimit,
|
||||
functionIndex,
|
||||
request.getValue(),
|
||||
0L,
|
||||
request.getAction(),
|
||||
ppCountMap));
|
||||
engine.redirectTracePS(new ProgramPointCounter(bo, this, gasLimit, functionIndex,
|
||||
request.getValue(), 0L, request.getAction(), ppCountMap));
|
||||
result = engine.executeContract(request);
|
||||
|
||||
result.analysis = bo.toString();
|
||||
// System.out.println(
|
||||
// "[withEvaluatesAnalysis ContractProcess] result.analysis =
|
||||
// "
|
||||
// + result.analysis);
|
||||
engine.redirectTracePS(previous);
|
||||
|
||||
} else {
|
||||
@ -890,15 +1033,12 @@ public class ContractProcess {
|
||||
result = engine.executeContract(request);
|
||||
engine.redirectTracePS(previous);
|
||||
}
|
||||
//
|
||||
|
||||
// System.out.println("res" + result.result);
|
||||
// result.addProperty("result",maskResult.getMaskResult(maskConf,
|
||||
// ret.get("result")).toString());
|
||||
|
||||
String ret = JsonUtil.toJson(result);
|
||||
if (reqID != null && reqID.endsWith("_mul")) {
|
||||
cachedRequests.add(reqID);
|
||||
edion.put(reqID, ret);
|
||||
}
|
||||
return ret;
|
||||
} catch (Exception e) {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
@ -908,7 +1048,6 @@ public class ContractProcess {
|
||||
}
|
||||
|
||||
public String evaluatesAnalysis(String getFunction) {
|
||||
// System.out.println("当前的function:" + getFunction);
|
||||
Map<String, byte[]> clzs = engine.dumpClass();
|
||||
Map<String, MethodNode> methods = new HashMap<>();
|
||||
for (byte[] clz : clzs.values()) {
|
||||
@ -923,8 +1062,7 @@ public class ContractProcess {
|
||||
for (String s : function) {
|
||||
MethodNode mn = methods.get(s);
|
||||
if (mn != null) {
|
||||
CFGraph cfg =
|
||||
new CFGraph(mn) {
|
||||
CFGraph cfg = new CFGraph(mn) {
|
||||
@Override
|
||||
public BasicBlock getBasicBlock(int id) {
|
||||
return new BasicBlock(id);
|
||||
@ -991,10 +1129,7 @@ public class ContractProcess {
|
||||
|
||||
TaintBB bb = cfg.getLastBlock();
|
||||
result.analysis = bb.getResultWithTaintBit();
|
||||
System.out.println(
|
||||
"[ContractProcess] dynamically verify: "
|
||||
+ ac.getAction()
|
||||
+ "-->"
|
||||
System.out.println("[ContractProcess] dynamically verify: " + ac.getAction() + "-->"
|
||||
+ result.analysis);
|
||||
}
|
||||
}
|
||||
@ -1017,8 +1152,7 @@ public class ContractProcess {
|
||||
return !JavaScriptEntry.topic_handlers.isEmpty();
|
||||
}
|
||||
|
||||
public void beforeSuicide() {
|
||||
}
|
||||
public void beforeSuicide() {}
|
||||
|
||||
public String redo(String path) {
|
||||
return engine.syncUtil.redo(path);
|
||||
@ -1034,7 +1168,7 @@ public class ContractProcess {
|
||||
|
||||
public String loadMemoryDump(String path) {
|
||||
String str = engine.syncUtil.loadMemoryDump(path, contract.getStateful());
|
||||
invokeOnRecover();
|
||||
invokeOnRecover(contract.getCreateParam());
|
||||
return str;
|
||||
}
|
||||
|
||||
@ -1073,8 +1207,7 @@ public class ContractProcess {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() {
|
||||
}
|
||||
protected void finalize() {}
|
||||
|
||||
public String getDeclaredEvents() {
|
||||
return JsonUtil.toJson(cn.events);
|
||||
@ -1089,16 +1222,16 @@ public class ContractProcess {
|
||||
for (FunctionNode fn : cn.getFunctions()) {
|
||||
if (fn.isExport() && !fn.functionName.equals("onCreate")) {
|
||||
function.add(fn.functionName);
|
||||
FunctionDesp desp =
|
||||
new FunctionDesp(fn.functionName, fn.annotations, fn.getRouteInfo(), fn.getJoinInfo(), fn.isView());
|
||||
FunctionDesp desp = new FunctionDesp(fn.functionName, fn.annotations,
|
||||
fn.getRouteInfo(), fn.getJoinInfo(), fn.isView());
|
||||
ret.add(desp);
|
||||
}
|
||||
}
|
||||
return JsonUtil.toJson(ret);
|
||||
}
|
||||
|
||||
public String getContract() {
|
||||
return JsonUtil.toJson(contract);
|
||||
public Contract getContract() {
|
||||
return contract;
|
||||
}
|
||||
|
||||
public String getPID() {
|
||||
@ -1111,11 +1244,8 @@ public class ContractProcess {
|
||||
|
||||
public String startAutoDump() {
|
||||
String dumpPeriod = projectConfig.getDumpPeriod();
|
||||
System.out.println(
|
||||
"[ContractProcess] startAutoDump : "
|
||||
+ cn.getContractName()
|
||||
+ " period = "
|
||||
+ dumpPeriod);
|
||||
System.out.println("[ContractProcess] startAutoDump : " + cn.getContractName()
|
||||
+ " period = " + dumpPeriod);
|
||||
String status = "startAutoDump status 0";
|
||||
if (null != dt) {
|
||||
if (null == dumpPeriod || dumpPeriod.isEmpty()) {
|
||||
@ -1296,8 +1426,7 @@ public class ContractProcess {
|
||||
private class DumpTask extends TimerTask {
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println(
|
||||
"[ContractProcess DumpTask] auto dump period : "
|
||||
System.out.println("[ContractProcess DumpTask] auto dump period : "
|
||||
+ projectConfig.getDumpPeriod());
|
||||
File file1 = new File(dir);
|
||||
File file2 = new File(file1.getParent());
|
||||
|
77
src/main/java/org/bdware/sc/SM2Helper.java
Normal file
77
src/main/java/org/bdware/sc/SM2Helper.java
Normal file
@ -0,0 +1,77 @@
|
||||
package org.bdware.sc;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.bdware.sc.util.FileUtil;
|
||||
import org.zz.gmhelper.SM2KeyPair;
|
||||
import org.zz.gmhelper.SM2Util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
public class SM2Helper {
|
||||
public static void main(String[] args) {
|
||||
|
||||
SM2KeyPair pair = SM2Util.generateSM2KeyPair(new SecureRandom());
|
||||
try {
|
||||
if (args.length > 0 && args[0].equals("generateKeyToFile")) {
|
||||
backupFile(new File("./manager.keypair"));
|
||||
backupFile(new File("./manager.key"));
|
||||
System.out.println("Generate file: manger.key, manager.keypair");
|
||||
FileOutputStream fout = new FileOutputStream("./manager.keypair");
|
||||
fout.write(pair.toJson().getBytes(StandardCharsets.UTF_8));
|
||||
fout.close();
|
||||
fout = new FileOutputStream("./manager.key");
|
||||
fout.write(pair.getPublicKeyStr().getBytes(StandardCharsets.UTF_8));
|
||||
fout.close();
|
||||
} else if (args.length > 0 && args[0].equals("generateCMConfig")) {
|
||||
backupFile(new File("./cmconfig.json"));
|
||||
System.out.println("Generate file: cmconfig.json");
|
||||
String content = FileUtil.getFileContent("./cmconfig.json.template");
|
||||
JsonObject jo =
|
||||
JsonParser.parseReader(new FileReader("cmvar.json")).getAsJsonObject();
|
||||
for (String key : jo.keySet()) {
|
||||
content = content.replaceAll(key, jo.get(key).getAsString());
|
||||
}
|
||||
JsonObject keypair =
|
||||
JsonParser.parseReader(new FileReader("manager.keypair")).getAsJsonObject();
|
||||
content = content.replaceAll("_PRIVKEY", keypair.get("privateKey").getAsString());
|
||||
content = content.replaceAll("_PUBKEY", keypair.get("publicKey").getAsString());
|
||||
content = content.replaceAll("CMI", System.currentTimeMillis() + "");
|
||||
FileOutputStream fout = new FileOutputStream("./cmconfig.json");
|
||||
fout.write(content.getBytes(StandardCharsets.UTF_8));
|
||||
fout.close();
|
||||
} else
|
||||
System.out.println(pair.toJson());
|
||||
printHelp();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void printHelp() {
|
||||
String usage = "Usage:\n";
|
||||
usage += "java -cp cp/libs/*:cp/yjs.jar org.bdware.sc.SM2Helper generateCMConfig\n";
|
||||
usage += "java -cp cp/libs/*:cp/yjs.jar org.bdware.sc.SM2Helper generateKeyToFile";
|
||||
System.out.println(usage);
|
||||
}
|
||||
|
||||
private static void backupFile(File file) {
|
||||
if (!file.exists())
|
||||
return;
|
||||
File backup = null;
|
||||
for (int i = 0; i <= 100; i++) {
|
||||
if (i == 100)
|
||||
throw new IllegalArgumentException("failed to backup:" + file.getAbsolutePath());
|
||||
backup = new File(file.getParent(), file.getName() + "." + i);
|
||||
if (!backup.exists())
|
||||
break;
|
||||
}
|
||||
FileUtil.copyFile(file, backup);
|
||||
System.out
|
||||
.println("Backup: " + file.getAbsolutePath() + " --> " + backup.getAbsolutePath());
|
||||
}
|
||||
}
|
@ -14,7 +14,6 @@ import org.objectweb.asm.tree.ClassNode;
|
||||
import org.objectweb.asm.tree.MethodNode;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.util.*;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
@ -44,10 +43,7 @@ public class FSAnalysis extends BreadthFirstSearch<TaintResult, TaintBB> {
|
||||
setToAnalysis(toAnalysis);
|
||||
if (isDebug) {
|
||||
System.out.println("===Method:" + cfg.getMethodNode().name + cfg.getMethodNode().desc);
|
||||
System.out.println(
|
||||
"===Local:"
|
||||
+ cfg.getMethodNode().maxLocals
|
||||
+ " "
|
||||
System.out.println("===Local:" + cfg.getMethodNode().maxLocals + " "
|
||||
+ cfg.getMethodNode().maxStack);
|
||||
}
|
||||
}
|
||||
@ -81,9 +77,8 @@ public class FSAnalysis extends BreadthFirstSearch<TaintResult, TaintBB> {
|
||||
ContractZipBundle czb = compiler.compile(zf);
|
||||
cn = czb.mergeContractNode();
|
||||
} else {
|
||||
cn =
|
||||
compiler.compile(
|
||||
new ByteArrayInputStream(script.getBytes()), "contract_main.yjs");
|
||||
cn = compiler.compile(new ByteArrayInputStream(script.getBytes()),
|
||||
"contract_main.yjs");
|
||||
}
|
||||
DesktopEngine engine = new DesktopEngine(); // engine.loadJar(zf);
|
||||
engine.loadContract(c, cn, false);
|
||||
@ -109,7 +104,8 @@ public class FSAnalysis extends BreadthFirstSearch<TaintResult, TaintBB> {
|
||||
FSAnalysis analysis = new FSAnalysis(cfg);
|
||||
analysis.analysis();
|
||||
TaintBB bb = cfg.getLastBlock();
|
||||
if (bb != null) result.put(fn.functionName, bb.getResult());
|
||||
if (bb != null)
|
||||
result.put(fn.functionName, bb.getResult());
|
||||
cfg.printSelf();
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +0,0 @@
|
||||
package org.bdware.sc.blockdb;
|
||||
|
||||
public class Constants {
|
||||
public static final int ELASTIC_DB = 0;
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
package org.bdware.sc.blockdb;
|
||||
|
||||
import org.bdware.sc.commParser.BDLedger.Transaction;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface DBRepository {
|
||||
public String Get(Map<String, Object> condition);//get hash of transaction
|
||||
public boolean Put(Transaction trans);//put transaction into databases
|
||||
public boolean Delete(String hash);//delete transaction
|
||||
public boolean Create_DB();//createDB
|
||||
public boolean Open_DB(); //OpenDB
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
package org.bdware.sc.blockdb;
|
||||
|
||||
public class DBUtil {
|
||||
private static DBRepository instance = null;
|
||||
|
||||
public static DBRepository getInstance(int type) {
|
||||
if (null == instance && type == Constants.ELASTIC_DB) {
|
||||
instance = new ElasticDBUtil();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
@ -1,106 +0,0 @@
|
||||
package org.bdware.sc.blockdb;
|
||||
|
||||
import okhttp3.*;
|
||||
import org.bdware.sc.commParser.BDLedger.Transaction;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ElasticDBUtil implements DBRepository {
|
||||
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
||||
public OkHttpClient client = new OkHttpClient();
|
||||
public List<Transaction> transactions = new ArrayList<>();
|
||||
public int bulk = 10000;
|
||||
|
||||
@Override
|
||||
public String Get(Map<String, Object> condition) {
|
||||
// TODO Auto-generated method stub
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
Map<String, Object> map1 = new HashMap<>();
|
||||
Map<String, String> map2 = new HashMap<>();
|
||||
map2.put("data", condition.get("data") + "*");
|
||||
map1.put("wildcard", map2);
|
||||
map.put("query", map1);
|
||||
String json = JsonUtil.toJson(map);
|
||||
RequestBody requestbody = FormBody.create(json, JSON);
|
||||
Request request = new Request.Builder().url("http://127.0.0.1:9200/transaction/_doc/_search").post(requestbody).build();
|
||||
try {
|
||||
Response resp = client.newCall(request).execute();
|
||||
System.out.print(resp.body().string());
|
||||
resp.body().close();
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean Put(Transaction trans) {
|
||||
// TODO Auto-generated method stub
|
||||
transactions.add(trans);
|
||||
if (transactions.size() == bulk) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
try {
|
||||
for (int i = 0; i < bulk; i++) {
|
||||
sb
|
||||
.append("{\"index\":{}}\n{\"data\":\"")
|
||||
.append(new String(transactions.get(i).data, StandardCharsets.UTF_8))
|
||||
.append("\",")
|
||||
.append("\"hash\":")
|
||||
.append("\"")
|
||||
.append(new String(transactions.get(i).hash, StandardCharsets.UTF_8))
|
||||
.append("\"")
|
||||
.append("}")
|
||||
.append("\n");
|
||||
}
|
||||
//System.out.println(sb.toString());
|
||||
RequestBody requestbody = FormBody.create(sb.toString(), JSON);
|
||||
Request request = new Request.Builder().url("http://127.0.0.1:9200/transaction/_doc/_bulk").post(requestbody).build();
|
||||
Response res1 = client.newCall(request).execute();
|
||||
boolean res = res1.isSuccessful();
|
||||
res1.body().close();
|
||||
System.out.println(res);
|
||||
transactions.clear();
|
||||
return res;
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean Delete(String hash) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean Create_DB() {
|
||||
// TODO Auto-generated method stub
|
||||
RequestBody body = RequestBody.create("", JSON);
|
||||
Request request = new Request.Builder().url("http://127.0.0.1:9200/transaction/").put(body).build();
|
||||
try {
|
||||
Response response = client.newCall(request).execute();
|
||||
boolean res = response.isSuccessful();
|
||||
response.body().close();
|
||||
return res;
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean Open_DB() {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
package org.bdware.sc.blockdb;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MongoDBUtil {
|
||||
public static Object connect(String url, int port, String dbName, String usrName, String pwd) {
|
||||
try {
|
||||
Class serverAddr = Class.forName("com.mongodb.ServerAddress");
|
||||
Constructor cons = serverAddr.getConstructor(String.class, Integer.TYPE);
|
||||
Object serverAddress = cons.newInstance(url, port);
|
||||
List addrs = new ArrayList<>();
|
||||
addrs.add(serverAddress);
|
||||
Method createeScramSha1 =
|
||||
Class.forName("com.mongodb.MongoCredential")
|
||||
.getDeclaredMethod(
|
||||
"createScramSha1Credential",
|
||||
String.class,
|
||||
String.class,
|
||||
char[].class);
|
||||
Object credential = createeScramSha1.invoke(null, usrName, dbName, pwd.toCharArray());
|
||||
List credentials = new ArrayList<>();
|
||||
credentials.add(credential);
|
||||
Constructor mongoClient =
|
||||
Class.forName("com.mongodb.MongoClient").getConstructor(List.class, List.class);
|
||||
Object client = mongoClient.newInstance(addrs, credentials);
|
||||
// 通过连接认证获取MongoDB连接
|
||||
return client;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,72 +0,0 @@
|
||||
package org.bdware.sc.blockdb;
|
||||
|
||||
import org.bdware.sc.commParser.BDLedger.Transaction;
|
||||
import org.rocksdb.Options;
|
||||
import org.rocksdb.RocksDB;
|
||||
import org.rocksdb.RocksDBException;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
|
||||
public class RocksDBUtil implements DBRepository{
|
||||
private static RocksDB rocksdb;
|
||||
static {
|
||||
RocksDB.loadLibrary();
|
||||
}
|
||||
|
||||
public static RocksDB loadDB(String path, String readOnly) {
|
||||
try {
|
||||
Options options = new Options();
|
||||
options.setCreateIfMissing(true);
|
||||
RocksDB rocksDB;
|
||||
File lockFile = new File(path,"LOCK");
|
||||
lockFile.delete();
|
||||
if (readOnly != null && readOnly.equals("true")) {
|
||||
rocksDB = RocksDB.openReadOnly(options, path);
|
||||
} else
|
||||
rocksDB = RocksDB.open(options, path);
|
||||
rocksdb = rocksDB;
|
||||
return rocksDB;
|
||||
|
||||
} catch (RocksDBException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String Get(Map<String, Object> condition) {
|
||||
// TODO Auto-generated method stub
|
||||
if(condition.containsKey("start")&&condition.containsKey("end")) {
|
||||
int start = (int)condition.get("start");
|
||||
int end = (int)condition.get("end");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean Put(Transaction trans) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean Delete(String hash) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean Create_DB() {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean Open_DB() {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@ -1,117 +0,0 @@
|
||||
package org.bdware.sc.blockdb;
|
||||
|
||||
import org.bdware.sc.commParser.BDLedger.Block;
|
||||
import org.bdware.sc.commParser.BDLedger.BlockBody;
|
||||
import org.bdware.sc.commParser.BDLedger.BlockHeader;
|
||||
import org.bdware.sc.util.HashUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.*;
|
||||
|
||||
public class SqliteDBUtil {
|
||||
private Connection conn;
|
||||
|
||||
public static SqliteDBUtil connect(String url) {
|
||||
try {
|
||||
String name = "org.sqlite.JDBC";
|
||||
SqliteDBUtil util = new SqliteDBUtil();
|
||||
String path = "jdbc:sqlite:";
|
||||
File file = new File(url);
|
||||
path = path + file.getAbsolutePath();
|
||||
System.out.println("[SqliteDBUtil] connect:" + path);
|
||||
Class.forName(name);
|
||||
util.conn = DriverManager.getConnection(path);
|
||||
return util;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public BlockBody getBlockBody(String headerHash, String bodyHash) {
|
||||
try {
|
||||
Statement stmt = conn.createStatement();
|
||||
// ResultSet result = stmt.executeQuery("select * from BlockHeader where hash =
|
||||
// " + headerHash);
|
||||
String sql = "select * from BlockBody where ID = ?";
|
||||
PreparedStatement pre = conn.prepareStatement(sql);
|
||||
pre.setBytes(1, HashUtil.str16ToBytes(bodyHash));
|
||||
ResultSet result = pre.executeQuery();
|
||||
// Assert we get only one!!!!
|
||||
while (result.next()) {
|
||||
System.out.println(result.getBytes("ID"));
|
||||
return BlockBody.fromBytes(result.getBytes("Data"));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public Block getBlock(String headerHash, String bodyHash) {
|
||||
try {
|
||||
BlockHeader header = new BlockHeader();
|
||||
BlockBody body = new BlockBody();
|
||||
Statement stmt = conn.createStatement();
|
||||
String sql = "select * from BlockBody where ID = ?";
|
||||
PreparedStatement pre = conn.prepareStatement(sql);
|
||||
pre.setBytes(1, HashUtil.str16ToBytes(bodyHash));
|
||||
ResultSet result = pre.executeQuery();
|
||||
while (result.next()) {
|
||||
body = BlockBody.fromBytes(result.getBytes("Data"));
|
||||
}
|
||||
String sql1 = "select * from BlockHeader where Hash = ?";
|
||||
pre = conn.prepareStatement(sql1);
|
||||
pre.setBytes(1, HashUtil.str16ToBytes(headerHash));
|
||||
result = pre.executeQuery();
|
||||
while (result.next()) {
|
||||
header.index = result.getInt(1);
|
||||
header.hash = result.getBytes(2);
|
||||
header.version = result.getInt(3);
|
||||
header.timestamp = result.getInt(4);
|
||||
header.prevblockID = result.getBytes(5);
|
||||
header.merkleroot = result.getBytes(6);
|
||||
header.creatorID = result.getBytes(7);
|
||||
Block block = new Block(header,body);
|
||||
return block;
|
||||
//return Block.fromBytes(result.getBytes("Data"));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public byte[] bodyselectall() {
|
||||
try {
|
||||
Statement stmt = conn.createStatement();
|
||||
// ResultSet result = stmt.executeQuery("select * from BlockHeader where hash =
|
||||
// " + headerHash);
|
||||
String sql = "select * from BlockBody limit 1,1";
|
||||
PreparedStatement pre = conn.prepareStatement(sql);
|
||||
ResultSet result = pre.executeQuery();
|
||||
// Assert we get only one!!!!
|
||||
while (result.next()) {
|
||||
return result.getBytes(1);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public byte[] headerselectall() {
|
||||
try {
|
||||
Statement stmt = conn.createStatement();
|
||||
// ResultSet result = stmt.executeQuery("select * from BlockHeader where hash =
|
||||
// " + headerHash);
|
||||
String sql = "select * from BlockHeader limit 1,1";
|
||||
PreparedStatement pre = conn.prepareStatement(sql);
|
||||
ResultSet result = pre.executeQuery();
|
||||
// Assert we get only one!!!!
|
||||
while (result.next()) {
|
||||
return result.getBytes(2);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
package org.bdware.sc.blockdb;
|
||||
|
||||
import org.bdware.sc.commParser.BDLedger.Transaction;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class TimeDBUtil implements DBRepository{
|
||||
|
||||
@Override
|
||||
public String Get(Map<String, Object> condition) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean Put(Transaction trans) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean Delete(String hash) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean Create_DB() {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean Open_DB() {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@ -126,7 +126,8 @@ public class AccountIndex {
|
||||
}
|
||||
|
||||
try {
|
||||
LenVarTimeSerialIndex2 index = getIndexFile(getString(args, "account") + getString(args, "file"));
|
||||
LenVarTimeSerialIndex2 index =
|
||||
getIndexFile(getString(args, "account") + getString(args, "file"));
|
||||
long startTime = getLong(args, "startTime");
|
||||
long endTime = getLong(args, "endTime");
|
||||
List<byte[]> result = index.requestByTime(startTime, endTime);
|
||||
@ -183,7 +184,8 @@ public class AccountIndex {
|
||||
}
|
||||
|
||||
try {
|
||||
LenVarTimeSerialIndex2 index = getIndexFile(getString(args, "account") + getString(args, "file"));
|
||||
LenVarTimeSerialIndex2 index =
|
||||
getIndexFile(getString(args, "account") + getString(args, "file"));
|
||||
long date = getLong(args, "date");
|
||||
String content = getString(args, "content");
|
||||
index.manullyIndex(date, HashUtil.str16ToBytes(content));
|
||||
|
@ -1,12 +1,11 @@
|
||||
package org.bdware.sc.boundry;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.sc.ContractProcess;
|
||||
import org.bdware.sc.bean.ContractExecType;
|
||||
import org.bdware.sc.bean.ContractRequest;
|
||||
import org.bdware.sc.boundry.utils.SQLUtil;
|
||||
import org.bdware.sc.conn.ResultCallback;
|
||||
import org.bdware.sc.conn.SocketGet;
|
||||
import org.bdware.sc.engine.DesktopEngine;
|
||||
@ -25,9 +24,6 @@ import wrp.jdk.nashorn.internal.runtime.ScriptFunction;
|
||||
import wrp.jdk.nashorn.internal.runtime.ScriptObject;
|
||||
import wrp.jdk.nashorn.internal.scripts.JO;
|
||||
|
||||
import javax.mail.*;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@ -35,11 +31,13 @@ import java.io.PrintStream;
|
||||
import java.math.BigInteger;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.security.Security;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
@ -54,22 +52,34 @@ public class JavaScriptEntry {
|
||||
// public static int contractManagerPort;
|
||||
public static Random random;
|
||||
public static long invokeID;
|
||||
public static String doi;
|
||||
public static String authInfoPersistDOI;
|
||||
public static SocketGet get; // public static CloseableHttpClient httpClient = getHttpClient();
|
||||
public static int numOfCopies;
|
||||
public static boolean isDebug;
|
||||
public static List<REvent> msgList;
|
||||
public static int shardingID;
|
||||
public static List<String> members;
|
||||
// private static SM2KeyPair keyPair = new SM2().generateKeyPair(); // TODO ?? 本地服务器的,39上运行39的
|
||||
// public static String privKey;
|
||||
// public static String pubKey;
|
||||
private static SM2KeyPair keyPair;
|
||||
|
||||
public static boolean resetContractName(String name) {
|
||||
StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
|
||||
String method = null;
|
||||
if (stacktrace.length > 2) {
|
||||
method = stacktrace[2].getMethodName();
|
||||
}
|
||||
if (method != null && method.equals("onCreate")) {
|
||||
ContractProcess.instance.resetContractName(name);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void setSM2KeyPair(String pubKey, String privKey) {
|
||||
keyPair =
|
||||
new SM2KeyPair(
|
||||
SM2KeyPair.publicKeyStr2ECPoint(pubKey), new BigInteger(privKey, 16));
|
||||
keyPair = new SM2KeyPair(SM2KeyPair.publicKeyStr2ECPoint(pubKey),
|
||||
new BigInteger(privKey, 16));
|
||||
}
|
||||
|
||||
public static SM2KeyPair getKeyPair() {
|
||||
@ -84,25 +94,6 @@ public class JavaScriptEntry {
|
||||
return HashUtil.hashByteArray(hash);
|
||||
}
|
||||
|
||||
public static Connection getMysqlConnection(String url, String usrName, String pwd) {
|
||||
return SQLUtil.getConnection("jdbc:mysql://" + url, usrName, pwd);
|
||||
}
|
||||
|
||||
public static String example(String arg) {
|
||||
LOGGER.debug("called: " + arg);
|
||||
return arg + 1;
|
||||
}
|
||||
|
||||
// public static MongoClient connectMongoDb(String url, int port, String dbName, String usrName,
|
||||
// String pwd) {
|
||||
// return getMongoDBConnection(url, port, dbName, usrName, pwd);
|
||||
// }
|
||||
//
|
||||
// public static MongoClient getMongoDBConnection(String url, int port, String dbName, String
|
||||
// usrName, String pwd) {
|
||||
// return MongoDBUtil.connect(url, port, dbName, usrName, pwd);
|
||||
// }
|
||||
|
||||
public static String bytes2Str(byte[] bytes) {
|
||||
return new String(bytes);
|
||||
}
|
||||
@ -153,44 +144,6 @@ public class JavaScriptEntry {
|
||||
return bo.toByteArray();
|
||||
}
|
||||
|
||||
// private static String list2Str(List<String> reservedList) {
|
||||
// return JsonUtil.toJson(reservedList);
|
||||
// }
|
||||
//
|
||||
// private static String map2Str(Map<String, Object> map) {
|
||||
// return JsonUtil.toJson(map);
|
||||
// }
|
||||
|
||||
// private static CloseableHttpClient getHttpClient(String url) {
|
||||
// try {
|
||||
// SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
|
||||
//
|
||||
// @Override
|
||||
// public boolean isTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
|
||||
// throws java.security.cert.CertificateException {
|
||||
// return true;
|
||||
// }
|
||||
// }).build();
|
||||
//
|
||||
// SSLConnectionSocketFactory sslSf = new SSLConnectionSocketFactory(sslcontext, null, null,
|
||||
// new NoopHostnameVerifier());
|
||||
// int tle = 10;
|
||||
// if (url.contains("data.tj.gov.cn"))
|
||||
// tle = 3;
|
||||
// return HttpClients.custom().setSSLSocketFactory(sslSf)
|
||||
// .setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
|
||||
// @Override
|
||||
// public long getKeepAliveDuration(HttpResponse arg0, HttpContext arg1) {
|
||||
// return 0;
|
||||
// }
|
||||
// }).setConnectionTimeToLive(tle, TimeUnit.SECONDS).build();
|
||||
//
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
|
||||
public static InputStream httpAsInputStream(String url) {
|
||||
try {
|
||||
URL realUrl = new URL(url);
|
||||
@ -202,53 +155,8 @@ public class JavaScriptEntry {
|
||||
}
|
||||
}
|
||||
|
||||
// public static String httpPost(String str) {
|
||||
// System.out.println("JavaSScriptEntry httpPost:" + str);
|
||||
// PostRequest req = new PostRequest();
|
||||
// req = JsonUtil.fromJson(str, PostRequest.class);
|
||||
// // System.out.println("url========>" + req.url);
|
||||
// // System.out.println("data=======>" + req.data);
|
||||
//
|
||||
// Result r = new Result();
|
||||
// try {
|
||||
// URL url = new URL(req.url);//
|
||||
// HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
// connection.setDoOutput(true);
|
||||
// connection.setDoInput(true);
|
||||
// connection.setUseCaches(false);
|
||||
// connection.setInstanceFollowRedirects(true);
|
||||
// connection.setRequestMethod("POST"); // 璁剧疆璇锋眰鏂瑰紡
|
||||
// connection.setRequestProperty("Accept", "application/json"); // 璁剧疆鎺ユ敹鏁版嵁鐨勬牸寮<EFBFBD>
|
||||
// connection.setRequestProperty("Content-Type", "application/json"); // 璁剧疆鍙戦<EFBFBD>佹暟鎹殑鏍煎紡
|
||||
// connection.connect();
|
||||
// OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); //
|
||||
// utf-8缂栫爜
|
||||
// out.append(req.data);
|
||||
// out.flush();
|
||||
// out.close();
|
||||
//
|
||||
// r.resposeCode = connection.getResponseCode();
|
||||
// InputStream input = connection.getInputStream();
|
||||
//
|
||||
// Scanner sc = new Scanner(input);
|
||||
// StringBuilder sb = new StringBuilder();
|
||||
// for (; sc.hasNextLine();) {
|
||||
// sb.append(sc.nextLine()).append("\n");
|
||||
// }
|
||||
// sc.close();
|
||||
// r.response = sb.toString();
|
||||
// return JsonUtil.toJson(r);
|
||||
// } catch (Throwable e) {
|
||||
// r.resposeCode = 505;
|
||||
// // ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
// // e.printStackTrace(new PrintStream(bo));
|
||||
// r.response = e.getMessage();
|
||||
// return JsonUtil.toJson(r);
|
||||
// }
|
||||
// }
|
||||
|
||||
public static void executeFunction(ScriptFunction callback, Object arg) {
|
||||
DesktopEngine.applyWithGlobal(callback, currentEngine.getNashornGlobal(), arg);
|
||||
public static Object executeFunction(ScriptFunction callback, Object arg) {
|
||||
return DesktopEngine.applyWithGlobal(callback, currentEngine.getNashornGlobal(), arg);
|
||||
}
|
||||
|
||||
public static ApiGate createAPIGate(String ip) {
|
||||
@ -259,8 +167,8 @@ public class JavaScriptEntry {
|
||||
return new ApiGate(ip, Integer.parseInt(port));
|
||||
}
|
||||
|
||||
public static String executeContractWithSig(
|
||||
String contractID, String action, String arg, String pubkey, String sig) {
|
||||
public static String executeContractWithSig(String contractID, String action, String arg,
|
||||
String pubkey, String sig) {
|
||||
try {
|
||||
ContractRequest app = new ContractRequest();
|
||||
app.setContractID(contractID).setAction(action).setArg(arg);
|
||||
@ -270,25 +178,13 @@ public class JavaScriptEntry {
|
||||
if (!app.verifySignature()) {
|
||||
return "{\"status\":\"Exception\",\"data\":\"invalid signature\"}";
|
||||
}
|
||||
app.setRequesterDOI(doi);
|
||||
app.setFromDebug(isDebug);
|
||||
if (numOfCopies > 1) {
|
||||
// The caller is special.
|
||||
app.setRequestID(
|
||||
app.getPublicKey().hashCode()
|
||||
+ "_"
|
||||
+ numOfCopies
|
||||
+ "_"
|
||||
+ (invokeID++)
|
||||
+ "_"
|
||||
+ random.nextInt()
|
||||
+ "_mul");
|
||||
app.setRequestID(app.getPublicKey().hashCode() + "_" + numOfCopies + "_"
|
||||
+ (invokeID++) + "_" + random.nextInt() + "_mul");
|
||||
} else {
|
||||
app.setRequestID(
|
||||
app.getPublicKey().hashCode()
|
||||
+ "_"
|
||||
+ (invokeID++)
|
||||
+ "_"
|
||||
app.setRequestID(app.getPublicKey().hashCode() + "_" + (invokeID++) + "_"
|
||||
+ random.nextInt());
|
||||
}
|
||||
return get.syncGet("dd", "executeContract", JsonUtil.toJson(app));
|
||||
@ -302,9 +198,8 @@ public class JavaScriptEntry {
|
||||
|
||||
public static Object executeContract(String contractID, String action, Object arg) {
|
||||
if (currentSyncUtil.engine.recovering) {
|
||||
String str =
|
||||
currentSyncUtil.transRecoverUtil.curRecoverRecord.getExecuteResult(
|
||||
invokeID + "");
|
||||
String str = currentSyncUtil.transRecoverUtil.curRecoverRecord
|
||||
.getExecuteResult(invokeID + "");
|
||||
String[] strs = str.split("<seperate>");
|
||||
String flag1 = strs[0];
|
||||
String flag = strs[1];
|
||||
@ -325,29 +220,22 @@ public class JavaScriptEntry {
|
||||
|
||||
try {
|
||||
ContractRequest app = new ContractRequest();
|
||||
app.setContractID(contractID).setAction(action).setArg(JSONTool.convertMirrorToJson(arg));
|
||||
app.setContractID(contractID).setAction(action)
|
||||
.setArg(JSONTool.convertMirrorToJson(arg));
|
||||
app.doSignature(keyPair);
|
||||
app.setRequesterDOI(doi);
|
||||
app.setFromDebug(isDebug);
|
||||
if (numOfCopies > 1) {
|
||||
ContractExecType type = ContractProcess.instance.getContract().getType();
|
||||
if (type.needSeq()) {
|
||||
app.setRequestID(
|
||||
String.format(
|
||||
"%d_%d_%d_%d_mul",
|
||||
keyPair.getPublicKeyStr().hashCode(),
|
||||
numOfCopies,
|
||||
(invokeID++),
|
||||
random.nextInt()));
|
||||
String.format("%d_%d_%d_%d_mul", keyPair.getPublicKeyStr().hashCode(),
|
||||
numOfCopies, (invokeID++), random.nextInt()));
|
||||
// The caller is special.
|
||||
flag = 1;
|
||||
flag1 = 1;
|
||||
LOGGER.warn("invoke contractExecution! " + JsonUtil.toJson(app));
|
||||
} else {
|
||||
app.setRequestID(
|
||||
String.format(
|
||||
"%d_%d_%d",
|
||||
keyPair.getPublicKeyStr().hashCode(),
|
||||
(invokeID++),
|
||||
random.nextInt()));
|
||||
app.setRequestID(String.format("%d_%d_%d", keyPair.getPublicKeyStr().hashCode(),
|
||||
(invokeID++), random.nextInt()));
|
||||
flag = 1;
|
||||
flag1 = 1;
|
||||
}
|
||||
@ -356,45 +244,38 @@ public class JavaScriptEntry {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
e.printStackTrace(new PrintStream(bo));
|
||||
String result = bo.toString();
|
||||
if (currentSyncUtil.startFlag
|
||||
&& currentSyncUtil.currType == SyncType.Trans
|
||||
if (currentSyncUtil.startFlag && currentSyncUtil.currType == SyncType.Trans
|
||||
&& !currentSyncUtil.engine.recovering) {
|
||||
currentSyncUtil.transRecordUtil.recordExecutes(
|
||||
formerInvokeID + "", flag1 + "<seperate>" + flag + "<seperate>" + result);
|
||||
currentSyncUtil.transRecordUtil.recordExecutes(formerInvokeID + "",
|
||||
flag1 + "<seperate>" + flag + "<seperate>" + result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private static Object executeContract(long formerInvokeID, int flag1, int flag, ContractRequest app) {
|
||||
private static Object executeContract(long formerInvokeID, int flag1, int flag,
|
||||
ContractRequest app) {
|
||||
String result = get.syncGet("dd", "executeContract", JsonUtil.toJson(app));
|
||||
if (currentSyncUtil.startFlag
|
||||
&& currentSyncUtil.currType == SyncType.Trans
|
||||
if (currentSyncUtil.startFlag && currentSyncUtil.currType == SyncType.Trans
|
||||
&& !currentSyncUtil.engine.recovering) {
|
||||
currentSyncUtil.transRecordUtil.recordExecutes(
|
||||
formerInvokeID + "",
|
||||
currentSyncUtil.transRecordUtil.recordExecutes(formerInvokeID + "",
|
||||
flag1 + "<seperate>" + flag + "<seperate>" + result);
|
||||
}
|
||||
JsonObject jo = JsonUtil.parseStringAsJsonObject(result);
|
||||
return JSONTool.convertJsonElementToMirror(jo);
|
||||
}
|
||||
|
||||
public static void executeContractAsyncWithoutSig(
|
||||
String contractID, String action, String arg, final ScriptFunction cb) {
|
||||
public static void executeContractAsyncWithoutSig(String contractID, String action, String arg,
|
||||
final ScriptFunction cb) {
|
||||
try {
|
||||
ContractRequest app = new ContractRequest();
|
||||
app.setContractID(contractID).setAction(action).setArg(arg);
|
||||
app.setRequestID((invokeID++) + "_" + random.nextInt());
|
||||
get.asyncGet(
|
||||
"dd",
|
||||
"executeContract",
|
||||
JsonUtil.toJson(app),
|
||||
new ResultCallback() {
|
||||
get.asyncGet("dd", "executeContract", JsonUtil.toJson(app), new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
if (null != cb) {
|
||||
DesktopEngine.applyWithGlobal(
|
||||
cb, currentEngine.getNashornGlobal(), str);
|
||||
DesktopEngine.applyWithGlobal(cb, currentEngine.getNashornGlobal(), str);
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -404,142 +285,20 @@ public class JavaScriptEntry {
|
||||
}
|
||||
}
|
||||
|
||||
/*public static String executeContract(String contractID, String action, String arg) {
|
||||
//redo,use record data
|
||||
if(currentSyncUtil.transRecoverUtil != null && currentSyncUtil.transRecoverUtil.recovering){
|
||||
String k = TransRecordUtil.produceExecuteIdentifier(contractID,action,arg);
|
||||
return currentSyncUtil.transRecoverUtil.curRecoverRecord.getExecuteResult(k);
|
||||
}
|
||||
|
||||
try {
|
||||
ContractRequest app = new ContractRequest();
|
||||
app.setContractID(contractID).setAction(action).setArg(arg);
|
||||
//app.doSignature(keyPair.getPrivateKey().toString(16));
|
||||
app.doSignature(keyPair);
|
||||
String result = get.syncGet("dd", "executeContract", JsonUtil.toJson(app));
|
||||
if(currentSyncUtil.startFlag && currentSyncUtil.currType == SyncType.Trans){
|
||||
String k = currentSyncUtil.transRecordUtil.produceExecuteIdentifier(contractID,action,arg);
|
||||
currentSyncUtil.transRecordUtil.recordExecutes(k,result);
|
||||
}
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
e.printStackTrace(new PrintStream(bo));
|
||||
String result = bo.toString();
|
||||
if(currentSyncUtil.startFlag && currentSyncUtil.currType == SyncType.Trans){
|
||||
String k = TransRecordUtil.produceExecuteIdentifier(contractID,action,arg);
|
||||
currentSyncUtil.transRecordUtil.recordExecutes(k,result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}*/
|
||||
|
||||
// public static String queryContractIdByDOI(String contractDOI) throws Exception {
|
||||
// DigitalObject contractDO;
|
||||
// DoipClient doipClient =
|
||||
// DoipClient.createByRepoUrlAndMsgFmt(
|
||||
// DOIPMainServer.repoUrl, DoipMessageFormat.PACKET.getName());
|
||||
// DoMessage response = doipClient.retrieve(contractDOI, null, null);
|
||||
// if (response.parameters.response == DoResponse.Success) {
|
||||
// contractDO = DigitalObject.parse(response.body);
|
||||
// } else {
|
||||
// response = DOAClient.getGlobalInstance().retrieve(contractDOI, null, null);
|
||||
// contractDO = DigitalObject.parse(response.body);
|
||||
// }
|
||||
// ContractInstanceDO contractInstanceDO =
|
||||
// (ContractInstanceDO)
|
||||
// ContractManager.toObject(contractDO.elements.get(0).getData());
|
||||
// return contractInstanceDO.id;
|
||||
// }
|
||||
|
||||
// public static String executeContractByDOI(String contractDOI, String action, String arg) {
|
||||
// try {
|
||||
// String contractID = queryContractIdByDOI(contractDOI);
|
||||
// return executeContract(contractID, action, arg);
|
||||
// } catch (Exception e) {
|
||||
// ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
// e.printStackTrace(new PrintStream(bo));
|
||||
// return bo.toString();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public static String getAuthInfo() {
|
||||
// try {
|
||||
// DigitalObject contractDO;
|
||||
// DoipClient doipClient =
|
||||
// DoipClient.createByRepoUrlAndMsgFmt(
|
||||
// DOIPMainServer.repoUrl, DoipMessageFormat.PACKET.getName());
|
||||
// DoMessage response = doipClient.retrieve(authInfoPersistDOI, null, null);
|
||||
// if (response.parameters.response != DoResponse.Success) {
|
||||
// response = DOAClient.getGlobalInstance().retrieve(authInfoPersistDOI, null,
|
||||
// null);
|
||||
// }
|
||||
// contractDO = DigitalObject.parse(response.body);
|
||||
// return new String(contractDO.elements.get(0).getData());
|
||||
// } catch (Exception e) {
|
||||
// ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
// e.printStackTrace(new PrintStream(bo));
|
||||
// return "Failed: " + bo.toString();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public static String setAuthInfo(String authInfo) {
|
||||
// try {
|
||||
//
|
||||
// DigitalObject contractDO = new DigitalObject(authInfoPersistDOI, DoType.Json);
|
||||
// Element e = new Element("authInfo", "JsonString");
|
||||
// e.setData(authInfo.getBytes());
|
||||
// contractDO.addElements(e);
|
||||
//
|
||||
// DoipClient doipClient =
|
||||
// DoipClient.createByRepoUrlAndMsgFmt(
|
||||
// DOIPMainServer.repoUrl, DoipMessageFormat.PACKET.getName());
|
||||
// DoMessage response = doipClient.update(contractDO);
|
||||
// if (response.parameters.response != DoResponse.Success) {
|
||||
// DoHandleRecord dohr =
|
||||
// DOAClient.getGlobalInstance().resolveDO(authInfoPersistDOI);
|
||||
// if (dohr == null) {
|
||||
// return "Failed: Can not resolve authInfoPersistDOI: " +
|
||||
// authInfoPersistDOI;
|
||||
// }
|
||||
// ServiceHandleRecord repoHandleRecord =
|
||||
// DOAClient.getGlobalInstance().resolveDOIPService(dohr.repository);
|
||||
// doipClient =
|
||||
// DoipClient.createByRepoUrlAndMsgFmt(
|
||||
// repoHandleRecord.getListenerInfos().get(0).url,
|
||||
// DoipMessageFormat.PACKET.getName());
|
||||
// response = doipClient.update(contractDO);
|
||||
// if (response.parameters.response != DoResponse.Success) {
|
||||
// return "Failed: Can not update authInfoPersistDOI: " + authInfoPersistDOI;
|
||||
// }
|
||||
// }
|
||||
// return "Succeeded";
|
||||
// } catch (Exception e) {
|
||||
// ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
// e.printStackTrace(new PrintStream(bo));
|
||||
// return "Failed: " + bo.toString();
|
||||
// }
|
||||
// }
|
||||
|
||||
public static String executeContractAsync(
|
||||
String contractID, String action, String arg, final ScriptFunction cb) {
|
||||
public static String executeContractAsync(String contractID, String action, String arg,
|
||||
final ScriptFunction cb) {
|
||||
try {
|
||||
|
||||
ContractRequest app = new ContractRequest();
|
||||
app.setContractID(contractID).setAction(action).setArg(arg);
|
||||
app.doSignature(keyPair);
|
||||
app.setRequestID((invokeID++) + "_" + random());
|
||||
app.setRequesterDOI(doi);
|
||||
get.asyncGet(
|
||||
"dd",
|
||||
"executeContract",
|
||||
JsonUtil.toJson(app),
|
||||
new ResultCallback() {
|
||||
get.asyncGet("dd", "executeContract", JsonUtil.toJson(app), new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
if (cb != null) {
|
||||
DesktopEngine.applyWithGlobal(
|
||||
cb, currentEngine.getNashornGlobal(), str, arg);
|
||||
DesktopEngine.applyWithGlobal(cb, currentEngine.getNashornGlobal(), str,
|
||||
arg);
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -551,6 +310,11 @@ public class JavaScriptEntry {
|
||||
}
|
||||
}
|
||||
|
||||
// YancloudUtil.exceptionReturn({"msg":"missing arguments repoId ","code":1});
|
||||
public static void exceptionReturn(Object obj) throws ScriptReturnException {
|
||||
throw new ScriptReturnException(JSONTool.convertMirrorToJson(obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* publish an event with semantic AT_LEAST_ONCE
|
||||
*
|
||||
@ -571,10 +335,8 @@ public class JavaScriptEntry {
|
||||
* @author Kaidong Wu
|
||||
*/
|
||||
public static void pubEventConstraint(String topic, String content, String constraint) {
|
||||
String reqID =
|
||||
String.format(
|
||||
"%d_%d_%d_%s_pe",
|
||||
keyPair.getPublicKeyStr().hashCode(), numOfCopies, invokeID, random());
|
||||
String reqID = String.format("%d_%d_%d_%s_pe", keyPair.getPublicKeyStr().hashCode(),
|
||||
numOfCopies, invokeID, random());
|
||||
REvent msg = new REvent(topic, PUBLISH, content, reqID);
|
||||
if (null != constraint) {
|
||||
msg.setSemantics(REvent.REventSemantics.valueOf(constraint));
|
||||
@ -590,10 +352,10 @@ public class JavaScriptEntry {
|
||||
* @author Kaidong Wu
|
||||
*/
|
||||
public static String subscribe(String topic, ScriptFunction fun) {
|
||||
subscribe(topic, fun, false);
|
||||
if (topic_handlers.containsKey(topic)) {
|
||||
ContractProcess.instance.unSubscribe(topic_handlers.get(topic).getName());
|
||||
}
|
||||
subscribe(topic, fun, false);
|
||||
topic_handlers.put(topic, fun);
|
||||
return topic;
|
||||
}
|
||||
@ -617,17 +379,11 @@ public class JavaScriptEntry {
|
||||
}
|
||||
|
||||
private static void subscribe(String topic, ScriptFunction fun, boolean fromPreSub) {
|
||||
String reqID =
|
||||
String.format(
|
||||
"%d_%d_%d_%s_se",
|
||||
keyPair.getPublicKeyStr().hashCode(), numOfCopies, invokeID, random());
|
||||
String reqID = String.format("%d_%d_%d_%s_se", keyPair.getPublicKeyStr().hashCode(),
|
||||
numOfCopies, invokeID, random());
|
||||
|
||||
REvent msg =
|
||||
new REvent(
|
||||
topic,
|
||||
SUBSCRIBE,
|
||||
String.format(
|
||||
"{\"subscriber\":\"%s\",\"handler\":\"%s\"}",
|
||||
REvent msg = new REvent(topic, SUBSCRIBE,
|
||||
String.format("{\"subscriber\":\"%s\",\"handler\":\"%s\"}",
|
||||
ContractProcess.instance.getContractName(), fun.getName()),
|
||||
reqID);
|
||||
if (fromPreSub) {
|
||||
@ -639,10 +395,8 @@ public class JavaScriptEntry {
|
||||
}
|
||||
|
||||
public static void unsubscribe(String topic) {
|
||||
String reqID =
|
||||
String.format(
|
||||
"%d_%d_%d_%s_us",
|
||||
keyPair.getPublicKeyStr().hashCode(), numOfCopies, invokeID, random());
|
||||
String reqID = String.format("%d_%d_%d_%s_us", keyPair.getPublicKeyStr().hashCode(),
|
||||
numOfCopies, invokeID, random());
|
||||
String content;
|
||||
if (null == topic) {
|
||||
content = "{\"subscriber\":\"" + ContractProcess.instance.getContractName() + "\"}";
|
||||
@ -652,9 +406,7 @@ public class JavaScriptEntry {
|
||||
});
|
||||
} else {
|
||||
String handler = topic_handlers.get(topic).getName();
|
||||
content =
|
||||
String.format(
|
||||
"{\"subscriber\":\"%s\",\"handler\":\"%s\"}",
|
||||
content = String.format("{\"subscriber\":\"%s\",\"handler\":\"%s\"}",
|
||||
ContractProcess.instance.getContractName(), handler);
|
||||
topic_handlers.remove(topic);
|
||||
ContractProcess.instance.unSubscribe(handler);
|
||||
@ -673,10 +425,8 @@ public class JavaScriptEntry {
|
||||
public static void preSub(String topic, String content) {
|
||||
String newTopic = topic + "|" + content + "|" + ContractProcess.instance.getContractName();
|
||||
subscribe(newTopic, topic_handlers.get(topic), true);
|
||||
String reqID =
|
||||
String.format(
|
||||
"%d_%d_%d_%s_pse",
|
||||
keyPair.getPublicKeyStr().hashCode(), numOfCopies, (invokeID++), random());
|
||||
String reqID = String.format("%d_%d_%d_%s_pse", keyPair.getPublicKeyStr().hashCode(),
|
||||
numOfCopies, (invokeID++), random());
|
||||
REvent msg = new REvent(topic, REvent.REventType.PRESUB, newTopic, reqID);
|
||||
msg.setSemantics(REvent.REventSemantics.ONLY_ONCE);
|
||||
msgList.add(msg);
|
||||
@ -687,7 +437,8 @@ public class JavaScriptEntry {
|
||||
* @author Kaidong Wu
|
||||
*/
|
||||
public static String random() {
|
||||
String seed = String.valueOf(null == random ? System.currentTimeMillis() : random.nextInt());
|
||||
String seed =
|
||||
String.valueOf(null == random ? System.currentTimeMillis() : random.nextInt());
|
||||
return HashUtil.sha3(seed);
|
||||
}
|
||||
|
||||
@ -696,46 +447,6 @@ public class JavaScriptEntry {
|
||||
// TODO
|
||||
}
|
||||
|
||||
public static String sendEmail(String json) {
|
||||
try {
|
||||
final JsonObject jo = JsonParser.parseString(json).getAsJsonObject();
|
||||
Properties props = new Properties();
|
||||
props.setProperty("mail.debug", "false");
|
||||
props.setProperty("mail.smtp.auth", "true");
|
||||
props.setProperty("mail.smtp.host", jo.get("host").getAsString());
|
||||
props.setProperty("mail.smtp.port", jo.get("port").getAsString());
|
||||
props.setProperty("mail.transport.protocol", "smtp");
|
||||
props.put("mail.smtp.auth", "true");
|
||||
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
|
||||
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
|
||||
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
|
||||
props.setProperty("mail.smtp.socketFactory.fallback", "false");
|
||||
props.setProperty("mail.smtp.socketFactory.port", jo.get("port").getAsString());
|
||||
Session session =
|
||||
Session.getDefaultInstance(
|
||||
props,
|
||||
new Authenticator() {
|
||||
public PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(
|
||||
jo.get("from").getAsString(),
|
||||
jo.get("pwd").getAsString()); // 发件人邮件用户名、密码
|
||||
}
|
||||
});
|
||||
// 创建邮件对象
|
||||
|
||||
Message msg = new MimeMessage(session);
|
||||
msg.setSubject(jo.get("subject").getAsString());
|
||||
msg.setText(jo.get("content").getAsString());
|
||||
msg.setFrom(new InternetAddress(jo.get("from").getAsString()));
|
||||
msg.addRecipient(
|
||||
Message.RecipientType.TO, new InternetAddress(jo.get("to").getAsString()));
|
||||
Transport.send(msg);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return "failed";
|
||||
}
|
||||
return "success";
|
||||
}
|
||||
|
||||
public static ScriptObject getCaller(int i) {
|
||||
JO ret = new JO(PropertyMap.newMap());
|
||||
@ -752,8 +463,4 @@ public class JavaScriptEntry {
|
||||
public String response;
|
||||
}
|
||||
|
||||
static class PostRequest {
|
||||
String url;
|
||||
String data;
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,6 @@ import java.util.Scanner;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
import java.util.zip.ZipInputStream;
|
||||
public class Resources {
|
||||
private final ZipFile zf;
|
||||
YJSClassLoader loader;
|
||||
@ -67,7 +66,8 @@ public class Resources {
|
||||
ZipEntry entry = zf.getEntry(path);
|
||||
if (entry == null)
|
||||
return null;
|
||||
reader = new BufferedReader(new InputStreamReader(zf.getInputStream(entry), StandardCharsets.UTF_8));
|
||||
reader = new BufferedReader(
|
||||
new InputStreamReader(zf.getInputStream(entry), StandardCharsets.UTF_8));
|
||||
String line = null;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
fileList.add(line);
|
||||
@ -84,7 +84,8 @@ public class Resources {
|
||||
public String unzipToDir(String path) {
|
||||
ZipEntry entry = zf.getEntry(path);
|
||||
try {
|
||||
return loader.unzipLibrary(zf.getInputStream(entry), entry.getName().replaceAll(".*/", ""));
|
||||
return loader.unzipLibrary(zf.getInputStream(entry),
|
||||
entry.getName().replaceAll(".*/", ""));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -0,0 +1,12 @@
|
||||
package org.bdware.sc.boundry;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public class ScriptReturnException extends IllegalStateException {
|
||||
public JsonObject message;
|
||||
|
||||
public ScriptReturnException(JsonElement jsonElement) {
|
||||
message = jsonElement.getAsJsonObject();
|
||||
}
|
||||
}
|
@ -16,13 +16,16 @@ import java.security.NoSuchAlgorithmException;
|
||||
|
||||
@PermissionStub(permission = Permission.AES)
|
||||
public class AESUtil {
|
||||
public static ScriptObject encrypt(String key, String plaintext) throws NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException, InvalidKeyException {
|
||||
public static ScriptObject encrypt(String key, String plaintext)
|
||||
throws NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException,
|
||||
IllegalBlockSizeException, InvalidAlgorithmParameterException, InvalidKeyException {
|
||||
JO ret = new JO(PropertyMap.newMap());
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||
String iv = generateKey(128);
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(DatatypeConverter.parseHexBinary(iv));
|
||||
byte[] byteContent = plaintext.getBytes();
|
||||
SecretKeySpec secretKeySpecSpec = new SecretKeySpec(DatatypeConverter.parseHexBinary(key), "AES");
|
||||
SecretKeySpec secretKeySpecSpec =
|
||||
new SecretKeySpec(DatatypeConverter.parseHexBinary(key), "AES");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpecSpec, ivSpec);
|
||||
byte[] result = cipher.doFinal(byteContent);
|
||||
ret.put("iv", iv, false);
|
||||
@ -30,12 +33,16 @@ public class AESUtil {
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static String decrypt(String key, String ciphertext, String iv) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
|
||||
public static String decrypt(String key, String ciphertext, String iv)
|
||||
throws NoSuchPaddingException, NoSuchAlgorithmException,
|
||||
InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException,
|
||||
IllegalBlockSizeException {
|
||||
// JO ret = new JO(PropertyMap.newMap());
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(DatatypeConverter.parseHexBinary(iv));
|
||||
byte[] byteContent = DatatypeConverter.parseHexBinary(ciphertext);
|
||||
SecretKeySpec secretKeySpecSpec = new SecretKeySpec(DatatypeConverter.parseHexBinary(key), "AES");
|
||||
SecretKeySpec secretKeySpecSpec =
|
||||
new SecretKeySpec(DatatypeConverter.parseHexBinary(key), "AES");
|
||||
cipher.init(Cipher.DECRYPT_MODE, secretKeySpecSpec, ivSpec);
|
||||
byte[] result = cipher.doFinal(byteContent);
|
||||
return new String(result);
|
||||
|
@ -30,15 +30,13 @@ public class AsyncUtil {
|
||||
}
|
||||
|
||||
public static String postFunction(final ScriptFunction callback, Object wrapper) {
|
||||
ServiceServer.executor.execute(
|
||||
() -> JavaScriptEntry.executeFunction(callback, wrapper));
|
||||
ServiceServer.executor.execute(() -> JavaScriptEntry.executeFunction(callback, wrapper));
|
||||
return "success";
|
||||
}
|
||||
|
||||
public static TimerTask setTimeOut(
|
||||
final ScriptFunction callback, long delay, final Object arg) {
|
||||
TimerTask task =
|
||||
new TimerTask() {
|
||||
public static TimerTask setTimeOut(final ScriptFunction callback, long delay,
|
||||
final Object arg) {
|
||||
TimerTask task = new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
JavaScriptEntry.executeFunction(callback, arg);
|
||||
@ -48,10 +46,9 @@ public class AsyncUtil {
|
||||
return task;
|
||||
}
|
||||
|
||||
public static TimerTask setInterval(
|
||||
final ScriptFunction callback, long delay, long interval, final Object arg) {
|
||||
TimerTask task =
|
||||
new TimerTask() {
|
||||
public static TimerTask setInterval(final ScriptFunction callback, long delay, long interval,
|
||||
final Object arg) {
|
||||
TimerTask task = new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
JavaScriptEntry.executeFunction(callback, arg);
|
||||
@ -61,22 +58,19 @@ public class AsyncUtil {
|
||||
return task;
|
||||
}
|
||||
|
||||
public static void executeContractAsyncWithoutSig(
|
||||
String contractID, String action, String arg, final ScriptFunction cb) {
|
||||
public static void executeContractAsyncWithoutSig(String contractID, String action, String arg,
|
||||
final ScriptFunction cb) {
|
||||
try {
|
||||
ContractRequest app = new ContractRequest();
|
||||
app.setContractID(contractID).setAction(action).setArg(arg);
|
||||
app.setRequestID((JavaScriptEntry.invokeID++) + "_" + JavaScriptEntry.random.nextInt());
|
||||
JavaScriptEntry.get.asyncGet(
|
||||
"dd",
|
||||
"executeContract",
|
||||
JsonUtil.toJson(app),
|
||||
JavaScriptEntry.get.asyncGet("dd", "executeContract", JsonUtil.toJson(app),
|
||||
new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
if (null != cb) {
|
||||
DesktopEngine.applyWithGlobal(
|
||||
cb, JavaScriptEntry.currentEngine.getNashornGlobal(), str);
|
||||
DesktopEngine.applyWithGlobal(cb,
|
||||
JavaScriptEntry.currentEngine.getNashornGlobal(), str);
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -85,25 +79,22 @@ public class AsyncUtil {
|
||||
e.printStackTrace(new PrintStream(bo));
|
||||
}
|
||||
}
|
||||
public static String executeContractAsync(
|
||||
String contractID, String action, String arg, final ScriptFunction cb) {
|
||||
|
||||
public static String executeContractAsync(String contractID, String action, String arg,
|
||||
final ScriptFunction cb) {
|
||||
try {
|
||||
|
||||
ContractRequest app = new ContractRequest();
|
||||
app.setContractID(contractID).setAction(action).setArg(arg);
|
||||
app.doSignature(JavaScriptEntry.getKeyPair());
|
||||
app.setRequestID((JavaScriptEntry.invokeID++) + "_" + JavaScriptEntry.random());
|
||||
app.setRequesterDOI(JavaScriptEntry.doi);
|
||||
JavaScriptEntry.get.asyncGet(
|
||||
"dd",
|
||||
"executeContract",
|
||||
JsonUtil.toJson(app),
|
||||
JavaScriptEntry.get.asyncGet("dd", "executeContract", JsonUtil.toJson(app),
|
||||
new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
if (cb != null) {
|
||||
DesktopEngine.applyWithGlobal(
|
||||
cb, JavaScriptEntry.currentEngine.getNashornGlobal(), str, arg);
|
||||
DesktopEngine.applyWithGlobal(cb,
|
||||
JavaScriptEntry.currentEngine.getNashornGlobal(), str, arg);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -2,7 +2,6 @@ package org.bdware.sc.boundry.utils;
|
||||
|
||||
import org.bdware.sc.ContractProcess;
|
||||
import org.bdware.sc.compiler.PermissionStub;
|
||||
import org.bdware.sc.db.TimeDBUtil;
|
||||
import org.bdware.sc.db.TimeRocksDBUtil;
|
||||
import org.bdware.sc.node.Permission;
|
||||
|
||||
|
44
src/main/java/org/bdware/sc/boundry/utils/ClusterUtil.java
Normal file
44
src/main/java/org/bdware/sc/boundry/utils/ClusterUtil.java
Normal file
@ -0,0 +1,44 @@
|
||||
package org.bdware.sc.boundry.utils;
|
||||
|
||||
import org.bdware.sc.boundry.JavaScriptEntry;
|
||||
import org.bdware.sc.compiler.PermissionStub;
|
||||
import org.bdware.sc.node.Permission;
|
||||
import wrp.jdk.nashorn.internal.objects.NativeArray;
|
||||
|
||||
@PermissionStub(permission = Permission.Cluster)
|
||||
public class ClusterUtil {
|
||||
public static int getShardingID() {
|
||||
return JavaScriptEntry.shardingID;
|
||||
}
|
||||
|
||||
public static Object getMembers() {
|
||||
if (JavaScriptEntry.members == null) {
|
||||
return new NativeArray();
|
||||
}
|
||||
NativeArray narray = new NativeArray();
|
||||
for (int i = 0; i < JavaScriptEntry.members.size(); i++)
|
||||
NativeArray.push(narray, JavaScriptEntry.members.get(i));
|
||||
return narray;
|
||||
}
|
||||
|
||||
public static String getMember(int i) {
|
||||
return JavaScriptEntry.members.get(i);
|
||||
}
|
||||
|
||||
public static int getMembersSize() {
|
||||
if (JavaScriptEntry.members != null)
|
||||
return JavaScriptEntry.members.size();
|
||||
return 1;
|
||||
}
|
||||
|
||||
public static Object getCurrentNodeID() {
|
||||
if (isCluster()) {
|
||||
return JavaScriptEntry.members.get(JavaScriptEntry.shardingID);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean isCluster() {
|
||||
return (JavaScriptEntry.members != null);
|
||||
}
|
||||
}
|
231
src/main/java/org/bdware/sc/boundry/utils/DOIPUtil.java
Normal file
231
src/main/java/org/bdware/sc/boundry/utils/DOIPUtil.java
Normal file
@ -0,0 +1,231 @@
|
||||
package org.bdware.sc.boundry.utils;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.doip.audit.EndpointConfig;
|
||||
import org.bdware.doip.audit.client.AuditIrpClient;
|
||||
import org.bdware.doip.codec.digitalObject.DigitalObject;
|
||||
import org.bdware.doip.codec.digitalObject.Element;
|
||||
import org.bdware.doip.codec.doipMessage.DoipMessage;
|
||||
import org.bdware.doip.codec.doipMessage.DoipMessageFactory;
|
||||
import org.bdware.doip.codec.operations.BasicOperations;
|
||||
import org.bdware.doip.endpoint.client.ClientConfig;
|
||||
import org.bdware.doip.endpoint.client.DoipClientImpl;
|
||||
import org.bdware.doip.endpoint.client.DoipMessageCallback;
|
||||
import org.bdware.irp.stateinfo.StateInfoBase;
|
||||
import org.bdware.sc.compiler.PermissionStub;
|
||||
import org.bdware.sc.engine.JSONTool;
|
||||
import org.bdware.sc.node.Permission;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@PermissionStub(permission = Permission.DOIP)
|
||||
public class DOIPUtil {
|
||||
private static final Logger LOGGER = LogManager.getLogger(DOIPUtil.class);
|
||||
private final String url;
|
||||
private final DoipClientImpl doipClient;
|
||||
|
||||
private DOIPUtil(String url) {
|
||||
this.url = url;
|
||||
this.doipClient = new DoipClientImpl();
|
||||
doipClient.connect(ClientConfig.fromUrl(url));
|
||||
}
|
||||
|
||||
public static DOIPUtil createClient(String url) {
|
||||
return new DOIPUtil(url);
|
||||
}
|
||||
|
||||
public String test(String doi) {
|
||||
return "create DOClient And hello " + doi + " World";
|
||||
}
|
||||
|
||||
public boolean reconnect() {
|
||||
try {
|
||||
if (doipClient != null)
|
||||
doipClient.reconnect();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private DoipMessage syncGetMessage(DoipMessage message) {
|
||||
List<DoipMessage> ret = new ArrayList<>();
|
||||
DoipMessage msg = null;
|
||||
try {
|
||||
doipClient.sendMessage(message, new DoipMessageCallback() {
|
||||
@Override
|
||||
public void onResult(DoipMessage doipMessage) {
|
||||
ret.add(doipMessage);
|
||||
synchronized (ret) {
|
||||
ret.notify();
|
||||
}
|
||||
}
|
||||
});
|
||||
synchronized (ret) {
|
||||
ret.wait(5000);
|
||||
}
|
||||
if (ret.size() > 0)
|
||||
msg = ret.get(0);
|
||||
if (msg != null) {
|
||||
return msg;
|
||||
} else
|
||||
return DoipMessageFactory.createTimeoutResponse(message.requestID, "timeout");
|
||||
} catch (Exception ie) {
|
||||
ie.printStackTrace();
|
||||
return DoipMessageFactory.createConnectFailedResponse(message.requestID);
|
||||
}
|
||||
}
|
||||
|
||||
private static String convertDoipMsgToString(DoipMessage ret) {
|
||||
try {
|
||||
LOGGER.info(new String(ret.body.encodedData));
|
||||
DigitalObject respDO = ret.body.getDataAsDigitalObject();
|
||||
return respDO.toString();
|
||||
} catch (Exception ie) {
|
||||
ie.printStackTrace();
|
||||
return ret.body.getDataAsJsonString();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public String hello(String repoID) {
|
||||
DoipMessage msg = (new DoipMessageFactory.DoipMessageBuilder())
|
||||
.createRequest(repoID, BasicOperations.Hello.getName()).create();
|
||||
return convertDoipMsgToString(syncGetMessage(msg));
|
||||
}
|
||||
|
||||
public String retrieve(String doi, String args) {
|
||||
DoipMessage msg = (new DoipMessageFactory.DoipMessageBuilder())
|
||||
.createRequest(doi, BasicOperations.Retrieve.getName()).create();
|
||||
msg.header.parameters.addAttribute("element", "");
|
||||
return convertDoipMsgToString(syncGetMessage(msg));
|
||||
}
|
||||
|
||||
public String call(String doi, String action, String args) {
|
||||
JsonObject jo = new JsonObject();
|
||||
jo.addProperty("action", action);
|
||||
jo.addProperty("args", args);
|
||||
DoipMessage msg = (new DoipMessageFactory.DoipMessageBuilder())
|
||||
.createRequest(doi, BasicOperations.Retrieve.getName())
|
||||
.setBody(jo.toString().getBytes(StandardCharsets.UTF_8)).create();
|
||||
return convertDoipMsgToString(syncGetMessage(msg));
|
||||
}
|
||||
|
||||
public String create(String repoID, String doString) {
|
||||
DigitalObject digitalObject = JsonUtil.fromJson(doString, DigitalObject.class);
|
||||
for (Element e : digitalObject.elements) {
|
||||
if (null != e.dataString) {
|
||||
e.setData(e.dataString.getBytes());
|
||||
}
|
||||
e.dataString = null;
|
||||
}
|
||||
DoipMessage msg = new DoipMessageFactory.DoipMessageBuilder()
|
||||
.createRequest(repoID, BasicOperations.Create.getName()).setBody(digitalObject)
|
||||
.create();
|
||||
return convertDoipMsgToString(syncGetMessage(msg));
|
||||
}
|
||||
|
||||
public String delete(String doID) {
|
||||
DoipMessage msg = new DoipMessageFactory.DoipMessageBuilder()
|
||||
.createRequest(doID, BasicOperations.Delete.getName()).create();
|
||||
return convertDoipMsgToString(syncGetMessage(msg));
|
||||
}
|
||||
|
||||
public String listOperation(String doID) {
|
||||
DoipMessage msg = new DoipMessageFactory.DoipMessageBuilder()
|
||||
.createRequest(doID, BasicOperations.ListOps.getName()).create();
|
||||
return convertDoipMsgToString(syncGetMessage(msg));
|
||||
}
|
||||
|
||||
public static class IRPClientWrapper {
|
||||
public AuditIrpClient impl;
|
||||
|
||||
public IRPClientWrapper(EndpointConfig config) {
|
||||
|
||||
impl = new AuditIrpClient(config);
|
||||
|
||||
}
|
||||
|
||||
public Object reconnect() {
|
||||
JsonObject jo2 = new JsonObject();
|
||||
try {
|
||||
impl.reconnect();
|
||||
jo2.addProperty("code", 1);
|
||||
jo2.addProperty("msg", "success");
|
||||
} catch (Exception e) {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
e.printStackTrace(new PrintStream(bo));
|
||||
jo2.addProperty("code", 0);
|
||||
jo2.addProperty("msg", bo.toString());
|
||||
|
||||
}
|
||||
return JSONTool.convertJsonElementToMirror(jo2);
|
||||
}
|
||||
|
||||
public Object resolve(String doId) {
|
||||
StateInfoBase jo = null;
|
||||
try {
|
||||
jo = impl.resolve(doId);
|
||||
} catch (Exception e) {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
e.printStackTrace(new PrintStream(bo));
|
||||
JsonObject jo2 = new JsonObject();
|
||||
jo2.addProperty("code", 0);
|
||||
jo2.addProperty("msg", bo.toString());
|
||||
return JSONTool.convertJsonElementToMirror(jo2);
|
||||
}
|
||||
return JSONTool.convertJsonElementToMirror(jo.getHandleValues());
|
||||
}
|
||||
|
||||
public Object register(ScriptObjectMirror obj) {
|
||||
JsonObject jo = JSONTool.convertMirrorToJson(obj).getAsJsonObject();
|
||||
StateInfoBase base = new StateInfoBase();
|
||||
jo.addProperty("repoId", impl.getEndpointInfo().getDoId());
|
||||
base.setHandleValues(jo);
|
||||
String ret = null;
|
||||
JsonObject jo2 = new JsonObject();
|
||||
try {
|
||||
ret = impl.register(base);
|
||||
if (ret != null) {
|
||||
jo2.addProperty("code", 1);
|
||||
jo2.addProperty("doId", ret.toString());
|
||||
} else {
|
||||
jo2.addProperty("code", 0);
|
||||
jo2.addProperty("msg", "connection failed!");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
e.printStackTrace(new PrintStream(bo));
|
||||
jo2.addProperty("code", 0);
|
||||
jo2.addProperty("msg", bo.toString());
|
||||
return JSONTool.convertJsonElementToMirror(jo2);
|
||||
}
|
||||
return JSONTool.convertJsonElementToMirror(jo2);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static IRPClientWrapper createIrpClient(String uri) {
|
||||
return createIrpClient(uri, null, null, null);
|
||||
}
|
||||
|
||||
public static IRPClientWrapper createIrpClient(String uri, String pubkey, String privateKey,
|
||||
String repoName) {
|
||||
EndpointConfig config = new EndpointConfig();
|
||||
config.routerURI = uri;
|
||||
config.repoName = repoName;
|
||||
config.privateKey = privateKey;
|
||||
config.publicKey = pubkey;
|
||||
return new IRPClientWrapper(config);
|
||||
}
|
||||
|
||||
}
|
15
src/main/java/org/bdware/sc/boundry/utils/DigestUtil.java
Normal file
15
src/main/java/org/bdware/sc/boundry/utils/DigestUtil.java
Normal file
@ -0,0 +1,15 @@
|
||||
package org.bdware.sc.boundry.utils;
|
||||
|
||||
import org.bdware.sc.compiler.PermissionStub;
|
||||
import org.bdware.sc.node.Permission;
|
||||
|
||||
@PermissionStub(permission = Permission.Digest)
|
||||
public class DigestUtil {
|
||||
public static String md5Hex(String arg) {
|
||||
return org.apache.commons.codec.digest.DigestUtils.md5Hex(arg);
|
||||
}
|
||||
|
||||
public static String sha256Hex(String arg) {
|
||||
return org.apache.commons.codec.digest.DigestUtils.sha256Hex(arg);
|
||||
}
|
||||
}
|
49
src/main/java/org/bdware/sc/boundry/utils/EmailUtil.java
Normal file
49
src/main/java/org/bdware/sc/boundry/utils/EmailUtil.java
Normal file
@ -0,0 +1,49 @@
|
||||
package org.bdware.sc.boundry.utils;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import javax.mail.*;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import java.security.Security;
|
||||
import java.util.Properties;
|
||||
|
||||
public class EmailUtil {
|
||||
public static String sendEmail(String json) {
|
||||
try {
|
||||
final JsonObject jo = JsonParser.parseString(json).getAsJsonObject();
|
||||
Properties props = new Properties();
|
||||
props.setProperty("mail.debug", "false");
|
||||
props.setProperty("mail.smtp.auth", "true");
|
||||
props.setProperty("mail.smtp.host", jo.get("host").getAsString());
|
||||
props.setProperty("mail.smtp.port", jo.get("port").getAsString());
|
||||
props.setProperty("mail.transport.protocol", "smtp");
|
||||
props.put("mail.smtp.auth", "true");
|
||||
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
|
||||
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
|
||||
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
|
||||
props.setProperty("mail.smtp.socketFactory.fallback", "false");
|
||||
props.setProperty("mail.smtp.socketFactory.port", jo.get("port").getAsString());
|
||||
Session session = Session.getDefaultInstance(props, new Authenticator() {
|
||||
public PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(jo.get("from").getAsString(),
|
||||
jo.get("pwd").getAsString()); // 发件人邮件用户名、密码
|
||||
}
|
||||
});
|
||||
// 创建邮件对象
|
||||
|
||||
Message msg = new MimeMessage(session);
|
||||
msg.setSubject(jo.get("subject").getAsString());
|
||||
msg.setText(jo.get("content").getAsString());
|
||||
msg.setFrom(new InternetAddress(jo.get("from").getAsString()));
|
||||
msg.addRecipient(Message.RecipientType.TO,
|
||||
new InternetAddress(jo.get("to").getAsString()));
|
||||
Transport.send(msg);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return "failed";
|
||||
}
|
||||
return "success";
|
||||
}
|
||||
}
|
@ -12,7 +12,7 @@ import java.io.PrintStream;
|
||||
public class FileUtil extends org.bdware.sc.util.FileUtil {
|
||||
private static String getInternalFile(String path) {
|
||||
File parent = new File("./ContractDB/" + ContractProcess.instance.getContractName());
|
||||
if (path.contains("..")) {
|
||||
if (path.contains("")) {
|
||||
return null;
|
||||
}
|
||||
File f = new File(parent, path);
|
||||
|
@ -48,7 +48,8 @@ public class HttpUtil {
|
||||
ScriptObjectMirror som = (ScriptObjectMirror) headers;
|
||||
for (String key : som.getOwnKeys(true)) {
|
||||
Object val = som.get(key);
|
||||
if (val instanceof String) connection.setRequestProperty(key, (String) val);
|
||||
if (val instanceof String)
|
||||
connection.setRequestProperty(key, (String) val);
|
||||
}
|
||||
} else {
|
||||
connection.setRequestProperty("Accept", "application/json");
|
||||
@ -77,9 +78,11 @@ public class HttpUtil {
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static String encodeURI(String str) {
|
||||
return URLEncoder.encode(str);
|
||||
}
|
||||
|
||||
public static String decodeURI(String str) {
|
||||
return URLDecoder.decode(str);
|
||||
}
|
||||
@ -131,7 +134,8 @@ public class HttpUtil {
|
||||
connection.setRequestProperty("Content-Type", "application/json");
|
||||
}
|
||||
connection.connect();
|
||||
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8);
|
||||
OutputStreamWriter out =
|
||||
new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8);
|
||||
out.append((String) str.get("data"));
|
||||
out.flush();
|
||||
out.close();
|
||||
@ -156,24 +160,17 @@ public class HttpUtil {
|
||||
private static CloseableHttpClient getHttpClient(String url) {
|
||||
try {
|
||||
SSLContext sslcontext =
|
||||
SSLContexts.custom()
|
||||
.loadTrustMaterial(
|
||||
null,
|
||||
(arg0, arg1) -> true)
|
||||
.build();
|
||||
SSLContexts.custom().loadTrustMaterial(null, (arg0, arg1) -> true).build();
|
||||
|
||||
SSLConnectionSocketFactory sslSf =
|
||||
new SSLConnectionSocketFactory(
|
||||
sslcontext, null, null, new NoopHostnameVerifier());
|
||||
SSLConnectionSocketFactory sslSf = new SSLConnectionSocketFactory(sslcontext, null,
|
||||
null, new NoopHostnameVerifier());
|
||||
int tle = 10;
|
||||
if (url.contains("data.tj.gov.cn")) {
|
||||
tle = 3;
|
||||
}
|
||||
return HttpClients.custom()
|
||||
.setSSLSocketFactory(sslSf)
|
||||
return HttpClients.custom().setSSLSocketFactory(sslSf)
|
||||
.setKeepAliveStrategy((arg0, arg1) -> 0)
|
||||
.setConnectionTimeToLive(tle, TimeUnit.SECONDS)
|
||||
.build();
|
||||
.setConnectionTimeToLive(tle, TimeUnit.SECONDS).build();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -235,18 +232,15 @@ public class HttpUtil {
|
||||
RequestBody.create(MediaType.parse("application/json; charset=utf-8"), req.data);
|
||||
Request request = new Request.Builder().url(req.url).post(body).build(); // 2.瀹氫箟涓<EFBFBD>涓猺equest
|
||||
Call call = okHttpClient.newCall(request); //
|
||||
call.enqueue(
|
||||
new Callback() { //
|
||||
call.enqueue(new Callback() { //
|
||||
@Override
|
||||
public void onFailure(Call call, IOException e) {
|
||||
}
|
||||
public void onFailure(Call call, IOException e) {}
|
||||
|
||||
@Override
|
||||
public void onResponse(Call call, Response response) throws IOException {
|
||||
String result = response.body().string(); //
|
||||
System.out.println("currentEngine:");
|
||||
DesktopEngine.applyWithGlobal(
|
||||
callback, currentEngine.getNashornGlobal(), result);
|
||||
DesktopEngine.applyWithGlobal(callback, currentEngine.getNashornGlobal(), result);
|
||||
}
|
||||
});
|
||||
return "success";
|
||||
|
79
src/main/java/org/bdware/sc/boundry/utils/IRPUtil.java
Normal file
79
src/main/java/org/bdware/sc/boundry/utils/IRPUtil.java
Normal file
@ -0,0 +1,79 @@
|
||||
package org.bdware.sc.boundry.utils;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.doip.audit.EndpointConfig;
|
||||
import org.bdware.doip.audit.client.AuditIrpClient;
|
||||
import org.bdware.doip.audit.config.TempConfigStorage;
|
||||
import org.bdware.irp.stateinfo.StateInfoBase;
|
||||
import org.bdware.sc.compiler.PermissionStub;
|
||||
import org.bdware.sc.engine.JSONTool;
|
||||
import org.bdware.sc.node.Permission;
|
||||
import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror;
|
||||
|
||||
|
||||
@PermissionStub(permission = Permission.IRP)
|
||||
public class IRPUtil {
|
||||
private static final Logger LOGGER = LogManager.getLogger(IRPUtil.class);
|
||||
private final AuditIrpClient auditIrpClient;
|
||||
|
||||
private IRPUtil(String config) {
|
||||
TempConfigStorage configStorage = new TempConfigStorage(config);
|
||||
JsonObject jo = configStorage.load();
|
||||
EndpointConfig endpointConfig = configStorage.loadAsEndpointConfig();
|
||||
|
||||
if (jo.has("clientDoId")) {
|
||||
this.auditIrpClient =
|
||||
new AuditIrpClient(jo.get("clientDoId").getAsString(), endpointConfig);
|
||||
} else {
|
||||
this.auditIrpClient = new AuditIrpClient(endpointConfig);
|
||||
}
|
||||
}
|
||||
|
||||
public static IRPUtil createClient(String jsonObject) {
|
||||
return new IRPUtil(jsonObject);
|
||||
}
|
||||
|
||||
public String test(String doi) {
|
||||
return "create DOClient And hello " + doi + " World";
|
||||
}
|
||||
|
||||
public Object resolve(String doId) {
|
||||
try {
|
||||
StateInfoBase result = auditIrpClient.resolve(doId);
|
||||
JsonObject r = new JsonObject();
|
||||
r.addProperty("code", 0);
|
||||
r.add("handleValues", result.handleValues);
|
||||
return JSONTool.convertJsonElementToMirror(result.handleValues);
|
||||
} catch (Exception e) {
|
||||
JsonObject r = new JsonObject();
|
||||
r.addProperty("code", 1);
|
||||
r.addProperty("msg", e.getMessage());
|
||||
return JSONTool.convertJsonElementToMirror(r);
|
||||
}
|
||||
}
|
||||
|
||||
public Object register(ScriptObjectMirror mirror) {
|
||||
JsonObject jo = JSONTool.convertMirrorToJson(mirror).getAsJsonObject();
|
||||
StateInfoBase base = new StateInfoBase();
|
||||
JsonObject result = new JsonObject();
|
||||
if (jo.has("identifier"))
|
||||
base.identifier = jo.get("identifier").getAsString();
|
||||
base.handleValues = jo.get("handleValues").getAsJsonObject();
|
||||
try {
|
||||
String val = auditIrpClient.register(base);
|
||||
result.addProperty("code", 0);
|
||||
result.addProperty("doId", val);
|
||||
if (val == null || val.length() == 0) {
|
||||
result.addProperty("code", 1);
|
||||
result.addProperty("msg", auditIrpClient.getLastResponse().getResponseMessage());
|
||||
}
|
||||
return JSONTool.convertJsonElementToMirror(result);
|
||||
} catch (Exception e) {
|
||||
result.addProperty("code", 1);
|
||||
result.addProperty("msg", e.getMessage());
|
||||
return JSONTool.convertJsonElementToMirror(result);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,11 +1,18 @@
|
||||
package org.bdware.sc.boundry.utils;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.bdledger.api.grpc.Client;
|
||||
import org.bdware.bdledger.api.grpc.pb.CommonProto.Transaction;
|
||||
import org.bdware.bdledger.api.grpc.pb.CommonProto.TransactionType;
|
||||
import org.bdware.bdledger.api.grpc.pb.LedgerProto.SendTransactionResponse;
|
||||
import org.bdware.bdledger.api.grpc.pb.QueryProto.GetTransactionByHashResponse;
|
||||
import org.bdware.sc.boundry.JavaScriptEntry;
|
||||
import org.bdware.sc.compiler.PermissionStub;
|
||||
import org.bdware.sc.engine.JSONTool;
|
||||
import org.bdware.sc.node.Permission;
|
||||
import org.bdware.sc.util.HashUtil;
|
||||
import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror;
|
||||
@ -19,11 +26,29 @@ import java.security.SecureRandom;
|
||||
public class LedgerUtil {
|
||||
static SecureRandom random = new SecureRandom((System.currentTimeMillis() + "").getBytes());
|
||||
|
||||
public static Object getLedgerParams() {
|
||||
// format:{nodes:[{ip:ip,port:port}]}
|
||||
String ledgerParam = JavaScriptEntry.get.syncGet("", "getLedgerParams", "");
|
||||
JsonElement jo = JsonParser.parseString(ledgerParam);
|
||||
return JSONTool.convertJsonElementToMirror(jo);
|
||||
}
|
||||
|
||||
public static Client getDefaultClient() {
|
||||
String ledgerParam = JavaScriptEntry.get.syncGet("", "getLedgerParams", "");
|
||||
JsonElement jo = JsonParser.parseString(ledgerParam);
|
||||
JsonObject param =
|
||||
jo.getAsJsonObject().get("nodes").getAsJsonArray().get(0).getAsJsonObject();
|
||||
return new Client(param.get("ip").getAsString(), param.get("port").getAsInt());
|
||||
}
|
||||
|
||||
public static Client getClient(ScriptObjectMirror str) {
|
||||
return new Client((String) str.get("ip"), Integer.parseInt(str.get("port").toString()));
|
||||
}
|
||||
|
||||
static Logger LOGGER = LogManager.getLogger(LedgerUtil.class);
|
||||
|
||||
public static ScriptObject queryByHash(Client c, ScriptObjectMirror str) {
|
||||
LOGGER.info("TID:" + Thread.currentThread().getId());
|
||||
String ledger = str.get("ledger").toString();
|
||||
String hash = str.get("hash").toString();
|
||||
JO ret = new JO(PropertyMap.newMap());
|
||||
@ -33,6 +58,8 @@ public class LedgerUtil {
|
||||
ret.put("to", HashUtil.byteArray2Str(transaction.getTo().toByteArray(), 0), false);
|
||||
ret.put("type", transaction.getType().toString(), false);
|
||||
ret.put("data", new String(transaction.getData().toByteArray()), false);
|
||||
ret.put("blockHsah", HashUtil.byteArray2Str(transaction.getBlockHash().toByteArray(), 0),
|
||||
false);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -41,14 +68,8 @@ public class LedgerUtil {
|
||||
String from = str.get("from").toString();
|
||||
String to = str.get("to").toString();
|
||||
String data = str.get("data").toString();
|
||||
SendTransactionResponse result =
|
||||
c.sendTransactionSync(
|
||||
ledger,
|
||||
TransactionType.MESSAGE,
|
||||
from,
|
||||
random.nextLong(),
|
||||
to,
|
||||
data.getBytes());
|
||||
SendTransactionResponse result = c.sendTransactionSync(ledger, TransactionType.MESSAGE,
|
||||
from, random.nextLong(), to, data.getBytes());
|
||||
return HashUtil.byteArray2Str(result.getHash().toByteArray(), 0);
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,34 @@ package org.bdware.sc.boundry.utils;
|
||||
import org.bdware.sc.compiler.PermissionStub;
|
||||
import org.bdware.sc.node.Permission;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@PermissionStub(permission = Permission.MongoDB)
|
||||
public class MongoDBUtil extends org.bdware.sc.blockdb.MongoDBUtil {
|
||||
public class MongoDBUtil {
|
||||
public static Object connect(String url, int port, String dbName, String usrName, String pwd) {
|
||||
try {
|
||||
Class serverAddr = Class.forName("com.mongodb.ServerAddress");
|
||||
Constructor cons = serverAddr.getConstructor(String.class, Integer.TYPE);
|
||||
Object serverAddress = cons.newInstance(url, port);
|
||||
List addrs = new ArrayList<>();
|
||||
addrs.add(serverAddress);
|
||||
Method createeScramSha1 =
|
||||
Class.forName("com.mongodb.MongoCredential").getDeclaredMethod(
|
||||
"createScramSha1Credential", String.class, String.class, char[].class);
|
||||
Object credential = createeScramSha1.invoke(null, usrName, dbName, pwd.toCharArray());
|
||||
List credentials = new ArrayList<>();
|
||||
credentials.add(credential);
|
||||
Constructor mongoClient =
|
||||
Class.forName("com.mongodb.MongoClient").getConstructor(List.class, List.class);
|
||||
Object client = mongoClient.newInstance(addrs, credentials);
|
||||
// 通过连接认证获取MongoDB连接
|
||||
return client;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,137 @@
|
||||
package org.bdware.sc.boundry.utils;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.sc.ContractProcess;
|
||||
import org.bdware.sc.compiler.PermissionStub;
|
||||
import org.bdware.sc.db.MultiIndexTimeRocksDBUtil;
|
||||
import org.bdware.sc.node.Permission;
|
||||
import org.rocksdb.RocksDB;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@PermissionStub(permission = Permission.MultiTagIndexDB)
|
||||
public class MultiTagIndexDBUtil {
|
||||
private static final Logger LOGGER = LogManager.getLogger(MultiIndexTimeRocksDBUtil.class);
|
||||
|
||||
static Map<String, MultiTagIndexDBUtil> cacheDB = new HashMap<>();
|
||||
|
||||
static {
|
||||
RocksDB.loadLibrary();
|
||||
}
|
||||
|
||||
MultiIndexTimeRocksDBUtil rocksDB;
|
||||
String path;
|
||||
|
||||
public MultiTagIndexDBUtil(String path, String tableName) {
|
||||
try {
|
||||
this.path = path;
|
||||
File parent = new File("./ContractDB/" + ContractProcess.getContractDir());
|
||||
File dir = new File(parent, path);
|
||||
|
||||
LOGGER.info("init RocksDB in " + dir.getAbsolutePath());
|
||||
if (!dir.exists()) {
|
||||
LOGGER.trace("create directory " + dir.getAbsolutePath() + ": " + dir.mkdirs());
|
||||
}
|
||||
File lockFile = new File(dir, "LOCK");
|
||||
LOGGER.trace("delete file" + lockFile.getAbsolutePath() + ": " + lockFile.delete());
|
||||
rocksDB = new MultiIndexTimeRocksDBUtil(dir.getAbsolutePath(), tableName);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static MultiTagIndexDBUtil loadDB(String path, String tableName) {
|
||||
if (cacheDB.containsKey(path)) {
|
||||
return cacheDB.get(path);
|
||||
}
|
||||
MultiTagIndexDBUtil ret = new MultiTagIndexDBUtil(path, tableName);
|
||||
cacheDB.put(path, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
rocksDB.close();
|
||||
cacheDB.remove(path);
|
||||
}
|
||||
|
||||
public String get(String key) {
|
||||
try {
|
||||
return new String(rocksDB.get(key));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public long size() {
|
||||
try {
|
||||
return rocksDB.size();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void put(String label, String value) {
|
||||
try {
|
||||
rocksDB.put(label, value);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> getTags(String prefix) {
|
||||
return rocksDB.getIndexStartWith(prefix);
|
||||
}
|
||||
|
||||
public List<String> getAllTags() {
|
||||
List<String> list = rocksDB.getAllIndexKey();
|
||||
return list;
|
||||
}
|
||||
|
||||
public long getSize(String tag) {
|
||||
try {
|
||||
return rocksDB.size(tag);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
return 0L;
|
||||
}
|
||||
|
||||
public long queryOffset(String tag, long startTimestamp) {
|
||||
return rocksDB.queryOffset(tag, startTimestamp);
|
||||
}
|
||||
|
||||
public List<JsonObject> queryByOffset(String tag, long offset, int count) {
|
||||
return rocksDB.queryByOffset(tag, offset, count);
|
||||
}
|
||||
|
||||
public List<Long> countInInterval(String tag, long startTime, long endTime, long interval) {
|
||||
List<Long> ret = new ArrayList<>();
|
||||
if (interval <= 0)
|
||||
return ret;
|
||||
long start = rocksDB.queryOffset(tag, startTime);
|
||||
long delta;
|
||||
startTime += interval;
|
||||
for (; startTime < endTime; startTime += interval) {
|
||||
delta = rocksDB.queryOffset(tag, startTime);
|
||||
ret.add(delta - start);
|
||||
start = delta;
|
||||
}
|
||||
delta = rocksDB.queryOffset(tag, endTime);
|
||||
ret.add(delta - start);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public List<String> queryInInterval(String tag, long startTime, long endTime) {
|
||||
return rocksDB.queryByDateAsString(tag, startTime, endTime);
|
||||
}
|
||||
}
|
@ -7,7 +7,6 @@ import org.bdware.sc.compiler.PermissionStub;
|
||||
import org.bdware.sc.node.Permission;
|
||||
import org.rocksdb.Options;
|
||||
import org.rocksdb.RocksDB;
|
||||
import org.rocksdb.RocksDBException;
|
||||
import org.rocksdb.RocksIterator;
|
||||
import wrp.jdk.nashorn.internal.runtime.PropertyMap;
|
||||
import wrp.jdk.nashorn.internal.runtime.ScriptObject;
|
||||
@ -30,32 +29,43 @@ public class RocksDBUtil {
|
||||
RocksDB rocksDB;
|
||||
String path;
|
||||
|
||||
public RocksDBUtil(String path, boolean readOnly) {
|
||||
try {
|
||||
public void tryLoad(String path, boolean readOnly) throws Exception {
|
||||
this.path = path;
|
||||
Options options = new Options();
|
||||
options.setCreateIfMissing(true);
|
||||
|
||||
File parent = new File("./ContractDB/" + ContractProcess.getContractDir());
|
||||
File dir = new File(parent, path);
|
||||
System.out.println("[EEEEEEEEEERO]");
|
||||
LOGGER.info("init RocksDB in " + dir.getAbsolutePath());
|
||||
// LOGGER.info("init RocksDB in " + dir.getAbsolutePath());
|
||||
if (!dir.exists()) {
|
||||
LOGGER.trace("create directory " + dir.getAbsolutePath() + ": " + dir.mkdirs());
|
||||
LOGGER.info("create directory " + dir.getAbsolutePath() + ": " + dir.mkdirs());
|
||||
}
|
||||
File lockFile = new File(dir, "LOCK");
|
||||
LOGGER.trace("delete file" + lockFile.getAbsolutePath() + ": " + lockFile.delete());
|
||||
if (readOnly) {
|
||||
rocksDB = RocksDB.openReadOnly(options, dir.getAbsolutePath());
|
||||
} else {
|
||||
rocksDB = RocksDB.open(options, dir.getAbsolutePath());
|
||||
}
|
||||
} catch (RocksDBException e) {
|
||||
}
|
||||
|
||||
public RocksDBUtil(String path, boolean readOnly) {
|
||||
try {
|
||||
tryLoad(path, readOnly);
|
||||
} catch (Exception e) {
|
||||
LOGGER.info("======TRY Load Again 2s later====");
|
||||
try {
|
||||
Thread.sleep(2000L);
|
||||
} catch (InterruptedException ex) {
|
||||
}
|
||||
try {
|
||||
tryLoad(path, readOnly);
|
||||
} catch (Exception ex) {
|
||||
LOGGER.info("======LOAD FAILED!====");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static RocksDBUtil loadDB(String path, boolean readOnly) {
|
||||
public static synchronized RocksDBUtil loadDB(String path, boolean readOnly) {
|
||||
if (cacheDB.containsKey(path)) {
|
||||
return cacheDB.get(path);
|
||||
}
|
||||
@ -77,7 +87,6 @@ public class RocksDBUtil {
|
||||
try {
|
||||
return new String(rocksDB.get(key.getBytes()));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -120,7 +129,6 @@ public class RocksDBUtil {
|
||||
}
|
||||
|
||||
public ScriptObject getNext(RocksIterator iter) {
|
||||
|
||||
if (iter.isValid()) {
|
||||
JO ret = new JO(PropertyMap.newMap());
|
||||
ret.put("key", new String(iter.key()), false);
|
||||
@ -130,4 +138,8 @@ public class RocksDBUtil {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String clean() {
|
||||
return "todo";
|
||||
}
|
||||
}
|
||||
|
@ -46,14 +46,13 @@ public class SM2Util {
|
||||
JO ret = new JO(PropertyMap.newMap());
|
||||
try {
|
||||
byte[] sig = ByteUtils.fromHexString(signature);
|
||||
ECPublicKeyParameters pubKey =
|
||||
BCECUtil.createECPublicKeyFromStrParameters(
|
||||
pubKeyStr,
|
||||
org.zz.gmhelper.SM2Util.CURVE,
|
||||
org.zz.gmhelper.SM2Util.DOMAIN_PARAMS);
|
||||
ECPublicKeyParameters pubKey = BCECUtil.createECPublicKeyFromStrParameters(pubKeyStr,
|
||||
org.zz.gmhelper.SM2Util.CURVE, org.zz.gmhelper.SM2Util.DOMAIN_PARAMS);
|
||||
boolean value = org.zz.gmhelper.SM2Util.verify(pubKey, content.getBytes(), sig);
|
||||
if (value) ret.put("status", "success", false);
|
||||
else ret.put("status", "failed", false);
|
||||
if (value)
|
||||
ret.put("status", "success", false);
|
||||
else
|
||||
ret.put("status", "failed", false);
|
||||
ret.put("result", value, false);
|
||||
} catch (Exception e) {
|
||||
ret.put("status", "failed", false);
|
||||
@ -65,9 +64,8 @@ public class SM2Util {
|
||||
|
||||
public static String encrypt(String content, String pubkey) {
|
||||
try {
|
||||
return ByteUtils.toHexString(
|
||||
org.zz.gmhelper.SM2Util.encrypt(
|
||||
SM2KeyPair.publicKeyStr2ECPoint(pubkey), content.getBytes()));
|
||||
return ByteUtils.toHexString(org.zz.gmhelper.SM2Util
|
||||
.encrypt(SM2KeyPair.publicKeyStr2ECPoint(pubkey), content.getBytes()));
|
||||
} catch (InvalidCipherTextException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -76,12 +74,10 @@ public class SM2Util {
|
||||
|
||||
public static String decrypt(String content, String privateKey) {
|
||||
try {
|
||||
ECPrivateKeyParameters privateKeyParam =
|
||||
new ECPrivateKeyParameters(
|
||||
ECPrivateKeyParameters privateKeyParam = new ECPrivateKeyParameters(
|
||||
new BigInteger(privateKey, 16), org.zz.gmhelper.SM2Util.DOMAIN_PARAMS);
|
||||
return new String(
|
||||
org.zz.gmhelper.SM2Util.decrypt(
|
||||
privateKeyParam, ByteUtils.fromHexString(content)));
|
||||
return new String(org.zz.gmhelper.SM2Util.decrypt(privateKeyParam,
|
||||
ByteUtils.fromHexString(content)));
|
||||
} catch (InvalidCipherTextException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -16,8 +16,7 @@ public class SQLUtil {
|
||||
.setContextClassLoader(ContractProcess.instance.engine.getClassLoad());
|
||||
Class.forName(driver, true, ContractProcess.instance.engine.getClassLoad());
|
||||
} catch (Exception e) {
|
||||
System.out.println(
|
||||
"Still can't find class! Cl of SQLUtil:\n\t\t"
|
||||
System.out.println("Still can't find class! Cl of SQLUtil:\n\t\t"
|
||||
+ SQLUtil.class.getClassLoader());
|
||||
System.out.println(
|
||||
"Cl of DEgine:\n\t\t" + ContractProcess.instance.engine.getClassLoad());
|
||||
@ -40,18 +39,15 @@ public class SQLUtil {
|
||||
if (password != null && !"undefined".equals(password)) {
|
||||
info.put("password", password);
|
||||
}
|
||||
if (url.startsWith("jdbc:postgresql")) info.put("sslmode", "allow");
|
||||
if (url.startsWith("jdbc:postgresql"))
|
||||
info.put("sslmode", "allow");
|
||||
|
||||
Class<?> clz =
|
||||
Class.forName(
|
||||
"java.sql.DriverManager",
|
||||
true,
|
||||
Class<?> clz = Class.forName("java.sql.DriverManager", true,
|
||||
ContractProcess.instance.engine.getClassLoad());
|
||||
// set caller class into null, thus use YJSClassLoader in
|
||||
// DriverManager.isDriverAllowed(driver,classloader);
|
||||
Method m =
|
||||
clz.getDeclaredMethod(
|
||||
"getConnection", String.class, Properties.class, Class.class);
|
||||
Method m = clz.getDeclaredMethod("getConnection", String.class, Properties.class,
|
||||
Class.class);
|
||||
m.setAccessible(true);
|
||||
return (Connection) m.invoke(null, url, info, null);
|
||||
} catch (Exception e) {
|
||||
|
@ -17,14 +17,14 @@ public class UtilRegistry {
|
||||
public static Map<String, String> stubClzNameMap = new HashMap<>();
|
||||
|
||||
public static List<Class<?>> getUtilClasses() {
|
||||
|
||||
List<String> allName = Permission.allName();
|
||||
List<Class<?>> ret = new ArrayList<>();
|
||||
try {
|
||||
for (String name : allName) {
|
||||
Class<?> clz;
|
||||
try {
|
||||
clz = Class.forName(String.format("%s.%sUtil", UtilRegistry.class.getPackage().getName(), name));
|
||||
clz = Class.forName(String.format("%s.%sUtil",
|
||||
UtilRegistry.class.getPackage().getName(), name));
|
||||
ret.add(clz);
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
@ -53,11 +53,8 @@ public class UtilRegistry {
|
||||
|
||||
public static String getInitStr(String s, boolean open) {
|
||||
if (stubClzNameMap.containsKey(s)) {
|
||||
String ret =
|
||||
String.format(
|
||||
"%sUtil = %s.%sUtil%s;\n",
|
||||
s, UtilRegistry.class.getPackage().getName(), s, open ? "" : "Stub");
|
||||
LOGGER.debug(ret);
|
||||
String ret = String.format("%sUtil = %s.%sUtil%s;\n", s,
|
||||
UtilRegistry.class.getPackage().getName(), s, open ? "" : "Stub");
|
||||
return ret;
|
||||
}
|
||||
return "";
|
||||
|
@ -3,14 +3,20 @@ package org.bdware.sc.compiler;
|
||||
import org.bdware.sc.node.AnnotationNode;
|
||||
import org.bdware.sc.node.ContractNode;
|
||||
import org.bdware.sc.node.FunctionNode;
|
||||
import org.bdware.sc.node.InterfaceNode;
|
||||
|
||||
public abstract class AnnotationProcessor {
|
||||
public void processContract(AnnotationNode anno, ContractNode contractNode) {
|
||||
return;
|
||||
}
|
||||
|
||||
public void processFunction(
|
||||
AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) {
|
||||
public void processFunction(AnnotationNode anno, ContractNode contractNode,
|
||||
FunctionNode functionNode) throws Exception {
|
||||
return;
|
||||
}
|
||||
|
||||
public void processInterface(AnnotationNode anno, ContractNode contractNode,
|
||||
InterfaceNode functionNode) throws Exception {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
15
src/main/java/org/bdware/sc/compiler/PermissionStub.java
Normal file
15
src/main/java/org/bdware/sc/compiler/PermissionStub.java
Normal file
@ -0,0 +1,15 @@
|
||||
package org.bdware.sc.compiler;
|
||||
|
||||
import org.bdware.sc.node.Permission;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface PermissionStub {
|
||||
Permission permission();
|
||||
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
package org.bdware.sc.compiler;
|
||||
|
||||
import org.objectweb.asm.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class PermissionStubGenerator extends ClassVisitor implements Opcodes {
|
||||
private final String p;
|
||||
|
||||
public PermissionStubGenerator(int api, ClassVisitor cn, String permission) {
|
||||
super(api, cn);
|
||||
this.p = permission;
|
||||
}
|
||||
|
||||
public static byte[] generateStub(Class<?> clz, String permission) {
|
||||
PermissionStub stub = clz.getAnnotation(PermissionStub.class);
|
||||
if (stub == null)
|
||||
return null;
|
||||
try {
|
||||
String resource = clz.getCanonicalName().replaceAll("\\.", "/") + ".class";
|
||||
InputStream input = clz.getClassLoader().getResourceAsStream(resource);
|
||||
ClassReader cr;
|
||||
cr = new ClassReader(input);
|
||||
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
|
||||
PermissionStubGenerator generator = new PermissionStubGenerator(ASM4, cw, permission);
|
||||
cr.accept(generator, 0);
|
||||
|
||||
return cw.toByteArray();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void visit(int version, int access, String name, String signature, String superName,
|
||||
String[] interfaces) {
|
||||
if (cv != null) {
|
||||
cv.visit(version, access, name + "Stub", null, "java/lang/Object", null);
|
||||
}
|
||||
MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
|
||||
mv.visitCode();
|
||||
Label l0 = new Label();
|
||||
mv.visitLabel(l0);
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
|
||||
mv.visitInsn(RETURN);
|
||||
Label l1 = new Label();
|
||||
mv.visitLabel(l1);
|
||||
// mv.visitLocalVariable("this", "Lcom/yancloud/sc/boundry/utils/FileUtilStub;",
|
||||
// null, l0, l1, 0);
|
||||
mv.visitMaxs(1, 1);
|
||||
mv.visitEnd();
|
||||
}
|
||||
|
||||
public MethodVisitor visitMethod(int access, String name, String desc, String signature,
|
||||
String[] exceptions) {
|
||||
if (cv != null) {
|
||||
if (name.equals("<clinit>"))
|
||||
return null;
|
||||
if (name.equals("<init>")) {
|
||||
return null;
|
||||
}
|
||||
|
||||
MethodVisitor mv = cv.visitMethod(access, name, desc, signature,
|
||||
new String[] {"javax/script/ScriptException"});
|
||||
mv.visitCode();
|
||||
Label l0 = new Label();
|
||||
mv.visitLabel(l0);
|
||||
mv.visitTypeInsn(NEW, "javax/script/ScriptException");
|
||||
mv.visitInsn(DUP);
|
||||
mv.visitLdcInsn("Do not have " + p + " Permission");
|
||||
mv.visitMethodInsn(INVOKESPECIAL, "javax/script/ScriptException", "<init>",
|
||||
"(Ljava/lang/String;)V");
|
||||
mv.visitInsn(ATHROW);
|
||||
mv.visitMaxs(5, 20);
|
||||
mv.visitEnd();
|
||||
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -21,6 +21,7 @@ import wrp.jdk.nashorn.internal.runtime.options.Options;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@ -33,8 +34,7 @@ public class YJSCompiler {
|
||||
ContractNode contract;
|
||||
private static final Logger LOGGER = LogManager.getLogger(YJSCompiler.class);
|
||||
|
||||
public YJSCompiler() {
|
||||
}
|
||||
public YJSCompiler() {}
|
||||
|
||||
public static ScriptFunction compileWithGlobal(Source source, Global global, Context context) {
|
||||
Global oldGlobal = Context.getGlobal();
|
||||
@ -54,8 +54,8 @@ public class YJSCompiler {
|
||||
}
|
||||
}
|
||||
|
||||
private static Context makeContext(
|
||||
final InputStream in, final OutputStream out, final OutputStream err) {
|
||||
private static Context makeContext(final InputStream in, final OutputStream out,
|
||||
final OutputStream err) {
|
||||
final PrintStream pout =
|
||||
out instanceof PrintStream ? (PrintStream) out : new PrintStream(out);
|
||||
final PrintStream perr =
|
||||
@ -91,14 +91,8 @@ public class YJSCompiler {
|
||||
}
|
||||
}
|
||||
}
|
||||
return new Context(
|
||||
options,
|
||||
errors,
|
||||
wout,
|
||||
werr,
|
||||
Thread.currentThread().getContextClassLoader(),
|
||||
new YJSFilter(),
|
||||
null);
|
||||
return new Context(options, errors, wout, werr,
|
||||
Thread.currentThread().getContextClassLoader(), new YJSFilter(), null);
|
||||
}
|
||||
|
||||
public ContractZipBundle compile(ZipFile zf) throws Exception {
|
||||
@ -114,38 +108,59 @@ public class YJSCompiler {
|
||||
// Gson gson = new GsonBuilder().registerTypeAdapter(Contract.Type.class,
|
||||
// typeAdapter)
|
||||
|
||||
ContractManifest cm =
|
||||
JsonUtil.GSON.fromJson(
|
||||
new InputStreamReader(manifestInput), ContractManifest.class);
|
||||
|
||||
ContractManifest cm = JsonUtil.GSON.fromJson(new InputStreamReader(manifestInput),
|
||||
ContractManifest.class);
|
||||
// 如果没有就不限制,根据gas进行插装
|
||||
if (0L != cm.getInsnLimit()) {
|
||||
System.out.println("++++++++++++++++++++++true");
|
||||
LOGGER.info("++++++++++++++++++++++true");
|
||||
}
|
||||
czb.setManifest(cm);
|
||||
Set<String> toParse = new HashSet<>();
|
||||
toParse.add(cm.main);
|
||||
Set<String> todo = new HashSet<>();
|
||||
Set<String> allEntries = new HashSet<>();
|
||||
Enumeration<? extends ZipEntry> iter = zf.entries();
|
||||
for (; iter.hasMoreElements();) {
|
||||
ZipEntry ele = iter.nextElement();
|
||||
if (ele != null)
|
||||
allEntries.add(ele.getName());
|
||||
}
|
||||
while (toParse.size() > 0) {
|
||||
for (String str : toParse) {
|
||||
if (czb.containsPath(str)) {
|
||||
continue;
|
||||
}
|
||||
ZipEntry entry = zf.getEntry("/" + str);
|
||||
ZipEntry entry = zf.getEntry(str.startsWith("/") ? str : "/" + str);
|
||||
LOGGER.info("load yjs:" + str);
|
||||
if (null == entry) {
|
||||
throw new IllegalStateException("missing import:" + str);
|
||||
}
|
||||
ContractNode cn = compile(zf.getInputStream(entry), str);
|
||||
String cnPath = entry.getName();
|
||||
int i = cnPath.lastIndexOf("/");
|
||||
String cnDir = "";
|
||||
if (i != -1)
|
||||
cnDir = cnPath.substring(0, i);
|
||||
czb.put(str, cn);
|
||||
System.out.println("----" + str);
|
||||
for (ImportNode in : cn.getImports()) {
|
||||
todo.add(in.getPath());
|
||||
String path = in.getPath();
|
||||
if (!path.startsWith("/"))
|
||||
path = cnDir + "/" + path;
|
||||
path = path.replaceAll("/\\./", "/");
|
||||
todo.add(path);
|
||||
}
|
||||
}
|
||||
toParse.clear();
|
||||
for (String str : todo) {
|
||||
if (!czb.containsPath(str)) {
|
||||
if (allEntries.contains(str))
|
||||
toParse.add(str);
|
||||
else {
|
||||
// TODO parse manifest.json first?
|
||||
for (String entry : allEntries)
|
||||
if (!czb.containsPath(entry) && entry.startsWith(str)
|
||||
&& entry.endsWith(".yjs")) {
|
||||
toParse.add(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
todo.clear();
|
||||
@ -153,20 +168,30 @@ public class YJSCompiler {
|
||||
// add function _preSub
|
||||
// Kaidong Wu
|
||||
String preSubConName = cm.main.substring(0, cm.main.length() - 4) + "PreSub";
|
||||
String preSubContract =
|
||||
"contract "
|
||||
+ preSubConName
|
||||
String preSubContract = "contract " + preSubConName
|
||||
+ " { function _preSub (e) { YancloudUtil.preSub(e.topic, e.content); }}";
|
||||
ContractNode preSubNode =
|
||||
compile(
|
||||
new ByteArrayInputStream(preSubContract.getBytes(StandardCharsets.UTF_8)),
|
||||
compile(new ByteArrayInputStream(preSubContract.getBytes(StandardCharsets.UTF_8)),
|
||||
preSubConName + ".yjs");
|
||||
czb.put(preSubConName + ".yjs", preSubNode);
|
||||
LOGGER.info("--compile-- " + preSubConName);
|
||||
|
||||
String globalBeanName = cm.main.substring(0, cm.main.length() - 4) + "GlobalBean";
|
||||
String globalBeanContract = "contract " + globalBeanName
|
||||
+ "{ function setGlobal (_global) { Global = _global; }\n"
|
||||
+ " function getGlobal () { return Global; }}";
|
||||
czb.put(globalBeanName + ".yjs",
|
||||
compile(new ByteArrayInputStream(
|
||||
globalBeanContract.getBytes(StandardCharsets.UTF_8)),
|
||||
globalBeanName + ".yjs"));
|
||||
LOGGER.info("--compile-- " + globalBeanName);
|
||||
czb.setMergedContractNode();
|
||||
ContractNode node = czb.mergeContractNode();
|
||||
handleFunctionAnnotation(node);
|
||||
return czb;
|
||||
}
|
||||
|
||||
public ContractNode compile(InputStream input, String fileName) throws IOException {
|
||||
public ContractNode compile(InputStream input, String fileName) throws Exception {
|
||||
// 词法分析
|
||||
JavaScriptLexer lexer = new JavaScriptLexer(new ANTLRInputStream(input));
|
||||
lexer.setUseStrictDefault(true);
|
||||
@ -179,37 +204,47 @@ public class YJSCompiler {
|
||||
ProgramContext tree = parser.program();
|
||||
// 应该是antlr4访问器进行遍历语法树
|
||||
ContractReader reader = new ContractReader(fileName);
|
||||
System.out.println("遍历语法树");
|
||||
contract = reader.visitProgram(tree);
|
||||
// 遍历完 获取 contract 里的 yjs type
|
||||
System.out.println(contract.getYjsType());
|
||||
contract.initPlainText(cts);
|
||||
handleAnnotation(contract);//处理注解
|
||||
handleModuleAnnotation(contract);// 处理注解
|
||||
handleFunctionAnnotation(contract);
|
||||
return contract;
|
||||
}
|
||||
|
||||
private void handleAnnotation(ContractNode contractNode) {
|
||||
System.out.println("handleAnnotation");
|
||||
private void handleModuleAnnotation(ContractNode contractNode) throws Exception {
|
||||
for (AnnotationNode node : contract.annotations) {
|
||||
AnnotationProcessor processor = findProcessor(node);
|
||||
if (processor != null) {
|
||||
processor.processContract(node, contractNode);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void handleFunctionAnnotation(ContractNode contractNode) throws Exception {
|
||||
for (FunctionNode functionNode : contractNode.getFunctions()) {
|
||||
List<AnnotationNode> annos = functionNode.annotations;// 函数里的annotation
|
||||
if (annos != null)
|
||||
for (AnnotationNode anno : annos) {
|
||||
System.out.println(anno.getType());//打印类型和参数
|
||||
System.out.println(anno.getArgs());
|
||||
AnnotationProcessor processor = findProcessor(anno);
|
||||
if (processor != null)
|
||||
processor.processFunction(anno, contractNode, functionNode);
|
||||
}
|
||||
}
|
||||
|
||||
for (InterfaceNode interfaceNode : contractNode.getInterfaces()) {
|
||||
List<AnnotationNode> annos = interfaceNode.annotations;// 函数里的annotation
|
||||
if (annos != null)
|
||||
for (AnnotationNode anno : annos) {
|
||||
AnnotationProcessor processor = findProcessor(anno);
|
||||
if (processor != null)
|
||||
processor.processInterface(anno, contractNode, interfaceNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private AnnotationProcessor findProcessor(AnnotationNode node) {
|
||||
public static AnnotationProcessor findProcessor(AnnotationNode node) {
|
||||
try {
|
||||
String clzName = YJSCompiler.class.getPackage().getName();
|
||||
clzName += ".ap." + node.getType();
|
||||
|
@ -3,13 +3,14 @@ package org.bdware.sc.compiler.ap;
|
||||
import org.bdware.sc.compiler.AnnotationProcessor;
|
||||
import org.bdware.sc.node.AnnotationNode;
|
||||
import org.bdware.sc.node.ContractNode;
|
||||
import org.bdware.sc.node.FunctionNode;
|
||||
|
||||
public class Access extends AnnotationProcessor {
|
||||
@Override
|
||||
public void processContract(AnnotationNode anno, ContractNode contractNode) {
|
||||
contractNode.sigRequired = false;
|
||||
if (anno != null) {
|
||||
contractNode.sigRequired = "\"verified\"".equals(anno.getArgs().get(0));
|
||||
for (FunctionNode fn : contractNode.getFunctions()) {
|
||||
fn.addAnnotation(anno);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ import org.bdware.sc.node.FunctionNode;
|
||||
|
||||
public class Confidential extends AnnotationProcessor {
|
||||
@Override
|
||||
public void processFunction(AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) {
|
||||
functionNode.setConfidential(true); }
|
||||
public void processFunction(AnnotationNode anno, ContractNode contractNode,
|
||||
FunctionNode functionNode) {
|
||||
functionNode.setConfidential(true);
|
||||
}
|
||||
}
|
||||
|
@ -9,8 +9,8 @@ import org.bdware.sc.util.JsonUtil;
|
||||
|
||||
public class Cost extends AnnotationProcessor {
|
||||
@Override
|
||||
public void processFunction(
|
||||
AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) {
|
||||
public void processFunction(AnnotationNode anno, ContractNode contractNode,
|
||||
FunctionNode functionNode) {
|
||||
CostDetail detail = JsonUtil.fromJson(anno.getArgs().get(0), CostDetail.class);
|
||||
functionNode.setCost(detail);
|
||||
if (detail.isCountGas())
|
||||
|
28
src/main/java/org/bdware/sc/compiler/ap/DOOP.java
Normal file
28
src/main/java/org/bdware/sc/compiler/ap/DOOP.java
Normal file
@ -0,0 +1,28 @@
|
||||
package org.bdware.sc.compiler.ap;
|
||||
|
||||
import org.bdware.sc.bean.DoipOperationInfo;
|
||||
import org.bdware.sc.compiler.AnnotationProcessor;
|
||||
import org.bdware.sc.node.AnnotationNode;
|
||||
import org.bdware.sc.node.ContractNode;
|
||||
import org.bdware.sc.node.FunctionNode;
|
||||
import org.bdware.sc.node.InterfaceNode;
|
||||
|
||||
|
||||
// DOOP is designed for DoipModule which contains specific functions for RepositoryHandler
|
||||
public class DOOP extends AnnotationProcessor {
|
||||
@Override
|
||||
public void processFunction(AnnotationNode anno, ContractNode contractNode,
|
||||
FunctionNode functionNode) throws Exception {
|
||||
// 通过DOOP注解,解析对应的值,并放进对应的FunctionNode中
|
||||
functionNode.setIsExport(true);
|
||||
functionNode.setIsDoipOperation(true);
|
||||
functionNode.setDoipOperationInfo(DoipOperationInfo.create(anno, contractNode));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processInterface(AnnotationNode anno, ContractNode contractNode,
|
||||
InterfaceNode interfaceNode) throws Exception {
|
||||
interfaceNode.setIsDoipOperation(true);
|
||||
interfaceNode.setDoipOperationInfo(DoipOperationInfo.create(anno, contractNode));
|
||||
}
|
||||
}
|
22
src/main/java/org/bdware/sc/compiler/ap/Fork.java
Normal file
22
src/main/java/org/bdware/sc/compiler/ap/Fork.java
Normal file
@ -0,0 +1,22 @@
|
||||
package org.bdware.sc.compiler.ap;
|
||||
|
||||
import org.bdware.sc.bean.ForkInfo;
|
||||
import org.bdware.sc.compiler.AnnotationProcessor;
|
||||
import org.bdware.sc.node.AnnotationNode;
|
||||
import org.bdware.sc.node.ContractNode;
|
||||
import org.bdware.sc.node.FunctionNode;
|
||||
import org.bdware.sc.node.InterfaceNode;
|
||||
|
||||
public class Fork extends AnnotationProcessor {
|
||||
@Override
|
||||
public void processFunction(AnnotationNode anno, ContractNode contractNode,
|
||||
FunctionNode functionNode) {
|
||||
functionNode.setForkInfo(ForkInfo.create(anno, contractNode));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processInterface(AnnotationNode anno, ContractNode contractNode,
|
||||
InterfaceNode interfaceNode) {
|
||||
interfaceNode.setForkInfo(ForkInfo.create(anno, contractNode));
|
||||
}
|
||||
}
|
@ -8,7 +8,8 @@ import org.bdware.sc.node.FunctionNode;
|
||||
|
||||
public class HomomorphicDecrypt extends AnnotationProcessor {
|
||||
@Override
|
||||
public void processFunction(AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) {
|
||||
public void processFunction(AnnotationNode anno, ContractNode contractNode,
|
||||
FunctionNode functionNode) {
|
||||
functionNode.setHomomorphicDecrypt(true);
|
||||
functionNode.setKeyManagerID(anno.getArgs().get(0));
|
||||
functionNode.setSecretID(anno.getArgs().get(1));
|
||||
|
@ -8,7 +8,8 @@ import org.bdware.sc.node.FunctionNode;
|
||||
|
||||
public class HomomorphicEncrypt extends AnnotationProcessor {
|
||||
@Override
|
||||
public void processFunction(AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) {
|
||||
public void processFunction(AnnotationNode anno, ContractNode contractNode,
|
||||
FunctionNode functionNode) {
|
||||
functionNode.setHomomorphicEncrypt(true);
|
||||
functionNode.setKeyManagerID(anno.getArgs().get(0));
|
||||
functionNode.setSecretID(anno.getArgs().get(1));
|
||||
|
@ -5,13 +5,20 @@ import org.bdware.sc.compiler.AnnotationProcessor;
|
||||
import org.bdware.sc.node.AnnotationNode;
|
||||
import org.bdware.sc.node.ContractNode;
|
||||
import org.bdware.sc.node.FunctionNode;
|
||||
import org.bdware.sc.node.InterfaceNode;
|
||||
|
||||
public class Join extends AnnotationProcessor {
|
||||
@Override
|
||||
public void processFunction(
|
||||
AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) {
|
||||
public void processFunction(AnnotationNode anno, ContractNode contractNode,
|
||||
FunctionNode functionNode) {
|
||||
// functionNode.setRouteInfo(RouteInfo.create(anno,contractNode));
|
||||
// 增加标记,在ContractNode中记录Join相关的函数和Join规则
|
||||
functionNode.setJoinInfo(JoinInfo.create(anno, contractNode));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processInterface(AnnotationNode anno, ContractNode contractNode,
|
||||
InterfaceNode interfaceNode) {
|
||||
interfaceNode.setJoinInfo(JoinInfo.create(anno, contractNode));
|
||||
}
|
||||
}
|
||||
|
@ -14,12 +14,11 @@ public class LogLocation extends AnnotationProcessor {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processFunction(
|
||||
AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) {
|
||||
public void processFunction(AnnotationNode anno, ContractNode contractNode,
|
||||
FunctionNode functionNode) {
|
||||
if (anno != null && anno.getArgs() != null)
|
||||
for (String s : anno.getArgs()) {
|
||||
if (s.equals("\"dataware\"")
|
||||
|| s.equals("\"bdledger\"")
|
||||
if (s.equals("\"dataware\"") || s.equals("\"bdledger\"")
|
||||
|| s.equals("\"bdledger:\"")) {
|
||||
functionNode.setLogToBDContract(true);
|
||||
} else if (s.startsWith("\"bdledger:") && s.length() > 11) {
|
||||
|
@ -15,12 +15,13 @@ public class LogType extends AnnotationProcessor {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processFunction(
|
||||
AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) {
|
||||
public void processFunction(AnnotationNode anno, ContractNode contractNode,
|
||||
FunctionNode functionNode) {
|
||||
for (String str : anno.getArgs()) {
|
||||
org.bdware.sc.node.LogType type = org.bdware.sc.node.LogType.parse(str);
|
||||
functionNode.addLogType(type);
|
||||
if (type == org.bdware.sc.node.LogType.Branch) contractNode.setInstrumentBranch(true);
|
||||
if (type == org.bdware.sc.node.LogType.Branch)
|
||||
contractNode.setInstrumentBranch(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,22 @@
|
||||
package org.bdware.sc.compiler.ap;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.bdware.sc.bean.RouteInfo;
|
||||
import org.bdware.sc.compiler.AnnotationProcessor;
|
||||
import org.bdware.sc.node.AnnotationNode;
|
||||
import org.bdware.sc.node.ContractNode;
|
||||
import org.bdware.sc.node.CostDetail;
|
||||
import org.bdware.sc.node.FunctionNode;
|
||||
import org.bdware.sc.node.InterfaceNode;
|
||||
|
||||
public class Route extends AnnotationProcessor {
|
||||
@Override
|
||||
public void processFunction(
|
||||
AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) {
|
||||
public void processFunction(AnnotationNode anno, ContractNode contractNode,
|
||||
FunctionNode functionNode) {
|
||||
functionNode.setRouteInfo(RouteInfo.create(anno, contractNode));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processInterface(AnnotationNode anno, ContractNode contractNode,
|
||||
InterfaceNode interfaceNode) {
|
||||
interfaceNode.setRouteInfo(RouteInfo.create(anno, contractNode));
|
||||
}
|
||||
}
|
||||
|
@ -9,8 +9,8 @@ import org.bdware.sc.node.FunctionNode;
|
||||
public class Split extends AnnotationProcessor {
|
||||
|
||||
@Override
|
||||
public void processFunction(
|
||||
AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) {
|
||||
public void processFunction(AnnotationNode anno, ContractNode contractNode,
|
||||
FunctionNode functionNode) {
|
||||
functionNode.setRouteInfo(RouteInfo.create(anno, contractNode));
|
||||
}
|
||||
}
|
||||
|
171
src/main/java/org/bdware/sc/crdt/SharableVarManager.java
Normal file
171
src/main/java/org/bdware/sc/crdt/SharableVarManager.java
Normal file
@ -0,0 +1,171 @@
|
||||
package org.bdware.sc.crdt;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.crdt.basic.Constants;
|
||||
import org.bdware.crdt.basic.JoinableCRDT;
|
||||
import org.bdware.doip.audit.AuditDoaClient;
|
||||
import org.bdware.doip.audit.EndpointConfig;
|
||||
import org.bdware.doip.audit.client.AuditDoipClient;
|
||||
import org.bdware.doip.audit.client.AuditIrpClient;
|
||||
import org.bdware.doip.codec.JsonDoipMessage;
|
||||
import org.bdware.doip.codec.doipMessage.DoipMessage;
|
||||
import org.bdware.doip.codec.doipMessage.DoipMessageFactory;
|
||||
import org.bdware.doip.codec.doipMessage.DoipResponseCode;
|
||||
import org.bdware.doip.endpoint.client.DoipMessageCallback;
|
||||
import org.bdware.irp.client.IrpClient;
|
||||
import org.bdware.irp.stateinfo.StateInfoBase;
|
||||
import org.bdware.sc.crdt.proxy.*;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class SharableVarManager {
|
||||
static Logger LOGGER = LogManager.getLogger(SharableVarManager.class);
|
||||
public static SharableVarManager instance;
|
||||
public final String cpId;
|
||||
Map<String, SharableVar> allVars;
|
||||
IrpClient client;
|
||||
AuditDoaClient doaClient;
|
||||
|
||||
public static final String SHARABLEOP = "86.100871/SyncVar";
|
||||
|
||||
public SharableVarManager(String cpId, EndpointConfig config) {
|
||||
this.allVars = new ConcurrentHashMap<>();
|
||||
this.client = new AuditIrpClient(config);
|
||||
this.doaClient = new AuditDoaClient("", config, null);
|
||||
this.cpId = cpId;
|
||||
}
|
||||
|
||||
public static void initSharableVarManager(String id, EndpointConfig config) {
|
||||
if (instance == null) {
|
||||
instance = new SharableVarManager(id, config);
|
||||
}
|
||||
}
|
||||
|
||||
public DoipMessage handleSyncMessage(DoipMessage message) {
|
||||
try {
|
||||
String varId = message.header.parameters.attributes.get("varId").getAsString();
|
||||
String content = message.header.parameters.attributes.get("content").getAsString();
|
||||
String type = message.header.parameters.attributes.get("type").getAsString();
|
||||
SharableVar var = allVars.get(varId);
|
||||
if (var != null) {
|
||||
if (Objects.equals(type, "r2r") || Objects.equals(type, "w2r")) {
|
||||
JoinableCRDT delta = JsonUtil.fromJson(content, var.readerVar.getClass());
|
||||
var.readerVar.join(delta);
|
||||
var.readerVarDeltaQueue.add(delta);
|
||||
} else if (Objects.equals(type, "w2w")) {
|
||||
JoinableCRDT delta = JsonUtil.fromJson(content, var.writerVar.getClass());
|
||||
var.writerVar.join(delta);
|
||||
var.writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
}
|
||||
DoipMessageFactory.DoipMessageBuilder builder =
|
||||
new DoipMessageFactory.DoipMessageBuilder();
|
||||
builder.createResponse(DoipResponseCode.Success, message);
|
||||
builder.addAttributes("msg", "success");
|
||||
return builder.create();
|
||||
} catch (Exception e) {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
e.printStackTrace();
|
||||
e.printStackTrace(new PrintStream(bo));
|
||||
DoipMessageFactory.DoipMessageBuilder builder =
|
||||
new DoipMessageFactory.DoipMessageBuilder();
|
||||
builder.createResponse(DoipResponseCode.UnKnownError, message);
|
||||
builder.addAttributes("exception", bo.toString());
|
||||
return builder.create();
|
||||
}
|
||||
}
|
||||
|
||||
private DoipMessage createSyncMessage(String target, String varId, String content,
|
||||
String type) {
|
||||
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
|
||||
builder.createRequest(target, SHARABLEOP);
|
||||
builder.addAttributes("varId", varId);
|
||||
builder.addAttributes("content", content);
|
||||
builder.addAttributes("type", type);
|
||||
return builder.create();
|
||||
}
|
||||
|
||||
public void broadcastSyncMessage(String varId, List<String> sendTo, String content,
|
||||
String type) {
|
||||
for (String target : sendTo) {
|
||||
DoipMessage doipMessage = createSyncMessage(target, varId, content, type);
|
||||
if (target.equals(cpId)) {
|
||||
LOGGER.info("Handle Sync locally:"
|
||||
+ JsonUtil.toJson(JsonDoipMessage.fromDoipMessage(doipMessage)));
|
||||
handleSyncMessage(doipMessage);
|
||||
} else {
|
||||
AuditDoipClient client = getClient(target);
|
||||
client.sendMessage(doipMessage, new DoipMessageCallback() {
|
||||
@Override
|
||||
public void onResult(DoipMessage doipMessage) {
|
||||
LOGGER.info("RECV Sync:"
|
||||
+ JsonUtil.toJson(JsonDoipMessage.fromDoipMessage(doipMessage)));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private AuditDoipClient getClient(String id) {
|
||||
return doaClient.convertDoidToRepo(id);
|
||||
}
|
||||
|
||||
public synchronized SharableVar createVar(String identifier, String type) {
|
||||
try {
|
||||
if (allVars.containsKey(identifier)) {
|
||||
return allVars.get(identifier);
|
||||
}
|
||||
StateInfoBase stateInfoBase = client.resolve(identifier);
|
||||
if (stateInfoBase.handleValues.has("bdwType") && stateInfoBase.handleValues
|
||||
.get("bdwType").getAsString().equals("SharableVar")) {
|
||||
SharableVarState.SharableVarConfiguration sharableVarConf =
|
||||
JsonUtil.fromJson(stateInfoBase.handleValues,
|
||||
SharableVarState.SharableVarConfiguration.class);
|
||||
SharableVar sharableVar = createSharableVar(sharableVarConf, identifier, type);
|
||||
if (sharableVar != null) {
|
||||
allVars.put(identifier, sharableVar);
|
||||
}
|
||||
return sharableVar;
|
||||
} else
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private SharableVar createSharableVar(SharableVarState.SharableVarConfiguration conf,
|
||||
String identifier, String type) {
|
||||
switch (type) {
|
||||
case Constants.TypeName.G_COUNTER:
|
||||
return new GCounterProxy(identifier, cpId, conf);
|
||||
case Constants.TypeName.PN_COUNTER:
|
||||
return new PNCounterProxy(identifier, cpId, conf);
|
||||
case Constants.TypeName.DW_FLAG:
|
||||
return new DWFlagProxy(identifier, cpId, conf);
|
||||
case Constants.TypeName.LWW_REGISTER:
|
||||
return new LWWRegisterProxy(identifier, cpId, conf);
|
||||
case Constants.TypeName.MV_REGISTER:
|
||||
return new MVRegisterProxy(identifier, cpId, conf);
|
||||
case Constants.TypeName.G_SET:
|
||||
return new GSetProxy(identifier, cpId, conf);
|
||||
case Constants.TypeName.TP_SET:
|
||||
return new TPSetProxy(identifier, cpId, conf);
|
||||
case Constants.TypeName.AW_OR_SET:
|
||||
return new AWORSetProxy(identifier, cpId, conf);
|
||||
case Constants.TypeName.RW_OR_SET:
|
||||
return new RWORSetProxy(identifier, cpId, conf);
|
||||
case Constants.TypeName.RW_LWW_SET:
|
||||
return new RWLWWSetProxy(identifier, cpId, conf);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
304
src/main/java/org/bdware/sc/crdt/SharableVarState.java
Normal file
304
src/main/java/org/bdware/sc/crdt/SharableVarState.java
Normal file
@ -0,0 +1,304 @@
|
||||
package org.bdware.sc.crdt;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class SharableVarState {
|
||||
private String myId;
|
||||
|
||||
private SharableVarConfiguration sharableVarConfiguration;
|
||||
|
||||
private Integer myIndex;
|
||||
|
||||
// 是reader
|
||||
private boolean readerFlag;
|
||||
|
||||
// 是writer
|
||||
private boolean writerFlag;
|
||||
|
||||
// 是reader的根结点
|
||||
private boolean readerRootFlag;
|
||||
|
||||
// 是writer的根结点
|
||||
private boolean writerRootFlag;
|
||||
|
||||
// 作为writer的话,非根节点具有parent。根结点为null
|
||||
private String writerParent;
|
||||
|
||||
// 作为reader的话,非叶子节点具有children,叶子节点为null
|
||||
private List<String> readerChildren;
|
||||
|
||||
// reader森林的根结点们,用来给writer根结点同步数据用
|
||||
private List<String> readerRoots;
|
||||
|
||||
private Long writeInterval;
|
||||
|
||||
private Long readerInterval;
|
||||
|
||||
public SharableVarState(String myId, SharableVarConfiguration sharableVarConfiguration) {
|
||||
this.myId = myId;
|
||||
this.sharableVarConfiguration = sharableVarConfiguration;
|
||||
parseProperties();
|
||||
}
|
||||
|
||||
private void parseProperties() {
|
||||
for (int i = 0; i < sharableVarConfiguration.getNodeIds().length; i++) {
|
||||
if (Objects.equals(sharableVarConfiguration.getNodeIds()[i], myId)) {
|
||||
this.myIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (this.myIndex == null) {
|
||||
return;
|
||||
}
|
||||
int writerParentIdx = sharableVarConfiguration.writerParents[this.myIndex];
|
||||
if (writerParentIdx == -1) {
|
||||
writerFlag = true;
|
||||
writerRootFlag = true;
|
||||
} else if (writerParentIdx >= 0) {
|
||||
writerFlag = true;
|
||||
writerParent = sharableVarConfiguration.nodeIds[writerParentIdx];
|
||||
}
|
||||
|
||||
int readerParentIdx = sharableVarConfiguration.readerParents[this.myIndex];
|
||||
if (readerParentIdx == -1) {
|
||||
readerFlag = true;
|
||||
readerRootFlag = true;
|
||||
} else if (readerParentIdx >= 0) {
|
||||
readerFlag = true;
|
||||
}
|
||||
if (readerFlag) {
|
||||
readerChildren = new ArrayList<>();
|
||||
for (int i = 0; i < sharableVarConfiguration.readerParents.length; i++) {
|
||||
if (sharableVarConfiguration.readerParents[i] == myIndex) {
|
||||
readerChildren.add(sharableVarConfiguration.nodeIds[i]);
|
||||
}
|
||||
}
|
||||
readerInterval = sharableVarConfiguration.readerIntervals[myIndex];
|
||||
}
|
||||
if (writerFlag) {
|
||||
writeInterval = sharableVarConfiguration.writerIntervals[myIndex];
|
||||
}
|
||||
if (writerRootFlag) {
|
||||
readerRoots = new ArrayList<>();
|
||||
for (int i = 0; i < sharableVarConfiguration.readerParents.length; i++) {
|
||||
if (sharableVarConfiguration.readerParents[i] == -1) {
|
||||
readerRoots.add(sharableVarConfiguration.nodeIds[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String getMyId() {
|
||||
return myId;
|
||||
}
|
||||
|
||||
public void setMyId(String myId) {
|
||||
this.myId = myId;
|
||||
}
|
||||
|
||||
public SharableVarConfiguration getSharableVarConfiguration() {
|
||||
return sharableVarConfiguration;
|
||||
}
|
||||
|
||||
public void setSharableVarConfiguration(SharableVarConfiguration sharableVarConfiguration) {
|
||||
this.sharableVarConfiguration = sharableVarConfiguration;
|
||||
}
|
||||
|
||||
public Integer getMyIndex() {
|
||||
return myIndex;
|
||||
}
|
||||
|
||||
public void setMyIndex(Integer myIndex) {
|
||||
this.myIndex = myIndex;
|
||||
}
|
||||
|
||||
public boolean isReaderFlag() {
|
||||
return readerFlag;
|
||||
}
|
||||
|
||||
public void setReaderFlag(boolean readerFlag) {
|
||||
this.readerFlag = readerFlag;
|
||||
}
|
||||
|
||||
public boolean isWriterFlag() {
|
||||
return writerFlag;
|
||||
}
|
||||
|
||||
public void setWriterFlag(boolean writerFlag) {
|
||||
this.writerFlag = writerFlag;
|
||||
}
|
||||
|
||||
public boolean isReaderRootFlag() {
|
||||
return readerRootFlag;
|
||||
}
|
||||
|
||||
public void setReaderRootFlag(boolean readerRootFlag) {
|
||||
this.readerRootFlag = readerRootFlag;
|
||||
}
|
||||
|
||||
public boolean isWriterRootFlag() {
|
||||
return writerRootFlag;
|
||||
}
|
||||
|
||||
public void setWriterRootFlag(boolean writerRootFlag) {
|
||||
this.writerRootFlag = writerRootFlag;
|
||||
}
|
||||
|
||||
public String getWriterParent() {
|
||||
return writerParent;
|
||||
}
|
||||
|
||||
public void setWriterParent(String writerParent) {
|
||||
this.writerParent = writerParent;
|
||||
}
|
||||
|
||||
public List<String> getReaderChildren() {
|
||||
return readerChildren;
|
||||
}
|
||||
|
||||
public void setReaderChildren(List<String> readerChildren) {
|
||||
this.readerChildren = readerChildren;
|
||||
}
|
||||
|
||||
public List<String> getReaderRoots() {
|
||||
return readerRoots;
|
||||
}
|
||||
|
||||
public void setReaderRoots(List<String> readerRoots) {
|
||||
this.readerRoots = readerRoots;
|
||||
}
|
||||
|
||||
public Long getWriteInterval() {
|
||||
return writeInterval;
|
||||
}
|
||||
|
||||
public void setWriteInterval(Long writeInterval) {
|
||||
this.writeInterval = writeInterval;
|
||||
}
|
||||
|
||||
public Long getReaderInterval() {
|
||||
return readerInterval;
|
||||
}
|
||||
|
||||
public void setReaderInterval(Long readerInterval) {
|
||||
this.readerInterval = readerInterval;
|
||||
}
|
||||
|
||||
public static class SharableVarConfiguration {
|
||||
// ["/bdrepo1/var/0", "/bdrepo1/var/1", "/bdrepo1/var/2", "/bdrepo1/var/3"]
|
||||
// 0(w) -> 2(w) -> 1(r) -> 3(r)
|
||||
// |
|
||||
// 1(w) -
|
||||
|
||||
// ["/bdrepo1/var/0", "/bdrepo1/var/1", "/bdrepo1/var/2", "/bdrepo1/var/3"]
|
||||
String[] nodeIds;
|
||||
long maxDelay;
|
||||
int[] writerIndexes;
|
||||
int[] readerIndexes;
|
||||
|
||||
// [2, 2, -1, -2] -2表示该节点为非writer节点,-1表示该结点为writer根结点
|
||||
int[] writerParents;
|
||||
// [-2, -1, -2, 1] -2表示该节点为非reader节点,-1表示该结点为reader根结点
|
||||
int[] readerParents;
|
||||
// [5, 5, 6, -1] -1为无需同步
|
||||
long[] writerIntervals;
|
||||
// [-1, 4, -1, -1]
|
||||
long[] readerIntervals;
|
||||
|
||||
public String[] getNodeIds() {
|
||||
return nodeIds;
|
||||
}
|
||||
|
||||
public void setNodeIds(String[] nodeIds) {
|
||||
this.nodeIds = nodeIds;
|
||||
}
|
||||
|
||||
public long getMaxDelay() {
|
||||
return maxDelay;
|
||||
}
|
||||
|
||||
public void setMaxDelay(long maxDelay) {
|
||||
this.maxDelay = maxDelay;
|
||||
}
|
||||
|
||||
public int[] getWriterIndexes() {
|
||||
return writerIndexes;
|
||||
}
|
||||
|
||||
public void setWriterIndexes(int[] writerIndexes) {
|
||||
this.writerIndexes = writerIndexes;
|
||||
}
|
||||
|
||||
public int[] getReaderIndexes() {
|
||||
return readerIndexes;
|
||||
}
|
||||
|
||||
public void setReaderIndexes(int[] readerIndexes) {
|
||||
this.readerIndexes = readerIndexes;
|
||||
}
|
||||
|
||||
public int[] getWriterParents() {
|
||||
return writerParents;
|
||||
}
|
||||
|
||||
public void setWriterParents(int[] writerParents) {
|
||||
this.writerParents = writerParents;
|
||||
}
|
||||
|
||||
public int[] getReaderParents() {
|
||||
return readerParents;
|
||||
}
|
||||
|
||||
public void setReaderParents(int[] readerParents) {
|
||||
this.readerParents = readerParents;
|
||||
}
|
||||
|
||||
public long[] getWriterIntervals() {
|
||||
return writerIntervals;
|
||||
}
|
||||
|
||||
public void setWriterIntervals(long[] writerIntervals) {
|
||||
this.writerIntervals = writerIntervals;
|
||||
}
|
||||
|
||||
public long[] getReaderIntervals() {
|
||||
return readerIntervals;
|
||||
}
|
||||
|
||||
public void setReaderIntervals(long[] readerIntervals) {
|
||||
this.readerIntervals = readerIntervals;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// ["/bdrepo1/var/0", "/bdrepo1/var/1", "/bdrepo1/var/2", "/bdrepo1/var/3"]
|
||||
// 0(w) -> 2(w) -> 1(r) -> 3(r)
|
||||
// |
|
||||
// 1(w) -
|
||||
|
||||
|
||||
SharableVarConfiguration sharableVarConfiguration = new SharableVarConfiguration();
|
||||
sharableVarConfiguration.setNodeIds(new String[] {"/bdrepo1/var/0", "/bdrepo1/var/1",
|
||||
"/bdrepo1/var/2", "/bdrepo1/var/3"});
|
||||
sharableVarConfiguration.setWriterParents(new int[] {2, 2, -1, -2});
|
||||
sharableVarConfiguration.setReaderParents(new int[] {-2, -1, -2, 1});
|
||||
sharableVarConfiguration.setWriterIntervals(new long[] {5, 5, 6, -1});
|
||||
sharableVarConfiguration.setReaderIntervals(new long[] {-1, 4, -1, -1});
|
||||
|
||||
|
||||
SharableVarState sharableVarState0 =
|
||||
new SharableVarState("/bdrepo1/var/0", sharableVarConfiguration);
|
||||
SharableVarState sharableVarState1 =
|
||||
new SharableVarState("/bdrepo1/var/1", sharableVarConfiguration);
|
||||
SharableVarState sharableVarState2 =
|
||||
new SharableVarState("/bdrepo1/var/2", sharableVarConfiguration);
|
||||
SharableVarState sharableVarState3 =
|
||||
new SharableVarState("/bdrepo1/var/3", sharableVarConfiguration);
|
||||
System.out.println(sharableVarState0);
|
||||
System.out.println(sharableVarState1);
|
||||
System.out.println(sharableVarState2);
|
||||
System.out.println(sharableVarState3);
|
||||
}
|
||||
}
|
54
src/main/java/org/bdware/sc/crdt/planning/PlanningTest.java
Normal file
54
src/main/java/org/bdware/sc/crdt/planning/PlanningTest.java
Normal file
@ -0,0 +1,54 @@
|
||||
package org.bdware.sc.crdt.planning;
|
||||
|
||||
public class PlanningTest {
|
||||
public static void main(String[] args) {
|
||||
|
||||
final int nodeIdsCount = 1000;
|
||||
final int writerCount = 500;
|
||||
final int readerCount = 500;
|
||||
String[] nodeIds = new String[nodeIdsCount];
|
||||
int[] writers = new int[writerCount];
|
||||
int[] readers = new int[readerCount];
|
||||
for (int i = 0; i < nodeIdsCount; ++i) {
|
||||
nodeIds[i] = "node_" + i;
|
||||
if (i < writerCount) {
|
||||
writers[i] = i;
|
||||
}
|
||||
if (i >= nodeIdsCount - readerCount) {
|
||||
readers[i - nodeIdsCount + readerCount] = i;
|
||||
}
|
||||
}
|
||||
long maxDelay = 300;
|
||||
int bandwidthUpload = 30;
|
||||
int bandwidthDownload = 30;
|
||||
int datasize = 10;
|
||||
double domainSize = 100 * datasize;
|
||||
|
||||
PlanningWith0Expansivity planning0 = new PlanningWith0Expansivity(nodeIds, writers, readers,
|
||||
maxDelay, bandwidthDownload, bandwidthUpload, datasize);
|
||||
PlanningWithkExpansivity planningK = new PlanningWithkExpansivity(nodeIds, writers, readers,
|
||||
maxDelay, bandwidthDownload, bandwidthUpload, datasize, domainSize);
|
||||
PlanningWith1Expansivity planning1 = new PlanningWith1Expansivity(nodeIds, writers, readers,
|
||||
maxDelay, bandwidthDownload, bandwidthUpload, datasize);
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
planning0.adjustAndCalc();
|
||||
planning0.allocate();
|
||||
long end = System.currentTimeMillis();
|
||||
System.out.println("took " + (end - start));
|
||||
System.out.println();
|
||||
|
||||
start = System.currentTimeMillis();
|
||||
planningK.adjustAndCalc();
|
||||
planningK.allocate();
|
||||
end = System.currentTimeMillis();
|
||||
System.out.println("took " + (end - start));
|
||||
System.out.println();
|
||||
|
||||
start = System.currentTimeMillis();
|
||||
planning1.adjustAndCalc();
|
||||
planning1.allocate();
|
||||
end = System.currentTimeMillis();
|
||||
System.out.println("took " + (end - start));
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
package org.bdware.sc.crdt.planning;
|
||||
|
||||
public class PlanningWith0Expansivity extends SharableNetworkPlanning {
|
||||
public PlanningWith0Expansivity(String[] nodeIds, int[] writers, int[] readers, long maxDelay,
|
||||
int bandwidthDownload, int bandwidthUpload, int dataSize) {
|
||||
this.nodeIds = nodeIds;
|
||||
this.writers = writers;
|
||||
this.readers = readers;
|
||||
this.maxDelay = maxDelay;
|
||||
this.bandwidthDownload = bandwidthDownload;
|
||||
this.bandwidthUpload = bandwidthUpload;
|
||||
this.dataSize = dataSize;
|
||||
this.totalCountW = writers.length;
|
||||
this.totalCountR = readers.length;
|
||||
}
|
||||
|
||||
public boolean writerTreeConstraint() {
|
||||
if (frequencySyncW > 0) {
|
||||
double common = frequencySyncW * dataSize;
|
||||
// 非叶子节点下载带宽
|
||||
boolean result1 = bandwidthDownload >= common * treeDegreeW;
|
||||
// 非根节点上行带宽
|
||||
boolean result2 = bandwidthUpload >= common;
|
||||
return result1 && result2;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean writer2ReaderConstraint() {
|
||||
double common = frequencySyncWR * dataSize;
|
||||
// Writer根节点上行带宽
|
||||
boolean result1 = bandwidthUpload >= common * rootCountR;
|
||||
// Reader根节点下载带宽
|
||||
boolean result2 = bandwidthDownload >= common * rootCountW;
|
||||
return result1 && result2;
|
||||
}
|
||||
|
||||
public boolean readerTreeConstraint() {
|
||||
if (frequencySyncR > 0) {
|
||||
double common = frequencySyncR * dataSize;
|
||||
// Reader非叶子节点上行带宽
|
||||
boolean result1 = bandwidthUpload >= common * treeDegreeR;
|
||||
// Reader非根节点下载带宽
|
||||
boolean result2 = bandwidthDownload >= common;
|
||||
return result1 && result2;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void calcOptimizedResult() {
|
||||
double a = (treeHeightW - 1) * (totalCountW - rootCountW);
|
||||
double b = rootCountR * rootCountW;
|
||||
double c = (treeHeightR - 1) * (totalCountR - rootCountR);
|
||||
|
||||
double A = Math.sqrt(a);
|
||||
double B = Math.sqrt(b);
|
||||
double C = Math.sqrt(c);
|
||||
|
||||
wDelay = (long) (maxDelay * (A / (A + B + C)));
|
||||
w2rDelay = (long) (maxDelay * (B / (A + B + C)));
|
||||
rDelay = (long) (maxDelay * (C / (A + B + C)));
|
||||
|
||||
frequencySyncW = wDelay > 0 ? (treeHeightW - 1) / wDelay : 0;
|
||||
frequencySyncR = rDelay > 0 ? (treeHeightR - 1) / rDelay : 0;
|
||||
frequencySyncWR = w2rDelay > 0 ? (1.0 / w2rDelay) : 0;
|
||||
|
||||
totalData =
|
||||
(long) (dataSize * (frequencySyncW * a + frequencySyncWR * b + frequencySyncR * c));
|
||||
}
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
package org.bdware.sc.crdt.planning;
|
||||
|
||||
public class PlanningWith1Expansivity extends SharableNetworkPlanning {
|
||||
public PlanningWith1Expansivity(String[] nodeIds, int[] writers, int[] readers, long maxDelay,
|
||||
int bandwidthDownload, int bandwidthUpload, int dataSize) {
|
||||
this.nodeIds = nodeIds;
|
||||
this.writers = writers;
|
||||
this.readers = readers;
|
||||
this.maxDelay = maxDelay;
|
||||
this.bandwidthDownload = bandwidthDownload;
|
||||
this.bandwidthUpload = bandwidthUpload;
|
||||
this.dataSize = dataSize;
|
||||
this.totalCountW = writers.length;
|
||||
this.totalCountR = readers.length;
|
||||
}
|
||||
|
||||
public boolean writerTreeConstraint() {
|
||||
if (frequencySyncW > 0) {
|
||||
double common = frequencySyncW * (treeNodeCountW - 1) * dataSize;
|
||||
// 非叶子节点下载带宽
|
||||
boolean result1 = bandwidthDownload >= common;
|
||||
// 非根节点上行带宽
|
||||
boolean result2 = bandwidthUpload >= common / treeDegreeW;
|
||||
return result1 && result2;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean writer2ReaderConstraint() {
|
||||
double common = frequencySyncWR * totalCountW * dataSize;
|
||||
// Writer根节点上行带宽
|
||||
boolean result1 = bandwidthUpload >= common * rootCountR / rootCountW;
|
||||
// Reader根节点下载带宽
|
||||
boolean result2 = bandwidthDownload >= common;
|
||||
return result1 && result2;
|
||||
}
|
||||
|
||||
public boolean readerTreeConstraint() {
|
||||
if (frequencySyncR > 0) {
|
||||
double common = frequencySyncR * totalCountW * dataSize;
|
||||
// Reader非叶子节点上行带宽
|
||||
boolean result1 = bandwidthUpload >= common * treeDegreeR;
|
||||
// Reader非根节点下载带宽
|
||||
boolean result2 = bandwidthDownload >= common;
|
||||
return result1 && result2;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void calcOptimizedResult() {
|
||||
double a = 0;
|
||||
if (treeHeightW > 1) {
|
||||
if (treeDegreeW > 1) {
|
||||
a = (treeHeightW - 1)
|
||||
* (treeHeightW * Math.pow(treeDegreeW, treeHeightW) / (treeDegreeW - 1)
|
||||
+ (treeDegreeW - Math.pow(treeDegreeW, treeHeightW + 1))
|
||||
/ (treeDegreeW - 1) / (treeDegreeW - 1))
|
||||
* rootCountW;
|
||||
} else {
|
||||
// treeDegreeW = 1
|
||||
a = (treeHeightW - 1) * (treeHeightW - 1) * treeHeightW / 2;
|
||||
}
|
||||
}
|
||||
double b = rootCountR * totalCountW;
|
||||
double c = (totalCountR - rootCountR) * (treeHeightR - 1) * totalCountW;
|
||||
|
||||
double A = Math.sqrt(a);
|
||||
double B = Math.sqrt(b);
|
||||
double C = Math.sqrt(c);
|
||||
|
||||
wDelay = (long) Math.ceil(maxDelay * (A / (A + B + C)));
|
||||
w2rDelay = (long) Math.ceil(maxDelay * (B / (A + B + C)));
|
||||
rDelay = (long) Math.ceil(maxDelay * (C / (A + B + C)));
|
||||
|
||||
|
||||
frequencySyncW = wDelay > 0 ? (treeHeightW - 1) / wDelay : 0;
|
||||
frequencySyncR = rDelay > 0 ? (treeHeightR - 1) / rDelay : 0;
|
||||
frequencySyncWR = w2rDelay > 0 ? (1.0 / w2rDelay) : 0;
|
||||
|
||||
totalData =
|
||||
(long) (dataSize * (frequencySyncW * a + frequencySyncWR * b + frequencySyncR * c));
|
||||
}
|
||||
}
|
@ -0,0 +1,122 @@
|
||||
package org.bdware.sc.crdt.planning;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class PlanningWithkExpansivity extends SharableNetworkPlanning {
|
||||
Map<Integer, Map<Integer, Double>> accumulationsCache;
|
||||
private final double k;
|
||||
private final double domainSize;
|
||||
|
||||
public PlanningWithkExpansivity(String[] nodeIds, int[] writers, int[] readers, long maxDelay,
|
||||
int bandwidthDownload, int bandwidthUpload, int dataSize, double domainSize) {
|
||||
this.nodeIds = nodeIds;
|
||||
this.writers = writers;
|
||||
this.readers = readers;
|
||||
this.maxDelay = maxDelay;
|
||||
this.bandwidthDownload = bandwidthDownload;
|
||||
this.bandwidthUpload = bandwidthUpload;
|
||||
this.dataSize = dataSize;
|
||||
this.totalCountW = writers.length;
|
||||
this.totalCountR = readers.length;
|
||||
this.domainSize = domainSize;
|
||||
this.k = (domainSize - dataSize) / domainSize;
|
||||
}
|
||||
|
||||
public boolean writerTreeConstraint() {
|
||||
if (frequencySyncW > 0) {
|
||||
if (treeDegreeW > 1) {
|
||||
double common = frequencySyncW * domainSize * (1 - Math.pow(k,
|
||||
(Math.pow(treeDegreeW, treeHeightW - 1) - 1) / (treeDegreeW - 1)));
|
||||
// 非叶子节点下载带宽
|
||||
boolean result1 = bandwidthDownload >= common * treeDegreeW;
|
||||
// 非根节点上行带宽
|
||||
boolean result2 = bandwidthUpload >= common;
|
||||
return result1 && result2;
|
||||
} else if (treeDegreeW == 1) {
|
||||
double common = frequencySyncW * domainSize * (1 - Math.pow(k, treeHeightW - 1));
|
||||
boolean result1 = bandwidthDownload >= common;
|
||||
// 非根节点上行带宽
|
||||
boolean result2 = bandwidthUpload >= common;
|
||||
return result1 && result2;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean writer2ReaderConstraint() {
|
||||
double common = frequencySyncWR * domainSize * (1 - Math.pow(k, treeNodeCountW));
|
||||
// Writer根节点上行带宽
|
||||
boolean result1 = bandwidthUpload >= common * rootCountR;
|
||||
// Reader根节点下载带宽
|
||||
boolean result2 = bandwidthDownload >= common * rootCountW;
|
||||
return result1 && result2;
|
||||
}
|
||||
|
||||
public boolean readerTreeConstraint() {
|
||||
if (frequencySyncR > 0) {
|
||||
double common = frequencySyncR * domainSize * (1 - Math.pow(k, totalCountW));
|
||||
// Reader非叶子节点上行带宽
|
||||
boolean result1 = bandwidthUpload >= common * treeDegreeR;
|
||||
// Reader非根节点下载带宽
|
||||
boolean result2 = bandwidthDownload >= common;
|
||||
return result1 && result2;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
double calcAccumulationWithK() {
|
||||
if (accumulationsCache == null) {
|
||||
accumulationsCache = new HashMap<>();
|
||||
}
|
||||
int H1 = (int) treeHeightW;
|
||||
int D1 = (int) treeDegreeW;
|
||||
if (accumulationsCache.get(H1) != null && accumulationsCache.get(H1).get(D1) != null) {
|
||||
return accumulationsCache.get(H1).get(D1);
|
||||
}
|
||||
double result = 0;
|
||||
for (int h = 1; h < H1; ++h) {
|
||||
result += (Math.pow(D1, h) * (1 - Math.pow(k, (Math.pow(D1, H1 - h) - 1) / (D1 - 1))));
|
||||
}
|
||||
accumulationsCache.computeIfAbsent(H1, k -> new HashMap<>()).put(D1, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
double calcAccumulation() {
|
||||
int H1 = (int) treeHeightW;
|
||||
double result = 0;
|
||||
for (int h = 1; h < H1; ++h) {
|
||||
result += (1 - Math.pow(k, H1 - h));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void calcOptimizedResult() {
|
||||
double a = 0;
|
||||
if (treeDegreeW > 1) {
|
||||
a = treeHeightW > 1 ? (treeHeightW - 1) * calcAccumulationWithK() * rootCountW : 0;
|
||||
} else if (treeDegreeW == 1) {
|
||||
a = treeHeightW > 1 ? (treeHeightW - 1) * calcAccumulation() * rootCountW : 0;
|
||||
}
|
||||
|
||||
double b = rootCountR * rootCountW * (1 - Math.pow(k, treeNodeCountW));
|
||||
double c = treeHeightR > 1
|
||||
? (treeHeightR - 1) * (totalCountR - rootCountR) * (1 - Math.pow(k, totalCountW))
|
||||
: 0;
|
||||
|
||||
double A = Math.sqrt(a);
|
||||
double B = Math.sqrt(b);
|
||||
double C = Math.sqrt(c);
|
||||
wDelay = (long) Math.ceil(maxDelay * (A / (A + B + C)));
|
||||
w2rDelay = (long) Math.ceil(maxDelay * (B / (A + B + C)));
|
||||
rDelay = (long) Math.ceil(maxDelay * (C / (A + B + C)));
|
||||
|
||||
|
||||
frequencySyncW = wDelay > 0 ? (treeHeightW - 1) / wDelay : 0;
|
||||
frequencySyncR = rDelay > 0 ? (treeHeightR - 1) / rDelay : 0;
|
||||
frequencySyncWR = w2rDelay > 0 ? (1.0 / w2rDelay) : 0;
|
||||
|
||||
totalData = (long) (domainSize
|
||||
* (frequencySyncW * a + frequencySyncWR * b + frequencySyncR * c));
|
||||
}
|
||||
}
|
@ -0,0 +1,197 @@
|
||||
package org.bdware.sc.crdt.planning;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class SharableNetworkPlanning {
|
||||
protected String[] nodeIds;
|
||||
protected int[] writers;
|
||||
protected int[] readers;
|
||||
protected long maxDelay;
|
||||
|
||||
protected int bandwidthDownload;
|
||||
protected int bandwidthUpload;
|
||||
protected int dataSize;
|
||||
|
||||
|
||||
protected long wDelay;
|
||||
protected long rDelay;
|
||||
protected long w2rDelay;
|
||||
|
||||
|
||||
protected double totalCountW;
|
||||
protected double rootCountW;
|
||||
protected double treeNodeCountW;
|
||||
protected double treeDegreeW;
|
||||
protected double treeHeightW;
|
||||
protected double frequencySyncW;
|
||||
|
||||
|
||||
protected double totalCountR;
|
||||
protected double rootCountR;
|
||||
protected double treeNodeCountR;
|
||||
protected double treeDegreeR;
|
||||
protected double treeHeightR;
|
||||
protected double frequencySyncR;
|
||||
|
||||
protected double frequencySyncWR;
|
||||
|
||||
protected long totalData;
|
||||
|
||||
protected static double logNM(double n, double m) {
|
||||
return Math.log(m) / Math.log(n);
|
||||
}
|
||||
|
||||
public void adjustWriterTree(int rootCountW, int treeDegreeW) {
|
||||
this.rootCountW = rootCountW;
|
||||
this.treeDegreeW = treeDegreeW;
|
||||
this.treeNodeCountW = Math.ceil(totalCountW / rootCountW);
|
||||
if (treeDegreeW > 1) {
|
||||
this.treeHeightW =
|
||||
Math.ceil(logNM(treeDegreeW, treeNodeCountW * (treeDegreeW - 1) + 1));
|
||||
} else {
|
||||
this.treeHeightW = treeNodeCountW;
|
||||
}
|
||||
}
|
||||
|
||||
public void adjustReaderTree(int rootCountR, int treeDegreeR) {
|
||||
this.rootCountR = rootCountR;
|
||||
this.treeDegreeR = treeDegreeR;
|
||||
this.treeNodeCountR = Math.ceil(totalCountR / rootCountR);
|
||||
if (treeDegreeR > 1) {
|
||||
this.treeHeightR =
|
||||
Math.ceil(logNM(treeDegreeR, treeNodeCountR * (treeDegreeR - 1) + 1));
|
||||
} else {
|
||||
this.treeHeightR = treeNodeCountR;
|
||||
}
|
||||
}
|
||||
|
||||
protected void adjustAndCalc() {
|
||||
long minTotalData = Long.MAX_VALUE;
|
||||
String result = "";
|
||||
for (int rootCountW = 1; rootCountW <= writers.length; ++rootCountW) {
|
||||
int nodeCountPerTree = (int) Math.ceil(writers.length * 1.0 / rootCountW);
|
||||
for (int treeDegreeW = 1; treeDegreeW <= nodeCountPerTree; ++treeDegreeW) {
|
||||
adjustWriterTree(rootCountW, treeDegreeW);
|
||||
for (int rootCountR = 1; rootCountR <= readers.length; ++rootCountR) {
|
||||
int maxTreeDegreeR = (int) Math.ceil(readers.length * 1.0 / rootCountR);
|
||||
for (int treeDegreeR = 1; treeDegreeR <= maxTreeDegreeR; ++treeDegreeR) {
|
||||
adjustReaderTree(rootCountR, treeDegreeR);
|
||||
calcOptimizedResult();
|
||||
if (!readerTreeConstraint()) {
|
||||
// System.out.println("reader");
|
||||
continue;
|
||||
}
|
||||
if (!writerTreeConstraint()) {
|
||||
// System.out.println("writer");
|
||||
continue;
|
||||
}
|
||||
if (!writer2ReaderConstraint()) {
|
||||
// System.out.println("writer2Reader");
|
||||
continue;
|
||||
}
|
||||
if (totalData > 0 && minTotalData > totalData) {
|
||||
minTotalData = totalData;
|
||||
result = toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(minTotalData);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
protected void calcOptimizedResult() {}
|
||||
|
||||
protected boolean writer2ReaderConstraint() {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected boolean writerTreeConstraint() {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected boolean readerTreeConstraint() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void allocate() {
|
||||
Set<Integer> writerOnlySet = new LinkedHashSet<>();
|
||||
for (int i : writers) {
|
||||
writerOnlySet.add(i);
|
||||
}
|
||||
Set<Integer> readerOnlySet = new LinkedHashSet<>();
|
||||
Set<Integer> rwSet = new LinkedHashSet<>();
|
||||
for (int i : readers) {
|
||||
if (writerOnlySet.contains(i)) {
|
||||
rwSet.add(i);
|
||||
writerOnlySet.remove(i);
|
||||
} else {
|
||||
readerOnlySet.add(i);
|
||||
}
|
||||
}
|
||||
int[] writerParents = allocateTreeNode(writerOnlySet, rwSet, (int) rootCountW,
|
||||
(int) treeDegreeW, wDelay, (int) treeHeightW);
|
||||
int[] readerParents = allocateTreeNode(readerOnlySet, rwSet, (int) rootCountR,
|
||||
(int) treeDegreeR, rDelay, (int) treeHeightR);
|
||||
System.out.println(readerParents);
|
||||
}
|
||||
|
||||
private int[] allocateTreeNode(Set<Integer> onlySet, Set<Integer> rwSet, int rootCount,
|
||||
int degree, long delay, int height) {
|
||||
int[] result = new int[nodeIds.length];
|
||||
long[] writerInterval = new long[nodeIds.length];
|
||||
Arrays.fill(result, -2);
|
||||
|
||||
Map<Integer, List<Integer>> children = new HashMap<>();
|
||||
Deque<Integer> notFullNodesIdx = new LinkedList<>();
|
||||
for (Integer idx : onlySet) {
|
||||
if (children.size() < rootCount) {
|
||||
children.put(idx, new ArrayList<>());
|
||||
notFullNodesIdx.add(idx);
|
||||
result[idx] = -1;
|
||||
} else {
|
||||
int parentIdx = notFullNodesIdx.peek();
|
||||
children.computeIfAbsent(parentIdx, k -> new ArrayList<>()).add(idx);
|
||||
notFullNodesIdx.addLast(idx);
|
||||
if (children.get(parentIdx).size() >= degree) {
|
||||
notFullNodesIdx.pop();
|
||||
}
|
||||
writerInterval[idx] = delay / (height - 1);
|
||||
}
|
||||
}
|
||||
|
||||
// 读写节点放在尽量靠近叶子节点,以便带宽的平衡
|
||||
for (Integer idx : rwSet) {
|
||||
if (children.size() < rootCount) {
|
||||
children.put(idx, new ArrayList<>());
|
||||
notFullNodesIdx.add(idx);
|
||||
result[idx] = -1;
|
||||
} else {
|
||||
int parentIdx = notFullNodesIdx.peek();
|
||||
children.computeIfAbsent(parentIdx, k -> new ArrayList<>()).add(idx);
|
||||
notFullNodesIdx.addLast(idx);
|
||||
if (children.get(parentIdx).size() >= degree) {
|
||||
notFullNodesIdx.pop();
|
||||
}
|
||||
writerInterval[idx] = delay / (height - 1);
|
||||
}
|
||||
}
|
||||
|
||||
for (Integer parentId : children.keySet()) {
|
||||
for (int childId : children.get(parentId)) {
|
||||
result[childId] = parentId;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String result = "[" + wDelay + "," + w2rDelay + "," + rDelay + "]\n"
|
||||
+ "writer tree: degree " + treeDegreeW + ", count " + rootCountW + ",\n"
|
||||
+ "reader tree: degree " + treeDegreeR + ", count " + rootCountR;
|
||||
return result;
|
||||
}
|
||||
}
|
38
src/main/java/org/bdware/sc/crdt/proxy/AWORSetProxy.java
Normal file
38
src/main/java/org/bdware/sc/crdt/proxy/AWORSetProxy.java
Normal file
@ -0,0 +1,38 @@
|
||||
package org.bdware.sc.crdt.proxy;
|
||||
|
||||
import org.bdware.crdt.set.AWORSet;
|
||||
import org.bdware.sc.crdt.SharableVarState;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class AWORSetProxy extends SharableVar<AWORSet<Object>> {
|
||||
public AWORSetProxy(String varId, String cpId, SharableVarState.SharableVarConfiguration conf) {
|
||||
super(varId, cpId, conf);
|
||||
}
|
||||
|
||||
public void add(Object val) {
|
||||
if (writerVar != null) {
|
||||
AWORSet<Object> delta = writerVar.add(val);
|
||||
writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(Object val) {
|
||||
if (writerVar != null) {
|
||||
AWORSet<Object> delta = writerVar.remove(val);
|
||||
writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
}
|
||||
|
||||
public Set<Object> read() {
|
||||
if (readerVar != null) {
|
||||
return readerVar.read();
|
||||
}
|
||||
return writerVar.read();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AWORSet<Object> createDeltaCrdt(String nodeId, String varId) {
|
||||
return new AWORSet<>(nodeId, varId);
|
||||
}
|
||||
}
|
36
src/main/java/org/bdware/sc/crdt/proxy/DWFlagProxy.java
Normal file
36
src/main/java/org/bdware/sc/crdt/proxy/DWFlagProxy.java
Normal file
@ -0,0 +1,36 @@
|
||||
package org.bdware.sc.crdt.proxy;
|
||||
|
||||
import org.bdware.crdt.flag.DWFlag;
|
||||
import org.bdware.sc.crdt.SharableVarState;
|
||||
|
||||
public class DWFlagProxy extends SharableVar<DWFlag> {
|
||||
public DWFlagProxy(String varId, String cpId, SharableVarState.SharableVarConfiguration conf) {
|
||||
super(varId, cpId, conf);
|
||||
}
|
||||
|
||||
public void enable() {
|
||||
if (writerVar != null) {
|
||||
DWFlag delta = writerVar.enable();
|
||||
writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
}
|
||||
|
||||
public void disable() {
|
||||
if (writerVar != null) {
|
||||
DWFlag delta = writerVar.disable();
|
||||
writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean read() {
|
||||
if (readerVar != null) {
|
||||
return readerVar.read();
|
||||
}
|
||||
return writerVar.read();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected DWFlag createDeltaCrdt(String nodeId, String varId) {
|
||||
return new DWFlag(nodeId, varId);
|
||||
}
|
||||
}
|
36
src/main/java/org/bdware/sc/crdt/proxy/EWFlagProxy.java
Normal file
36
src/main/java/org/bdware/sc/crdt/proxy/EWFlagProxy.java
Normal file
@ -0,0 +1,36 @@
|
||||
package org.bdware.sc.crdt.proxy;
|
||||
|
||||
import org.bdware.crdt.flag.EWFlag;
|
||||
import org.bdware.sc.crdt.SharableVarState;
|
||||
|
||||
public class EWFlagProxy extends SharableVar<EWFlag> {
|
||||
public EWFlagProxy(String varId, String cpId, SharableVarState.SharableVarConfiguration conf) {
|
||||
super(varId, cpId, conf);
|
||||
}
|
||||
|
||||
public void enable() {
|
||||
if (writerVar != null) {
|
||||
EWFlag delta = writerVar.enable();
|
||||
writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
}
|
||||
|
||||
public void disable() {
|
||||
if (writerVar != null) {
|
||||
EWFlag delta = writerVar.disable();
|
||||
writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean read() {
|
||||
if (readerVar != null) {
|
||||
return readerVar.read();
|
||||
}
|
||||
return writerVar.read();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected EWFlag createDeltaCrdt(String nodeId, String varId) {
|
||||
return new EWFlag(nodeId, varId);
|
||||
}
|
||||
}
|
47
src/main/java/org/bdware/sc/crdt/proxy/GCounterProxy.java
Normal file
47
src/main/java/org/bdware/sc/crdt/proxy/GCounterProxy.java
Normal file
@ -0,0 +1,47 @@
|
||||
package org.bdware.sc.crdt.proxy;
|
||||
|
||||
import org.bdware.crdt.counter.GCounter;
|
||||
import org.bdware.sc.crdt.SharableVarState;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class GCounterProxy extends SharableVar<GCounter> {
|
||||
|
||||
public GCounterProxy(String varId, String cpId,
|
||||
SharableVarState.SharableVarConfiguration conf) {
|
||||
super(varId, cpId, conf);
|
||||
}
|
||||
|
||||
public void inc() {
|
||||
if (writerVar != null) {
|
||||
GCounter delta = writerVar.inc();
|
||||
writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
}
|
||||
|
||||
public void inc(long var) {
|
||||
if (writerVar != null) {
|
||||
GCounter delta = writerVar.inc(var);
|
||||
writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
}
|
||||
|
||||
public Long read() {
|
||||
if (readerVar != null) {
|
||||
return readerVar.read();
|
||||
}
|
||||
return writerVar.read();
|
||||
}
|
||||
|
||||
public Map<String, Long> getM() {
|
||||
if (readerVar != null) {
|
||||
return readerVar.getM();
|
||||
}
|
||||
return writerVar.getM();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GCounter createDeltaCrdt(String nodeId, String varId) {
|
||||
return new GCounter(nodeId, varId);
|
||||
}
|
||||
}
|
31
src/main/java/org/bdware/sc/crdt/proxy/GSetProxy.java
Normal file
31
src/main/java/org/bdware/sc/crdt/proxy/GSetProxy.java
Normal file
@ -0,0 +1,31 @@
|
||||
package org.bdware.sc.crdt.proxy;
|
||||
|
||||
import org.bdware.crdt.set.GSet;
|
||||
import org.bdware.sc.crdt.SharableVarState;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class GSetProxy extends SharableVar<GSet<Object>> {
|
||||
public GSetProxy(String varId, String cpId, SharableVarState.SharableVarConfiguration conf) {
|
||||
super(varId, cpId, conf);
|
||||
}
|
||||
|
||||
public void add(Object val) {
|
||||
if (writerVar != null) {
|
||||
GSet<Object> delta = writerVar.add(val);
|
||||
writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
}
|
||||
|
||||
public Set<Object> read() {
|
||||
if (readerVar != null) {
|
||||
return readerVar.read();
|
||||
}
|
||||
return writerVar.read();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GSet<Object> createDeltaCrdt(String nodeId, String varId) {
|
||||
return new GSet<>(nodeId, varId);
|
||||
}
|
||||
}
|
30
src/main/java/org/bdware/sc/crdt/proxy/LWWRegisterProxy.java
Normal file
30
src/main/java/org/bdware/sc/crdt/proxy/LWWRegisterProxy.java
Normal file
@ -0,0 +1,30 @@
|
||||
package org.bdware.sc.crdt.proxy;
|
||||
|
||||
import org.bdware.crdt.register.LWWRegister;
|
||||
import org.bdware.sc.crdt.SharableVarState;
|
||||
|
||||
public class LWWRegisterProxy extends SharableVar<LWWRegister<Long, Object>> {
|
||||
public LWWRegisterProxy(String varId, String cpId,
|
||||
SharableVarState.SharableVarConfiguration conf) {
|
||||
super(varId, cpId, conf);
|
||||
}
|
||||
|
||||
public void write(Object val) {
|
||||
if (writerVar != null) {
|
||||
LWWRegister<Long, Object> delta = writerVar.write(System.currentTimeMillis(), val);
|
||||
writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
}
|
||||
|
||||
public Object read() {
|
||||
if (readerVar != null) {
|
||||
return readerVar.read();
|
||||
}
|
||||
return writerVar.read();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected LWWRegister<Long, Object> createDeltaCrdt(String nodeId, String varId) {
|
||||
return new LWWRegister<>(nodeId, varId);
|
||||
}
|
||||
}
|
36
src/main/java/org/bdware/sc/crdt/proxy/MVRegisterProxy.java
Normal file
36
src/main/java/org/bdware/sc/crdt/proxy/MVRegisterProxy.java
Normal file
@ -0,0 +1,36 @@
|
||||
package org.bdware.sc.crdt.proxy;
|
||||
|
||||
import org.bdware.crdt.register.MVRegister;
|
||||
import org.bdware.sc.crdt.SharableVarState;
|
||||
|
||||
public class MVRegisterProxy extends SharableVar<MVRegister<Object>> {
|
||||
|
||||
public MVRegisterProxy(String varId, String cpId,
|
||||
SharableVarState.SharableVarConfiguration conf) {
|
||||
super(varId, cpId, conf);
|
||||
}
|
||||
|
||||
public void write(Object val) {
|
||||
if (writerVar != null) {
|
||||
MVRegister<Object> delta = writerVar.write(val);
|
||||
writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
}
|
||||
|
||||
public void resolve(Object val) {
|
||||
MVRegister<Object> delta = writerVar.resolve();
|
||||
writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
|
||||
public Object read() {
|
||||
if (readerVar != null) {
|
||||
return readerVar.read();
|
||||
}
|
||||
return writerVar.read();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MVRegister<Object> createDeltaCrdt(String nodeId, String varId) {
|
||||
return new MVRegister<>(nodeId, varId);
|
||||
}
|
||||
}
|
51
src/main/java/org/bdware/sc/crdt/proxy/PNCounterProxy.java
Normal file
51
src/main/java/org/bdware/sc/crdt/proxy/PNCounterProxy.java
Normal file
@ -0,0 +1,51 @@
|
||||
package org.bdware.sc.crdt.proxy;
|
||||
|
||||
import org.bdware.crdt.counter.PNCounter;
|
||||
import org.bdware.sc.crdt.SharableVarState;
|
||||
|
||||
public class PNCounterProxy extends SharableVar<PNCounter> {
|
||||
public PNCounterProxy(String varId, String cpId,
|
||||
SharableVarState.SharableVarConfiguration conf) {
|
||||
super(varId, cpId, conf);
|
||||
}
|
||||
|
||||
public void inc() {
|
||||
if (writerVar != null) {
|
||||
PNCounter delta = writerVar.inc();
|
||||
writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
}
|
||||
|
||||
public void inc(long val) {
|
||||
if (writerVar != null) {
|
||||
PNCounter delta = writerVar.inc(val);
|
||||
writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
}
|
||||
|
||||
public void dec() {
|
||||
if (writerVar != null) {
|
||||
PNCounter delta = writerVar.dec();
|
||||
writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
}
|
||||
|
||||
public void dec(long val) {
|
||||
if (writerVar != null) {
|
||||
PNCounter delta = writerVar.dec(val);
|
||||
writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
}
|
||||
|
||||
public Long read() {
|
||||
if (readerVar != null) {
|
||||
return readerVar.read();
|
||||
}
|
||||
return writerVar.read();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PNCounter createDeltaCrdt(String nodeId, String varId) {
|
||||
return new PNCounter(nodeId, varId);
|
||||
}
|
||||
}
|
39
src/main/java/org/bdware/sc/crdt/proxy/RWLWWSetProxy.java
Normal file
39
src/main/java/org/bdware/sc/crdt/proxy/RWLWWSetProxy.java
Normal file
@ -0,0 +1,39 @@
|
||||
package org.bdware.sc.crdt.proxy;
|
||||
|
||||
import org.bdware.crdt.set.RWLWWSet;
|
||||
import org.bdware.sc.crdt.SharableVarState;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class RWLWWSetProxy extends SharableVar<RWLWWSet<Long, Object>> {
|
||||
public RWLWWSetProxy(String varId, String cpId,
|
||||
SharableVarState.SharableVarConfiguration conf) {
|
||||
super(varId, cpId, conf);
|
||||
}
|
||||
|
||||
public void add(Object val) {
|
||||
if (writerVar != null) {
|
||||
RWLWWSet<Long, Object> delta = writerVar.add(System.currentTimeMillis(), val);
|
||||
writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(Object val) {
|
||||
if (writerVar != null) {
|
||||
RWLWWSet<Long, Object> delta = writerVar.remove(System.currentTimeMillis(), val);
|
||||
writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
}
|
||||
|
||||
public Set<Object> read() {
|
||||
if (readerVar != null) {
|
||||
return readerVar.read();
|
||||
}
|
||||
return writerVar.read();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RWLWWSet<Long, Object> createDeltaCrdt(String nodeId, String varId) {
|
||||
return new RWLWWSet<>(nodeId, varId);
|
||||
}
|
||||
}
|
38
src/main/java/org/bdware/sc/crdt/proxy/RWORSetProxy.java
Normal file
38
src/main/java/org/bdware/sc/crdt/proxy/RWORSetProxy.java
Normal file
@ -0,0 +1,38 @@
|
||||
package org.bdware.sc.crdt.proxy;
|
||||
|
||||
import org.bdware.crdt.set.RWORSet;
|
||||
import org.bdware.sc.crdt.SharableVarState;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class RWORSetProxy extends SharableVar<RWORSet<Object>> {
|
||||
public RWORSetProxy(String varId, String cpId, SharableVarState.SharableVarConfiguration conf) {
|
||||
super(varId, cpId, conf);
|
||||
}
|
||||
|
||||
public void add(Object val) {
|
||||
if (writerVar != null) {
|
||||
RWORSet<Object> delta = writerVar.add(val);
|
||||
writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(Object val) {
|
||||
if (writerVar != null) {
|
||||
RWORSet<Object> delta = writerVar.remove(val);
|
||||
writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
}
|
||||
|
||||
public Set<Object> read() {
|
||||
if (readerVar != null) {
|
||||
return readerVar.read();
|
||||
}
|
||||
return writerVar.read();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RWORSet<Object> createDeltaCrdt(String nodeId, String varId) {
|
||||
return new RWORSet<>(nodeId, varId);
|
||||
}
|
||||
}
|
146
src/main/java/org/bdware/sc/crdt/proxy/SharableVar.java
Normal file
146
src/main/java/org/bdware/sc/crdt/proxy/SharableVar.java
Normal file
@ -0,0 +1,146 @@
|
||||
package org.bdware.sc.crdt.proxy;
|
||||
|
||||
import io.netty.util.HashedWheelTimer;
|
||||
import io.netty.util.Timeout;
|
||||
import io.netty.util.TimerTask;
|
||||
import org.bdware.crdt.basic.JoinableCRDT;
|
||||
import org.bdware.sc.crdt.SharableVarManager;
|
||||
import org.bdware.sc.crdt.SharableVarState;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public abstract class SharableVar<T extends JoinableCRDT> {
|
||||
// 用于writer节点的变量副本
|
||||
public T writerVar;
|
||||
// 用于reader节点的变量副本
|
||||
public T readerVar;
|
||||
|
||||
public Queue<JoinableCRDT> writerVarDeltaQueue;
|
||||
public Queue<JoinableCRDT> readerVarDeltaQueue;
|
||||
|
||||
public String varId;
|
||||
public SharableVarState sharableVarState;
|
||||
|
||||
public HashedWheelTimer readerTimer;
|
||||
public ReaderSyncTimerTask readerSyncTimerTask;
|
||||
public HashedWheelTimer writerTimer;
|
||||
public WriterSyncTimerTask writerSyncTimerTask;
|
||||
|
||||
public SharableVar(String varId, String cpId,
|
||||
SharableVarState.SharableVarConfiguration resolveResult) {
|
||||
this.varId = varId;
|
||||
this.sharableVarState = new SharableVarState(cpId, resolveResult);
|
||||
|
||||
if (this.sharableVarState.isReaderFlag()) {
|
||||
this.readerVar = createDeltaCrdt(cpId, varId);
|
||||
this.readerVarDeltaQueue = new LinkedList<>();
|
||||
if (this.sharableVarState.getReaderChildren() != null
|
||||
&& this.sharableVarState.getReaderChildren().size() > 0
|
||||
&& this.sharableVarState.getReaderInterval() != null
|
||||
&& this.sharableVarState.getReaderInterval() > 0) {
|
||||
this.readerTimer = new HashedWheelTimer(r -> {
|
||||
Thread t = Executors.defaultThreadFactory().newThread(r);
|
||||
t.setDaemon(true);
|
||||
return t;
|
||||
}, 5, TimeUnit.MILLISECONDS, 2);
|
||||
this.readerTimer.newTimeout(timeout -> {
|
||||
readerSyncTimerTask = new ReaderSyncTimerTask();
|
||||
readerTimer.newTimeout(readerSyncTimerTask,
|
||||
sharableVarState.getReaderInterval(), TimeUnit.SECONDS);
|
||||
}, this.sharableVarState.getReaderInterval(), TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
if (this.sharableVarState.isWriterFlag()) {
|
||||
this.writerVar = createDeltaCrdt(cpId, varId);
|
||||
this.writerVarDeltaQueue = new LinkedList<>();
|
||||
if (this.sharableVarState.getWriteInterval() != null
|
||||
&& this.sharableVarState.getWriteInterval() > 0) {
|
||||
this.writerTimer = new HashedWheelTimer(r -> {
|
||||
Thread t = Executors.defaultThreadFactory().newThread(r);
|
||||
t.setDaemon(true);
|
||||
return t;
|
||||
}, 5, TimeUnit.MILLISECONDS, 2);
|
||||
this.writerTimer.newTimeout(timeout -> {
|
||||
writerSyncTimerTask = new WriterSyncTimerTask();
|
||||
writerTimer.newTimeout(writerSyncTimerTask, sharableVarState.getWriteInterval(),
|
||||
TimeUnit.SECONDS);
|
||||
}, this.sharableVarState.getWriteInterval(), TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void syncReaderVar() {
|
||||
if (readerVarDeltaQueue.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
JoinableCRDT joinedDelta = createDeltaCrdt(null, varId);
|
||||
synchronized (readerVarDeltaQueue) {
|
||||
while (!readerVarDeltaQueue.isEmpty()) {
|
||||
JoinableCRDT delta = readerVarDeltaQueue.poll();
|
||||
joinedDelta.join(delta);
|
||||
}
|
||||
}
|
||||
String content = JsonUtil.toJson(joinedDelta);
|
||||
SharableVarManager.instance.broadcastSyncMessage(varId,
|
||||
sharableVarState.getReaderChildren(), content, "r2r");
|
||||
}
|
||||
|
||||
private void syncWriterVar() {
|
||||
if (writerVarDeltaQueue.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
JoinableCRDT joinedDelta = createDeltaCrdt(null, varId);
|
||||
synchronized (writerVarDeltaQueue) {
|
||||
while (!writerVarDeltaQueue.isEmpty()) {
|
||||
JoinableCRDT delta = writerVarDeltaQueue.poll();
|
||||
joinedDelta.join(delta);
|
||||
}
|
||||
}
|
||||
String content = JsonUtil.toJson(joinedDelta);
|
||||
if (sharableVarState.getWriterParent() != null) {
|
||||
// 父节点是Writer
|
||||
SharableVarManager.instance.broadcastSyncMessage(varId,
|
||||
Collections.singletonList(sharableVarState.getWriterParent()), content, "w2w");
|
||||
} else if (sharableVarState.getReaderRoots() != null) {
|
||||
// 自己是writer根结点 向reader根结点们广播
|
||||
SharableVarManager.instance.broadcastSyncMessage(varId,
|
||||
sharableVarState.getReaderRoots(), content, "w2r");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
abstract protected T createDeltaCrdt(String nodeId, String varId);
|
||||
|
||||
class ReaderSyncTimerTask implements TimerTask {
|
||||
@Override
|
||||
public void run(Timeout timeout) throws Exception {
|
||||
try {
|
||||
syncReaderVar();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
readerTimer.newTimeout(readerSyncTimerTask, sharableVarState.getReaderInterval(),
|
||||
TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class WriterSyncTimerTask implements TimerTask {
|
||||
@Override
|
||||
public void run(Timeout timeout) throws Exception {
|
||||
try {
|
||||
syncWriterVar();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
writerTimer.newTimeout(writerSyncTimerTask, sharableVarState.getWriteInterval(),
|
||||
TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
38
src/main/java/org/bdware/sc/crdt/proxy/TPSetProxy.java
Normal file
38
src/main/java/org/bdware/sc/crdt/proxy/TPSetProxy.java
Normal file
@ -0,0 +1,38 @@
|
||||
package org.bdware.sc.crdt.proxy;
|
||||
|
||||
import org.bdware.crdt.set.TPSet;
|
||||
import org.bdware.sc.crdt.SharableVarState;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class TPSetProxy extends SharableVar<TPSet<Object>> {
|
||||
public TPSetProxy(String varId, String cpId, SharableVarState.SharableVarConfiguration conf) {
|
||||
super(varId, cpId, conf);
|
||||
}
|
||||
|
||||
public void add(Object val) {
|
||||
if (writerVar != null) {
|
||||
TPSet<Object> delta = writerVar.add(val);
|
||||
writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(Object val) {
|
||||
if (writerVar != null) {
|
||||
TPSet<Object> delta = writerVar.remove(val);
|
||||
writerVarDeltaQueue.add(delta);
|
||||
}
|
||||
}
|
||||
|
||||
public Set<Object> read() {
|
||||
if (readerVar != null) {
|
||||
return readerVar.read();
|
||||
}
|
||||
return writerVar.read();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected TPSet<Object> createDeltaCrdt(String nodeId, String varId) {
|
||||
return new TPSet<>(nodeId, varId);
|
||||
}
|
||||
}
|
@ -23,23 +23,26 @@ public class ConfidentialContractUtil {
|
||||
|
||||
public static final String CONFIDENTIAL_TEMPLATE_PATH = System.getenv("GRAPHENE_DIR")
|
||||
+ File.separator + "Examples" + File.separator + "nodejs-secret";
|
||||
public static final String CONFIDENTIAL_SCRIPT_PATH = System.getenv("GRAPHENE_DIR")
|
||||
+ File.separator + "App";
|
||||
public static final String CONFIDENTIAL_SCRIPT_PATH =
|
||||
System.getenv("GRAPHENE_DIR") + File.separator + "App";
|
||||
public static final String[] COMMAND = {"bash", "executeContract.sh"};
|
||||
private static final Type MapType = TypeToken.getParameterized(HashMap.class, String.class, String.class).getType();
|
||||
private static final Type MapType =
|
||||
TypeToken.getParameterized(HashMap.class, String.class, String.class).getType();
|
||||
|
||||
public static String executeConfidentialContract(ContractRequest input) throws IOException, InterruptedException {
|
||||
public static String executeConfidentialContract(ContractRequest input)
|
||||
throws IOException, InterruptedException {
|
||||
File runDir = new File(CONFIDENTIAL_SCRIPT_PATH + File.separator + input.getRequestID());
|
||||
ProcessBuilder pb = new ProcessBuilder(COMMAND);
|
||||
pb.directory(runDir);
|
||||
Process p = pb.start();
|
||||
p.waitFor();
|
||||
File resultFile = new File(CONFIDENTIAL_SCRIPT_PATH + File.separator + input.getRequestID() +
|
||||
File.separator + "result.json");
|
||||
File resultFile = new File(CONFIDENTIAL_SCRIPT_PATH + File.separator + input.getRequestID()
|
||||
+ File.separator + "result.json");
|
||||
return FileUtils.readFileToString(resultFile, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public static void generateConfidentialContract(ContractNode cn, ScriptObjectMirror globalVars, Global global) {
|
||||
public static void generateConfidentialContract(ContractNode cn, ScriptObjectMirror globalVars,
|
||||
Global global) {
|
||||
List<FunctionNode> functionNodes = cn.getFunctions();
|
||||
for (FunctionNode fn : functionNodes) {
|
||||
// assuming only one confidential function for now
|
||||
@ -54,18 +57,46 @@ public class ConfidentialContractUtil {
|
||||
}
|
||||
}
|
||||
// load necessary Node.js libraries
|
||||
jsStr.append("var fs = require('fs');\n" + "var crypto = require('crypto');\n" + "var sm2 = require('sm-crypto').sm2;\n");
|
||||
jsStr.append("var fs = require('fs');\n" + "var crypto = require('crypto');\n"
|
||||
+ "var sm2 = require('sm-crypto').sm2;\n");
|
||||
// load Global variables and arguments from files
|
||||
jsStr.append("let rawGlobal = fs.readFileSync('global.json').toString();\n" + "let Global = JSON.parse(rawGlobal);\n");
|
||||
jsStr.append("let rawArg = fs.readFileSync('arg.json').toString();\n" + "let jsonArg = JSON.parse(rawArg);\n" + "let requester = jsonArg.requester;\n" + "let arg = jsonArg.arg;\n");
|
||||
jsStr.append("let rawGlobal = fs.readFileSync('global.json').toString();\n"
|
||||
+ "let Global = JSON.parse(rawGlobal);\n");
|
||||
jsStr.append("let rawArg = fs.readFileSync('arg.json').toString();\n"
|
||||
+ "let jsonArg = JSON.parse(rawArg);\n"
|
||||
+ "let requester = jsonArg.requester;\n" + "let arg = jsonArg.arg;\n");
|
||||
jsStr.append("let srcStr = fs.readFileSync('contract.js').toString();\n");
|
||||
// verify signatures and decrypt all confidential variables Important!!!!!
|
||||
jsStr.append("for (var k in Global) {\n" + " if (Global.hasOwnProperty(k)) {\n" + " if (k.startsWith('conf_')) {\n" + " let sig = Global[k].signature;\n" + " let pubKey = Global[k].owner;\n" + " let verifyResult = sm2.doVerifySignature(srcStr, sig, pubKey);\n" + " if (verifyResult) {\n" + " let newKey = k.substring(5);\n" + " let decKey = Buffer.from(process.env['KEY_'+pubKey.substring(0,10).toUpperCase()], 'hex');\n" + " let decIv = Buffer.from(Global[k].iv, 'hex');\n" + " let cipherText = Buffer.from(Global[k].cipherText, 'hex');\n" + " let decipher = crypto.createDecipheriv('aes-256-cbc', decKey, decIv);\n" + " let decrypted = decipher.update(cipherText);\n" + " decrypted = Buffer.concat([decrypted, decipher.final()]);\n" + " let plaintext = decrypted.toString();\n" + " Global[newKey] = plaintext;\n" + " }\n" + " }\n" + " }\n" + "}\n");
|
||||
jsStr.append("for (var k in Global) {\n" + " if (Global.hasOwnProperty(k)) {\n"
|
||||
+ " if (k.startsWith('conf_')) {\n"
|
||||
+ " let sig = Global[k].signature;\n"
|
||||
+ " let pubKey = Global[k].owner;\n"
|
||||
+ " let verifyResult = sm2.doVerifySignature(srcStr, sig, pubKey);\n"
|
||||
+ " if (verifyResult) {\n" + " let newKey = k.substring(5);\n"
|
||||
+ " let decKey = Buffer.from(process.env['KEY_'+pubKey.substring(0,10).toUpperCase()], 'hex');\n"
|
||||
+ " let decIv = Buffer.from(Global[k].iv, 'hex');\n"
|
||||
+ " let cipherText = Buffer.from(Global[k].cipherText, 'hex');\n"
|
||||
+ " let decipher = crypto.createDecipheriv('aes-256-cbc', decKey, decIv);\n"
|
||||
+ " let decrypted = decipher.update(cipherText);\n"
|
||||
+ " decrypted = Buffer.concat([decrypted, decipher.final()]);\n"
|
||||
+ " let plaintext = decrypted.toString();\n"
|
||||
+ " Global[newKey] = plaintext;\n" + " }\n" + " }\n"
|
||||
+ " }\n" + "}\n");
|
||||
// call function
|
||||
jsStr.append("var ret = ").append(fn.functionName).append("(arg, requester, null);\n");
|
||||
// TODO: encrypt all confidential variables so state can be updated in confidential function @shujunyi
|
||||
jsStr.append("var ret = ").append(fn.functionName)
|
||||
.append("(arg, requester, null);\n");
|
||||
// TODO: encrypt all confidential variables so state can be updated in confidential
|
||||
// function @shujunyi
|
||||
// encrypt return value and write to a file
|
||||
jsStr.append("var retStr = JSON.stringify(ret);\n" + "var key = Buffer.from(process.env['KEY_'+requester.substring(0,10).toUpperCase()], 'hex');\n" + "var iv = crypto.randomBytes(16);\n" + "let cipher = crypto.createCipheriv('aes-256-cbc', key, iv); \n" + "let encRet = cipher.update(retStr);\n" + "encRet = Buffer.concat([encRet, cipher.final()]);\n" + "let result = {iv: iv.toString('hex'), encryptedData: encRet.toString('hex')};\n" + "let resultStr = JSON.stringify(result);\n" + "fs.writeFileSync('result.json', resultStr);\n");
|
||||
jsStr.append("var retStr = JSON.stringify(ret);\n"
|
||||
+ "var key = Buffer.from(process.env['KEY_'+requester.substring(0,10).toUpperCase()], 'hex');\n"
|
||||
+ "var iv = crypto.randomBytes(16);\n"
|
||||
+ "let cipher = crypto.createCipheriv('aes-256-cbc', key, iv); \n"
|
||||
+ "let encRet = cipher.update(retStr);\n"
|
||||
+ "encRet = Buffer.concat([encRet, cipher.final()]);\n"
|
||||
+ "let result = {iv: iv.toString('hex'), encryptedData: encRet.toString('hex')};\n"
|
||||
+ "let resultStr = JSON.stringify(result);\n"
|
||||
+ "fs.writeFileSync('result.json', resultStr);\n");
|
||||
// put script into Global so owner can send it and collect signatures
|
||||
Object som = ScriptObjectMirror.wrap(jsStr.toString(), global);
|
||||
globalVars.put("src_" + fn.functionName, som);
|
||||
@ -85,11 +116,8 @@ public class ConfidentialContractUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public static void dumpScriptAndStates(
|
||||
Invocable engine,
|
||||
FunctionNode functionNode,
|
||||
ContractRequest input,
|
||||
ScriptObjectMirror globalVars)
|
||||
public static void dumpScriptAndStates(Invocable engine, FunctionNode functionNode,
|
||||
ContractRequest input, ScriptObjectMirror globalVars)
|
||||
throws IOException, ScriptException, NoSuchMethodException {
|
||||
Map<String, Object> globalMap = (Map<String, Object>) convertIntoJavaObject(globalVars);
|
||||
String dest = CONFIDENTIAL_SCRIPT_PATH + File.separator + input.getRequestID();
|
||||
@ -136,156 +164,116 @@ public class ConfidentialContractUtil {
|
||||
return dependentFunctions;
|
||||
}
|
||||
|
||||
private static String generateGrapheneManifestStr(Invocable engine, ContractRequest input) throws ScriptException, NoSuchMethodException {
|
||||
String manifestStr = "# Nodejs manifest file example\n" +
|
||||
"#\n" +
|
||||
"# This manifest was prepared and tested on Ubuntu 18.04.\n" +
|
||||
"\n" +
|
||||
"loader.argv0_override = \"nodejs\"\n" +
|
||||
"\n" +
|
||||
"# LibOS layer library of Graphene. There is currently only one implementation,\n" +
|
||||
"# so it is always set to libsysdb.so.\n" +
|
||||
"loader.preload = \"file:$(GRAPHENEDIR)/Runtime/libsysdb.so\"\n" +
|
||||
"\n" +
|
||||
"# Show/hide debug log of Graphene ('inline' or 'none' respectively).\n" +
|
||||
"loader.debug_type = \"$(GRAPHENEDEBUG)\"\n" +
|
||||
"\n" +
|
||||
"# Read application arguments directly from the command line. Don't use this on production!\n" +
|
||||
"loader.insecure__use_cmdline_argv = 1\n" +
|
||||
"\n" +
|
||||
"# Specify paths to search for libraries. The usual LD_LIBRARY_PATH syntax\n" +
|
||||
"# applies. Paths must be in-Graphene visible paths, not host-OS paths (i.e.,\n" +
|
||||
"# paths must be taken from fs.mount.xxx.path, not fs.mount.xxx.uri).\n" +
|
||||
"loader.env.LD_LIBRARY_PATH = \"/lib:/usr/lib:$(ARCH_LIBDIR):/usr/$(ARCH_LIBDIR):./\"\n" +
|
||||
"\n" +
|
||||
"# Mount host-OS directory to required libraries (in 'uri') into in-Graphene\n" +
|
||||
"# visible directory /lib (in 'path').\n" +
|
||||
"fs.mount.lib.type = \"chroot\"\n" +
|
||||
"fs.mount.lib.path = \"/lib\"\n" +
|
||||
"fs.mount.lib.uri = \"file:$(GRAPHENEDIR)/Runtime\"\n" +
|
||||
"\n" +
|
||||
"fs.mount.lib2.type = \"chroot\"\n" +
|
||||
"fs.mount.lib2.path = \"$(ARCH_LIBDIR)\"\n" +
|
||||
"fs.mount.lib2.uri = \"file:$(ARCH_LIBDIR)\"\n" +
|
||||
"\n" +
|
||||
"#fs.mount.lib3.type = \"chroot\"\n" +
|
||||
"#fs.mount.lib3.path = \"/usr/$(ARCH_LIBDIR)\"\n" +
|
||||
"#fs.mount.lib3.uri = \"file:/usr/$(ARCH_LIBDIR)\"\n" +
|
||||
"\n" +
|
||||
"fs.mount.usr.type = \"chroot\"\n" +
|
||||
"fs.mount.usr.path = \"/usr\"\n" +
|
||||
"fs.mount.usr.uri = \"file:/usr\"\n" +
|
||||
"\n" +
|
||||
"# Host-level directory to NSS files required by Glibc + NSS libs\n" +
|
||||
"fs.mount.etc.type = \"chroot\"\n" +
|
||||
"fs.mount.etc.path = \"/etc\"\n" +
|
||||
"fs.mount.etc.uri = \"file:/etc\"\n" +
|
||||
"\n" +
|
||||
"# Workload needs to create temporary files\n" +
|
||||
"fs.mount.tmp.type = \"chroot\"\n" +
|
||||
"fs.mount.tmp.path = \"/tmp\"\n" +
|
||||
"fs.mount.tmp.uri = \"file:/tmp\"\n" +
|
||||
"\n" +
|
||||
"# Set enclave size to 2GB; NodeJS expects around 1.7GB of heap on startup,\n" +
|
||||
"# see e.g. https://github.com/nodejs/node/issues/13018.\n" +
|
||||
"# Recall that SGX v1 requires to specify enclave size at enclave creation time.\n" +
|
||||
"sgx.enclave_size = \"2G\"\n" +
|
||||
"\n" +
|
||||
"# Set maximum number of in-enclave threads (somewhat arbitrarily) to 8. Recall\n" +
|
||||
"# that SGX v1 requires to specify the maximum number of simultaneous threads at\n" +
|
||||
"# enclave creation time.\n" +
|
||||
"sgx.thread_num = 16\n" +
|
||||
"\n" +
|
||||
"# Specify all libraries used by Node.js and its dependencies (including all libs\n" +
|
||||
"# which can be loaded at runtime via dlopen).\n" +
|
||||
"sgx.trusted_files.ld = \"file:$(GRAPHENEDIR)/Runtime/ld-linux-x86-64.so.2\"\n" +
|
||||
"sgx.trusted_files.libc = \"file:$(GRAPHENEDIR)/Runtime/libc.so.6\"\n" +
|
||||
"sgx.trusted_files.libm = \"file:$(GRAPHENEDIR)/Runtime/libm.so.6\"\n" +
|
||||
"sgx.trusted_files.libdl = \"file:$(GRAPHENEDIR)/Runtime/libdl.so.2\"\n" +
|
||||
"sgx.trusted_files.librt = \"file:$(GRAPHENEDIR)/Runtime/librt.so.1\"\n" +
|
||||
"sgx.trusted_files.libutil = \"file:$(GRAPHENEDIR)/Runtime/libutil.so.1\"\n" +
|
||||
"sgx.trusted_files.libpthread = \"file:$(GRAPHENEDIR)/Runtime/libpthread.so.0\"\n" +
|
||||
"sgx.trusted_files.libnssdns = \"file:$(GRAPHENEDIR)/Runtime/libnss_dns.so.2\"\n" +
|
||||
"sgx.trusted_files.libresolv = \"file:$(GRAPHENEDIR)/Runtime/libresolv.so.2\"\n" +
|
||||
"\n" +
|
||||
"sgx.trusted_files.libstdc = \"file:/usr/$(ARCH_LIBDIR)/libstdc++.so.6\"\n" +
|
||||
"sgx.trusted_files.libgccs = \"file:$(ARCH_LIBDIR)/libgcc_s.so.1\"\n" +
|
||||
"sgx.trusted_files.libaptpkg = \"file:/usr/$(ARCH_LIBDIR)/libapt-pkg.so.5.0\"\n" +
|
||||
"sgx.trusted_files.liblz4 = \"file:/usr/$(ARCH_LIBDIR)/liblz4.so.1\"\n" +
|
||||
"sgx.trusted_files.libsystemd = \"file:$(ARCH_LIBDIR)/libsystemd.so.0\"\n" +
|
||||
"sgx.trusted_files.libselinux = \"file:$(ARCH_LIBDIR)/libselinux.so.1\"\n" +
|
||||
"sgx.trusted_files.libgcrypt = \"file:$(ARCH_LIBDIR)/libgcrypt.so.20\"\n" +
|
||||
"sgx.trusted_files.libpcre = \"file:$(ARCH_LIBDIR)/libpcre.so.3\"\n" +
|
||||
"sgx.trusted_files.libgpgerror = \"file:$(ARCH_LIBDIR)/libgpg-error.so.0\"\n" +
|
||||
"sgx.trusted_files.libexpat = \"file:$(ARCH_LIBDIR)/libexpat.so.1\"\n" +
|
||||
"sgx.trusted_files.libz = \"file:$(ARCH_LIBDIR)/libz.so.1\"\n" +
|
||||
"sgx.trusted_files.libz2 = \"file:$(ARCH_LIBDIR)/libbz2.so.1.0\"\n" +
|
||||
"sgx.trusted_files.liblzma = \"file:$(ARCH_LIBDIR)/liblzma.so.5\"\n" +
|
||||
"sgx.trusted_files.libmpdec = \"file:/usr/$(ARCH_LIBDIR)/libmpdec.so.2\"\n" +
|
||||
"\n" +
|
||||
"# Name Service Switch (NSS) libraries (Glibc dependencies)\n" +
|
||||
"sgx.trusted_files.libnssfiles = \"file:$(ARCH_LIBDIR)/libnss_files.so.2\"\n" +
|
||||
"sgx.trusted_files.libnsscompat = \"file:$(ARCH_LIBDIR)/libnss_compat.so.2\"\n" +
|
||||
"sgx.trusted_files.libnssnis = \"file:$(ARCH_LIBDIR)/libnss_nis.so.2\"\n" +
|
||||
"sgx.trusted_files.libnsl = \"file:$(ARCH_LIBDIR)/libnsl.so.1\"\n" +
|
||||
"sgx.trusted_files.libnssmyhostname = \"file:$(ARCH_LIBDIR)/libnss_myhostname.so.2\"\n" +
|
||||
"sgx.trusted_files.libnssmdns = \"file:$(ARCH_LIBDIR)/libnss_mdns4_minimal.so.2\"\n" +
|
||||
"\n" +
|
||||
"# Scratch space\n" +
|
||||
"sgx.allowed_files.tmp = \"file:/tmp\"\n" +
|
||||
"\n" +
|
||||
"# APT config files\n" +
|
||||
"sgx.allowed_files.aptconfd = \"file:/etc/apt/apt.conf.d\"\n" +
|
||||
"sgx.allowed_files.aptconf = \"file:/etc/apt/apt.conf\"\n" +
|
||||
"sgx.allowed_files.apport = \"file:/etc/default/apport\"\n" +
|
||||
"\n" +
|
||||
"# Name Service Switch (NSS) files (Glibc reads these files)\n" +
|
||||
"sgx.allowed_files.nsswitch = \"file:/etc/nsswitch.conf\"\n" +
|
||||
"sgx.allowed_files.group = \"file:/etc/group\"\n" +
|
||||
"sgx.allowed_files.passwd = \"file:/etc/passwd\"\n" +
|
||||
"\n" +
|
||||
"# DNS hostname resolution files (Glibc reads these files)\n" +
|
||||
"sgx.allowed_files.hostconf = \"file:/etc/host.conf\"\n" +
|
||||
"sgx.allowed_files.hosts = \"file:/etc/hosts\"\n" +
|
||||
"sgx.allowed_files.gaiconf = \"file:/etc/gai.conf\"\n" +
|
||||
"sgx.allowed_files.resolv = \"file:/etc/resolv.conf\"\n" +
|
||||
"\n" +
|
||||
"sgx.allowed_files.openssl = \"file:/etc/ssl/openssl.cnf\"\n" +
|
||||
"\n" +
|
||||
"# System's file system table\n" +
|
||||
"sgx.allowed_files.fstab = \"file:/etc/fstab\"\n" +
|
||||
"\n" +
|
||||
"$(NODEJS_TRUSTED_LIBS)\n" +
|
||||
"\n" +
|
||||
"# JavaScript (trusted)\n" +
|
||||
"sgx.allowed_files.smlib = \"file:node_modules\"\n" +
|
||||
"sgx.trusted_files.npminfo = \"file:package.json\"\n" +
|
||||
"sgx.trusted_files.contract = \"file:contract.js\"\n" +
|
||||
"sgx.trusted_files.globaljson = \"file:global.json\"\n" +
|
||||
"sgx.trusted_files.argjson = \"file:arg.json\"\n" +
|
||||
"\n" +
|
||||
"sys.insecure__allow_eventfd = 1\n" +
|
||||
"\n" +
|
||||
"sgx.remote_attestation = 1\n" +
|
||||
"\n" +
|
||||
"loader.env.LD_PRELOAD = \"libsecret_prov_attest.so\"\n" +
|
||||
"loader.env.SECRET_PROVISION_CONSTRUCTOR = \"1\"\n" +
|
||||
"loader.env.SECRET_PROVISION_SET_PF_KEY = \"1\"\n" +
|
||||
"loader.env.SECRET_PROVISION_CA_CHAIN_PATH = \"certs/test-ca-sha256.crt\"\n" +
|
||||
"loader.env.SECRET_PROVISION_SERVERS = \"localhost:4433\"\n" +
|
||||
"\n" +
|
||||
"sgx.trusted_files.libsecretprovattest = \"file:libsecret_prov_attest.so\"\n" +
|
||||
"sgx.trusted_files.cachain = \"file:certs/test-ca-sha256.crt\"\n" +
|
||||
"\n" +
|
||||
"# Specify your SPID and linkable/unlinkable attestation policy\n" +
|
||||
"sgx.ra_client_spid = \"DF3A8BA098E93F66CC64E8A215E98333\"\n" +
|
||||
"sgx.ra_client_linkable = 0\n";
|
||||
private static String generateGrapheneManifestStr(Invocable engine, ContractRequest input)
|
||||
throws ScriptException, NoSuchMethodException {
|
||||
String manifestStr = "# Nodejs manifest file example\n" + "#\n"
|
||||
+ "# This manifest was prepared and tested on Ubuntu 18.04.\n" + "\n"
|
||||
+ "loader.argv0_override = \"nodejs\"\n" + "\n"
|
||||
+ "# LibOS layer library of Graphene. There is currently only one implementation,\n"
|
||||
+ "# so it is always set to libsysdb.so.\n"
|
||||
+ "loader.preload = \"file:$(GRAPHENEDIR)/Runtime/libsysdb.so\"\n" + "\n"
|
||||
+ "# Show/hide debug log of Graphene ('inline' or 'none' respectively).\n"
|
||||
+ "loader.debug_type = \"$(GRAPHENEDEBUG)\"\n" + "\n"
|
||||
+ "# Read application arguments directly from the command line. Don't use this on production!\n"
|
||||
+ "loader.insecure__use_cmdline_argv = 1\n" + "\n"
|
||||
+ "# Specify paths to search for libraries. The usual LD_LIBRARY_PATH syntax\n"
|
||||
+ "# applies. Paths must be in-Graphene visible paths, not host-OS paths (i.e.,\n"
|
||||
+ "# paths must be taken from fs.mount.xxx.path, not fs.mount.xxx.uri).\n"
|
||||
+ "loader.env.LD_LIBRARY_PATH = \"/lib:/usr/lib:$(ARCH_LIBDIR):/usr/$(ARCH_LIBDIR):./\"\n"
|
||||
+ "\n"
|
||||
+ "# Mount host-OS directory to required libraries (in 'uri') into in-Graphene\n"
|
||||
+ "# visible directory /lib (in 'path').\n" + "fs.mount.lib.type = \"chroot\"\n"
|
||||
+ "fs.mount.lib.path = \"/lib\"\n"
|
||||
+ "fs.mount.lib.uri = \"file:$(GRAPHENEDIR)/Runtime\"\n" + "\n"
|
||||
+ "fs.mount.lib2.type = \"chroot\"\n" + "fs.mount.lib2.path = \"$(ARCH_LIBDIR)\"\n"
|
||||
+ "fs.mount.lib2.uri = \"file:$(ARCH_LIBDIR)\"\n" + "\n"
|
||||
+ "#fs.mount.lib3.type = \"chroot\"\n"
|
||||
+ "#fs.mount.lib3.path = \"/usr/$(ARCH_LIBDIR)\"\n"
|
||||
+ "#fs.mount.lib3.uri = \"file:/usr/$(ARCH_LIBDIR)\"\n" + "\n"
|
||||
+ "fs.mount.usr.type = \"chroot\"\n" + "fs.mount.usr.path = \"/usr\"\n"
|
||||
+ "fs.mount.usr.uri = \"file:/usr\"\n" + "\n"
|
||||
+ "# Host-level directory to NSS files required by Glibc + NSS libs\n"
|
||||
+ "fs.mount.etc.type = \"chroot\"\n" + "fs.mount.etc.path = \"/etc\"\n"
|
||||
+ "fs.mount.etc.uri = \"file:/etc\"\n" + "\n"
|
||||
+ "# Workload needs to create temporary files\n"
|
||||
+ "fs.mount.tmp.type = \"chroot\"\n" + "fs.mount.tmp.path = \"/tmp\"\n"
|
||||
+ "fs.mount.tmp.uri = \"file:/tmp\"\n" + "\n"
|
||||
+ "# Set enclave size to 2GB; NodeJS expects around 1.7GB of heap on startup,\n"
|
||||
+ "# see e.g. https://github.com/nodejs/node/issues/13018.\n"
|
||||
+ "# Recall that SGX v1 requires to specify enclave size at enclave creation time.\n"
|
||||
+ "sgx.enclave_size = \"2G\"\n" + "\n"
|
||||
+ "# Set maximum number of in-enclave threads (somewhat arbitrarily) to 8. Recall\n"
|
||||
+ "# that SGX v1 requires to specify the maximum number of simultaneous threads at\n"
|
||||
+ "# enclave creation time.\n" + "sgx.thread_num = 16\n" + "\n"
|
||||
+ "# Specify all libraries used by Node.js and its dependencies (including all libs\n"
|
||||
+ "# which can be loaded at runtime via dlopen).\n"
|
||||
+ "sgx.trusted_files.ld = \"file:$(GRAPHENEDIR)/Runtime/ld-linux-x86-64.so.2\"\n"
|
||||
+ "sgx.trusted_files.libc = \"file:$(GRAPHENEDIR)/Runtime/libc.so.6\"\n"
|
||||
+ "sgx.trusted_files.libm = \"file:$(GRAPHENEDIR)/Runtime/libm.so.6\"\n"
|
||||
+ "sgx.trusted_files.libdl = \"file:$(GRAPHENEDIR)/Runtime/libdl.so.2\"\n"
|
||||
+ "sgx.trusted_files.librt = \"file:$(GRAPHENEDIR)/Runtime/librt.so.1\"\n"
|
||||
+ "sgx.trusted_files.libutil = \"file:$(GRAPHENEDIR)/Runtime/libutil.so.1\"\n"
|
||||
+ "sgx.trusted_files.libpthread = \"file:$(GRAPHENEDIR)/Runtime/libpthread.so.0\"\n"
|
||||
+ "sgx.trusted_files.libnssdns = \"file:$(GRAPHENEDIR)/Runtime/libnss_dns.so.2\"\n"
|
||||
+ "sgx.trusted_files.libresolv = \"file:$(GRAPHENEDIR)/Runtime/libresolv.so.2\"\n"
|
||||
+ "\n" + "sgx.trusted_files.libstdc = \"file:/usr/$(ARCH_LIBDIR)/libstdc++.so.6\"\n"
|
||||
+ "sgx.trusted_files.libgccs = \"file:$(ARCH_LIBDIR)/libgcc_s.so.1\"\n"
|
||||
+ "sgx.trusted_files.libaptpkg = \"file:/usr/$(ARCH_LIBDIR)/libapt-pkg.so.5.0\"\n"
|
||||
+ "sgx.trusted_files.liblz4 = \"file:/usr/$(ARCH_LIBDIR)/liblz4.so.1\"\n"
|
||||
+ "sgx.trusted_files.libsystemd = \"file:$(ARCH_LIBDIR)/libsystemd.so.0\"\n"
|
||||
+ "sgx.trusted_files.libselinux = \"file:$(ARCH_LIBDIR)/libselinux.so.1\"\n"
|
||||
+ "sgx.trusted_files.libgcrypt = \"file:$(ARCH_LIBDIR)/libgcrypt.so.20\"\n"
|
||||
+ "sgx.trusted_files.libpcre = \"file:$(ARCH_LIBDIR)/libpcre.so.3\"\n"
|
||||
+ "sgx.trusted_files.libgpgerror = \"file:$(ARCH_LIBDIR)/libgpg-error.so.0\"\n"
|
||||
+ "sgx.trusted_files.libexpat = \"file:$(ARCH_LIBDIR)/libexpat.so.1\"\n"
|
||||
+ "sgx.trusted_files.libz = \"file:$(ARCH_LIBDIR)/libz.so.1\"\n"
|
||||
+ "sgx.trusted_files.libz2 = \"file:$(ARCH_LIBDIR)/libbz2.so.1.0\"\n"
|
||||
+ "sgx.trusted_files.liblzma = \"file:$(ARCH_LIBDIR)/liblzma.so.5\"\n"
|
||||
+ "sgx.trusted_files.libmpdec = \"file:/usr/$(ARCH_LIBDIR)/libmpdec.so.2\"\n" + "\n"
|
||||
+ "# Name Service Switch (NSS) libraries (Glibc dependencies)\n"
|
||||
+ "sgx.trusted_files.libnssfiles = \"file:$(ARCH_LIBDIR)/libnss_files.so.2\"\n"
|
||||
+ "sgx.trusted_files.libnsscompat = \"file:$(ARCH_LIBDIR)/libnss_compat.so.2\"\n"
|
||||
+ "sgx.trusted_files.libnssnis = \"file:$(ARCH_LIBDIR)/libnss_nis.so.2\"\n"
|
||||
+ "sgx.trusted_files.libnsl = \"file:$(ARCH_LIBDIR)/libnsl.so.1\"\n"
|
||||
+ "sgx.trusted_files.libnssmyhostname = \"file:$(ARCH_LIBDIR)/libnss_myhostname.so.2\"\n"
|
||||
+ "sgx.trusted_files.libnssmdns = \"file:$(ARCH_LIBDIR)/libnss_mdns4_minimal.so.2\"\n"
|
||||
+ "\n" + "# Scratch space\n" + "sgx.allowed_files.tmp = \"file:/tmp\"\n" + "\n"
|
||||
+ "# APT config files\n"
|
||||
+ "sgx.allowed_files.aptconfd = \"file:/etc/apt/apt.conf.d\"\n"
|
||||
+ "sgx.allowed_files.aptconf = \"file:/etc/apt/apt.conf\"\n"
|
||||
+ "sgx.allowed_files.apport = \"file:/etc/default/apport\"\n" + "\n"
|
||||
+ "# Name Service Switch (NSS) files (Glibc reads these files)\n"
|
||||
+ "sgx.allowed_files.nsswitch = \"file:/etc/nsswitch.conf\"\n"
|
||||
+ "sgx.allowed_files.group = \"file:/etc/group\"\n"
|
||||
+ "sgx.allowed_files.passwd = \"file:/etc/passwd\"\n" + "\n"
|
||||
+ "# DNS hostname resolution files (Glibc reads these files)\n"
|
||||
+ "sgx.allowed_files.hostconf = \"file:/etc/host.conf\"\n"
|
||||
+ "sgx.allowed_files.hosts = \"file:/etc/hosts\"\n"
|
||||
+ "sgx.allowed_files.gaiconf = \"file:/etc/gai.conf\"\n"
|
||||
+ "sgx.allowed_files.resolv = \"file:/etc/resolv.conf\"\n" + "\n"
|
||||
+ "sgx.allowed_files.openssl = \"file:/etc/ssl/openssl.cnf\"\n" + "\n"
|
||||
+ "# System's file system table\n"
|
||||
+ "sgx.allowed_files.fstab = \"file:/etc/fstab\"\n" + "\n"
|
||||
+ "$(NODEJS_TRUSTED_LIBS)\n" + "\n" + "# JavaScript (trusted)\n"
|
||||
+ "sgx.allowed_files.smlib = \"file:node_modules\"\n"
|
||||
+ "sgx.trusted_files.npminfo = \"file:package.json\"\n"
|
||||
+ "sgx.trusted_files.contract = \"file:contract.js\"\n"
|
||||
+ "sgx.trusted_files.globaljson = \"file:global.json\"\n"
|
||||
+ "sgx.trusted_files.argjson = \"file:arg.json\"\n" + "\n"
|
||||
+ "sys.insecure__allow_eventfd = 1\n" + "\n" + "sgx.remote_attestation = 1\n" + "\n"
|
||||
+ "loader.env.LD_PRELOAD = \"libsecret_prov_attest.so\"\n"
|
||||
+ "loader.env.SECRET_PROVISION_CONSTRUCTOR = \"1\"\n"
|
||||
+ "loader.env.SECRET_PROVISION_SET_PF_KEY = \"1\"\n"
|
||||
+ "loader.env.SECRET_PROVISION_CA_CHAIN_PATH = \"certs/test-ca-sha256.crt\"\n"
|
||||
+ "loader.env.SECRET_PROVISION_SERVERS = \"localhost:4433\"\n" + "\n"
|
||||
+ "sgx.trusted_files.libsecretprovattest = \"file:libsecret_prov_attest.so\"\n"
|
||||
+ "sgx.trusted_files.cachain = \"file:certs/test-ca-sha256.crt\"\n" + "\n"
|
||||
+ "# Specify your SPID and linkable/unlinkable attestation policy\n"
|
||||
+ "sgx.ra_client_spid = \"DF3A8BA098E93F66CC64E8A215E98333\"\n"
|
||||
+ "sgx.ra_client_linkable = 0\n";
|
||||
// add secret servers
|
||||
manifestStr += "loader.env.SECRET_PROVISION_CC_SERVERS = ";
|
||||
Object resultStr = engine.invokeFunction(
|
||||
"getAllSecret",
|
||||
"",
|
||||
input.getRequester(),
|
||||
Object resultStr = engine.invokeFunction("getAllSecret", "", input.getRequester(),
|
||||
input.getRequesterDOI());
|
||||
Map<String, String> resultMap = JsonUtil.fromJson(resultStr.toString(), MapType);
|
||||
Map<String, String> serverMap = JsonUtil.fromJson(resultMap.get("result"), MapType);
|
||||
|
@ -1,6 +1,5 @@
|
||||
package org.bdware.sc.engine;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
@ -18,6 +17,7 @@ import org.bdware.sc.bean.ContractRequest;
|
||||
import org.bdware.sc.bean.ProjectConfig;
|
||||
import org.bdware.sc.boundry.JavaScriptEntry;
|
||||
import org.bdware.sc.boundry.Resources;
|
||||
import org.bdware.sc.boundry.ScriptReturnException;
|
||||
import org.bdware.sc.boundry.utils.UtilRegistry;
|
||||
import org.bdware.sc.encrypt.HardwareInfo;
|
||||
import org.bdware.sc.encrypt.HardwareInfo.OSType;
|
||||
@ -31,6 +31,7 @@ import org.bdware.sc.util.HashUtil;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
import org.objectweb.asm.ClassReader;
|
||||
import org.objectweb.asm.tree.MethodNode;
|
||||
import wrp.jdk.nashorn.api.scripting.NashornException;
|
||||
import wrp.jdk.nashorn.api.scripting.NashornScriptEngine;
|
||||
import wrp.jdk.nashorn.api.scripting.NashornScriptEngineFactory;
|
||||
import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror;
|
||||
@ -39,6 +40,7 @@ import wrp.jdk.nashorn.internal.runtime.*;
|
||||
|
||||
import javax.script.*;
|
||||
import java.io.*;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
@ -64,6 +66,7 @@ public class DesktopEngine extends JSEngine {
|
||||
// private String traceDir;
|
||||
private ContractProcess.Logger tracePS = null;
|
||||
private Contract contract;
|
||||
private ContractManifest manifest;
|
||||
|
||||
public DesktopEngine() {
|
||||
startEngine();
|
||||
@ -82,7 +85,7 @@ public class DesktopEngine extends JSEngine {
|
||||
startEngine();
|
||||
}
|
||||
|
||||
public static void applyWithGlobal(ScriptFunction script, Global global, Object... obj) {
|
||||
public static Object applyWithGlobal(ScriptFunction script, Global global, Object... obj) {
|
||||
Global oldGlobal = Context.getGlobal();
|
||||
boolean globalChanged = (oldGlobal != global);
|
||||
try {
|
||||
@ -91,8 +94,10 @@ public class DesktopEngine extends JSEngine {
|
||||
}
|
||||
// System.out.println("[DesktopEngine]" + script.getName() + " -->\n" +
|
||||
// script.safeToString());
|
||||
ScriptRuntime.apply(script, global, obj);
|
||||
|
||||
Object ret = ScriptRuntime.apply(script, global, obj);
|
||||
return ret;
|
||||
} catch (NashornException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
@ -100,6 +105,7 @@ public class DesktopEngine extends JSEngine {
|
||||
Context.setGlobal(oldGlobal);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setRecovering(boolean b) {
|
||||
@ -132,11 +138,7 @@ public class DesktopEngine extends JSEngine {
|
||||
ccl = (ccl == null) ? NashornScriptEngineFactory.class.getClassLoader() : ccl;
|
||||
String[] args = new String[] {"--loader-per-compile=false", "-strict=false"};
|
||||
classLoader = new YJSClassLoader(ccl, new YJSFilter());
|
||||
engine =
|
||||
(NashornScriptEngine)
|
||||
new NashornScriptEngineFactory()
|
||||
.getScriptEngine(
|
||||
args, // "--print-ast",
|
||||
engine = (NashornScriptEngine) new NashornScriptEngineFactory().getScriptEngine(args, // "--print-ast",
|
||||
// "true",
|
||||
// "-d=/Users/huaqiancai/Downloads/dumpedClz",
|
||||
// "--trace-callsites=enterexit"
|
||||
@ -150,17 +152,12 @@ public class DesktopEngine extends JSEngine {
|
||||
// engine = (NashornScriptEngine) new
|
||||
// NashornScriptEngineFactory().getScriptEngine(new YJSFilter());
|
||||
if (_with_init_script) {
|
||||
InputStream in =
|
||||
DesktopEngine.class
|
||||
.getClassLoader()
|
||||
InputStream in = DesktopEngine.class.getClassLoader()
|
||||
.getResourceAsStream("org/bdware/sc/engine/yancloud_desktop.js");
|
||||
assert in != null;
|
||||
InputStreamReader streamReader = new InputStreamReader(in);
|
||||
engine.getContext()
|
||||
.setAttribute(
|
||||
ScriptEngine.FILENAME,
|
||||
"org/bdware/sc/engine/yancloud_desktop.js",
|
||||
ScriptContext.ENGINE_SCOPE);
|
||||
engine.getContext().setAttribute(ScriptEngine.FILENAME,
|
||||
"org/bdware/sc/engine/yancloud_desktop.js", ScriptContext.ENGINE_SCOPE);
|
||||
engine.eval(streamReader);
|
||||
}
|
||||
global = engine.getNashornGlobal();
|
||||
@ -188,10 +185,7 @@ public class DesktopEngine extends JSEngine {
|
||||
yancloud_desktop.append(UtilRegistry.getInitStr(str, false));
|
||||
}
|
||||
// LOGGER.debug("[initScript] " + yancloud_desktop);
|
||||
engine.getContext()
|
||||
.setAttribute(
|
||||
ScriptEngine.FILENAME,
|
||||
yancloud_desktop.toString(),
|
||||
engine.getContext().setAttribute(ScriptEngine.FILENAME, yancloud_desktop.toString(),
|
||||
ScriptContext.ENGINE_SCOPE);
|
||||
engine.eval(yancloud_desktop.toString());
|
||||
} catch (ScriptException e) {
|
||||
@ -245,27 +239,27 @@ public class DesktopEngine extends JSEngine {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ContractResult loadContract(
|
||||
Contract contract, ContractNode contractNode, boolean isInsnLimit) {
|
||||
LOGGER.info("loadContract isInsnLimit:" + isInsnLimit);
|
||||
public ContractResult loadContract(Contract contract, ContractNode contractNode,
|
||||
boolean isInsnLimit) {
|
||||
cn = contractNode;
|
||||
engine.getContext()
|
||||
.setAttribute(ScriptEngine.FILENAME, ScriptFileName, ScriptContext.ENGINE_SCOPE);
|
||||
engine.getContext().setAttribute(ScriptEngine.FILENAME, ScriptFileName,
|
||||
ScriptContext.ENGINE_SCOPE);
|
||||
try {
|
||||
setPermission(cn.getPermission());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
for (SharableNode sharable : contractNode.getSharables()) {
|
||||
for (String variableStatement : sharable.getVariableStatements()) {
|
||||
compileSharable(sharable, variableStatement);
|
||||
}
|
||||
}
|
||||
for (FunctionNode fun : contractNode.getFunctions())
|
||||
try {
|
||||
String str = fun.plainText();
|
||||
engine.getContext()
|
||||
.setAttribute(
|
||||
ScriptEngine.FILENAME,
|
||||
fun.getFileName(),
|
||||
engine.getContext().setAttribute(ScriptEngine.FILENAME, fun.getFileName(),
|
||||
ScriptContext.ENGINE_SCOPE);
|
||||
compileFunction(str, isInsnLimit);
|
||||
compileFunction(fun, str, isInsnLimit);
|
||||
} catch (ScriptException e) {
|
||||
return wrapperException(e, fun);
|
||||
} catch (Exception e) {
|
||||
@ -274,7 +268,8 @@ public class DesktopEngine extends JSEngine {
|
||||
e.printStackTrace();
|
||||
return new ContractResult(Status.Error, new JsonPrimitive(bo.toString()));
|
||||
}
|
||||
LOGGER.debug(JsonUtil.toJson(contractNode.events) + "\n\t" + JsonUtil.toJson(contractNode.logs));
|
||||
LOGGER.debug(
|
||||
JsonUtil.toJson(contractNode.events) + "\n\t" + JsonUtil.toJson(contractNode.logs));
|
||||
for (String topic : contractNode.events.keySet()) {
|
||||
compileEventFunction(topic, topic, contractNode.events.get(topic));
|
||||
}
|
||||
@ -293,33 +288,42 @@ public class DesktopEngine extends JSEngine {
|
||||
}
|
||||
ScriptObjectMirror globalVars = (ScriptObjectMirror) engine.get("Global");
|
||||
ConfidentialContractUtil.generateConfidentialContract(cn, globalVars, global);
|
||||
Context.setGlobal(global);
|
||||
|
||||
ContractResult cResult =
|
||||
new ContractResult(Status.Success, new JsonPrimitive(contract.getPublicKey()));
|
||||
cResult.isInsnLimit = isInsnLimit;
|
||||
return cResult;
|
||||
}
|
||||
|
||||
private void compileSharable(SharableNode sharable, String variableStatement) {
|
||||
try {
|
||||
engine.getContext().setAttribute(ScriptEngine.FILENAME, sharable.getFileName(),
|
||||
ScriptContext.ENGINE_SCOPE);
|
||||
engine.eval("var " + variableStatement);
|
||||
LOGGER.info("load sharable: " + variableStatement);
|
||||
} catch (ScriptException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void compileEventFunction(String name, String topic, REventSemantics semantics) {
|
||||
try {
|
||||
String str;
|
||||
if (REventSemantics.AT_LEAST_ONCE.equals(semantics)) {
|
||||
str =
|
||||
String.format(
|
||||
"function %s(arg) { YancloudUtil.pubEvent(\"%s\", arg); }",
|
||||
str = String.format("function %s(arg) { YancloudUtil.pubEvent(\"%s\", arg); }",
|
||||
name, topic);
|
||||
} else {
|
||||
str =
|
||||
String.format(
|
||||
str = String.format(
|
||||
"function %s(arg) { YancloudUtil.pubEventConstraint(\"%s\", arg, \"%s\"); }",
|
||||
name, topic, semantics.name());
|
||||
}
|
||||
compileFunction(str, false);
|
||||
compileFunction(null, str, false);
|
||||
LOGGER.debug("compile function " + name + " success!");
|
||||
str =
|
||||
String.format(
|
||||
str = String.format(
|
||||
"function %ss(arg0, arg1) { YancloudUtil.pubEventConstraint(\"%s\", arg0, arg1); }",
|
||||
name, topic);
|
||||
compileFunction(str, false);
|
||||
compileFunction(null, str, false);
|
||||
LOGGER.debug("compile function " + name + "s success!");
|
||||
} catch (ScriptException e) {
|
||||
e.printStackTrace();
|
||||
@ -398,7 +402,8 @@ public class DesktopEngine extends JSEngine {
|
||||
//
|
||||
// return new ContractResult(Status.Success, new JsonPrimitive(""));
|
||||
// }
|
||||
private void compileFunction(ScriptObjectMirror sf, boolean instrumentBranch) {
|
||||
private void compileFunction(FunctionNode functionNode, ScriptObjectMirror sf,
|
||||
boolean instrumentBranch) {
|
||||
Global oldGlobal = Context.getGlobal();
|
||||
boolean globalChanged = (oldGlobal != global);
|
||||
try {
|
||||
@ -410,21 +415,54 @@ public class DesktopEngine extends JSEngine {
|
||||
Context.TRACEMETHOD = true;
|
||||
}
|
||||
sf.compileScriptFunction();
|
||||
ScriptFunction scriptFunction = (ScriptFunction) sf.getScriptObject();
|
||||
Field f = ScriptFunction.class.getDeclaredField("data");
|
||||
f.setAccessible(true);
|
||||
ScriptFunctionData scriptFunctioNData = (ScriptFunctionData) f.get(scriptFunction);
|
||||
Object scope = scriptFunction.getScope();
|
||||
Method getGeneric = ScriptFunctionData.class.getDeclaredMethod("getGenericInvoker",
|
||||
ScriptObject.class);
|
||||
getGeneric.setAccessible(true);
|
||||
MethodHandle methodHandle = (MethodHandle) getGeneric.invoke(scriptFunctioNData, scope);
|
||||
if (methodHandle.getClass() != Class.forName("java.lang.invoke.DirectMethodHandle")) {
|
||||
Field argL0 = methodHandle.getClass().getDeclaredField("argL0");
|
||||
argL0.setAccessible(true);
|
||||
methodHandle = (MethodHandle) argL0.get(methodHandle);
|
||||
|
||||
}
|
||||
Field memberNameField = methodHandle.getClass().getDeclaredField("member");
|
||||
|
||||
memberNameField.setAccessible(true);
|
||||
Object memberName = memberNameField.get(methodHandle);
|
||||
Field clazz = memberName.getClass().getDeclaredField("clazz");
|
||||
clazz.setAccessible(true);
|
||||
Class clazz2 = (Class) clazz.get(memberName);
|
||||
if (functionNode != null)
|
||||
functionNode.compiledClazz = clazz2;
|
||||
// functionNode==null --> event functions
|
||||
} catch (Exception e) {
|
||||
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (globalChanged) Context.setGlobal(oldGlobal);
|
||||
if (globalChanged)
|
||||
Context.setGlobal(oldGlobal);
|
||||
Context.TRACEIF = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void compileFunction(String snippet, boolean instrumentBranch) throws ScriptException {
|
||||
compileFunction((ScriptObjectMirror) engine.eval(snippet), instrumentBranch);
|
||||
public void compileFunction(FunctionNode functionNode, String snippet, boolean instrumentBranch)
|
||||
throws ScriptException {
|
||||
compileFunction(functionNode, (ScriptObjectMirror) engine.eval(snippet), instrumentBranch);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public synchronized ContractResult executeContract(ContractRequest input) {
|
||||
public ContractResult executeContract(ContractRequest input) {
|
||||
Global oldGlobal = Context.getGlobal();
|
||||
boolean globalChanged = (oldGlobal != global);
|
||||
if (globalChanged) {
|
||||
Context.setGlobal(global);
|
||||
}
|
||||
ContractProcess.Logger previous = this.getTracePS();
|
||||
ByteArrayOutputStream bo = null;
|
||||
if (syncUtil.startFlag && syncUtil.currType == SyncType.Trace && !recovering) {
|
||||
@ -443,8 +481,7 @@ public class DesktopEngine extends JSEngine {
|
||||
JavaScriptEntry.msgList = new ArrayList<>();
|
||||
FunctionNode fun = cn.getFunction(input.getAction());
|
||||
if (fun == null) {
|
||||
return new ContractResult(
|
||||
Status.Exception,
|
||||
return new ContractResult(Status.Exception,
|
||||
new JsonPrimitive("Action " + input.getAction() + " is not exists"));
|
||||
}
|
||||
ProgramPointCounter ppc = null;
|
||||
@ -461,49 +498,27 @@ public class DesktopEngine extends JSEngine {
|
||||
HashMap<String, Long> ppCountMap = evaluatesAnalysis(input.getAction(), functions);
|
||||
Long extraGas = getExtraGas(fun.getCost().getExtraGas(), input);
|
||||
bo = new ByteArrayOutputStream();
|
||||
ppc =
|
||||
new ProgramPointCounter(
|
||||
bo,
|
||||
previous.getCp(),
|
||||
Long.MAX_VALUE,
|
||||
functionIndex,
|
||||
input.getGasLimit(),
|
||||
extraGas,
|
||||
input.getAction(),
|
||||
ppCountMap);
|
||||
ppc = new ProgramPointCounter(bo, previous.getCp(), Long.MAX_VALUE, functionIndex,
|
||||
input.getGasLimit(), extraGas, input.getAction(), ppCountMap);
|
||||
this.redirectTracePS(ppc);
|
||||
}
|
||||
Invocable cal = engine;
|
||||
if (fun.isExport() ||
|
||||
// if the function has been registered as event handler
|
||||
(fun.isHandler() &&
|
||||
null != input.getRequester() &&
|
||||
input.getRequester().startsWith("event"))) {
|
||||
Object ret = null;
|
||||
// long start = System.currentTimeMillis();
|
||||
(fun.isHandler() && null != input.getRequester()
|
||||
&& input.getRequester().startsWith("event"))) {
|
||||
Object ret;
|
||||
if (fun.isView()) {
|
||||
ret = executeWithoutLock(fun, input, null);
|
||||
} else {
|
||||
synchronized (this) {
|
||||
ret = executeWithoutLock(fun, input, null);
|
||||
|
||||
for (AnnotationHook handler : fun.beforeExecutionAnnotations()) {
|
||||
ret = handler.handle(input, this, ret);
|
||||
}
|
||||
// actually invoke!
|
||||
if (ret == null) {
|
||||
ret =
|
||||
cal.invokeFunction(
|
||||
input.getAction(),
|
||||
(fun.isHandler()
|
||||
? JsonUtil.fromJson(input.getArg(), Event.class)
|
||||
: JSONTool.convertJsonElementToMirror(input.getArg())),
|
||||
input.getRequester(),
|
||||
input.getRequesterDOI());
|
||||
}
|
||||
for (AnnotationHook handler : fun.afterExecutionAnnotations()) {
|
||||
//Mask在after裏面
|
||||
//System.out.println("afterHook"+contract.Mask);
|
||||
|
||||
ret = handler.handle(input, this, ret);
|
||||
}
|
||||
//System.out.println("[DesktopEngine MaskConfig]"+ContractProcess.instance.getProjectConfig().getMaskConfig().config.toString());
|
||||
ContractResult contractRes = new ContractResult(Status.Success, (JsonElement) ret);
|
||||
// System.out.println("[DesktopEngine
|
||||
// MaskConfig]"+ContractProcess.instance.getProjectConfig().getMaskConfig().config.toString());
|
||||
ContractResult contractRes =
|
||||
new ContractResult(Status.Success, JSONTool.convertMirrorToJson(ret));
|
||||
if (ppc != null) {
|
||||
contractRes.extraGas = ppc.extraGas;
|
||||
contractRes.executionGas = ppc.cost;
|
||||
@ -519,7 +534,7 @@ public class DesktopEngine extends JSEngine {
|
||||
|
||||
List<REvent> msgList = JavaScriptEntry.msgList;
|
||||
JavaScriptEntry.msgList = null;
|
||||
if (!msgList.isEmpty()) {
|
||||
if (msgList != null && !msgList.isEmpty()) {
|
||||
contractRes.events = msgList;
|
||||
contractRes.eventRelated = true;
|
||||
}
|
||||
@ -530,10 +545,10 @@ public class DesktopEngine extends JSEngine {
|
||||
if (syncUtil.startFlag && !recovering) {
|
||||
switch (syncUtil.currType) {
|
||||
case Trace:
|
||||
syncUtil.traceRecordUtil.eachFinish();
|
||||
// syncUtil.traceRecordUtil.eachFinish();
|
||||
break;
|
||||
case Trans:
|
||||
syncUtil.transRecordUtil.eachFinish();
|
||||
// syncUtil.transRecordUtil.eachFinish();
|
||||
break;
|
||||
case Memory:
|
||||
default:
|
||||
@ -545,35 +560,62 @@ public class DesktopEngine extends JSEngine {
|
||||
} else {
|
||||
// return new ContractResult(Status.Exception, "Action " + input.getAction() + "
|
||||
// is not exported!");
|
||||
return new ContractResult(
|
||||
Status.Exception,
|
||||
return new ContractResult(Status.Exception,
|
||||
new JsonPrimitive("Action " + input.getAction() + " is not exported!"));
|
||||
}
|
||||
|
||||
} catch (ScriptReturnException e) {
|
||||
e.printStackTrace();
|
||||
return new ContractResult(Status.Exception, e.message);
|
||||
} catch (ScriptException e) {
|
||||
Throwable cause = e.getCause();
|
||||
e.printStackTrace();
|
||||
return new ContractResult(
|
||||
Status.Exception,
|
||||
new JsonPrimitive(extractException(e.getMessage(), extract(cn, cause))));
|
||||
return wrapperException(e, fun);
|
||||
} catch (Throwable e) {
|
||||
ByteArrayOutputStream bo1 = new ByteArrayOutputStream();
|
||||
PrintStream ps = new PrintStream(bo1);
|
||||
e.printStackTrace(ps);
|
||||
e.printStackTrace();
|
||||
if (e.getCause() != null && e.getCause() instanceof ScriptException) {
|
||||
return new ContractResult(
|
||||
Status.Exception,
|
||||
new JsonPrimitive(
|
||||
return new ContractResult(Status.Exception, new JsonPrimitive(
|
||||
extractException(bo1.toString(), extract(cn, e.getCause()))));
|
||||
} else {
|
||||
return new ContractResult(
|
||||
Status.Exception,
|
||||
return new ContractResult(Status.Exception,
|
||||
new JsonPrimitive(extractException(bo1.toString(), extract(cn, e))));
|
||||
}
|
||||
} finally {
|
||||
this.redirectTracePS(previous);
|
||||
if (globalChanged)
|
||||
Context.setGlobal(oldGlobal);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Object executeWithoutLock(FunctionNode fun, ContractRequest input, Object injectedArg)
|
||||
throws ScriptException, NoSuchMethodException {
|
||||
// long start = System.currentTimeMillis();
|
||||
ArgPacks argPacks = new ArgPacks(input, input.getArg(), null);
|
||||
if (injectedArg != null) {
|
||||
argPacks.arg = injectedArg;
|
||||
}
|
||||
for (AnnotationHook handler : fun.beforeExecutionAnnotations()) {
|
||||
argPacks = handler.handle(this, argPacks);
|
||||
}
|
||||
|
||||
// actually invoke!
|
||||
if (argPacks.ret == null) {
|
||||
argPacks.ret = engine.invokeFunction(input.getAction(),
|
||||
(fun.isHandler() ? JsonUtil.fromJson(input.getArg(), Event.class)
|
||||
: JSONTool.convertJsonElementToMirror(argPacks.arg)),
|
||||
input.getRequester(), input.getRequesterDOI());
|
||||
}
|
||||
|
||||
for (AnnotationHook handler : fun.afterExecutionAnnotations()) {
|
||||
// Mask在after裏面
|
||||
// System.out.println("afterHook"+contract.Mask);
|
||||
argPacks = handler.handle(this, argPacks);
|
||||
}
|
||||
return argPacks.ret;
|
||||
}
|
||||
|
||||
private String extractException(String msg, List<String> stack) {
|
||||
@ -603,8 +645,7 @@ public class DesktopEngine extends JSEngine {
|
||||
for (String function : functions) {
|
||||
MethodNode mn = methods.get(function);
|
||||
if (mn != null) {
|
||||
CFGraph cfg =
|
||||
new CFGraph(mn) {
|
||||
CFGraph cfg = new CFGraph(mn) {
|
||||
@Override
|
||||
public BasicBlock getBasicBlock(int id) {
|
||||
return new BasicBlock(id);
|
||||
@ -628,15 +669,15 @@ public class DesktopEngine extends JSEngine {
|
||||
flag++;
|
||||
}
|
||||
}
|
||||
/* for (Map.Entry<String, Long> map : PPCount.functionSumGas.entrySet()) {
|
||||
if (map.getKey().contains(getFunction) && map.getKey().contains("true")) {
|
||||
System.out.println("[合约方法pub中条件循环为true时:]" + map.getValue());
|
||||
} else if (map.getKey().contains(getFunction) && map.getKey().contains("false")) {
|
||||
System.out.println("[合约方法pub中条件循环为false时:]" + map.getValue());
|
||||
} else if (map.getKey().contains(getFunction)) {
|
||||
System.out.println("[合约方法pub中其他语句消耗:]" + map.getValue());
|
||||
}
|
||||
}*/
|
||||
/*
|
||||
* for (Map.Entry<String, Long> map : PPCount.functionSumGas.entrySet()) { if
|
||||
* (map.getKey().contains(getFunction) && map.getKey().contains("true")) {
|
||||
* System.out.println("[合约方法pub中条件循环为true时:]" + map.getValue()); } else if
|
||||
* (map.getKey().contains(getFunction) && map.getKey().contains("false")) {
|
||||
* System.out.println("[合约方法pub中条件循环为false时:]" + map.getValue()); } else if
|
||||
* (map.getKey().contains(getFunction)) { System.out.println("[合约方法pub中其他语句消耗:]" +
|
||||
* map.getValue()); } }
|
||||
*/
|
||||
return ppCountMap;
|
||||
}
|
||||
|
||||
@ -646,11 +687,7 @@ public class DesktopEngine extends JSEngine {
|
||||
return 0L;
|
||||
}
|
||||
Invocable cal = engine;
|
||||
Object ret =
|
||||
cal.invokeFunction(
|
||||
costFunction,
|
||||
input.getArg(),
|
||||
input.getRequester(),
|
||||
Object ret = cal.invokeFunction(costFunction, input.getArg(), input.getRequester(),
|
||||
input.getRequesterDOI());
|
||||
if (ret != null && StringUtils.isNumeric(ret.toString())) {
|
||||
return Long.parseLong(ret.toString());
|
||||
@ -707,15 +744,16 @@ public class DesktopEngine extends JSEngine {
|
||||
if (actLine != -1) {
|
||||
actLine += line - 1;
|
||||
}
|
||||
message =
|
||||
message.replaceAll(
|
||||
"at line number " + e.getLineNumber(), "at line number " + (actLine));
|
||||
message = message.replaceAll("at line number " + e.getLineNumber(),
|
||||
"at line number " + (actLine));
|
||||
if (fun.getFileName() != null)
|
||||
message = message.replace("in contract_main.yjs", "in " + fun.getFileName());
|
||||
content.append(message);
|
||||
content.append("(");
|
||||
if (fun.functionName != null) content.append(fun.functionName);
|
||||
else content.append("contract_main.yjs");
|
||||
if (fun.functionName != null)
|
||||
content.append(fun.functionName);
|
||||
else
|
||||
content.append("contract_main.yjs");
|
||||
content.append(":");
|
||||
|
||||
content.append(actLine);
|
||||
@ -734,16 +772,14 @@ public class DesktopEngine extends JSEngine {
|
||||
String methodName = element.getMethodName();
|
||||
String fileName = element.getFileName();
|
||||
|
||||
if (fileName.endsWith(".java")) continue;
|
||||
if (fileName.endsWith(".java"))
|
||||
continue;
|
||||
if (c.isBundle()) {
|
||||
fileName = fixFile(c, methodName);
|
||||
}
|
||||
if (fileName.equals("--")) continue;
|
||||
ret.add(
|
||||
String.format(
|
||||
"at %s(%s:%d)",
|
||||
methodName,
|
||||
fileName,
|
||||
if (fileName.equals("--"))
|
||||
continue;
|
||||
ret.add(String.format("at %s(%s:%d)", methodName, fileName,
|
||||
(fixLine(c, methodName) + element.getLineNumber())));
|
||||
}
|
||||
return ret;
|
||||
@ -856,20 +892,17 @@ public class DesktopEngine extends JSEngine {
|
||||
try {
|
||||
assert null != loader;
|
||||
if (entry.getName().endsWith(".jar")) {
|
||||
System.out.println("[DesktopEngine] loadJar:" + entry.getName());
|
||||
System.out.println("[DesktopEngine] classLoader:" + getClassLoad());
|
||||
|
||||
loader.loadJar(zf.getInputStream(entry), entry.getName().replaceAll(".*/", ""));
|
||||
}
|
||||
if (entry.getName().endsWith(".so") || entry.getName().endsWith(".so.1")) {
|
||||
System.out.println("unzip library:" + entry.getName().replaceAll(".*/", ""));
|
||||
loader.unzipLibrary(
|
||||
zf.getInputStream(entry), entry.getName().replaceAll(".*/", ""));
|
||||
loader.unzipLibrary(zf.getInputStream(entry),
|
||||
entry.getName().replaceAll(".*/", ""));
|
||||
}
|
||||
if (HardwareInfo.type == OSType.mac && entry.getName().endsWith(".dylib")) {
|
||||
System.out.println("unzip library:" + entry.getName().replaceAll(".*/", ""));
|
||||
loader.unzipLibrary(
|
||||
zf.getInputStream(entry), entry.getName().replaceAll(".*/", ""));
|
||||
loader.unzipLibrary(zf.getInputStream(entry),
|
||||
entry.getName().replaceAll(".*/", ""));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@ -895,8 +928,12 @@ public class DesktopEngine extends JSEngine {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void initManifest(ContractManifest manifest) {
|
||||
// TODO Auto-generated method stub
|
||||
public void setManifest(ContractManifest manifest) {
|
||||
this.manifest = manifest;
|
||||
}
|
||||
|
||||
public ContractManifest getManifest() {
|
||||
return this.manifest;
|
||||
}
|
||||
|
||||
public ContractProcess.Logger getTracePS() {
|
||||
|
@ -2,10 +2,13 @@ package org.bdware.sc.engine;
|
||||
|
||||
import com.google.gson.*;
|
||||
import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror;
|
||||
import wrp.jdk.nashorn.internal.objects.NativeArray;
|
||||
import wrp.jdk.nashorn.internal.runtime.PropertyMap;
|
||||
import wrp.jdk.nashorn.internal.scripts.JO;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class JSONTool {
|
||||
@ -29,22 +32,65 @@ public class JSONTool {
|
||||
}
|
||||
return jo;
|
||||
} else if (jsonElement.isJsonArray()) {
|
||||
JO jo = new JO(PropertyMap.newMap());
|
||||
jo.setIsArray();
|
||||
NativeArray narray = new NativeArray();
|
||||
JsonArray jsonArray = jsonElement.getAsJsonArray();
|
||||
for (int i = 0; i < jsonArray.size(); i++)
|
||||
jo.put(i, convertJsonElementToMirror(jsonArray.get(i)), false);
|
||||
return jo;
|
||||
NativeArray.push(narray, convertJsonElementToMirror(jsonArray.get(i)));
|
||||
return narray;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Object convertJsonElementToMirror(Object input) {
|
||||
if (input instanceof JsonElement) {
|
||||
return convertJsonElementToMirror((JsonElement) input);
|
||||
} else {
|
||||
return input;
|
||||
}
|
||||
}
|
||||
|
||||
private static JsonElement convertMirrorToJsonInternal(Object obj, Set<Object> recorded) {
|
||||
if (recorded.contains(obj)) return JsonNull.INSTANCE;
|
||||
if (obj == null) return JsonNull.INSTANCE;
|
||||
if (recorded.contains(obj))
|
||||
return JsonNull.INSTANCE;
|
||||
if (obj == null)
|
||||
return JsonNull.INSTANCE;
|
||||
if (obj.getClass() == wrp.jdk.nashorn.internal.runtime.Undefined.class)
|
||||
return JsonNull.INSTANCE;
|
||||
if (obj.getClass() == ScriptObjectMirror.class) {
|
||||
if (obj instanceof JsonElement)
|
||||
return (JsonElement) obj;
|
||||
if (obj.getClass().isArray()) {
|
||||
Object[] arr = (Object[]) obj;
|
||||
recorded.add(obj);
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
jsonArray.add(convertMirrorToJsonInternal(arr[i], recorded));
|
||||
}
|
||||
return jsonArray;
|
||||
} else if (List.class.isAssignableFrom(obj.getClass())) {
|
||||
List arr = (List) obj;
|
||||
recorded.add(arr);
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
for (int i = 0; i < arr.size(); i++) {
|
||||
jsonArray.add(convertMirrorToJsonInternal(arr.get(i), recorded));
|
||||
}
|
||||
return jsonArray;
|
||||
} else if (List.class.isAssignableFrom(obj.getClass())) {
|
||||
List arr = (List) obj;
|
||||
recorded.add(arr);
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
for (int i = 0; i < arr.size(); i++) {
|
||||
jsonArray.add(convertMirrorToJsonInternal(arr.get(i), recorded));
|
||||
}
|
||||
return jsonArray;
|
||||
} else if (Set.class.isAssignableFrom(obj.getClass())) {
|
||||
Set arr = (Set) obj;
|
||||
recorded.add(arr);
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
for (Object k : arr) {
|
||||
jsonArray.add(convertMirrorToJsonInternal(k, recorded));
|
||||
}
|
||||
return jsonArray;
|
||||
} else if (obj.getClass() == ScriptObjectMirror.class) {
|
||||
recorded.add(obj);
|
||||
ScriptObjectMirror som = (ScriptObjectMirror) obj;
|
||||
if (som.isFunction()) {
|
||||
@ -52,7 +98,6 @@ public class JSONTool {
|
||||
}
|
||||
if (som.isArray()) {
|
||||
JsonArray jarray = new JsonArray();
|
||||
|
||||
for (String str : som.getOwnKeys(true)) {
|
||||
try {
|
||||
if (Integer.parseInt(str) >= 0)
|
||||
@ -95,19 +140,27 @@ public class JSONTool {
|
||||
}
|
||||
return jo;
|
||||
}
|
||||
} else if (Map.class.isAssignableFrom(obj.getClass())) {
|
||||
Map arr = (Map) obj;
|
||||
recorded.add(arr);
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
for (Object k : arr.keySet()) {
|
||||
jsonObject.add(k.toString(), convertMirrorToJsonInternal(arr.get(k), recorded));
|
||||
}
|
||||
return jsonObject;
|
||||
} else if (obj.getClass() == jdk.internal.dynalink.beans.StaticClass.class) {
|
||||
return JsonNull.INSTANCE;
|
||||
} else if (obj instanceof Number) {
|
||||
return new JsonPrimitive((Number) obj);
|
||||
|
||||
} else if (obj instanceof String) {
|
||||
return new JsonPrimitive((String) obj);
|
||||
|
||||
} else if (obj instanceof Character) {
|
||||
return new JsonPrimitive((Character) obj);
|
||||
}
|
||||
if (obj instanceof Boolean) {
|
||||
} else if (obj instanceof Boolean) {
|
||||
return new JsonPrimitive((Boolean) obj);
|
||||
} else if (obj.getClass() == Boolean.TYPE) {
|
||||
return new JsonPrimitive((boolean) obj);
|
||||
|
||||
}
|
||||
return JsonNull.INSTANCE;
|
||||
}
|
||||
|
@ -128,17 +128,18 @@ public class SyncMechUtil {
|
||||
}
|
||||
|
||||
/*
|
||||
* memory的操作
|
||||
* 无需startFlag就可以使用memory的操作
|
||||
* memory的操作 无需startFlag就可以使用memory的操作
|
||||
*/
|
||||
public synchronized String dumpMemory(String path) {
|
||||
if (memoryDumpUtil == null) memoryDumpUtil = new MemoryDumpUtil(engine.engine);
|
||||
if (memoryDumpUtil == null)
|
||||
memoryDumpUtil = new MemoryDumpUtil(engine.engine);
|
||||
return memoryDumpUtil.dumpMemory(path, true);
|
||||
}
|
||||
|
||||
public synchronized String dumpMemory(String path, boolean stateful) {
|
||||
LOGGER.info("dumpMemroy : stateful=" + stateful);
|
||||
if (memoryDumpUtil == null) memoryDumpUtil = new MemoryDumpUtil(engine.engine);
|
||||
if (memoryDumpUtil == null)
|
||||
memoryDumpUtil = new MemoryDumpUtil(engine.engine);
|
||||
return memoryDumpUtil.dumpMemory(path, stateful);
|
||||
}
|
||||
|
||||
@ -210,7 +211,8 @@ public class SyncMechUtil {
|
||||
}
|
||||
|
||||
public void changeCurrType(SyncType t) {
|
||||
if (t == currType) return;
|
||||
if (t == currType)
|
||||
return;
|
||||
|
||||
// finASyncRecord();
|
||||
currType = t;
|
||||
@ -226,7 +228,8 @@ public class SyncMechUtil {
|
||||
}
|
||||
break;
|
||||
case Memory:
|
||||
if (memoryDumpUtil == null) memoryDumpUtil = new MemoryDumpUtil(engine.engine);
|
||||
if (memoryDumpUtil == null)
|
||||
memoryDumpUtil = new MemoryDumpUtil(engine.engine);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@ -256,7 +259,8 @@ public class SyncMechUtil {
|
||||
* Trans操作
|
||||
*/
|
||||
public synchronized String redo(String path) {
|
||||
if (transRecoverUtil == null) transRecoverUtil = new TransRecoverUtil(engine);
|
||||
if (transRecoverUtil == null)
|
||||
transRecoverUtil = new TransRecoverUtil(engine);
|
||||
|
||||
// 先清空,否则会重复执行一些trans
|
||||
if (transRecoverUtil.transRecords != null && !transRecoverUtil.transRecords.isEmpty())
|
||||
@ -264,7 +268,8 @@ public class SyncMechUtil {
|
||||
|
||||
// 某一次检查点之后没有transRecords
|
||||
File file = new File(path);
|
||||
if (!file.exists()) return "success";
|
||||
if (!file.exists())
|
||||
return "success";
|
||||
|
||||
transRecoverUtil.setTraceRecords(path);
|
||||
transRecoverUtil.recoverFromTransRecord();
|
||||
|
@ -6,9 +6,8 @@ import javax.script.ScriptEngine;
|
||||
|
||||
public class TestClassFilter {
|
||||
public TestClassFilter() {
|
||||
final String script = "print(java.lang.System.getProperty(\"java.home\"));" +
|
||||
"print(\"Create file variable\");" +
|
||||
"var File = Java.type(\"java.io.File\");";
|
||||
final String script = "print(java.lang.System.getProperty(\"java.home\"));"
|
||||
+ "print(\"Create file variable\");" + "var File = Java.type(\"java.io.File\");";
|
||||
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
|
||||
ScriptEngine engine = factory.getScriptEngine(new YJSFilter());
|
||||
try {
|
||||
|
@ -63,7 +63,8 @@ public class YJSClassLoader extends URLClassLoader {
|
||||
public String unzipLibrary(InputStream inputStream, String name) {
|
||||
try {
|
||||
if (libDir == null) {
|
||||
libDir = new File(System.getProperty("java.io.tmpdir"), "yjscontract_" + System.currentTimeMillis());
|
||||
libDir = new File(System.getProperty("java.io.tmpdir"),
|
||||
"yjscontract_" + System.currentTimeMillis());
|
||||
libDir.mkdirs();
|
||||
}
|
||||
File f = new File(libDir, name);
|
||||
@ -84,7 +85,7 @@ public class YJSClassLoader extends URLClassLoader {
|
||||
|
||||
private void addDirToPath(String s) {
|
||||
try {
|
||||
System.out.println("[YJSClassloader] addtopath:" + s);
|
||||
// System.out.println("[YJSClassloader] addtopath:" + s);
|
||||
Field field = ClassLoader.class.getDeclaredField("sys_paths");
|
||||
field.setAccessible(true);
|
||||
String[] path = (String[]) field.get(null);
|
||||
|
87
src/main/java/org/bdware/sc/engine/hook/AccessHandler.java
Normal file
87
src/main/java/org/bdware/sc/engine/hook/AccessHandler.java
Normal file
@ -0,0 +1,87 @@
|
||||
package org.bdware.sc.engine.hook;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.sc.JSEngine;
|
||||
import org.bdware.sc.bean.ContractRequest;
|
||||
import org.bdware.sc.boundry.ScriptReturnException;
|
||||
import org.bdware.sc.engine.DesktopEngine;
|
||||
import org.bdware.sc.engine.JSONTool;
|
||||
import org.bdware.sc.entity.DoipMessagePacker;
|
||||
import org.bdware.sc.node.AnnotationHook;
|
||||
import org.bdware.sc.node.AnnotationNode;
|
||||
import org.bdware.sc.node.ArgPacks;
|
||||
import org.bdware.sc.node.FunctionNode;
|
||||
import wrp.jdk.nashorn.internal.runtime.PropertyMap;
|
||||
import wrp.jdk.nashorn.internal.scripts.JO;
|
||||
|
||||
@YJSAnnotation(name = "Access")
|
||||
public class AccessHandler implements AnnotationHook {
|
||||
static Logger LOGGER = LogManager.getLogger(AccessHandler.class);
|
||||
public String acFunction;
|
||||
public boolean requireSign;
|
||||
|
||||
public AccessHandler(AnnotationNode annoNode) {
|
||||
requireSign = false;
|
||||
String content = annoNode.getArgs().get(0);
|
||||
JsonElement je = JsonParser.parseString(content);
|
||||
if (je.isJsonPrimitive() && je.getAsJsonPrimitive().getAsString().equals("verified")) {
|
||||
requireSign = true;
|
||||
}
|
||||
if (je.isJsonObject()) {
|
||||
acFunction = je.getAsJsonObject().get("ACFunction").getAsString();
|
||||
}
|
||||
}
|
||||
|
||||
public static AccessHandler fromAnnotationNode(FunctionNode funNode, AnnotationNode annoNode) {
|
||||
return new AccessHandler(annoNode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArgPacks handle(JSEngine desktopEngine, ArgPacks argPacks) {
|
||||
if (requireSign) {
|
||||
if (!verifyRequest(argPacks)) {
|
||||
throw new ScriptReturnException(
|
||||
JsonParser.parseString("{\"code\":400,\"msg\":\"permission denied\"}"));
|
||||
}
|
||||
return argPacks;
|
||||
}
|
||||
if (!verifyRequest(argPacks)) {
|
||||
LOGGER.info("verify failed! clear requester," + argPacks.request.getContentStr()
|
||||
+ " -> " + argPacks.request.getPublicKey() + "sign:"
|
||||
+ argPacks.request.getSignature());
|
||||
argPacks.request.setRequester(null);
|
||||
} else
|
||||
LOGGER.info("verify success!" + argPacks.request.getRequester());
|
||||
if (acFunction == null)
|
||||
return argPacks;
|
||||
DesktopEngine de = (DesktopEngine) desktopEngine;
|
||||
try {
|
||||
ContractRequest input = argPacks.request;
|
||||
JO jo = new JO(PropertyMap.newMap());
|
||||
jo.put("requester", input.getRequester(), false);
|
||||
jo.put("action", input.getAction(), false);
|
||||
jo.put("arg", JSONTool.convertJsonElementToMirror(input.getArg()), false);
|
||||
de.engine.invokeFunction(acFunction, jo);
|
||||
return argPacks;
|
||||
} catch (ScriptReturnException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
JsonObject jo = new JsonObject();
|
||||
jo.addProperty("code", "401");
|
||||
jo.addProperty("msg", "access check meets exception! " + e);
|
||||
throw new ScriptReturnException(jo);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private boolean verifyRequest(ArgPacks argPacks) {
|
||||
if (argPacks.arg != null && argPacks.arg instanceof DoipMessagePacker) {
|
||||
return true;
|
||||
}
|
||||
return argPacks.request.verifySignature();
|
||||
}
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
package org.bdware.sc.engine.hook;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.sc.JSEngine;
|
||||
import org.bdware.sc.bean.ContractRequest;
|
||||
import org.bdware.sc.boundry.ScriptReturnException;
|
||||
import org.bdware.sc.node.AnnotationHook;
|
||||
import org.bdware.sc.node.AnnotationNode;
|
||||
import org.bdware.sc.node.ArgPacks;
|
||||
import org.bdware.sc.node.FunctionNode;
|
||||
|
||||
@YJSAnnotation(name = "ArgSchema")
|
||||
public class ArgSchemaHandler implements AnnotationHook {
|
||||
private AnnotationNode a;
|
||||
private static final Logger LOGGER = LogManager.getLogger(ArgSchemaHandler.class);
|
||||
|
||||
public ArgSchemaHandler(AnnotationNode annoNode) {
|
||||
a = annoNode;
|
||||
String arg = a.getArgs().get(0);
|
||||
// if (arg.startsWith("/")){
|
||||
// ;//ContractProcess.instance.engine.getResources().loadAsString(arg);
|
||||
// }else {
|
||||
// ;
|
||||
// }
|
||||
}
|
||||
|
||||
public static ArgSchemaHandler fromAnnotationNode(FunctionNode funNode,
|
||||
AnnotationNode annoNode) {
|
||||
return new ArgSchemaHandler(annoNode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArgPacks handle(JSEngine Engine, ArgPacks argPacks) throws ScriptReturnException {
|
||||
ContractRequest input = argPacks.request;
|
||||
JsonElement je = input.getArg();
|
||||
ArgSchemaVisitor visitor;
|
||||
if (je.isJsonObject())
|
||||
visitor = new ArgSchemaVisitor(input.getArg().getAsJsonObject());
|
||||
else {
|
||||
try {
|
||||
JsonElement obj = JsonParser.parseString(input.getArg().getAsString());
|
||||
visitor = new ArgSchemaVisitor(obj);
|
||||
// IMPORTANT automatically convert arg type here
|
||||
input.setArg(obj);
|
||||
argPacks.arg = obj;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
JsonObject jo = new JsonObject();
|
||||
jo.addProperty("msg", "[Illegal Type] argument should be JSON");
|
||||
jo.addProperty("code", 1004);
|
||||
throw new ScriptReturnException(jo);
|
||||
}
|
||||
}
|
||||
if (je.toString().isEmpty() && !a.getArgs().get(0).equals("")) {
|
||||
JsonObject jo = new JsonObject();
|
||||
jo.addProperty("msg", "[Empty argument] argument should not be empty");
|
||||
jo.addProperty("code", 1003);
|
||||
throw new ScriptReturnException(jo);
|
||||
}
|
||||
visitor.visit(JsonParser.parseString(a.getArgs().get(0)));
|
||||
if (!visitor.getStatus()) {
|
||||
JsonObject jo = new JsonObject();
|
||||
jo.addProperty("msg", visitor.getException());
|
||||
jo.addProperty("code", visitor.errorCode);
|
||||
jo.add("argSchema", JsonParser.parseString(a.getArgs().get(0)));
|
||||
throw new ScriptReturnException(jo);
|
||||
}
|
||||
return argPacks;
|
||||
}
|
||||
}
|
154
src/main/java/org/bdware/sc/engine/hook/ArgSchemaVisitor.java
Normal file
154
src/main/java/org/bdware/sc/engine/hook/ArgSchemaVisitor.java
Normal file
@ -0,0 +1,154 @@
|
||||
package org.bdware.sc.engine.hook;
|
||||
|
||||
import com.google.gson.*;
|
||||
import org.bdware.mockjava.JsonVisitor;
|
||||
|
||||
public class ArgSchemaVisitor extends JsonVisitor {
|
||||
JsonElement root;
|
||||
public boolean status = true;
|
||||
public int errorCode = 0;
|
||||
// {"msg":"xxx","code":1000}
|
||||
//
|
||||
// JsonElement message = new JsonPrimitive("");
|
||||
public String message = "";
|
||||
|
||||
public ArgSchemaVisitor(JsonElement ret) {
|
||||
root = ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonVisitor visitObject(JsonObject schema) {
|
||||
// message = new JsonObject();
|
||||
if (root.isJsonObject()) {
|
||||
JsonObject jo = root.getAsJsonObject();
|
||||
for (String key : schema.keySet()) {
|
||||
if (key.startsWith("!")) {
|
||||
// TODO
|
||||
// if(必選)
|
||||
if (jo.has(key.substring(1))) {
|
||||
ArgSchemaVisitor visitor = new ArgSchemaVisitor(jo.get(key.substring(1)));
|
||||
visitor.visit(schema.get(key));
|
||||
if (!visitor.status) {
|
||||
errorCode += visitor.errorCode;
|
||||
message += visitor.message;
|
||||
}
|
||||
status &= visitor.status;
|
||||
} else {
|
||||
message += "[Missing key] " + key.substring(1) + " should be supplied ";
|
||||
status = false;
|
||||
errorCode = 1002;
|
||||
}
|
||||
} else {
|
||||
if (jo.has(key) && jo.get(key) != null && jo.get(key) != JsonNull.INSTANCE) {
|
||||
ArgSchemaVisitor visitor = new ArgSchemaVisitor(jo.get(key));
|
||||
visitor.visit(schema.get(key));
|
||||
if (!visitor.status) {
|
||||
message += visitor.message;
|
||||
errorCode += visitor.errorCode;
|
||||
}
|
||||
status &= visitor.status;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
message += "[Incorrect type] should be object";
|
||||
status = false;
|
||||
errorCode = 1003;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonVisitor visitJsonArray(JsonArray ele) {
|
||||
if (root.isJsonArray()) {
|
||||
JsonArray array = root.getAsJsonArray();
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
ArgSchemaVisitor visitor = new ArgSchemaVisitor(array.get(i));
|
||||
visitor.visit(ele.get(0));
|
||||
message += visitor.message;
|
||||
status &= visitor.status;
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public JsonElement get() {
|
||||
return root;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
public JsonVisitor visitPrimitive(JsonPrimitive primitive) {
|
||||
//
|
||||
if (primitive.isString()) {
|
||||
String type = primitive.getAsString();
|
||||
try {
|
||||
String result = "";
|
||||
// md5不需要参数
|
||||
if (type.equals("string")) {
|
||||
if (root.isJsonPrimitive() && root.getAsJsonPrimitive().isString()) {
|
||||
return this;
|
||||
} else {
|
||||
message = "[Type error] The value ("
|
||||
+ root.getAsJsonPrimitive().getAsString() + ") should be string";
|
||||
status = false;
|
||||
errorCode = 1001;
|
||||
return this;
|
||||
}
|
||||
} else if (type.equals("number")) {
|
||||
if (root.isJsonPrimitive() && root.getAsJsonPrimitive().isNumber()) {
|
||||
return this;
|
||||
} else {
|
||||
message = "[Type error] The value ("
|
||||
+ root.getAsJsonPrimitive().getAsString() + ") should be number";
|
||||
status = false;
|
||||
errorCode = 1001;
|
||||
return this;
|
||||
}
|
||||
} else if (type.equals("boolean")) {
|
||||
if (root.isJsonPrimitive() && root.getAsJsonPrimitive().isBoolean()) {
|
||||
return this;
|
||||
} else {
|
||||
message = "[Type error] The value ("
|
||||
+ root.getAsJsonPrimitive().getAsString() + ") should be boolean";
|
||||
status = false;
|
||||
errorCode = 1001;
|
||||
return this;
|
||||
}
|
||||
} else if (type.equals("[]")) {
|
||||
if (root.isJsonArray()) {
|
||||
return this;
|
||||
} else {
|
||||
message = "[Type error] The value ("
|
||||
+ root.getAsJsonPrimitive().getAsString() + ") should be array";
|
||||
status = false;
|
||||
errorCode = 1001;
|
||||
return this;
|
||||
}
|
||||
} else if (type.equals("{}")) {
|
||||
if (root.isJsonObject()) {
|
||||
return this;
|
||||
} else {
|
||||
message = "[Type error] The value ("
|
||||
+ root.getAsJsonPrimitive().getAsString() + ") should be object";
|
||||
status = false;
|
||||
errorCode = 1001;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public String getException() {
|
||||
return message;
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ import org.bdware.sc.bean.ContractRequest;
|
||||
import org.bdware.sc.engine.ConfidentialContractUtil;
|
||||
import org.bdware.sc.engine.DesktopEngine;
|
||||
import org.bdware.sc.node.AnnotationHook;
|
||||
import org.bdware.sc.node.ArgPacks;
|
||||
import org.bdware.sc.node.FunctionNode;
|
||||
import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror;
|
||||
|
||||
@ -16,19 +17,20 @@ public class ConfidentialHandler implements AnnotationHook {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object handle(ContractRequest input, JSEngine engine, Object ret) {
|
||||
public ArgPacks handle(JSEngine engine, ArgPacks argPacks) {
|
||||
try {
|
||||
ContractRequest input = argPacks.request;
|
||||
DesktopEngine desktopEngine = (DesktopEngine) engine;
|
||||
ConfidentialContractUtil.copyTemplateToDestination(input);
|
||||
ScriptObjectMirror globalVars = (ScriptObjectMirror) desktopEngine.get("Global");
|
||||
ConfidentialContractUtil.dumpScriptAndStates(
|
||||
desktopEngine.engine, fun, input, globalVars);
|
||||
ConfidentialContractUtil.dumpScriptAndStates(desktopEngine.engine, fun, input,
|
||||
globalVars);
|
||||
// run in SGX instead of Nashorn if function has @Confidential annotation
|
||||
ret = ConfidentialContractUtil.executeConfidentialContract(input);
|
||||
return ret;
|
||||
argPacks.ret = ConfidentialContractUtil.executeConfidentialContract(input);
|
||||
return argPacks;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return ret;
|
||||
return argPacks;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,129 @@
|
||||
package org.bdware.sc.engine.hook;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.bdware.doip.codec.JsonDoipMessage;
|
||||
import org.bdware.doip.codec.doipMessage.DoipMessage;
|
||||
import org.bdware.doip.codec.doipMessage.DoipResponseCode;
|
||||
import org.bdware.doip.codec.operations.BasicOperations;
|
||||
import org.bdware.sc.JSEngine;
|
||||
import org.bdware.sc.boundry.ScriptReturnException;
|
||||
import org.bdware.sc.conn.ByteUtil;
|
||||
import org.bdware.sc.entity.DoipMessagePacker;
|
||||
import org.bdware.sc.node.AnnotationHook;
|
||||
import org.bdware.sc.node.ArgPacks;
|
||||
import org.bdware.sc.node.FunctionNode;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class DOOPAfterExecHandler implements AnnotationHook {
|
||||
private JsonElement jsonResponseRules;
|
||||
|
||||
public DOOPAfterExecHandler(BasicOperations operations) {
|
||||
jsonResponseRules = getRulesForJsonResponse(operations);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArgPacks handle(JSEngine desktopEngine, ArgPacks argPacks) {
|
||||
Object originDoipMsgPacker = argPacks.arg;
|
||||
DoipMessage originDoipMsg = null;
|
||||
if (originDoipMsgPacker instanceof DoipMessagePacker) {
|
||||
DoipMessagePacker doipMessagePacker = (DoipMessagePacker) originDoipMsgPacker;
|
||||
originDoipMsg = doipMessagePacker.rawDoipMsg;
|
||||
// if http, directly return
|
||||
if (doipMessagePacker.source.equals("http")) {
|
||||
if (argPacks.ret != null && argPacks.ret instanceof DoipMessage) {
|
||||
argPacks.ret = JsonUtil.parseObject(
|
||||
JsonDoipMessage.fromDoipMessage((DoipMessage) argPacks.ret));
|
||||
}
|
||||
return argPacks;
|
||||
} else {
|
||||
// pack
|
||||
if (!(argPacks.ret instanceof DoipMessage)) {
|
||||
JsonObject jsonObjectRes = ((JsonElement) argPacks.ret).getAsJsonObject();
|
||||
if (!jsonObjectRes.has("bodyBase64Encoded")
|
||||
|| jsonObjectRes.get("bodyBase64Encoded").getAsBoolean() == true) {
|
||||
if (jsonObjectRes.has("body")) {
|
||||
String body = jsonObjectRes.get("body").getAsString();
|
||||
jsonObjectRes.addProperty("body",
|
||||
ByteUtil.encodeBASE64(body.getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
}
|
||||
// validate json response
|
||||
ArgSchemaVisitor visitor = new ArgSchemaVisitor(jsonObjectRes);
|
||||
validateJsonElementRulesByArgSchemaVisitor(jsonResponseRules, visitor);
|
||||
JsonDoipMessage returnedMessage =
|
||||
JsonUtil.fromJson(jsonObjectRes, JsonDoipMessage.class);
|
||||
argPacks.ret = returnedMessage.toResponseDoipMessage(originDoipMsg);
|
||||
}
|
||||
return argPacks;
|
||||
}
|
||||
} else {
|
||||
return argPacks;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static JsonElement getRulesForJsonResponse(BasicOperations basicOperations) {
|
||||
switch (basicOperations) {
|
||||
case Hello:
|
||||
case Retrieve:
|
||||
case Create:
|
||||
case Update:
|
||||
case Search:
|
||||
case ListOps:
|
||||
case Delete:
|
||||
return JsonParser.parseString(
|
||||
"{\"header\":{\"response\":\"string\",\"attributes\":{}},\"body\":\"string\"}");
|
||||
case Extension:
|
||||
case Unknown:
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// old convert jsonResponse from argPack's ret to Doip response in doip chain logic
|
||||
public DoipMessage convertJsonResponseToDoipMessage(FunctionNode fn, JsonElement jsonResponse,
|
||||
DoipMessage msg) {
|
||||
JsonObject jsonParams = jsonResponse.getAsJsonObject();
|
||||
// validate json response
|
||||
ArgSchemaVisitor visitor = new ArgSchemaVisitor(jsonResponse);
|
||||
validateJsonElementRulesByArgSchemaVisitor(jsonResponseRules, visitor);
|
||||
|
||||
JsonObject header =
|
||||
jsonParams.get("header") != null ? jsonParams.get("header").getAsJsonObject()
|
||||
: null;
|
||||
String body = jsonParams.get("body") != null ? jsonParams.get("body").getAsString() : null;
|
||||
|
||||
if (header != null) {
|
||||
String headerRespCode =
|
||||
header.get("response") != null ? header.get("response").getAsString() : null;
|
||||
if (headerRespCode != null) {
|
||||
for (DoipResponseCode responseCode : DoipResponseCode.values()) {
|
||||
if (responseCode.toString().equals(headerRespCode)) {
|
||||
msg.header.parameters.response = responseCode;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (body != null) {
|
||||
msg.body.encodedData = body.getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
public static void validateJsonElementRulesByArgSchemaVisitor(JsonElement jsonElement,
|
||||
ArgSchemaVisitor visitor) {
|
||||
visitor.visit(jsonElement);
|
||||
if (!visitor.getStatus()) {
|
||||
JsonObject jo = new JsonObject();
|
||||
jo.addProperty("msg", visitor.getException());
|
||||
jo.addProperty("code", visitor.errorCode);
|
||||
throw new ScriptReturnException(jo);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,109 @@
|
||||
package org.bdware.sc.engine.hook;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.bdware.doip.codec.JsonDoipMessage;
|
||||
import org.bdware.doip.codec.doipMessage.DoipMessage;
|
||||
import org.bdware.doip.codec.doipMessage.MessageCredential;
|
||||
import org.bdware.doip.codec.operations.BasicOperations;
|
||||
import org.bdware.sc.JSEngine;
|
||||
import org.bdware.sc.bean.ContractRequest;
|
||||
import org.bdware.sc.boundry.ScriptReturnException;
|
||||
import org.bdware.sc.entity.DoipMessagePacker;
|
||||
import org.bdware.sc.node.AnnotationHook;
|
||||
import org.bdware.sc.node.ArgPacks;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
|
||||
public class DOOPBeforeExecHandler implements AnnotationHook {
|
||||
|
||||
private final BasicOperations httpOperation;
|
||||
private JsonElement httpArgsRules;
|
||||
|
||||
public DOOPBeforeExecHandler(BasicOperations operations) {
|
||||
httpOperation = operations;
|
||||
httpArgsRules = getRulesForHTTPRequest(operations);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArgPacks handle(JSEngine desktopEngine, ArgPacks argPacks) {
|
||||
Object arg = argPacks.arg;
|
||||
DoipMessagePacker doipMsgPackerArg = new DoipMessagePacker();
|
||||
if (arg instanceof DoipMessagePacker) {
|
||||
doipMsgPackerArg = (DoipMessagePacker) arg;
|
||||
} else {
|
||||
// validate http request's params
|
||||
ContractRequest httpReq = argPacks.request;
|
||||
validateHTTPRequestArgs(httpReq);
|
||||
// set doipMsgPackerArg struct's params
|
||||
doipMsgPackerArg.setSource("http");
|
||||
doipMsgPackerArg.rawDoipMsg = convertHttpRequestToDoipMessage(httpReq);
|
||||
|
||||
if (httpReq.verifySignature()) {
|
||||
doipMsgPackerArg.rawDoipMsg.credential =
|
||||
new MessageCredential(httpReq.getRequester(), new byte[0]);
|
||||
} else {
|
||||
doipMsgPackerArg.rawDoipMsg.credential = null;
|
||||
}
|
||||
}
|
||||
|
||||
argPacks.arg = doipMsgPackerArg;
|
||||
return argPacks;
|
||||
}
|
||||
|
||||
public void validateHTTPRequestArgs(ContractRequest httpReq) {
|
||||
JsonElement originArgs = httpReq.getArg();
|
||||
JsonElement httpArgs = null;
|
||||
if (originArgs.isJsonObject())
|
||||
httpArgs = originArgs;
|
||||
else
|
||||
httpArgs = JsonParser.parseString(originArgs.getAsString());
|
||||
// get args rules and validate http args
|
||||
ArgSchemaVisitor visitor = new ArgSchemaVisitor(httpArgs);
|
||||
validateJsonElementRulesByArgSchemaVisitor(httpArgsRules, visitor);
|
||||
}
|
||||
|
||||
public static JsonElement getRulesForHTTPRequest(BasicOperations basicOperation) {
|
||||
switch (basicOperation) {
|
||||
case Hello:
|
||||
case Delete:
|
||||
case ListOps:
|
||||
return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\"}}");
|
||||
case Create:
|
||||
case Update:
|
||||
return JsonParser.parseString(
|
||||
"{\"!header\":{\"!identifier\":\"string\"}, \"!body\":\"string\"}");
|
||||
case Search:
|
||||
return JsonParser.parseString(
|
||||
"{\"!header\":{\"!identifier\":\"string\", \"!attributes\":{\"!query\":\"string\", \"!pageNum\":\"int\", \"!pageSize\":\"int\", \"!type\":\"string\"}}}");
|
||||
case Retrieve:
|
||||
return JsonParser.parseString(
|
||||
"{\"!header\":{\"!identifier\":\"string\", \"attributes\":{\"element\":\"string\", \"includeElementData\":\"boolean\"}}}");
|
||||
case Extension:
|
||||
case Unknown:
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public DoipMessage convertHttpRequestToDoipMessage(ContractRequest httpReq) {
|
||||
JsonElement arg = httpReq.getArg();
|
||||
if (!arg.isJsonObject()) {
|
||||
arg = JsonParser.parseString(arg.getAsString());
|
||||
}
|
||||
JsonDoipMessage doipMessage = JsonUtil.fromJson(arg, JsonDoipMessage.class);
|
||||
return doipMessage.toRequestDoipMessage();
|
||||
}
|
||||
|
||||
|
||||
public static void validateJsonElementRulesByArgSchemaVisitor(JsonElement jsonElement,
|
||||
ArgSchemaVisitor visitor) {
|
||||
visitor.visit(jsonElement);
|
||||
if (!visitor.getStatus()) {
|
||||
JsonObject jo = new JsonObject();
|
||||
jo.addProperty("msg", visitor.getException());
|
||||
jo.addProperty("code", visitor.errorCode);
|
||||
throw new ScriptReturnException(jo);
|
||||
}
|
||||
}
|
||||
}
|
@ -6,9 +6,9 @@ import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.sc.ContractResult;
|
||||
import org.bdware.sc.JSEngine;
|
||||
import org.bdware.sc.bean.ContractRequest;
|
||||
import org.bdware.sc.boundry.JavaScriptEntry;
|
||||
import org.bdware.sc.node.AnnotationHook;
|
||||
import org.bdware.sc.node.ArgPacks;
|
||||
import org.bdware.sc.node.FunctionNode;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
import org.paillier.PaillierKeyPair;
|
||||
@ -25,7 +25,7 @@ public class HomomorphicDecryptHandler implements AnnotationHook {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object handle(ContractRequest input, JSEngine engine, Object ret) {
|
||||
public ArgPacks handle(JSEngine engine, ArgPacks argPacks) {
|
||||
try {
|
||||
// GetHomArgs args =
|
||||
// new GetHomArgs(
|
||||
@ -33,30 +33,30 @@ public class HomomorphicDecryptHandler implements AnnotationHook {
|
||||
// String arg = JsonUtil.toJson(args);
|
||||
JsonElement homoDecryptConf = this.fun.getHomoDecryptConf();
|
||||
if (null != homoDecryptConf && !homoDecryptConf.isJsonNull()) {
|
||||
String res = (String)
|
||||
JavaScriptEntry.executeContract(
|
||||
"keyManager_1",
|
||||
"getPrivKey",
|
||||
String res = (String) JavaScriptEntry.executeContract("keyManager_1", "getPrivKey",
|
||||
this.fun.getSecretID().replaceAll("\"", ""));
|
||||
LOGGER.info("HomomorphicDecryptHandler--------------------------------1: " + res);
|
||||
ContractResult results = JsonUtil.fromJson(res, ContractResult.class);
|
||||
String privKeyStr = results.result.getAsString();
|
||||
LOGGER.info("HomomorphicEncryptHandler--------------------------------2: " + privKeyStr);
|
||||
LOGGER.info("HomomorphicEncryptHandler--------------------------------2: "
|
||||
+ privKeyStr);
|
||||
RSAPrivateKey privkey = (RSAPrivateKey) PaillierKeyPair.pemToPrivateKey(privKeyStr);
|
||||
LOGGER.info("HomomorphicEncryptHandler--------------------------------3: " + privkey);
|
||||
LOGGER.info(
|
||||
"HomomorphicEncryptHandler--------------------------------3: " + privkey);
|
||||
HomoVisitor.privateKey = privkey;
|
||||
ret = getDecryptResult(homoDecryptConf, JsonUtil.parseObject(ret));
|
||||
if (null != ret) {
|
||||
return ret;
|
||||
argPacks.ret =
|
||||
getDecryptResult(homoDecryptConf, JsonUtil.parseObject(argPacks.ret));
|
||||
if (null != argPacks.ret) {
|
||||
return argPacks;
|
||||
}
|
||||
ret = new JsonObject();
|
||||
argPacks.ret = new JsonObject();
|
||||
}
|
||||
return ret;
|
||||
return argPacks;
|
||||
// return ret;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return ret;
|
||||
return argPacks;
|
||||
}
|
||||
|
||||
private Object getDecryptResult(JsonElement homoDecryptConf, JsonElement data) {
|
||||
|
@ -9,6 +9,7 @@ import org.bdware.sc.JSEngine;
|
||||
import org.bdware.sc.bean.ContractRequest;
|
||||
import org.bdware.sc.boundry.JavaScriptEntry;
|
||||
import org.bdware.sc.node.AnnotationHook;
|
||||
import org.bdware.sc.node.ArgPacks;
|
||||
import org.bdware.sc.node.FunctionNode;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
import org.paillier.PaillierKeyPair;
|
||||
@ -25,21 +26,17 @@ public class HomomorphicEncryptHandler implements AnnotationHook {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object handle(ContractRequest input, JSEngine engine, Object ret) {
|
||||
public ArgPacks handle(JSEngine engine, ArgPacks argPacks) {
|
||||
try {
|
||||
LOGGER.info(
|
||||
"HomomorphicEncryptHandler--------------------------------1: "
|
||||
ContractRequest input = argPacks.request;
|
||||
LOGGER.info("HomomorphicEncryptHandler--------------------------------1: "
|
||||
+ input.getRequester());
|
||||
LOGGER.info(
|
||||
"HomomorphicEncryptHandler--------------------------------2: "
|
||||
LOGGER.info("HomomorphicEncryptHandler--------------------------------2: "
|
||||
+ this.fun.getSecretID());
|
||||
JsonElement response = (JsonElement) ret;
|
||||
JsonElement response = (JsonElement) argPacks.ret;
|
||||
JsonElement homoEncryptConf = this.fun.getHomoEncryptConf();
|
||||
if (homoEncryptConf != null && !homoEncryptConf.isJsonNull()) {
|
||||
String res =
|
||||
(String) JavaScriptEntry.executeContract(
|
||||
"keyManager_1",
|
||||
"getPubKey",
|
||||
String res = (String) JavaScriptEntry.executeContract("keyManager_1", "getPubKey",
|
||||
this.fun.getSecretID().replaceAll("\"", ""));
|
||||
// String res =
|
||||
// JavaScriptEntry.executeContract(
|
||||
@ -49,25 +46,30 @@ public class HomomorphicEncryptHandler implements AnnotationHook {
|
||||
LOGGER.info("HomomorphicEncryptHandler--------------------------------4: " + res);
|
||||
ContractResult results = JsonUtil.fromJson(res, ContractResult.class);
|
||||
String pubKeyStr = results.result.getAsString();
|
||||
LOGGER.info("HomomorphicEncryptHandler--------------------------------5: " + pubKeyStr);
|
||||
LOGGER.info(
|
||||
"HomomorphicEncryptHandler--------------------------------5: " + pubKeyStr);
|
||||
HomoVisitor.publicKey = (RSAPublicKey) PaillierKeyPair.pemToPublicKey(pubKeyStr);
|
||||
// if (homoEncryptConf.getAsJsonPrimitive().isString())
|
||||
// homoEncryptConf = JsonParser.parseString(homoEncryptConf.getAsString());
|
||||
LOGGER.info("HomomorphicEncryptHandler--------------------------------6: " + homoEncryptConf);
|
||||
LOGGER.info("HomomorphicEncryptHandler--------------------------------7: " + ret);
|
||||
LOGGER.info("HomomorphicEncryptHandler--------------------------------8: " + ret.toString());
|
||||
// LOGGER.info("HomomorphicEncryptHandler--------------------------------9: " + JsonUtil.toJson(ret));
|
||||
ret = getEncryptResult(homoEncryptConf, response);
|
||||
if (ret != null) {
|
||||
return ret;
|
||||
LOGGER.info("HomomorphicEncryptHandler--------------------------------6: "
|
||||
+ homoEncryptConf);
|
||||
LOGGER.info("HomomorphicEncryptHandler--------------------------------7: "
|
||||
+ argPacks.ret);
|
||||
LOGGER.info("HomomorphicEncryptHandler--------------------------------8: "
|
||||
+ argPacks.ret.toString());
|
||||
// LOGGER.info("HomomorphicEncryptHandler--------------------------------9: " +
|
||||
// JsonUtil.toJson(ret));
|
||||
argPacks.ret = getEncryptResult(homoEncryptConf, response);
|
||||
if (argPacks.ret != null) {
|
||||
return argPacks;
|
||||
}
|
||||
ret = new JsonObject();
|
||||
argPacks.ret = new JsonObject();
|
||||
}
|
||||
return ret;
|
||||
return argPacks;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return ret;
|
||||
return argPacks;
|
||||
}
|
||||
|
||||
private Object getEncryptResult(JsonElement homoEncryptConf, JsonElement data) {
|
||||
|
@ -9,13 +9,16 @@ import org.bdware.sc.bean.ContractRequest;
|
||||
import org.bdware.sc.bean.ProjectConfig;
|
||||
import org.bdware.sc.engine.DesktopEngine;
|
||||
import org.bdware.sc.node.AnnotationHook;
|
||||
import org.bdware.sc.node.ArgPacks;
|
||||
|
||||
public class MaskHandler implements AnnotationHook {
|
||||
private static final Logger LOGGER = LogManager.getLogger(MaskHandler.class);
|
||||
|
||||
@Override
|
||||
public Object handle(ContractRequest input, JSEngine Engine, Object ret) {
|
||||
public ArgPacks handle(JSEngine Engine, ArgPacks argPacks) {
|
||||
try {
|
||||
ContractRequest input = argPacks.request;
|
||||
Object ret = argPacks.ret;
|
||||
DesktopEngine desktopEngine = (DesktopEngine) Engine;
|
||||
ProjectConfig projectConfig = desktopEngine.getProjectConfig();
|
||||
JsonElement maskConf = projectConfig.getMask(input.getAction());
|
||||
@ -35,14 +38,17 @@ public class MaskHandler implements AnnotationHook {
|
||||
ret = visitor.get();
|
||||
LOGGER.debug(maskConf);
|
||||
if (null != ret) {
|
||||
return ret;
|
||||
argPacks.ret = ret;
|
||||
|
||||
return argPacks;
|
||||
}
|
||||
ret = JsonParser.parseString("");
|
||||
}
|
||||
return ret;
|
||||
argPacks.ret = ret;
|
||||
return argPacks;
|
||||
} catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
return ret;
|
||||
return argPacks;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,11 +6,13 @@ import org.bdware.sc.bean.ContractRequest;
|
||||
import org.bdware.sc.bean.ProjectConfig;
|
||||
import org.bdware.sc.engine.DesktopEngine;
|
||||
import org.bdware.sc.node.AnnotationHook;
|
||||
import org.bdware.sc.node.ArgPacks;
|
||||
|
||||
public class MockTemplateHandler implements AnnotationHook {
|
||||
@Override
|
||||
public Object handle(ContractRequest request, JSEngine engine, Object ret) {
|
||||
public ArgPacks handle(JSEngine engine, ArgPacks argPacks) {
|
||||
try {
|
||||
ContractRequest request = argPacks.request;
|
||||
if (request.fromDebug()) {
|
||||
System.out.println(request.getAction());
|
||||
DesktopEngine desktopEngine = (DesktopEngine) engine;
|
||||
@ -19,13 +21,15 @@ public class MockTemplateHandler implements AnnotationHook {
|
||||
if (template != null && template.length() > 0) {
|
||||
System.out.println(template);
|
||||
MockUtil Mock = new MockUtil();
|
||||
return Mock.mock(template).toString();
|
||||
} else return ret; //When mock config is null defined just ignore.
|
||||
argPacks.ret = Mock.mock(template).toString();
|
||||
return argPacks;
|
||||
} else
|
||||
return argPacks; // When mock config is null defined just ignore.
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
return ret;
|
||||
return argPacks;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,17 @@
|
||||
package org.bdware.sc.engine.hook;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonNull;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import org.bdware.doip.codec.doipMessage.DoipMessage;
|
||||
import org.bdware.sc.JSEngine;
|
||||
import org.bdware.sc.bean.ContractRequest;
|
||||
import org.bdware.sc.engine.JSONTool;
|
||||
import org.bdware.sc.node.AnnotationHook;
|
||||
import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror;
|
||||
import org.bdware.sc.node.ArgPacks;
|
||||
|
||||
public class ObjToJsonHandler implements AnnotationHook {
|
||||
@Override
|
||||
public Object handle(ContractRequest input, JSEngine desktopEngine, Object ret) {
|
||||
return JSONTool.convertMirrorToJson(ret);
|
||||
public ArgPacks handle(JSEngine desktopEngine, ArgPacks argPacks) {
|
||||
if (argPacks.ret instanceof DoipMessage)
|
||||
return argPacks;
|
||||
argPacks.ret = JSONTool.convertMirrorToJson(argPacks.ret);
|
||||
return argPacks;
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import org.bdware.sc.JSEngine;
|
||||
import org.bdware.sc.bean.ContractRequest;
|
||||
import org.bdware.sc.engine.DesktopEngine;
|
||||
import org.bdware.sc.node.AnnotationHook;
|
||||
import org.bdware.sc.node.ArgPacks;
|
||||
import org.commonmark.node.FencedCodeBlock;
|
||||
import org.commonmark.node.Heading;
|
||||
import org.commonmark.node.Node;
|
||||
@ -12,10 +13,9 @@ import org.commonmark.parser.Parser;
|
||||
|
||||
public class ReadMeHandler implements AnnotationHook {
|
||||
String getReadMeData(DesktopEngine desktopEngine, ContractRequest c) {
|
||||
String fileReadme =
|
||||
desktopEngine
|
||||
.getResources()
|
||||
.loadAsString("/assets/README.md"); // is "/README.md" not"./README.md"!!!!
|
||||
String fileReadme = desktopEngine.getResources().loadAsString("/assets/README.md"); // is
|
||||
// "/README.md"
|
||||
// not"./README.md"!!!!
|
||||
// System.out.println("fileReadme:" + fileReadme);
|
||||
if (null == fileReadme) {
|
||||
return "项目目录下无预览文档";
|
||||
@ -29,18 +29,11 @@ public class ReadMeHandler implements AnnotationHook {
|
||||
while (visitor != null) {
|
||||
if (visitor instanceof Heading) {
|
||||
if (((Heading) visitor).getLevel() == 2) {
|
||||
if (((Text) (visitor.getFirstChild()))
|
||||
.getLiteral()
|
||||
if (((Text) (visitor.getFirstChild())).getLiteral()
|
||||
.equals(targetFunction)) {
|
||||
FencedCodeBlock blockResult =
|
||||
(FencedCodeBlock)
|
||||
(visitor.getNext()
|
||||
.getNext()
|
||||
.getNext()
|
||||
.getNext()
|
||||
.getNext()
|
||||
.getNext()
|
||||
.getNext());
|
||||
(FencedCodeBlock) (visitor.getNext().getNext().getNext()
|
||||
.getNext().getNext().getNext().getNext());
|
||||
result = blockResult.getLiteral();
|
||||
break;
|
||||
}
|
||||
@ -56,12 +49,14 @@ public class ReadMeHandler implements AnnotationHook {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object handle(ContractRequest input, JSEngine engine, Object ret) {
|
||||
public ArgPacks handle(JSEngine engine, ArgPacks argPacks) {
|
||||
DesktopEngine desktopEngine = (DesktopEngine) engine;
|
||||
if (input.fromDebug() && (ret == null || ret.equals("emptyMock"))) {
|
||||
ret = getReadMeData(desktopEngine, input);
|
||||
System.out.println(ret);
|
||||
ContractRequest input = argPacks.request;
|
||||
|
||||
if (input.fromDebug() && (argPacks.ret == null || argPacks.ret.equals("emptyMock"))) {
|
||||
argPacks.ret = getReadMeData(desktopEngine, input);
|
||||
System.out.println(argPacks.ret);
|
||||
}
|
||||
return ret;
|
||||
return argPacks;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,85 @@
|
||||
package org.bdware.sc.engine.hook;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.doip.codec.JsonDoipMessage;
|
||||
import org.bdware.doip.codec.doipMessage.DoipMessage;
|
||||
import org.bdware.sc.JSEngine;
|
||||
import org.bdware.sc.boundry.ScriptReturnException;
|
||||
import org.bdware.sc.node.AnnotationHook;
|
||||
import org.bdware.sc.node.AnnotationNode;
|
||||
import org.bdware.sc.node.ArgPacks;
|
||||
import org.bdware.sc.node.FunctionNode;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
|
||||
@YJSAnnotation(name = "ResultSchema")
|
||||
public class ResultSchemaHandler implements AnnotationHook {
|
||||
private AnnotationNode a;
|
||||
private static final Logger LOGGER = LogManager.getLogger(ResultSchemaHandler.class);
|
||||
|
||||
public ResultSchemaHandler(AnnotationNode annotationNode) {
|
||||
a = annotationNode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArgPacks handle(JSEngine desktopEngine, ArgPacks argPacks) throws ScriptReturnException {
|
||||
Object ret = argPacks.ret;
|
||||
if (ret != null) {
|
||||
JsonElement je = null;
|
||||
if (ret instanceof DoipMessage) {
|
||||
JsonDoipMessage jo = JsonDoipMessage.fromDoipMessage((DoipMessage) ret);
|
||||
je = JsonUtil.parseObjectAsJsonObject(jo);
|
||||
} else if (ret instanceof JsonElement) {
|
||||
je = (JsonElement) ret;
|
||||
}
|
||||
if (je == null) {
|
||||
JsonObject jo = new JsonObject();
|
||||
jo.addProperty("msg", "[Illegal Type] result should not be empty or null");
|
||||
jo.addProperty("code", 1004);
|
||||
throw new ScriptReturnException(jo);
|
||||
}
|
||||
ArgSchemaVisitor visitor;
|
||||
if (je.isJsonObject())
|
||||
visitor = new ArgSchemaVisitor(je.getAsJsonObject());
|
||||
else {
|
||||
try {
|
||||
JsonElement obj = JsonParser.parseString(je.getAsString());
|
||||
visitor = new ArgSchemaVisitor(obj);
|
||||
// IMPORTANT automatically convert arg type here
|
||||
argPacks.ret = obj;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
JsonObject jo = new JsonObject();
|
||||
jo.addProperty("msg", "[Illegal Type] result should be JSON");
|
||||
jo.add("errorResult", je);
|
||||
jo.addProperty("code", 1004);
|
||||
throw new ScriptReturnException(jo);
|
||||
}
|
||||
}
|
||||
if (je.toString().isEmpty() && !a.getArgs().get(0).equals("")) {
|
||||
JsonObject jo = new JsonObject();
|
||||
jo.addProperty("msg", "[Empty result] result should not be empty");
|
||||
jo.addProperty("code", 1003);
|
||||
throw new ScriptReturnException(jo);
|
||||
}
|
||||
visitor.visit(JsonParser.parseString(a.getArgs().get(0)));
|
||||
if (!visitor.getStatus()) {
|
||||
JsonObject jo = new JsonObject();
|
||||
jo.addProperty("msg", visitor.getException());
|
||||
jo.addProperty("code", visitor.errorCode);
|
||||
jo.add("errorResult", je);
|
||||
jo.add("resultSchema", JsonParser.parseString(a.getArgs().get(0)));
|
||||
throw new ScriptReturnException(jo);
|
||||
}
|
||||
}
|
||||
return argPacks;
|
||||
}
|
||||
|
||||
public static ResultSchemaHandler fromAnnotationNode(FunctionNode funNode,
|
||||
AnnotationNode annoNode) {
|
||||
return new ResultSchemaHandler(annoNode);
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package org.bdware.sc.engine.hook;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface YJSAnnotation {
|
||||
String name() default "";
|
||||
}
|
@ -3,8 +3,7 @@ package org.bdware.sc.engine.setArgs;
|
||||
public class GetHomArgs {
|
||||
public String secretID;
|
||||
|
||||
public GetHomArgs() {
|
||||
}
|
||||
public GetHomArgs() {}
|
||||
|
||||
public GetHomArgs(String secretID) {
|
||||
this.secretID = secretID;
|
||||
|
33
src/main/java/org/bdware/sc/entity/DoipMessagePacker.java
Normal file
33
src/main/java/org/bdware/sc/entity/DoipMessagePacker.java
Normal file
@ -0,0 +1,33 @@
|
||||
package org.bdware.sc.entity;
|
||||
|
||||
import org.bdware.doip.codec.JsonDoipMessage;
|
||||
import org.bdware.doip.codec.doipMessage.DoipMessage;
|
||||
import org.bdware.sc.engine.JSONTool;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
|
||||
public class DoipMessagePacker {
|
||||
// the DoipMessagePacker is raised by http/doip
|
||||
public String source;
|
||||
// the well-composed DoipMessage
|
||||
public DoipMessage rawDoipMsg;
|
||||
|
||||
public DoipMessagePacker() {}
|
||||
|
||||
public DoipMessagePacker(String source, DoipMessage rawMsg) {
|
||||
this.source = source;
|
||||
this.rawDoipMsg = rawMsg;
|
||||
}
|
||||
|
||||
public String getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
public void setSource(String source) {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
public Object toJSObject() {
|
||||
return JSONTool.convertJsonElementToMirror(
|
||||
JsonUtil.parseObject(JsonDoipMessage.fromDoipMessage(rawDoipMsg)));
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package org.bdware.sc.handler;
|
||||
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.sc.ContractProcess;
|
||||
@ -12,6 +13,7 @@ import org.bdware.sc.get.GetMessage;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ContractHandler extends MsgHandler implements Runnable {
|
||||
@ -100,6 +102,12 @@ public class ContractHandler extends MsgHandler implements Runnable {
|
||||
cb.onResult(cs.setContractBundle(JsonUtil.fromJson(msg.arg, Contract.class)));
|
||||
}
|
||||
|
||||
@Description("set members")
|
||||
public void setMembers(GetMessage msg, ResultCallback cb) {
|
||||
cb.onResult(cs.setMembers(
|
||||
JsonUtil.fromJson(msg.arg, new TypeToken<List<String>>() {}.getType())));
|
||||
}
|
||||
|
||||
@Description("get current contract name")
|
||||
public void getContractName(GetMessage msg, ResultCallback cb) {
|
||||
cb.onResult(cs.getContractName());
|
||||
@ -110,11 +118,16 @@ public class ContractHandler extends MsgHandler implements Runnable {
|
||||
cb.onResult(cs.setContract(JsonUtil.fromJson(msg.arg, Contract.class)));
|
||||
}
|
||||
|
||||
@Description(value = "execute contract")
|
||||
@Description(value = "execute contract", isAsync = true)
|
||||
public void executeContract(GetMessage msg, ResultCallback cb) {
|
||||
cb.onResult(cs.executeContract(msg.arg));
|
||||
}
|
||||
|
||||
@Description(value = "invoke function without limit")
|
||||
public void invokeFunctionWithoutLimit(GetMessage msg, ResultCallback cb) {
|
||||
cb.onResult(cs.executeFunctionWithoutLimit(msg.arg));
|
||||
}
|
||||
|
||||
@Description("set DesktopPermission")
|
||||
public void setDesktopPermission(GetMessage msg, ResultCallback cb) {
|
||||
String result = cs.setDesktopPermission(msg.arg);
|
||||
@ -221,7 +234,7 @@ public class ContractHandler extends MsgHandler implements Runnable {
|
||||
|
||||
@Description("get contract")
|
||||
public void getContract(GetMessage msg, ResultCallback cb) {
|
||||
cb.onResult(cs.getContract());
|
||||
cb.onResult(JsonUtil.toJson(cs.getContract()));
|
||||
}
|
||||
|
||||
@Description("setPID")
|
||||
|
117
src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java
Normal file
117
src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java
Normal file
@ -0,0 +1,117 @@
|
||||
package org.bdware.sc.handler;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.doip.codec.JsonDoipMessage;
|
||||
import org.bdware.doip.codec.doipMessage.DoipMessage;
|
||||
import org.bdware.doip.codec.doipMessage.DoipMessageFactory;
|
||||
import org.bdware.doip.codec.doipMessage.DoipResponseCode;
|
||||
import org.bdware.doip.codec.doipMessage.HeaderParameter;
|
||||
import org.bdware.doip.codec.operations.BasicOperations;
|
||||
import org.bdware.doip.endpoint.server.DoipRequestHandler;
|
||||
import org.bdware.doip.endpoint.server.NettyServerHandler;
|
||||
import org.bdware.sc.ContractProcess;
|
||||
import org.bdware.sc.bean.ContractRequest;
|
||||
import org.bdware.sc.boundry.JavaScriptEntry;
|
||||
import org.bdware.sc.boundry.ScriptReturnException;
|
||||
import org.bdware.sc.crdt.SharableVarManager;
|
||||
import org.bdware.sc.entity.DoipMessagePacker;
|
||||
import org.bdware.sc.node.FunctionNode;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class DOOPRequestHandler implements DoipRequestHandler {
|
||||
static Logger logger = LogManager.getLogger(NettyServerHandler.class);
|
||||
public Map<String, FunctionNode> doipFunctionNodeMap;
|
||||
|
||||
public DOOPRequestHandler() {
|
||||
doipFunctionNodeMap = new HashMap<>();
|
||||
}
|
||||
|
||||
public void addDoipOperation(FunctionNode function) throws Exception {
|
||||
String operationName = function.getDoipOperationInfo().operationName;
|
||||
if (doipFunctionNodeMap.containsKey(operationName)) {
|
||||
throw new Exception("Contract is wrong: One DO operation maps multiple functions");
|
||||
}
|
||||
doipFunctionNodeMap.put(operationName, function);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DoipMessage onRequest(ChannelHandlerContext ctx, DoipMessage msg) {
|
||||
String str = msg.header.parameters.operation;
|
||||
if (msg.header != null
|
||||
&& msg.header.parameters.operation.equals(SharableVarManager.SHARABLEOP)) {
|
||||
return SharableVarManager.instance.handleSyncMessage(msg);
|
||||
}
|
||||
logger.debug("[Call operation] name: " + str);
|
||||
if (str != null) {
|
||||
FunctionNode fn;
|
||||
fn = doipFunctionNodeMap.get(str);
|
||||
if (fn == null)
|
||||
fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName());
|
||||
if (fn != null) {
|
||||
return buildRequestAndInvokeEngine(fn, msg);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public DoipMessage buildRequestAndInvokeEngine(FunctionNode fn, DoipMessage msg) {
|
||||
ContractRequest contractRequest = constructContractRequest(fn, msg);
|
||||
DoipMessagePacker arg = new DoipMessagePacker("doip", msg);
|
||||
try {
|
||||
// 改变调用的函数 + 构造DoipMessagePacker
|
||||
Object ret =
|
||||
ContractProcess.instance.engine.executeWithoutLock(fn, contractRequest, arg);
|
||||
DoipMessage finalDoipMsg = (DoipMessage) ret;
|
||||
finalDoipMsg.requestID = msg.requestID;
|
||||
if (finalDoipMsg.header.parameters == null) {
|
||||
finalDoipMsg.header.parameters = new HeaderParameter("", "");
|
||||
}
|
||||
if (finalDoipMsg.header.parameters.attributes == null) {
|
||||
finalDoipMsg.header.parameters.attributes = new JsonObject();
|
||||
}
|
||||
finalDoipMsg.header.parameters.attributes.addProperty("nodeID",
|
||||
String.valueOf(JavaScriptEntry.shardingID));
|
||||
return finalDoipMsg;
|
||||
} catch (ScriptReturnException e) {
|
||||
DoipMessageFactory.DoipMessageBuilder builder =
|
||||
new DoipMessageFactory.DoipMessageBuilder();
|
||||
builder.createResponse(DoipResponseCode.Declined, msg);
|
||||
builder.setBody(e.message.toString().getBytes(StandardCharsets.UTF_8));
|
||||
logger.error("buildRequestAndInvokeEngine catch ScriptReturnException");
|
||||
return builder.create();
|
||||
} catch (Exception e) {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
e.printStackTrace(new PrintStream(bo));
|
||||
DoipMessageFactory.DoipMessageBuilder builder =
|
||||
new DoipMessageFactory.DoipMessageBuilder();
|
||||
builder.createResponse(DoipResponseCode.Declined, msg);
|
||||
builder.setBody(bo.toByteArray());
|
||||
builder.addAttributes("nodeID", String.valueOf(JavaScriptEntry.shardingID));
|
||||
logger.error(
|
||||
"buildRequestAndInvokeEngine has something wrong, executeWithoutLock err or validateJsonElementRulesByArgSchemaVisitor err");
|
||||
return builder.create();
|
||||
}
|
||||
}
|
||||
|
||||
public ContractRequest constructContractRequest(FunctionNode fn, DoipMessage request) {
|
||||
ContractRequest cr = new ContractRequest();
|
||||
cr.setContractID("");
|
||||
if (request.credential == null) {
|
||||
cr.setRequester(null);
|
||||
} else {
|
||||
cr.setRequester(request.credential.getSigner());
|
||||
}
|
||||
cr.setAction(fn.functionName);
|
||||
cr.setArg(JsonUtil.parseObjectAsJsonObject(JsonDoipMessage.fromDoipMessage(request)));
|
||||
return cr;
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user