get dates from block header, better equality

This commit is contained in:
Craig Raw 2020-06-02 15:15:16 +02:00
parent cb18f7c4c3
commit fa30f37e23
4 changed files with 34 additions and 20 deletions

View file

@ -47,7 +47,7 @@ public class BlockHeader extends Message {
} }
public Date getTimeAsDate() { public Date getTimeAsDate() {
return new Date(time); return new Date(time * 1000);
} }
public long getDifficultyTarget() { public long getDifficultyTarget() {

View file

@ -3,11 +3,13 @@ package com.sparrowwallet.drongo.wallet;
import com.sparrowwallet.drongo.protocol.Sha256Hash; import com.sparrowwallet.drongo.protocol.Sha256Hash;
import com.sparrowwallet.drongo.protocol.Transaction; import com.sparrowwallet.drongo.protocol.Transaction;
import java.util.Date;
public class BlockTransaction extends BlockTransactionHash implements Comparable<BlockTransaction> { public class BlockTransaction extends BlockTransactionHash implements Comparable<BlockTransaction> {
private final Transaction transaction; private final Transaction transaction;
public BlockTransaction(Sha256Hash hash, int height, Long fee, Transaction transaction) { public BlockTransaction(Sha256Hash hash, int height, Date date, Long fee, Transaction transaction) {
super(hash, height, fee); super(hash, height, date, fee);
this.transaction = transaction; this.transaction = transaction;
} }

View file

@ -2,26 +2,21 @@ package com.sparrowwallet.drongo.wallet;
import com.sparrowwallet.drongo.protocol.Sha256Hash; import com.sparrowwallet.drongo.protocol.Sha256Hash;
import java.util.Date;
import java.util.Objects; import java.util.Objects;
public abstract class BlockTransactionHash { public abstract class BlockTransactionHash {
private final Sha256Hash hash; private final Sha256Hash hash;
private final int height; private final int height;
private final Date date;
private final Long fee; private final Long fee;
private String label; private String label;
public BlockTransactionHash(Sha256Hash hash) { public BlockTransactionHash(Sha256Hash hash, int height, Date date, Long fee) {
this(hash, 0, 0L);
}
public BlockTransactionHash(Sha256Hash hash, int height) {
this(hash, height, 0L);
}
public BlockTransactionHash(Sha256Hash hash, int height, Long fee) {
this.hash = hash; this.hash = hash;
this.height = height; this.height = height;
this.date = date;
this.fee = fee; this.fee = fee;
} }
@ -37,6 +32,10 @@ public abstract class BlockTransactionHash {
return height; return height;
} }
public Date getDate() {
return date;
}
public Long getFee() { public Long getFee() {
return fee; return fee;
} }

View file

@ -2,6 +2,7 @@ package com.sparrowwallet.drongo.wallet;
import com.sparrowwallet.drongo.protocol.Sha256Hash; import com.sparrowwallet.drongo.protocol.Sha256Hash;
import java.util.Date;
import java.util.Objects; import java.util.Objects;
public class BlockTransactionHashIndex extends BlockTransactionHash implements Comparable<BlockTransactionHashIndex> { public class BlockTransactionHashIndex extends BlockTransactionHash implements Comparable<BlockTransactionHashIndex> {
@ -9,12 +10,12 @@ public class BlockTransactionHashIndex extends BlockTransactionHash implements C
private final long value; private final long value;
private BlockTransactionHashIndex spentBy; private BlockTransactionHashIndex spentBy;
public BlockTransactionHashIndex(Sha256Hash hash, int height, Long fee, long index, long value) { public BlockTransactionHashIndex(Sha256Hash hash, int height, Date date, Long fee, long index, long value) {
this(hash, height, fee, index, value, null); this(hash, height, date, fee, index, value, null);
} }
public BlockTransactionHashIndex(Sha256Hash hash, int height, Long fee, long index, long value, BlockTransactionHashIndex spentBy) { public BlockTransactionHashIndex(Sha256Hash hash, int height, Date date, Long fee, long index, long value, BlockTransactionHashIndex spentBy) {
super(hash, height, fee); super(hash, height, date, fee);
this.index = index; this.index = index;
this.value = value; this.value = value;
this.spentBy = spentBy; this.spentBy = spentBy;
@ -51,12 +52,14 @@ public class BlockTransactionHashIndex extends BlockTransactionHash implements C
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false; if (!super.equals(o)) return false;
BlockTransactionHashIndex that = (BlockTransactionHashIndex) o; BlockTransactionHashIndex that = (BlockTransactionHashIndex) o;
return index == that.index; return index == that.index &&
value == that.value &&
Objects.equals(spentBy, that.spentBy);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(super.hashCode(), index); return Objects.hash(super.hashCode(), index, value, spentBy);
} }
@Override @Override
@ -66,10 +69,20 @@ public class BlockTransactionHashIndex extends BlockTransactionHash implements C
return diff; return diff;
} }
return (int)(index - reference.index); diff = (int)(index - reference.index);
if(diff != 0) {
return diff;
}
diff = (int)(value - reference.value);
if(diff != 0) {
return diff;
}
return spentBy == null ? (reference.spentBy == null ? 0 : Integer.MIN_VALUE) : (reference.spentBy == null ? Integer.MAX_VALUE : spentBy.compareTo(reference.spentBy));
} }
public BlockTransactionHashIndex copy() { public BlockTransactionHashIndex copy() {
return new BlockTransactionHashIndex(super.getHash(), super.getHeight(), super.getFee(), index, value, spentBy.copy()); return new BlockTransactionHashIndex(super.getHash(), super.getHeight(), super.getDate(), super.getFee(), index, value, spentBy == null ? null : spentBy.copy());
} }
} }