Skip to content

Commit

Permalink
keep consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonheaven committed Feb 2, 2021
1 parent 97d2b91 commit 61198bb
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 10 deletions.
60 changes: 60 additions & 0 deletions node/pegnet/txhistory.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,63 @@ func (p *Pegnet) InsertDeveloperRewardCoinbase(tx *sql.Tx, txid string, addTxid

return nil
}

// Special construction to nullify burn address
func (p *Pegnet) InsertZeroingCoinbase(tx *sql.Tx, txid string, addTxid string, height uint32, heightTimestamp time.Time, payout uint64, asset string, faAdd factom.FAAddress) error {
txidBytes, err := hex.DecodeString(txid)
if err != nil {
return err
}

// First we need to record the batch. The batch is the entire set of transactions, where
// each tx is a stake payout.
stmt, err := tx.Prepare(`INSERT INTO "pn_history_txbatch"
(entry_hash, height, blockorder, timestamp, executed) VALUES
(?, ?, ?, ?, ?)`)
if err != nil {
return err
}

// The Entryhash is the custom txid, it is not an actual entry on chain
// The executed height is the same height as the recorded.
_, err = stmt.Exec(txidBytes, height, 0, heightTimestamp.Unix(), height)
if err != nil {
return err
}

// Now we record balance zeroing .

// All addresses are stored as bytes in the sqlitedb
add := faAdd[:]
// index for the address
index, _, err := SplitTxID(addTxid)
if err != nil {
return err
}

// Decrease each balance as a coinbase.
// Insert the TX
coinbaseStatement, err := tx.Prepare(`INSERT INTO "pn_history_transaction"
(entry_hash, tx_index, action_type, from_address, from_asset, from_amount, to_asset, to_amount, outputs) VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?)`)
if err != nil {
return err
}

_, err = coinbaseStatement.Exec(txidBytes, index, Coinbase, add, "", 0, asset, -payout, "") // -payout means we substract value
if err != nil {
return err
}

// Insert into lookup table
lookup, err := tx.Prepare(insertLookupQuery)
if err != nil {
return err
}

if _, err = lookup.Exec(txidBytes, index, add); err != nil {
return err
}

return nil
}
48 changes: 38 additions & 10 deletions node/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ func (d *Pegnetd) NullifyBurnAddress(ctx context.Context, tx *sql.Tx, height uin
}

i := 0 // value to keep witin 0-9 range for mock tx
j := 50 // value for uniqueness
j := 0 // value for uniqueness
if height >= V202EnhanceActivation {
j = 50
}
for ticker := fat2.PTickerInvalid + 1; ticker < fat2.PTickerMax; ticker++ {

// Substract from every issuance
Expand All @@ -219,7 +222,10 @@ func (d *Pegnetd) NullifyBurnAddress(ctx context.Context, tx *sql.Tx, height uin
// We need to mock a TXID to record nullify recrods
// add more uniqness into hash value by reusing iterating j value in addtion to current height
// so it doesn't overlap with staking and rewards we have in place
txid = fmt.Sprintf("%03d%061d", j, height)
txid = fmt.Sprintf("%064d", height-(uint32(j)))
if height >= V202EnhanceActivation {
txid = fmt.Sprintf("%03d%061d", j, height)
}

// Mock entry hash value
addTxid := fmt.Sprintf("%d-%s", i, txid)
Expand All @@ -234,6 +240,16 @@ func (d *Pegnetd) NullifyBurnAddress(ctx context.Context, tx *sql.Tx, height uin
"txid": txid,
"addtxid": addTxid,
}).Info("burn nullify | prep")

if height < V202EnhanceActivation {
err = d.Pegnet.InsertZeroingCoinbase(tx, txid, addTxid, height, heightTimestamp, value, ticker.String(), FAGlobalBurnAddress)
if err != nil {
fLog.WithFields(log.Fields{
"error": err,
}).Info("zeroing burn | coinbase tx failed")
return err
}
}
}

return nil
Expand Down Expand Up @@ -497,7 +513,7 @@ func (d *Pegnetd) SnapshotPayouts(tx *sql.Tx, fLog *log.Entry, rates map[fat2.PT
if bal.Balances[i] == 0 { // Ignore 0 balances
continue
}
if (rates[i] == 0 || rates[fat2.PTickerUSD] == 0) && height > V202EnhanceActivation {
if (rates[i] == 0 || rates[fat2.PTickerUSD] == 0) && height >= V202EnhanceActivation {
continue
}

Expand Down Expand Up @@ -616,7 +632,10 @@ func (d *Pegnetd) DevelopersPayouts(tx *sql.Tx, fLog *log.Entry, height uint32,
txid = fmt.Sprintf("%02d%062d", j, height)

// we calculate developers reward from % pre-defined
rewardPayout := uint64((conversions.PerBlockDevelopers / 100) * dev.DevRewardPct * pegnet.SnapshotRate)
rewardPayout := uint64((conversions.PerBlockDevelopers / 100) * dev.DevRewardPct)
if height >= V202EnhanceActivation {
rewardPayout = uint64((conversions.PerBlockDevelopers / 100) * dev.DevRewardPct * pegnet.SnapshotRate)
}
addr, err := factom.NewFAAddress(dev.DevAddress)

_, err = d.Pegnet.AddToBalance(tx, &addr, fat2.PTickerPEG, rewardPayout)
Expand Down Expand Up @@ -1294,19 +1313,28 @@ func (d *Pegnetd) GetAssetRates(oprWinners []opr.AssetUint, sprWinners []opr.Ass
if oprWinners[i].Name == sprWinners[i].Name {
sprRate := sprWinners[i].Value
oprRate := oprWinners[i].Value
toleranceRate := 0.25 // 25% band
toleranceRate := 0.1 // 10% band
if height >= V202EnhanceActivation {
toleranceRate = 0.25 // 25% band
}
toleranceBandHigh := float64(sprRate) * (1 + toleranceRate)
toleranceBandLow := float64(sprRate) * (1 - toleranceRate)
if (float64(oprRate) >= toleranceBandLow) && (float64(oprRate) <= toleranceBandHigh) {
filteredRates = append(filteredRates, oprWinners[i])
} else {
fmt.Println("Rate difference", oprWinners[i].Name, oprWinners[i].Value, sprWinners[i].Value)
if height < V202EnhanceActivation {
if height >= V202EnhanceActivation {
fmt.Println("Rate difference", oprWinners[i].Name, oprWinners[i].Value, sprWinners[i].Value)
diffWinner := sprWinners[i]
diffWinner.Value = 0
filteredRates = append(filteredRates, diffWinner)
} else {
fmt.Println("opr is out side of spr's tolerance band")

fmt.Println("=== asset name:", oprWinners[i].Name)
fmt.Println("<<<<=== opr rate:", oprWinners[i].Value)
fmt.Println("<<<<=== spr rate:", sprWinners[i].Value)
return nil, fmt.Errorf("opr is out side of tolerance band")
}
diffWinner := sprWinners[i]
diffWinner.Value = 0
filteredRates = append(filteredRates, diffWinner)
}
}
}
Expand Down

0 comments on commit 61198bb

Please sign in to comment.