/
migration105alpha.go
executable file
·198 lines (185 loc) · 5.94 KB
/
migration105alpha.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
191
192
193
194
195
196
197
198
/*
*
* 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 developers <dev@pallet.one>
* * @date 2018-2019
*
*/
package migration
import (
"github.com/ethereum/go-ethereum/rlp"
"github.com/palletone/go-palletone/common"
"github.com/palletone/go-palletone/common/crypto"
"github.com/palletone/go-palletone/common/log"
"github.com/palletone/go-palletone/common/ptndb"
"github.com/palletone/go-palletone/common/util"
"github.com/palletone/go-palletone/dag/constants"
"github.com/palletone/go-palletone/dag/errors"
"github.com/palletone/go-palletone/dag/modules"
"github.com/palletone/go-palletone/dag/storage"
)
type Migration104beta_105alpha struct {
dagdb ptndb.Database
idxdb ptndb.Database
utxodb ptndb.Database
statedb ptndb.Database
propdb ptndb.Database
}
func (m *Migration104beta_105alpha) FromVersion() string {
return "1.0.4-rc1"
}
func (m *Migration104beta_105alpha) ToVersion() string {
return "1.0.5-alpha"
}
func (m *Migration104beta_105alpha) ExecuteUpgrade() error {
//
if err := m.upgradeContractInfoInStatedb(); err != nil {
return err
}
return nil
}
func getprefix(db storage.DatabaseReader, prefix []byte) map[string][]byte {
iter := db.NewIteratorWithPrefix(prefix)
result := make(map[string][]byte)
for iter.Next() {
key := make([]byte, 0)
value := make([]byte, 0)
key = append(key, iter.Key()...)
result[string(key)] = append(value, iter.Value()...)
}
return result
}
func (m *Migration104beta_105alpha) upgradeContractInfoInStatedb() error {
// 状态数据库所有
rows := getprefix(m.statedb, constants.CONTRACT_PREFIX)
oldContracts := make([]*OldContract, 0, len(rows))
for _, v := range rows {
contract := &OldContract{}
rlp.DecodeBytes(v, contract)
oldContracts = append(oldContracts, contract)
}
log.Debugf("old contracts len = %d", len(oldContracts))
// 状态数据库
// 新的contracts
newContracts := make([]*NewContract, 0, len(rows))
for _, c := range oldContracts {
newContract := NewContract{
ContractId: c.ContractId,
TemplateId: c.TemplateId,
Name: c.Name,
Status: c.Status,
Creator: c.Creator,
CreationTime: c.CreationTime,
DuringTime: c.DuringTime,
Version: "",
}
newContracts = append(newContracts, &newContract)
}
log.Debugf("new contracts len = %d", len(newContracts))
// 新的contracts
// 通过用户合约的模板id获取模板并取得version
for i, _ := range newContracts {
if len(newContracts[i].TemplateId) != 0 {
key := append(constants.CONTRACT_TPL, newContracts[i].TemplateId...)
tpl := modules.ContractTemplate{}
err := storage.RetrieveFromRlpBytes(m.statedb, key, &tpl)
if err != nil {
return err
}
newContracts[i].Version = tpl.Version
}
}
// 通过用户合约的模板id获取模板并取得version
// 获取陪审员地址
contractNameAndJuryAddr := make(map[string][]common.Address, 0)
for _, c := range newContracts {
if len(c.TemplateId) != 0 {
key := append(constants.CONTRACT_JURY_PREFIX, c.ContractId...)
data, err := m.statedb.Get(key)
if err != nil {
log.Error(err.Error())
return err
}
if len(data) < 28 {
return errors.New("the data is irregular.")
}
verBytes := data[:28]
objData := data[28:]
version := &modules.StateVersion{}
version.SetBytes(verBytes)
jury := modules.ElectionNode{}
err = rlp.DecodeBytes(objData, &jury)
if err != nil {
log.Error(err.Error())
return err
}
for _, j := range jury.EleList {
ja := crypto.PubkeyBytesToAddress(j.PublicKey)
contractNameAndJuryAddr[c.Name] = append(contractNameAndJuryAddr[c.Name], ja)
}
}
}
log.Debugf("jury addresses len = %d", len(contractNameAndJuryAddr))
// 获取陪审员地址
// 保存新合约
for _, nc := range newContracts {
// 保存新的 contract
key := append(constants.CONTRACT_PREFIX, nc.ContractId...)
err := storage.StoreToRlpBytes(m.statedb, key, nc)
if err != nil {
return err
}
}
// 保存新合约
// 保存 jury 对应新合约
if len(contractNameAndJuryAddr) != 0 {
for _, nc := range newContracts {
// 保存对应的陪审员地址
for _, juryAddr := range contractNameAndJuryAddr[nc.Name] {
key1 := append(constants.CONTRACT_JURY_PREFIX, util.RlpHash(juryAddr).Bytes()...)
key2 := append(key1, nc.ContractId...)
log.Debugf("save contract id = %v with jury address = %s,key1 = %v", nc.ContractId, juryAddr.String(), key1)
// 保存陪审员对应的状态
err := storage.StoreToRlpBytes(m.statedb, key2, nc)
if err != nil {
return err
}
}
}
}
// 保存 jury 对应新合约
return nil
}
type OldContract struct {
// 根据用户创建合约实例申请的RequestId截取其后20字节生成
ContractId []byte
TemplateId []byte
Name string
Status byte // 合约状态
Creator []byte // address 20bytes
CreationTime uint64 // creation date
DuringTime uint64 //合约部署持续时间,单位秒
}
type NewContract struct {
// 根据用户创建合约实例申请的RequestId截取其后20字节生成
ContractId []byte
TemplateId []byte
Name string
Status byte // 合约状态
Creator []byte // address 20bytes
CreationTime uint64 // creation date
DuringTime uint64 //合约部署持续时间,单位秒
Version string
}