-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[staking] patch for staking protocol
- Loading branch information
Showing
8 changed files
with
153 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,6 @@ type ( | |
BuilderConfig struct { | ||
Staking genesis.Staking | ||
PersistCandsMapBlock uint64 | ||
CandsMapPatchDir string | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (c) 2020 IoTeX Foundation | ||
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no | ||
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent | ||
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache | ||
// License 2.0 that can be found in the LICENSE file. | ||
|
||
package staking | ||
|
||
import ( | ||
"encoding/csv" | ||
"encoding/hex" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
_name = "name" | ||
_operator = "operator" | ||
) | ||
|
||
// PatchStore is the patch store of staking protocol | ||
type PatchStore struct { | ||
dir string | ||
} | ||
|
||
// NewPatchStore creates a new staking patch store | ||
func NewPatchStore(dir string) *PatchStore { | ||
return &PatchStore{dir: dir} | ||
} | ||
|
||
func (store *PatchStore) pathOf(height uint64) string { | ||
return filepath.Join(store.dir, fmt.Sprintf("%d.patch", height)) | ||
} | ||
|
||
func (store *PatchStore) read(reader *csv.Reader) (CandidateList, error) { | ||
record, err := reader.Read() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(record) != 1 { | ||
return nil, errors.Errorf("invalid record %+v", record) | ||
} | ||
data, err := hex.DecodeString(record[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var list CandidateList | ||
if err := list.Deserialize(data); err != nil { | ||
return nil, err | ||
} | ||
return list, nil | ||
} | ||
|
||
// Read reads CandidateList by name and CandidateList by operator of given height | ||
func (store *PatchStore) Read(height uint64) (CandidateList, CandidateList, CandidateList, error) { | ||
file, err := os.Open(store.pathOf(height)) | ||
if err != nil { | ||
return nil, nil, nil, err | ||
} | ||
reader := csv.NewReader(file) | ||
reader.FieldsPerRecord = -1 | ||
listByName, err := store.read(reader) | ||
if err != nil { | ||
return nil, nil, nil, err | ||
} | ||
listByOperator, err := store.read(reader) | ||
if err != nil { | ||
return nil, nil, nil, err | ||
} | ||
listByOwner, err := store.read(reader) | ||
if err != nil && err != io.EOF { | ||
// io.EOF indicates an empty owner list | ||
return nil, nil, nil, err | ||
} | ||
return listByName, listByOperator, listByOwner, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) 2019 IoTeX Foundation | ||
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no | ||
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent | ||
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache | ||
// License 2.0 that can be found in the LICENSE file. | ||
|
||
package staking | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestInvalidDirectory(t *testing.T) { | ||
require := require.New(t) | ||
dir := filepath.Join(t.TempDir(), "invalid") | ||
_, err := os.Create(dir) | ||
require.NoError(err) | ||
_, _, _, err = NewPatchStore(dir).Read(0) | ||
require.ErrorContains(err, "not a directory") | ||
} | ||
|
||
func TestInvalidDirectory2(t *testing.T) { | ||
require := require.New(t) | ||
dir := t.TempDir() | ||
require.NoError(os.Remove(dir)) | ||
_, err := os.Stat(dir) | ||
require.ErrorIs(err, os.ErrNotExist) | ||
_, _, _, err = NewPatchStore(dir).Read(0) | ||
require.ErrorContains(err, "no such file or directory") | ||
} | ||
|
||
func TestCorruptedData(t *testing.T) { | ||
// TODO: add test for corrupted data | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters