-
Notifications
You must be signed in to change notification settings - Fork 2
/
export_accounts.go
167 lines (145 loc) · 4.34 KB
/
export_accounts.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
package token
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"path"
"time"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
ethermint "github.com/fibonacci-chain/fbc/app/types"
sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
authexported "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/exported"
"github.com/fibonacci-chain/fbc/libs/tendermint/libs/cli"
"github.com/spf13/viper"
)
const (
FlagOSSEnable = "oss-enable"
FlagOSSEndpoint = "oss-endpoint"
FlagOSSAccessKeyID = "oss-access-key-id"
FlagOSSAccessKeySecret = "oss-access-key-secret"
FlagOSSBucketName = "oss-bucket-name"
FlagOSSObjectPath = "oss-object-path"
)
var (
logFileName = "export-upload-account.log"
)
type AccType int
const (
UserAccount AccType = iota
ContractAccount
ModuleAccount
OtherAccount
WasmAccount
)
func exportAccounts(ctx sdk.Context, keeper Keeper) (filePath string) {
pt := time.Now().UTC().Format(time.RFC3339)
rootDir := viper.GetString(cli.HomeFlag)
accFileName := fmt.Sprintf("accounts-%d-%s.csv", ctx.BlockHeight(), pt)
// 1. open log file
logFile, logWr, err := openLogFile()
if err != nil {
return
}
defer logFile.Close()
defer logWr.Flush()
recodeLog(logWr, "===============")
recodeLog(logWr, fmt.Sprintf("time: %s", pt))
recodeLog(logWr, fmt.Sprintf("height: %d", ctx.BlockHeight()))
recodeLog(logWr, fmt.Sprintf("file name: %s", accFileName))
// 2. open account file
accFile, err := os.OpenFile(path.Join(rootDir, accFileName), os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
if err != nil {
recodeLog(logWr, fmt.Sprintf("open account file error: %s", err))
return
}
defer accFile.Close()
accWr := bufio.NewWriter(accFile)
defer accWr.Flush()
defer func() {
if err := recover(); err != nil {
recodeLog(logWr, fmt.Sprintf("export accounts panic: %s", err))
}
}()
count := 0
startTime := time.Now()
keeper.accountKeeper.IterateAccounts(ctx, func(account authexported.Account) bool {
ethAcc, ok := account.(*ethermint.EthAccount)
if !ok {
return false
}
//account.SpendableCoins()
fiboBalance := account.GetCoins().AmountOf(sdk.DefaultBondDenom)
if !fiboBalance.GT(sdk.ZeroDec()) {
return false
}
accType := UserAccount
if !bytes.Equal(ethAcc.CodeHash, ethcrypto.Keccak256(nil)) {
accType = ContractAccount
}
csvStr := fmt.Sprintf("%s,%d,%s,%d,%s",
ethAcc.EthAddress().String(),
accType,
fiboBalance.String(),
ctx.BlockHeight(),
pt,
)
fmt.Fprintln(accWr, csvStr)
count++
return false
})
recodeLog(logWr, fmt.Sprintf("count: %d", count))
recodeLog(logWr, fmt.Sprintf("export duration: %s", time.Since(startTime).String()))
return path.Join(rootDir, accFileName)
}
func uploadOSS(filePath string) {
// 1. open log file
logFile, logWr, err := openLogFile()
if err != nil {
return
}
defer logFile.Close()
defer logWr.Flush()
defer func() {
if err := recover(); err != nil {
recodeLog(logWr, fmt.Sprintf("upload OSS panic: %s", err))
}
}()
startTime := time.Now()
// create OSSClient
ossClient, err := oss.New(viper.GetString(FlagOSSEndpoint), viper.GetString(FlagOSSAccessKeyID), viper.GetString(FlagOSSAccessKeySecret))
if err != nil {
recodeLog(logWr, fmt.Sprintf("creates oss lcient error: %s", err))
return
}
// gets the bucket instance
bucket, err := ossClient.Bucket(viper.GetString(FlagOSSBucketName))
if err != nil {
recodeLog(logWr, fmt.Sprintf("gets the bucket instance error: %s", err))
return
}
_, fileName := path.Split(filePath)
objectName := viper.GetString(FlagOSSObjectPath) + fmt.Sprintf("accounts-%s/", time.Now().Format("20060102")) + fileName
// multipart file upload
err = bucket.UploadFile(objectName, filePath, 100*1024, oss.Routines(3), oss.Checkpoint(true, ""))
if err != nil {
recodeLog(logWr, fmt.Sprintf("multipart file upload error: %s", err))
return
}
recodeLog(logWr, fmt.Sprintf("oss file: %s", objectName))
recodeLog(logWr, fmt.Sprintf("upload duration: %s", time.Since(startTime).String()))
}
func openLogFile() (*os.File, *bufio.Writer, error) {
rootDir := viper.GetString(cli.HomeFlag)
file, err := os.OpenFile(path.Join(rootDir, logFileName), os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
if err != nil {
return nil, nil, err
}
logWr := bufio.NewWriter(file)
return file, logWr, nil
}
func recodeLog(w io.Writer, s string) {
fmt.Fprintln(w, s)
}