Skip to content

Commit

Permalink
add oprversion
Browse files Browse the repository at this point in the history
  • Loading branch information
StarNeit committed Jul 14, 2020
1 parent d2342d4 commit 76b6893
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 37 deletions.
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func rootPreRunSetup(cmd *cobra.Command, args []string) error {
common.GradingHeights[common.TestNetwork] = func(height int64) uint8 { return 2 }
common.FloatingPegPriceActivation = 0
common.V4HeightActivation = 0
common.V20HeightActivation = 0
}

if testingact, _ := cmd.Flags().GetInt32("testingact"); testingact != -1 {
Expand Down
11 changes: 7 additions & 4 deletions common/activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ var (
if height < V4HeightActivation {
return 3
}
return 4 // Latest code version
if height < V20HeightActivation {
return 4
}
return 5 // Latest code version
},
TestNetwork: func(height int64) uint8 {
if height < 96145 { // V1 ends at 96145 on community testnet
Expand All @@ -46,10 +49,10 @@ var (
// V4HeightActivation indicates the activation of additional currencies and ecdsa keys.
// Estimated to be Feb 12, 2020, 18:00 UTC
V4HeightActivation int64 = 231620

// V20HeightActivation indicates the activation of PegNet 2.0.
// Estimated to be XXXX XXXX XXXX
V20HeightActivation int64 = 999999
// Estimated to be July 29th 2020 16:40 UTC
V20HeightActivation int64 = 255778
)

// NetworkActive returns true if the network height is above the activation height.
Expand Down
62 changes: 32 additions & 30 deletions polling/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func (d *DataSources) PullAllPEGAssets(oprversion uint8) (pa PegAssets, err erro
for _, asset := range assets {
var price PegItem
// For each asset we try and find the best price quote we can.
price, err := d.PullBestPrice(asset, start, cacheWrap)
price, err := d.PullBestPrice(asset, start, cacheWrap, oprversion)
if err != nil { // This will only be the last err in the data source list.
// No prices found for a peg, this pull failed
return nil, fmt.Errorf("no price found for %s : %s", asset, err.Error())
Expand Down Expand Up @@ -324,7 +324,7 @@ func (d *DataSources) PullAllPEGAssets(oprversion uint8) (pa PegAssets, err erro
// asset Asset to pull pricing data
// reference Time reference to determine 'staleness' from
// sources Map of datasources to pull the price quote from.
func (d *DataSources) PullBestPrice(asset string, reference time.Time, sources map[string]IDataSource) (pa PegItem, err error) {
func (d *DataSources) PullBestPrice(asset string, reference time.Time, sources map[string]IDataSource, oprversion uint8) (pa PegItem, err error) {
if sources == nil {
// If our data sources passed in are nil, then we don't need to do cache wrapping.
// We should always have sources passed in, aside from unit tests.
Expand All @@ -349,34 +349,36 @@ func (d *DataSources) PullBestPrice(asset string, reference time.Time, sources m
}
}

pricesClone := prices
if len(pricesClone) > 0 {
// We calculate the tolerance band here, and
// If one datasource returns a price out of the defined tolerance band,
// it will not allow that source’s price to be included in that block.
sort.Slice(pricesClone, func(i, j int) bool {
return pricesClone[i].Value < pricesClone[j].Value
})
middle := pricesClone[len(pricesClone)/2]

toleranceRate := 0.01
if middle.Value >= 100000 {
toleranceRate = 0.001
}
if middle.Value >= 1000 {
toleranceRate = 0.01
}
if middle.Value < 1000 {
toleranceRate = 0.1
}
toleranceBandHigh := middle.Value * (1 + toleranceRate)
toleranceBandLow := middle.Value * (1 - toleranceRate)

// We keep datasource priority order here.
for i := 0; i < len(prices); i++ {
currentPrice := prices[i]
if currentPrice.Value >= toleranceBandLow && currentPrice.Value <= toleranceBandHigh {
return currentPrice, nil
if oprversion == 5 {
pricesClone := prices
if len(pricesClone) > 0 {
// We calculate the tolerance band here, and
// If one datasource returns a price out of the defined tolerance band,
// it will not allow that source’s price to be included in that block.
sort.Slice(pricesClone, func(i, j int) bool {
return pricesClone[i].Value < pricesClone[j].Value
})
middle := pricesClone[len(pricesClone)/2]

toleranceRate := 0.01
if middle.Value >= 100000 {
toleranceRate = 0.001
}
if middle.Value >= 1000 {
toleranceRate = 0.01
}
if middle.Value < 1000 {
toleranceRate = 0.1
}
toleranceBandHigh := middle.Value * (1 + toleranceRate)
toleranceBandLow := middle.Value * (1 - toleranceRate)

// We keep datasource priority order here.
for i := 0; i < len(prices); i++ {
currentPrice := prices[i]
if currentPrice.Value >= toleranceBandLow && currentPrice.Value <= toleranceBandHigh {
return currentPrice, nil
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions polling/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func TestDataSourceStaleness(t *testing.T) {
d := NewDataSources(configWithStaleness("10m"))
d.AssetSources["EUR"] = reverse(names)

price, err := d.PullBestPrice("EUR", reference, mapped)
price, err := d.PullBestPrice("EUR", reference, mapped, 4)
if err != nil {
t.Error(err)
}
Expand All @@ -164,7 +164,7 @@ func TestDataSourceStaleness(t *testing.T) {
d = NewDataSources(configWithStaleness("0s"))
d.AssetSources["EUR"] = reverse(names)

price, err = d.PullBestPrice("EUR", reference, mapped)
price, err = d.PullBestPrice("EUR", reference, mapped, 4)
if err != nil {
t.Error(err)
}
Expand All @@ -177,7 +177,7 @@ func TestDataSourceStaleness(t *testing.T) {
d = NewDataSources(configWithStaleness("1h"))
d.AssetSources["EUR"] = reverse(names)

price, err = d.PullBestPrice("EUR", reference, mapped)
price, err = d.PullBestPrice("EUR", reference, mapped, 4)
if err != nil {
t.Error(err)
}
Expand Down

0 comments on commit 76b6893

Please sign in to comment.