Skip to content

Commit 393c3db

Browse files
authored
Merge pull request #228 from kyonRay/main
Release v3.9.0
2 parents afe37fc + 588b887 commit 393c3db

File tree

6 files changed

+334
-23
lines changed

6 files changed

+334
-23
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ println("Notice: current gradle version is " + gradle.gradleVersion)
1111
// Additional attribute definition
1212
ext {
1313
// jackson version
14-
javaSDKVersion="3.7.0-SNAPSHOT"
14+
javaSDKVersion="3.7.0"
1515
//solcJVersion = "0.4.25.1"
1616
//solcJVersion = "0.5.2.1"
1717
//solcJVersion = "0.6.10.1"
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright (C) @2014-2022 Webank
2+
// SPDX-License-Identifier: Apache-2.0
3+
pragma solidity >=0.6.10 <0.8.20;
4+
pragma experimental ABIEncoderV2;
5+
6+
import "./ZkpPrecompiled.sol";
7+
import "./WedprUtils.sol";
8+
import "./Crypto.sol";
9+
10+
struct SystemParameters {
11+
int256 itemId; // the itemId
12+
string itemMeta; // item meta data
13+
string[] candidates;
14+
bytes hpoint;
15+
}
16+
17+
struct Ballot{
18+
string candidateId;
19+
bytes cipher1;
20+
bytes cipher2;
21+
bool isAssigned;
22+
}
23+
// the ballot proof
24+
struct BallotProof{
25+
bytes formatProof;
26+
bytes eitherEqualityProof;
27+
Ballot ballot;
28+
}
29+
30+
struct UnlistedBallot
31+
{
32+
bytes cipher1;
33+
bytes cipher2;
34+
Ballot ballot;
35+
bool isAssigned;
36+
}
37+
// the candiate ballot proof
38+
struct UnlistedCandidateBallotProof
39+
{
40+
UnlistedBallot ballot;
41+
BallotProof unlisted_ballot_proof;
42+
}
43+
struct VoteRequest{
44+
Ballot blankBallot;
45+
Ballot zeroBallot;
46+
// bytes publicKey;
47+
// bytes32 signatureR;
48+
// bytes32 signatureS;
49+
}
50+
struct VoteStorage{
51+
bool isAssigned;
52+
VoteRequest voteRequest;
53+
string[] candidateListForBallot;
54+
bytes[] cipherCandidateListForUnlistedBallot;
55+
}
56+
struct CounterStorage
57+
{
58+
// 计票公钥分片
59+
bytes hpointShare;
60+
// 计票公钥是否被聚合
61+
bool setted;
62+
// 计票服务状态,1:计票中,2:计票完成
63+
int8 counterStatus;
64+
// 计票服务实例的心跳时间
65+
uint256 updateTime;
66+
// 一个投票item中投票密文的聚合值
67+
string voteStorageSum;
68+
// 使用acv-core的公钥加密的计票服务sm4密钥
69+
string sm4SecretKeyCipherText;
70+
// 使用计票服务的sm4密钥加密的计票分片
71+
string countingPartResultCipherText;
72+
// 计票分片哈希值
73+
bytes decryptedPartResultHash;
74+
}
75+
76+
contract AnonymousVotingInterface {
77+
function initialize(bytes memory basePointG1, bytes memory basePointG2) public virtual;
78+
function setSystemParameters(int256 itemId, string memory itemMeta, string[] memory candidates, bytes memory hpoint) public virtual;
79+
function getSystemParameters() public view virtual returns(SystemParameters memory);
80+
function setCounterNumber(uint256 counterNumber) public virtual;
81+
function getCounterNumber() public view virtual returns(uint256);
82+
function setContractState(uint8 state) public virtual;
83+
function getContractState() public view virtual returns(uint8);
84+
function setVoterIdList(string[] memory voterIdList) public virtual;
85+
function getVoterIdList() public view virtual returns(string[] memory);
86+
function setVoterIdToVoteStorage(string memory voterId, VoteStorage memory voteStorage) public virtual;
87+
function getVoterIdToVoteStorage(string memory voterId) public view virtual returns(VoteStorage memory);
88+
function setVoterIDToCandidateIDToBallot(string memory voterId, string memory candidateId, Ballot memory ballot) public virtual;
89+
function getVoterIDToCandidateIDToBallot(string memory voterId, string memory candidateId) public view virtual returns(Ballot memory);
90+
function setVoterIDToCandidateIDToUnlistedBallot(string memory voterId, bytes memory candidateId, UnlistedBallot memory unlistedBallot) public virtual;
91+
function getVoterIDToCandidateIDToUnlistedBallot(string memory voterId, bytes memory candidateId) public view virtual returns(UnlistedBallot memory);
92+
function setCounterIdToCounterStorage(string memory counterId, CounterStorage memory counterStorage) public virtual;
93+
function getCounterIdToCounterStorage(string memory counterId) public view virtual returns(CounterStorage memory);
94+
function setCounterStatus(string memory counterId, int8 status) public virtual;
95+
function getCounterStatus(string memory counterId) public view virtual returns(int8);
96+
function setCounterUpdateTime(string memory counterId, uint256 updateTime) public virtual;
97+
function getCounterUpdateTime(string memory counterId) public view virtual returns(uint256);
98+
function setVoteStorageSum(string memory counterId, string memory voteStorageSum) public virtual;
99+
function getVoteStorageSum(string memory counterId) public view virtual returns(string memory);
100+
function setSm4SecretKeyCipherText(string memory counterId, string memory sm4SecretKeyCipherText) public virtual;
101+
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
pragma solidity ^0.6.0;
2+
pragma experimental ABIEncoderV2;
3+
4+
contract ReceiveAndCallbackFunction {
5+
event Info(string, uint256);
6+
// receive function
7+
receive() external payable {
8+
require(msg.value > 10, "receive() msg.value is zero");
9+
emit Info("receive() msg.value", msg.value);
10+
}
11+
12+
// fallback function
13+
fallback() external payable {
14+
require(msg.value > 0, "fallback() msg.value is zero");
15+
emit Info("fallback() msg.value", msg.value);
16+
}
17+
}
18+
19+
contract OnlyReceiveFunction {
20+
event Info(string, uint256);
21+
// receive function
22+
receive() external payable {
23+
require(msg.value > 10, "receive() msg.value is zero");
24+
emit Info("receive() msg.value", msg.value);
25+
}
26+
}
27+
28+
contract OnlyFallbackFunction {
29+
30+
event Info(string, uint256);
31+
32+
// fallback function
33+
fallback() external payable {
34+
require(msg.value > 0, "fallback() msg.value is zero");
35+
emit Info("fallback() msg.value", msg.value);
36+
}
37+
}
38+
39+
40+
contract BalanceReceiveTest {
41+
modifier mustHasBalance() {
42+
require(address(this).balance > 0, "balance not enough");
43+
_;
44+
}
45+
46+
function callTransfer(address payable to, uint256 amount) public payable {
47+
to.transfer(amount);
48+
}
49+
50+
function callFallback(address payable to, uint256 amount) public payable {
51+
(bool success, bytes memory reason) = to.call{value: amount}("aaa()");
52+
require(success, string(abi.encodePacked("callFallback failed: ", reason)));
53+
}
54+
55+
function checkOnlyFallbackFunction() public payable {
56+
uint256 callValue = 7;
57+
58+
OnlyFallbackFunction contractAddress = new OnlyFallbackFunction();
59+
// must success
60+
try this.callTransfer(address(contractAddress), callValue) {
61+
} catch (bytes memory reason) {
62+
require(false, string(abi.encodePacked("checkOnlyFallbackFunction callTransfer with value failed: ", reason)));
63+
}
64+
65+
require(address(contractAddress).balance == callValue, "checkReceive failed: balance not equal");
66+
67+
// must failed transfer 0
68+
try this.callTransfer(address(contractAddress), 0) {
69+
require(false, "checkOnlyFallbackFunction callTransfer without value should revert");
70+
} catch {
71+
}
72+
73+
// must success on fallback value is not 0
74+
try this.callFallback(address(contractAddress), callValue) {
75+
} catch (bytes memory reason) {
76+
require(false, string(abi.encodePacked("checkOnlyFallbackFunction callFallback with value failed: ", reason)));
77+
}
78+
79+
require(address(contractAddress).balance == callValue * 2, "checkReceive failed: balance not equal");
80+
81+
// must failed on fallback value is 0
82+
try this.callFallback(address(contractAddress), 0) {
83+
require(false, "checkOnlyFallbackFunction callFallback without value should revert");
84+
} catch {
85+
}
86+
}
87+
88+
function checkOnlyReceiveFunction() public payable {
89+
uint256 callValue = 17;
90+
91+
OnlyReceiveFunction contractAddress = new OnlyReceiveFunction();
92+
// must success
93+
try this.callTransfer(address(contractAddress), callValue) {
94+
} catch (bytes memory reason) {
95+
require(false, string(abi.encodePacked("checkOnlyReceiveFunction callTransfer with value failed: ", reason)));
96+
}
97+
98+
require(address(contractAddress).balance == callValue, "checkReceive failed: balance not equal");
99+
100+
// must failed transfer lesser than 10
101+
try this.callTransfer(address(contractAddress), 1) {
102+
require(false, "checkOnlyReceiveFunction callTransfer without value should revert");
103+
} catch {
104+
}
105+
106+
// must failed on fallback value is over 10
107+
try this.callFallback(address(contractAddress), callValue) {
108+
require(false, "checkOnlyReceiveFunction callFallback with value should revert");
109+
} catch {
110+
}
111+
112+
require(address(contractAddress).balance == callValue, "checkReceive failed: balance not equal");
113+
114+
// must failed on fallback value is lesser than 10
115+
try this.callFallback(address(contractAddress), 1) {
116+
require(false, "checkOnlyReceiveFunction callFallback without value should revert");
117+
} catch {
118+
}
119+
}
120+
121+
function checkReceiveAndFallbackFunction() public payable {
122+
uint256 callValue = 17;
123+
ReceiveAndCallbackFunction contractAddress = new ReceiveAndCallbackFunction();
124+
// must success
125+
try this.callTransfer(address(contractAddress), callValue) {
126+
} catch (bytes memory reason) {
127+
require(false, string(abi.encodePacked("checkReceiveAndFallbackFunction callTransfer with value failed: ", reason)));
128+
}
129+
130+
require(address(contractAddress).balance == callValue, "checkReceive failed: balance not equal");
131+
132+
// must failed transfer 0
133+
try this.callTransfer(address(contractAddress), 1) {
134+
require(false, "checkReceiveAndFallbackFunction callTransfer without value should revert");
135+
} catch {
136+
}
137+
require(address(contractAddress).balance == callValue, "checkReceive failed: balance not equal");
138+
139+
uint256 callValue2 = 5;
140+
141+
// must success on fallback value is not 0
142+
try this.callFallback(address(contractAddress), callValue2) {
143+
} catch (bytes memory reason) {
144+
require(false, string(abi.encodePacked("checkReceiveAndFallbackFunction callFallback with value failed: ", reason)));
145+
}
146+
147+
require(address(contractAddress).balance == callValue + callValue2, "checkReceive failed: balance not equal");
148+
149+
// must failed on fallback value is 0
150+
try this.callFallback(address(contractAddress), 0) {
151+
require(false, "checkReceiveAndFallbackFunction callFallback without value should revert");
152+
} catch {
153+
}
154+
155+
require(address(contractAddress).balance == callValue + callValue2, "checkReceive failed2: balance not equal");
156+
}
157+
158+
function check() public payable mustHasBalance {
159+
checkOnlyFallbackFunction();
160+
checkOnlyReceiveFunction();
161+
checkReceiveAndFallbackFunction();
162+
}
163+
}

src/main/java/org/fisco/bcos/sdk/demo/contract/sol/BalanceTest.sol

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ contract BalanceTest {
9191
}
9292
}
9393

94-
return;
9594
// foreach errorAddresses
9695
for (uint256 i = 0; i < errorAddresses.length; i++) {
9796
address payable addr = errorAddresses[i];
@@ -115,6 +114,19 @@ contract BalanceTest {
115114
require(balanceBeforeSelf - balanceAfterSelf == 1, "self balance should be decreased by amount");
116115
}
117116

117+
event Info2(string info, address addr, uint256 value);
118+
function testTransferBalanceToOrigin() public mustHasBalance {
119+
uint256 balanceBefore = getBalance(tx.origin);
120+
uint256 balanceBeforeSelf = getSelfBalance();
121+
address payable origin = payable(tx.origin);
122+
origin.transfer(1);
123+
uint256 balanceAfter = getBalance(tx.origin);
124+
uint256 balanceAfterSelf = getSelfBalance();
125+
require(balanceAfter - balanceBefore == 1, "balance should be increased by amount");
126+
require(balanceBeforeSelf - balanceAfterSelf == 1, "self balance should be decreased by amount");
127+
Info2("testTransferBalanceToOrigin", tx.origin, balanceAfter);
128+
}
129+
118130
function testSelfdestruct() public payable {
119131
uint256 balanceBefore = getBalance(msg.sender);
120132
uint256 balanceBeforeSelf = getSelfBalance();
@@ -168,15 +180,14 @@ contract BalanceTest {
168180
testCallNotPayableWithValue();
169181
testDeployWithValue();
170182
testTransferBalance();
183+
testTransferBalanceToOrigin();
171184
testBaseFee();
172185
testGasPrice();
173186
testMsgValue();
187+
testTransferBalanceToPrecompiled();
174188
testSelfdestruct();
175-
176-
177189
//testSelfdestructZeroAddress();
178-
//testDeployNotPayableWithValue()
179-
//testTransferBalanceToPrecompiled();
190+
180191
/* testTransferIndelegateCall check sender
181192
testTransferToPrecompiledIndelegateCall
182193
testTransferBalance();

src/main/java/org/fisco/bcos/sdk/demo/contract/sol/ContractTestAll.sol

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,26 @@ import "./ECRecoverTest.sol";
1111
import "./DeployTreeTest.sol"; // DMC
1212
import "./BalanceTest.sol";
1313
import "./BalancePrecompiledTest.sol";
14+
import "./BalanceReceiveTest.sol";
1415

1516
contract ContractTestAll {
17+
event Info(string, uint256, uint256);
1618

1719
function checkOne(string memory name, address addr, bool needProxyCheck, uint256 callValue) private {
20+
uint256 gasBefore = gasleft();
21+
(bool success, bytes memory reason) = address(addr).call{value: callValue}(abi.encodeWithSignature("check()"));
22+
require(success, string(abi.encodePacked(name, " check failed: ", reason)));
1823

19-
try ContractTestAll(addr).check{value: callValue}() { // just a little trick to call check() in the target contract
20-
// success
21-
} catch (bytes memory reason) {
22-
revert(string(abi.encodePacked(name, " check failed: ", reason)));
23-
}
24+
uint256 gasBeforeProxy = gasleft();
2425

25-
if (!needProxyCheck) {
26-
return;
26+
if (needProxyCheck) {
27+
// use proxy to call check()
28+
Proxy proxy = new ProxyImpl(addr);
29+
(success, reason) = address(proxy).call{value: callValue}(abi.encodeWithSignature("check()"));
30+
require(success, string(abi.encodePacked(name, " proxy check failed: ", reason)));
2731
}
28-
29-
// use proxy to call check()
30-
Proxy proxy = new ProxyImpl(addr);
31-
(bool success, bytes memory reason) = address(proxy).call{value: callValue}(abi.encodeWithSignature("check()"));
32-
require(success, string(abi.encodePacked(name, " proxy check failed: ", reason)));
32+
uint256 gasAfter = gasleft();
33+
emit Info(string(abi.encodePacked(name, " gas used <non-proxy, proxy>")), gasBefore - gasBeforeProxy, gasBeforeProxy - gasAfter);
3334
}
3435

3536
function check() public payable {
@@ -43,11 +44,16 @@ contract ContractTestAll {
4344
checkOne("EventTest", address(new EventTest()), true, 0);
4445
checkOne("LibraryTest", address(new LibraryTest()), true, 0);
4546
checkOne("ProxyTest", address(new ProxyTest()), true, 0);
46-
//checkOne("StaticCall", address(new StaticCall()), true, 0);
4747
checkOne("TablePrecompiledTest", address(new TablePrecompiledTest()), true, 0);
4848
checkOne("ECRecoverTest", address(new ECRecoverTest()), true, 0);
4949
checkOne("DeployTreeTest", address(new DeployTreeTest()), true, 0); // DMC
5050
checkOne("BalanceTest", address(new BalanceTest()), true, callValue);
5151
checkOne("BalancePrecompiledTest", address(new BalancePrecompiledTest()), false, 0);
52+
checkOne("BalanceReceiveTest", address(new BalanceReceiveTest()), true, callValue);
53+
54+
checkOne("StaticCall", address(new StaticCall()), false, 0); // must at last for gas will be used up by staticcall failed
5255
}
56+
57+
// fallback
58+
fallback() external payable {}
5359
}

0 commit comments

Comments
 (0)