Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize delegates #4788

Merged
merged 2 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ public long calcFee() {
private long delegateResource(byte[] ownerAddress, byte[] receiverAddress, boolean isBandwidth,
long balance, long expireTime) {
AccountStore accountStore = chainBaseManager.getAccountStore();
DynamicPropertiesStore dynamicPropertiesStore = chainBaseManager.getDynamicPropertiesStore();
DelegatedResourceStore delegatedResourceStore = chainBaseManager.getDelegatedResourceStore();
DelegatedResourceAccountIndexStore delegatedResourceAccountIndexStore = chainBaseManager
.getDelegatedResourceAccountIndexStore();
Expand Down Expand Up @@ -312,35 +313,39 @@ private long delegateResource(byte[] ownerAddress, byte[] receiverAddress, boole
delegatedResourceStore.put(key, delegatedResourceCapsule);

//modify DelegatedResourceAccountIndexStore
{
DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule = delegatedResourceAccountIndexStore
.get(ownerAddress);
if (delegatedResourceAccountIndexCapsule == null) {
delegatedResourceAccountIndexCapsule = new DelegatedResourceAccountIndexCapsule(
if (!dynamicPropertiesStore.supportAllowDelegateOptimization()) {

DelegatedResourceAccountIndexCapsule ownerIndexCapsule =
delegatedResourceAccountIndexStore.get(ownerAddress);
if (ownerIndexCapsule == null) {
ownerIndexCapsule = new DelegatedResourceAccountIndexCapsule(
ByteString.copyFrom(ownerAddress));
}
List<ByteString> toAccountsList = delegatedResourceAccountIndexCapsule.getToAccountsList();
List<ByteString> toAccountsList = ownerIndexCapsule.getToAccountsList();
if (!toAccountsList.contains(ByteString.copyFrom(receiverAddress))) {
delegatedResourceAccountIndexCapsule.addToAccount(ByteString.copyFrom(receiverAddress));
ownerIndexCapsule.addToAccount(ByteString.copyFrom(receiverAddress));
}
delegatedResourceAccountIndexStore
.put(ownerAddress, delegatedResourceAccountIndexCapsule);
}
delegatedResourceAccountIndexStore.put(ownerAddress, ownerIndexCapsule);

{
DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule = delegatedResourceAccountIndexStore
.get(receiverAddress);
if (delegatedResourceAccountIndexCapsule == null) {
delegatedResourceAccountIndexCapsule = new DelegatedResourceAccountIndexCapsule(
DelegatedResourceAccountIndexCapsule receiverIndexCapsule
= delegatedResourceAccountIndexStore.get(receiverAddress);
if (receiverIndexCapsule == null) {
receiverIndexCapsule = new DelegatedResourceAccountIndexCapsule(
ByteString.copyFrom(receiverAddress));
}
List<ByteString> fromAccountsList = delegatedResourceAccountIndexCapsule
List<ByteString> fromAccountsList = receiverIndexCapsule
.getFromAccountsList();
if (!fromAccountsList.contains(ByteString.copyFrom(ownerAddress))) {
delegatedResourceAccountIndexCapsule.addFromAccount(ByteString.copyFrom(ownerAddress));
receiverIndexCapsule.addFromAccount(ByteString.copyFrom(ownerAddress));
}
delegatedResourceAccountIndexStore
.put(receiverAddress, delegatedResourceAccountIndexCapsule);
delegatedResourceAccountIndexStore.put(receiverAddress, receiverIndexCapsule);

} else {
// modify DelegatedResourceAccountIndexStore new
delegatedResourceAccountIndexStore.convert(ownerAddress);
delegatedResourceAccountIndexStore.convert(receiverAddress);
delegatedResourceAccountIndexStore.delegate(ownerAddress, receiverAddress,
dynamicPropertiesStore.getLatestBlockHeaderTimestamp());
}

//modify AccountStore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,32 +157,32 @@ public boolean execute(Object result) throws ContractExeException {
delegatedResourceStore.delete(key);

//modify DelegatedResourceAccountIndexStore
{
DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule = delegatedResourceAccountIndexStore
.get(ownerAddress);
if (delegatedResourceAccountIndexCapsule != null) {
List<ByteString> toAccountsList = new ArrayList<>(delegatedResourceAccountIndexCapsule
if (!dynamicStore.supportAllowDelegateOptimization()) {
DelegatedResourceAccountIndexCapsule ownerIndexCapsule =
delegatedResourceAccountIndexStore.get(ownerAddress);
if (ownerIndexCapsule != null) {
List<ByteString> toAccountsList = new ArrayList<>(ownerIndexCapsule
.getToAccountsList());
toAccountsList.remove(ByteString.copyFrom(receiverAddress));
delegatedResourceAccountIndexCapsule.setAllToAccounts(toAccountsList);
delegatedResourceAccountIndexStore
.put(ownerAddress, delegatedResourceAccountIndexCapsule);
ownerIndexCapsule.setAllToAccounts(toAccountsList);
delegatedResourceAccountIndexStore.put(ownerAddress, ownerIndexCapsule);
}
}

{
DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule = delegatedResourceAccountIndexStore
.get(receiverAddress);
if (delegatedResourceAccountIndexCapsule != null) {
List<ByteString> fromAccountsList = new ArrayList<>(delegatedResourceAccountIndexCapsule
DelegatedResourceAccountIndexCapsule receiverIndexCapsule =
delegatedResourceAccountIndexStore.get(receiverAddress);
if (receiverIndexCapsule != null) {
List<ByteString> fromAccountsList = new ArrayList<>(receiverIndexCapsule
.getFromAccountsList());
fromAccountsList.remove(ByteString.copyFrom(ownerAddress));
delegatedResourceAccountIndexCapsule.setAllFromAccounts(fromAccountsList);
delegatedResourceAccountIndexStore
.put(receiverAddress, delegatedResourceAccountIndexCapsule);
receiverIndexCapsule.setAllFromAccounts(fromAccountsList);
delegatedResourceAccountIndexStore.put(receiverAddress, receiverIndexCapsule);
}
} else {
//modify DelegatedResourceAccountIndexStore new
delegatedResourceAccountIndexStore.convert(ownerAddress);
delegatedResourceAccountIndexStore.convert(receiverAddress);
delegatedResourceAccountIndexStore.unDelegate(ownerAddress, receiverAddress);
}

} else {
delegatedResourceStore.put(key, delegatedResourceCapsule);
}
Expand Down
14 changes: 13 additions & 1 deletion actuator/src/main/java/org/tron/core/utils/ProposalUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,17 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
}
break;
}
case ALLOW_DELEGATE_OPTIMIZATION: {
if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_6)) {
throw new ContractValidateException(
"Bad chain parameter id [ALLOW_DELEGATE_OPTIMIZATION]");
}
if (value != 1) {
throw new ContractValidateException(
"This value[ALLOW_DELEGATE_OPTIMIZATION] is only allowed to be 1");
}
break;
}
default:
break;
}
Expand Down Expand Up @@ -651,7 +662,8 @@ public enum ProposalType { // current value, value range
ALLOW_HIGHER_LIMIT_FOR_MAX_CPU_TIME_OF_ONE_TX(65), // 0, 1
ALLOW_ASSET_OPTIMIZATION(66), // 0, 1
ALLOW_NEW_REWARD(67), // 0, 1
MEMO_FEE(68); // 0, [0, 1000_000_000]
MEMO_FEE(68), // 0, [0, 1000_000_000]
ALLOW_DELEGATE_OPTIMIZATION(69); // 0, 1

private long code;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ public void removeToAccount(ByteString toAccount) {
}
}

public void setTimestamp(long time) {
this.delegatedResourceAccountIndex = this.delegatedResourceAccountIndex.toBuilder()
.setTimestamp(time)
.build();
}

public long getTimestamp() {
return this.delegatedResourceAccountIndex.getTimestamp();
}

public byte[] createDbKey() {
return getAccount().toByteArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -29,6 +30,7 @@
import org.tron.core.db2.common.IRevokingDB;
import org.tron.core.db2.common.LevelDB;
import org.tron.core.db2.common.RocksDB;
import org.tron.core.db2.common.WrappedByteArray;
import org.tron.core.db2.core.Chainbase;
import org.tron.core.db2.core.ITronChainBase;
import org.tron.core.db2.core.RevokingDBWithCachingOldValue;
Expand Down Expand Up @@ -234,4 +236,16 @@ public long size() {
public void setCursor(Chainbase.Cursor cursor) {
revokingDB.setCursor(cursor);
}

public Map<WrappedByteArray, T> prefixQuery(byte[] key) {
return revokingDB.prefixQuery(key).entrySet().stream().collect(
Collectors.toMap(Map.Entry::getKey, e -> {
try {
return of(e.getValue());
} catch (BadItemException e1) {
throw new RuntimeException(e1);
}
}
));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package org.tron.core.store;

import com.google.protobuf.ByteString;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -11,6 +16,9 @@
public class DelegatedResourceAccountIndexStore extends
TronStoreWithRevoking<DelegatedResourceAccountIndexCapsule> {

private static final byte[] FROM_PREFIX = {0x01};
private static final byte[] TO_PREFIX = {0x02};

@Autowired
public DelegatedResourceAccountIndexStore(@Value("DelegatedResourceAccountIndex") String dbName) {
super(dbName);
Expand All @@ -23,4 +31,85 @@ public DelegatedResourceAccountIndexCapsule get(byte[] key) {
return ArrayUtils.isEmpty(value) ? null : new DelegatedResourceAccountIndexCapsule(value);
}

private byte[] createKey(byte[] prefix, byte[] address1, byte[] address2) {
byte[] key = new byte[prefix.length + address1.length + address2.length];
System.arraycopy(prefix, 0, key, 0, prefix.length);
System.arraycopy(address1, 0, key, prefix.length, address1.length);
System.arraycopy(address2, 0, key, prefix.length + address1.length, address2.length);
return key;
}

public void convert(byte[] address) {
DelegatedResourceAccountIndexCapsule indexCapsule = this.get(address);
if (indexCapsule == null) {
// convert complete or have no delegate
return;
}
// convert old data
List<ByteString> toList = indexCapsule.getToAccountsList();
for (int i = 0; i < toList.size(); i++) {
// use index as the timestamp, just to keep index in order
this.delegate(address, toList.get(i).toByteArray(), i + 1L);
}

List<ByteString> fromList = indexCapsule.getFromAccountsList();
for (int i = 0; i < fromList.size(); i++) {
// use index as the timestamp, just to keep index in order
this.delegate(fromList.get(i).toByteArray(), address, i + 1L);
}
this.delete(address);
}

public void delegate(byte[] from, byte[] to, long time) {
byte[] fromKey = createKey(FROM_PREFIX, from, to);
DelegatedResourceAccountIndexCapsule toIndexCapsule =
new DelegatedResourceAccountIndexCapsule(ByteString.copyFrom(to));
toIndexCapsule.setTimestamp(time);
this.put(fromKey, toIndexCapsule);

byte[] toKey = createKey(TO_PREFIX, to, from);
DelegatedResourceAccountIndexCapsule fromIndexCapsule =
new DelegatedResourceAccountIndexCapsule(ByteString.copyFrom(from));
fromIndexCapsule.setTimestamp(time);
this.put(toKey, fromIndexCapsule);
}

public void unDelegate(byte[] from, byte[] to) {
byte[] fromKey = createKey(FROM_PREFIX, from, to);
this.delete(fromKey);
byte[] toKey = createKey(TO_PREFIX, to, from);
this.delete(toKey);
}

public DelegatedResourceAccountIndexCapsule getIndex(byte[] address) {
DelegatedResourceAccountIndexCapsule indexCapsule = get(address);
if (indexCapsule != null) {
return indexCapsule;
}

DelegatedResourceAccountIndexCapsule tmpIndexCapsule =
new DelegatedResourceAccountIndexCapsule(ByteString.copyFrom(address));
byte[] key = new byte[FROM_PREFIX.length + address.length];

System.arraycopy(FROM_PREFIX, 0, key, 0, FROM_PREFIX.length);
System.arraycopy(address, 0, key, FROM_PREFIX.length, address.length);
List<DelegatedResourceAccountIndexCapsule> tmpToList =
new ArrayList<>(this.prefixQuery(key).values());

tmpToList.sort(Comparator.comparing(DelegatedResourceAccountIndexCapsule::getTimestamp));
List<ByteString> list = tmpToList.stream()
.map(DelegatedResourceAccountIndexCapsule::getAccount).collect(Collectors.toList());
tmpIndexCapsule.setAllToAccounts(list);

System.arraycopy(TO_PREFIX, 0, key, 0, TO_PREFIX.length);
System.arraycopy(address, 0, key, TO_PREFIX.length, address.length);
List<DelegatedResourceAccountIndexCapsule> tmpFromList =
new ArrayList<>(this.prefixQuery(key).values());
tmpFromList.sort(Comparator.comparing(DelegatedResourceAccountIndexCapsule::getTimestamp));
list = tmpFromList.stream().map(DelegatedResourceAccountIndexCapsule::getAccount).collect(
Collectors.toList());
tmpIndexCapsule.setAllFromAccounts(list);
return tmpIndexCapsule;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ public class DynamicPropertiesStore extends TronStoreWithRevoking<BytesCapsule>
private static final byte[] ALLOW_NEW_REWARD = "ALLOW_NEW_REWARD".getBytes();
private static final byte[] MEMO_FEE = "MEMO_FEE".getBytes();
private static final byte[] MEMO_FEE_HISTORY = "MEMO_FEE_HISTORY".getBytes();
private static final byte[] ALLOW_DELEGATE_OPTIMIZATION =
"ALLOW_DELEGATE_OPTIMIZATION".getBytes();


@Autowired
private DynamicPropertiesStore(@Value("properties") String dbName) {
Expand Down Expand Up @@ -879,6 +882,13 @@ private DynamicPropertiesStore(@Value("properties") String dbName) {
this.saveMemoFee(memoFee);
this.saveMemoFeeHistory("0:" + memoFee);
}

try {
this.getAllowDelegateOptimization();
} catch (IllegalArgumentException e) {
this.saveAllowDelegateOptimization(
CommonParameter.getInstance().getAllowDelegateOptimization());
}
}

public String intArrayToString(int[] a) {
Expand Down Expand Up @@ -2592,6 +2602,22 @@ public void saveAllowNewReward(long newReward) {
this.put(ALLOW_NEW_REWARD, new BytesCapsule(ByteArray.fromLong(newReward)));
}

public long getAllowDelegateOptimization() {
return Optional.ofNullable(getUnchecked(ALLOW_DELEGATE_OPTIMIZATION))
.map(BytesCapsule::getData)
.map(ByteArray::toLong)
.orElseThrow(
() -> new IllegalArgumentException("not found ALLOW_DELEGATE_OPTIMIZATION"));
}

public boolean supportAllowDelegateOptimization() {
return getAllowDelegateOptimization() == 1L;
}

public void saveAllowDelegateOptimization(long value) {
this.put(ALLOW_DELEGATE_OPTIMIZATION, new BytesCapsule(ByteArray.fromLong(value)));
}

public boolean allowNewReward() {
return getAllowNewReward() == 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,10 @@ public class CommonParameter {
@Setter
public long memoFee = 0L;

@Getter
@Setter
public long allowDelegateOptimization = 0L;

private static double calcMaxTimeRatio() {
//return max(2.0, min(5.0, 5 * 4.0 / max(Runtime.getRuntime().availableProcessors(), 1)));
return 5.0;
Expand Down
1 change: 1 addition & 0 deletions common/src/main/java/org/tron/core/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ public class Constant {
public static final String ALLOW_ASSET_OPTIMIZATION = "committee.allowAssetOptimization";
public static final String ALLOW_NEW_REWARD = "committee.allowNewReward";
public static final String MEMO_FEE = "committee.memoFee";
public static final String ALLOW_DELEGATE_OPTIMIZATION = "committee.allowDelegateOptimization";

public static final String LOCAL_HOST = "127.0.0.1";

Expand Down
7 changes: 6 additions & 1 deletion framework/src/main/java/org/tron/core/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ public DelegatedResourceList getDelegatedResource(ByteString fromAddress, ByteSt

public DelegatedResourceAccountIndex getDelegatedResourceAccountIndex(ByteString address) {
DelegatedResourceAccountIndexCapsule accountIndexCapsule =
chainBaseManager.getDelegatedResourceAccountIndexStore().get(address.toByteArray());
chainBaseManager.getDelegatedResourceAccountIndexStore().getIndex(address.toByteArray());
if (accountIndexCapsule != null) {
return accountIndexCapsule.getInstance();
} else {
Expand Down Expand Up @@ -1108,6 +1108,11 @@ public Protocol.ChainParameters getChainParameters() {
.setValue(dbManager.getDynamicPropertiesStore().getMemoFee())
.build());

builder.addChainParameter(Protocol.ChainParameters.ChainParameter.newBuilder()
.setKey("getAllowDelegateOptimization")
.setValue(dbManager.getDynamicPropertiesStore().getAllowDelegateOptimization())
.build());

return builder.build();
}

Expand Down
Loading