github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/consensus/neatcon/epoch/scheme.go (about) 1 package epoch 2 3 import ( 4 "fmt" 5 "math/big" 6 "sync" 7 8 ncTypes "github.com/neatlab/neatio/chain/consensus/neatcon/types" 9 "github.com/neatlab/neatio/chain/log" 10 dbm "github.com/neatlib/db-go" 11 "github.com/neatlib/wire-go" 12 ) 13 14 const rewardSchemeKey = "REWARDSCHEME" 15 16 type RewardScheme struct { 17 mtx sync.Mutex 18 db dbm.DB 19 20 TotalReward *big.Int 21 RewardFirstYear *big.Int 22 EpochNumberPerYear uint64 23 TotalMintingYears uint64 24 } 25 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 func MakeRewardScheme(db dbm.DB, rsDoc *ncTypes.RewardSchemeDoc) *RewardScheme { 42 43 rs := &RewardScheme{ 44 db: db, 45 TotalReward: rsDoc.TotalReward, 46 RewardFirstYear: rsDoc.RewardFirstYear, 47 EpochNumberPerYear: rsDoc.EpochNumberPerYear, 48 TotalMintingYears: rsDoc.TotalMintingYears, 49 } 50 51 return rs 52 } 53 54 func (rs *RewardScheme) Save() { 55 rs.mtx.Lock() 56 defer rs.mtx.Unlock() 57 rs.db.SetSync([]byte(rewardSchemeKey), wire.BinaryBytes(*rs)) 58 } 59 60 func (rs *RewardScheme) String() string { 61 62 return fmt.Sprintf("RewardScheme : {"+ 63 "totalReward : %v,\n"+ 64 "rewardFirstYear : %v,\n"+ 65 "epochNumberPerYear : %v,\n"+ 66 "}", 67 rs.TotalReward, 68 rs.RewardFirstYear, 69 rs.EpochNumberPerYear) 70 }