Skip to content

Commit

Permalink
Replaced asserts to allow error handling instead of crashing the appl…
Browse files Browse the repository at this point in the history
…ication
  • Loading branch information
jannickheisch committed Oct 12, 2023
1 parent 7c72cd0 commit 2d26865
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class Node(val context: MainActivity) {
fun publish_public_content(content: ByteArray) {
val repo = context.tinyRepo
Log.d("node", "publish_public_content ${content.size}B")
val pkt = repo.mk_logEntry(content)
val seq = repo.mk_logEntry(content)
//Log.d("node", "publish_public_content --> pkt ${if (pkt == null) 0 else pkt.size}B")
//Log.d("node", "publish_public_content --> content ${if (pkt == null) 0 else pkt.toHex()}B")
//if (pkt == null) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ class Replica(val context: MainActivity, val datapath: File, val fid: ByteArray)
var pos: Int
try {
pend = state.pend_sc[seq]!!
assert(pend.hptr.contentEquals(pkt.sha256().sliceArray(0 until HASH_LEN)))
if(pend.hptr.contentEquals(pkt.sha256().sliceArray(0 until HASH_LEN)))
return false
RandomAccessFile(log, "rwd").use { f ->
f.seek(pend.pos.toLong())
f.write(pkt)
Expand Down Expand Up @@ -307,7 +308,8 @@ class Replica(val context: MainActivity, val datapath: File, val fid: ByteArray)

fun get_chunk_pkt(seq: Int, cnr: Int): ByteArray? {
try {
assert(seq >= 1 && seq <= state.max_seq)
if(seq < 1 || seq > state.max_seq)
return null
if (state.pend_sc.containsKey(seq)) {
if (cnr >= state.pend_sc[seq]!!.cnr)
return null
Expand Down Expand Up @@ -377,7 +379,8 @@ class Replica(val context: MainActivity, val datapath: File, val fid: ByteArray)
// TODO implement PKTTYPE_plain48
fun write48(c: ByteArray): Int {
var content = c
assert(log.length().toInt() == state.max_pos)
if(log.length().toInt() != state.max_pos)
return -1
if (content.size < 48) {
content += ByteArray(48-content.size) //TODO: Is this correct? (compare to simplepub code l. 237)
} else {
Expand All @@ -388,20 +391,23 @@ class Replica(val context: MainActivity, val datapath: File, val fid: ByteArray)
val dmx = context.tinyDemux.compute_dmx(nam)
val msg = dmx + ByteArray(1) { PKTTYPE_plain48.toByte()} + content
val wire = msg + signDetached(nam + msg, context.idStore.identity.signingKey!!)
assert(wire.size == TINYSSB_PKT_LEN)
assert(verifySignDetached(
if(wire.size != TINYSSB_PKT_LEN)
return -1
if(!verifySignDetached(
wire.sliceArray(56..wire.lastIndex),
nam + wire.sliceArray(0 until 56),
fid
))
return -1
log.appendBytes(wire + state.max_pos.toByteArray())
persist_frontier(seq, wire.size + 4, (nam +wire).sha256().sliceArray(0 until HASH_LEN))
return seq
}

fun write(c: ByteArray): Int {
var content = c
assert(log.length().toInt() == state.max_pos) // TODO assert can throw an Error which is not handled
if(log.length().toInt() != state.max_pos)
return -1//
val chunks = ArrayList<ByteArray>()
val seq = state.max_seq + 1
val sz = Bipf.varint_encode(content.size)
Expand All @@ -428,12 +434,14 @@ class Replica(val context: MainActivity, val datapath: File, val fid: ByteArray)
Log.d("replica write", "dmx is ${dmx.toHex()}, chnk_cnt: ${chunks.size}")
val msg = dmx + ByteArray(1) { PKTTYPE_chain20.toByte()} + payload
var wire = msg + signDetached(nam + msg, context.idStore.identity.signingKey!!)
assert(wire.size == TINYSSB_PKT_LEN)
assert(verifySignDetached(
if(wire.size != TINYSSB_PKT_LEN)
return -1
if(!verifySignDetached(
wire.sliceArray(56..wire.lastIndex),
nam + wire.sliceArray(0 until 56),
fid
))
return -1
chunks.add(0, wire)
var log_entry = chunks.reduce {acc, it -> acc + it}
log_entry += state.max_pos.toByteArray()
Expand Down

0 comments on commit 2d26865

Please sign in to comment.