Skip to content

Commit

Permalink
remove remaining ad-hoc previous hash passing
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesmac committed Nov 19, 2024
1 parent 9b62e5f commit 4afea98
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 24 deletions.
4 changes: 2 additions & 2 deletions sdk/src/androidTest/java/network/xyo/client/XyoPanelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class XyoPanelTest {
runBlocking {
val witnessAccount = Account.fromPrivateKey("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")
val witness2Account = Account.fromPrivateKey("5a95531488b4d0d3645aea49678297ae9e2034879ce0389b80eb788e8b533592")
val witness = XyoWitness(witnessAccount, fun(_: Context, _: String?): List<XyoPayload> {
val witness = XyoWitness(witnessAccount, fun(_: Context): List<XyoPayload> {
return listOf(BasicPayload())
})
val panel = XyoPanel(appContext, Account.random(), arrayListOf(Pair(nodeUrl, Account.random())), listOf(witness, XyoSystemInfoWitness(witness2Account), XyoLocationWitness()))
Expand Down Expand Up @@ -94,7 +94,7 @@ class XyoPanelTest {
@Test
fun testSimplePanelReport() {
runBlocking {
val panel = XyoPanel(appContext, Account.random(), fun(_:Context, _: String?): List<XyoEventPayload> {
val panel = XyoPanel(appContext, Account.random(), fun(_:Context): List<XyoEventPayload> {
return listOf(XyoEventPayload("test_event"))
})
val result = panel.reportAsyncQuery()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class XyoPayloadTest {
@Test
fun testRoundTripPanel() {
val address = Account.fromPrivateKey("5a95531488b4d0d3645aea49678297ae9e2034879ce0389b80eb788e8b533592")
val witness = XyoWitness(address, fun(_: Context, _: String?): List<XyoPayload> {
val witness = XyoWitness(address, fun(_: Context): List<XyoPayload> {
return listOf(BasicPayload())
})

Expand Down
9 changes: 3 additions & 6 deletions sdk/src/main/java/network/xyo/client/XyoPanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class XyoPanel(
private val witnesses: List<XyoWitness<XyoPayload>>?,
private val nodeUrlsAndAccounts: ArrayList<Pair<String, AccountInstance?>>?
) {
var previousHash: String? = null
private var nodes: MutableList<NodeClient>? = null
var defaultAccount: AccountInstance? = null

Expand Down Expand Up @@ -80,7 +79,7 @@ class XyoPanel(
constructor(
context: Context,
account: AccountInstance,
observe: ((context: Context, previousHash: String?) -> List<XyoEventPayload>?)?
observe: ((context: Context) -> List<XyoEventPayload>?)?
): this(
context,
account,
Expand Down Expand Up @@ -114,7 +113,7 @@ class XyoPanel(
suspend fun eventAsync(event: String): XyoPanelReportResult {
val adhocWitnessList = listOf(
XyoWitness({
_, previousHash -> listOf(XyoEventPayload(event))
_, -> listOf(XyoEventPayload(event))
})
)
return this.reportAsync(adhocWitnessList)
Expand All @@ -124,7 +123,7 @@ class XyoPanel(
suspend fun eventAsyncQuery(event: String): XyoPanelReportQueryResult {
val adhocWitnessList = listOf(
XyoWitness({
_, previousHash -> listOf(XyoEventPayload(event))
_, -> listOf(XyoEventPayload(event))
})
)
return reportAsyncQuery(adhocWitnessList)
Expand Down Expand Up @@ -167,7 +166,6 @@ class XyoPanel(
@kotlinx.coroutines.ExperimentalCoroutinesApi
suspend fun reportAsync(adhocWitnesses: List<XyoWitness<XyoPayload>> = emptyList()): XyoPanelReportResult {
val bw = generateBoundWitnessJson(adhocWitnesses)
previousHash = bw._hash
val results = mutableListOf<PostBoundWitnessesResult>()
archivists?.forEach { archivist ->
results.add(archivist.postBoundWitnessAsync(bw))
Expand All @@ -179,7 +177,6 @@ class XyoPanel(
suspend fun reportAsyncQuery(adhocWitnesses: List<XyoWitness<XyoPayload>> = emptyList()): XyoPanelReportQueryResult {
if (nodes == null) resolveNodes()
val bw = generateBoundWitnessJson()
previousHash = bw._hash
val payloads = generatePayloads(adhocWitnesses)
val results = mutableListOf<PostQueryResult>()

Expand Down
17 changes: 7 additions & 10 deletions sdk/src/main/java/network/xyo/client/XyoWitness.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,34 @@ import network.xyo.client.account.model.AccountInstance
import network.xyo.client.payload.XyoPayload

abstract class DeferredObserver<out T: XyoPayload> {
abstract suspend fun deferredDetect(context: Context, previousHash: String?): List<T>?
abstract suspend fun deferredDetect(context: Context): List<T>?
}

@RequiresApi(Build.VERSION_CODES.M)
open class XyoWitness<out T: XyoPayload> (
val address: AccountInstance = Account.random(),
private val observer: ((context: Context, previousHash: String) -> List<T>?)? = null,
var previousHash: String = "",
private val observer: ((context: Context) -> List<T>?)? = null,
val deferredObserver: DeferredObserver<T>? = null
) {

constructor(
observer: ((context: Context, previousHash: String) -> List<T>?)?,
previousHash: String = "",
observer: ((context: Context) -> List<T>?)?,
account: AccountInstance = Account.random()
): this(account, observer, previousHash, null)
): this(account, observer)

constructor(
observer: DeferredObserver<T>?,
previousHash: String = "",
account: AccountInstance = Account.random()
): this(account, null, previousHash, observer)
): this(account, null, observer)

open suspend fun observe(context: Context): List<T>? {
val appContext = context.applicationContext
if (deferredObserver !== null) {
val payload = deferredObserver.deferredDetect(appContext, previousHash)
val payload = deferredObserver.deferredDetect(appContext)
return payload
}
observer?.let {
val payloads = it(appContext, previousHash)
val payloads = it(appContext)
return payloads
}
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import network.xyo.client.account.model.AccountInstance
class DeferredLocationObserver : DeferredObserver<XyoLocationPayload>() {
override suspend fun deferredDetect(
context: Context,
previousHash: String?
): List<XyoLocationPayload>? {
try {
return listOf(XyoLocationPayload.detect(context))
Expand All @@ -26,6 +25,5 @@ class DeferredLocationObserver : DeferredObserver<XyoLocationPayload>() {
@RequiresApi(Build.VERSION_CODES.M)
class XyoLocationWitness(address: AccountInstance = Account.random()) : XyoWitness<XyoLocationPayload>(
DeferredLocationObserver(),
"",
address
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import network.xyo.client.account.model.AccountInstance

@RequiresApi(Build.VERSION_CODES.M)
class XyoSystemInfoWitness(address: AccountInstance = Account.random()) : XyoWitness<XyoSystemInfoPayload>(
fun (context: Context, _: String?): List<XyoSystemInfoPayload> {
fun (context: Context): List<XyoSystemInfoPayload> {
return listOf(XyoSystemInfoPayload.detect(context))
},
"",
address
)

0 comments on commit 4afea98

Please sign in to comment.