/
reward_db_key.go
90 lines (76 loc) · 3.28 KB
/
reward_db_key.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2021 The Alaya Network Authors
// This file is part of the Alaya-Go library.
//
// The Alaya-Go library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Alaya-Go library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Alaya-Go library. If not, see <http://www.gnu.org/licenses/>.
package reward
import (
"github.com/AlayaNetwork/Alaya-Go/x/xutil"
"github.com/AlayaNetwork/Alaya-Go/p2p/discover"
"github.com/AlayaNetwork/Alaya-Go/common"
)
const DelegateRewardPerLength = 1000
var (
HistoryIncreasePrefix = []byte("RewardHistory")
LastYearEndBalancePrefix = []byte("RewardBalance")
YearStartBlockNumberKey = []byte("YearStartBlockNumberKey")
YearStartTimeKey = []byte("YearStartTimeKey")
RemainingRewardKey = []byte("RemainingRewardKey")
NewBlockRewardKey = []byte("NewBlockRewardKey")
StakingRewardKey = []byte("StakingRewardKey")
ChainYearNumberKey = []byte("ChainYearNumberKey")
delegateRewardPerKey = []byte("DelegateRewardPerKey")
)
// GetHistoryIncreaseKey used for search the balance of reward pool at last year
func GetHistoryIncreaseKey(year uint32) []byte {
return append(HistoryIncreasePrefix, common.Uint32ToBytes(year)...)
}
//
func HistoryBalancePrefix(year uint32) []byte {
return append(LastYearEndBalancePrefix, common.Uint32ToBytes(year)...)
}
func DelegateRewardPerKey(nodeID discover.NodeID, stakingNum, epoch uint64) []byte {
index := uint32(epoch / DelegateRewardPerLength)
add, err := xutil.NodeId2Addr(nodeID)
if err != nil {
panic(err)
}
perKeyLength := len(delegateRewardPerKey)
lengthUint32, lengthUint64 := 4, 8
keyAdd := make([]byte, perKeyLength+common.AddressLength+lengthUint64+lengthUint32)
n := copy(keyAdd[:perKeyLength], delegateRewardPerKey)
n += copy(keyAdd[n:n+common.AddressLength], add.Bytes())
n += copy(keyAdd[n:n+lengthUint64], common.Uint64ToBytes(stakingNum))
copy(keyAdd[n:n+lengthUint32], common.Uint32ToBytes(index))
return keyAdd
}
func DelegateRewardPerKeys(nodeID discover.NodeID, stakingNum, fromEpoch, toEpoch uint64) [][]byte {
indexFrom := uint32(fromEpoch / DelegateRewardPerLength)
indexTo := uint32(toEpoch / DelegateRewardPerLength)
add, err := xutil.NodeId2Addr(nodeID)
if err != nil {
panic(err)
}
perKeyLength := len(delegateRewardPerKey)
lengthUint64 := 8
delegateRewardPerPrefix := make([]byte, perKeyLength+common.AddressLength+lengthUint64)
n := copy(delegateRewardPerPrefix[:perKeyLength], delegateRewardPerKey)
n += copy(delegateRewardPerPrefix[n:n+common.AddressLength], add.Bytes())
n += copy(delegateRewardPerPrefix[n:n+lengthUint64], common.Uint64ToBytes(stakingNum))
keys := make([][]byte, 0)
for i := indexFrom; i <= indexTo; i++ {
delegateRewardPerKey := append(delegateRewardPerPrefix[:], common.Uint32ToBytes(i)...)
keys = append(keys, delegateRewardPerKey)
}
return keys
}