Skip to content

Commit

Permalink
Add in-order delivery api for frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
jannickheisch committed Dec 10, 2023
1 parent 8ed4c58 commit ce86842
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
18 changes: 18 additions & 0 deletions android/tinySSB/app/src/main/assets/web/tremola.js
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,24 @@ function b2f_local_peer(type, identifier, displayname, status) {
refresh_connection_entry(identifier)
}

/**
* This function is called, when the backend received a new log entry and successfully completed the corresponding sidechain.
* The backend assures, that the log entries are sent to the frontend in the same sequential order as in the append-only log.
*
* @param {Object} e Object containing all information of the log_entry.
* @param {Object} e.hdr Contains basic information about the log entry.{tst: 1700234500672, ref: 417869, fid: '@AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=.ed25519'}
* @param {number} e.hdr.tst Timestamp at which the message was created. (Number of milliseconds elapsed since midnight at the beginning of January 1970 00:00 UTC)
* @param {string} e.hdr.ref The message ID of this log entry.
* @param {string} e.hdr.fid The public key of the author encoded in base64.
* @param {[]} e.public The payload of the message
*
*/
function b2f_new_in_order_event(e) {
}

function b2f_new_incomplete_event(e) {
}

function b2f_new_event(e) { // incoming SSB log event: we get map with three entries
// console.log('hdr', JSON.stringify(e.header))
console.log('pub', JSON.stringify(e.public))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nz.scuttlebutt.tremolavossbol
import android.Manifest
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.util.Base64
Expand Down Expand Up @@ -30,6 +31,7 @@ import org.json.JSONArray
class WebAppInterface(val act: MainActivity, val webView: WebView) {

var frontend_ready = false
private val frontend_frontier = act.getSharedPreferences("frontend_frontier", Context.MODE_PRIVATE)

@JavascriptInterface
fun onFrontendRequest(s: String) {
Expand Down Expand Up @@ -63,7 +65,7 @@ class WebAppInterface(val act: MainActivity, val webView: WebView) {
val mid = r.get_mid(i)
if (payload == null || mid == null) break
Log.d("restream", "${i}, ${payload.size} Bytes")
sendToFrontend(fid, i, mid, payload)
sendTinyEventToFrontend(fid, i, mid, payload)
i++
}
}
Expand Down Expand Up @@ -352,26 +354,39 @@ class WebAppInterface(val act: MainActivity, val webView: WebView) {

fun sendTinyEventToFrontend(fid: ByteArray, seq: Int, mid:ByteArray, body: ByteArray) {
Log.d("wai","sendTinyEvent ${body.toHex()}")
sendToFrontend(fid, seq, mid, body)
var e = toFrontendObject(fid, seq, mid, body)
if (e != null)
eval("b2f_new_event($e)")

// in-order api
val replica = act.tinyRepo.fid2replica(fid)

if (frontend_frontier.getInt(fid.toHex(), 0) == seq && replica != null) {
for (i in seq .. replica.state.max_seq ) {
val content = replica.read(i)
val message_id= replica.get_mid(seq)
if(content == null || message_id == null)
break
e = toFrontendObject(fid, i, message_id, content)
if (e != null)
eval("b2f_new_in_order_event($e)")
frontend_frontier.edit().putInt(fid.toHex(), i + 1).apply()
}
}
}

fun sendToFrontend(fid: ByteArray, seq: Int, mid: ByteArray, payload: ByteArray) {
Log.d("wai", "sendToFrontend seq=${seq} ${payload.toHex()}")
fun toFrontendObject(fid: ByteArray, seq: Int, mid: ByteArray, payload: ByteArray): String? {
val bodyList = Bipf.decode(payload)
if (bodyList == null || bodyList.typ != BIPF_LIST) {
Log.d("sendToFrontend", "decoded payload == null")
return
Log.d("toFrontendObject", "decoded payload == null")
return null
}
val param = Bipf.bipf_list2JSON(bodyList)
var hdr = JSONObject()
hdr.put("fid", "@" + fid.toBase64() + ".ed25519")
hdr.put("ref", mid.toBase64())
hdr.put("seq", seq)
var cmd = "b2f_new_event({header:${hdr.toString()},"
cmd += "public:${param.toString()}"
cmd += "});"
Log.d("CMD", cmd)
eval(cmd)
return "{header:${hdr.toString()}, public:${param.toString()}}"
}


Expand Down

0 comments on commit ce86842

Please sign in to comment.