Skip to content

Commit

Permalink
Merge pull request #75 from unigrid-project/unlockstate
Browse files Browse the repository at this point in the history
Added an enum to wallet to handel unlock and lock stat
  • Loading branch information
Fim-84 authored Jun 7, 2022
2 parents d42a589 + a2451ed commit 595f19f
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class WalletController implements Initializable, PropertyChangeListener {

private Wallet wallet;

private static TransactionList transList = new TransactionList();
private TransactionList transList = new TransactionList();
private static WindowService window = WindowService.getInstance();


Expand Down Expand Up @@ -250,18 +250,18 @@ private Color setColor(int r, int g, int b, int confirmations) {

public void propertyChange(PropertyChangeEvent event) {
if (event.getPropertyName().equals(wallet.BALANCE_PROPERTY)) {
debug.log(String.format("Value: %.8f", (double) event.getNewValue()));
lblBalance.setText(String.format("%.8f", (double) event.getNewValue()));
lblBalanceSend.setText(String.format("%.8f", (double) event.getNewValue()));
debug.log("Value: " + event.getNewValue().toString());
lblBalance.setText(event.getNewValue().toString());
lblBalanceSend.setText(event.getNewValue().toString());
}
if (event.getPropertyName().equals(wallet.LOCKED_PROPERTY)) {
boolean locked = (boolean) event.getNewValue();
// can determine from this if a send transaction needs a passphrase
}
if (event.getPropertyName().equals(transList.TRANSACTION_LIST)) {
/*if (event.getPropertyName().equals(transList.TRANSACTION_LIST)) {
ObservableList<Transaction> list = transList.getLatestTransactions(10);
tblWalletTrans.setItems(list);
}
}*/
if (event.getPropertyName().equals(wallet.TRANSACTION_COUNT)) {
ObservableList<Transaction> list = transList.getLatestTransactions(10);
tblWalletTrans.setItems(list);
Expand Down
7 changes: 3 additions & 4 deletions fx/src/main/java/org/unigrid/janus/model/TransactionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ public ObservableList<Transaction> getTransactions() {

public ObservableList<Transaction> getLatestTransactions(int count) {
System.out.println(this.transactions.size());
count = this.transactions.size() < count ? this.transactions.size() : count;
return (ObservableList<Transaction>) FXCollections.observableArrayList(this.transactions.subList(0,
count));
count = count > transactions.size() ? transactions.size() : count;
return (ObservableList<Transaction>) FXCollections.observableArrayList(transactions.subList(0, count));
}

public int loadNewTransactions() {
Expand Down Expand Up @@ -114,7 +113,7 @@ public Transaction getMultiPart(Transaction trans, boolean beginning) {
public boolean addTransaction(int index, Transaction trans) {
boolean result = false;
try {
boolean beginning = (index == 0);
//boolean beginning = (index == 0);
int idx = -1; //isDuplicate(trans, beginning);
if (idx != -1) {
// TODO: check for multipart and add, or create multipart if needed.
Expand Down
57 changes: 26 additions & 31 deletions fx/src/main/java/org/unigrid/janus/model/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ of the License (see COPYING and COPYING.addendum).
import jakarta.inject.Inject;
import java.beans.PropertyChangeSupport;
import java.beans.PropertyChangeListener;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.unigrid.janus.model.rpc.entity.GetWalletInfo;
Expand All @@ -44,7 +46,7 @@ public class Wallet {
public static final String IS_OFFLINE = "offline";
public static final String STATUS_PROPERTY = "walletstatus";
public static final String TRANSACTION_COUNT = "transactioncount";
private static double balance;
private static BigDecimal balance = new BigDecimal(0);
private static double totalbalance;
private static double moneysupply;
private static double blacklisted;
Expand All @@ -68,6 +70,23 @@ public class Wallet {
@Inject
private static DebugService debug = new DebugService();
private static PropertyChangeSupport pcs;

@AllArgsConstructor
public static enum LockState {
UNLOCKED("unlocked"),
LOCKED("locked"),
UNLOCKED_FOR_STAKING("unlocked-for-staking");

@Getter private String currentState;

public static LockState from(String s) {
return switch(s) {
case "locked" -> LockState.LOCKED;
case "unlocked" -> LockState.UNLOCKED;
default -> LockState.UNLOCKED_FOR_STAKING;
};
}
}

public Wallet() {
if (this.pcs != null) {
Expand All @@ -84,12 +103,12 @@ public void removePropertyChangeListener(PropertyChangeListener listener) {
this.pcs.removePropertyChangeListener(listener);
}

public double getBalance() {
public BigDecimal getBalance() {
return this.balance;
}

public void setBalance(double newValue) {
double oldValue = this.balance;
public void setBalance(BigDecimal newValue) {
BigDecimal oldValue = this.balance;
this.balance = newValue;
this.pcs.firePropertyChange(this.BALANCE_PROPERTY, oldValue, newValue);
}
Expand Down Expand Up @@ -257,33 +276,9 @@ public void setInfo(Info newInfo) {
//debug.log(unlock);

}

public void setWalletState(GetWalletInfo walletInfo) {
long timestamp = walletInfo.getResult().getUnlockUntil();
// only 4999 == an unencrypted wallet
this.setEncrypted(timestamp != 4999);
//debug.log(String.format("ENCRYPTED: %s", getEncrypted()));
long time = timestamp;
Date date = new Date(time);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.get(Calendar.YEAR);
int yearFromDaemon = calendar.get(Calendar.YEAR);
//debug.log(String.format("Year from daemon: %s", (int) calendar.get(Calendar.YEAR)));
Date date2 = new Date(stakingStartTime);
calendar.setTime(date2);
calendar.get(Calendar.YEAR);
int yeahFromGui = calendar.get(Calendar.YEAR);
//debug.log(String.format("Year from GUI: %s", (int) calendar.get(Calendar.YEAR)));

if (yearFromDaemon >= yeahFromGui) {
this.setLocked(true);
//debug.log(String.format("Wallet Unlocked for staking only"));
} else if (timestamp > 0) {
this.setLocked(false);
} else if (timestamp == 0) {
this.setLocked(true);
}

public void setWalletState(LockState state) {
setLocked(state == LockState.LOCKED);
}

public Boolean getProcessingStatus() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
The Janus Wallet
Copyright © 2021-2022 The Unigrid Foundation
This program is free software: you can redistribute it and/or modify it under the terms of the
addended GNU Affero General Public License as published by the Free Software Foundation, version 3
of the License (see COPYING and COPYING.addendum).
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 Affero General Public License for more details.
You should have received an addended copy of the GNU Affero General Public License with this program.
If not, see <http://www.gnu.org/licenses/> and <https://github.com/unigrid-project/janus-java>.
*/

package org.unigrid.janus.model.rpc.entity;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
public class GetUnlockState extends BaseResult<GetUnlockState.Result> {

public static final String METHOD = "getunlockstate";

public static class Request extends BaseRequest {

public Request() {
super(METHOD);
}
}

@Data
public static class Result {
private String state;
private int remainingtime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ of the License (see COPYING and COPYING.addendum).
package org.unigrid.janus.model.rpc.entity;

import jakarta.json.bind.annotation.JsonbProperty;
import java.math.BigDecimal;
import lombok.Data;
import lombok.EqualsAndHashCode;

Expand All @@ -35,8 +36,8 @@ public Request() {

@Data
public static class Result {
private float balance;
private float totalbalance;
private BigDecimal balance;
private BigDecimal totalbalance;
@JsonbProperty("unlocked_until")
private long unlockUntil = 4999;
private String walletversion = "0";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ of the License (see COPYING and COPYING.addendum).
import lombok.Data;
import lombok.EqualsAndHashCode;
import jakarta.json.bind.annotation.JsonbProperty;
import java.math.BigDecimal;

@Data
@EqualsAndHashCode(callSuper = false)
Expand All @@ -42,7 +43,7 @@ public static class Result {
@JsonbProperty("protocolversion")
private int protocolVersion;
private float totalbalance;
private float balance;
private BigDecimal balance;
@JsonbProperty("unlocked_until")
private long unlockUntil = 4999;
// unpacked from nested bootstrapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ of the License (see COPYING and COPYING.addendum).
import jakarta.json.bind.JsonbBuilder;
import org.unigrid.janus.model.rpc.entity.GetBlockCount;
import org.unigrid.janus.model.rpc.entity.GetConnectionCount;
import org.unigrid.janus.model.rpc.entity.GetUnlockState;
import org.unigrid.janus.model.rpc.entity.GetWalletInfo;

public class PollingTask extends TimerTask {
Expand All @@ -47,10 +48,12 @@ public void run() {
final GetConnectionCount connCount = rpc.call(new GetConnectionCount.Request(),
GetConnectionCount.class);
final StakingStatus staking = rpc.call(new StakingStatus.Request(), StakingStatus.class);
final GetUnlockState unlockState = rpc.call(new GetUnlockState.Request(), GetUnlockState.class);
wallet.setBalance(walletInfo.getResult().getTotalbalance());
wallet.setBlocks(Integer.parseInt(blockCount.getResult().toString()));
wallet.setConnections(Integer.parseInt(connCount.getResult().toString()));
wallet.setWalletState(walletInfo);
//wallet.setWalletState(Wallet.LockState(unlockState.getResult().getState());
wallet.setWalletState(Wallet.LockState.from(unlockState.getResult().getState()));
wallet.setStakingStatus(staking);
wallet.setTransactionCount(walletInfo.getResult().getTxcount());
wallet.setStatus("done");
Expand Down

0 comments on commit 595f19f

Please sign in to comment.