Skip to content

Commit

Permalink
#56 modify contract code, add voting function of contract to votingma…
Browse files Browse the repository at this point in the history
…inFragment
  • Loading branch information
chad1994 committed Nov 10, 2018
1 parent c20f4ef commit 1a8e0f7
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 37 deletions.
15 changes: 14 additions & 1 deletion app/src/main/assets/solidity/Election.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ contract Election {
string name;
}

struct VoteRecord{
uint election_id;
bool isvoted;
}

//초기 토큰 공급량.
// uint public decimals = 2; //자리수
// uint public INITIAL_SUPPLY = 10000 * (10 ** decimals);

//
mapping(address => VoteRecord) public VoteRecords;

// 투표 유무 저장
mapping(address => bool) public voters;

Expand Down Expand Up @@ -60,11 +68,16 @@ contract Election {

function vote (uint _candidateId) public {
// require that they haven't voted before
require(!voters[msg.sender]);

// require(!voters[msg.sender]);
require(!VoteRecords[msg.sender].isvoted);

// require a valid candidate
require(_candidateId > 0 && _candidateId <= candidatesCount);


VoteRecords[msg.sender].election_id= candidates[_candidateId].electionId;
VoteRecords[msg.sender].isvoted = true;
// record that voter has voted
voters[msg.sender] = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,22 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
web3j = Web3jFactory.build(new HttpService("https://ropsten.infura.io/v3/de770d2ce1834cc794cfd6dfe42fb83d"));//해당 컨트렉트 주소로 연결
credentials = getCredentialsFromPrivateKey(); //개인키를 통한 자격 획득.

// new Thread() {
// public void run() {
// try {
// String contract_add = deployContract(web3j, credentials);
// Log.d("@@@Contract_add", "/" + contract_add);
// } catch (
// Exception e)
// {
// e.printStackTrace();
// }
// }
// }.start();
new Thread() {
public void run() {
try {
String contract_add = deployContract(web3j, credentials);
Log.d("@@@Contract_add", "/" + contract_add);
} catch (
Exception e)
{
e.printStackTrace();
}
}
}.start();


// election = loadContract(CONTRACT_ADDRESS, web3j, credentials); //컨트랙트 주소, web3, 자격을 통한 컨트렉트 로딩.

election = loadContract(CONTRACT_ADDRESS, web3j, credentials); //컨트랙트 주소, web3, 자격을 통한 컨트렉트 로딩.
//
// Credentials myCredential = null;
// try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* <p>Generated with web3j version 3.6.0.
*/
public class Election extends Contract {
private static final String BINARY = "608060405234801561001057600080fd5b5060408051808201909152600c81527f3139eb8c8020eb8c80ec84a000000000000000000000000000000000000000006020820152610057906401000000006101c2810204565b60408051808201909152600b81527f312eed998deca480ed919c000000000000000000000000000000000000000000602082015261009f906001640100000000610212810204565b60408051808201909152600b81527f322eebacb8ec9eacec9db800000000000000000000000000000000000000000060208201526100e7906001640100000000610212810204565b60408051808201909152600c81527f3230eb8c8020eb8c80ec84a00000000000000000000000000000000000000000602082015261012d906401000000006101c2810204565b60408051808201909152600b81527f312eeab980ecb2a0ec88980000000000000000000000000000000000000000006020820152610175906002640100000000610212810204565b60408051808201909152600b81527f322eec9db4eba7b9eab5ac00000000000000000000000000000000000000000060208201526101bd906002640100000000610212810204565b61031f565b6004805460019081019182905560408051808201825283815260208082018681526000958652600282529290942081518155915180519194929361020c9390850192910190610284565b50505050565b60038054600190810191829055604080516080810182528381526020808201878152600083850181905260608401889052958652848252929094208151815591518051919492936102699390850192910190610284565b50604082015160028201556060909101516003909101555050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102c557805160ff19168380011785556102f2565b828001600101855582156102f2579182015b828111156102f25782518255916020019190600101906102d7565b506102fe929150610302565b5090565b61031c91905b808211156102fe5760008155600101610308565b90565b6108198061032e6000396000f3006080604052600436106100a35763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416630121b93f81146100a85780632d35a8a2146100c25780633477ee2e146100e9578063358b9d971461018e5780635e6fef01146101e7578063997d28301461027e5780639d48419c14610293578063a3ec138d146102ab578063e23cec7b146102ed578063fef169e914610348575b600080fd5b3480156100b457600080fd5b506100c06004356103d5565b005b3480156100ce57600080fd5b506100d7610442565b60408051918252519081900360200190f35b3480156100f557600080fd5b50610101600435610448565b6040518085815260200180602001848152602001838152602001828103825285818151815260200191508051906020019080838360005b83811015610150578181015183820152602001610138565b50505050905090810190601f16801561017d5780820380516001836020036101000a031916815260200191505b509550505050505060405180910390f35b34801561019a57600080fd5b506040805160206004803580820135601f81018490048402850184019095528484526100c09436949293602493928401919081908401838280828437509497506104fa9650505050505050565b3480156101f357600080fd5b506101ff60043561054a565b6040518083815260200180602001828103825283818151815260200191508051906020019080838360005b8381101561024257818101518382015260200161022a565b50505050905090810190601f16801561026f5780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b34801561028a57600080fd5b506100d76105ef565b34801561029f57600080fd5b506100d76004356105f5565b3480156102b757600080fd5b506102d973ffffffffffffffffffffffffffffffffffffffff60043516610629565b604080519115158252519081900360200190f35b3480156102f957600080fd5b506040805160206004803580820135601f81018490048402850184019095528484526100c0943694929360249392840191908190840183828082843750949750509335945061063e9350505050565b34801561035457600080fd5b506103606004356106b0565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561039a578181015183820152602001610382565b50505050905090810190601f1680156103c75780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b3360009081526020819052604090205460ff16156103f257600080fd5b60008111801561040457506003548111155b151561040f57600080fd5b33600090815260208181526040808320805460ff1916600190811790915593835290839052902060020180549091019055565b60035481565b600160208181526000928352604092839020805481840180548651600296821615610100026000190190911695909504601f810185900485028601850190965285855290949193929091908301828280156104e45780601f106104b9576101008083540402835291602001916104e4565b820191906000526020600020905b8154815290600101906020018083116104c757829003601f168201915b5050505050908060020154908060030154905084565b600480546001908101918290556040805180820182528381526020808201868152600095865260028252929094208151815591518051919492936105449390850192910190610752565b50505050565b60026020818152600092835260409283902080546001808301805487519281161561010002600019011695909504601f81018590048502820185019096528581529094919390929091908301828280156105e55780601f106105ba576101008083540402835291602001916105e5565b820191906000526020600020905b8154815290600101906020018083116105c857829003601f168201915b5050505050905082565b60045481565b6000808211801561060857506003548211155b151561061357600080fd5b5060009081526001602052604090206002015490565b60006020819052908152604090205460ff1681565b60038054600190810191829055604080516080810182528381526020808201878152600083850181905260608401889052958652848252929094208151815591518051919492936106959390850192910190610752565b50604082015160028201556060909101516003909101555050565b6000818152600260208181526040928390206001908101805485519281161561010002600019011693909304601f81018390048302820183019094528381526060939092918301828280156107465780601f1061071b57610100808354040283529160200191610746565b820191906000526020600020905b81548152906001019060200180831161072957829003601f168201915b50505050509050919050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061079357805160ff19168380011785556107c0565b828001600101855582156107c0579182015b828111156107c05782518255916020019190600101906107a5565b506107cc9291506107d0565b5090565b6107ea91905b808211156107cc57600081556001016107d6565b905600a165627a7a723058201dd46614ba92d78e7c0a5398ea74a9b7003e590d2d438d38f536f88737232c390029";
private static final String BINARY = "608060405234801561001057600080fd5b5060408051808201909152600c81527f3139eb8c8020eb8c80ec84a000000000000000000000000000000000000000006020820152610057906401000000006101c2810204565b60408051808201909152600b81527f312eed998deca480ed919c000000000000000000000000000000000000000000602082015261009f906001640100000000610212810204565b60408051808201909152600b81527f322eebacb8ec9eacec9db800000000000000000000000000000000000000000060208201526100e7906001640100000000610212810204565b60408051808201909152600c81527f3230eb8c8020eb8c80ec84a00000000000000000000000000000000000000000602082015261012d906401000000006101c2810204565b60408051808201909152600b81527f312eeab980ecb2a0ec88980000000000000000000000000000000000000000006020820152610175906002640100000000610212810204565b60408051808201909152600b81527f322eec9db4eba7b9eab5ac00000000000000000000000000000000000000000060208201526101bd906002640100000000610212810204565b610320565b6005805460019081019182905560408051808201825283815260208082018681526000958652600382529290942081518155915180519194929361020c9390850192910190610285565b50505050565b60048054600190810191829055604080516080810182528381526020808201878152600083850181905260608401889052958652600282529290942081518155915180519194929361026a9390850192910190610285565b50604082015160028201556060909101516003909101555050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102c657805160ff19168380011785556102f3565b828001600101855582156102f3579182015b828111156102f35782518255916020019190600101906102d8565b506102ff929150610303565b5090565b61031d91905b808211156102ff5760008155600101610309565b90565b6108c880620003306000396000f3006080604052600436106100ae5763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416630121b93f81146100b35780632d35a8a2146100cd5780633477ee2e146100f4578063358b9d97146101995780635e6fef01146101f257806378f160fb14610289578063997d2830146102d05780639d48419c146102e5578063a3ec138d146102fd578063e23cec7b1461033f578063fef169e91461039a575b600080fd5b3480156100bf57600080fd5b506100cb600435610427565b005b3480156100d957600080fd5b506100e26104bc565b60408051918252519081900360200190f35b34801561010057600080fd5b5061010c6004356104c2565b6040518085815260200180602001848152602001838152602001828103825285818151815260200191508051906020019080838360005b8381101561015b578181015183820152602001610143565b50505050905090810190601f1680156101885780820380516001836020036101000a031916815260200191505b509550505050505060405180910390f35b3480156101a557600080fd5b506040805160206004803580820135601f81018490048402850184019095528484526100cb9436949293602493928401919081908401838280828437509497506105739650505050505050565b3480156101fe57600080fd5b5061020a6004356105c3565b6040518083815260200180602001828103825283818151815260200191508051906020019080838360005b8381101561024d578181015183820152602001610235565b50505050905090810190601f16801561027a5780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b34801561029557600080fd5b506102b773ffffffffffffffffffffffffffffffffffffffff6004351661066a565b6040805192835290151560208301528051918290030190f35b3480156102dc57600080fd5b506100e2610686565b3480156102f157600080fd5b506100e260043561068c565b34801561030957600080fd5b5061032b73ffffffffffffffffffffffffffffffffffffffff600435166106c1565b604080519115158252519081900360200190f35b34801561034b57600080fd5b506040805160206004803580820135601f81018490048402850184019095528484526100cb94369492936024939284019190819084018382808284375094975050933594506106d69350505050565b3480156103a657600080fd5b506103b2600435610749565b6040805160208082528351818301528351919283929083019185019080838360005b838110156103ec5781810151838201526020016103d4565b50505050905090810190601f1680156104195780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b3360009081526020819052604090206001015460ff161561044757600080fd5b60008111801561045957506004548111155b151561046457600080fd5b600081815260026020818152604080842060038101543386528584528286209081556001908101805460ff19908116831790915581855292862080549093168117909255949093528190529190910180549091019055565b60045481565b60026020818152600092835260409283902080546001808301805487519281161561010002600019011695909504601f810185900485028201850190965285815290949193909290919083018282801561055d5780601f106105325761010080835404028352916020019161055d565b820191906000526020600020905b81548152906001019060200180831161054057829003601f168201915b5050505050908060020154908060030154905084565b600580546001908101918290556040805180820182528381526020808201868152600095865260038252929094208151815591518051919492936105bd9390850192910190610801565b50505050565b6003602090815260009182526040918290208054600180830180548651600293821615610100026000190190911692909204601f8101869004860283018601909652858252919492939092908301828280156106605780601f1061063557610100808354040283529160200191610660565b820191906000526020600020905b81548152906001019060200180831161064357829003601f168201915b5050505050905082565b6000602081905290815260409020805460019091015460ff1682565b60055481565b6000808211801561069f57506004548211155b15156106aa57600080fd5b506000908152600260208190526040909120015490565b60016020526000908152604090205460ff1681565b60048054600190810191829055604080516080810182528381526020808201878152600083850181905260608401889052958652600282529290942081518155915180519194929361072e9390850192910190610801565b50604082015160028201556060909101516003909101555050565b6060600360008381526020019081526020016000206001018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156107f55780601f106107ca576101008083540402835291602001916107f5565b820191906000526020600020905b8154815290600101906020018083116107d857829003601f168201915b50505050509050919050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061084257805160ff191683800117855561086f565b8280016001018555821561086f579182015b8281111561086f578251825591602001919060010190610854565b5061087b92915061087f565b5090565b61089991905b8082111561087b5760008155600101610885565b905600a165627a7a723058208851861ff8c096672a2cf8b6819e22f456b575bd0812b6c55149e9afdef658770029";

public static final String FUNC_VOTE = "vote";

Expand All @@ -42,6 +42,8 @@ public class Election extends Contract {

public static final String FUNC_ELECTIONS = "elections";

public static final String FUNC_VOTERECORDS = "VoteRecords";

public static final String FUNC_ELECTIONCOUNT = "electionCount";

public static final String FUNC_GETVOTECOUNT = "getvoteCount";
Expand Down Expand Up @@ -123,6 +125,22 @@ public Tuple2<BigInteger, String> call() throws Exception {
});
}

public RemoteCall<Tuple2<BigInteger, Boolean>> VoteRecords(String param0) {
final Function function = new Function(FUNC_VOTERECORDS,
Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(param0)),
Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}, new TypeReference<Bool>() {}));
return new RemoteCall<Tuple2<BigInteger, Boolean>>(
new Callable<Tuple2<BigInteger, Boolean>>() {
@Override
public Tuple2<BigInteger, Boolean> call() throws Exception {
List<Type> results = executeCallMultipleValueReturn(function);
return new Tuple2<BigInteger, Boolean>(
(BigInteger) results.get(0).getValue(),
(Boolean) results.get(1).getValue());
}
});
}

public RemoteCall<BigInteger> electionCount() {
final Function function = new Function(FUNC_ELECTIONCOUNT,
Arrays.<Type>asList(),
Expand Down Expand Up @@ -161,6 +179,7 @@ public RemoteCall<String> getelectionName(BigInteger _electionId) {
}



@Deprecated
public static RemoteCall<Election> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return deployRemoteCall(Election.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");
Expand All @@ -181,5 +200,4 @@ public static Election load(String contractAddress, Web3j web3j, TransactionMana
return new Election(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ public class FingerprintFragment extends Fragment {
private TextView textView;
private int candidateId; //TODO: 체크된 후보자 id 가져와서 입력


public static FingerprintFragment newInstance(int candidateId) {

Bundle args = new Bundle();

FingerprintFragment fragment = new FingerprintFragment();
fragment.setArguments(args);
fragment.candidateId = candidateId;
return fragment;
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Expand Down Expand Up @@ -85,7 +96,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c

if (cipherInit()) {
FingerprintManager.CryptoObject cryptoObject = new FingerprintManager.CryptoObject(cipher);
FingerprintHandler helper = new FingerprintHandler(getContext());
FingerprintHandler helper = new FingerprintHandler(getContext(),candidateId);
helper.startAuth(fingerprintManager, cryptoObject);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,21 @@
import android.os.CancellationSignal;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.example.choejun_yeong.blocker_android.R;
import com.example.choejun_yeong.blocker_android.util.ContractUtil;

import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tuples.generated.Tuple2;

import java.math.BigInteger;

import rx.functions.Func1;
import rx.schedulers.Schedulers;


/**
* Created by whit3hawks on 11/16/16.
Expand All @@ -27,8 +37,9 @@ public class FingerprintHandler extends FingerprintManager.AuthenticationCallbac


// Constructor
public FingerprintHandler(Context mContext) {
public FingerprintHandler(Context mContext,int candidateId) {
context = mContext;
this.candidateId = candidateId;
contractUtil = new ContractUtil(context);
}

Expand Down Expand Up @@ -62,8 +73,22 @@ public void onAuthenticationFailed() {

@Override
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
this.update("지문인증 완료.", true);

this.update("지문인증 완료.\n투표완료까지 잠시 기다려 주세요.", true);

contractUtil.voting(candidateId)
.subscribeOn(Schedulers.computation())
.observeOn(rx.android.schedulers.AndroidSchedulers.mainThread())
.onErrorReturn(new Func1<Throwable, TransactionReceipt>() {
@Override
public TransactionReceipt call(Throwable throwable) {
Log.d("@@@ERROR in Voting", throwable.getMessage());
return null;
}
})
.subscribe(x -> {
Toast.makeText(context, "투표 완료 ", Toast.LENGTH_SHORT).show();
this.update("투표가 완료 되었습니다.\n이전 화면으로 돌아가셔도 좋습니다.",true);
});
// contractUtil.voting() TODO: 후보자 id 가져와서 voting기능 구현.
}

Expand Down
Loading

0 comments on commit 1a8e0f7

Please sign in to comment.