Skip to content

Commit

Permalink
Refactor SPV models into SDK models
Browse files Browse the repository at this point in the history
  • Loading branch information
Warchant committed Aug 16, 2022
1 parent e887189 commit ed2900b
Show file tree
Hide file tree
Showing 32 changed files with 135 additions and 653 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,22 +325,10 @@ class EthereumFamilyChain(
}

override suspend fun getVbkBlock(hash: String): VbkBlockResponse? {
val response = rpcRequest<EthGetVbkBlockResponse>(
return rpcRequest<VbkBlockResponse>(
method="pop_getVbkBlockByHash",
params = listOf(hash),
version = "2.0")

return VbkBlockResponse(
chainWork = response.chainWork,
containingEndorsements = response.containingEndorsements,
endorsedBy = response.endorsedBy,
blockOfProofEndorsements = response.blockOfProofEndorsements,
height = response.height,
header = response.header.toVbkBlock(),
status = response.status,
altrefs = response.altrefs,
stored = StoredInVbkBlockData(response.stored.vtbids)
)
}

override suspend fun getVbkBlockHash(height: Int): String? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.veriblock.spv.standalone.commands
import com.google.gson.GsonBuilder
import com.google.gson.JsonPrimitive
import com.google.gson.JsonSerializer
import kotlinx.coroutines.runBlocking
import org.jline.utils.AttributedStyle
import org.veriblock.core.WalletException
import org.veriblock.core.utilities.Utility
Expand All @@ -17,7 +16,6 @@ import org.veriblock.shell.command
import org.veriblock.shell.core.failure
import org.veriblock.shell.core.success
import org.veriblock.spv.SpvContext
import org.veriblock.spv.model.Output
import org.veriblock.spv.model.asLightAddress

fun CommandFactory.spvCommands(
Expand Down
4 changes: 2 additions & 2 deletions nodecore-spv/src/main/java/org/veriblock/spv/SpvContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class SpvContext(
val addressManager: AddressManager
val transactionService: TransactionService
val pendingTransactionContainer: PendingTransactionContainer
val pendingTransactionDownloadedListener: PendingTransactionDownloadedListener
// val pendingTransactionDownloadedListener: PendingTransactionDownloadedListener

private val addressState: ConcurrentHashMap<Address, LedgerContext> = ConcurrentHashMap()

Expand Down Expand Up @@ -96,7 +96,7 @@ class SpvContext(
addressManager = AddressManager()
val walletFile = File(directory, filePrefix + FILE_EXTENSION)
addressManager.load(walletFile)
pendingTransactionDownloadedListener = PendingTransactionDownloadedListener(this)
// pendingTransactionDownloadedListener = PendingTransactionDownloadedListener(this)

val externalPeerEndpoints = config.connectDirectlyTo.map {
try {
Expand Down
34 changes: 0 additions & 34 deletions nodecore-spv/src/main/java/org/veriblock/spv/model/FullBlock.kt

This file was deleted.

38 changes: 0 additions & 38 deletions nodecore-spv/src/main/java/org/veriblock/spv/model/Output.kt

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
package org.veriblock.spv.model

class SigningResult(
private val succeeded: Boolean,
val signature: ByteArray?,
val publicKey: ByteArray?
) {
fun succeeded(): Boolean {
return succeeded
}
}
val signature: ByteArray,
val publicKey: ByteArray
)
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.veriblock.core.crypto.Sha256Hash
import org.veriblock.core.crypto.VbkTxId
import org.veriblock.core.crypto.asVbkTxId
import org.veriblock.core.params.NetworkParameters
import org.veriblock.sdk.models.VeriBlockTransaction
import org.veriblock.sdk.models.asCoin
import org.veriblock.sdk.services.SerializeDeserializeService
import java.io.ByteArrayOutputStream
Expand All @@ -29,150 +30,7 @@ import java.util.ArrayList

private val logger = createLogger {}

open class StandardTransaction : Transaction {
var inputAmount: Coin? = null
private val outputs: MutableList<Output> = ArrayList()
private var signatureIndex: Long = 0
private var transactionFee: Long = 0
override var data: ByteArray? = null

constructor(txId: VbkTxId) : super(txId)

constructor(
inputAddress: String,
inputAmount: Long,
outputs: List<Output>,
signatureIndex: Long,
networkParameters: NetworkParameters
) : this(
null, inputAddress, inputAmount, outputs, signatureIndex, ByteArray(0), networkParameters
)

constructor(
txId: VbkTxId?,
inputAddress: String,
inputAmount: Long,
outputs: List<Output>,
signatureIndex: Long,
data: ByteArray?,
networkParameters: NetworkParameters
) {
var totalOutput = 0L
for (o in outputs) {
totalOutput += o.amount.atomicUnits
}
val fee = inputAmount - totalOutput

// Only for Alt Chain Endorsement Transactions
this.data = data
this.signatureIndex = signatureIndex
addAllOutput(outputs)
this.inputAmount = inputAmount.asCoin()
this.inputAddress = StandardAddress(inputAddress)
transactionFee = fee
if (txId == null) {
this.txId = calculateTxId(networkParameters)
} else {
this.txId = txId
}
}

override fun toByteArray(networkParameters: NetworkParameters): ByteArray {
ByteArrayOutputStream().use { stream ->
serializeToStream(stream, networkParameters)
return stream.toByteArray()
}
}

override fun getSignedMessageBuilder(networkParameters: NetworkParameters): RpcSignedTransaction.Builder {
val transaction = getTransactionMessageBuilder(networkParameters).build()
val builder = RpcSignedTransaction.newBuilder()
builder.transaction = transaction
builder.signatureIndex = signatureIndex
builder.publicKey = ByteString.copyFrom(publicKey)
builder.signature = ByteString.copyFrom(signature)
return builder
}

private fun getTransactionMessageBuilder(networkParameters: NetworkParameters): RpcTransaction.Builder {
val builder = RpcTransaction.newBuilder()
builder.timestamp = Utility.getCurrentTimeSeconds()
builder.transactionFee = transactionFee
builder.txId = txId.toString().asHexByteString()
if (transactionTypeIdentifier == TransactionTypeIdentifier.STANDARD) {
builder.type = RpcTransaction.Type.STANDARD
} else if (transactionTypeIdentifier == TransactionTypeIdentifier.MULTISIG) {
builder.type = RpcTransaction.Type.MULTISIG
}
builder.sourceAmount = inputAmount!!.atomicUnits
builder.sourceAddress = ByteString.copyFrom(inputAddress!!.toByteArray())
builder.data = ByteString.copyFrom(data)
builder.size = toByteArray(networkParameters).size
for (output in getOutputs()) {
val outputBuilder = builder.addOutputsBuilder()
outputBuilder.address = ByteString.copyFrom(output.address.toByteArray())
outputBuilder.amount = output.amount.atomicUnits
}
return builder
}

@Throws(IOException::class)
private fun serializeToStream(stream: OutputStream, networkParameters: NetworkParameters) {
val magicByte = networkParameters.transactionPrefix
if (magicByte != null) {
stream.write(magicByte.toInt())
}

// Write type
stream.write(transactionTypeIdentifier.id.toInt())

// Write source address
inputAddress!!.serializeToStream(stream)

// Write source amount
SerializeDeserializeService.serialize(inputAmount!!, stream)

// Write destinations
stream.write(getOutputs().size)
for (output in getOutputs()) {
output.serializeToStream(stream)
}
SerializerUtility.writeVariableLengthValueToStream(stream, signatureIndex)
SerializerUtility.writeVariableLengthValueToStream(stream, data)
}

override fun getOutputs(): List<Output> {
return outputs
}

fun addOutput(o: Output) {
outputs.add(o)
}

fun addAllOutput(o: Collection<Output>) {
outputs.addAll(o)
}

override fun getSignatureIndex(): Long =
signatureIndex

fun setSignatureIndex(signatureIndex: Long) {
this.signatureIndex = signatureIndex
}

override fun getTransactionFee(): Long =
transactionFee

override val transactionTypeIdentifier: TransactionTypeIdentifier
get() = TransactionTypeIdentifier.STANDARD

private fun calculateTxId(
networkParameters: NetworkParameters
): VbkTxId {
return calculateTxIDBytes(toByteArray(networkParameters)).asVbkTxId()
}

private fun calculateTxIDBytes(rawTx: ByteArray): ByteArray {
return Crypto().SHA256ReturnBytes(rawTx)
}
}
class StandardTransaction(
val tx: VeriBlockTransaction,
val meta: TransactionMeta
)
Loading

0 comments on commit ed2900b

Please sign in to comment.