forked from Fantom-foundation/Substate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
destroyed_account_db.go
162 lines (141 loc) · 4.94 KB
/
destroyed_account_db.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
package substate
import (
"encoding/binary"
"fmt"
"log"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/rlp"
)
type DestroyedAccountDB struct {
backend BackendDatabase
}
func NewDestroyedAccountDB(backend BackendDatabase) *DestroyedAccountDB {
return &DestroyedAccountDB{backend: backend}
}
func OpenDestroyedAccountDB(destroyedAccountDir string) (*DestroyedAccountDB, error) {
return openDestroyedAccountDB(destroyedAccountDir, false)
}
func OpenDestroyedAccountDBReadOnly(destroyedAccountDir string) (*DestroyedAccountDB, error) {
return openDestroyedAccountDB(destroyedAccountDir, true)
}
func openDestroyedAccountDB(destroyedAccountDir string, readOnly bool) (*DestroyedAccountDB, error) {
log.Println("substate: OpenDestroyedAccountDB")
backend, err := rawdb.NewLevelDBDatabase(destroyedAccountDir, 1024, 100, "destroyed_accounts", readOnly)
if err != nil {
return nil, fmt.Errorf("error opening deletion-db %s: %v", destroyedAccountDir, err)
}
return NewDestroyedAccountDB(backend), nil
}
func (db *DestroyedAccountDB) Close() error {
return db.backend.Close()
}
type SuicidedAccountLists struct {
DestroyedAccounts []common.Address
ResurrectedAccounts []common.Address
}
func (db *DestroyedAccountDB) SetDestroyedAccounts(block uint64, tx int, des []common.Address, res []common.Address) error {
accountList := SuicidedAccountLists{DestroyedAccounts: des, ResurrectedAccounts: res}
value, err := rlp.EncodeToBytes(accountList)
if err != nil {
panic(err)
}
return db.backend.Put(encodeDestroyedAccountKey(block, tx), value)
}
func (db *DestroyedAccountDB) GetDestroyedAccounts(block uint64, tx int) ([]common.Address, []common.Address, error) {
data, err := db.backend.Get(encodeDestroyedAccountKey(block, tx))
if err != nil {
return nil, nil, err
}
list, err := DecodeAddressList(data)
return list.DestroyedAccounts, list.ResurrectedAccounts, err
}
// GetAccountsDestroyedInRange get list of all accounts between block from and to (including from and to).
func (db *DestroyedAccountDB) GetAccountsDestroyedInRange(from, to uint64) ([]common.Address, error) {
startingBlockBytes := make([]byte, 8)
binary.BigEndian.PutUint64(startingBlockBytes, from)
iter := db.backend.NewIterator([]byte(DestroyedAccountPrefix), startingBlockBytes)
defer iter.Release()
isDestroyed := make(map[common.Address]bool)
for iter.Next() {
block, _, err := DecodeDestroyedAccountKey(iter.Key())
if err != nil {
return nil, err
}
if block > to {
break
}
list, err := DecodeAddressList(iter.Value())
if err != nil {
return nil, err
}
for _, addr := range list.DestroyedAccounts {
isDestroyed[addr] = true
}
for _, addr := range list.ResurrectedAccounts {
isDestroyed[addr] = false
}
}
accountList := []common.Address{}
for addr, isDeleted := range isDestroyed {
if isDeleted {
accountList = append(accountList, addr)
}
}
return accountList, nil
}
const (
DestroyedAccountPrefix = "da" // DestroyedAccountPrefix + block (64-bit) -> SuicidedAccountLists
)
func encodeDestroyedAccountKey(block uint64, tx int) []byte {
prefix := []byte(DestroyedAccountPrefix)
key := make([]byte, len(prefix)+12)
copy(key[0:], prefix)
binary.BigEndian.PutUint64(key[len(prefix):], block)
binary.BigEndian.PutUint32(key[len(prefix)+8:], uint32(tx))
return key
}
func DecodeDestroyedAccountKey(data []byte) (uint64, int, error) {
if len(data) != len(DestroyedAccountPrefix)+12 {
return 0, 0, fmt.Errorf("invalid length of destroyed account key, expected %d, got %d", len(DestroyedAccountPrefix)+12, len(data))
}
if string(data[0:len(DestroyedAccountPrefix)]) != DestroyedAccountPrefix {
return 0, 0, fmt.Errorf("invalid prefix of destroyed account key")
}
block := binary.BigEndian.Uint64(data[len(DestroyedAccountPrefix):])
tx := binary.BigEndian.Uint32(data[len(DestroyedAccountPrefix)+8:])
return block, int(tx), nil
}
func DecodeAddressList(data []byte) (SuicidedAccountLists, error) {
list := SuicidedAccountLists{}
err := rlp.DecodeBytes(data, &list)
return list, err
}
// GetFirstKey returns the first block number in the database.
func (db *DestroyedAccountDB) GetFirstKey() (uint64, error) {
iter := db.backend.NewIterator([]byte(DestroyedAccountPrefix), nil)
defer iter.Release()
for iter.Next() {
firstBlock, _, err := DecodeDestroyedAccountKey(iter.Key())
if err != nil {
return 0, fmt.Errorf("cannot decode updateset key; %v", err)
}
return firstBlock, nil
}
return 0, fmt.Errorf("no updateset found")
}
// GetLastKey returns the last block number in the database.
// if not found then returns 0
func (db *DestroyedAccountDB) GetLastKey() (uint64, error) {
var block uint64
var err error
iter := db.backend.NewIterator([]byte(DestroyedAccountPrefix), nil)
for iter.Next() {
block, _, err = DecodeDestroyedAccountKey(iter.Key())
if err != nil {
return 0, fmt.Errorf("cannot decode updateset key; %v", err)
}
}
iter.Release()
return block, nil
}