## Description The `io/ioutil` package has been deprecated in Go 1.16 (See https://golang.org/doc/go1.16#ioutil). Since cosmos-sdk has upgraded to Go 1.17 (#9987), this PR replaces the existing `io/ioutil` functions with their new definitions in `io` and `os` packages. --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
152 lines
3.3 KiB
Go
152 lines
3.3 KiB
Go
package snapshots_test
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
db "github.com/tendermint/tm-db"
|
|
|
|
"github.com/cosmos/cosmos-sdk/snapshots"
|
|
"github.com/cosmos/cosmos-sdk/snapshots/types"
|
|
)
|
|
|
|
func checksums(slice [][]byte) [][]byte {
|
|
hasher := sha256.New()
|
|
checksums := make([][]byte, len(slice))
|
|
for i, chunk := range slice {
|
|
hasher.Write(chunk)
|
|
checksums[i] = hasher.Sum(nil)
|
|
hasher.Reset()
|
|
}
|
|
return checksums
|
|
}
|
|
|
|
func hash(chunks [][]byte) []byte {
|
|
hasher := sha256.New()
|
|
for _, chunk := range chunks {
|
|
hasher.Write(chunk)
|
|
}
|
|
return hasher.Sum(nil)
|
|
}
|
|
|
|
func makeChunks(chunks [][]byte) <-chan io.ReadCloser {
|
|
ch := make(chan io.ReadCloser, len(chunks))
|
|
for _, chunk := range chunks {
|
|
ch <- io.NopCloser(bytes.NewReader(chunk))
|
|
}
|
|
close(ch)
|
|
return ch
|
|
}
|
|
|
|
func readChunks(chunks <-chan io.ReadCloser) [][]byte {
|
|
bodies := [][]byte{}
|
|
for chunk := range chunks {
|
|
body, err := io.ReadAll(chunk)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
bodies = append(bodies, body)
|
|
}
|
|
return bodies
|
|
}
|
|
|
|
type mockSnapshotter struct {
|
|
chunks [][]byte
|
|
}
|
|
|
|
func (m *mockSnapshotter) Restore(
|
|
height uint64, format uint32, chunks <-chan io.ReadCloser, ready chan<- struct{},
|
|
) error {
|
|
if format == 0 {
|
|
return types.ErrUnknownFormat
|
|
}
|
|
if m.chunks != nil {
|
|
return errors.New("already has contents")
|
|
}
|
|
if ready != nil {
|
|
close(ready)
|
|
}
|
|
|
|
m.chunks = [][]byte{}
|
|
for reader := range chunks {
|
|
chunk, err := io.ReadAll(reader)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
m.chunks = append(m.chunks, chunk)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (m *mockSnapshotter) Snapshot(height uint64, format uint32) (<-chan io.ReadCloser, error) {
|
|
if format == 0 {
|
|
return nil, types.ErrUnknownFormat
|
|
}
|
|
ch := make(chan io.ReadCloser, len(m.chunks))
|
|
for _, chunk := range m.chunks {
|
|
ch <- io.NopCloser(bytes.NewReader(chunk))
|
|
}
|
|
close(ch)
|
|
return ch, nil
|
|
}
|
|
|
|
// setupBusyManager creates a manager with an empty store that is busy creating a snapshot at height 1.
|
|
// The snapshot will complete when the returned closer is called.
|
|
func setupBusyManager(t *testing.T) *snapshots.Manager {
|
|
// os.MkdirTemp() is used instead of testing.T.TempDir()
|
|
// see https://github.com/cosmos/cosmos-sdk/pull/8475 for
|
|
// this change's rationale.
|
|
tempdir, err := os.MkdirTemp("", "")
|
|
require.NoError(t, err)
|
|
t.Cleanup(func() { _ = os.RemoveAll(tempdir) })
|
|
|
|
store, err := snapshots.NewStore(db.NewMemDB(), tempdir)
|
|
require.NoError(t, err)
|
|
hung := newHungSnapshotter()
|
|
mgr := snapshots.NewManager(store, hung)
|
|
|
|
go func() {
|
|
_, err := mgr.Create(1)
|
|
require.NoError(t, err)
|
|
}()
|
|
time.Sleep(10 * time.Millisecond)
|
|
t.Cleanup(hung.Close)
|
|
|
|
return mgr
|
|
}
|
|
|
|
// hungSnapshotter can be used to test operations in progress. Call close to end the snapshot.
|
|
type hungSnapshotter struct {
|
|
ch chan struct{}
|
|
}
|
|
|
|
func newHungSnapshotter() *hungSnapshotter {
|
|
return &hungSnapshotter{
|
|
ch: make(chan struct{}),
|
|
}
|
|
}
|
|
|
|
func (m *hungSnapshotter) Close() {
|
|
close(m.ch)
|
|
}
|
|
|
|
func (m *hungSnapshotter) Snapshot(height uint64, format uint32) (<-chan io.ReadCloser, error) {
|
|
<-m.ch
|
|
ch := make(chan io.ReadCloser, 1)
|
|
ch <- io.NopCloser(bytes.NewReader([]byte{}))
|
|
return ch, nil
|
|
}
|
|
|
|
func (m *hungSnapshotter) Restore(
|
|
height uint64, format uint32, chunks <-chan io.ReadCloser, ready chan<- struct{},
|
|
) error {
|
|
panic("not implemented")
|
|
}
|