-
Notifications
You must be signed in to change notification settings - Fork 5
/
UTXO.java
executable file
·91 lines (81 loc) · 2.53 KB
/
UTXO.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.util.Arrays;
public class UTXO implements Comparable<UTXO> {
/** Hash of the transaction from which this UTXO originates */
private byte[] txHash;
/** Index of the corresponding output in said transaction */
private int index;
/**
* Creates a new UTXO corresponding to the output with index <index> in the transaction whose
* hash is {@code txHash}
*/
public UTXO(byte[] txHash, int index) {
this.txHash = Arrays.copyOf(txHash, txHash.length);
this.index = index;
}
/** @return the transaction hash of this UTXO */
public byte[] getTxHash() {
return txHash;
}
/** @return the index of this UTXO */
public int getIndex() {
return index;
}
/**
* Compares this UTXO to the one specified by {@code other}, considering them equal if they have
* {@code txHash} arrays with equal contents and equal {@code index} values
*/
public boolean equals(Object other) {
if (other == null) {
return false;
}
if (getClass() != other.getClass()) {
return false;
}
UTXO utxo = (UTXO) other;
byte[] hash = utxo.txHash;
int in = utxo.index;
if (hash.length != txHash.length || index != in)
return false;
for (int i = 0; i < hash.length; i++) {
if (hash[i] != txHash[i])
return false;
}
return true;
}
/**
* Simple implementation of a UTXO hashCode that respects equality of UTXOs // (i.e.
* utxo1.equals(utxo2) => utxo1.hashCode() == utxo2.hashCode())
*/
public int hashCode() {
int hash = 1;
hash = hash * 17 + index;
hash = hash * 31 + Arrays.hashCode(txHash);
return hash;
}
/** Compares this UTXO to the one specified by {@code utxo} */
public int compareTo(UTXO utxo) {
byte[] hash = utxo.txHash;
int in = utxo.index;
if (in > index)
return -1;
else if (in < index)
return 1;
else {
int len1 = txHash.length;
int len2 = hash.length;
if (len2 > len1)
return -1;
else if (len2 < len1)
return 1;
else {
for (int i = 0; i < len1; i++) {
if (hash[i] > txHash[i])
return -1;
else if (hash[i] < txHash[i])
return 1;
}
return 0;
}
}
}
}