Skip to content

Commit bf552cb

Browse files
authored
erigon store optimization (#4743)
1 parent 4382ee2 commit bf552cb

File tree

20 files changed

+677
-266
lines changed

20 files changed

+677
-266
lines changed

action/protocol/managers.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,24 @@ func ErigonStoreOnlyOption() StateOption {
7070
}
7171
}
7272

73+
// ErigonStoreKeyOption sets the erigon store key for call
74+
func ErigonStoreKeyOption(key []byte) StateOption {
75+
return func(cfg *StateConfig) error {
76+
cfg.ErigonStoreKey = make([]byte, len(key))
77+
copy(cfg.ErigonStoreKey, key)
78+
return nil
79+
}
80+
}
81+
7382
type (
7483
// StateConfig is the config for accessing stateDB
7584
StateConfig struct {
7685
Namespace string // namespace used by state's storage
7786
Key []byte
7887
Keys [][]byte
79-
Object any // object used by state's storage
80-
ErigonStoreOnly bool // whether only read/write from/to erigon store
88+
Object any // object used by state's storage
89+
ErigonStoreOnly bool // whether only read/write from/to erigon store
90+
ErigonStoreKey []byte // erigon store key used by state's storage. If nil, use Key field
8191
}
8292

8393
// StateOption sets parameter for access state

action/protocol/poll/util.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ func setCandidates(
213213
}
214214
}
215215
if loadCandidatesLegacy {
216-
_, err := sm.PutState(&candidates, protocol.LegacyKeyOption(candidatesutil.ConstructLegacyKey(height)))
216+
key, org := candidatesutil.ConstructLegacyKeyWithOrg(height)
217+
_, err := sm.PutState(&candidates, protocol.LegacyKeyOption(key), protocol.ErigonStoreKeyOption(org))
217218
return err
218219
}
219220
nextKey := candidatesutil.ConstructKey(candidatesutil.NxtCandidateKey)

action/protocol/rewarding/protocol.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ const (
3434
var (
3535
_adminKey = []byte("adm")
3636
_fundKey = []byte("fnd")
37-
_blockRewardHistoryKeyPrefix = []byte("brh")
38-
_epochRewardHistoryKeyPrefix = []byte("erh")
37+
_blockRewardHistoryKeyPrefix = state.BlockRewardHistoryKeyPrefix
38+
_epochRewardHistoryKeyPrefix = state.EpochRewardHistoryKeyPrefix
3939
_accountKeyPrefix = []byte("acc")
4040
_exemptKey = []byte("xpt")
4141
errInvalidEpoch = errors.New("invalid start/end epoch number")
@@ -61,7 +61,7 @@ func NewProtocol(cfg genesis.Rewarding) *Protocol {
6161
log.L().Panic("failed to validate foundation bonus extension", zap.Error(err))
6262
}
6363
return &Protocol{
64-
keyPrefix: h[:],
64+
keyPrefix: state.RewardingKeyPrefix[:],
6565
addr: addr,
6666
cfg: cfg,
6767
}
@@ -331,8 +331,9 @@ func (p *Protocol) stateCheckLegacy(ctx context.Context, sm protocol.StateReader
331331
}
332332

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

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

350351
func (p *Protocol) putStateV1(sm protocol.StateManager, key []byte, value interface{}) error {
351-
keyHash := hash.Hash160b(append(p.keyPrefix, key...))
352-
_, err := sm.PutState(value, protocol.LegacyKeyOption(keyHash))
352+
orgKey := append(p.keyPrefix, key...)
353+
keyHash := hash.Hash160b(orgKey)
354+
_, err := sm.PutState(value, protocol.LegacyKeyOption(keyHash), protocol.ErigonStoreKeyOption(orgKey))
353355
return err
354356
}
355357

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

362364
func (p *Protocol) deleteStateV1(sm protocol.StateManager, key []byte, obj any) error {
363-
keyHash := hash.Hash160b(append(p.keyPrefix, key...))
364-
_, err := sm.DelState(protocol.LegacyKeyOption(keyHash), protocol.ObjectOption(obj))
365+
orgKey := append(p.keyPrefix, key...)
366+
keyHash := hash.Hash160b(orgKey)
367+
_, err := sm.DelState(protocol.LegacyKeyOption(keyHash), protocol.ObjectOption(obj), protocol.ErigonStoreKeyOption(orgKey))
365368
if errors.Cause(err) == state.ErrStateNotExist {
366369
// don't care if not exist
367370
return nil

action/protocol/rewarding/reward.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,29 @@ func (b rewardHistory) Serialize() ([]byte, error) {
4141
// Deserialize deserializes bytes into reward history state
4242
func (b *rewardHistory) Deserialize(data []byte) error { return nil }
4343

44-
func (b *rewardHistory) Encode() (systemcontracts.GenericValue, error) {
45-
data, err := b.Serialize()
44+
func (b *rewardHistory) Encode(suffix []byte) (systemcontracts.GenericValue, error) {
45+
height := enc.MachineEndian.Uint64(suffix)
46+
data, err := proto.Marshal(&rewardingpb.RewardHistory{
47+
Height: height,
48+
})
4649
if err != nil {
4750
return systemcontracts.GenericValue{}, err
4851
}
4952
return systemcontracts.GenericValue{
50-
AuxiliaryData: data,
53+
PrimaryData: data,
5154
}, nil
5255
}
5356

54-
func (b *rewardHistory) Decode(v systemcontracts.GenericValue) error {
55-
return b.Deserialize(v.AuxiliaryData)
57+
func (b *rewardHistory) Decode(suffix []byte, v systemcontracts.GenericValue) error {
58+
rh := &rewardingpb.RewardHistory{}
59+
if err := proto.Unmarshal(v.PrimaryData, rh); err != nil {
60+
return err
61+
}
62+
height := enc.MachineEndian.Uint64(suffix)
63+
if rh.Height != height {
64+
return errors.Wrapf(state.ErrStateNotExist, "expected height %d, got %d", height, rh.Height)
65+
}
66+
return nil
5667
}
5768

5869
// rewardAccount stores the unclaimed balance of an account

0 commit comments

Comments
 (0)