|
| 1 | +import 'package:cw_bitcoin/locktime.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | + |
| 4 | +void main() { |
| 5 | + group('antiFeeSnipingLocktime', () { |
| 6 | + test('returns 0 when not synced', () { |
| 7 | + expect(antiFeeSnipingLocktime(chainTip: 800000, synced: false), 0); |
| 8 | + }); |
| 9 | + |
| 10 | + test('returns 0 when chainTip <= 0', () { |
| 11 | + expect(antiFeeSnipingLocktime(chainTip: 0, synced: true), 0); |
| 12 | + }); |
| 13 | + |
| 14 | + test('returns tip when the 1-in-10 branch does not fire', () { |
| 15 | + expect( |
| 16 | + antiFeeSnipingLocktime(chainTip: 800000, synced: true, randomBelow: (_) => 1), |
| 17 | + 800000, |
| 18 | + ); |
| 19 | + }); |
| 20 | + |
| 21 | + test('backdates by rand(0..99) when the 1-in-10 branch fires', () { |
| 22 | + var calls = 0; |
| 23 | + int rng(int _) => (calls++ == 0) ? 0 : 37; |
| 24 | + expect( |
| 25 | + antiFeeSnipingLocktime(chainTip: 800000, synced: true, randomBelow: rng), |
| 26 | + 799963, |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + test('backdate floors at 0 for low tips', () { |
| 31 | + var calls = 0; |
| 32 | + int rng(int _) => (calls++ == 0) ? 0 : 99; |
| 33 | + expect(antiFeeSnipingLocktime(chainTip: 50, synced: true, randomBelow: rng), 0); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + group('locktimeToBytes', () { |
| 38 | + test('encodes 0 as four zero bytes', () { |
| 39 | + expect(locktimeToBytes(0), [0, 0, 0, 0]); |
| 40 | + }); |
| 41 | + |
| 42 | + test('encodes a block height little-endian', () { |
| 43 | + expect(locktimeToBytes(800000), [0x00, 0x35, 0x0C, 0x00]); |
| 44 | + }); |
| 45 | + |
| 46 | + test('encodes max 32-bit value', () { |
| 47 | + expect(locktimeToBytes(0xFFFFFFFF), [0xFF, 0xFF, 0xFF, 0xFF]); |
| 48 | + }); |
| 49 | + }); |
| 50 | +} |
0 commit comments