Added missing files.

This commit is contained in:
Adrian Tiberius 2015-06-23 21:56:14 +02:00
parent 877e237a27
commit 8314d132ea
6 changed files with 8495 additions and 0 deletions

View File

@ -0,0 +1,311 @@
package org.ethereum.android_app;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import com.j256.ormlite.android.apptools.OpenHelperManager;
import org.ethereum.android.db.OrmLiteBlockStoreDatabase;
import org.ethereum.android.util.Scanner;
import org.ethereum.config.SystemProperties;
import org.ethereum.core.Block;
import org.ethereum.db.BlockStore;
import org.ethereum.android.db.InMemoryBlockStore;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex;
import java.io.File;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import static java.math.BigInteger.ZERO;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
@RunWith(AndroidJUnit4.class)
public class InMemoryBlockStoreTest extends ActivityInstrumentationTestCase2<TestActivity> {
private static final Logger logger = LoggerFactory.getLogger("test");
private static boolean loaded = false;
private TestActivity activity;
private List<Block> blocks = new ArrayList<>();
private OrmLiteBlockStoreDatabase database;
public InMemoryBlockStoreTest() {
super(TestActivity.class);
}
@Before
public void setUp() throws Exception {
super.setUp();
loaded = false;
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
activity = getActivity();
InputStream inputStream = InstrumentationRegistry.getInstrumentation().getTargetContext().getAssets().open("blockstore");
final Scanner scanner = new Scanner(inputStream);
ThreadGroup group = new ThreadGroup("threadGroup");
new Thread(group, new Runnable() {
@Override
public void run() {
logger.info("Loading blocks.");
BigInteger cumDifficulty = ZERO;
while (scanner.hasNext()) {
String blockString = scanner.nextLine();
logger.info("BlockString: " + blockString);
try {
Block block = new Block(
Hex.decode(blockString));
//if (block.getNumber() % 1000 == 0)
logger.info("adding block.hash: [{}] block.number: [{}]",
block.getShortHash(),
block.getNumber());
blocks.add(block);
cumDifficulty = cumDifficulty.add(block.getCumulativeDifficulty());
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
logger.info("total difficulty: {}", cumDifficulty);
InMemoryBlockStoreTest.loaded = true;
}
}, "EthereumConnect", 32768000).start();
database = OpenHelperManager.getHelper(activity, OrmLiteBlockStoreDatabase.class);
}
@Test
public void testSet0() {
while(!loaded) {
//Thread.sleep(1000);
}
assertThat(activity, notNullValue());
assertThat(getInstrumentation(), notNullValue());
}
@Test
public void testEmpty(){
while(!loaded) {
//Thread.sleep(1000);
}
BlockStore blockStore = new InMemoryBlockStore(database);
assertNull(blockStore.getBestBlock());
}
@Test
public void testFlush(){
while(!loaded) {
//Thread.sleep(1000);
}
BlockStore blockStore = new InMemoryBlockStore(database);
for( Block block : blocks ){
blockStore.saveBlock(block, null);
}
blockStore.flush();
}
@Test
public void testSimpleLoad(){
while(!loaded) {
//Thread.sleep(1000);
}
BlockStore blockStore = new InMemoryBlockStore(database);
for( Block block : blocks ){
blockStore.saveBlock(block, null);
}
blockStore.flush();
blockStore = new InMemoryBlockStore(database);
blockStore.load();
assertTrue(blockStore.getBestBlock().getNumber() == 8003);
}
@Test
public void testFlushEach1000(){
while(!loaded) {
//Thread.sleep(1000);
}
InMemoryBlockStore blockStore = new InMemoryBlockStore(database);
for( int i = 0; i < blocks.size(); ++i ){
blockStore.saveBlock(blocks.get(i), null);
if ( i % 1000 == 0){
blockStore.flush();
assertTrue(blockStore.blocks.size() == 1);
}
}
}
@Test
public void testBlockHashByNumber(){
while(!loaded) {
//Thread.sleep(1000);
}
BlockStore blockStore = new InMemoryBlockStore(database);
for( Block block : blocks ){
blockStore.saveBlock(block, null);
}
String hash = Hex.toHexString(blockStore.getBlockHashByNumber(7000));
assertTrue(hash.startsWith("459a8f"));
hash = Hex.toHexString(blockStore.getBlockHashByNumber(6000));
assertTrue(hash.startsWith("7a577a"));
hash = Hex.toHexString(blockStore.getBlockHashByNumber(5000));
assertTrue(hash.startsWith("820aa7"));
blockStore.flush();
hash = Hex.toHexString(blockStore.getBlockHashByNumber(7000));
assertTrue(hash.startsWith("459a8f"));
hash = Hex.toHexString(blockStore.getBlockHashByNumber(6000));
assertTrue(hash.startsWith("7a577a"));
hash = Hex.toHexString(blockStore.getBlockHashByNumber(5000));
assertTrue(hash.startsWith("820aa7"));
}
@Test
public void testBlockByNumber(){
while(!loaded) {
//Thread.sleep(1000);
}
BlockStore blockStore = new InMemoryBlockStore(database);
for( Block block : blocks ){
blockStore.saveBlock(block, null);
}
String hash = Hex.toHexString(blockStore.getBlockByNumber(7000).getHash());
assertTrue(hash.startsWith("459a8f"));
hash = Hex.toHexString(blockStore.getBlockByNumber(6000).getHash());
assertTrue(hash.startsWith("7a577a"));
hash = Hex.toHexString(blockStore.getBlockByNumber(5000).getHash());
assertTrue(hash.startsWith("820aa7"));
blockStore.flush();
hash = Hex.toHexString(blockStore.getBlockByNumber(7000).getHash());
assertTrue(hash.startsWith("459a8f"));
hash = Hex.toHexString(blockStore.getBlockByNumber(6000).getHash());
assertTrue(hash.startsWith("7a577a"));
hash = Hex.toHexString(blockStore.getBlockByNumber(5000).getHash());
assertTrue(hash.startsWith("820aa7"));
}
@Test
public void testGetBlockByNumber() {
while(!loaded) {
//Thread.sleep(1000);
}
BlockStore blockStore = new InMemoryBlockStore(database);
for( Block block : blocks ){
blockStore.saveBlock(block, null);
}
assertEquals("4312750101", blockStore.getTotalDifficulty().toString());
blockStore.flush();
assertEquals("4312750101", blockStore.getTotalDifficulty().toString());
}
@Test
public void testDbGetBlockByHash(){
while(!loaded) {
//Thread.sleep(1000);
}
BlockStore blockStore = new InMemoryBlockStore(database);
for( Block block : blocks ){
blockStore.saveBlock(block, null);
}
byte[] hash7000 = Hex.decode("459a8f0ee5d4b0c9ea047797606c94f0c1158ed0f30120490b96f7df9893e1fa");
byte[] hash6000 = Hex.decode("7a577a6b0b7e72e51a646c4cec82cf684c977bca6307e2a49a4116af49316159");
byte[] hash5000 = Hex.decode("820aa786619e1a2ae139877ba342078c83e5bd65c559069336c13321441e03dc");
Long number = blockStore.getBlockByHash(hash7000).getNumber();
assertTrue(number == 7000);
number = blockStore.getBlockByHash(hash6000).getNumber();
assertTrue(number == 6000);
number = blockStore.getBlockByHash(hash5000).getNumber();
assertTrue(number == 5000);
}
/*
@Ignore // TO much time to run it on general basis
@Test
public void save100KBlocks() throws FileNotFoundException {
String blocksFile = "E:\\temp\\_poc-9-blocks\\poc-9-492k.dmp";
FileInputStream inputStream = new FileInputStream(blocksFile);
Scanner scanner = new Scanner(inputStream, "UTF-8");
BlockStore blockStore = new InMemoryBlockStore();
//blockStore.setSessionFactory(sessionFactory());
while (scanner.hasNextLine()) {
byte[] blockRLPBytes = Hex.decode( scanner.nextLine());
Block block = new Block(blockRLPBytes);
System.out.println(block.getNumber());
blockStore.saveBlock(block, null);
if (block.getNumber() > 100_000) break;
}
blockStore.flush();
}
*/
}

View File

@ -0,0 +1,124 @@
package org.ethereum.android_app;
import android.os.Environment;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import org.ethereum.android.datasource.LevelDbDataSource;
import org.ethereum.config.SystemProperties;
import org.ethereum.datasource.KeyValueDataSource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.spongycastle.util.encoders.Hex;
import java.io.File;
import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.is;
@RunWith(AndroidJUnit4.class)
public class LevelDbDataSourceTest extends ActivityInstrumentationTestCase2<TestActivity> {
private TestActivity activity;
public LevelDbDataSourceTest() {
super(TestActivity.class);
}
@Before
public void setUp() throws Exception {
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
activity = getActivity();
System.setProperty("sun.arch.data.model", "32");
System.setProperty("leveldb.mmap", "false");
String databaseFolder = null;
File extStore = Environment.getExternalStorageDirectory();
if (extStore.exists()) {
databaseFolder = extStore.getAbsolutePath();
} else {
databaseFolder = activity.getApplicationInfo().dataDir;
}
if (databaseFolder != null) {
System.out.println("Database folder: " + databaseFolder);
SystemProperties.CONFIG.setDataBaseDir(databaseFolder);
}
}
@Test
public void testSet0() {
assertThat(activity, notNullValue());
assertThat(getInstrumentation(), notNullValue());
}
@Test
public void testSet1() {
KeyValueDataSource dataSource = createDataSource("test-state");
try {
byte[] key = Hex.decode("a1a2a3");
byte[] val = Hex.decode("b1b2b3");
dataSource.put(key, val);
byte[] val2 = dataSource.get(key);
assertThat(Hex.toHexString(val), is(Hex.toHexString(val2)));
} finally {
clear(dataSource);
}
}
@Test
public void testSet2() {
KeyValueDataSource states = createDataSource("test-state");
KeyValueDataSource details = createDataSource("test-details");
try {
byte[] key = Hex.decode("a1a2a3");
byte[] val1 = Hex.decode("b1b2b3");
byte[] val2 = Hex.decode("c1c2c3");
states.put(key, val1);
details.put(key, val2);
byte[] res1 = states.get(key);
byte[] res2 = details.get(key);
assertThat(Hex.toHexString(val1), is(Hex.toHexString(res1)));
assertThat(Hex.toHexString(val2), is(Hex.toHexString(res2)));
} finally {
clear(states);
clear(details);
}
}
@After
public void tearDown() throws Exception {
super.tearDown();
}
private KeyValueDataSource createDataSource(String name) {
LevelDbDataSource result = new LevelDbDataSource();
result.setContext(activity);
result.setName(name);
result.init();
return result;
}
private void clear(KeyValueDataSource dataSource) {
((LevelDbDataSource) dataSource).close();
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,38 @@
package org.ethereum.android_app;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class TestActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_test, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

View File

@ -0,0 +1,12 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="org.ethereum.android_app.TestActivity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>

View File

@ -0,0 +1,7 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="org.ethereum.android_app.TestActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>