16
16
17
17
import java .math .BigInteger ;
18
18
import java .util .HashMap ;
19
+ import java .util .List ;
19
20
import java .util .Map ;
20
21
import java .util .function .Predicate ;
22
+ import java .util .stream .Collectors ;
21
23
import org .fisco .bcos .sdk .v3 .client .Client ;
24
+ import org .fisco .bcos .sdk .v3 .client .protocol .response .BcosGroupNodeInfo ;
22
25
import org .fisco .bcos .sdk .v3 .contract .precompiled .model .PrecompiledAddress ;
23
26
import org .fisco .bcos .sdk .v3 .crypto .keypair .CryptoKeyPair ;
27
+ import org .fisco .bcos .sdk .v3 .model .EnumNodeVersion ;
24
28
import org .fisco .bcos .sdk .v3 .model .RetCode ;
25
29
import org .fisco .bcos .sdk .v3 .model .TransactionReceipt ;
26
30
import org .fisco .bcos .sdk .v3 .transaction .codec .decode .ReceiptParser ;
27
31
import org .fisco .bcos .sdk .v3 .transaction .model .exception .ContractException ;
28
32
29
33
public class SystemConfigService {
30
34
private final SystemConfigPrecompiled systemConfigPrecompiled ;
35
+ private final Client client ;
31
36
public static final String TX_COUNT_LIMIT = "tx_count_limit" ;
32
37
public static final String TX_GAS_LIMIT = "tx_gas_limit" ;
33
38
public static final String CONSENSUS_PERIOD = "consensus_leader_period" ;
34
39
public static final String AUTH_STATUS = "auth_check_status" ;
40
+ public static final String COMPATIBILITY_VERSION = "compatibility_version" ;
35
41
public static final int TX_GAS_LIMIT_MIN = 100000 ;
36
42
private static final Map <String , Predicate <BigInteger >> predicateMap = new HashMap <>();
37
43
@@ -44,6 +50,7 @@ public class SystemConfigService {
44
50
}
45
51
46
52
public SystemConfigService (Client client , CryptoKeyPair credential ) {
53
+ this .client = client ;
47
54
this .systemConfigPrecompiled =
48
55
SystemConfigPrecompiled .load (
49
56
client .isWASM ()
@@ -54,6 +61,18 @@ public SystemConfigService(Client client, CryptoKeyPair credential) {
54
61
}
55
62
56
63
public RetCode setValueByKey (String key , String value ) throws ContractException {
64
+ if (COMPATIBILITY_VERSION .equals (key ) && !checkCompatibilityVersion (client , value )) {
65
+ String nodeVersionString =
66
+ client .getGroupInfo ().getResult ().getNodeList ().stream ()
67
+ .map (node -> node .getIniConfig ().getBinaryInfo ().getVersion ())
68
+ .collect (Collectors .joining ("," ));
69
+ throw new ContractException (
70
+ "The compatibility version "
71
+ + value
72
+ + " is not supported, please check the version of the chain. (The version of the chain is "
73
+ + nodeVersionString
74
+ + ")" );
75
+ }
57
76
TransactionReceipt receipt = systemConfigPrecompiled .setValueByKey (key , value );
58
77
return ReceiptParser .parseTransactionReceipt (
59
78
receipt , tr -> systemConfigPrecompiled .getSetValueByKeyOutput (receipt ).getValue1 ());
@@ -76,4 +95,23 @@ public static boolean checkSysNumberValueValidation(String key, String value) {
76
95
public static boolean isCheckableInValueValidation (String key ) {
77
96
return predicateMap .containsKey (key );
78
97
}
98
+
99
+ public static boolean checkCompatibilityVersion (Client client , String version ) {
100
+ try {
101
+ EnumNodeVersion .Version setVersion = EnumNodeVersion .getClassVersion (version );
102
+ List <BcosGroupNodeInfo .GroupNodeInfo > nodeList =
103
+ client .getGroupInfo ().getResult ().getNodeList ();
104
+ return nodeList .stream ()
105
+ .allMatch (
106
+ node ->
107
+ setVersion .compareTo (
108
+ EnumNodeVersion .getClassVersion (
109
+ node .getIniConfig ()
110
+ .getBinaryInfo ()
111
+ .getVersion ()))
112
+ <= 0 );
113
+ } catch (Exception e ) {
114
+ return false ;
115
+ }
116
+ }
79
117
}
0 commit comments