github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/consensus/ipbft/epoch/scheme.go (about)

     1  package epoch
     2  
     3  import (
     4  	"fmt"
     5  	tmTypes "github.com/intfoundation/intchain/consensus/ipbft/types"
     6  	"github.com/intfoundation/intchain/log"
     7  	dbm "github.com/intfoundation/go-db"
     8  	"github.com/intfoundation/go-wire"
     9  	"math/big"
    10  	"sync"
    11  )
    12  
    13  const rewardSchemeKey = "REWARDSCHEME"
    14  
    15  type RewardScheme struct {
    16  	mtx sync.Mutex
    17  	db  dbm.DB
    18  
    19  	TotalReward        *big.Int
    20  	RewardFirstYear    *big.Int
    21  	EpochNumberPerYear uint64
    22  	TotalYear          uint64
    23  }
    24  
    25  // Load Reward Scheme
    26  func LoadRewardScheme(db dbm.DB) *RewardScheme {
    27  	buf := db.Get([]byte(rewardSchemeKey))
    28  	if len(buf) == 0 {
    29  		return nil
    30  	} else {
    31  		rs := &RewardScheme{}
    32  		err := wire.ReadBinaryBytes(buf, rs)
    33  		if err != nil {
    34  			log.Errorf("LoadRewardScheme Failed, error: %v", err)
    35  			return nil
    36  		}
    37  		return rs
    38  	}
    39  }
    40  
    41  // Convert Reward Scheme from json to struct
    42  func MakeRewardScheme(db dbm.DB, rsDoc *tmTypes.RewardSchemeDoc) *RewardScheme {
    43  
    44  	rs := &RewardScheme{
    45  		db:                 db,
    46  		TotalReward:        rsDoc.TotalReward,
    47  		RewardFirstYear:    rsDoc.RewardFirstYear,
    48  		EpochNumberPerYear: rsDoc.EpochNumberPerYear,
    49  		TotalYear:          rsDoc.TotalYear,
    50  	}
    51  
    52  	return rs
    53  }
    54  
    55  // Save the Reward Scheme to DB
    56  func (rs *RewardScheme) Save() {
    57  	rs.mtx.Lock()
    58  	defer rs.mtx.Unlock()
    59  	rs.db.SetSync([]byte(rewardSchemeKey), wire.BinaryBytes(*rs))
    60  }
    61  
    62  func (rs *RewardScheme) String() string {
    63  
    64  	return fmt.Sprintf("RewardScheme : {"+
    65  		"totalReward : %v,\n"+
    66  		"rewardFirstYear : %v,\n"+
    67  		"epochNumberPerYear : %v,\n"+
    68  		"}",
    69  		rs.TotalReward,
    70  		rs.RewardFirstYear,
    71  		rs.EpochNumberPerYear)
    72  }