/
propertydb.go
executable file
·190 lines (159 loc) · 6.32 KB
/
propertydb.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
This file is part of go-palletone.
go-palletone is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
go-palletone 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with go-palletone. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* @author PalletOne core developer Albert·Gou <dev@pallet.one>
* @date 2018
*
*/
package storage
import (
"reflect"
"github.com/palletone/go-palletone/common/log"
"github.com/palletone/go-palletone/common/ptndb"
"github.com/palletone/go-palletone/core"
"github.com/palletone/go-palletone/dag/constants"
"github.com/palletone/go-palletone/dag/modules"
)
type PropertyDb struct {
db ptndb.Database
}
type IPropertyDb interface {
StoreGlobalProp(gp *modules.GlobalProperty) error
RetrieveGlobalProp() (*modules.GlobalProperty, error)
StoreDynGlobalProp(dgp *modules.DynamicGlobalProperty) error
RetrieveDynGlobalProp() (*modules.DynamicGlobalProperty, error)
StoreMediatorSchl(ms *modules.MediatorSchedule) error
RetrieveMediatorSchl() (*modules.MediatorSchedule, error)
//StoreGlobalPropHistory(gp *modules.GlobalPropertyHistory) error
//RetrieveGlobalPropHistories() ([]*modules.GlobalPropertyHistory, error)
SetNewestUnit(header *modules.Header) error
//GetNewestUnit(token modules.AssetId) (common.Hash, *modules.ChainIndex, int64, error)
GetNewestUnit(token modules.AssetId) (*modules.UnitProperty, error)
GetChainParameters() *core.ChainParameters
}
func (propdb *PropertyDb) GetChainParameters() *core.ChainParameters {
gp, err := propdb.RetrieveGlobalProp()
if err != nil {
return nil
}
return &gp.ChainParameters
}
// modified by Yiran
// initialize PropertyDB , and retrieve gp,dgp,mc from IPropertyDb.
func NewPropertyDb(db ptndb.Database) *PropertyDb {
pdb := &PropertyDb{db: db}
return pdb
}
func (propdb *PropertyDb) StoreMediatorSchl(ms *modules.MediatorSchedule) error {
err := StoreToRlpBytes(propdb.db, constants.MEDIATOR_SCHEDULE_KEY, ms)
if err != nil {
log.Errorf("Store mediator schedule error: %v", err.Error())
}
return err
}
func (propdb *PropertyDb) StoreDynGlobalProp(dgp *modules.DynamicGlobalProperty) error {
log.Debugf("DB[%s] Save dynamic global property to db.", reflect.TypeOf(propdb.db).String())
err := StoreToRlpBytes(propdb.db, constants.DYNAMIC_GLOBALPROPERTY_KEY, dgp)
if err != nil {
log.Debugf("Store dynamic global properties error: %v", err.Error())
}
return err
}
func (propdb *PropertyDb) StoreGlobalProp(gp *modules.GlobalProperty) error {
log.Debugf("DB[%s] Save global property to db.", reflect.TypeOf(propdb.db).String())
err := StoreToRlpBytes(propdb.db, constants.GLOBALPROPERTY_KEY, gp)
if err != nil {
log.Debugf("Store global properties error: %v", err.Error())
}
return err
}
func (propdb *PropertyDb) RetrieveGlobalProp() (*modules.GlobalProperty, error) {
gp := modules.NewGlobalProp()
err := RetrieveFromRlpBytes(propdb.db, constants.GLOBALPROPERTY_KEY, gp)
if err != nil {
log.Errorf("Retrieve global properties error: %v", err.Error())
}
return gp, err
}
func (propdb *PropertyDb) RetrieveDynGlobalProp() (*modules.DynamicGlobalProperty, error) {
dgp := modules.NewDynGlobalProp()
err := RetrieveFromRlpBytes(propdb.db, constants.DYNAMIC_GLOBALPROPERTY_KEY, dgp)
if err != nil {
log.Errorf("Retrieve dynamic global properties error: %v", err.Error())
}
return dgp, err
}
func (propdb *PropertyDb) RetrieveMediatorSchl() (*modules.MediatorSchedule, error) {
ms := modules.NewMediatorSchl()
err := RetrieveFromRlpBytes(propdb.db, constants.MEDIATOR_SCHEDULE_KEY, ms)
if err != nil {
log.Errorf("Retrieve mediator schedule error: %v", err.Error())
}
return ms, err
}
//func makeGlobalPropHistoryKey(gp *modules.GlobalPropertyHistory) []byte {
// b := make([]byte, 8)
// binary.LittleEndian.PutUint64(b, gp.EffectiveTime)
// return append(constants.GLOBAL_PROPERTY_HISTORY_PREFIX, b...)
//}
//
//func (propdb *PropertyDb) StoreGlobalPropHistory(gp *modules.GlobalPropertyHistory) error {
// log.Debugf("DB[%s] Save global property history to db.", reflect.TypeOf(propdb.db).String())
// key := makeGlobalPropHistoryKey(gp)
// err := StoreToRlpBytes(propdb.db, key, gp)
// if err != nil {
// log.Debugf("Store global properties history error: %v", err.Error())
// }
//
// return err
//}
//
//func (propdb *PropertyDb) RetrieveGlobalPropHistories() ([]*modules.GlobalPropertyHistory, error) {
// kv := getprefix(propdb.db, constants.GLOBAL_PROPERTY_HISTORY_PREFIX)
// result := make([]*modules.GlobalPropertyHistory, 0)
// for _, v := range kv {
// gp := &modules.GlobalPropertyHistory{}
// rlp.DecodeBytes(v, gp)
// result = append(result, gp)
// }
// return result, nil
//}
func (db *PropertyDb) SetNewestUnit(header *modules.Header) error {
hash := header.Hash()
index := header.GetNumber()
timestamp := uint32(header.Timestamp())
data := &modules.UnitProperty{Hash: hash, ChainIndex: index, Timestamp: timestamp}
//key := append(constants.LastUnitInfo, index.AssetID.Bytes()...)
log.Debugf("DB[%s]Save newest unit %s,index:%s", reflect.TypeOf(db.db).String(), hash.String(), index.String())
//return StoreToRlpBytes(db.db, key, data)
return StoreToRlpBytes(db.db, getNewestUnit(index.AssetID), data)
}
func getNewestUnit(asset modules.AssetId) []byte {
return append(constants.LastUnitInfo, asset.Bytes()...)
}
//func (db *PropertyDb) GetNewestUnit(asset modules.AssetId) (common.Hash, *modules.ChainIndex, int64, error) {
func (db *PropertyDb) GetNewestUnit(asset modules.AssetId) (*modules.UnitProperty, error) {
data := &modules.UnitProperty{}
//key := append(constants.LastUnitInfo, asset.Bytes()...)
//err := RetrieveFromRlpBytes(db.db, key, data)
err := RetrieveFromRlpBytes(db.db, getNewestUnit(asset), data)
if err != nil {
//return common.Hash{}, nil, 0, err
return nil, err
}
log.Debugf("DB[%s] GetNewestUnit: %s,Index:%s,timestamp:%d", reflect.TypeOf(db.db).String(),
data.Hash.String(), data.ChainIndex.String(), data.Timestamp)
//return data.Hash, data.Index, int64(data.Timestamp), nil
return data, nil
}