-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.go
140 lines (121 loc) · 4.49 KB
/
bootstrap.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
package tokenmeta
import (
"context"
"fmt"
"time"
"github.com/streamingfast/bstream"
pbstatedb "github.com/zhongshuwen/histnew/pb/dfuse/zswhq/statedb/v1"
pbtokenmeta "github.com/zhongshuwen/histnew/pb/dfuse/zswhq/tokenmeta/v1"
"github.com/zhongshuwen/histnew/tokenmeta/cache"
"github.com/zhongshuwen/zswchain-go"
"github.com/tidwall/gjson"
"go.uber.org/zap"
)
func Bootstrap(abisFileContent []byte, stateClient pbstatedb.StateClient, bootstrapblockOffset uint64) (tokens []*pbtokenmeta.Token, balances []*pbtokenmeta.AccountBalance, stakeds []*cache.EOSStakeEntry, startBlock bstream.BlockRef, err error) {
startBlock = bstream.NewBlockRef("", 0)
tokenContracts := parseContractFromABIs(abisFileContent)
abiStartBlock, err := parseCursorFromABIs(abisFileContent)
if err != nil {
zlog.Warn("parsing cursor from ABIs Cache", zap.Error(err))
} else {
// the cursor located in the ABIs cached (exported by abicodec) is following the live, thus there is no guaranty that it is
// irreversible. Tokenmeta start with a forkable which only processes irreversible blocks, so to avoid
// stalling forkable (starting it off with an ID within a fork block) we disregard the block id from abi codec cursor.
// Furthermore, we added a bootstrap block offeset to ensure when you are querying StateDB you do not hit reversible blocks
startBlock = bstream.NewBlockRef("", (abiStartBlock.Num() - bootstrapblockOffset))
}
ctx := context.Background()
sta, err := getEOSStakedFromStateDB(ctx, stateClient, uint32(startBlock.Num()))
stakeds = append(stakeds, sta...)
zlog.Info("looping through valid contracts",
zap.Uint64("start_block_num", startBlock.Num()),
zap.String("start_block_id", startBlock.ID()),
zap.Int("valid_contracts_count", len(tokenContracts)),
)
for _, tokenContract := range tokenContracts {
for attempt := 1; true; attempt++ {
toks, bals, err := processContract(ctx, tokenContract, uint32(startBlock.Num()), stateClient)
if err == nil {
if toks == nil {
zlog.Info("skipping empty token contract",
zap.String("token_contract", string(tokenContract)),
)
break
}
tokens = append(tokens, toks...)
balances = append(balances, bals...)
break
}
if !isRetryableStateDBError(err) {
zlog.Info("skipping invalid token contract, unable to get symbols with non-retryable error",
zap.String("token_contract", string(tokenContract)),
zap.Error(err),
)
break
}
if attempt > 5 {
return nil, nil, nil, nil, fmt.Errorf("failing after 5 attempts to get symbols from token contract: %w", err)
}
zlog.Warn("unable to get symbols from token contract, retrying", zap.String("token_contract", string(tokenContract)), zap.Error(err))
time.Sleep(time.Duration(attempt) * time.Second)
}
}
return tokens, balances, stakeds, startBlock, nil
}
func parseContractFromABIs(cnt []byte) (out []zsw.AccountName) {
var accounts, withTableAccounts, withTableStat, tokenContracts int
gjson.GetBytes(cnt, "Abis").ForEach(func(k, v gjson.Result) bool {
accounts++
account := k.String()
var lastABI gjson.Result
v.ForEach(func(k, v gjson.Result) bool {
lastABI = v
return true
})
rawABI := lastABI.Get("ABI").Raw
if rawABI == "" {
zlog.Info("skipping missing ABI in account, probably normal",
zap.String("account", account),
)
return true
}
contractStats, err := getTokenContractStats(account, []byte(rawABI), true)
if err != nil {
zlog.Warn("failed decoding ABI in account",
zap.String("account", account),
zap.String("raw_abi", rawABI),
zap.Error(err),
)
return false
}
if contractStats.hasStatsTable {
withTableStat++
}
if contractStats.hasAccountsTable {
withTableAccounts++
}
if contractStats.isTokenContract {
out = append(out, zsw.AccountName(account))
tokenContracts++
}
return true
})
zlog.Info("abis content stats",
zap.Int("accounts_count", accounts),
zap.Int("accounts_with_accounts_table", withTableAccounts),
zap.Int("accounts_with_stat_table", withTableStat),
zap.Int("accounts_with_stat_table_and_accounts_table", tokenContracts),
)
return
}
func parseCursorFromABIs(cnt []byte) (bstream.BlockRef, error) {
cursor := gjson.GetBytes(cnt, "cursor").String()
if cursor == "" {
return nil, fmt.Errorf("cursor expected in ABIs cached file")
}
blockNum, headBlockID, _, err := parseCursor(cursor)
if err != nil {
return nil, fmt.Errorf("unable to parse cursor %q in ABIs cached file: %w", cursor, err)
}
return bstream.NewBlockRef(headBlockID, blockNum), nil
}