0
votes

I know that every file which is uploaded as an attachment on Corda gets an unique hash generated and these files are to be converted to either zip/jar format because these are the types Corda supports for saving them as attachments. I have come across a scenario where I have uploaded a same file twice on the same node with a time gap and this resulted it two attachment hashes which is understandable because Im compressing this file as a zip first and then uploading. But when I try to upload the same file twice at the same instant then there is this DuplicateAttachmentException. I tried debugging and what I have found out is that, while we are zipping the files they are having the same time because of which it looks to Corda that it these zips are the same. If I delay the zipping process for multiple files by 1 second or so, this exception doesn't occur. Is there any solution for this?

private fun uploader(inputStream: InputStream, fileName: String, uploader: String): AttachmentId {

        val zipName = "$fileName.zip"

        FileOutputStream(zipName).use { fileOutputStream ->
            ZipOutputStream(fileOutputStream).use { zipOutputStream ->
                val zipEntry = ZipEntry(fileName)
                zipOutputStream.putNextEntry(zipEntry)
                inputStream.copyTo(zipOutputStream, 1024)
            }
        }
        return FileInputStream(zipName).use { fileInputStream ->
            val hash = proxy.uploadAttachmentWithMetadata(
                    jar = fileInputStream,
                    uploader = uploader,
                    filename = fileName
            )
            Files.deleteIfExists(Paths.get(zipName))
            hash
        }

    }

This is the code using which I was able to produce the issue.

1

1 Answers

0
votes

I don't think this is an exception you can avoid. If the hashes are the same then Corda is likely just going to throw it.

I'd be very curious what the use case is where you'd need to upload the same attachment more than once, I suspect you'll have an easier time avoiding that issue in your design than you would creating workarounds for corda.

I suppose you could also slightly modify the files on the way in to ensure they're slightly different on upload.

You can find the actual code itself within corda that checks for duplicate attachments here: https://github.com/corda/corda/blob/82a114a329fee1a249f9c0786d5c2f8593759863/node/src/main/kotlin/net/corda/node/services/persistence/NodeAttachmentService.kt#L435