Added storeAllBlocks option to EthereumModule.
Added block_transaction database table and getTransactionLocation method.
This commit is contained in:
parent
44f7ec4ae5
commit
a9f208c82d
|
@ -33,4 +33,10 @@ public interface BlockStoreDatabase {
|
|||
public TransactionReceipt getTransactionReceiptByHash(byte[] hash);
|
||||
|
||||
public boolean flush(List<Block> blocks);
|
||||
|
||||
public void save(BlockTransactionVO blockTransactionVO);
|
||||
|
||||
public BlockTransactionVO getTransactionLocation(byte[] transactionHash);
|
||||
|
||||
public void setFullStorage(boolean storeAllBlocks);
|
||||
}
|
||||
|
|
|
@ -65,16 +65,22 @@ public class BlockStoreImpl implements BlockStore {
|
|||
|
||||
public void saveBlock(Block block, List<TransactionReceipt> receipts) {
|
||||
|
||||
BlockVO blockVO = new BlockVO(block.getNumber(), block.getHash(),
|
||||
byte[] blockHash = block.getHash();
|
||||
BlockVO blockVO = new BlockVO(block.getNumber(), blockHash,
|
||||
block.getEncoded(), block.getCumulativeDifficulty());
|
||||
|
||||
int index = 0;
|
||||
for (TransactionReceipt receipt : receipts) {
|
||||
|
||||
byte[] hash = receipt.getTransaction().getHash();
|
||||
byte[] transactionHash = receipt.getTransaction().getHash();
|
||||
byte[] rlp = receipt.getEncoded();
|
||||
|
||||
TransactionReceiptVO transactionReceiptVO = new TransactionReceiptVO(hash, rlp);
|
||||
TransactionReceiptVO transactionReceiptVO = new TransactionReceiptVO(transactionHash, rlp);
|
||||
database.save(transactionReceiptVO);
|
||||
|
||||
BlockTransactionVO blockTransactionVO = new BlockTransactionVO(blockHash, transactionHash, index);
|
||||
database.save(blockTransactionVO);
|
||||
index++;
|
||||
}
|
||||
|
||||
database.save(blockVO);
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
package org.ethereum.android.db;
|
||||
|
||||
|
||||
import com.j256.ormlite.field.DataType;
|
||||
import com.j256.ormlite.field.DatabaseField;
|
||||
import com.j256.ormlite.table.DatabaseTable;
|
||||
|
||||
@DatabaseTable(tableName = "block_transaction")
|
||||
public class BlockTransactionVO {
|
||||
|
||||
@DatabaseField(index = true, persisterClass = HashPersister.class)
|
||||
byte[] blockHash;
|
||||
|
||||
@DatabaseField(index = true, persisterClass = HashPersister.class)
|
||||
byte[] transactionHash;
|
||||
|
||||
@DatabaseField(dataType = DataType.INTEGER)
|
||||
int transactionIndex;
|
||||
|
||||
public BlockTransactionVO() {
|
||||
|
||||
}
|
||||
|
||||
public BlockTransactionVO(byte[] blockHash, byte[] transactionHash, int transactionIndex) {
|
||||
|
||||
this.blockHash = blockHash;
|
||||
this.transactionHash = transactionHash;
|
||||
this.transactionIndex = transactionIndex;
|
||||
}
|
||||
|
||||
public byte[] getBlockHash() {
|
||||
return blockHash;
|
||||
}
|
||||
|
||||
public void setBlockHash(byte[] blockHash) {
|
||||
this.blockHash = blockHash;
|
||||
}
|
||||
|
||||
public byte[] getTransactionHash() {
|
||||
return transactionHash;
|
||||
}
|
||||
|
||||
public void setTransactionHash(byte[] transactionHash) {
|
||||
this.transactionHash = transactionHash;
|
||||
}
|
||||
|
||||
public int getTransactionIndex() {
|
||||
return transactionIndex;
|
||||
}
|
||||
|
||||
public void setTransactionIndex(int transactionIndex) {
|
||||
this.transactionIndex = transactionIndex;
|
||||
}
|
||||
}
|
|
@ -27,6 +27,7 @@ public class InMemoryBlockStore implements BlockStore {
|
|||
List<Block> blocks = new ArrayList<>();
|
||||
|
||||
private BlockStoreDatabase database;
|
||||
protected boolean storeAllBlocks = false;
|
||||
|
||||
BigInteger totalDifficulty = ZERO;
|
||||
|
||||
|
@ -35,6 +36,13 @@ public class InMemoryBlockStore implements BlockStore {
|
|||
this.database = database;
|
||||
}
|
||||
|
||||
public InMemoryBlockStore(BlockStoreDatabase database, boolean storeAllBlocks) {
|
||||
|
||||
this.database = database;
|
||||
this.database.setFullStorage(storeAllBlocks);
|
||||
this.storeAllBlocks = storeAllBlocks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getBlockHashByNumber(long blockNumber) {
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@ import com.j256.ormlite.table.TableUtils;
|
|||
|
||||
import org.ethereum.config.SystemProperties;
|
||||
import org.ethereum.core.Block;
|
||||
import org.ethereum.core.Transaction;
|
||||
import org.ethereum.core.TransactionReceipt;
|
||||
|
||||
import java.io.File;
|
||||
|
@ -33,6 +34,9 @@ public class OrmLiteBlockStoreDatabase extends OrmLiteSqliteOpenHelper implement
|
|||
|
||||
private Dao<BlockVO, Integer> blockDao = null;
|
||||
private Dao<TransactionReceiptVO, Integer> transactionDao = null;
|
||||
private Dao<BlockTransactionVO, Integer> blockTransactionDao = null;
|
||||
|
||||
protected boolean storeAllBLocks = false;
|
||||
|
||||
public static synchronized OrmLiteBlockStoreDatabase getHelper(Context context)
|
||||
{
|
||||
|
@ -58,6 +62,7 @@ public class OrmLiteBlockStoreDatabase extends OrmLiteSqliteOpenHelper implement
|
|||
Log.i(OrmLiteBlockStoreDatabase.class.getName(), "onCreate");
|
||||
TableUtils.createTable(connectionSource, BlockVO.class);
|
||||
TableUtils.createTable(connectionSource, TransactionReceiptVO.class);
|
||||
TableUtils.createTable(connectionSource, BlockTransactionVO.class);
|
||||
} catch (SQLException e) {
|
||||
Log.e(OrmLiteBlockStoreDatabase.class.getName(), "Can't create database", e);
|
||||
throw new RuntimeException(e);
|
||||
|
@ -75,6 +80,7 @@ public class OrmLiteBlockStoreDatabase extends OrmLiteSqliteOpenHelper implement
|
|||
Log.i(OrmLiteBlockStoreDatabase.class.getName(), "onUpgrade");
|
||||
TableUtils.dropTable(connectionSource, BlockVO.class, true);
|
||||
TableUtils.dropTable(connectionSource, TransactionReceiptVO.class, true);
|
||||
TableUtils.dropTable(connectionSource, BlockTransactionVO.class, true);
|
||||
// after we drop the old databases, we create the new ones
|
||||
onCreate(db, connectionSource);
|
||||
} catch (SQLException e) {
|
||||
|
@ -106,6 +112,17 @@ public class OrmLiteBlockStoreDatabase extends OrmLiteSqliteOpenHelper implement
|
|||
return transactionDao;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached
|
||||
* value.
|
||||
*/
|
||||
public Dao<BlockTransactionVO, Integer> getBlockTransactionDao() throws SQLException {
|
||||
if (blockTransactionDao == null) {
|
||||
blockTransactionDao = getDao(BlockTransactionVO.class);
|
||||
}
|
||||
return blockTransactionDao;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the database connections and clear any cached DAOs.
|
||||
*/
|
||||
|
@ -253,6 +270,15 @@ public class OrmLiteBlockStoreDatabase extends OrmLiteSqliteOpenHelper implement
|
|||
}
|
||||
}
|
||||
|
||||
public void save(BlockTransactionVO blockTransactionV0) {
|
||||
|
||||
try {
|
||||
getBlockTransactionDao().create(blockTransactionV0);
|
||||
} catch(java.sql.SQLException e) {
|
||||
Log.e(OrmLiteBlockStoreDatabase.class.getName(), "Error saving blockTransaction", e);
|
||||
}
|
||||
}
|
||||
|
||||
public TransactionReceipt getTransactionReceiptByHash(byte[] hash) {
|
||||
|
||||
List<TransactionReceiptVO> list = new ArrayList<TransactionReceiptVO>();
|
||||
|
@ -269,19 +295,54 @@ public class OrmLiteBlockStoreDatabase extends OrmLiteSqliteOpenHelper implement
|
|||
|
||||
}
|
||||
|
||||
public BlockTransactionVO getTransactionLocation(byte[] transactionHash) {
|
||||
|
||||
List<BlockTransactionVO> list = new ArrayList<>();
|
||||
try {
|
||||
list = getBlockTransactionDao().queryForEq("transactionHash", transactionHash);
|
||||
} catch(java.sql.SQLException e) {
|
||||
Log.e(OrmLiteBlockStoreDatabase.class.getName(), "Error querying for transaction hash", e);
|
||||
}
|
||||
|
||||
if (list.size() == 0) return null;
|
||||
BlockTransactionVO blockTransactionVO = list.get(0);
|
||||
|
||||
return blockTransactionVO;
|
||||
}
|
||||
|
||||
public boolean flush(final List<Block> blocks) {
|
||||
|
||||
if (!storeAllBLocks) {
|
||||
reset();
|
||||
}
|
||||
try {
|
||||
TransactionManager.callInTransaction(getBlockDao().getConnectionSource(),
|
||||
new Callable<Void>() {
|
||||
public Void call() throws Exception {
|
||||
|
||||
if (storeAllBLocks) {
|
||||
for (Block block: blocks) {
|
||||
byte[] blockHash = block.getHash();
|
||||
BlockVO blockVO = new BlockVO(block.getNumber(), blockHash, block.getEncoded(), block.getCumulativeDifficulty());
|
||||
save(blockVO);
|
||||
|
||||
List<Transaction> transactions = block.getTransactionsList();
|
||||
|
||||
int index = 0;
|
||||
for (Transaction transaction: transactions) {
|
||||
BlockTransactionVO transactionVO = new BlockTransactionVO(blockHash, transaction.getHash(), index);
|
||||
save(transactionVO);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
int lastIndex = blocks.size() - 1;
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
Block block = blocks.get(lastIndex - i);
|
||||
BlockVO blockVO = new BlockVO(block.getNumber(), block.getHash(), block.getEncoded(), block.getCumulativeDifficulty());
|
||||
save(blockVO);
|
||||
}
|
||||
}
|
||||
// you could pass back an object here
|
||||
return null;
|
||||
}
|
||||
|
@ -290,8 +351,13 @@ public class OrmLiteBlockStoreDatabase extends OrmLiteSqliteOpenHelper implement
|
|||
|
||||
return true;
|
||||
} catch(java.sql.SQLException e) {
|
||||
Log.e(OrmLiteBlockStoreDatabase.class.getName(), "Error querying for hash", e);
|
||||
Log.e(OrmLiteBlockStoreDatabase.class.getName(), "Error flushing blocks", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void setFullStorage(boolean storeAllBlocks) {
|
||||
|
||||
this.storeAllBLocks = storeAllBlocks;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,8 +44,18 @@ public class EthereumModule {
|
|||
|
||||
private Context context;
|
||||
|
||||
boolean storeAllBlocks;
|
||||
|
||||
public EthereumModule(Context context) {
|
||||
|
||||
this.context = context;
|
||||
this.storeAllBlocks = false;
|
||||
}
|
||||
|
||||
public EthereumModule(Context context,boolean storeAllBlocks) {
|
||||
|
||||
this.context = context;
|
||||
this.storeAllBlocks = storeAllBlocks;
|
||||
}
|
||||
|
||||
@Provides
|
||||
|
@ -74,7 +84,7 @@ public class EthereumModule {
|
|||
@Singleton
|
||||
BlockStore provideBlockStore() {
|
||||
OrmLiteBlockStoreDatabase database = OrmLiteBlockStoreDatabase.getHelper(context);
|
||||
return new InMemoryBlockStore(database);
|
||||
return new InMemoryBlockStore(database, storeAllBlocks);
|
||||
}
|
||||
|
||||
@Provides
|
||||
|
|
Loading…
Reference in New Issue