Coverage Summary for Class: BlocksBloomEncoder (co.rsk.logfilter)
Class |
Class, %
|
Method, %
|
Line, %
|
BlocksBloomEncoder |
0%
(0/1)
|
0%
(0/6)
|
0%
(0/15)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2017 RSK Labs Ltd.
4 * (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>)
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package co.rsk.logfilter;
21
22 import org.bouncycastle.util.BigIntegers;
23 import org.ethereum.core.Bloom;
24 import org.ethereum.util.RLP;
25 import org.ethereum.util.RLPList;
26
27 import java.math.BigInteger;
28 import java.util.Arrays;
29
30 /**
31 * Created by ajlopez on 19/02/2020.
32 */
33 public class BlocksBloomEncoder {
34 private static final Bloom emptyBloom = new Bloom();
35
36 private BlocksBloomEncoder() {
37
38 }
39
40 public static byte[] encode(BlocksBloom blocksBloom) {
41 byte[] rlpFrom = encodeLong(blocksBloom.fromBlock());
42 byte[] rlpTo = encodeLong(blocksBloom.toBlock());
43 byte[] rlpData = RLP.encodeElement(blocksBloom.getBloom().getData());
44
45 return RLP.encodeList(rlpFrom, rlpTo, rlpData);
46 }
47
48 public static BlocksBloom decode(byte[] data) {
49 RLPList list = (RLPList) RLP.decode2(data).get(0);
50
51 long from = decodeLong(list.get(0).getRLPData());
52 long to = decodeLong(list.get(1).getRLPData());
53 Bloom bloom = new Bloom(list.get(2).getRLPData());
54
55 if (from == 0 && to == 0 && Arrays.equals(emptyBloom.getData(), bloom.getData())) {
56 return new BlocksBloom();
57 }
58
59 return new BlocksBloom(from, to, bloom);
60 }
61
62 private static byte[] encodeLong(long value) {
63 return RLP.encodeBigInteger(BigInteger.valueOf(value));
64 }
65
66 private static long decodeLong(byte[] data) {
67 return data == null ? 0 : BigIntegers.fromUnsignedByteArray(data).longValueExact();
68 }
69 }