Skip to content

Commit

Permalink
feat(db): optimize old rewards withdrawal (tronprotocol#5406)
Browse files Browse the repository at this point in the history
  • Loading branch information
halibobo1205 authored Jan 5, 2024
1 parent db9f3be commit 52b3327
Show file tree
Hide file tree
Showing 24 changed files with 969 additions and 175 deletions.
23 changes: 22 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 @@ -728,6 +728,26 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
}
break;
}
case ALLOW_OLD_REWARD_OPT: {
if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_7_4)) {
throw new ContractValidateException(
"Bad chain parameter id [ALLOW_OLD_REWARD_OPT]");
}
if (value != 1) {
throw new ContractValidateException(
"This value[ALLOW_OLD_REWARD_OPT] is only allowed to be 1");
}
if (!dynamicPropertiesStore.useNewRewardAlgorithm()) {
throw new ContractValidateException(
"[ALLOW_NEW_REWARD] proposal must be approved "
+ "before [ALLOW_OLD_REWARD_OPT] can be proposed");
}
if (dynamicPropertiesStore.useNewRewardAlgorithmFromStart()) {
throw new ContractValidateException(
"no need old reward opt, ALLOW_NEW_REWARD from start cycle 1");
}
break;
}
default:
break;
}
Expand Down Expand Up @@ -803,7 +823,8 @@ public enum ProposalType { // current value, value range
DYNAMIC_ENERGY_MAX_FACTOR(75), // 0, [0, 100_000]
ALLOW_TVM_SHANGHAI(76), // 0, 1
ALLOW_CANCEL_ALL_UNFREEZE_V2(77), // 0, 1
MAX_DELEGATE_LOCK_PERIOD(78); // (86400, 10512000]
MAX_DELEGATE_LOCK_PERIOD(78), // (86400, 10512000]
ALLOW_OLD_REWARD_OPT(79); // 0, 1

private long code;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,77 @@
package org.tron.core.db.common.iterator;

import com.google.common.collect.Iterators;
import com.google.common.collect.UnmodifiableIterator;
import com.google.common.primitives.Bytes;
import java.io.Closeable;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.NoSuchElementException;

public interface DBIterator extends Iterator<Entry<byte[], byte[]>>, Closeable {
public interface DBIterator extends Iterator<Entry<byte[], byte[]>>, AutoCloseable, Closeable {

void seek(byte[] key);

void seekToFirst();

void seekToLast();

default UnmodifiableIterator<Entry<byte[], byte[]>> prefixQueryAfterThat
(byte[] key, byte[] afterThat) {
this.seek(afterThat == null ? key : afterThat);
return Iterators.filter(this, entry -> Bytes.indexOf(entry.getKey(), key) == 0);
}

/**
* An iterator is either positioned at a key/value pair, or
* not valid. This method returns true iff the iterator is valid.
*
* REQUIRES: iterator not closed
*
* @throws IllegalStateException if the iterator is closed.
* @return an iterator is either positioned at a key/value pair
*/
boolean valid();

/**
* The underlying storage for
* the returned slice is valid only until the next modification of
* the iterator.
*
* REQUIRES: valid() && !closed
*
* @throws IllegalStateException if the iterator is closed.
* @throws NoSuchElementException if the iterator is not valid.
*
* @return the key for the current entry
*/
byte[] getKey();

/**
* The underlying storage for
* the returned slice is valid only until the next modification of
* the iterator.
*
* REQUIRES: valid() && !closed
*
* @throws IllegalStateException if the iterator is closed.
* @throws NoSuchElementException if the iterator is not valid.
*
* @return the value for the current entry
*/
byte[] getValue();

/**
* @throws IllegalStateException if the iterator is closed.
*/
void checkState();

/**
* @throws NoSuchElementException if the iterator is not valid.
*/
default void checkValid() {
if (!valid()) {
throw new NoSuchElementException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicBoolean;
import lombok.extern.slf4j.Slf4j;
import org.rocksdb.RocksIterator;

Expand All @@ -13,20 +14,22 @@ public final class RockStoreIterator implements DBIterator {
private final RocksIterator dbIterator;
private boolean first = true;

private boolean valid = true;
private final AtomicBoolean close = new AtomicBoolean(false);

public RockStoreIterator(RocksIterator dbIterator) {
this.dbIterator = dbIterator;
}

@Override
public void close() throws IOException {
dbIterator.close();
if (close.compareAndSet(false, true)) {
dbIterator.close();
}
}

@Override
public boolean hasNext() {
if (!valid) {
if (close.get()) {
return false;
}
boolean hasNext = false;
Expand All @@ -37,13 +40,12 @@ public boolean hasNext() {
first = false;
}
if (!(hasNext = dbIterator.isValid())) { // false is last item
dbIterator.close();
valid = false;
close();
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
try {
dbIterator.close();
close();
} catch (Exception e1) {
logger.error(e.getMessage(), e);
}
Expand All @@ -53,7 +55,7 @@ public boolean hasNext() {

@Override
public Entry<byte[], byte[]> next() {
if (!valid) {
if (close.get()) {
throw new NoSuchElementException();
}
byte[] key = dbIterator.key();
Expand All @@ -76,4 +78,52 @@ public byte[] setValue(byte[] value) {
}
};
}
}

@Override
public void seek(byte[] key) {
checkState();
dbIterator.seek(key);
this.first = false;
}

@Override
public void seekToFirst() {
checkState();
dbIterator.seekToFirst();
this.first = false;
}

@Override
public void seekToLast() {
checkState();
dbIterator.seekToLast();
this.first = false;
}

@Override
public boolean valid() {
checkState();
return dbIterator.isValid();
}

@Override
public byte[] getKey() {
checkState();
checkValid();
return dbIterator.key();
}

@Override
public byte[] getValue() {
checkState();
checkValid();
return dbIterator.value();
}

@Override
public void checkState() {
if (close.get()) {
throw new IllegalStateException("iterator has been closed");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicBoolean;
import lombok.extern.slf4j.Slf4j;
import org.iq80.leveldb.DBIterator;

Expand All @@ -13,20 +14,22 @@ public final class StoreIterator implements org.tron.core.db.common.iterator.DBI
private final DBIterator dbIterator;
private boolean first = true;

private boolean valid = true;
private final AtomicBoolean close = new AtomicBoolean(false);

public StoreIterator(DBIterator dbIterator) {
this.dbIterator = dbIterator;
}

@Override
public void close() throws IOException {
dbIterator.close();
if (close.compareAndSet(false, true)) {
dbIterator.close();
}
}

@Override
public boolean hasNext() {
if (!valid) {
if (close.get()) {
return false;
}

Expand All @@ -39,8 +42,7 @@ public boolean hasNext() {
}

if (!(hasNext = dbIterator.hasNext())) { // false is last item
dbIterator.close();
valid = false;
close();
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
Expand All @@ -51,7 +53,7 @@ public boolean hasNext() {

@Override
public Entry<byte[], byte[]> next() {
if (!valid) {
if (close.get()) {
throw new NoSuchElementException();
}
return dbIterator.next();
Expand All @@ -61,4 +63,53 @@ public Entry<byte[], byte[]> next() {
public void remove() {
throw new UnsupportedOperationException();
}

@Override
public void seek(byte[] key) {
checkState();
dbIterator.seek(key);
this.first = false;
}

@Override
public void seekToFirst() {
checkState();
dbIterator.seekToFirst();
this.first = false;
}

@Override
public void seekToLast() {
checkState();
dbIterator.seekToLast();
this.first = false;
}

@Override
public boolean valid() {
checkState();
return dbIterator.hasNext();
}

@Override
public byte[] getKey() {
checkState();
checkValid();
return dbIterator.peekNext().getKey();
}

@Override
public byte[] getValue() {
checkState();
checkValid();
return dbIterator.peekNext().getValue();
}

@Override
public void checkState() {
if (close.get()) {
throw new IllegalStateException("iterator has been closed");
}
}
}

Loading

0 comments on commit 52b3327

Please sign in to comment.