aboutsummaryrefslogtreecommitdiffstats
path: root/core/vm/sqlvm/runtime/instructions.go
diff options
context:
space:
mode:
authorTing-Wei Lan <tingwei.lan@cobinhood.com>2019-04-02 16:50:01 +0800
committerJhih-Ming Huang <jm@dexon.org>2019-05-08 13:20:53 +0800
commit6008337f6e562af1e9cf4c6ab70fe2c09d993c62 (patch)
tree9c066ce7df5f49301844c98d06c919e979eff549 /core/vm/sqlvm/runtime/instructions.go
parent682e15d8212cc376509f8f2ef854adf48efb64ef (diff)
downloaddexon-6008337f6e562af1e9cf4c6ab70fe2c09d993c62.tar
dexon-6008337f6e562af1e9cf4c6ab70fe2c09d993c62.tar.gz
dexon-6008337f6e562af1e9cf4c6ab70fe2c09d993c62.tar.bz2
dexon-6008337f6e562af1e9cf4c6ab70fe2c09d993c62.tar.lz
dexon-6008337f6e562af1e9cf4c6ab70fe2c09d993c62.tar.xz
dexon-6008337f6e562af1e9cf4c6ab70fe2c09d993c62.tar.zst
dexon-6008337f6e562af1e9cf4c6ab70fe2c09d993c62.zip
core: vm: sqlvm: return a bool from DecimalEncode and DecimalDecode
Instead of returning a fixed error code, we can return a boolean here because the only possibility of an error is invalid arguments. Callers of these functions must know where invalid arguments come from, and correctly handle it by converting it to an error struct or panicking.
Diffstat (limited to 'core/vm/sqlvm/runtime/instructions.go')
-rw-r--r--core/vm/sqlvm/runtime/instructions.go52
1 files changed, 28 insertions, 24 deletions
diff --git a/core/vm/sqlvm/runtime/instructions.go b/core/vm/sqlvm/runtime/instructions.go
index 6fa72d61a..35d3135b2 100644
--- a/core/vm/sqlvm/runtime/instructions.go
+++ b/core/vm/sqlvm/runtime/instructions.go
@@ -188,16 +188,16 @@ func decode(ctx *common.Context, dt ast.DataType, slot dexCommon.Hash, bytes []b
case ast.DataTypeMajorFixedBytes, ast.DataTypeMajorAddress:
rVal.Bytes = bytes
case ast.DataTypeMajorBool, ast.DataTypeMajorInt, ast.DataTypeMajorUint:
- d, err := ast.DecimalDecode(dt, bytes)
- if err != nil {
- return nil, err
+ d, ok := ast.DecimalDecode(dt, bytes)
+ if !ok {
+ panic(fmt.Sprintf("DecimalDecode does not handle %v", dt))
}
rVal.Value = d
}
if major.IsFixedRange() || major.IsUfixedRange() {
- d, err := ast.DecimalDecode(dt, bytes)
- if err != nil {
- return nil, err
+ d, ok := ast.DecimalDecode(dt, bytes)
+ if !ok {
+ panic(fmt.Sprintf("DecimalDecode does not handle %v", dt))
}
rVal.Value = d
}
@@ -1607,16 +1607,16 @@ func (r *Raw) castValue(
ctx *common.Context,
origin, target ast.DataType,
l int, signed, rPadding bool) (err error) {
- oBytes, err := ast.DecimalEncode(origin, r.Value)
- if err != nil {
- return
+ oBytes, ok := ast.DecimalEncode(origin, r.Value)
+ if !ok {
+ panic(fmt.Sprintf("DecimalEncode does not handle %v", origin))
}
bytes2 := r.shiftBytes(oBytes, l, signed, rPadding)
- r.Value, err = ast.DecimalDecode(target, bytes2)
- if err != nil {
- return
+ r.Value, ok = ast.DecimalDecode(target, bytes2)
+ if !ok {
+ panic(fmt.Sprintf("DecimalDecode does not handle %v", target))
}
err = flowCheck(ctx, r.Value, target)
@@ -1644,9 +1644,10 @@ func (r *Raw) castInt(ctx *common.Context, origin, target ast.DataType) (err err
return
}
- r.Bytes, err = ast.DecimalEncode(mockDt, r.Value)
- if err != nil {
- return
+ var ok bool
+ r.Bytes, ok = ast.DecimalEncode(mockDt, r.Value)
+ if !ok {
+ panic(fmt.Sprintf("DecimalEncode does not handle %v", origin))
}
r.Value = decimal.Zero
case ast.DataTypeMajorFixedBytes:
@@ -1654,9 +1655,10 @@ func (r *Raw) castInt(ctx *common.Context, origin, target ast.DataType) (err err
err = se.ErrorCodeInvalidCastType
return
}
- r.Bytes, err = ast.DecimalEncode(origin, r.Value)
- if err != nil {
- return
+ var ok bool
+ r.Bytes, ok = ast.DecimalEncode(origin, r.Value)
+ if !ok {
+ panic(fmt.Sprintf("DecimalEncode does not handle %v", origin))
}
r.Value = decimal.Zero
case ast.DataTypeMajorBool:
@@ -1677,9 +1679,10 @@ func (r *Raw) castFixedBytes(ctx *common.Context, origin, target ast.DataType) (
err = se.ErrorCodeInvalidCastType
return
}
- r.Value, err = ast.DecimalDecode(target, r.Bytes)
- if err != nil {
- return
+ var ok bool
+ r.Value, ok = ast.DecimalDecode(target, r.Bytes)
+ if !ok {
+ panic(fmt.Sprintf("DecimalDecode does not handle %v", target))
}
r.Bytes = nil
case ast.DataTypeMajorFixedBytes:
@@ -1701,12 +1704,13 @@ func (r *Raw) castAddress(ctx *common.Context, origin, target ast.DataType) (err
switch tMajor {
case ast.DataTypeMajorAddress:
case ast.DataTypeMajorInt, ast.DataTypeMajorUint:
- r.Value, err = ast.DecimalDecode(
+ var ok bool
+ r.Value, ok = ast.DecimalDecode(
target,
r.shiftBytes(r.Bytes, int(tMinor)+1, false, false),
)
- if err != nil {
- return
+ if !ok {
+ panic(fmt.Sprintf("DecimalDecode does not handle %v", target))
}
err = flowCheck(ctx, r.Value, target)
if err != nil {