5 var optimizations = []struct {
15 {"OVER OVER", "2DUP"},
16 {"SWAP OVER", "TUCK"},
17 {"DROP DROP", "2DROP"},
19 {"5 ROLL 5 ROLL", "2ROT"},
20 {"3 PICK 3 PICK", "2OVER"},
21 {"3 ROLL 3 ROLL", "2SWAP"},
22 {"2 PICK 2 PICK 2 PICK", "3DUP"},
25 {"EQUAL VERIFY", "EQUALVERIFY"},
26 {"SWAP TXSIGHASH ROT", "TXSIGHASH SWAP"},
27 {"SWAP EQUAL", "EQUAL"},
28 {"SWAP EQUALVERIFY", "EQUALVERIFY"},
30 {"SWAP BOOLAND", "BOOLAND"},
31 {"SWAP BOOLOR", "BOOLOR"},
34 {"DUP 2 PICK EQUAL", "2DUP EQUAL"},
35 {"DUP 2 PICK EQUALVERIFY", "2DUP EQUALVERIFY"},
36 {"DUP 2 PICK ADD", "2DUP ADD"},
37 {"DUP 2 PICK BOOLAND", "2DUP BOOLAND"},
38 {"DUP 2 PICK BOOLOR", "2DUP BOOLOR"},
39 {"DUP 2 PICK MIN", "2DUP MIN"},
40 {"DUP 2 PICK MAX", "2DUP MAX"},
43 func optimize(opcodes string) string {
44 opcodes = " " + opcodes + " "
48 for _, o := range optimizations {
49 before := " " + o.before + " "
54 after = " " + o.after + " "
56 newOpcodes := strings.Replace(opcodes, before, after, -1)
57 if newOpcodes != opcodes {
63 return strings.TrimSpace(opcodes)