-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Version store #9
base: version-store
Are you sure you want to change the base?
Changes from all commits
f0fc6e6
66ee597
ec465d5
79163ab
90bb2f7
a0f319f
0418958
28d3e06
1f590ad
9531101
649a852
e10a999
cc22b7b
fe8361e
f16555a
f73463b
b732cfd
bbc8053
b24dfcb
0eb6bf7
58476f2
1f4fc87
b8b75f7
7518c5e
591d604
97f6f09
8321afc
ef93aa8
4a9cd2f
ac15f12
575a003
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/store/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/gogo/protobuf/proto" | ||
) | ||
|
||
func DecodeData(data []byte) (pairs []types.StoreKVPair, err error) { | ||
const prefixLen = 8 | ||
offset := prefixLen | ||
dataSize := sdk.BigEndianToUint64(data[:offset]) | ||
size := len(data) | ||
if int(dataSize)+prefixLen != size { | ||
Check failure Code scanning / gosec Potential integer overflow by integer type conversion
Potential integer overflow by integer type conversion
|
||
return nil, fmt.Errorf("incomplete file: %v vs %v", dataSize, size) | ||
} | ||
for offset < size { | ||
size, n := proto.DecodeVarint(data[offset:]) | ||
offset += n | ||
pair := new(types.StoreKVPair) | ||
if err := proto.Unmarshal(data[offset:offset+int(size)], pair); err != nil { | ||
Check failure Code scanning / gosec Potential integer overflow by integer type conversion
Potential integer overflow by integer type conversion
|
||
return nil, err | ||
} | ||
pairs = append(pairs, *pair) | ||
offset += int(size) | ||
Check failure Code scanning / gosec Potential integer overflow by integer type conversion
Potential integer overflow by integer type conversion
|
||
} | ||
return | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
package file | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/crypto-org-chain/cronos/client" | ||
"github.com/crypto-org-chain/cronos/versiondb/tmdb" | ||
tmjson "github.com/tendermint/tendermint/libs/json" | ||
tmtypes "github.com/tendermint/tendermint/types" | ||
) | ||
|
||
// Simplify block height for header | ||
type Header struct { | ||
Height int64 `json:"height,omitempty"` | ||
} | ||
|
||
type Block struct { | ||
Header Header `json:"header"` | ||
} | ||
|
||
type GetLatestBlockResponse struct { | ||
Block *Block `json:"block,omitempty"` | ||
} | ||
|
||
func Sync(versionDB *tmdb.Store, remoteGrpcUrl, remoteUrl, remoteWsUrl, rootDir string, isLocal bool, concurrency int) { | ||
|
||
const defaultMaxRetry = 50 | ||
latestVersion, err := versionDB.GetLatestVersion() | ||
fmt.Printf("mm-latestVersion: %+v\n", latestVersion) | ||
if err != nil { | ||
panic(err) | ||
} | ||
startBlockNum := latestVersion | ||
if startBlockNum < 0 { | ||
startBlockNum = 0 | ||
} | ||
nextBlockNum := int(startBlockNum) + 1 | ||
maxBlockNum := -1 | ||
for i := 0; i < defaultMaxRetry; i++ { | ||
if i > 0 { | ||
time.Sleep(time.Second) | ||
} | ||
resp, err := http.Get(fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/blocks/latest", remoteGrpcUrl)) | ||
if err != nil { | ||
fmt.Printf("error making http request: %s\n", err) | ||
continue | ||
} | ||
|
||
var bz []byte | ||
if result := func() bool { | ||
defer resp.Body.Close() | ||
bz, err = ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return false | ||
} | ||
if resp.StatusCode != http.StatusOK { | ||
return false | ||
} | ||
return true | ||
}(); !result { | ||
continue | ||
} | ||
|
||
res := new(GetLatestBlockResponse) | ||
err = tmjson.Unmarshal(bz, res) | ||
if err != nil { | ||
fmt.Printf("mm-read-res-err: %+v\n", err) | ||
continue | ||
} | ||
maxBlockNum = int(res.Block.Header.Height) | ||
fmt.Printf("mm-maxBlockNum: %d\n", maxBlockNum) | ||
break | ||
} | ||
if maxBlockNum < 0 { | ||
panic(fmt.Sprintf("max retries %d reached", defaultMaxRetry)) | ||
} | ||
|
||
interval := time.Second | ||
directory := filepath.Join(rootDir, "data", "file_streamer") | ||
// streamer write the file blk by blk with concurrency 1 | ||
streamer := NewBlockFileWatcher(1, maxBlockNum, func(blockNum int) string { | ||
return GetLocalDataFileName(directory, blockNum) | ||
}, nil, true) | ||
streamer.Start(nextBlockNum, interval) | ||
go func() { | ||
chData, chErr := streamer.SubscribeData(), streamer.SubscribeError() | ||
for { | ||
select { | ||
case data := <-chData: | ||
pairs, err := client.DecodeData(data.Data) | ||
fmt.Printf("mm-pairs: %+v, %+v\n", len(pairs), err) | ||
if err != nil { | ||
fmt.Println("invalid decode") | ||
} else if err = versionDB.PutAtVersion(int64(data.BlockNum), pairs); err != nil { | ||
fmt.Println("mm-put-at-version-panic") | ||
panic(err) | ||
} | ||
data.ChResult <- err | ||
case err := <-chErr: | ||
// fail read | ||
fmt.Println("mm-fail-read-panic") | ||
panic(err) | ||
} | ||
} | ||
}() | ||
|
||
synchronizer := NewBlockFileWatcher( | ||
concurrency, | ||
maxBlockNum, | ||
func(blockNum int) string { | ||
return fmt.Sprintf("%s/%s", remoteUrl, DataFileName(blockNum)) | ||
}, | ||
func(blockNum int) bool { | ||
path := GetLocalDataFileName(directory, blockNum) | ||
f, err := os.Open(path) | ||
Check failure Code scanning / gosec Potential file inclusion via variable
Potential file inclusion via variable
|
||
if err == nil { | ||
defer func() { | ||
_ = f.Close() | ||
Check warning Code scanning / gosec Returned error is not propagated up the stack.
Returned error is not propagated up the stack.
|
||
}() | ||
// valid 1st 8 bytes for downloaded file | ||
var bytes [8]byte | ||
if _, err = io.ReadFull(f, bytes[:]); err == nil { | ||
size := binary.BigEndian.Uint64(bytes[:]) | ||
if info, err := f.Stat(); err == nil && size+uint64(8) == uint64(info.Size()) { | ||
Check failure Code scanning / gosec Potential integer overflow by integer type conversion
Potential integer overflow by integer type conversion
|
||
return false | ||
} | ||
} | ||
} | ||
return true | ||
}, | ||
isLocal, | ||
) | ||
synchronizer.Start(nextBlockNum, interval) | ||
go func() { | ||
// max retry for temporary io error | ||
maxRetry := concurrency * 2 | ||
retry := 0 | ||
chData, chErr := synchronizer.SubscribeData(), synchronizer.SubscribeError() | ||
for { | ||
select { | ||
case data := <-chData: | ||
file := GetLocalDataFileName(directory, data.BlockNum) | ||
fmt.Printf("mm-data.BlockNum: %+v\n", data.BlockNum) | ||
if err := os.WriteFile(file, data.Data, 0600); err != nil { | ||
fmt.Println("mm-WriteFile-panic") | ||
panic(err) | ||
} else { | ||
retry = 0 | ||
fmt.Println("mm-reset-retry") | ||
if data.BlockNum > maxBlockNum { | ||
streamer.SetMaxBlockNum(data.BlockNum) | ||
} | ||
} | ||
data.ChResult <- err | ||
case err := <-chErr: | ||
retry++ | ||
fmt.Println("mm-retry", retry) | ||
if retry == maxRetry { | ||
// data corrupt | ||
fmt.Println("mm-data-corrupt-panic") | ||
panic(err) | ||
} | ||
} | ||
} | ||
}() | ||
|
||
go func() { | ||
for i := 0; i < defaultMaxRetry; i++ { | ||
if i > 0 { | ||
time.Sleep(time.Second) | ||
} | ||
wsClient := client.NewWebsocketClient(remoteWsUrl) | ||
chResult, err := wsClient.Subscribe() | ||
if err != nil { | ||
fmt.Printf("mm-subscribed[%+v]: %+v\n", i, err) | ||
continue | ||
} | ||
fmt.Println("subscribing") | ||
err = wsClient.Send("subscribe", []string{ | ||
"tm.event='NewBlockHeader'", | ||
}) | ||
if err != nil { | ||
fmt.Printf("mm-subscribed: %+v\n", err) | ||
continue | ||
} | ||
i = 0 | ||
fmt.Println("subscribed ws") | ||
for res := range chResult { | ||
if res == nil || res.Data == nil { | ||
continue | ||
} | ||
data, ok := res.Data.(tmtypes.EventDataNewBlockHeader) | ||
if !ok { | ||
continue | ||
} | ||
blockNum := int(data.Header.Height) | ||
fmt.Printf("mm-set-max-blk: %+v\n", blockNum) | ||
synchronizer.SetMaxBlockNum(blockNum) | ||
} | ||
} | ||
panic(fmt.Sprintf("max retries %d reached", defaultMaxRetry)) | ||
}() | ||
} |
Check failure
Code scanning / gosec
the value in the range statement should be _ unless copying a map: want: for key := range m