-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #228 from kyonRay/main
Release v3.9.0
- Loading branch information
Showing
6 changed files
with
334 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/main/java/org/fisco/bcos/sdk/demo/contract/sol/AnonymousVotingInterface.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright (C) @2014-2022 Webank | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity >=0.6.10 <0.8.20; | ||
pragma experimental ABIEncoderV2; | ||
|
||
import "./ZkpPrecompiled.sol"; | ||
import "./WedprUtils.sol"; | ||
import "./Crypto.sol"; | ||
|
||
struct SystemParameters { | ||
int256 itemId; // the itemId | ||
string itemMeta; // item meta data | ||
string[] candidates; | ||
bytes hpoint; | ||
} | ||
|
||
struct Ballot{ | ||
string candidateId; | ||
bytes cipher1; | ||
bytes cipher2; | ||
bool isAssigned; | ||
} | ||
// the ballot proof | ||
struct BallotProof{ | ||
bytes formatProof; | ||
bytes eitherEqualityProof; | ||
Ballot ballot; | ||
} | ||
|
||
struct UnlistedBallot | ||
{ | ||
bytes cipher1; | ||
bytes cipher2; | ||
Ballot ballot; | ||
bool isAssigned; | ||
} | ||
// the candiate ballot proof | ||
struct UnlistedCandidateBallotProof | ||
{ | ||
UnlistedBallot ballot; | ||
BallotProof unlisted_ballot_proof; | ||
} | ||
struct VoteRequest{ | ||
Ballot blankBallot; | ||
Ballot zeroBallot; | ||
// bytes publicKey; | ||
// bytes32 signatureR; | ||
// bytes32 signatureS; | ||
} | ||
struct VoteStorage{ | ||
bool isAssigned; | ||
VoteRequest voteRequest; | ||
string[] candidateListForBallot; | ||
bytes[] cipherCandidateListForUnlistedBallot; | ||
} | ||
struct CounterStorage | ||
{ | ||
// 计票公钥分片 | ||
bytes hpointShare; | ||
// 计票公钥是否被聚合 | ||
bool setted; | ||
// 计票服务状态,1:计票中,2:计票完成 | ||
int8 counterStatus; | ||
// 计票服务实例的心跳时间 | ||
uint256 updateTime; | ||
// 一个投票item中投票密文的聚合值 | ||
string voteStorageSum; | ||
// 使用acv-core的公钥加密的计票服务sm4密钥 | ||
string sm4SecretKeyCipherText; | ||
// 使用计票服务的sm4密钥加密的计票分片 | ||
string countingPartResultCipherText; | ||
// 计票分片哈希值 | ||
bytes decryptedPartResultHash; | ||
} | ||
|
||
contract AnonymousVotingInterface { | ||
function initialize(bytes memory basePointG1, bytes memory basePointG2) public virtual; | ||
function setSystemParameters(int256 itemId, string memory itemMeta, string[] memory candidates, bytes memory hpoint) public virtual; | ||
function getSystemParameters() public view virtual returns(SystemParameters memory); | ||
function setCounterNumber(uint256 counterNumber) public virtual; | ||
function getCounterNumber() public view virtual returns(uint256); | ||
function setContractState(uint8 state) public virtual; | ||
function getContractState() public view virtual returns(uint8); | ||
function setVoterIdList(string[] memory voterIdList) public virtual; | ||
function getVoterIdList() public view virtual returns(string[] memory); | ||
function setVoterIdToVoteStorage(string memory voterId, VoteStorage memory voteStorage) public virtual; | ||
function getVoterIdToVoteStorage(string memory voterId) public view virtual returns(VoteStorage memory); | ||
function setVoterIDToCandidateIDToBallot(string memory voterId, string memory candidateId, Ballot memory ballot) public virtual; | ||
function getVoterIDToCandidateIDToBallot(string memory voterId, string memory candidateId) public view virtual returns(Ballot memory); | ||
function setVoterIDToCandidateIDToUnlistedBallot(string memory voterId, bytes memory candidateId, UnlistedBallot memory unlistedBallot) public virtual; | ||
function getVoterIDToCandidateIDToUnlistedBallot(string memory voterId, bytes memory candidateId) public view virtual returns(UnlistedBallot memory); | ||
function setCounterIdToCounterStorage(string memory counterId, CounterStorage memory counterStorage) public virtual; | ||
function getCounterIdToCounterStorage(string memory counterId) public view virtual returns(CounterStorage memory); | ||
function setCounterStatus(string memory counterId, int8 status) public virtual; | ||
function getCounterStatus(string memory counterId) public view virtual returns(int8); | ||
function setCounterUpdateTime(string memory counterId, uint256 updateTime) public virtual; | ||
function getCounterUpdateTime(string memory counterId) public view virtual returns(uint256); | ||
function setVoteStorageSum(string memory counterId, string memory voteStorageSum) public virtual; | ||
function getVoteStorageSum(string memory counterId) public view virtual returns(string memory); | ||
function setSm4SecretKeyCipherText(string memory counterId, string memory sm4SecretKeyCipherText) public virtual; | ||
} |
163 changes: 163 additions & 0 deletions
163
src/main/java/org/fisco/bcos/sdk/demo/contract/sol/BalanceReceiveTest.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
pragma solidity ^0.6.0; | ||
pragma experimental ABIEncoderV2; | ||
|
||
contract ReceiveAndCallbackFunction { | ||
event Info(string, uint256); | ||
// receive function | ||
receive() external payable { | ||
require(msg.value > 10, "receive() msg.value is zero"); | ||
emit Info("receive() msg.value", msg.value); | ||
} | ||
|
||
// fallback function | ||
fallback() external payable { | ||
require(msg.value > 0, "fallback() msg.value is zero"); | ||
emit Info("fallback() msg.value", msg.value); | ||
} | ||
} | ||
|
||
contract OnlyReceiveFunction { | ||
event Info(string, uint256); | ||
// receive function | ||
receive() external payable { | ||
require(msg.value > 10, "receive() msg.value is zero"); | ||
emit Info("receive() msg.value", msg.value); | ||
} | ||
} | ||
|
||
contract OnlyFallbackFunction { | ||
|
||
event Info(string, uint256); | ||
|
||
// fallback function | ||
fallback() external payable { | ||
require(msg.value > 0, "fallback() msg.value is zero"); | ||
emit Info("fallback() msg.value", msg.value); | ||
} | ||
} | ||
|
||
|
||
contract BalanceReceiveTest { | ||
modifier mustHasBalance() { | ||
require(address(this).balance > 0, "balance not enough"); | ||
_; | ||
} | ||
|
||
function callTransfer(address payable to, uint256 amount) public payable { | ||
to.transfer(amount); | ||
} | ||
|
||
function callFallback(address payable to, uint256 amount) public payable { | ||
(bool success, bytes memory reason) = to.call{value: amount}("aaa()"); | ||
require(success, string(abi.encodePacked("callFallback failed: ", reason))); | ||
} | ||
|
||
function checkOnlyFallbackFunction() public payable { | ||
uint256 callValue = 7; | ||
|
||
OnlyFallbackFunction contractAddress = new OnlyFallbackFunction(); | ||
// must success | ||
try this.callTransfer(address(contractAddress), callValue) { | ||
} catch (bytes memory reason) { | ||
require(false, string(abi.encodePacked("checkOnlyFallbackFunction callTransfer with value failed: ", reason))); | ||
} | ||
|
||
require(address(contractAddress).balance == callValue, "checkReceive failed: balance not equal"); | ||
|
||
// must failed transfer 0 | ||
try this.callTransfer(address(contractAddress), 0) { | ||
require(false, "checkOnlyFallbackFunction callTransfer without value should revert"); | ||
} catch { | ||
} | ||
|
||
// must success on fallback value is not 0 | ||
try this.callFallback(address(contractAddress), callValue) { | ||
} catch (bytes memory reason) { | ||
require(false, string(abi.encodePacked("checkOnlyFallbackFunction callFallback with value failed: ", reason))); | ||
} | ||
|
||
require(address(contractAddress).balance == callValue * 2, "checkReceive failed: balance not equal"); | ||
|
||
// must failed on fallback value is 0 | ||
try this.callFallback(address(contractAddress), 0) { | ||
require(false, "checkOnlyFallbackFunction callFallback without value should revert"); | ||
} catch { | ||
} | ||
} | ||
|
||
function checkOnlyReceiveFunction() public payable { | ||
uint256 callValue = 17; | ||
|
||
OnlyReceiveFunction contractAddress = new OnlyReceiveFunction(); | ||
// must success | ||
try this.callTransfer(address(contractAddress), callValue) { | ||
} catch (bytes memory reason) { | ||
require(false, string(abi.encodePacked("checkOnlyReceiveFunction callTransfer with value failed: ", reason))); | ||
} | ||
|
||
require(address(contractAddress).balance == callValue, "checkReceive failed: balance not equal"); | ||
|
||
// must failed transfer lesser than 10 | ||
try this.callTransfer(address(contractAddress), 1) { | ||
require(false, "checkOnlyReceiveFunction callTransfer without value should revert"); | ||
} catch { | ||
} | ||
|
||
// must failed on fallback value is over 10 | ||
try this.callFallback(address(contractAddress), callValue) { | ||
require(false, "checkOnlyReceiveFunction callFallback with value should revert"); | ||
} catch { | ||
} | ||
|
||
require(address(contractAddress).balance == callValue, "checkReceive failed: balance not equal"); | ||
|
||
// must failed on fallback value is lesser than 10 | ||
try this.callFallback(address(contractAddress), 1) { | ||
require(false, "checkOnlyReceiveFunction callFallback without value should revert"); | ||
} catch { | ||
} | ||
} | ||
|
||
function checkReceiveAndFallbackFunction() public payable { | ||
uint256 callValue = 17; | ||
ReceiveAndCallbackFunction contractAddress = new ReceiveAndCallbackFunction(); | ||
// must success | ||
try this.callTransfer(address(contractAddress), callValue) { | ||
} catch (bytes memory reason) { | ||
require(false, string(abi.encodePacked("checkReceiveAndFallbackFunction callTransfer with value failed: ", reason))); | ||
} | ||
|
||
require(address(contractAddress).balance == callValue, "checkReceive failed: balance not equal"); | ||
|
||
// must failed transfer 0 | ||
try this.callTransfer(address(contractAddress), 1) { | ||
require(false, "checkReceiveAndFallbackFunction callTransfer without value should revert"); | ||
} catch { | ||
} | ||
require(address(contractAddress).balance == callValue, "checkReceive failed: balance not equal"); | ||
|
||
uint256 callValue2 = 5; | ||
|
||
// must success on fallback value is not 0 | ||
try this.callFallback(address(contractAddress), callValue2) { | ||
} catch (bytes memory reason) { | ||
require(false, string(abi.encodePacked("checkReceiveAndFallbackFunction callFallback with value failed: ", reason))); | ||
} | ||
|
||
require(address(contractAddress).balance == callValue + callValue2, "checkReceive failed: balance not equal"); | ||
|
||
// must failed on fallback value is 0 | ||
try this.callFallback(address(contractAddress), 0) { | ||
require(false, "checkReceiveAndFallbackFunction callFallback without value should revert"); | ||
} catch { | ||
} | ||
|
||
require(address(contractAddress).balance == callValue + callValue2, "checkReceive failed2: balance not equal"); | ||
} | ||
|
||
function check() public payable mustHasBalance { | ||
checkOnlyFallbackFunction(); | ||
checkOnlyReceiveFunction(); | ||
checkReceiveAndFallbackFunction(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.