Coverage Summary for Class: ReleaseRequestQueue (co.rsk.peg)
Class |
Method, %
|
Line, %
|
ReleaseRequestQueue |
0%
(0/7)
|
0%
(0/18)
|
ReleaseRequestQueue$Entry |
0%
(0/7)
|
0%
(0/16)
|
Total |
0%
(0/14)
|
0%
(0/34)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2017 RSK Labs Ltd.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package co.rsk.peg;
20
21 import co.rsk.bitcoinj.core.Address;
22 import co.rsk.bitcoinj.core.Coin;
23 import co.rsk.crypto.Keccak256;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.ListIterator;
28 import java.util.Objects;
29 import java.util.stream.Collectors;
30
31 /**
32 * Representation of a queue of btc release
33 * requests waiting to be processed by the bridge.
34 *
35 * @author Ariel Mendelzon
36 */
37 public class ReleaseRequestQueue {
38 public static class Entry {
39 private Address destination;
40 private Coin amount;
41 private Keccak256 rskTxHash;
42
43 public Entry(Address destination, Coin amount, Keccak256 rskTxHash) {
44 this.destination = destination;
45 this.amount = amount;
46 this.rskTxHash = rskTxHash;
47 }
48
49 public Entry(Address destination, Coin amount) {
50 this(destination, amount, null);
51 }
52
53 public Address getDestination() {
54 return destination;
55 }
56
57 public Coin getAmount() {
58 return amount;
59 }
60
61 public Keccak256 getRskTxHash() {
62 return rskTxHash;
63 }
64
65 @Override
66 public boolean equals(Object o) {
67 if (o == null || this.getClass() != o.getClass()) {
68 return false;
69 }
70
71 Entry otherEntry = (Entry) o;
72
73 return otherEntry.getDestination().equals(getDestination()) &&
74 otherEntry.getAmount().equals(getAmount()) &&
75 (otherEntry.getRskTxHash() == null && getRskTxHash() == null ||
76 otherEntry.getRskTxHash() != null && otherEntry.getRskTxHash().equals(getRskTxHash()));
77
78 }
79
80 @Override
81 public int hashCode() {
82 return Objects.hash(this.getDestination(), this.getAmount());
83 }
84 }
85
86 public interface Processor {
87 boolean process(Entry entry);
88 }
89
90 private List<Entry> entries;
91
92 public ReleaseRequestQueue(List<Entry> entries) {
93 this.entries = new ArrayList<>(entries);
94 }
95
96 public List<Entry> getEntriesWithoutHash() {
97 return entries.stream().filter((entry) -> entry.getRskTxHash() == null).collect(Collectors.toList());
98 }
99
100 public List<Entry> getEntriesWithHash() {
101 return entries.stream().filter((entry) -> entry.getRskTxHash() != null).collect(Collectors.toList());
102 }
103
104 public List<Entry> getEntries() {
105 return new ArrayList<>(entries);
106 }
107
108 public void add(Address destination, Coin amount, Keccak256 rskTxHash) {
109 entries.add(new Entry(destination, amount, rskTxHash));
110 }
111
112 public void add(Address destination, Coin amount) {
113 entries.add(new Entry(destination, amount, null));
114 }
115
116 /**
117 * This methods iterates the requests in the queue
118 * and calls the processor for each. If the
119 * processor returns true, then the item is removed
120 * (i.e., processing was successful). Otherwise it is
121 * sent to the back of the queue for future processing.
122 */
123 public void process(int maxIterations, Processor processor) {
124 ListIterator<Entry> iterator = entries.listIterator();
125 List<Entry> toRetry = new ArrayList<>();
126 int i = 0;
127 while (iterator.hasNext() && i < maxIterations) {
128 Entry entry = iterator.next();
129 iterator.remove();
130 ++i;
131
132 if (!processor.process(entry)) {
133 toRetry.add(entry);
134 }
135 }
136
137 entries.addAll(toRetry);
138 }
139 }