Skip to content

Commit

Permalink
Adding activation for OPR Version 2
Browse files Browse the repository at this point in the history
- Diffs come from another branch
  • Loading branch information
Emyrk committed Aug 26, 2019
1 parent b8fe9ca commit 9b2069b
Show file tree
Hide file tree
Showing 39 changed files with 935 additions and 141 deletions.
14 changes: 10 additions & 4 deletions api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package api
import (
"strconv"

"github.com/pegnet/pegnet/common"
"github.com/FactomProject/factom"
"github.com/pegnet/pegnet/opr"
)
Expand All @@ -14,8 +15,13 @@ import (
// Required for M1

func (a *APIServer) getPerformance(params interface{}) (*PerformanceResult, *Error) {
net, err := common.LoadConfigNetwork(a.config)
if err != nil {
return nil, NewInternalError()
}

performanceParams := new(PerformanceParameters)
err := MapToObject(params, performanceParams)
err = MapToObject(params, performanceParams)
if err != nil {
return nil, NewJSONDecodingError()
}
Expand Down Expand Up @@ -79,7 +85,7 @@ func (a *APIServer) getPerformance(params interface{}) (*PerformanceResult, *Err
// TODO: Rename param to fit coinbase option
if record.FactomDigitalID == performanceParams.DigitalID || record.CoinbaseAddress == performanceParams.DigitalID {
submissions += 1
if i <= 50 {
if i < 50 {
difficultyPlacementsCount += 1
difficultyPlacementsSum += int64(i + 1)
for k := range difficultyPlacements {
Expand All @@ -94,7 +100,7 @@ func (a *APIServer) getPerformance(params interface{}) (*PerformanceResult, *Err
for i, record := range block.GradedOPRs {
// TODO: Rename param to fit coinbase option
if record.FactomDigitalID == performanceParams.DigitalID || record.CoinbaseAddress == performanceParams.DigitalID {
rewards += int64(opr.GetRewardFromPlace(i))
rewards += int64(opr.GetRewardFromPlace(i, net, int64(record.Dbht)))
gradingPlacementsCount += 1
gradingPlacementsSum += int64(i + 1)
for k := range gradingPlacements {
Expand Down Expand Up @@ -210,7 +216,7 @@ func (a *APIServer) getBalance(params interface{}) (*GenericResult, *Error) {
// Helpers

// getWinners returns the current 10 winners entry shorthashes from the last recorded block
func (a *APIServer) getWinners() [10]string {
func (a *APIServer) getWinners() []string {
height := getLeaderHeight()
currentOPRS := a.Grader.OprBlockByHeight(height)
record := currentOPRS.OPRs[0]
Expand Down
6 changes: 4 additions & 2 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"fmt"
"net/http"

"github.com/zpatrick/go-config"
"github.com/pegnet/pegnet/balances"

"github.com/pegnet/pegnet/mining"
"github.com/pegnet/pegnet/opr"
log "github.com/sirupsen/logrus"
Expand All @@ -22,9 +22,10 @@ type APIServer struct {
Grader *opr.QuickGrader
Balances *balances.BalanceTracker
Mux *http.ServeMux
config *config.Config
}

func NewApiServer(grader *opr.QuickGrader, balances *balances.BalanceTracker) *APIServer {
func NewApiServer(grader *opr.QuickGrader, balances *balances.BalanceTracker, config *config.Config) *APIServer {
s := new(APIServer)
s.Server = &http.Server{}
mux := http.NewServeMux()
Expand All @@ -33,6 +34,7 @@ func NewApiServer(grader *opr.QuickGrader, balances *balances.BalanceTracker) *A
s.Mux = mux
s.Grader = grader
s.Balances = balances
s.config = config

return s
}
Expand Down
30 changes: 20 additions & 10 deletions balances/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@ func ConvertAddress(address string) (prefix string, adr [32]byte, err error) {
return
}

func (b *BalanceTracker) AssetHumanReadable(prefix string) map[string]int64 {
b.Lock()
defer b.Unlock()
r := make(map[string]int64)
for k, v := range b.Balances[prefix] {
r[common.ConvertRawToFCT(k[:])] = v
}
return r
}

// AddToBalance adds the given value to the human-readable address
// Note that add to balance takes a signed update, so this can be used to add to or
// subtract from a balance. An error is returned if the value would drive the balance
Expand Down Expand Up @@ -88,3 +78,23 @@ func (b *BalanceTracker) GetBalance(address string) (balance int64) {
balance = b.Balances[prefix][adr]
return
}

// DiagnosticAssetHumanReadablePNTBalances is used to monitor the PNT distribution. It's mainly a diagnosing/debugging
// function.
func (b *BalanceTracker) DiagnosticAssetHumanReadablePNTBalances(prefix string) map[string]string {
b.Lock()
defer b.Unlock()
r := make(map[string]string)
total := int64(0)
for k, v := range b.Balances[prefix] {
r[common.ConvertRawToFCT(k[:])] = fmt.Sprintf("%d", v/1e8)
total += v / 1e8
}

for k, v := range b.Balances[prefix] {
r[common.ConvertRawToFCT(k[:])+"%"] = fmt.Sprintf("%.2f%%", float64(v/1e8)/float64(total)*100)
}

r["all"] = fmt.Sprintf("%d", total)
return r
}
9 changes: 7 additions & 2 deletions balances/burns.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func NewBurnTracking(balanceTracker *BalanceTracker) *BurnTracking {
}

func (b *BurnTracking) UpdateBurns(c *config.Config, startBlock int64) error {
return nil // Disable this for now, we are not using it
network, err := common.LoadConfigNetwork(c)
if err != nil {
panic("cannot find the network designation for updating burn txs")
Expand All @@ -32,7 +31,12 @@ func (b *BurnTracking) UpdateBurns(c *config.Config, startBlock int64) error {
b.FctDbht = startBlock
}

for i := b.FctDbht + 1; ; i++ {
heights, err := factom.GetHeights()
if err != nil {
return err
}

for i := b.FctDbht + 1; i < heights.DirectoryBlockHeight; i++ {
deltas := make(map[string]int64)

fc, _, err := factom.GetFBlockByHeight(i)
Expand Down Expand Up @@ -88,6 +92,7 @@ func (b *BurnTracking) UpdateBurns(c *config.Config, startBlock int64) error {
}
b.FctDbht = i
}
return nil
}

type FactoidTransaction struct {
Expand Down
4 changes: 4 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ var networkCoordinator = &cobra.Command{
"Remote miners therefore can directly and ONLY communicate with the coordinator.",
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithCancel(context.Background())
common.GlobalExitHandler.AddCancel(cancel)
ValidateConfig(Config) // Will fatal log if it fails

b := balances.NewBalanceTracker()
Expand All @@ -410,6 +411,7 @@ var networkMinerCmd = &cobra.Command{
Use: "netminer",
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithCancel(context.Background())
common.GlobalExitHandler.AddCancel(cancel)
ValidateConfig(Config) // Will fatal log if it fails

cl := networkMiner.NewMiningClient(Config)
Expand Down Expand Up @@ -457,6 +459,7 @@ var pegnetNode = &cobra.Command{
Short: "Runs a pegnet node",
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithCancel(context.Background())
common.GlobalExitHandler.AddCancel(cancel)
ValidateConfig(Config) // Will fatal log if it fails
b := balances.NewBalanceTracker()

Expand All @@ -468,6 +471,7 @@ var pegnetNode = &cobra.Command{
if err != nil {
CmdError(cmd, err)
}
common.GlobalExitHandler.AddExit(pegnetnode.Close)

go pegnetnode.Run(ctx)

Expand Down
18 changes: 16 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"context"
"fmt"
"os"
"os/signal"
"os/user"
"path/filepath"
"strings"

"github.com/pegnet/pegnet/balances"

"github.com/FactomProject/factom"
"github.com/pegnet/pegnet/common"
log "github.com/sirupsen/logrus"
Expand All @@ -21,7 +21,8 @@ import (
)

var (
Config *config.Config
Config *config.Config
ExitHandler *common.ExitHandler
// Global Flags
LogLevel string
FactomdLocation string
Expand Down Expand Up @@ -186,6 +187,19 @@ func rootPreRunSetup(cmd *cobra.Command, args []string) error {
go StartProfiler(p)
}

// Catch ctl+c
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
go func() {
<-signalChan
log.Info("Gracefully closing")
common.GlobalExitHandler.Close()

log.Info("closing application")
// If something is hanging, we have to kill it
os.Exit(0)
}()

return nil
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func LaunchGrader(config *config.Config, monitor *common.Monitor, balances *bala
if run {
go grader.Run(monitor, ctx)
}
common.GlobalExitHandler.AddExit(grader.Close)
return grader
}

Expand All @@ -77,7 +78,7 @@ func LaunchStatistics(config *config.Config, ctx context.Context) *mining.Global
}

func LaunchAPI(config *config.Config, stats *mining.GlobalStatTracker, grader *opr.QuickGrader, bals *balances.BalanceTracker, run bool) *api.APIServer {
s := api.NewApiServer(grader, bals)
s := api.NewApiServer(grader, bals, config)

if run {
apiport, err := config.Int(common.ConfigAPIPort)
Expand Down
30 changes: 30 additions & 0 deletions common/activation.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
package common

var (
// ActivationHeights signals the network is active and all miners can begin.
// This is the signal that pegnet has launched.
ActivationHeights = map[string]int64{
// roughly 17:00 UTC on Monday 8/19/2019
MainNetwork: 206422,
TestNetwork: 0,
}

// GradingHeights indicates the OPR version, which dictates the grading and OPR format.
// When we switch formats. The activation height indicates the block that grading changes.
// So if the grading change is on block 100, then the entries in block 100 will be using the
// new grading format.
GradingHeights = map[string]func(height int64) uint8{
MainNetwork: func(height int64) uint8 {
// Version 1 deprecates on block XXXXXX
// TODO: Set a real block height activate height
if height < 500000 {
return 1
}
return 2 // Latest code version
},
TestNetwork: func(height int64) uint8 {
if height < 0 {
return 1
}
return 2
},
}
)

// NetworkActive returns true if the network height is above the activation height.
Expand All @@ -17,3 +40,10 @@ func NetworkActive(network string, height int64) bool {
// Not a network we know of? Default to active.
return true
}

// OPRVersion returns the OPR version for a given height and network.
// If an OPR has a different version, it is invalid. The version dictates the grading
// algo to use and the OPR format.
func OPRVersion(network string, height int64) uint8 {
return GradingHeights[network](height)
}
28 changes: 28 additions & 0 deletions common/activation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package common_test

import (
"testing"

"github.com/pegnet/pegnet/common"
)

// Just checking around the hardcoded activation
func TestNetworkActive(t *testing.T) {
for i := int64(0); i < 100; i++ {
if common.NetworkActive(common.MainNetwork, i) {
t.Errorf("Mainnet is not active yet")
}
}

for i := int64(0); i < 100; i++ {
if common.NetworkActive(common.MainNetwork, 206421-i) {
t.Errorf("Mainnet is not active yet")
}
}

for i := int64(0); i < 100; i++ {
if !common.NetworkActive(common.MainNetwork, 206422+i) {
t.Errorf("Mainnet is active")
}
}
}
15 changes: 14 additions & 1 deletion common/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ var (
"DCR",
}

AllAssets = MergeLists(PNTAsset, CurrencyAssets, CommodityAssets, CryptoAssets)
AllAssets = MergeLists(PNTAsset, CurrencyAssets, CommodityAssets, CryptoAssets)
VersionOneAssets = AllAssets
// Version One, subtract 2 assets
VersionTwoAssets = SubtractFromSet(VersionOneAssets, "XPD", "XPT")
)

// AssetListContainsCaseInsensitive is for when using user input. It's helpful for the
Expand All @@ -115,6 +118,16 @@ func AssetListContains(assetList []string, asset string) bool {
return false
}

func SubtractFromSet(set []string, sub ...string) []string {
var result []string
for _, r := range set {
if !AssetListContains(sub, r) {
result = append(result, r)
}
}
return result
}

func MergeLists(assets ...[]string) []string {
acc := []string{}
for _, list := range assets {
Expand Down
29 changes: 29 additions & 0 deletions common/assets_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common_test

import (
"fmt"
"strings"
"testing"

Expand All @@ -18,3 +19,31 @@ func TestBasicAssetList(t *testing.T) {
}
}
}

func TestSetSubtract(t *testing.T) {
for _, asset := range VersionTwoAssets {
if asset == "XPD" || asset == "XPT" {
t.Errorf("contains %s when it should not", asset)
}
}

var set []string
for i := 0; i < 100; i++ {
set = append(set, fmt.Sprintf("%d", i))
}

for i := 0; i < 50; i += 2 {
first := fmt.Sprintf("%d", i)
last := fmt.Sprintf("%d", 100-i)
midish := fmt.Sprintf("%d", (100-i)/2)
newset := SubtractFromSet(set,
first, // Subtract from index off front
last, // Subtract from index off end
midish, // Subtract from middle-ish of first and last
)

if AssetListContains(newset, first) || AssetListContains(newset, last) || AssetListContains(newset, midish) {
t.Errorf("set subtract failed")
}
}
}
Loading

0 comments on commit 9b2069b

Please sign in to comment.