-
Notifications
You must be signed in to change notification settings - Fork 9
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
Integrate with lightwalletd and librustzcash #16
Changes from 1 commit
c99f9a3
2c0966a
986ee50
5915ad3
9448f43
13642d6
84b8756
e668946
bfdd093
21f09fa
1892e4d
913ecb4
37c9cf7
df9f020
c5e5941
09de20c
56fada2
ed5e9c0
d38c348
c7d85f2
764455d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
CREATE TABLE IF NOT EXISTS blocks ( | ||
height INTEGER PRIMARY KEY, | ||
time INTEGER, | ||
sapling_tree BLOB | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS transactions ( | ||
id_tx INTEGER PRIMARY KEY, | ||
txid BLOB NOT NULL UNIQUE, | ||
block INTEGER, | ||
raw BLOB, | ||
FOREIGN KEY (block) REFERENCES blocks(height) | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS received_notes ( | ||
id_note INTEGER PRIMARY KEY, | ||
tx INTEGER NOT NULL, | ||
output_index INTEGER NOT NULL, | ||
account INTEGER NOT NULL, | ||
diversifier BLOB NOT NULL, | ||
value INTEGER NOT NULL, | ||
rcm BLOB NOT NULL, | ||
nf BLOB NOT NULL UNIQUE, | ||
memo BLOB, | ||
spent INTEGER, | ||
FOREIGN KEY (tx) REFERENCES transactions(id_tx), | ||
FOREIGN KEY (spent) REFERENCES transactions(id_tx), | ||
CONSTRAINT tx_output UNIQUE (tx, output_index) | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS sapling_witnesses ( | ||
id_witness INTEGER PRIMARY KEY, | ||
note INTEGER NOT NULL, | ||
block INTEGER NOT NULL, | ||
witness BLOB NOT NULL, | ||
FOREIGN KEY (note) REFERENCES received_notes(id_note), | ||
FOREIGN KEY (block) REFERENCES blocks(height), | ||
CONSTRAINT witness_height UNIQUE (note, block) | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package cash.z.wallet.sdk.dao | ||
|
||
import androidx.arch.core.executor.testing.InstantTaskExecutorRule | ||
import androidx.room.Room | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import cash.z.wallet.sdk.db.CompactBlockDb | ||
import cash.z.wallet.sdk.db.DerivedDataDb | ||
import cash.z.wallet.sdk.vo.CompactBlock | ||
import cash.z.wallet.sdk.vo.Transaction | ||
import org.junit.* | ||
import org.junit.Assert.* | ||
|
||
class TransactionDaoTest { | ||
|
||
@get:Rule | ||
var instantTaskExecutorRule = InstantTaskExecutorRule() | ||
|
||
private lateinit var dao: TransactionDao | ||
private lateinit var db: DerivedDataDb | ||
|
||
@Before | ||
fun initDb() { | ||
db = Room.inMemoryDatabaseBuilder( | ||
InstrumentationRegistry.getInstrumentation().context, | ||
DerivedDataDb::class.java | ||
) | ||
.build() | ||
.apply { dao = transactionDao() } | ||
} | ||
|
||
@After | ||
fun close() { | ||
db.close() | ||
} | ||
|
||
@Test | ||
fun testDbExists() { | ||
assertNotNull(db) | ||
} | ||
|
||
@Test | ||
fun testDaoExists() { | ||
assertNotNull(dao) | ||
} | ||
|
||
@Test | ||
fun testDaoInsert() { | ||
Transaction(4, "sample".toByteArray(), 356418, null).let { transaction -> | ||
dao.insert(transaction) | ||
val result = dao.findById(transaction.id) | ||
assertEquals(transaction.id, result?.id) | ||
assertTrue(transaction.transactionId.contentEquals(result!!.transactionId)) | ||
dao.delete(transaction) | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package cash.z.wallet.sdk.db | ||
|
||
import androidx.arch.core.executor.testing.InstantTaskExecutorRule | ||
import androidx.room.Room | ||
import androidx.room.RoomDatabase | ||
import androidx.test.core.app.ApplicationProvider | ||
import cash.z.wallet.sdk.dao.BlockDao | ||
import cash.z.wallet.sdk.dao.CompactBlockDao | ||
import cash.z.wallet.sdk.dao.NoteDao | ||
import cash.z.wallet.sdk.dao.TransactionDao | ||
import cash.z.wallet.sdk.vo.CompactBlock | ||
import org.junit.* | ||
import org.junit.Assert.* | ||
|
||
class DerivedDbIntegrationTest { | ||
@get:Rule | ||
var instantTaskExecutorRule = InstantTaskExecutorRule() | ||
|
||
@Test | ||
fun testDbExists() { | ||
assertNotNull(db) | ||
} | ||
|
||
@Test | ||
fun testDaoExists_Transaction() { | ||
assertNotNull(transactions) | ||
} | ||
|
||
@Test | ||
fun testDaoExists_Block() { | ||
assertNotNull(blocks) | ||
} | ||
|
||
@Test | ||
fun testDaoExists_Note() { | ||
assertNotNull(notes) | ||
} | ||
|
||
@Test | ||
fun testCount_Transaction() { | ||
assertEquals(5, transactions.count()) | ||
} | ||
|
||
@Test | ||
fun testCount_Block() { | ||
assertEquals(80101, blocks.count()) | ||
} | ||
|
||
@Test | ||
fun testCount_Note() { | ||
assertEquals(5, notes.count()) | ||
} | ||
@Test | ||
fun testTransactionDaoPrepopulated() { | ||
val tran = transactions.findById(1) | ||
|
||
assertEquals(343987, tran?.block) | ||
} | ||
|
||
@Test | ||
fun testBlockDaoPrepopulated() { | ||
val tran = blocks.findById(1)?.apply { | ||
assertEquals(343987, this.height) | ||
} | ||
} | ||
|
||
companion object { | ||
private lateinit var transactions: TransactionDao | ||
private lateinit var blocks: BlockDao | ||
private lateinit var notes: NoteDao | ||
private lateinit var db: DerivedDataDb | ||
|
||
@BeforeClass | ||
@JvmStatic | ||
fun setup() { | ||
// TODO: put this database in the assets directory and open it from there via .openHelperFactory(new AssetSQLiteOpenHelperFactory()) seen here https://github.com/albertogiunta/sqliteAsset | ||
db = Room | ||
.databaseBuilder(ApplicationProvider.getApplicationContext(), DerivedDataDb::class.java, "dummy-data2.db") | ||
.setJournalMode(RoomDatabase.JournalMode.TRUNCATE) | ||
.fallbackToDestructiveMigration() | ||
.build() | ||
.apply { | ||
transactions = transactionDao() | ||
blocks = blockDao() | ||
notes = noteDao() | ||
} | ||
} | ||
|
||
@AfterClass | ||
@JvmStatic | ||
fun close() { | ||
db.close() | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cash.z.wallet.sdk.dao | ||
|
||
import androidx.room.* | ||
import cash.z.wallet.sdk.vo.Block | ||
import androidx.lifecycle.LiveData | ||
|
||
|
||
|
||
@Dao | ||
interface BlockDao { | ||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
fun insert(block: Block) | ||
|
||
@Query("SELECT * FROM blocks WHERE height = :height") | ||
fun findById(height: Int): Block? | ||
|
||
@Query("DELETE FROM blocks WHERE height = :height") | ||
fun deleteById(height: Int) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto re: modifications to data DB from Android side. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 81141d9 |
||
|
||
@Delete | ||
fun delete(block: Block) | ||
|
||
@Query("SELECT COUNT(height) FROM blocks") | ||
fun count(): Int | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cash.z.wallet.sdk.dao | ||
|
||
import androidx.room.* | ||
import cash.z.wallet.sdk.vo.Note | ||
|
||
@Dao | ||
interface NoteDao { | ||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
fun insert(block: Note) | ||
|
||
@Query("SELECT * FROM received_notes WHERE id_note = :id") | ||
fun findById(id: Int): Note? | ||
|
||
@Query("DELETE FROM received_notes WHERE id_note = :id") | ||
fun deleteById(id: Int) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This allows modifications to the data DB from the Android side. Do we want this as part of the public API? What is it used for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 81141d9 |
||
|
||
@Query("SELECT * FROM received_notes WHERE 1") | ||
fun getAll(): List<Note> | ||
|
||
@Delete | ||
fun delete(block: Note) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know how Also, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 81141d9 |
||
|
||
@Query("SELECT COUNT(id_note) FROM received_notes") | ||
fun count(): Int | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cash.z.wallet.sdk.dao | ||
|
||
import androidx.room.* | ||
import cash.z.wallet.sdk.vo.Transaction | ||
|
||
@Dao | ||
interface TransactionDao { | ||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
fun insert(block: Transaction) | ||
|
||
@Query("SELECT * FROM transactions WHERE id_tx = :id") | ||
fun findById(id: Int): Transaction? | ||
|
||
@Query("DELETE FROM transactions WHERE id_tx = :id") | ||
fun deleteById(id: Int) | ||
|
||
@Query("SELECT * FROM transactions WHERE 1") | ||
fun getAll(): List<Transaction> | ||
|
||
@Delete | ||
fun delete(block: Transaction) | ||
|
||
@Query("SELECT COUNT(id_tx) FROM transactions") | ||
fun count(): Int | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cash.z.wallet.sdk.db | ||
|
||
import androidx.room.Database | ||
import androidx.room.RoomDatabase | ||
import cash.z.wallet.sdk.dao.BlockDao | ||
import cash.z.wallet.sdk.dao.NoteDao | ||
import cash.z.wallet.sdk.dao.TransactionDao | ||
import cash.z.wallet.sdk.vo.Block | ||
import cash.z.wallet.sdk.vo.Note | ||
import cash.z.wallet.sdk.vo.Transaction | ||
|
||
@Database( | ||
entities = [ | ||
Transaction::class, | ||
Block::class, | ||
Note::class | ||
], | ||
version = 1, | ||
exportSchema = false | ||
) | ||
abstract class DerivedDataDb : RoomDatabase() { | ||
abstract fun transactionDao(): TransactionDao | ||
abstract fun noteDao(): NoteDao | ||
abstract fun blockDao(): BlockDao | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test suggests that inserts into the data db from the Android side are okay. Add a comment that this is only for testing purposes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 81141d9