-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
442 additions
and
44 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
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,13 @@ | ||
package staking | ||
|
||
import "github.com/iotexproject/iotex-core/blockchain/genesis" | ||
|
||
type ( | ||
|
||
// BuilderConfig returns the configuration of the builder | ||
BuilderConfig struct { | ||
Staking genesis.Staking | ||
PersistStakingPatchBlock uint64 | ||
StakingPatchDir 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,99 @@ | ||
// Copyright (c) 2022 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 | ||
|
||
func (cb *candBase) clone() *candBase { | ||
cb.lock.RLock() | ||
defer cb.lock.RUnlock() | ||
clone := newCandBase() | ||
for name, cand := range cb.nameMap { | ||
clone.nameMap[name] = cand.Clone() | ||
} | ||
for owner, cand := range cb.ownerMap { | ||
clone.ownerMap[owner] = cand.Clone() | ||
} | ||
for operator, cand := range cb.operatorMap { | ||
clone.operatorMap[operator] = cand.Clone() | ||
} | ||
for bucket, cand := range cb.selfStkBucketMap { | ||
clone.selfStkBucketMap[bucket] = cand.Clone() | ||
} | ||
if len(cb.owners) > 0 { | ||
for _, cand := range cb.owners { | ||
clone.owners = append(clone.owners, cand.Clone()) | ||
} | ||
} | ||
return clone | ||
} | ||
|
||
func (cb *candBase) candsInNameMap() CandidateList { | ||
cb.lock.RLock() | ||
defer cb.lock.RUnlock() | ||
if len(cb.nameMap) == 0 { | ||
return nil | ||
} | ||
|
||
list := make(CandidateList, 0, len(cb.nameMap)) | ||
for _, d := range cb.nameMap { | ||
list = append(list, d.Clone()) | ||
} | ||
return list | ||
} | ||
|
||
func (cb *candBase) candsInOperatorMap() CandidateList { | ||
cb.lock.RLock() | ||
defer cb.lock.RUnlock() | ||
if len(cb.operatorMap) == 0 { | ||
return nil | ||
} | ||
|
||
list := make(CandidateList, 0, len(cb.operatorMap)) | ||
for _, d := range cb.operatorMap { | ||
list = append(list, d.Clone()) | ||
} | ||
return list | ||
} | ||
|
||
func (cb *candBase) ownersList() CandidateList { | ||
cb.lock.RLock() | ||
defer cb.lock.RUnlock() | ||
return cb.owners | ||
} | ||
|
||
func (cb *candBase) recordOwner(c *Candidate) { | ||
cb.lock.Lock() | ||
defer cb.lock.Unlock() | ||
for i, d := range cb.owners { | ||
if d.Owner.String() == c.Owner.String() { | ||
cb.owners[i] = c.Clone() | ||
return | ||
} | ||
} | ||
// this is a new candidate | ||
cb.owners = append(cb.owners, c.Clone()) | ||
} | ||
|
||
func (cb *candBase) loadNameOperatorMapOwnerList(name, op, owners CandidateList) error { | ||
cb.lock.Lock() | ||
defer cb.lock.Unlock() | ||
cb.nameMap = make(map[string]*Candidate) | ||
for _, d := range name { | ||
if err := d.Validate(); err != nil { | ||
return err | ||
} | ||
cb.nameMap[d.Name] = d | ||
} | ||
cb.operatorMap = make(map[string]*Candidate) | ||
for _, d := range op { | ||
if err := d.Validate(); err != nil { | ||
return err | ||
} | ||
cb.operatorMap[d.Operator.String()] = d | ||
} | ||
cb.owners = owners | ||
return 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
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
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 | ||
} |
Oops, something went wrong.