OSDN Git Service

edit code for elegant (#1983)
[bytom/bytom.git] / protocol / vm / introspection.go
1 package vm
2
3 import (
4         "github.com/holiman/uint256"
5 )
6
7 func opCheckOutput(vm *virtualMachine) error {
8         if err := vm.applyCost(16); err != nil {
9                 return err
10         }
11
12         code, err := vm.pop(true)
13         if err != nil {
14                 return err
15         }
16
17         vmVersion, err := vm.popBigInt(true)
18         if err != nil {
19                 return err
20         }
21
22         assetID, err := vm.pop(true)
23         if err != nil {
24                 return err
25         }
26
27         amountInt, err := vm.popBigInt(true)
28         if err != nil {
29                 return err
30         }
31
32         amount, overflow := amountInt.Uint64WithOverflow()
33         if overflow {
34                 return ErrBadValue
35         }
36
37         index, err := vm.popBigInt(true)
38         if err != nil {
39                 return err
40         }
41
42         if vm.context.CheckOutput == nil {
43                 return ErrContext
44         }
45
46         ok, err := vm.context.CheckOutput(uint64(index.Uint64()), amount, assetID, uint64(vmVersion.Uint64()), code, vm.altStack, vm.expansionReserved)
47         if err != nil {
48                 return err
49         }
50         return vm.pushBool(ok, true)
51 }
52
53 func opAsset(vm *virtualMachine) error {
54         if err := vm.applyCost(1); err != nil {
55                 return err
56         }
57
58         if vm.context.AssetID == nil {
59                 return ErrContext
60         }
61         return vm.pushDataStack(*vm.context.AssetID, true)
62 }
63
64 func opAmount(vm *virtualMachine) error {
65         if err := vm.applyCost(1); err != nil {
66                 return err
67         }
68
69         if vm.context.Amount == nil {
70                 return ErrContext
71         }
72
73         return vm.pushBigInt(uint256.NewInt().SetUint64(*vm.context.Amount), true)
74 }
75
76 func opProgram(vm *virtualMachine) error {
77         if err := vm.applyCost(1); err != nil {
78                 return err
79         }
80
81         return vm.pushDataStack(vm.context.Code, true)
82 }
83
84 func opIndex(vm *virtualMachine) error {
85         if err := vm.applyCost(1); err != nil {
86                 return err
87         }
88
89         if vm.context.DestPos == nil {
90                 return ErrContext
91         }
92
93         return vm.pushBigInt(uint256.NewInt().SetUint64(*vm.context.DestPos), true)
94 }
95
96 func opEntryID(vm *virtualMachine) error {
97         if err := vm.applyCost(1); err != nil {
98                 return err
99         }
100         return vm.pushDataStack(vm.context.EntryID, true)
101 }
102
103 func opOutputID(vm *virtualMachine) error {
104         if err := vm.applyCost(1); err != nil {
105                 return err
106         }
107
108         if vm.context.SpentOutputID == nil {
109                 return ErrContext
110         }
111         return vm.pushDataStack(*vm.context.SpentOutputID, true)
112 }
113
114 func opBlockHeight(vm *virtualMachine) error {
115         if err := vm.applyCost(1); err != nil {
116                 return err
117         }
118
119         if vm.context.BlockHeight == nil {
120                 return ErrContext
121         }
122
123         return vm.pushBigInt(uint256.NewInt().SetUint64(*vm.context.BlockHeight), true)
124 }