Skip to content

Commit

Permalink
♻️ Fix tests to adopt bitcoin value as UInt64
Browse files Browse the repository at this point in the history
  • Loading branch information
usatie committed Sep 18, 2018
1 parent 1dc9280 commit eff8ce3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Tests/BitcoinKitTests/OpCodeTests.swift
Expand Up @@ -582,7 +582,7 @@ class OpCodeTests: XCTestCase {
let index: UInt32 = 1
let outpoint = TransactionOutPoint(hash: hash, index: index)

let balance: Int64 = 169012961
let balance: UInt64 = 169012961

let privateKey = try! PrivateKey(wif: "92pMamV6jNyEq9pDpY4f6nBy9KpV2cfJT4L5zDUYiGqyQHJfF1K")

Expand Down Expand Up @@ -668,7 +668,7 @@ class OpCodeTests: XCTestCase {
let index: UInt32 = 1
let outpoint = TransactionOutPoint(hash: hash, index: index)

let balance: Int64 = 2047900000
let balance: UInt64 = 2047900000

let privateKey = try! PrivateKey(wif: "92pMamV6jNyEq9pDpY4f6nBy9KpV2cfJT4L5zDUYiGqyQHJfF1K")

Expand Down
6 changes: 3 additions & 3 deletions Tests/BitcoinKitTests/ScriptMachineTests.swift
Expand Up @@ -36,9 +36,9 @@ class ScriptMachineTests: XCTestCase {
let index: UInt32 = 1
let outpoint = TransactionOutPoint(hash: hash, index: index)

let balance: Int64 = 169012961
let amount: Int64 = 50000000
let fee: Int64 = 10000000
let balance: UInt64 = 169012961
let amount: UInt64 = 50000000
let fee: UInt64 = 10000000
let toAddress = "mv4rnyY3Su5gjcDNzbMLKBQkBicCtHUtFB" // https://testnet.coinfaucet.eu/en/

let privateKey = try! PrivateKey(wif: "92pMamV6jNyEq9pDpY4f6nBy9KpV2cfJT4L5zDUYiGqyQHJfF1K")
Expand Down
29 changes: 15 additions & 14 deletions Tests/BitcoinKitTests/TestHelpers.swift
Expand Up @@ -41,29 +41,29 @@ extension Array {
}

extension Array where Element == UnspentTransaction {
func sum() -> Int64 {
func sum() -> UInt64 {
return reduce(0) { $0 + $1.output.value }
}
}

public struct Fee {
public static let feePerByte: Int64 = 1 // ideally get this value from Bitcoin node
public static let dust: Int64 = 3 * 182 * feePerByte
public static let feePerByte: UInt64 = 1 // ideally get this value from Bitcoin node
public static let dust: UInt64 = 3 * 182 * feePerByte

// size for txin(P2PKH) : 148 bytes
// size for txout(P2PKH) : 34 bytes
// cf. size for txin(P2SH) : not determined to one
// cf. size for txout(P2SH) : 32 bytes
// cf. size for txout(OP_RETURN + String) : Roundup([#characters]/32) + [#characters] + 11 bytes
public static func calculate(nIn: Int, nOut: Int = 2, extraOutputSize: Int = 0) -> Int64 {
public static func calculate(nIn: Int, nOut: Int = 2, extraOutputSize: Int = 0) -> UInt64 {
var txsize: Int {
return ((148 * nIn) + (34 * nOut) + 10) + extraOutputSize
}
return Int64(txsize) * feePerByte
return UInt64(txsize) * feePerByte
}
}

public func selectTx(from utxos: [UnspentTransaction], targetValue: Int64, dustThreshhold: Int64 = Fee.dust) throws -> (utxos: [UnspentTransaction], fee: Int64) {
public func selectTx(from utxos: [UnspentTransaction], targetValue: UInt64, dustThreshhold: UInt64 = Fee.dust) throws -> (utxos: [UnspentTransaction], fee: UInt64) {
// if target value is zero, fee is zero
guard targetValue > 0 else {
return ([], 0)
Expand All @@ -73,13 +73,13 @@ public func selectTx(from utxos: [UnspentTransaction], targetValue: Int64, dustT
let doubleTargetValue = targetValue * 2
var numOutputs = 2 // if allow multiple output, it will be changed.
var numInputs = 2
var fee: Int64 {
var fee: UInt64 {
return Fee.calculate(nIn: numInputs, nOut: numOutputs)
}
var targetWithFee: Int64 {
var targetWithFee: UInt64 {
return targetValue + fee
}
var targetWithFeeAndDust: Int64 {
var targetWithFeeAndDust: UInt64 {
return targetWithFee + dustThreshhold
}

Expand All @@ -91,8 +91,9 @@ public func selectTx(from utxos: [UnspentTransaction], targetValue: Int64, dustT
}

// difference from 2x targetValue
func distFrom2x(_ val: Int64) -> Int64 {
return abs(val - doubleTargetValue)
func distFrom2x(_ val: UInt64) -> UInt64 {
if val > doubleTargetValue { return val - doubleTargetValue }
else { return doubleTargetValue - val }
}

// 1. Find a combination of the fewest outputs that is
Expand Down Expand Up @@ -128,10 +129,10 @@ public func selectTx(from utxos: [UnspentTransaction], targetValue: Int64, dustT
throw UtxoSelectError.insufficient
}

public func createUnsignedTx(toAddress: Address, amount: Int64, changeAddress: Address, utxos: [UnspentTransaction]) -> UnsignedTransaction {
public func createUnsignedTx(toAddress: Address, amount: UInt64, changeAddress: Address, utxos: [UnspentTransaction]) -> UnsignedTransaction {
let (utxos, fee) = try! selectTx(from: utxos, targetValue: amount)
let totalAmount: Int64 = utxos.reduce(0) { $0 + $1.output.value }
let change: Int64 = totalAmount - amount - fee
let totalAmount: UInt64 = utxos.reduce(0) { $0 + $1.output.value }
let change: UInt64 = totalAmount - amount - fee

let toPubKeyHash: Data = toAddress.data
let changePubkeyHash: Data = changeAddress.data
Expand Down
6 changes: 3 additions & 3 deletions Tests/BitcoinKitTests/TransactionTests.swift
Expand Up @@ -36,9 +36,9 @@ class TransactionTests: XCTestCase {
let index: UInt32 = 1
let outpoint = TransactionOutPoint(hash: hash, index: index)

let balance: Int64 = 169012961
let amount: Int64 = 50000000
let fee: Int64 = 10000000
let balance: UInt64 = 169012961
let amount: UInt64 = 50000000
let fee: UInt64 = 10000000
let toAddress = "mv4rnyY3Su5gjcDNzbMLKBQkBicCtHUtFB" // https://testnet.coinfaucet.eu/en/

let privateKey = try! PrivateKey(wif: "92pMamV6jNyEq9pDpY4f6nBy9KpV2cfJT4L5zDUYiGqyQHJfF1K")
Expand Down

0 comments on commit eff8ce3

Please sign in to comment.