Skip to content

Commit

Permalink
fix: yileds calculations because of wrong getting from db
Browse files Browse the repository at this point in the history
  • Loading branch information
kstdl committed Jul 29, 2023
1 parent 7eed825 commit d36de65
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 164 deletions.
42 changes: 42 additions & 0 deletions internal/chain/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package chain

import (
"github.com/Taraxa-project/taraxa-indexer/internal/common"
)

type initialValidator struct {
Address string `json:"address"`
Delegations map[string]string `json:"delegations"`
}

type DposConfig struct {
BlocksPerYear string `json:"blocks_per_year"`
DagProposersReward string `json:"dag_proposers_reward"`
MaxBlockAuthorReward string `json:"max_block_author_reward"`
EligibilityBalanceThreshold string `json:"eligibility_balance_threshold"`
YieldPercentage string `json:"yield_percentage"`
InitialValidators []initialValidator `json:"initial_validators"`
}

type PbftConfig struct {
CommitteeSize string `json:"committee_size"`
LambdaMs string `json:"lambda_ms"`
}

type GenesisObject struct {
DagGenesisBlock DagBlock `json:"dag_genesis_block"`
InitialBalances map[string]string `json:"initial_balances"`
Pbft PbftConfig `json:"pbft"`
Dpos DposConfig `json:"dpos"`
}

func (g *GenesisObject) ToChainConfig() (c *common.ChainConfig) {
c = new(common.ChainConfig)
c.CommitteeSize = common.ParseStringToBigInt(g.Pbft.CommitteeSize)
c.BlocksPerYear = common.ParseStringToBigInt(g.Dpos.BlocksPerYear)
c.YieldPercentage = common.ParseStringToBigInt(g.Dpos.YieldPercentage)
c.DagProposersReward = common.ParseStringToBigInt(g.Dpos.DagProposersReward)
c.MaxBlockAuthorReward = common.ParseStringToBigInt(g.Dpos.MaxBlockAuthorReward)
c.EligibilityBalanceThreshold = common.ParseStringToBigInt(g.Dpos.EligibilityBalanceThreshold)
return
}
42 changes: 0 additions & 42 deletions internal/chain/genesis.go

This file was deleted.

63 changes: 13 additions & 50 deletions internal/chain/types.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,12 @@
package chain

import (
"log"
"math/big"
"runtime/debug"
"strconv"

"github.com/Taraxa-project/taraxa-indexer/internal/common"
"github.com/Taraxa-project/taraxa-indexer/models"
)

func ParseUInt(s string) (v uint64) {
if len(s) == 0 {
return
}
v, err := strconv.ParseUint(s, 0, 64)
if err != nil {
debug.PrintStack()
log.Fatal(s, "ParseUInt ", err)
}
return v
}

func ParseInt(s string) (v int64) {
if len(s) == 0 {
return
}
v, err := strconv.ParseInt(s, 0, 64)
if err != nil {
debug.PrintStack()
log.Fatal(s, "ParseUInt ", err)
}
return v
}

func parseBool(s string) (v bool) {
if len(s) == 0 {
return
}
i, err := strconv.ParseUint(s, 0, 64)
if err != nil {
log.Fatal("parseBool ", v)
}
return i > 0
}

type Block struct {
models.Pbft
Number string `json:"number"`
Expand All @@ -54,8 +17,8 @@ type Block struct {

func (b *Block) ToModel() (pbft *models.Pbft) {
pbft = &b.Pbft
pbft.Timestamp = ParseUInt(b.Timestamp)
pbft.Number = ParseUInt(b.Number)
pbft.Timestamp = common.ParseUInt(b.Timestamp)
pbft.Number = common.ParseUInt(b.Number)
pbft.TransactionCount = uint64(len(b.Transactions))

return
Expand All @@ -70,8 +33,8 @@ type DagBlock struct {

func (b *DagBlock) ToModel() (dag *models.Dag) {
dag = &b.Dag
dag.Timestamp = ParseUInt(b.Timestamp)
dag.Level = ParseUInt(b.Level)
dag.Timestamp = common.ParseUInt(b.Timestamp)
dag.Level = common.ParseUInt(b.Level)
dag.TransactionCount = uint64(len(b.Transactions))

return
Expand Down Expand Up @@ -119,12 +82,12 @@ func GetTransactionType(to, input string, internal bool) models.TransactionType

func (t *Transaction) ToModelWithTimestamp(timestamp uint64) (trx models.Transaction) {
trx = t.Transaction
trx.BlockNumber = ParseUInt(t.BlockNumber)
trx.Nonce = ParseUInt(t.Nonce)
trx.GasPrice = ParseUInt(t.GasPrice)
trx.GasUsed = ParseUInt(t.GasUsed)
trx.TransactionIndex = ParseUInt(t.TransactionIndex)
trx.Status = parseBool(t.Status)
trx.BlockNumber = common.ParseUInt(t.BlockNumber)
trx.Nonce = common.ParseUInt(t.Nonce)
trx.GasPrice = common.ParseUInt(t.GasPrice)
trx.GasUsed = common.ParseUInt(t.GasUsed)
trx.TransactionIndex = common.ParseUInt(t.TransactionIndex)
trx.Status = common.ParseBool(t.Status)
trx.Type = GetTransactionType(trx.To, t.Input, false)
if trx.Type == models.ContractCreation {
trx.To = t.ContractAddress
Expand All @@ -145,13 +108,13 @@ func (t *Transaction) ExtractLogs() (logs []models.EventLog) {
eLog := models.EventLog{
Address: log.Address,
Data: log.Data,
LogIndex: ParseUInt(log.LogIndex),
LogIndex: common.ParseUInt(log.LogIndex),
Name: "",
Params: []string{},
Removed: log.Removed,
Topics: log.Topics,
TransactionHash: log.TransactionHash,
TransactionIndex: ParseUInt(log.TransactionIndex),
TransactionIndex: common.ParseUInt(log.TransactionIndex),
}
logs = append(logs, eLog)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/chain/ws_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (client *WsClient) GetLatestPeriod() (uint64, error) {
if err != nil {
return 0, err
}
return ParseUInt(blk.Number), err
return common.ParseUInt(blk.Number), err
}

func (client *WsClient) TraceBlockTransactions(number uint64) (traces []TransactionTrace, err error) {
Expand Down
29 changes: 19 additions & 10 deletions internal/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ import (
)

type ChainConfig struct {
CommitteeSize *big.Int
BlocksPerYear *big.Int
YieldPercentage *big.Int
DagProposersReward *big.Int
MaxBlockAuthorReward *big.Int
CommitteeSize *big.Int
BlocksPerYear *big.Int
YieldPercentage *big.Int
DagProposersReward *big.Int
MaxBlockAuthorReward *big.Int
EligibilityBalanceThreshold *big.Int
}

func DefaultChainConfig() *ChainConfig {
return &ChainConfig{
CommitteeSize: big.NewInt(1000),
BlocksPerYear: big.NewInt(365 * 24 * 60 * 15),
YieldPercentage: big.NewInt(20),
DagProposersReward: big.NewInt(50),
MaxBlockAuthorReward: big.NewInt(10),
CommitteeSize: big.NewInt(1000),
BlocksPerYear: big.NewInt(365 * 24 * 60 * 15),
YieldPercentage: big.NewInt(20),
DagProposersReward: big.NewInt(50),
MaxBlockAuthorReward: big.NewInt(10),
EligibilityBalanceThreshold: ParseStringToBigInt("0x69E10DE76676D0800000"),
}
}

Expand All @@ -28,6 +30,13 @@ type Config struct {
ValidatorsYieldSavingInterval uint64
}

func (c *Config) IsEligible(stake *big.Int) bool {
if c.Chain != nil && c.Chain.EligibilityBalanceThreshold != nil && stake.Cmp(c.Chain.EligibilityBalanceThreshold) >= 0 {
return true
}
return false
}

func DefaultConfig() *Config {
return &Config{
Chain: DefaultChainConfig(),
Expand Down
38 changes: 38 additions & 0 deletions internal/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package common

import (
"fmt"
"log"
"math/big"
"reflect"
"runtime"
"runtime/debug"
"strconv"
"strings"

Expand All @@ -19,6 +21,42 @@ func MakeThreadPool() pool.Pool {
return pool.New(uint(runtime.NumCPU()))
}

func ParseUInt(s string) (v uint64) {
if len(s) == 0 {
return
}
v, err := strconv.ParseUint(s, 0, 64)
if err != nil {
debug.PrintStack()
log.Fatal(s, "ParseUInt ", err)
}
return v
}

func ParseInt(s string) (v int64) {
if len(s) == 0 {
return
}
v, err := strconv.ParseInt(s, 0, 64)
if err != nil {
debug.PrintStack()
log.Fatal(s, "ParseUInt ", err)
}
return v
}

func ParseBool(s string) (v bool) {
if len(s) == 0 {
return
}
i, err := strconv.ParseUint(s, 0, 64)
if err != nil {
debug.PrintStack()
log.Fatal("parseBool ", v)
}
return i > 0
}

func ParseStringToBigInt(v string) *big.Int {
a := big.NewInt(0)
a.SetString(v, 0)
Expand Down
2 changes: 1 addition & 1 deletion internal/indexer/block_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ func (bc *blockContext) process(raw chain.Block) (dags_count, trx_count uint64,
return
}

r := rewards.MakeRewards(bc.Storage, bc.Batch, bc.Config, bc.block.Number, bc.block.Author)
total_minted := common.ParseStringToBigInt(raw.TotalReward)
bc.balances.AddToBalance(common.DposContractAddress, total_minted)

Expand All @@ -85,6 +84,7 @@ func (bc *blockContext) process(raw chain.Block) (dags_count, trx_count uint64,
}
bc.Batch.SaveAccounts(bc.balances)

r := rewards.MakeRewards(bc.Storage, bc.Batch, bc.Config, bc.block.Number, bc.block.Author)
if total_minted.Cmp(big.NewInt(0)) == 1 {
r.Process(total_minted, bc.dags, bc.transactions, *votes, validators)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/indexer/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (g *Genesis) makeInitBalanceTrx(addr, value string) (trx models.Transaction
trx.To = addr
trx.Value = value
trx.BlockNumber = 0
trx.Timestamp = chain.ParseUInt(g.genesis.DagGenesisBlock.Timestamp)
trx.Timestamp = common.ParseUInt(g.genesis.DagGenesisBlock.Timestamp)
trx.Status = true
return
}
Expand Down
2 changes: 1 addition & 1 deletion internal/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (i *Indexer) run() error {
case err := <-sub.Err():
return err
case blk := <-ch:
p := chain.ParseUInt(blk.Number)
p := common.ParseUInt(blk.Number)
finalized_period := i.storage.GetFinalizationData().PbftCount
if p != finalized_period+1 {
err := i.sync()
Expand Down
7 changes: 5 additions & 2 deletions internal/indexer/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ func (bc *blockContext) processInternalTransactions(trace chain.TransactionTrace
internal_transactions.Data = append(internal_transactions.Data, internal)

bc.SaveTransaction(internal)
bc.balances.UpdateBalances(internal.From, internal.To, internal.Value)
// TODO: hotfix, remove after fix in taraxa-node
if entry.Action.CallType != "delegatecall" {
bc.balances.UpdateBalances(internal.From, internal.To, internal.Value)
}
}
return
}
Expand Down Expand Up @@ -112,7 +115,7 @@ func makeInternal(trx models.Transaction, entry chain.TraceEntry) (internal mode
internal.From = entry.Action.From
internal.To = entry.Action.To
internal.Value = entry.Action.Value
internal.GasUsed = chain.ParseUInt(entry.Result.GasUsed)
internal.GasUsed = common.ParseUInt(entry.Result.GasUsed)
internal.Type = chain.GetTransactionType(trx.To, entry.Action.Input, true)
internal.BlockNumber = trx.BlockNumber
return
Expand Down
Loading

0 comments on commit d36de65

Please sign in to comment.