Skip to content

Commit

Permalink
Merge pull request #183 from icon-project/story/intrachain-blacklist#182
Browse files Browse the repository at this point in the history
Story/intrachain blacklist#182
  • Loading branch information
manishbista28 authored Aug 5, 2022
2 parents 9c82bb9 + c75e9b7 commit e846b42
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public void setProperties(BMCProperties properties) {
this.properties.set(properties);
}

@External(readonly = true)
public String name() {
return "BTP Message Center";
}

@External(readonly = true)
public String getBtpAddress() {
return btpAddr.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,29 @@ public void addBlacklistAddress(String _net, String[] _addresses) {
requireOwnerAccess();

// check for valid link
require(isValidLink(_net), "Invalid link");
require(isValidLink(_net) || _net.equals(net), "Invalid link");

List<String> blacklist = new ArrayList<>();

for (String addr: _addresses) {
require(!isUserBlackListed(addr, _net), "User already blacklisted");
if (! isUserBlackListed(addr, _net)) {
blacklist.add(addr);
blacklistDB.addToBlacklist(_net, addr);
}
}

if (blacklist.size() == 0 || net.equals(_net)) {
return;
}

int size = blacklist.size();
String[] addresses = new String[size];
for (int i = 0; i < size; i++) {
addresses[i] = blacklist.get(i);
}

BigInteger sn = increaseSn();
BlacklistTransaction request = new BlacklistTransaction(_addresses, _net);
BlacklistTransaction request = new BlacklistTransaction(addresses, _net);

blacklistTxn.set(sn, request);

Expand All @@ -233,14 +248,29 @@ public void removeBlacklistAddress(String _net, String[] _addresses) {
requireOwnerAccess();

// check for valid link
require(isValidLink(_net), "Invalid link");
require(isValidLink(_net) || _net.equals(net), "Invalid link");

List<String> blacklist = new ArrayList<>();

for (String addr: _addresses) {
require(isUserBlackListed(addr, _net), "User not in blacklist");
if ( isUserBlackListed(addr, _net)) {
blacklist.add(addr);
blacklistDB.removeFromBlacklist(_net, addr);
}
}

if (net.equals(_net) || blacklist.size() == 0) {
return;
}

int size = blacklist.size();
String[] addresses = new String[size];
for (int i = 0; i < size; i++) {
addresses[i] = blacklist.get(i);
}

BigInteger sn = increaseSn();
BlacklistTransaction request = new BlacklistTransaction(_addresses, _net);
BlacklistTransaction request = new BlacklistTransaction(addresses, _net);

blacklistTxn.set(sn, request);

Expand Down Expand Up @@ -779,17 +809,15 @@ private void handleAddToBlacklist(BigInteger sn, BlacklistResponse response) {
if (txn != null) {
BigInteger code = response.getCode();
if (BlacklistResponse.RC_OK.equals(code)) {
blacklistTxn.set(sn, null);
AddedToBlacklist(sn, response.getMessage() != null ? response.getMessage().getBytes() : null);
} else {
String[] addresses = txn.getAddress();
String net = txn.getNet();
for(String addr : addresses) {
addToBlacklistInternal(net, addr);
for(String addr: addresses) {
removeFromBlacklistInternal(net, addr);
}
} else {
throw BTSException.unknown("Invalid add to blacklist transaction");
}
blacklistTxn.set(sn, null);
AddedToBlacklist(sn, response.getMessage() != null ? response.getMessage().getBytes() : null);

}
logger.println("handleAddToBlacklist", "end");
}
Expand All @@ -800,17 +828,15 @@ private void handleRemoveFromBlacklist(BigInteger sn, BlacklistResponse response
if (txn != null) {
BigInteger code = response.getCode();
if (BlacklistResponse.RC_OK.equals(code)) {
blacklistTxn.set(sn, null);
RemovedFromBlacklist(sn, response.getMessage() != null ? response.getMessage().getBytes() : null);
} else {
String[] addresses = txn.getAddress();
String net = txn.getNet();
for(String addr : addresses) {
removeFromBlacklistInternal(net, addr);
addToBlacklistInternal(net, addr);
}
} else {
throw BTSException.unknown("Invalid remove from blacklist transaction");
}
blacklistTxn.set(sn, null);
RemovedFromBlacklist(sn, response.getMessage() != null ? response.getMessage().getBytes() : null);

}
logger.println("handleRemoveFromBlacklist", "end");
}
Expand All @@ -836,16 +862,10 @@ private void handleChangeTokenLimit(String from, BigInteger sn, TokenLimitRespon
}

private void addToBlacklistInternal(String net, String addr) {
if (blacklistDB.contains(net, addr)) {
throw BTSException.unknown("User already blacklisted");
}
blacklistDB.addToBlacklist(net, addr);
}

private void removeFromBlacklistInternal(String net, String addr) {
if (! blacklistDB.contains(net, addr)) {
throw BTSException.unknown("User not in blacklist");
}
blacklistDB.removeFromBlacklist(net, addr);
}

Expand Down Expand Up @@ -1068,7 +1088,7 @@ private void validateRestriction(String _net, String _token, String _from, Strin
throw BTSException.restricted("_to user is Blacklisted");
}
BigInteger tokenLimit = getTokenLimit(_token);
if (tokenLimit.compareTo(_value) < 0) {
if (_value.compareTo(tokenLimit) > 0) {
throw BTSException.restricted("Transfer amount exceeds the transaction limit");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public void addToLockListTest() {


String[] addr2 = new String[] {" all too well "};
call = () -> score.invoke(owner, "addBlacklistAddress", "network", addr2);
expectErrorMessage(call, "User already blacklisted");
score.invoke(owner, "addBlacklistAddress", "network", addr2);

List<String> actual = (List<String>) score.call("getBlackListedUsers", "network", 0, 10);
List<String> expected = List.of("all too well");
Expand Down Expand Up @@ -105,8 +104,7 @@ public void removeFromLocklist() {
expectErrorMessage(call, "require owner access");

// try to remove non blacklisted
call = () -> score.invoke(owner, "removeBlacklistAddress", "harmony",new String[]{"hell"});
expectErrorMessage(call, "User not in blacklist");
score.invoke(owner, "removeBlacklistAddress", "harmony",new String[]{"hell"});

// remove legit user
score.invoke(owner, "removeBlacklistAddress", "network", addr1);
Expand Down
2 changes: 1 addition & 1 deletion solidity/bts/contracts/BTSPeriphery.sol
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,6 @@ contract BTSPeriphery is Initializable, IBTSPeriphery {
uint256 _value
) public view override {
require(!blacklist[_user],"Blacklisted");
require(tokenLimit[_coinName] >= _value,"LimitExceed");
require(tokenLimit[_coinName] > _value ,"LimitExceed");
}
}

0 comments on commit e846b42

Please sign in to comment.