Skip to content

Commit 7b8ee9f

Browse files
committed
bl
2 parents 6fbdf58 + 356fc16 commit 7b8ee9f

File tree

8 files changed

+129
-2
lines changed

8 files changed

+129
-2
lines changed

src/main/java/org/tron/core/actuator/TransferActuator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import org.tron.core.exception.BalanceInsufficientException;
1515
import org.tron.core.exception.ContractExeException;
1616
import org.tron.core.exception.ContractValidateException;
17+
import org.tron.core.exception.WhitelistException;
18+
import org.tron.core.services.WhitelistService;
1719
import org.tron.protos.Contract.TransferContract;
1820
import org.tron.protos.Protocol.AccountType;
1921
import org.tron.protos.Protocol.Transaction.Result.code;
@@ -115,7 +117,6 @@ public boolean validate() throws ContractValidateException {
115117
}
116118

117119
try {
118-
119120
AccountCapsule toAccount = dbManager.getAccountStore().get(toAddress);
120121
if (toAccount == null) {
121122
fee = fee + dbManager.getDynamicPropertiesStore().getCreateNewAccountFeeInSystemContract();

src/main/java/org/tron/core/config/args/Args.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.beust.jcommander.JCommander;
44
import com.beust.jcommander.Parameter;
5+
import com.google.common.collect.Maps;
56
import com.typesafe.config.Config;
67
import com.typesafe.config.ConfigObject;
78
import io.grpc.internal.GrpcUtil;
@@ -19,7 +20,9 @@
1920
import java.net.URL;
2021
import java.util.ArrayList;
2122
import java.util.Collections;
23+
import java.util.HashMap;
2224
import java.util.List;
25+
import java.util.Map;
2326
import java.util.Objects;
2427
import java.util.Optional;
2528
import java.util.Properties;
@@ -915,6 +918,21 @@ public String getOutputDirectory() {
915918
return this.outputDirectory;
916919
}
917920

921+
public Map<String, String> getBlacklist() {
922+
Config config = Configuration.getByFileName(INSTANCE.shellConfFileName, Constant.TESTNET_CONF);
923+
if (!config.hasPath("blacklist")) {
924+
return Collections.emptyMap();
925+
}
926+
927+
return config.getObjectList("blacklist").stream()
928+
.map((ConfigObject e) -> Maps.immutableEntry(
929+
e.get("from") == null ? "" : e.get("from").unwrapped().toString(),
930+
e.get("to") == null ? "" : e.get("to").unwrapped().toString()))
931+
.filter(e -> e.getKey() != null)
932+
.filter(e -> e.getValue() != null)
933+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (k1, k2) -> k2));
934+
}
935+
918936
private static List<Node> getNodes(final com.typesafe.config.Config config, String path) {
919937
if (!config.hasPath(path)) {
920938
return Collections.emptyList();

src/main/java/org/tron/core/db/Manager.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@
101101
import org.tron.core.exception.VMIllegalException;
102102
import org.tron.core.exception.ValidateScheduleException;
103103
import org.tron.core.exception.ValidateSignatureException;
104+
import org.tron.core.exception.WhitelistException;
105+
import org.tron.core.services.WhitelistService;
104106
import org.tron.core.services.WitnessService;
105107
import org.tron.core.witness.ProposalController;
106108
import org.tron.core.witness.WitnessController;
@@ -1206,6 +1208,13 @@ public boolean processTransaction(final TransactionCapsule trxCap, BlockCapsule
12061208
throw new ValidateSignatureException("trans sig validate failed");
12071209
}
12081210

1211+
try {
1212+
WhitelistService.check(trxCap);
1213+
} catch (WhitelistException e) {
1214+
logger.debug(e.getMessage());
1215+
throw new ContractValidateException(e.getMessage(), e);
1216+
}
1217+
12091218
TransactionTrace trace = new TransactionTrace(trxCap, this);
12101219
trxCap.setTrxTrace(trace);
12111220

src/main/java/org/tron/core/exception/ContractValidateException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ public ContractValidateException() {
99
public ContractValidateException(String message) {
1010
super(message);
1111
}
12+
13+
public ContractValidateException(String message, Throwable throwable) {
14+
super(message, throwable);
15+
}
1216
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.tron.core.exception;
2+
3+
public class WhitelistException extends TronException {
4+
5+
public WhitelistException() {
6+
super();
7+
}
8+
9+
public WhitelistException(String message) {
10+
super(message);
11+
}
12+
13+
public WhitelistException(String message, Throwable cause) {
14+
super(message, cause);
15+
}
16+
17+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.tron.core.services;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Objects;
6+
import javax.annotation.PostConstruct;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.apache.commons.collections4.CollectionUtils;
9+
import org.apache.commons.collections4.MapUtils;
10+
import org.springframework.stereotype.Component;
11+
import org.tron.common.utils.ByteArray;
12+
import org.tron.common.utils.ForkController;
13+
import org.tron.core.Wallet;
14+
import org.tron.core.capsule.TransactionCapsule;
15+
import org.tron.core.config.Parameter.ForkBlockVersionEnum;
16+
import org.tron.core.config.args.Args;
17+
import org.tron.core.db.common.WrappedByteArray;
18+
import org.tron.core.exception.WhitelistException;
19+
import org.tron.protos.Protocol.Transaction.Contract;
20+
import org.tron.protos.Protocol.Transaction.Contract.ContractType;
21+
22+
// TODO
23+
@Component
24+
@Slf4j
25+
public class WhitelistService {
26+
private final static String TEST_FROM = "41ceee995c01c9bb7d720f9013336363cdc7c8c4d8";
27+
private final static String TEST_TO = "41216352a10649ffc3e37ba492feb0c35b3b6258e0";
28+
private static Map<WrappedByteArray, WrappedByteArray> whitelist = new HashMap<>();
29+
30+
public WhitelistService() {
31+
// test
32+
whitelist.put(WrappedByteArray.of(ByteArray.fromHexString(TEST_FROM)),
33+
WrappedByteArray.of(ByteArray.fromHexString(TEST_TO)));
34+
}
35+
36+
// TODO
37+
@PostConstruct
38+
public void loadFromConfig() {
39+
// Args.getInstance().getBlacklist().forEach((k, v) -> {
40+
// WrappedByteArray key = WrappedByteArray.of(ByteArray.fromHexString(k));
41+
// WrappedByteArray value = WrappedByteArray.of(ByteArray.fromHexString(v));
42+
// whitelist.put(key, value);
43+
// });
44+
}
45+
46+
public static void check(TransactionCapsule transactionCapsule) throws WhitelistException {
47+
if (!ForkController.instance().pass(ForkBlockVersionEnum.VERSION_3_5)) {
48+
return;
49+
}
50+
51+
if (MapUtils.isEmpty(whitelist)) {
52+
return;
53+
}
54+
55+
Contract contract = transactionCapsule.getInstance().getRawData().getContractList().get(0);
56+
Contract.ContractType contractType = contract.getType();
57+
if (contractType == ContractType.UnfreezeBalanceContract) {
58+
return;
59+
}
60+
61+
byte[] fromAddress = TransactionCapsule.getOwner(contract);
62+
byte[] toAddress = TransactionCapsule.getToAddress(contract);
63+
WrappedByteArray from = WrappedByteArray.of(fromAddress);
64+
WrappedByteArray to = WrappedByteArray.of(toAddress);
65+
WrappedByteArray value = whitelist.get(from);
66+
if (Objects.nonNull(value) && (contractType != ContractType.TransferContract || !value.equals(to))) {
67+
throw new WhitelistException("Not the specified address. "
68+
+ "contractType: " + contractType
69+
+ ", from:" + Wallet.encode58Check(fromAddress)
70+
+ ", to:" + (toAddress == null ? null : Wallet.encode58Check(toAddress)));
71+
}
72+
}
73+
}

src/main/java/org/tron/program/Version.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class Version {
44

5-
private static final String version = "3.2.4";
5+
private static final String version = "3.5";
66

77
public static String getVersion() {
88
return version;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.tron.core.services;
2+
3+
public class WhitelistServiceTest {
4+
5+
}

0 commit comments

Comments
 (0)