Skip to content
Open
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
14 changes: 12 additions & 2 deletions action/protocol/managers.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,24 @@ func ErigonStoreOnlyOption() StateOption {
}
}

// ErigonStoreKeyOption sets the erigon store key for call
func ErigonStoreKeyOption(key []byte) StateOption {
return func(cfg *StateConfig) error {
cfg.ErigonStoreKey = make([]byte, len(key))
copy(cfg.ErigonStoreKey, key)
return nil
}
}

type (
// StateConfig is the config for accessing stateDB
StateConfig struct {
Namespace string // namespace used by state's storage
Key []byte
Keys [][]byte
Object any // object used by state's storage
ErigonStoreOnly bool // whether only read/write from/to erigon store
Object any // object used by state's storage
ErigonStoreOnly bool // whether only read/write from/to erigon store
ErigonStoreKey []byte // erigon store key used by state's storage. If nil, use Key field
}

// StateOption sets parameter for access state
Expand Down
3 changes: 2 additions & 1 deletion action/protocol/poll/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ func setCandidates(
}
}
if loadCandidatesLegacy {
_, err := sm.PutState(&candidates, protocol.LegacyKeyOption(candidatesutil.ConstructLegacyKey(height)))
key, org := candidatesutil.ConstructLegacyKeyWithOrg(height)
_, err := sm.PutState(&candidates, protocol.LegacyKeyOption(key), protocol.ErigonStoreKeyOption(org))
return err
}
nextKey := candidatesutil.ConstructKey(candidatesutil.NxtCandidateKey)
Expand Down
21 changes: 12 additions & 9 deletions action/protocol/rewarding/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ const (
var (
_adminKey = []byte("adm")
_fundKey = []byte("fnd")
_blockRewardHistoryKeyPrefix = []byte("brh")
_epochRewardHistoryKeyPrefix = []byte("erh")
_blockRewardHistoryKeyPrefix = state.BlockRewardHistoryKeyPrefix
_epochRewardHistoryKeyPrefix = state.EpochRewardHistoryKeyPrefix
_accountKeyPrefix = []byte("acc")
_exemptKey = []byte("xpt")
errInvalidEpoch = errors.New("invalid start/end epoch number")
Expand All @@ -61,7 +61,7 @@ func NewProtocol(cfg genesis.Rewarding) *Protocol {
log.L().Panic("failed to validate foundation bonus extension", zap.Error(err))
}
return &Protocol{
keyPrefix: h[:],
keyPrefix: state.RewardingKeyPrefix[:],
addr: addr,
cfg: cfg,
}
Expand Down Expand Up @@ -331,8 +331,9 @@ func (p *Protocol) stateCheckLegacy(ctx context.Context, sm protocol.StateReader
}

func (p *Protocol) stateV1(sm protocol.StateReader, key []byte, value interface{}) (uint64, error) {
keyHash := hash.Hash160b(append(p.keyPrefix, key...))
return sm.State(value, protocol.LegacyKeyOption(keyHash))
orgKey := append(p.keyPrefix, key...)
keyHash := hash.Hash160b(orgKey)
return sm.State(value, protocol.LegacyKeyOption(keyHash), protocol.ErigonStoreKeyOption(orgKey))
}

func (p *Protocol) stateV2(sm protocol.StateReader, key []byte, value interface{}) (uint64, error) {
Expand All @@ -348,8 +349,9 @@ func (p *Protocol) putState(ctx context.Context, sm protocol.StateManager, key [
}

func (p *Protocol) putStateV1(sm protocol.StateManager, key []byte, value interface{}) error {
keyHash := hash.Hash160b(append(p.keyPrefix, key...))
_, err := sm.PutState(value, protocol.LegacyKeyOption(keyHash))
orgKey := append(p.keyPrefix, key...)
keyHash := hash.Hash160b(orgKey)
_, err := sm.PutState(value, protocol.LegacyKeyOption(keyHash), protocol.ErigonStoreKeyOption(orgKey))
return err
}

Expand All @@ -360,8 +362,9 @@ func (p *Protocol) putStateV2(sm protocol.StateManager, key []byte, value interf
}

func (p *Protocol) deleteStateV1(sm protocol.StateManager, key []byte, obj any) error {
keyHash := hash.Hash160b(append(p.keyPrefix, key...))
_, err := sm.DelState(protocol.LegacyKeyOption(keyHash), protocol.ObjectOption(obj))
orgKey := append(p.keyPrefix, key...)
keyHash := hash.Hash160b(orgKey)
_, err := sm.DelState(protocol.LegacyKeyOption(keyHash), protocol.ObjectOption(obj), protocol.ErigonStoreKeyOption(orgKey))
if errors.Cause(err) == state.ErrStateNotExist {
// don't care if not exist
return nil
Expand Down
21 changes: 16 additions & 5 deletions action/protocol/rewarding/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,29 @@ func (b rewardHistory) Serialize() ([]byte, error) {
// Deserialize deserializes bytes into reward history state
func (b *rewardHistory) Deserialize(data []byte) error { return nil }

func (b *rewardHistory) Encode() (systemcontracts.GenericValue, error) {
data, err := b.Serialize()
func (b *rewardHistory) Encode(suffix []byte) (systemcontracts.GenericValue, error) {
height := enc.MachineEndian.Uint64(suffix)
data, err := proto.Marshal(&rewardingpb.RewardHistory{
Height: height,
})
if err != nil {
return systemcontracts.GenericValue{}, err
}
return systemcontracts.GenericValue{
AuxiliaryData: data,
PrimaryData: data,
}, nil
}

func (b *rewardHistory) Decode(v systemcontracts.GenericValue) error {
return b.Deserialize(v.AuxiliaryData)
func (b *rewardHistory) Decode(suffix []byte, v systemcontracts.GenericValue) error {
rh := &rewardingpb.RewardHistory{}
if err := proto.Unmarshal(v.PrimaryData, rh); err != nil {
return err
}
height := enc.MachineEndian.Uint64(suffix)
if rh.Height != height {
return errors.Wrapf(state.ErrStateNotExist, "expected height %d, got %d", height, rh.Height)
}
return nil
}

// rewardAccount stores the unclaimed balance of an account
Expand Down
Loading