MCPcopy Create free account
hub / github.com/copernet/copernicus / GetScriptNum

Function GetScriptNum

model/script/scriptnum.go:101–140  ·  view source on GitHub ↗

GetScriptNum interprets the passed serialized bytes as an encoded integer and returns the result as a script number. Since the consensus rules dictate that serialized bytes interpreted as ints are only allowed to be in the range determined by a maximum number of bytes, on a per opcode basis, an err

(vch []byte, requireMinimal bool, maxNumSize int)

Source from the content-addressed store, hash-verified

99// defaultScriptNumLen, which could lead to addition and multiplication
100// overflows.
101func GetScriptNum(vch []byte, requireMinimal bool, maxNumSize int) (scriptNum *ScriptNum, err error) {
102 vchLen := len(vch)
103
104 if vchLen > maxNumSize {
105 log.Debug("ScriptErrNumberOverflow")
106 err = errcode.New(errcode.ScriptErrUnknownError)
107 scriptNum = NewScriptNum(0)
108 return
109 }
110 // one byte should > 0
111 // two bytes should > 255 or < -255
112 if requireMinimal {
113 if !IsMinimallyEncoded(vch, int64(maxNumSize)) {
114 log.Debug("ScriptNumIsNotMinimallyEncodede")
115 return NewScriptNum(0), errcode.New(errcode.ScriptErrUnknownError)
116 }
117 }
118
119 if vchLen == 0 {
120 scriptNum = NewScriptNum(0)
121 return
122 }
123
124 var v int64
125 for i := 0; i < vchLen; i++ {
126 v |= int64(vch[i]) << uint8(8*i)
127 }
128
129 // If the input vector's most significant byte is 0x80, remove it from
130 // the result and return a negative(set the sign bit of int64 to 1).
131 if vch[vchLen-1]&0x80 != 0 {
132 v &= ^(int64(0x80) << uint8(8*(vchLen-1)))
133 scriptNum = NewScriptNum(-v)
134 return
135 }
136
137 scriptNum = NewScriptNum(v)
138
139 return
140}
141
142func (n *ScriptNum) ToInt32() int32 {
143 if n.Value > MaxInt32 {

Callers 1

TestGetScriptNumFunction · 0.85

Calls 2

NewScriptNumFunction · 0.85
IsMinimallyEncodedFunction · 0.85

Tested by 1

TestGetScriptNumFunction · 0.68