All files / src/storage/methods generateChange.ts

86.77% Statements 210/242
73.21% Branches 82/112
96.07% Functions 49/51
89.65% Lines 182/203

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 51857x                                       57x             32x   32x                 32x 32x   32x   32x 32x   32x 180x 180x     180x 180x     180x 180x           32x 180x 180x     32x 32x         32x 472x           32x 814x           32x 740x     32x   32x 474x   814x   474x 474x           32x 444x 444x                           32x 442x 442x 375x 442x       32x 32x   32x 32x     32x 96x     32x 53x 38x     35x 35x 9x 9x 9x     35x         32x 43x           32x   32x   53x 53x   53x 53x 14x   53x 53x   53x   53x   5x     48x   48x 24x 22x           48x     32x   35x   35x   53x 53x       35x 30x   5x 5x 14x   5x   2x               32x         32x 2x           30x             30x 116x 18x 18x 98x 8x 8x 8x     90x 90x 90x 90x       30x 30x     30x 30x       30x         30x     2x 2x         2x       57x 30x 30x 36x 51x 30x       30x 30x       30x 30x         30x                                                                                                                                                       57x   32x 32x 6x 6x     32x 32x 51x 51x     32x 32x     32x   32x 32x   32x 32x               57x           526x 2038x   25x 526x   25x   25x 41x 41x 41x     46x   46x   46x 375x 14x   383x 45x 27x 27x 55x 22x 22x     27x 5x 5x     25x 9x 18x 9x 9x 9x     25x             57x 3053x 3053x             57x 520x                     57x 1585x                         57x 474x   520x   1585x      
import { sdk, validateStorageFeeModel } from '../../index.client'
 
export interface GenerateChangeSdkResult {
    allocatedChangeInputs: GenerateChangeSdkChangeInput[]
    changeOutputs: GenerateChangeSdkChangeOutput[]
    size: number
    fee: number
    satsPerKb: number
}
 
/**
 * Simplifications:
 *  - only support one change type with fixed length scripts.
 *  - only support satsPerKb fee model.
 * 
 * Confirms for each availbleChange output that it remains available as they are allocated and selects alternate if not.
 * 
 * @param params 
 * @returns 
 */
export async function generateChangeSdk(
    params: GenerateChangeSdkParams,
    allocateChangeInput: (targetSatoshis: number, exactSatoshis?: number) => Promise<GenerateChangeSdkChangeInput | undefined>,
    releaseChangeInput: (outputId: number) => Promise<void>
)
: Promise<GenerateChangeSdkResult>
{
    Iif (params.noLogging === false) logGenerateChangeSdkParams(params);
 
    const r: GenerateChangeSdkResult = {
        allocatedChangeInputs: [],
        changeOutputs: [],
        size: 0,
        fee: 0,
        satsPerKb: 0
    }
 
    // eslint-disable-next-line no-useless-catch
    try {
        validateGenerateChangeSdkParams(params)
 
        const satsPerKb = params.feeModel.value || 0
 
        const randomVals = [...(params.randomVals || [])]
        const randomValsUsed: number[] = []
 
        const nextRandomVal = () : number => {
            let val = 0
            Iif (!randomVals || randomVals.length === 0) {
                val = Math.random()
            } else {
                val = randomVals.shift() || 0
                randomVals.push(val)
            }
            // Capture random sequence used if not supplied
            randomValsUsed.push(val)
            return val
        }
 
        /**
         * @returns a random integer betweenn min and max, inclussive.
         */
        const rand = (min: number, max: number): number => {
            Iif (max < min) throw new sdk.WERR_INVALID_PARAMETER('max', `less than min (${min}). max is (${max})`)
            return Math.floor(nextRandomVal() * (max - min + 1) + min)
        }
 
        const fixedInputs = params.fixedInputs
        const fixedOutputs = params.fixedOutputs
 
        /**
         * @returns sum of transaction fixedInputs satoshis and fundingInputs satoshis
         */
        const funding = () : number => {
            return fixedInputs.reduce((a, e) => a + e.satoshis, 0) + r.allocatedChangeInputs.reduce((a, e) => a + e.satoshis, 0)
        }
 
        /**
         * @returns sum of transaction fixedOutputs satoshis 
         */
        const spending = () : number => {
            return fixedOutputs.reduce((a, e) => a + e.satoshis, 0)
        }
 
        /**
         * @returns sum of transaction changeOutputs satoshis 
         */
        const change = () : number => {
            return r.changeOutputs.reduce((a, e) => a + e.satoshis, 0)
        }
 
        const fee = () : number => funding() - spending() - change()
 
        const size = (addedChangeInputs?: number, addedChangeOutputs?: number) : number => {
            const inputScriptLengths = [...fixedInputs.map(x => x.unlockingScriptLength),
                ...Array(r.allocatedChangeInputs.length + (addedChangeInputs || 0)).fill(params.changeUnlockingScriptLength)]
            const outputScriptLengths = [...fixedOutputs.map(x => x.lockingScriptLength),
                ...Array(r.changeOutputs.length + (addedChangeOutputs || 0)).fill(params.changeLockingScriptLength)]
            const size = transactionSize(inputScriptLengths, outputScriptLengths)
            return size
        }
 
        /**
         * @returns the target fee required for the transaction as currently configured under feeModel.
         */
        const feeTarget = (addedChangeInputs?: number, addedChangeOutputs?: number): number => {
            const fee = Math.ceil(size(addedChangeInputs, addedChangeOutputs) / 1000 * satsPerKb)
            return fee
        }
 
        /**
         * @returns the current excess fee for the transaction as currently configured.
         *
         * This is funding() - spending() - change() - feeTarget()
         *
         * The goal is an excess fee of zero.
         *
         * A positive value is okay if the cost of an additional change output is greater.
         *
         * A negative value means the transaction is under funded, or over spends, and may be rejected.
         */
        const feeExcess = (addedChangeInputs?: number, addedChangeOutputs?: number): number => {
            const fe = funding() - spending() - change() - feeTarget(addedChangeInputs, addedChangeOutputs)
            if (!addedChangeInputs && !addedChangeOutputs)
                feeExcessNow = fe
            return fe
        }
 
        // The most recent feeExcess()
        let feeExcessNow = 0
        feeExcess()
        
        const hasTargetNetCount = params.targetNetCount !== undefined
        const targetNetCount = params.targetNetCount || 0
 
        // current net change in count of change outputs
        const netChangeCount = (): number => {
            return r.changeOutputs.length - r.allocatedChangeInputs.length
        }
 
        const addOutputToBalanceNewInput = (): boolean => {
            if (!hasTargetNetCount) return false
            return netChangeCount() - 1 < targetNetCount
        }
 
        const releaseAllocatedChangeInputs = async () : Promise<void> => {
            while (r.allocatedChangeInputs.length > 0) {
                const i = r.allocatedChangeInputs.pop()
                if (i) {
                    await releaseChangeInput(i.outputId)
                }
            }
            feeExcessNow = feeExcess()
        }
 
        // If we'd like to have more change outputs create them now.
        // They may be removed if it turns out we can't fund them.
        while (hasTargetNetCount && targetNetCount > netChangeCount() || r.changeOutputs.length === 0 && feeExcess() > 0) {
            r.changeOutputs.push({
                satoshis: r.changeOutputs.length === 0 ? params.changeFirstSatoshis : params.changeInitialSatoshis,
                lockingScriptLength: params.changeLockingScriptLength
            })
        }
        
        const fundTransaction = async (): Promise<void> => {
 
            let removingOutputs = false
 
            const attemptToFundTransaction = async () : Promise<boolean> => {
                Iif (feeExcess() > 0) return true
                
                let exactSatoshis: number | undefined = undefined
                if (!hasTargetNetCount && r.changeOutputs.length === 0) {
                    exactSatoshis = -feeExcess(1);
                }
                const ao = addOutputToBalanceNewInput() ? 1 : 0
                const targetSatoshis = -feeExcess(1, ao) + (ao === 1 ? 2 * params.changeInitialSatoshis : 0)
 
                const allocatedChangeInput = await allocateChangeInput(targetSatoshis, exactSatoshis)
 
                if (!allocatedChangeInput) {
                    // Unable to add another funding change input
                    return false
                }
 
                r.allocatedChangeInputs.push(allocatedChangeInput)
 
                if (!removingOutputs && feeExcess() > 0) {
                    if (ao == 1 || r.changeOutputs.length === 0) {
                        r.changeOutputs.push({
                            satoshis: Math.min(feeExcess(), r.changeOutputs.length === 0 ? params.changeFirstSatoshis : params.changeInitialSatoshis),
                            lockingScriptLength: params.changeLockingScriptLength
                        })
                    }
                }
                return true
            }
 
            for (;;) {
                // This is the starvation loop, drops change outputs one at a time if unable to fund them...
                await releaseAllocatedChangeInputs()
 
                while (feeExcess() < 0) {
                    // This is the funding loop, add one change input at a time...
                    const ok = await attemptToFundTransaction()
                    if (!ok) break
                }
 
                // Done if blanced overbalanced or impossible (all funding applied, all change outputs removed).
                if (feeExcess() >= 0 || r.changeOutputs.length === 0)
                    break
 
                removingOutputs = true
                while (r.changeOutputs.length > 0 && feeExcess() < 0) {
                    r.changeOutputs.pop()
                }
                if (feeExcess() < 0)
                    // Not enough available funding even if no change outputs
                    break
                // and try again...
            }
        }
 
        /**
         * Add funding to achieve a non-negative feeExcess value, if necessary.
         */
        await fundTransaction()
 
        /**
         * Trigger an account funding event if we don't have enough to cover this transaction.
         */
        if (feeExcess() < 0) {
            throw new sdk.WERR_INSUFFICIENT_FUNDS(spending() + feeTarget(), -feeExcessNow)
        }
 
        /**
         * If needed, seek funding to avoid overspending on fees without a change output to recapture it.
         */
        Iif (r.changeOutputs.length === 0 && feeExcessNow > 0) {
            throw new sdk.WERR_INSUFFICIENT_FUNDS(spending() + feeTarget(), params.changeFirstSatoshis)
        }
 
        /**
         * Distribute the excess fees across the changeOutputs added.
         */
        while (r.changeOutputs.length > 0 && feeExcessNow > 0) {
            if (r.changeOutputs.length === 1) {
                r.changeOutputs[0].satoshis += feeExcessNow
                feeExcessNow = 0
            } else if (r.changeOutputs[0].satoshis < params.changeInitialSatoshis) {
                const sats = Math.min(feeExcessNow, params.changeInitialSatoshis - r.changeOutputs[0].satoshis)
                feeExcessNow -= sats
                r.changeOutputs[0].satoshis += sats
            } else {
                // Distribute a random percentage between 25% and 50% but at least one satoshi
                const sats = Math.max(1, Math.floor(rand(2500, 5000) / 10000 * feeExcessNow))
                feeExcessNow -= sats
                const index = rand(0, r.changeOutputs.length - 1)
                r.changeOutputs[index].satoshis += sats
            }
        }
 
        r.size = size()
        r.fee = fee(),
        r.satsPerKb = satsPerKb
 
        const { ok, log } = validateGenerateChangeSdkResult(params, r)
        Iif (!ok) {
            throw new sdk.WERR_INTERNAL(`generateChangeSdk error: ${log}`)
        }
 
        Iif (r.allocatedChangeInputs.length > 4 && r.changeOutputs.length > 4) {
            console.log('generateChangeSdk_Capture_too_many_ins_and_outs')
            logGenerateChangeSdkParams(params)
        }
 
        return r
 
    } catch (eu: unknown) {
        const e = sdk.WalletError.fromUnknown(eu)
        Iif (e.code === 'ERR_DOJO_NOT_SUFFICIENT_FUNDS') throw eu
 
        // Capture the params in cloud run log which has a 100k text length limit per line.
        // logGenerateChangeSdkParams(params, eu)
 
        throw eu
    }
}
 
export function validateGenerateChangeSdkResult(params: GenerateChangeSdkParams, r: GenerateChangeSdkResult) : { ok: boolean, log: string } {
    let ok = true
    let log = ''
    const sumIn = params.fixedInputs.reduce((a, e) => a + e.satoshis, 0) + r.allocatedChangeInputs.reduce((a, e) => a + e.satoshis, 0);
    const sumOut = params.fixedOutputs.reduce((a, e) => a + e.satoshis, 0) + r.changeOutputs.reduce((a, e) => a + e.satoshis, 0);
    Iif (r.fee && Number.isInteger(r.fee) && r.fee < 0) {
        log += `basic fee error ${r.fee};`
        ok = false
    }
    const feePaid = sumIn - sumOut;
    Iif (feePaid !== r.fee) {
        log += `exact fee error ${feePaid} !== ${r.fee};`
        ok = false
    }
    const feeRequired = Math.ceil((r.size || 0) / 1000 * (r.satsPerKb || 0));
    Iif (feeRequired !== r.fee) {
        log += `required fee error ${feeRequired} !== ${r.fee};`
        ok = false
    }
 
    return { ok, log }
}
 
function logGenerateChangeSdkParams(params: GenerateChangeSdkParams, eu?: unknown) {
    let s = JSON.stringify(params)
    console.log(`generateChangeSdk params length ${s.length}${eu ? ` error: ${eu}` : ""}`)
    let i = -1
    const maxlen = 99900
    for (; ;) {
        i++
        console.log(`generateChangeSdk params ${i} XXX${s.slice(0, maxlen)}XXX`)
        s = s.slice(maxlen)
        Iif (!s || i > 100) break
    }
}
 
export interface GenerateChangeSdkParams {
    fixedInputs: GenerateChangeSdkInput[]
    fixedOutputs: GenerateChangeSdkOutput[]
 
    feeModel: sdk.StorageFeeModel
 
    /**
     * Target for number of new change outputs added minus number of funding change outputs consumed.
     * If undefined, only a single change output will be added if excess fees must be recaptured.
     */
    targetNetCount?: number
    /**
     * Satoshi amount to initialize optional new change outputs.
     */
    changeInitialSatoshis: number
    /**
     * Lowest amount value to assign to a change output.
     * Drop the output if unable to satisfy. 
     * default 285
     */
    changeFirstSatoshis: number
 
    /**
     * Fixed change locking script length.
     * 
     * For P2PKH template, 25 bytes
     */
    changeLockingScriptLength: number
    /**
     * Fixed change unlocking script length.
     * 
     * For P2PKH template, 107 bytes
     */
    changeUnlockingScriptLength: number
 
    randomVals?: number[]
    noLogging?: boolean
    log?: string
}
 
export interface GenerateChangeSdkInput {
    satoshis: number
    unlockingScriptLength: number
}
 
export interface GenerateChangeSdkOutput {
    satoshis: number
    lockingScriptLength: number
}
 
export interface GenerateChangeSdkChangeInput {
    outputId: number
    satoshis: number
}
 
export interface GenerateChangeSdkChangeOutput {
    satoshis: number
    lockingScriptLength: number
}
 
export function validateGenerateChangeSdkParams(params: GenerateChangeSdkParams) {
 
    Iif (!Array.isArray(params.fixedInputs)) throw new sdk.WERR_INVALID_PARAMETER('fixedInputs', 'an array of objects')
    params.fixedInputs.forEach((x, i) => {
        sdk.validateSatoshis(x.satoshis, `fixedInputs[${i}].satoshis`)
        sdk.validateInteger(x.unlockingScriptLength, `fixedInputs[${i}].unlockingScriptLength`, undefined, 0)
    })
 
    Iif (!Array.isArray(params.fixedOutputs)) throw new sdk.WERR_INVALID_PARAMETER('fixedOutputs', 'an array of objects')
    params.fixedOutputs.forEach((x, i) => {
        sdk.validateSatoshis(x.satoshis, `fixedOutputs[${i}].satoshis`)
        sdk.validateInteger(x.lockingScriptLength, `fixedOutputs[${i}].lockingScriptLength`, undefined, 0)
    })
 
    params.feeModel = validateStorageFeeModel(params.feeModel)
    Iif (params.feeModel.model !== 'sat/kb')
        throw new sdk.WERR_INVALID_PARAMETER('feeModel.model', `'sat/kb'`)
 
    sdk.validateOptionalInteger(params.targetNetCount, `targetNetCount`)
 
    sdk.validateSatoshis(params.changeFirstSatoshis, 'changeFirstSatoshis', 1)
    sdk.validateSatoshis(params.changeInitialSatoshis, 'changeInitialSatoshis', 1)
 
    sdk.validateInteger(params.changeLockingScriptLength, `changeLockingScriptLength`)
    sdk.validateInteger(params.changeUnlockingScriptLength, `changeUnlockingScriptLength`)
 
}
 
export interface GenerateChangeSdkStorageChange extends GenerateChangeSdkChangeInput {
    spendable: boolean
}
 
export function generateChangeSdkMakeStorage(availableChange: GenerateChangeSdkChangeInput[]) 
: { allocateChangeInput: (targetSatoshis: number, exactSatoshis?: number) => Promise<GenerateChangeSdkChangeInput | undefined>,
    releaseChangeInput: (outputId: number) => Promise<void>,
    getLog: () => string
 }
{
    const change: GenerateChangeSdkStorageChange[] = availableChange.map(c => ({ ...c, spendable: true }))
    change.sort((a, b) => a.satoshis < b.satoshis ? -1 : a.satoshis > b.satoshis ? 1 : a.outputId < b.outputId ? -1 : a.outputId > b.outputId ? 1 : 0)
 
    let log = ''
    for (const c of change) log += `change ${c.satoshis} ${c.outputId}\n`
 
    const getLog = () : string => log
 
    const allocate = (c: GenerateChangeSdkStorageChange) => {
        log += ` -> ${c.satoshis} sats, id ${c.outputId}\n`
        c.spendable = false;
        return c
    }
 
    const allocateChangeInput = async (targetSatoshis: number, exactSatoshis?: number) : Promise<GenerateChangeSdkChangeInput | undefined> => {
 
        log += `allocate target ${targetSatoshis} exact ${exactSatoshis}`
 
        if (exactSatoshis !== undefined) {
            const exact = change.find(c => c.spendable && c.satoshis === exactSatoshis)
            if (exact) return allocate(exact)
        }
        const over = change.find(c => c.spendable && c.satoshis >= targetSatoshis)
        if (over) return allocate(over)
        let under: GenerateChangeSdkStorageChange | undefined = undefined
        for (let i = change.length - 1; i >= 0; i--) {
            if (change[i].spendable) {
                under = change[i]
                break
            }
        }
        if (under) return allocate(under)
        log += `\n`
        return undefined
    }
 
    const releaseChangeInput = async (outputId: number) : Promise<void> => {
        log += `release id ${outputId}\n`
        const c = change.find(x => x.outputId === outputId)
        Iif (!c) throw new sdk.WERR_INTERNAL(`unknown outputId ${outputId}`);
        Iif (c.spendable) throw new sdk.WERR_INTERNAL(`release of spendable outputId ${outputId}`);
        c.spendable = true
    }
 
    return { allocateChangeInput, releaseChangeInput, getLog }
}
 
/**
 * Returns the byte size required to encode number as Bitcoin VarUint
 * @publicbody
 */
export function varUintSize(val: number): 1 | 3 | 5 | 9 {
  Iif (val < 0) throw new sdk.WERR_INVALID_PARAMETER('varUint', 'non-negative')
  return (val <= 0xfc ? 1 : val <= 0xffff ? 3 : val <= 0xffffffff ? 5 : 9)
}
 
/**
 * @param scriptSize byte length of input script
 * @returns serialized byte length a transaction input
 */
export function transactionInputSize (scriptSize: number): number {
  return 32 + // txid
        4 + // vout
        varUintSize(scriptSize) + // script length, this is already in bytes
        scriptSize + // script
        4 // sequence number
}
 
/**
 * @param scriptSize byte length of output script
 * @returns serialized byte length a transaction output
 */
export function transactionOutputSize (scriptSize: number): number {
  return varUintSize(scriptSize) + // output script length, from script encoded as hex string
        scriptSize + // output script
        8 // output amount (satoshis)
}
 
/**
 * Compute the serialized binary transaction size in bytes
 * given the number of inputs and outputs,
 * and the size of each script.
 * @param inputs array of input script lengths, in bytes
 * @param outputs array of output script lengths, in bytes
 * @returns total transaction size in bytes
 */
export function transactionSize (inputs: number[], outputs: number[]): number {
  return 4 + // Version
        varUintSize(inputs.length) + // Number of inputs
        inputs.reduce((a, e) => a + transactionInputSize(e), 0) + // all inputs
        varUintSize(outputs.length) + // Number of outputs
        outputs.reduce((a, e) => a + transactionOutputSize(e), 0) + // all outputs
        4 // lock time
}