fixed8: switch to more uniform function naming

This commit is contained in:
Roman Khimov 2019-08-23 15:41:22 +03:00
parent b0af8b306d
commit 0cde8d962d
6 changed files with 37 additions and 37 deletions

View file

@ -127,14 +127,14 @@ func Load(path string, netMode NetMode) (Config, error) {
func (s SystemFee) TryGetValue(txType transaction.TXType) util.Fixed8 { func (s SystemFee) TryGetValue(txType transaction.TXType) util.Fixed8 {
switch txType { switch txType {
case transaction.EnrollmentType: case transaction.EnrollmentType:
return util.NewFixed8(s.EnrollmentTransaction) return util.Fixed8FromInt64(s.EnrollmentTransaction)
case transaction.IssueType: case transaction.IssueType:
return util.NewFixed8(s.IssueTransaction) return util.Fixed8FromInt64(s.IssueTransaction)
case transaction.PublishType: case transaction.PublishType:
return util.NewFixed8(s.PublishTransaction) return util.Fixed8FromInt64(s.PublishTransaction)
case transaction.RegisterType: case transaction.RegisterType:
return util.NewFixed8(s.RegisterTransaction) return util.Fixed8FromInt64(s.RegisterTransaction)
default: default:
return util.NewFixed8(0) return util.Fixed8FromInt64(0)
} }
} }

View file

@ -635,14 +635,14 @@ func (bc *Blockchain) FeePerByte(t *transaction.Transaction) util.Fixed8 {
// NetworkFee returns network fee // NetworkFee returns network fee
func (bc *Blockchain) NetworkFee(t *transaction.Transaction) util.Fixed8 { func (bc *Blockchain) NetworkFee(t *transaction.Transaction) util.Fixed8 {
inputAmount := util.NewFixed8(0) inputAmount := util.Fixed8FromInt64(0)
for _, txOutput := range bc.References(t) { for _, txOutput := range bc.References(t) {
if txOutput.AssetID == utilityTokenTX().Hash() { if txOutput.AssetID == utilityTokenTX().Hash() {
inputAmount.Add(txOutput.Amount) inputAmount.Add(txOutput.Amount)
} }
} }
outputAmount := util.NewFixed8(0) outputAmount := util.Fixed8FromInt64(0)
for _, txOutput := range t.Outputs { for _, txOutput := range t.Outputs {
if txOutput.AssetID == utilityTokenTX().Hash() { if txOutput.AssetID == utilityTokenTX().Hash() {
outputAmount.Add(txOutput.Amount) 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 // IsLowPriority flags a trnsaction as low priority if the network fee is less than
// LowPriorityThreshold // LowPriorityThreshold
func (bc *Blockchain) IsLowPriority(t *transaction.Transaction) bool { 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. // GetMemPool returns the memory pool of the blockchain.

View file

@ -17,7 +17,7 @@ func TestRegisterTX(t *testing.T) {
Data: &RegisterTX{ Data: &RegisterTX{
AssetType: UtilityToken, AssetType: UtilityToken,
Name: "this is some token I created", Name: "this is some token I created",
Amount: util.NewFixed8(1000000), Amount: util.Fixed8FromInt64(1000000),
Precision: 8, Precision: 8,
Owner: &crypto.PublicKey{}, Owner: &crypto.PublicKey{},
Admin: util.RandomUint160(), Admin: util.RandomUint160(),
@ -48,7 +48,7 @@ func TestDecodeRegisterTXFromRawString(t *testing.T) {
txData := tx.Data.(*RegisterTX) txData := tx.Data.(*RegisterTX)
assert.Equal(t, GoverningToken, txData.AssetType) assert.Equal(t, GoverningToken, txData.AssetType)
assert.Equal(t, "[{\"lang\":\"zh-CN\",\"name\":\"小蚁股\"},{\"lang\":\"en\",\"name\":\"AntShare\"}]", txData.Name) 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, uint8(0), txData.Precision)
assert.Equal(t, &crypto.PublicKey{}, txData.Owner) assert.Equal(t, &crypto.PublicKey{}, txData.Owner)
assert.Equal(t, "Abf2qMs1pzQb8kYk9RuxtUb9jtRKJVuBJt", crypto.AddressFromUint160(txData.Admin)) assert.Equal(t, "Abf2qMs1pzQb8kYk9RuxtUb9jtRKJVuBJt", crypto.AddressFromUint160(txData.Admin))

View file

@ -101,7 +101,7 @@ func governingTokenTX() *transaction.Transaction {
registerTX := &transaction.RegisterTX{ registerTX := &transaction.RegisterTX{
AssetType: transaction.GoverningToken, AssetType: transaction.GoverningToken,
Name: "[{\"lang\":\"zh-CN\",\"name\":\"小蚁股\"},{\"lang\":\"en\",\"name\":\"AntShare\"}]", Name: "[{\"lang\":\"zh-CN\",\"name\":\"小蚁股\"},{\"lang\":\"en\",\"name\":\"AntShare\"}]",
Amount: util.NewFixed8(100000000), Amount: util.Fixed8FromInt64(100000000),
Precision: 0, Precision: 0,
Owner: &crypto.PublicKey{}, Owner: &crypto.PublicKey{},
Admin: admin, Admin: admin,
@ -170,7 +170,7 @@ func calculateUtilityAmount() util.Fixed8 {
for i := 0; i < len(genAmount); i++ { for i := 0; i < len(genAmount); i++ {
sum += genAmount[i] sum += genAmount[i]
} }
return util.NewFixed8(int64(sum * decrementInterval)) return util.Fixed8FromInt64(int64(sum * decrementInterval))
} }
// headerSliceReverse reverses the given slice of *Header. // headerSliceReverse reverses the given slice of *Header.

View file

@ -49,19 +49,19 @@ func (f Fixed8) Int64Value() int64 {
return int64(f) / decimals return int64(f) / decimals
} }
// NewFixed8 returns a new Fixed8 type multiplied by decimals. // Fixed8FromInt64 returns a new Fixed8 type multiplied by decimals.
func NewFixed8(val int64) Fixed8 { func Fixed8FromInt64(val int64) Fixed8 {
return Fixed8(decimals * val) return Fixed8(decimals * val)
} }
// NewFixed8FromFloat returns a new Fixed8 type multiplied by decimals. // Fixed8FromFloat returns a new Fixed8 type multiplied by decimals.
func NewFixed8FromFloat(val float64) Fixed8 { func Fixed8FromFloat(val float64) Fixed8 {
return Fixed8(int64(decimals * val)) 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 // with precision up to 10^-8
func Fixed8DecodeString(s string) (Fixed8, error) { func Fixed8FromString(s string) (Fixed8, error) {
parts := strings.SplitN(s, ".", 2) parts := strings.SplitN(s, ".", 2)
ip, err := strconv.ParseInt(parts[0], 10, 64) ip, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil { if err != nil {
@ -87,7 +87,7 @@ func Fixed8DecodeString(s string) (Fixed8, error) {
func (f *Fixed8) UnmarshalJSON(data []byte) error { func (f *Fixed8) UnmarshalJSON(data []byte) error {
var s string var s string
if err := json.Unmarshal(data, &s); err == nil { if err := json.Unmarshal(data, &s); err == nil {
p, err := Fixed8DecodeString(s) p, err := Fixed8FromString(s)
if err != nil { if err != nil {
return err return err
} }
@ -121,7 +121,7 @@ func Satoshi() Fixed8 {
// Div implements Fixd8 division operator. // Div implements Fixd8 division operator.
func (f Fixed8) Div(i int64) Fixed8 { func (f Fixed8) Div(i int64) Fixed8 {
return f / NewFixed8(i) return f / Fixed8FromInt64(i)
} }
// Add implements Fixd8 addition operator. // Add implements Fixd8 addition operator.

View file

@ -8,18 +8,18 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestNewFixed8(t *testing.T) { func TestFixed8FromInt64(t *testing.T) {
values := []int64{9000, 100000000, 5, 10945, -42} values := []int64{9000, 100000000, 5, 10945, -42}
for _, val := range values { for _, val := range values {
assert.Equal(t, Fixed8(val*decimals), NewFixed8(val)) assert.Equal(t, Fixed8(val*decimals), Fixed8FromInt64(val))
assert.Equal(t, val, NewFixed8(val).Int64Value()) assert.Equal(t, val, Fixed8FromInt64(val).Int64Value())
} }
} }
func TestFixed8Add(t *testing.T) { func TestFixed8Add(t *testing.T) {
a := NewFixed8(1) a := Fixed8FromInt64(1)
b := NewFixed8(2) b := Fixed8FromInt64(2)
c := a.Add(b) c := a.Add(b)
expected := int64(3) expected := int64(3)
@ -28,8 +28,8 @@ func TestFixed8Add(t *testing.T) {
func TestFixed8Sub(t *testing.T) { func TestFixed8Sub(t *testing.T) {
a := NewFixed8(42) a := Fixed8FromInt64(42)
b := NewFixed8(34) b := Fixed8FromInt64(34)
c := a.Sub(b) c := a.Sub(b)
assert.Equal(t, int64(8), c.Int64Value()) 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} inputs := []float64{12.98, 23.87654333, 100.654322, 456789.12345665, -3.14159265}
for _, val := range inputs { for _, val := range inputs {
assert.Equal(t, Fixed8(val*decimals), NewFixed8FromFloat(val)) assert.Equal(t, Fixed8(val*decimals), Fixed8FromFloat(val))
assert.Equal(t, val, NewFixed8FromFloat(val).FloatValue()) assert.Equal(t, val, Fixed8FromFloat(val).FloatValue())
} }
} }
func TestFixed8DecodeString(t *testing.T) { func TestFixed8FromString(t *testing.T) {
// Fixed8DecodeString works correctly with integers // Fixed8FromString works correctly with integers
ivalues := []string{"9000", "100000000", "5", "10945", "20.45", "0.00000001", "-42"} ivalues := []string{"9000", "100000000", "5", "10945", "20.45", "0.00000001", "-42"}
for _, val := range ivalues { for _, val := range ivalues {
n, err := Fixed8DecodeString(val) n, err := Fixed8FromString(val)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, val, n.String()) assert.Equal(t, val, n.String())
} }
// Fixed8DecodeString parses number with maximal precision // Fixed8FromString parses number with maximal precision
val := "123456789.12345678" val := "123456789.12345678"
n, err := Fixed8DecodeString(val) n, err := Fixed8FromString(val)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, Fixed8(12345678912345678), n) assert.Equal(t, Fixed8(12345678912345678), n)
// Fixed8DecodeString parses number with non-maximal precision // Fixed8FromString parses number with non-maximal precision
val = "901.2341" val = "901.2341"
n, err = Fixed8DecodeString(val) n, err = Fixed8FromString(val)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, Fixed8(90123410000), n) assert.Equal(t, Fixed8(90123410000), n)
} }
@ -79,7 +79,7 @@ func TestFixed8UnmarshalJSON(t *testing.T) {
for _, fl := range testCases { for _, fl := range testCases {
str := strconv.FormatFloat(fl, 'g', -1, 64) str := strconv.FormatFloat(fl, 'g', -1, 64)
expected, _ := Fixed8DecodeString(str) expected, _ := Fixed8FromString(str)
// UnmarshalJSON should decode floats // UnmarshalJSON should decode floats
var u1 Fixed8 var u1 Fixed8