Coverage Summary for Class: ConfigCapabilitiesImpl (org.ethereum.net.client)
Class |
Class, %
|
Method, %
|
Line, %
|
ConfigCapabilitiesImpl |
0%
(0/1)
|
0%
(0/3)
|
0%
(0/36)
|
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 org.ethereum.net.client;
21
22 import org.ethereum.config.SystemProperties;
23 import org.ethereum.net.eth.EthVersion;
24 import org.ethereum.net.p2p.HelloMessage;
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.SortedSet;
29 import java.util.TreeSet;
30
31 import static org.ethereum.net.client.Capability.RSK;
32 import static org.ethereum.net.eth.EthVersion.fromCode;
33
34 /**
35 * Created by Anton Nashatyrev on 13.10.2015.
36 */
37 public class ConfigCapabilitiesImpl implements ConfigCapabilities{
38
39 private final SystemProperties config;
40
41 private SortedSet<Capability> allCaps = new TreeSet<>();
42
43 public ConfigCapabilitiesImpl(SystemProperties config) {
44 if (config.syncVersion() != null) {
45 EthVersion eth = fromCode(config.syncVersion());
46 if (eth != null) {
47 allCaps.add(new Capability(RSK, eth.getCode()));
48 }
49 } else {
50 for (EthVersion v : EthVersion.supported()) {
51 allCaps.add(new Capability(RSK, v.getCode()));
52 }
53 }
54 this.config = config;
55 }
56
57 /**
58 * Gets the capabilities listed in 'peer.capabilities' config property
59 * sorted by their names.
60 */
61 public List<Capability> getConfigCapabilities() {
62 List<Capability> ret = new ArrayList<>();
63 List<String> caps = config.peerCapabilities();
64 for (Capability capability : allCaps) {
65 if (caps.contains(capability.getName())) {
66 ret.add(capability);
67 }
68 }
69 return ret;
70 }
71
72 /**
73 * Returns the node's supported capabilities for this hello message
74 */
75 @Override
76 public List<Capability> getSupportedCapabilities(HelloMessage hello) {
77 List<Capability> configCaps = getConfigCapabilities();
78 List<Capability> supported = new ArrayList<>();
79
80 List<Capability> eths = new ArrayList<>();
81
82 for (Capability cap : hello.getCapabilities()) {
83 if (configCaps.contains(cap)) {
84 if (cap.isRSK()) {
85 eths.add(cap);
86 } else {
87 supported.add(cap);
88 }
89 }
90 }
91
92 if (eths.isEmpty()) {
93 return supported;
94 }
95
96 // we need to pick up
97 // the most recent Eth version
98 Capability highest = null;
99 for (Capability eth : eths) {
100 if (highest == null || highest.getVersion() < eth.getVersion()) {
101 highest = eth;
102 }
103 }
104
105 supported.add(highest);
106 return supported;
107 }
108
109 }