|
| 1 | +/* eslint-env mocha */ |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +const assert = require('bsert'); |
| 6 | +const ContractState = require('../lib/mempool/contractstate'); |
| 7 | +const Network = require('../lib/protocol/network'); |
| 8 | +const MTX = require('../lib/primitives/mtx'); |
| 9 | +const Output = require('../lib/primitives/output'); |
| 10 | +const CoinView = require('../lib/coins/coinview'); |
| 11 | +const rules = require('../lib/covenants/rules'); |
| 12 | +const NameState = require('../lib/covenants/namestate'); |
| 13 | +const {types} = rules; |
| 14 | + |
| 15 | +const network = Network.get('regtest'); |
| 16 | + |
| 17 | +function nameContext(name, type) { |
| 18 | + const rawName = Buffer.from(name, 'ascii'); |
| 19 | + const nameHash = rules.hashName(rawName); |
| 20 | + |
| 21 | + const output = new Output(); |
| 22 | + const mtx = new MTX(); |
| 23 | + const ns = new NameState(); |
| 24 | + const view = new CoinView(); |
| 25 | + |
| 26 | + switch (type) { |
| 27 | + case types.OPEN: |
| 28 | + output.covenant.type = types.OPEN; |
| 29 | + output.covenant.pushHash(nameHash); // nameHash |
| 30 | + output.covenant.pushU32(0); // start |
| 31 | + output.covenant.push(rawName); // rawName |
| 32 | + break; |
| 33 | + case types.BID: |
| 34 | + output.covenant.type = types.BID; |
| 35 | + output.covenant.pushHash(nameHash); // nameHash |
| 36 | + output.covenant.pushU32(0); // start |
| 37 | + output.covenant.push(rawName); // rawName |
| 38 | + output.covenant.pushHash(Buffer.alloc(32)); // blind |
| 39 | + break; |
| 40 | + case types.REVEAL: |
| 41 | + output.covenant.type = types.REVEAL; |
| 42 | + output.covenant.pushHash(nameHash); // nameHash |
| 43 | + output.covenant.pushU32(100); // height |
| 44 | + output.covenant.pushHash(Buffer.alloc(32)); // nonce |
| 45 | + break; |
| 46 | + case types.UPDATE: { |
| 47 | + const data = Buffer.from('hello world', 'ascii'); |
| 48 | + output.covenant.type = types.UPDATE; |
| 49 | + output.covenant.pushHash(nameHash); // nameHash |
| 50 | + output.covenant.pushU32(100); // height |
| 51 | + output.covenant.push(data); // record |
| 52 | + break; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + mtx.outputs.push(output); |
| 57 | + ns.name = name; |
| 58 | + ns.nameHash = nameHash; |
| 59 | + ns.height = 1; // prevent null ns |
| 60 | + |
| 61 | + view.names.set(nameHash, ns); |
| 62 | + |
| 63 | + return [mtx, view]; |
| 64 | +} |
| 65 | + |
| 66 | +describe('Contract State', function() { |
| 67 | + const name = 'foo'; |
| 68 | + const rawName = Buffer.from(name, 'ascii'); |
| 69 | + const nameHash = rules.hashName(rawName); |
| 70 | + |
| 71 | + it('Should construct', () => { |
| 72 | + const contract = new ContractState(network); |
| 73 | + assert.ok(contract); |
| 74 | + |
| 75 | + // Requires a network |
| 76 | + assert.throws(() => new ContractState()); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should track an open', () => { |
| 80 | + const contract = new ContractState(network); |
| 81 | + |
| 82 | + const [mtx, view] = nameContext(name, types.OPEN); |
| 83 | + contract.track(mtx, view); |
| 84 | + |
| 85 | + assert.ok(contract.opens.has(nameHash)); |
| 86 | + |
| 87 | + const opens = contract.opens.get(nameHash); |
| 88 | + assert.ok(opens.has(Buffer.from(mtx.txid(), 'hex'))); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should track a bid', () => { |
| 92 | + const contract = new ContractState(network); |
| 93 | + |
| 94 | + const [mtx, view] = nameContext(name, types.BID); |
| 95 | + contract.track(mtx, view); |
| 96 | + |
| 97 | + assert.ok(contract.bids.has(nameHash)); |
| 98 | + |
| 99 | + const bids = contract.bids.get(nameHash); |
| 100 | + assert.ok(bids.has(Buffer.from(mtx.txid(), 'hex'))); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should track a reveal', () => { |
| 104 | + const contract = new ContractState(network); |
| 105 | + |
| 106 | + const [mtx, view] = nameContext(name, types.REVEAL); |
| 107 | + contract.track(mtx, view); |
| 108 | + |
| 109 | + assert.ok(contract.reveals.has(nameHash)); |
| 110 | + |
| 111 | + const reveals = contract.reveals.get(nameHash); |
| 112 | + assert.ok(reveals.has(Buffer.from(mtx.txid(), 'hex'))); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should track an update', () => { |
| 116 | + const contract = new ContractState(network); |
| 117 | + |
| 118 | + const [mtx, view] = nameContext(name, types.UPDATE); |
| 119 | + contract.track(mtx, view); |
| 120 | + |
| 121 | + assert.ok(contract.updates.has(nameHash)); |
| 122 | + |
| 123 | + const updates = contract.updates.get(nameHash); |
| 124 | + assert.ok(updates.has(Buffer.from(mtx.txid(), 'hex'))); |
| 125 | + }); |
| 126 | +}); |
0 commit comments