diff options
author | Meng-Ying Yang <garfield@dexon.org> | 2019-03-07 10:35:59 +0800 |
---|---|---|
committer | Jhih-Ming Huang <jm.huang@cobinhood.com> | 2019-05-06 10:44:04 +0800 |
commit | ba3337841485beb022d75e9182b380a192c420e2 (patch) | |
tree | afb63df8f9c82770c046fe1dd4b235f8888fb9d5 | |
parent | 72aec0139d85a2b5e2a5684f1f0a20f2db208d2e (diff) | |
download | dexon-ba3337841485beb022d75e9182b380a192c420e2.tar.gz dexon-ba3337841485beb022d75e9182b380a192c420e2.tar.zst dexon-ba3337841485beb022d75e9182b380a192c420e2.zip |
core: vm: sqlvm: add shared variables (#228)
-rw-r--r-- | core/vm/sqlvm/ast/types.go | 14 | ||||
-rw-r--r-- | core/vm/sqlvm/ast/types_test.go | 5 | ||||
-rw-r--r-- | core/vm/sqlvm/common/decimal/decimal.go | 11 |
3 files changed, 19 insertions, 11 deletions
diff --git a/core/vm/sqlvm/ast/types.go b/core/vm/sqlvm/ast/types.go index 19bcf6314..1928d2338 100644 --- a/core/vm/sqlvm/ast/types.go +++ b/core/vm/sqlvm/ast/types.go @@ -7,6 +7,7 @@ import ( "github.com/shopspring/decimal" "github.com/dexon-foundation/dexon/common" + dec "github.com/dexon-foundation/dexon/core/vm/sqlvm/common/decimal" se "github.com/dexon-foundation/dexon/core/vm/sqlvm/errors" ) @@ -20,11 +21,6 @@ type decPair struct { } var ( - decOne = decimal.New(1, 0) - - decFalse = decimal.Zero - decTrue = decimal.New(1, 0) - decPairMap = make(map[DataType]decPair) ) @@ -260,18 +256,18 @@ func GetMinMax(dt DataType) (min, max decimal.Decimal, err error) { major, minor := DecomposeDataType(dt) switch major { case DataTypeMajorBool: - min, max = decFalse, decTrue + min, max = dec.False, dec.True case DataTypeMajorAddress: bigUMax := new(big.Int).Lsh(bigIntOne, common.AddressLength*8) - max = decimal.NewFromBigInt(bigUMax, 0).Sub(decOne) + max = decimal.NewFromBigInt(bigUMax, 0).Sub(dec.One) case DataTypeMajorInt: bigMax := new(big.Int).Lsh(bigIntOne, (uint(minor)+1)*8-1) decMax := decimal.NewFromBigInt(bigMax, 0) - min, max = decMax.Neg(), decMax.Sub(decOne) + min, max = decMax.Neg(), decMax.Sub(dec.One) case DataTypeMajorUint, DataTypeMajorFixedBytes: bigUMax := new(big.Int).Lsh(bigIntOne, (uint(minor)+1)*8) - max = decimal.NewFromBigInt(bigUMax, 0).Sub(decOne) + max = decimal.NewFromBigInt(bigUMax, 0).Sub(dec.One) default: err = ErrGetMinMax return diff --git a/core/vm/sqlvm/ast/types_test.go b/core/vm/sqlvm/ast/types_test.go index efdea17bc..17378d2bd 100644 --- a/core/vm/sqlvm/ast/types_test.go +++ b/core/vm/sqlvm/ast/types_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/dexon-foundation/dexon/common" + dec "github.com/dexon-foundation/dexon/core/vm/sqlvm/common/decimal" "github.com/shopspring/decimal" "github.com/stretchr/testify/suite" ) @@ -154,14 +155,14 @@ func (s *TypesTestSuite) TestEncodeAndDecodeDecimal() { } func (s *TypesTestSuite) TestGetMinMax() { - decAddressMax := decimal.New(2, 0).Pow(decimal.New(common.AddressLength*8, 0)).Sub(decOne) + decAddressMax := decimal.New(2, 0).Pow(decimal.New(common.AddressLength*8, 0)).Sub(dec.One) testcases := []struct { Name string In DataType Min, Max decimal.Decimal Err error }{ - {"Bool", ComposeDataType(DataTypeMajorBool, 0), decFalse, decTrue, nil}, + {"Bool", ComposeDataType(DataTypeMajorBool, 0), dec.False, dec.True, nil}, {"Address", ComposeDataType(DataTypeMajorAddress, 0), decimal.Zero, decAddressMax, nil}, {"Int8", ComposeDataType(DataTypeMajorInt, 0), decimal.New(-128, 0), decimal.New(127, 0), nil}, {"Int16", ComposeDataType(DataTypeMajorInt, 1), decimal.New(-32768, 0), decimal.New(32767, 0), nil}, diff --git a/core/vm/sqlvm/common/decimal/decimal.go b/core/vm/sqlvm/common/decimal/decimal.go new file mode 100644 index 000000000..0c3d5971a --- /dev/null +++ b/core/vm/sqlvm/common/decimal/decimal.go @@ -0,0 +1,11 @@ +package decimal + +import "github.com/shopspring/decimal" + +// Shared vars. +var ( + One = decimal.New(1, 0) + + False = decimal.New(0, 0) + True = decimal.New(1, 0) +) |