-
Notifications
You must be signed in to change notification settings - Fork 1
/
io.go
180 lines (148 loc) · 5.75 KB
/
io.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
// This file is part of the Dazzle Gravity library.
//
// The Dazzle Gravity 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 Dazzle Gravity 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 Dazzle Gravity library. If not, see <e <http://www.gnu.org/licenses/>./>.
package smartcontract
import (
"fmt"
"github.com/SHDMT/gravity/infrastructure/crypto/hash"
"github.com/SHDMT/gravity/infrastructure/database"
"github.com/SHDMT/gravity/infrastructure/log"
"github.com/SHDMT/gravity/platform/smartcontract/internal/dbnamespace"
)
func dbPutContract(dbTx database.Tx, key, value []byte) error {
contractBucket := dbTx.Data().Bucket(dbnamespace.ContractBucket)
err := contractBucket.Put(key, value)
if err != nil {
errString := fmt.Sprintf("Failed to put contract %v", key)
return NewSmartContractError(ErrPutDB, errString, err)
}
return nil
}
func dbFetchContract(dbTx database.Tx, key []byte) ([]byte, error) {
contractBucket := dbTx.Data().Bucket([]byte(dbnamespace.ContractBucket))
value := contractBucket.Get(key)
if value == nil {
errString := fmt.Sprintf("Failed to find contract %v", key)
return nil, NewSmartContractError(ErrNotFoundFormDB, errString, nil)
}
return value, nil
}
func dbDeleteContract(dbTx database.Tx, key []byte) error {
contractBucket := dbTx.Data().Bucket([]byte(dbnamespace.ContractBucket))
err := contractBucket.Delete(key)
if err != nil {
errString := fmt.Sprintf("Failed to delete contract %v", key)
return NewSmartContractError(ErrDeleteDB, errString, err)
}
return nil
}
func dbHasContract(dbTx database.Tx, key []byte) bool {
contractBucket := dbTx.Data().Bucket(dbnamespace.ContractBucket)
return contractBucket.KeyExists(key)
}
func dbListAllContracts(dbTx database.Tx) ([]hash.HashType, error) {
contractBucket := dbTx.Data().Bucket(dbnamespace.ContractBucket)
contracts := make([]hash.HashType, 0, 16)
err := contractBucket.ForEach(func(k, v []byte) error {
contracts = append(contracts, k)
return nil
})
if err != nil {
errString := fmt.Sprintf("Failed to list all contracts")
return nil, NewSmartContractError(ErrForEachDB, errString, err)
}
return contracts, nil
}
func dbPutAssetContract(dbTx database.Tx, key, value []byte) error {
assetContractBucket := dbTx.Data().Bucket(dbnamespace.AssetContractBucket)
err := assetContractBucket.Put(key, value)
if err != nil {
errString := fmt.Sprintf("Failed to put assetContract %v", key)
return NewSmartContractError(ErrPutDB, errString, err)
}
return nil
}
func dbFetchAssetContract(dbTx database.Tx, key []byte) ([]byte, error) {
assetContractBucket := dbTx.Data().Bucket([]byte(dbnamespace.AssetContractBucket))
value := assetContractBucket.Get(key)
if value == nil {
errString := fmt.Sprintf("Failed to find assetContract %v", key)
return nil, NewSmartContractError(ErrNotFoundFormDB, errString, nil)
}
return value, nil
}
func dbDeleteAssetContract(dbTx database.Tx, key []byte) error {
assetContractBucket := dbTx.Data().Bucket([]byte(dbnamespace.AssetContractBucket))
err := assetContractBucket.Delete(key)
if err != nil {
errString := fmt.Sprintf("Failed to delete assetContract %v", key)
return NewSmartContractError(ErrDeleteDB, errString, err)
}
return nil
}
func dbHasAssetContract(dbTx database.Tx, key []byte) bool {
assetContractBucket := dbTx.Data().Bucket(dbnamespace.AssetContractBucket)
return assetContractBucket.KeyExists(key)
}
func dbPutAsset(dbTx database.Tx, key, value []byte) error {
assetBucket := dbTx.Data().Bucket(dbnamespace.AssetBucket)
err := assetBucket.Put(key, value)
if err != nil {
errString := fmt.Sprintf("Failed to put asset %v", key)
return NewSmartContractError(ErrPutDB, errString, err)
}
return nil
}
func dbFetchAsset(dbTx database.Tx, key []byte) ([]byte, error) {
assetBucket := dbTx.Data().Bucket([]byte(dbnamespace.AssetBucket))
value := assetBucket.Get(key)
if value == nil {
errString := fmt.Sprintf("Failed to find asset %v", key)
return nil, NewSmartContractError(ErrNotFoundFormDB, errString, nil)
}
return value, nil
}
func dbDeleteAsset(dbTx database.Tx, key []byte) error {
assetBucket := dbTx.Data().Bucket([]byte(dbnamespace.AssetBucket))
err := assetBucket.Delete(key)
if err != nil {
errString := fmt.Sprintf("Failed to delete asset %v", key)
return NewSmartContractError(ErrDeleteDB, errString, err)
}
return nil
}
func dbHasAsset(dbTx database.Tx, key []byte) bool {
assetBucket := dbTx.Data().Bucket(dbnamespace.AssetBucket)
return assetBucket.KeyExists(key)
}
//CreateSmartContractBucket is the bucket associated with creating smart contracts
func CreateSmartContractBucket(db database.Db) error {
err := db.Update(func(tx database.Tx) error {
errs := make([]error, 3)
_, errs[0] = tx.Data().CreateBucket(dbnamespace.ContractBucket)
_, errs[1] = tx.Data().CreateBucket(dbnamespace.AssetContractBucket)
_, errs[2] = tx.Data().CreateBucket(dbnamespace.AssetBucket)
for _, err := range errs {
if err != nil {
dbErr := err.(*database.DbError)
if !database.IsBucketAlreadyExistsError(dbErr) {
log.Errorf("create bucket err : %v\n", err)
return err
}
}
}
return nil
})
return err
}