Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions action/protocol/poll/blockmeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import (
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/iotexproject/iotex-core/v2/action/protocol"
"github.com/iotexproject/iotex-core/v2/action/protocol/poll/blockmetapb"
"github.com/iotexproject/iotex-core/v2/pkg/util/assertions"
"github.com/iotexproject/iotex-core/v2/state/factory/erigonstore"
"github.com/iotexproject/iotex-core/v2/systemcontracts"
)

Expand All @@ -26,10 +23,6 @@ type BlockMeta struct {
MintTime time.Time
}

func init() {
assertions.MustNoError(erigonstore.GetObjectStorageRegistry().RegisterPollBlockMeta(protocol.SystemNamespace, &BlockMeta{}))
}

// NewBlockMeta constructs new blockmeta struct with given fieldss
func NewBlockMeta(height uint64, producer string, mintTime time.Time) *BlockMeta {
return &BlockMeta{
Expand Down
11 changes: 0 additions & 11 deletions action/protocol/rewarding/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ import (
"github.com/iotexproject/iotex-core/v2/action/protocol"
"github.com/iotexproject/iotex-core/v2/action/protocol/rewarding/rewardingpb"
"github.com/iotexproject/iotex-core/v2/blockchain/genesis"
"github.com/iotexproject/iotex-core/v2/pkg/util/assertions"
"github.com/iotexproject/iotex-core/v2/state"
"github.com/iotexproject/iotex-core/v2/state/factory/erigonstore"
"github.com/iotexproject/iotex-core/v2/systemcontracts"
)

Expand All @@ -34,14 +31,6 @@ type admin struct {
productivityThreshold uint64
}

func init() {
registry := erigonstore.GetObjectStorageRegistry()
assertions.MustNoError(registry.RegisterRewardingV1(state.AccountKVNamespace, &admin{}))
assertions.MustNoError(registry.RegisterRewardingV1(state.AccountKVNamespace, &exempt{}))
assertions.MustNoError(registry.RegisterRewardingV2(_v2RewardingNamespace, &admin{}))
assertions.MustNoError(registry.RegisterRewardingV2(_v2RewardingNamespace, &exempt{}))
}

// Serialize serializes admin state into bytes
func (a admin) Serialize() ([]byte, error) {
gen := rewardingpb.Admin{
Expand Down
8 changes: 0 additions & 8 deletions action/protocol/rewarding/fund.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ import (
"github.com/iotexproject/iotex-core/v2/action/protocol"
accountutil "github.com/iotexproject/iotex-core/v2/action/protocol/account/util"
"github.com/iotexproject/iotex-core/v2/action/protocol/rewarding/rewardingpb"
"github.com/iotexproject/iotex-core/v2/pkg/util/assertions"
"github.com/iotexproject/iotex-core/v2/state"
"github.com/iotexproject/iotex-core/v2/state/factory/erigonstore"
"github.com/iotexproject/iotex-core/v2/systemcontracts"
)

Expand All @@ -32,12 +30,6 @@ type fund struct {
unclaimedBalance *big.Int
}

func init() {
registry := erigonstore.GetObjectStorageRegistry()
assertions.MustNoError(registry.RegisterRewardingV1(state.AccountKVNamespace, &fund{}))
assertions.MustNoError(registry.RegisterRewardingV2(_v2RewardingNamespace, &fund{}))
}

// Serialize serializes fund state into bytes
func (f fund) Serialize() ([]byte, error) {
gen := rewardingpb.Fund{
Expand Down
10 changes: 0 additions & 10 deletions action/protocol/rewarding/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,13 @@ import (
"github.com/iotexproject/iotex-core/v2/action/protocol/staking"
"github.com/iotexproject/iotex-core/v2/pkg/enc"
"github.com/iotexproject/iotex-core/v2/pkg/log"
"github.com/iotexproject/iotex-core/v2/pkg/util/assertions"
"github.com/iotexproject/iotex-core/v2/state"
"github.com/iotexproject/iotex-core/v2/state/factory/erigonstore"
"github.com/iotexproject/iotex-core/v2/systemcontracts"
)

// rewardHistory is the dummy struct to record a reward. Only key matters.
type rewardHistory struct{}

func init() {
registry := erigonstore.GetObjectStorageRegistry()
assertions.MustNoError(registry.RegisterRewardingV1(state.AccountKVNamespace, &rewardHistory{}))
assertions.MustNoError(registry.RegisterRewardingV1(state.AccountKVNamespace, &rewardAccount{}))
assertions.MustNoError(registry.RegisterRewardingV2(_v2RewardingNamespace, &rewardHistory{}))
assertions.MustNoError(registry.RegisterRewardingV2(_v2RewardingNamespace, &rewardAccount{}))
}

// Serialize serializes reward history state into bytes
func (b rewardHistory) Serialize() ([]byte, error) {
gen := rewardingpb.RewardHistory{}
Expand Down
15 changes: 15 additions & 0 deletions action/protocol/staking/bucket_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/iotexproject/iotex-address/address"

"github.com/iotexproject/iotex-core/v2/action/protocol/staking/stakingpb"
"github.com/iotexproject/iotex-core/v2/systemcontracts"
)

type (
Expand Down Expand Up @@ -51,6 +52,20 @@ func (bis *BucketIndices) Serialize() ([]byte, error) {
return proto.Marshal(bis.Proto())
}

// Encode encodes bucket indices into generic value
func (bis *BucketIndices) Encode() (systemcontracts.GenericValue, error) {
data, err := bis.Serialize()
if err != nil {
return systemcontracts.GenericValue{}, errors.Wrap(err, "failed to serialize bucket indices")
}
return systemcontracts.GenericValue{PrimaryData: data}, nil
}

// Decode decodes bucket indices from generic value
func (bis *BucketIndices) Decode(gv systemcontracts.GenericValue) error {
return bis.Deserialize(gv.PrimaryData)
}

func (bis *BucketIndices) addBucketIndex(index uint64) {
*bis = append(*bis, index)
}
Expand Down
16 changes: 16 additions & 0 deletions action/protocol/staking/bucket_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
"math/big"

"github.com/iotexproject/go-pkgs/hash"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-core/v2/action/protocol"
"github.com/iotexproject/iotex-core/v2/action/protocol/staking/stakingpb"
"github.com/iotexproject/iotex-core/v2/state"
"github.com/iotexproject/iotex-core/v2/systemcontracts"
)

// const
Expand Down Expand Up @@ -91,6 +93,20 @@ func (t *totalAmount) SubBalance(amount *big.Int) error {
return nil
}

// Encode encodes total amount into generic value
func (t *totalAmount) Encode() (systemcontracts.GenericValue, error) {
data, err := t.Serialize()
if err != nil {
return systemcontracts.GenericValue{}, errors.Wrap(err, "failed to serialize total amount")
}
return systemcontracts.GenericValue{PrimaryData: data}, nil
}

// Decode decodes total amount from generic value
func (t *totalAmount) Decode(gv systemcontracts.GenericValue) error {
return t.Deserialize(gv.PrimaryData)
}

// IsDirty returns true if the bucket pool is dirty
func (bp *BucketPool) IsDirty() bool {
return bp.dirty
Expand Down
59 changes: 59 additions & 0 deletions action/protocol/staking/candidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/iotexproject/iotex-core/v2/action"
"github.com/iotexproject/iotex-core/v2/action/protocol/staking/stakingpb"
"github.com/iotexproject/iotex-core/v2/state"
"github.com/iotexproject/iotex-core/v2/systemcontracts"
)

type (
Expand Down Expand Up @@ -194,6 +195,50 @@ func (d *Candidate) GetIdentifier() address.Address {
return d.Identifier
}

// Encode encodes candidate into generic value
func (d *Candidate) Encode() (systemcontracts.GenericValue, error) {
var (
primaryData []byte
secondaryData []byte
err error
value systemcontracts.GenericValue
)
if d.Votes.Sign() > 0 {
secondaryData, err = proto.Marshal(&stakingpb.Candidate{Votes: d.Votes.String()})
if err != nil {
return value, errors.Wrap(err, "failed to marshal candidate votes")
}
}
clone := d.Clone()
clone.Votes = big.NewInt(0)
primaryData, err = clone.Serialize()
if err != nil {
return value, errors.Wrap(err, "failed to serialize candidate")
}
value.PrimaryData = primaryData
value.SecondaryData = secondaryData
return value, nil
}

// Decode decodes candidate from generic value
func (d *Candidate) Decode(gv systemcontracts.GenericValue) error {
if err := d.Deserialize(gv.PrimaryData); err != nil {
return errors.Wrap(err, "failed to deserialize candidate")
}
if len(gv.SecondaryData) > 0 {
votes := &stakingpb.Candidate{}
if err := proto.Unmarshal(gv.SecondaryData, votes); err != nil {
return errors.Wrap(err, "failed to unmarshal candidate votes")
}
vote, ok := new(big.Int).SetString(votes.Votes, 10)
if !ok {
return errors.Wrapf(action.ErrInvalidAmount, "failed to parse candidate votes: %s", votes.Votes)
}
d.Votes = vote
}
return nil
}

func (d *Candidate) toProto() (*stakingpb.Candidate, error) {
if d.Owner == nil || d.Operator == nil || d.Reward == nil ||
len(d.Name) == 0 || d.Votes == nil || d.SelfStake == nil {
Expand Down Expand Up @@ -360,6 +405,20 @@ func (l *CandidateList) Deserialize(buf []byte) error {
return nil
}

// Encode encodes candidate list into generic value
func (l *CandidateList) Encode() (systemcontracts.GenericValue, error) {
data, err := l.Serialize()
if err != nil {
return systemcontracts.GenericValue{}, errors.Wrap(err, "failed to serialize candidate list")
}
return systemcontracts.GenericValue{PrimaryData: data}, nil
}

// Decode decodes candidate list from generic value
func (l *CandidateList) Decode(gv systemcontracts.GenericValue) error {
return l.Deserialize(gv.PrimaryData)
}

func (l CandidateList) toStateCandidateList() (state.CandidateList, error) {
list := make(state.CandidateList, 0, len(l))
for _, c := range l {
Expand Down
10 changes: 7 additions & 3 deletions action/protocol/staking/candidate_statemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ func (csm *candSM) putBucket(bucket *VoteBucket) (uint64, error) {
func (csm *candSM) delBucket(index uint64) error {
_, err := csm.DelState(
protocol.NamespaceOption(_stakingNameSpace),
protocol.KeyOption(bucketKey(index)))
protocol.KeyOption(bucketKey(index)),
protocol.ObjectOption(&VoteBucket{}),
)
return err
}

Expand Down Expand Up @@ -295,7 +297,9 @@ func (csm *candSM) delBucketIndex(addr address.Address, prefix byte, index uint6
if len(bis) == 0 {
_, err = csm.DelState(
protocol.NamespaceOption(_stakingNameSpace),
protocol.KeyOption(key))
protocol.KeyOption(key),
protocol.ObjectOption(&BucketIndices{}),
)
} else {
_, err = csm.PutState(
&bis,
Expand All @@ -319,7 +323,7 @@ func (csm *candSM) putCandBucketIndex(addr address.Address, index uint64) error
}

func (csm *candSM) delCandidate(name address.Address) error {
_, err := csm.DelState(protocol.NamespaceOption(_candidateNameSpace), protocol.KeyOption(name.Bytes()))
_, err := csm.DelState(protocol.NamespaceOption(_candidateNameSpace), protocol.KeyOption(name.Bytes()), protocol.ObjectOption(&Candidate{}))
return err
}

Expand Down
3 changes: 2 additions & 1 deletion action/protocol/staking/candidate_statereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ func (c *candSR) NativeBuckets() ([]*VoteBucket, uint64, error) {
}
return keys, nil
}),
protocol.ObjectOption(&VoteBucket{}),
)
if err != nil {
return nil, height, err
Expand Down Expand Up @@ -297,7 +298,7 @@ func (c *candSR) CandidateByAddress(name address.Address) (*Candidate, uint64, e
}

func (c *candSR) CreateCandidateCenter() (*CandidateCenter, uint64, error) {
height, iter, err := c.States(protocol.NamespaceOption(_candidateNameSpace))
height, iter, err := c.States(protocol.NamespaceOption(_candidateNameSpace), protocol.ObjectOption(&Candidate{}))
var cands CandidateList
switch errors.Cause(err) {
case nil:
Expand Down
15 changes: 15 additions & 0 deletions action/protocol/staking/endorsement.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-core/v2/action/protocol/staking/stakingpb"
"github.com/iotexproject/iotex-core/v2/systemcontracts"
)

// EndorsementStatus
Expand Down Expand Up @@ -84,6 +85,20 @@ func (e *Endorsement) Deserialize(buf []byte) error {
return e.fromProto(pb)
}

// Encode encodes endorsement into generic value
func (e *Endorsement) Encode() (systemcontracts.GenericValue, error) {
data, err := e.Serialize()
if err != nil {
return systemcontracts.GenericValue{}, errors.Wrap(err, "failed to serialize endorsement")
}
return systemcontracts.GenericValue{PrimaryData: data}, nil
}

// Decode decodes endorsement from generic value
func (e *Endorsement) Decode(gv systemcontracts.GenericValue) error {
return e.Deserialize(gv.PrimaryData)
}

func (e *Endorsement) toProto() (*stakingpb.Endorsement, error) {
return &stakingpb.Endorsement{
ExpireHeight: e.ExpireHeight,
Expand Down
2 changes: 1 addition & 1 deletion action/protocol/staking/endorsement_statemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (esm *EndorsementStateManager) Put(bucketIndex uint64, endorse *Endorsement

// Delete deletes the endorsement of a bucket
func (esm *EndorsementStateManager) Delete(bucketIndex uint64) error {
_, err := esm.DelState(protocol.NamespaceOption(_stakingNameSpace), protocol.KeyOption(endorsementKey(bucketIndex)))
_, err := esm.DelState(protocol.NamespaceOption(_stakingNameSpace), protocol.KeyOption(endorsementKey(bucketIndex)), protocol.ObjectOption(&Endorsement{}))
return err
}

Expand Down
4 changes: 2 additions & 2 deletions action/protocol/staking/staking_statereader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func TestStakingStateReader(t *testing.T) {
}
t.Run("readStateBuckets", func(t *testing.T) {
sf, _, stakeSR, ctx, r := prepare(t)
sf.EXPECT().States(gomock.Any(), gomock.Any()).DoAndReturn(func(arg0 ...protocol.StateOption) (uint64, state.Iterator, error) {
sf.EXPECT().States(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(arg0 ...protocol.StateOption) (uint64, state.Iterator, error) {
iter, err := state.NewIterator(keys, states)
r.NoError(err)
return uint64(1), iter, nil
Expand Down Expand Up @@ -195,7 +195,7 @@ func TestStakingStateReader(t *testing.T) {
})
t.Run("readStateBucketsWithEndorsement", func(t *testing.T) {
sf, _, stakeSR, ctx, r := prepare(t)
sf.EXPECT().States(gomock.Any(), gomock.Any()).DoAndReturn(func(arg0 ...protocol.StateOption) (uint64, state.Iterator, error) {
sf.EXPECT().States(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(arg0 ...protocol.StateOption) (uint64, state.Iterator, error) {
iter, err := state.NewIterator(keys, states)
r.NoError(err)
return uint64(1), iter, nil
Expand Down
29 changes: 29 additions & 0 deletions action/protocol/staking/vote_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/iotexproject/iotex-core/v2/action/protocol/staking/stakingpb"
"github.com/iotexproject/iotex-core/v2/blockchain/genesis"
"github.com/iotexproject/iotex-core/v2/pkg/util/byteutil"
"github.com/iotexproject/iotex-core/v2/systemcontracts"
)

type (
Expand Down Expand Up @@ -73,6 +74,20 @@ func (vb *VoteBucket) Deserialize(buf []byte) error {
return vb.fromProto(pb)
}

// Encode encodes VoteBucket into generic value
func (vb *VoteBucket) Encode() (systemcontracts.GenericValue, error) {
data, err := vb.Serialize()
if err != nil {
return systemcontracts.GenericValue{}, errors.Wrap(err, "failed to serialize bucket")
}
return systemcontracts.GenericValue{PrimaryData: data}, nil
}

// Decode decodes VoteBucket from generic value
func (vb *VoteBucket) Decode(gv systemcontracts.GenericValue) error {
return vb.Deserialize(gv.PrimaryData)
}

func (vb *VoteBucket) fromProto(pb *stakingpb.Bucket) error {
vote, ok := new(big.Int).SetString(pb.GetStakedAmount(), 10)
if !ok {
Expand Down Expand Up @@ -209,6 +224,20 @@ func (tc *totalBucketCount) Count() uint64 {
return tc.count
}

// Encode encodes totalBucketCount into generic value
func (tc *totalBucketCount) Encode() (systemcontracts.GenericValue, error) {
data, err := tc.Serialize()
if err != nil {
return systemcontracts.GenericValue{}, errors.Wrap(err, "failed to serialize total bucket count")
}
return systemcontracts.GenericValue{PrimaryData: data}, nil
}

// Decode decodes totalBucketCount from generic value
func (tc *totalBucketCount) Decode(gv systemcontracts.GenericValue) error {
return tc.Deserialize(gv.PrimaryData)
}

func bucketKey(index uint64) []byte {
key := []byte{_bucket}
return append(key, byteutil.Uint64ToBytesBigEndian(index)...)
Expand Down
Loading