diff --git a/config/config.go b/config/config.go index de855f88d..67eed94d6 100644 --- a/config/config.go +++ b/config/config.go @@ -127,14 +127,14 @@ func Load(path string, netMode NetMode) (Config, error) { func (s SystemFee) TryGetValue(txType transaction.TXType) util.Fixed8 { switch txType { case transaction.EnrollmentType: - return util.NewFixed8(s.EnrollmentTransaction) + return util.Fixed8FromInt64(s.EnrollmentTransaction) case transaction.IssueType: - return util.NewFixed8(s.IssueTransaction) + return util.Fixed8FromInt64(s.IssueTransaction) case transaction.PublishType: - return util.NewFixed8(s.PublishTransaction) + return util.Fixed8FromInt64(s.PublishTransaction) case transaction.RegisterType: - return util.NewFixed8(s.RegisterTransaction) + return util.Fixed8FromInt64(s.RegisterTransaction) default: - return util.NewFixed8(0) + return util.Fixed8FromInt64(0) } } diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 1cbdda6a7..bd8e6365d 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -635,14 +635,14 @@ func (bc *Blockchain) FeePerByte(t *transaction.Transaction) util.Fixed8 { // NetworkFee returns network fee func (bc *Blockchain) NetworkFee(t *transaction.Transaction) util.Fixed8 { - inputAmount := util.NewFixed8(0) + inputAmount := util.Fixed8FromInt64(0) for _, txOutput := range bc.References(t) { if txOutput.AssetID == utilityTokenTX().Hash() { inputAmount.Add(txOutput.Amount) } } - outputAmount := util.NewFixed8(0) + outputAmount := util.Fixed8FromInt64(0) for _, txOutput := range t.Outputs { if txOutput.AssetID == utilityTokenTX().Hash() { outputAmount.Add(txOutput.Amount) @@ -660,7 +660,7 @@ func (bc *Blockchain) SystemFee(t *transaction.Transaction) util.Fixed8 { // IsLowPriority flags a trnsaction as low priority if the network fee is less than // LowPriorityThreshold func (bc *Blockchain) IsLowPriority(t *transaction.Transaction) bool { - return bc.NetworkFee(t) < util.NewFixed8FromFloat(bc.GetConfig().LowPriorityThreshold) + return bc.NetworkFee(t) < util.Fixed8FromFloat(bc.GetConfig().LowPriorityThreshold) } // GetMemPool returns the memory pool of the blockchain. diff --git a/pkg/core/transaction/register_test.go b/pkg/core/transaction/register_test.go index 00ef0f218..8512f8f7d 100644 --- a/pkg/core/transaction/register_test.go +++ b/pkg/core/transaction/register_test.go @@ -17,7 +17,7 @@ func TestRegisterTX(t *testing.T) { Data: &RegisterTX{ AssetType: UtilityToken, Name: "this is some token I created", - Amount: util.NewFixed8(1000000), + Amount: util.Fixed8FromInt64(1000000), Precision: 8, Owner: &crypto.PublicKey{}, Admin: util.RandomUint160(), @@ -48,7 +48,7 @@ func TestDecodeRegisterTXFromRawString(t *testing.T) { txData := tx.Data.(*RegisterTX) assert.Equal(t, GoverningToken, txData.AssetType) assert.Equal(t, "[{\"lang\":\"zh-CN\",\"name\":\"小蚁股\"},{\"lang\":\"en\",\"name\":\"AntShare\"}]", txData.Name) - assert.Equal(t, util.NewFixed8(100000000), txData.Amount) + assert.Equal(t, util.Fixed8FromInt64(100000000), txData.Amount) assert.Equal(t, uint8(0), txData.Precision) assert.Equal(t, &crypto.PublicKey{}, txData.Owner) assert.Equal(t, "Abf2qMs1pzQb8kYk9RuxtUb9jtRKJVuBJt", crypto.AddressFromUint160(txData.Admin)) diff --git a/pkg/core/util.go b/pkg/core/util.go index a5eab4b85..dffbc7b41 100644 --- a/pkg/core/util.go +++ b/pkg/core/util.go @@ -101,7 +101,7 @@ func governingTokenTX() *transaction.Transaction { registerTX := &transaction.RegisterTX{ AssetType: transaction.GoverningToken, Name: "[{\"lang\":\"zh-CN\",\"name\":\"小蚁股\"},{\"lang\":\"en\",\"name\":\"AntShare\"}]", - Amount: util.NewFixed8(100000000), + Amount: util.Fixed8FromInt64(100000000), Precision: 0, Owner: &crypto.PublicKey{}, Admin: admin, @@ -170,7 +170,7 @@ func calculateUtilityAmount() util.Fixed8 { for i := 0; i < len(genAmount); i++ { sum += genAmount[i] } - return util.NewFixed8(int64(sum * decrementInterval)) + return util.Fixed8FromInt64(int64(sum * decrementInterval)) } // headerSliceReverse reverses the given slice of *Header. diff --git a/pkg/util/fixed8.go b/pkg/util/fixed8.go index 21317a2aa..ec7b96d61 100644 --- a/pkg/util/fixed8.go +++ b/pkg/util/fixed8.go @@ -49,19 +49,19 @@ func (f Fixed8) Int64Value() int64 { return int64(f) / decimals } -// NewFixed8 returns a new Fixed8 type multiplied by decimals. -func NewFixed8(val int64) Fixed8 { +// Fixed8FromInt64 returns a new Fixed8 type multiplied by decimals. +func Fixed8FromInt64(val int64) Fixed8 { return Fixed8(decimals * val) } -// NewFixed8FromFloat returns a new Fixed8 type multiplied by decimals. -func NewFixed8FromFloat(val float64) Fixed8 { +// Fixed8FromFloat returns a new Fixed8 type multiplied by decimals. +func Fixed8FromFloat(val float64) Fixed8 { return Fixed8(int64(decimals * val)) } -// Fixed8DecodeString parses s which must be a fixed point number +// Fixed8FromString parses s which must be a fixed point number // with precision up to 10^-8 -func Fixed8DecodeString(s string) (Fixed8, error) { +func Fixed8FromString(s string) (Fixed8, error) { parts := strings.SplitN(s, ".", 2) ip, err := strconv.ParseInt(parts[0], 10, 64) if err != nil { @@ -87,7 +87,7 @@ func Fixed8DecodeString(s string) (Fixed8, error) { func (f *Fixed8) UnmarshalJSON(data []byte) error { var s string if err := json.Unmarshal(data, &s); err == nil { - p, err := Fixed8DecodeString(s) + p, err := Fixed8FromString(s) if err != nil { return err } @@ -121,7 +121,7 @@ func Satoshi() Fixed8 { // Div implements Fixd8 division operator. func (f Fixed8) Div(i int64) Fixed8 { - return f / NewFixed8(i) + return f / Fixed8FromInt64(i) } // Add implements Fixd8 addition operator. diff --git a/pkg/util/fixed8_test.go b/pkg/util/fixed8_test.go index 352b75250..aea003130 100644 --- a/pkg/util/fixed8_test.go +++ b/pkg/util/fixed8_test.go @@ -8,18 +8,18 @@ import ( "github.com/stretchr/testify/assert" ) -func TestNewFixed8(t *testing.T) { +func TestFixed8FromInt64(t *testing.T) { values := []int64{9000, 100000000, 5, 10945, -42} for _, val := range values { - assert.Equal(t, Fixed8(val*decimals), NewFixed8(val)) - assert.Equal(t, val, NewFixed8(val).Int64Value()) + assert.Equal(t, Fixed8(val*decimals), Fixed8FromInt64(val)) + assert.Equal(t, val, Fixed8FromInt64(val).Int64Value()) } } func TestFixed8Add(t *testing.T) { - a := NewFixed8(1) - b := NewFixed8(2) + a := Fixed8FromInt64(1) + b := Fixed8FromInt64(2) c := a.Add(b) expected := int64(3) @@ -28,8 +28,8 @@ func TestFixed8Add(t *testing.T) { func TestFixed8Sub(t *testing.T) { - a := NewFixed8(42) - b := NewFixed8(34) + a := Fixed8FromInt64(42) + b := Fixed8FromInt64(34) c := a.Sub(b) assert.Equal(t, int64(8), c.Int64Value()) @@ -39,29 +39,29 @@ func TestFixed8FromFloat(t *testing.T) { inputs := []float64{12.98, 23.87654333, 100.654322, 456789.12345665, -3.14159265} for _, val := range inputs { - assert.Equal(t, Fixed8(val*decimals), NewFixed8FromFloat(val)) - assert.Equal(t, val, NewFixed8FromFloat(val).FloatValue()) + assert.Equal(t, Fixed8(val*decimals), Fixed8FromFloat(val)) + assert.Equal(t, val, Fixed8FromFloat(val).FloatValue()) } } -func TestFixed8DecodeString(t *testing.T) { - // Fixed8DecodeString works correctly with integers +func TestFixed8FromString(t *testing.T) { + // Fixed8FromString works correctly with integers ivalues := []string{"9000", "100000000", "5", "10945", "20.45", "0.00000001", "-42"} for _, val := range ivalues { - n, err := Fixed8DecodeString(val) + n, err := Fixed8FromString(val) assert.Nil(t, err) assert.Equal(t, val, n.String()) } - // Fixed8DecodeString parses number with maximal precision + // Fixed8FromString parses number with maximal precision val := "123456789.12345678" - n, err := Fixed8DecodeString(val) + n, err := Fixed8FromString(val) assert.Nil(t, err) assert.Equal(t, Fixed8(12345678912345678), n) - // Fixed8DecodeString parses number with non-maximal precision + // Fixed8FromString parses number with non-maximal precision val = "901.2341" - n, err = Fixed8DecodeString(val) + n, err = Fixed8FromString(val) assert.Nil(t, err) assert.Equal(t, Fixed8(90123410000), n) } @@ -79,7 +79,7 @@ func TestFixed8UnmarshalJSON(t *testing.T) { for _, fl := range testCases { str := strconv.FormatFloat(fl, 'g', -1, 64) - expected, _ := Fixed8DecodeString(str) + expected, _ := Fixed8FromString(str) // UnmarshalJSON should decode floats var u1 Fixed8