Skip to content
This repository has been archived by the owner on Oct 28, 2022. It is now read-only.

Commit

Permalink
Sync special dash transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
ant013 committed Jul 22, 2022
1 parent e2c4815 commit 84d0e5f
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 10 deletions.
20 changes: 13 additions & 7 deletions BitcoinCore/Classes/Models/DataObjects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public struct BlockHeader {

}

public struct FullTransaction {
open class FullTransaction {

public let header: Transaction
public let inputs: [Input]
Expand All @@ -35,15 +35,21 @@ public struct FullTransaction {
self.inputs = inputs
self.outputs = outputs

let hash = Kit.sha256sha256(TransactionSerializer.serialize(transaction: self, withoutWitness: true))
self.header.dataHash = hash
if header.dataHash.isEmpty { //todo: check logic. It avoid double check if some special transactions make tx with already hash
let hash = Kit.sha256sha256(TransactionSerializer.serialize(transaction: self, withoutWitness: true))
set(hash: hash)
}
}

public func set(hash: Data) {
header.dataHash = hash
metaData.transactionHash = hash

for input in self.inputs {
input.transactionHash = self.header.dataHash
for input in inputs {
input.transactionHash = header.dataHash
}
for output in self.outputs {
output.transactionHash = self.header.dataHash
for output in outputs {
output.transactionHash = header.dataHash
}
}

Expand Down
4 changes: 4 additions & 0 deletions BitcoinCore/Classes/Network/Helpers/ByteStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public class ByteStream {
}

public func read(_ type: VarInt.Type) -> VarInt {
guard data.count > offset else {
return VarInt(0)
}

let len = data[offset..<(offset + 1)].to(type: UInt8.self)
let length: UInt64
switch len {
Expand Down
11 changes: 8 additions & 3 deletions BitcoinCore/Classes/Network/Messages/TransactionMessage.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import UIExtensions

struct TransactionMessage: IMessage {
public struct TransactionMessage: IMessage {
let transaction: FullTransaction
let size: Int

var description: String {
return "\(transaction.header.dataHash.reversedHex)"
public init(transaction: FullTransaction, size: Int) {
self.transaction = transaction
self.size = size
}

public var description: String {
"\(transaction.header.dataHash.reversedHex)"
}

}
1 change: 1 addition & 0 deletions DashKit/Classes/Core/Kit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public class Kit: AbstractKit {
.add(messageParser: TransactionLockVoteMessageParser())
.add(messageParser: MasternodeListDiffMessageParser(masternodeParser: masternodeParser, quorumParser: quorumParser))
.add(messageParser: ISLockParser(hasher: doubleShaHasher))
.add(messageParser: TransactionMessageParser(hasher: doubleShaHasher))

bitcoinCore.add(messageSerializer: GetMasternodeListDiffMessageSerializer())

Expand Down
11 changes: 11 additions & 0 deletions DashKit/Classes/Models/SpecialTransaction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation
import BitcoinCore

class SpecialTransaction: FullTransaction {
let extraPayload: Data

init(transaction: FullTransaction, extraPayload: Data) {
self.extraPayload = extraPayload
super.init(header: transaction.header, inputs: transaction.inputs, outputs: transaction.outputs)
}
}
40 changes: 40 additions & 0 deletions DashKit/Classes/Network/Messages/TransactionMessageParser.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Foundation
import BitcoinCore
import OpenSslKit

class TransactionMessageParser: IMessageParser {
let id: String = "tx"

let hasher: IDashHasher

init(hasher: IDashHasher) {
self.hasher = hasher
}

private func parseSpecialTxData(input: ByteStream, transaction: FullTransaction) -> SpecialTransaction {
let payloadSize = input.read(VarInt.self)
let payload = input.read(Data.self, count: Int(payloadSize.underlyingValue))

var output = TransactionSerializer.serialize(transaction: transaction)
output += payloadSize.data
output += payload

let hash = hasher.hash(data: output)
transaction.set(hash: hash)

return SpecialTransaction(transaction: transaction, extraPayload: payload)
}

func parse(data: Data) -> IMessage {
let byteStream = ByteStream(data)
var transaction = TransactionSerializer.deserialize(byteStream: byteStream)

let version = Data(from: transaction.header.version)
let isSpecialTransaction = (Int(version[0]) + Int(version[1])) > 0

This comment has been minimized.

Copy link
@fpcornelis

fpcornelis Jul 29, 2022

I believe this is always true

if isSpecialTransaction {
transaction = parseSpecialTxData(input: byteStream, transaction: transaction)
}

return TransactionMessage(transaction: transaction, size: data.count)
}
}

0 comments on commit 84d0e5f

Please sign in to comment.