This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
/
db_claim.go
75 lines (68 loc) · 1.72 KB
/
db_claim.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
package main
import (
"errors"
"fmt"
"github.com/icon-project/rewardcalculator/common"
"github.com/icon-project/rewardcalculator/common/db"
"github.com/icon-project/rewardcalculator/core"
"github.com/syndtr/goleveldb/leveldb/util"
"path/filepath"
)
func queryClaimDB(input Input) (err error) {
if input.path == "" {
fmt.Println("Enter dbPath")
return errors.New("invalid db path")
}
if input.address == "" {
err = printDB(input.path, util.BytesPrefix([]byte(db.PrefixClaim)), printClaim)
} else {
dir, name := filepath.Split(input.path)
qdb := db.Open(dir, string(db.GoLevelDBBackend), name)
defer qdb.Close()
address := common.NewAddressFromString(input.address)
if claim, err := getClaim(qdb, address); err != nil {
} else {
printClaimInstance(claim)
}
}
return
}
func getClaim(qdb db.Database, address *common.Address) (*core.Claim, error) {
bucket, err := qdb.GetBucket(db.PrefixClaim)
if err != nil {
fmt.Println("Failed to get claim Bucket")
return nil, err
}
key := address.Bytes()
value, e := bucket.Get(key)
if e != nil {
fmt.Println("Error while get claim value")
return nil, e
}
if value != nil {
return newClaim(key, value)
}
return nil, nil
}
func printClaim(key []byte, value []byte) (err error) {
if claim, e := newClaim(key, value); e != nil {
return e
} else {
printClaimInstance(claim)
return nil
}
}
func printClaimInstance(claim *core.Claim) {
if claim != nil {
fmt.Printf("%s\n", claim.String())
}
}
func newClaim(key []byte, value []byte) (*core.Claim, error) {
if claim, err := core.NewClaimFromBytes(value); err != nil {
fmt.Println("Failed to make claim instance")
return nil, err
} else {
claim.Address = *common.NewAddress(key)
return claim, nil
}
}