-
Notifications
You must be signed in to change notification settings - Fork 84
/
turingMachine.scrypttest.js
175 lines (127 loc) · 4.92 KB
/
turingMachine.scrypttest.js
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
const { expect } = require('chai');
const { bsv, buildContractClass, getPreimage, toHex, serializeState, deserializeState, SigHashPreimage } = require('scryptlib');
const {
inputIndex,
inputSatoshis,
newTx,
compileContract
} = require('../../helper');
const outputAmount = 222222
describe('Test sCrypt contract TuringMachine In Javascript', () => {
let turingMachine, preimage, result
before(() => {
const TuringMachine = buildContractClass(compileContract('turingMachine.scrypt'))
turingMachine = new TuringMachine()
});
function run(curState, newState) {
const tx = newTx();
const oldSerial = serializeState(curState)
turingMachine.setDataPart(oldSerial)
const newSerial = serializeState(newState)
const newLockingScript = [turingMachine.codePart.toASM(), newSerial].join(' ');
tx.addOutput(new bsv.Transaction.Output({
script: bsv.Script.fromASM(newLockingScript),
satoshis: outputAmount
}))
preimage = getPreimage(tx, turingMachine.lockingScript.toASM(), inputSatoshis)
// set txContext for verification
turingMachine.txContext = {
tx,
inputIndex,
inputSatoshis
}
result = turingMachine.transit(new SigHashPreimage(toHex(preimage)), outputAmount).verify()
expect(result.success, result.error).to.be.true
}
it('run step 1', () => {
// set initial state
let state = {'headPos': 0, 'tape': '01010202', 'curState': '00'};
let newState = {'headPos': 1, 'tape': '01010202', 'curState': '00'}
run(state, newState);
});
it('run step 2', () => {
let state = {'headPos': 1, 'tape': '01010202', 'curState': '00'}
let newState = {'headPos': 2, 'tape': '01010202', 'curState': '00'};
run(state, newState);
});
it('run step 3', () => {
let state = {'headPos': 2, 'tape': '01010202', 'curState': '00'};
let newState = {'headPos': 1, 'tape': '01010302', 'curState': '01'}
run(state, newState);
});
it('run step 4', () => {
let state = {'headPos': 1, 'tape': '01010302', 'curState': '01'}
let newState = {'headPos': 2, 'tape': '01030302', 'curState': '00'};
run(state, newState);
});
it('run step 5', () => {
let state = {'headPos': 2, 'tape': '01030302', 'curState': '00'};
let newState = {'headPos': 3, 'tape': '01030302', 'curState': '00'};
run(state, newState);
});
it('run step 6', () => {
let state = {'headPos': 3, 'tape': '01030302', 'curState': '00'};
let newState = {'headPos': 2, 'tape': '01030303', 'curState': '01'};
run(state, newState);
});
it('run step 7', () => {
let state = {'headPos': 2, 'tape': '01030303', 'curState': '01'};
let newState = {'headPos': 1, 'tape': '01030303', 'curState': '01'};
run(state, newState);
});
it('run step 8', () => {
let state = {'headPos': 1, 'tape': '01030303', 'curState': '01'};
let newState = {'headPos': 0, 'tape': '01030303', 'curState': '01'};
run(state, newState);
});
it('run step 9', () => {
let state = {'headPos': 0, 'tape': '01030303', 'curState': '01'};
let newState = {'headPos': 1, 'tape': '03030303', 'curState': '00'};
run(state, newState);
});
it('run step 10', () => {
let state = {'headPos': 1, 'tape': '03030303', 'curState': '00'};
let newState = {'headPos': 2, 'tape': '03030303', 'curState': '00'};
run(state, newState);
});
it('run step 11', () => {
let state = {'headPos': 2, 'tape': '03030303', 'curState': '00'};
let newState = {'headPos': 3, 'tape': '03030303', 'curState': '00'};
run(state, newState);
});
it('run step 12', () => {
let state = {'headPos': 3, 'tape': '03030303', 'curState': '00'};
let newState = {'headPos': 4, 'tape': '0303030300', 'curState': '00'};
run(state, newState);
});
it('run step 13', () => {
let state = {'headPos': 4, 'tape': '0303030300', 'curState': '00'};
let newState = {'headPos': 3, 'tape': '0303030300', 'curState': '02'};
run(state, newState);
});
it('run step 14', () => {
let state = {'headPos': 3, 'tape': '0303030300', 'curState': '02'};
let newState = {'headPos': 2, 'tape': '0303030300', 'curState': '02'};
run(state, newState);
});
it('run step 15', () => {
let state = {'headPos': 2, 'tape': '0303030300', 'curState': '02'};
let newState = {'headPos': 1, 'tape': '0303030300', 'curState': '02'};
run(state, newState);
});
it('run step 16', () => {
let state = {'headPos': 1, 'tape': '0303030300', 'curState': '02'};
let newState = {'headPos': 0, 'tape': '0303030300', 'curState': '02'};
run(state, newState);
});
it('run step 17', () => {
let state = {'headPos': 0, 'tape': '0303030300', 'curState': '02'};
let newState = {'headPos': 0, 'tape': '000303030300', 'curState': '02'};
run(state, newState);
});
it('run step 18', () => {
let state = {'headPos': 0, 'tape': '000303030300', 'curState': '02'};
let newState = {};
run(state, newState);
});
});