Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricker2000 committed Aug 2, 2018
2 parents 781b45e + 3c7297a commit b246e5d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 50 deletions.
22 changes: 14 additions & 8 deletions uscj-core/src/main/java/co/usc/core/SnapshotManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,21 @@
*/
public class SnapshotManager {
private List<Long> snapshots = new ArrayList<>();
private final Blockchain blockchain;
private final TransactionPool transactionPool;

public int takeSnapshot(Blockchain blockchain) {
snapshots.add(Long.valueOf(blockchain.getBestBlock().getNumber()));

public SnapshotManager(Blockchain blockchain, TransactionPool transactionPool) {
this.blockchain = blockchain;
this.transactionPool = transactionPool;
}

public int takeSnapshot() {
snapshots.add(blockchain.getBestBlock().getNumber());
return this.snapshots.size();
}

public boolean resetSnapshots(Blockchain blockchain, TransactionPool transactionPool){
public boolean resetSnapshots() {
this.snapshots = new ArrayList<>();

long bestNumber = blockchain.getBestBlock().getNumber();
Expand All @@ -65,16 +73,14 @@ public boolean resetSnapshots(Blockchain blockchain, TransactionPool transaction
return true;
}

public boolean revertToSnapshot(Blockchain blockchain, TransactionPool transactionPool, int snapshotId) {
public boolean revertToSnapshot(int snapshotId) {
if (snapshotId <= 0 || snapshotId > this.snapshots.size()) {
return false;
}

long newBestBlockNumber = this.snapshots.get(snapshotId - 1).longValue();

List<Long> newSnapshots = this.snapshots.stream().limit(snapshotId).collect(Collectors.toList());
long newBestBlockNumber = this.snapshots.get(snapshotId - 1);

this.snapshots = newSnapshots;
this.snapshots = this.snapshots.stream().limit(snapshotId).collect(Collectors.toList());

long currentBestBlockNumber = blockchain.getBestBlock().getNumber();

Expand Down
14 changes: 8 additions & 6 deletions uscj-core/src/main/java/org/ethereum/rpc/Web3Impl.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
public class Web3Impl implements Web3 {
private static final Logger logger = LoggerFactory.getLogger("web3");

private final SnapshotManager snapshotManager = new SnapshotManager();
private final MinerManager minerManager = new MinerManager();

public org.ethereum.core.Repository repository;
Expand All @@ -98,11 +97,13 @@ public class Web3Impl implements Web3 {
private final TransactionPool transactionPool;
private final UscSystemProperties config;

private final FilterManager filterManager;
private final SnapshotManager snapshotManager;

private final PersonalModule personalModule;
private final EthModule ethModule;
private final TxPoolModule txPoolModule;

private FilterManager filterManager;
private TxPoolModule txPoolModule;

protected Web3Impl(Ethereum eth,
Blockchain blockchain,
Expand Down Expand Up @@ -141,6 +142,7 @@ protected Web3Impl(Ethereum eth,
this.configCapabilities = configCapabilities;
this.config = config;
filterManager = new FilterManager(eth);
snapshotManager = new SnapshotManager(blockchain, transactionPool);
initialBlockNumber = this.blockchain.getBestBlock().getNumber();
personalModule.init(this.config);
}
Expand Down Expand Up @@ -1106,7 +1108,7 @@ public TxPoolModule getTxPoolModule() {

@Override
public String evm_snapshot() {
int snapshotId = snapshotManager.takeSnapshot(blockchain);
int snapshotId = snapshotManager.takeSnapshot();

logger.debug("evm_snapshot(): {}", snapshotId);

Expand All @@ -1117,7 +1119,7 @@ public String evm_snapshot() {
public boolean evm_revert(String snapshotId) {
try {
int sid = stringHexToBigInteger(snapshotId).intValue();
return snapshotManager.revertToSnapshot(this.blockchain, this.transactionPool, sid);
return snapshotManager.revertToSnapshot(sid);
} catch (NumberFormatException | StringIndexOutOfBoundsException e) {
throw new JsonRpcInvalidParamException("invalid snapshot id " + snapshotId, e);
} finally {
Expand All @@ -1129,7 +1131,7 @@ public boolean evm_revert(String snapshotId) {

@Override
public void evm_reset() {
snapshotManager.resetSnapshots(this.blockchain, this.transactionPool);
snapshotManager.resetSnapshots();
if (logger.isDebugEnabled()) {
logger.debug("evm_reset()");
}
Expand Down
51 changes: 18 additions & 33 deletions uscj-core/src/test/java/co/usc/core/SnapshotManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
public class SnapshotManagerTest {
private BlockChainImpl blockchain;
private TransactionPool transactionPool;
private SnapshotManager manager;

@Before
public void setUp() {
Expand All @@ -51,22 +52,19 @@ public void setUp() {
transactionPool = factory.getTransactionPool();
// don't call start to avoid creating threads
transactionPool.processBest(blockchain.getBestBlock());
manager = new SnapshotManager(blockchain, transactionPool);
}


@Test
public void createWithNoSnapshot() {
SnapshotManager manager = new SnapshotManager();

Assert.assertNotNull(manager.getSnapshots());
Assert.assertTrue(manager.getSnapshots().isEmpty());
}

@Test
public void takeSnapshotOnGenesis() {
SnapshotManager manager = new SnapshotManager();

int result = manager.takeSnapshot(blockchain);
int result = manager.takeSnapshot();

Assert.assertEquals(1, result);

Expand All @@ -80,9 +78,7 @@ public void takeSnapshotOnGenesis() {
public void takeSnapshotOnManyBlocks() {
addBlocks(blockchain, 10);

SnapshotManager manager = new SnapshotManager();

int result = manager.takeSnapshot(blockchain);
int result = manager.takeSnapshot();

Assert.assertEquals(1, result);

Expand All @@ -96,15 +92,13 @@ public void takeSnapshotOnManyBlocks() {
public void takeTwoSnapshots() {
addBlocks(blockchain, 10);

SnapshotManager manager = new SnapshotManager();

int result1 = manager.takeSnapshot(blockchain);
int result1 = manager.takeSnapshot();

Assert.assertEquals(1, result1);

addBlocks(blockchain, 10);

int result2 = manager.takeSnapshot(blockchain);
int result2 = manager.takeSnapshot();

Assert.assertEquals(2, result2);

Expand All @@ -117,18 +111,14 @@ public void takeTwoSnapshots() {

@Test
public void revertToNegativeSnapshot() {
SnapshotManager manager = new SnapshotManager();

Assert.assertFalse(manager.revertToSnapshot(null, null, -1));
Assert.assertFalse(manager.revertToSnapshot(-1));
}

@Test
public void revertToNonExistentSnapshot() {
SnapshotManager manager = new SnapshotManager();

Assert.assertFalse(manager.revertToSnapshot(null, null, 0));
Assert.assertFalse(manager.revertToSnapshot(null, null, 1));
Assert.assertFalse(manager.revertToSnapshot(null, null, 10));
Assert.assertFalse(manager.revertToSnapshot(0));
Assert.assertFalse(manager.revertToSnapshot(1));
Assert.assertFalse(manager.revertToSnapshot(10));
}

@Test
Expand All @@ -137,15 +127,13 @@ public void revertToSnapshot() {

BlockChainStatus status = blockchain.getStatus();

SnapshotManager manager = new SnapshotManager();

int snapshotId = manager.takeSnapshot(blockchain);
int snapshotId = manager.takeSnapshot();

addBlocks(blockchain, 20);

Assert.assertEquals(30, blockchain.getStatus().getBestBlockNumber());

Assert.assertTrue(manager.revertToSnapshot(blockchain, transactionPool, snapshotId));
Assert.assertTrue(manager.revertToSnapshot(snapshotId));

BlockChainStatus newStatus = blockchain.getStatus();

Expand All @@ -163,13 +151,11 @@ public void revertToSnapshotClearingTransactionPool() {

BlockChainStatus status = blockchain.getStatus();

SnapshotManager manager = new SnapshotManager();

int snapshotId = manager.takeSnapshot(blockchain);
int snapshotId = manager.takeSnapshot();

addBlocks(blockchain, 20);

manager.takeSnapshot(blockchain);
manager.takeSnapshot();

Assert.assertEquals(2, manager.getSnapshots().size());

Expand All @@ -185,7 +171,7 @@ public void revertToSnapshotClearingTransactionPool() {

Assert.assertEquals(30, blockchain.getStatus().getBestBlockNumber());

Assert.assertTrue(manager.revertToSnapshot(blockchain, transactionPool, snapshotId));
Assert.assertTrue(manager.revertToSnapshot(snapshotId));

BlockChainStatus newStatus = blockchain.getStatus();

Expand Down Expand Up @@ -220,14 +206,13 @@ public void resetSnapshotClearingTransactionPool() {
Assert.assertFalse(transactionPool.getPendingTransactions().isEmpty());
Assert.assertFalse(transactionPool.getPendingTransactions().isEmpty());

SnapshotManager manager = new SnapshotManager();
manager.takeSnapshot();

manager.takeSnapshot(blockchain);
Assert.assertFalse(manager.getSnapshots().isEmpty());
Assert.assertTrue(manager.resetSnapshots(blockchain, transactionPool));
Assert.assertTrue(manager.resetSnapshots());
Assert.assertTrue(manager.getSnapshots().isEmpty());

Assert.assertTrue(manager.resetSnapshots(blockchain, transactionPool));
Assert.assertTrue(manager.resetSnapshots());

BlockChainStatus newStatus = blockchain.getStatus();

Expand Down
6 changes: 3 additions & 3 deletions uscj-core/src/test/java/co/usc/mine/MinerManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,16 @@ public void mineBlock() {
Assert.assertEquals(1, blockchain.getBestBlock().getNumber());
Assert.assertFalse(blockchain.getBestBlock().getTransactionsList().isEmpty());

SnapshotManager snapshotManager = new SnapshotManager();
snapshotManager.resetSnapshots(blockchain, transactionPool);
SnapshotManager snapshotManager = new SnapshotManager(blockchain, transactionPool);
snapshotManager.resetSnapshots();

Assert.assertEquals(0, blockchain.getBestBlock().getNumber());

manager.mineBlock(blockchain, minerClient, minerServer);
manager.mineBlock(blockchain, minerClient, minerServer);
Assert.assertEquals(2, blockchain.getBestBlock().getNumber());

snapshotManager.resetSnapshots(blockchain, transactionPool);
snapshotManager.resetSnapshots();
Assert.assertTrue(transactionPool.getPendingTransactions().isEmpty());


Expand Down

0 comments on commit b246e5d

Please sign in to comment.