Skip to content
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
7 changes: 7 additions & 0 deletions action/protocol/staking/contractstake_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/iotexproject/iotex-address/address"

"github.com/iotexproject/iotex-core/v2/action"
"github.com/iotexproject/iotex-core/v2/action/protocol"
"github.com/iotexproject/iotex-core/v2/action/protocol/staking/contractstaking"
Expand Down Expand Up @@ -64,6 +65,12 @@ type (
LoadStakeView(context.Context, protocol.StateReader) (ContractStakeView, error)
// CreateEventProcessor creates a new event processor
CreateEventProcessor(context.Context, EventHandler) EventProcessor
// ContractStakingBuckets returns all the contract staking buckets
ContractStakingBuckets() (uint64, map[uint64]*contractstaking.Bucket, error)
// IndexerAt returns the contract staking indexer at a specific height
IndexerAt(protocol.StateReader) ContractStakingIndexer
// BucketReader defines the interface to read buckets
BucketReader
}
// ContractStakingIndexerWithBucketType defines the interface of contract staking reader with bucket type
ContractStakingIndexerWithBucketType interface {
Expand Down
Loading
Loading