Skip to content

Commit

Permalink
gather key make functions in schema
Browse files Browse the repository at this point in the history
todo : make unit tests for key functions
  • Loading branch information
rabbitprincess committed Oct 11, 2023
1 parent acf14b5 commit 4dfbcaf
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 95 deletions.
19 changes: 4 additions & 15 deletions chain/chaindb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package chain

import (
"bytes"
"encoding/binary"
"encoding/gob"
"encoding/json"
"errors"
Expand Down Expand Up @@ -646,7 +645,7 @@ func (cdb *ChainDB) getReceipt(blockHash []byte, blockNo types.BlockNo, idx int3

func (cdb *ChainDB) getReceipts(blockHash []byte, blockNo types.BlockNo,
hardForkConfig *config.HardforkConfig) (*types.Receipts, error) {
data := cdb.store.Get(receiptsKey(blockHash, blockNo))
data := cdb.store.Get(schema.KeyReceipts(blockHash, blockNo))
if len(data) == 0 {
return nil, errors.New("cannot find a receipt")
}
Expand All @@ -662,7 +661,7 @@ func (cdb *ChainDB) getReceipts(blockHash []byte, blockNo types.BlockNo,
}

func (cdb *ChainDB) checkExistReceipts(blockHash []byte, blockNo types.BlockNo) bool {
data := cdb.store.Get(receiptsKey(blockHash, blockNo))
data := cdb.store.Get(schema.KeyReceipts(blockHash, blockNo))
if len(data) == 0 {
return false
}
Expand Down Expand Up @@ -703,23 +702,13 @@ func (cdb *ChainDB) writeReceipts(blockHash []byte, blockNo types.BlockNo, recei
gobEncoder := gob.NewEncoder(&val)
gobEncoder.Encode(receipts)

dbTx.Set(receiptsKey(blockHash, blockNo), val.Bytes())
dbTx.Set(schema.KeyReceipts(blockHash, blockNo), val.Bytes())

dbTx.Commit()
}

func (cdb *ChainDB) deleteReceipts(dbTx *db.Transaction, blockHash []byte, blockNo types.BlockNo) {
(*dbTx).Delete(receiptsKey(blockHash, blockNo))
}

func receiptsKey(blockHash []byte, blockNo types.BlockNo) []byte {
var key bytes.Buffer
key.Write([]byte(schema.ReceiptsPrefix))
key.Write(blockHash)
l := make([]byte, 8)
binary.LittleEndian.PutUint64(l[:], blockNo)
key.Write(l)
return key.Bytes()
(*dbTx).Delete(schema.KeyReceipts(blockHash, blockNo))
}

func (cdb *ChainDB) writeReorgMarker(marker *ReorgMarker) error {
Expand Down
46 changes: 8 additions & 38 deletions chain/chaindbForRaft.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package chain

import (
"bytes"
"encoding/binary"
"encoding/gob"
"errors"

Expand Down Expand Up @@ -87,7 +86,7 @@ func (cdb *ChainDB) ClearWAL() {
defer bulk.DiscardLast()

for i := lastIdx; i >= 1; i-- {
bulk.Delete(getRaftEntryKey(i))
bulk.Delete(schema.KeyRaftEntry(i))
}

bulk.Delete([]byte(schema.RaftEntryLastIdxKey))
Expand Down Expand Up @@ -154,22 +153,6 @@ func (cdb *ChainDB) GetHardState() (*raftpb.HardState, error) {
return state, nil
}

func getRaftEntryKey(idx uint64) []byte {
var key bytes.Buffer
key.Write([]byte(schema.RaftEntryPrefix))
l := make([]byte, 8)
binary.LittleEndian.PutUint64(l[:], idx)
key.Write(l)
return key.Bytes()
}

func getRaftEntryInvertKey(blockHash []byte) []byte {
var key bytes.Buffer
key.Write([]byte(schema.RaftEntryInvertPrefix))
key.Write(blockHash)
return key.Bytes()
}

func (cdb *ChainDB) WriteRaftEntry(ents []*consensus.WalEntry, blocks []*types.Block, ccProposes []*raftpb.ConfChange) error {
var data []byte
var err error
Expand All @@ -189,7 +172,7 @@ func (cdb *ChainDB) WriteRaftEntry(ents []*consensus.WalEntry, blocks []*types.B

for i := ents[0].Index; i <= last; i++ {
// delete ents[0].Index ~ lastIndex of wal
dbTx.Delete(getRaftEntryKey(i))
dbTx.Delete(schema.KeyRaftEntry(i))
}
}

Expand All @@ -209,11 +192,11 @@ func (cdb *ChainDB) WriteRaftEntry(ents []*consensus.WalEntry, blocks []*types.B
}

lastIdx = entry.Index
dbTx.Set(getRaftEntryKey(entry.Index), data)
dbTx.Set(schema.KeyRaftEntry(entry.Index), data)

// invert key to search raft entry corresponding to block hash
if entry.Type == consensus.EntryBlock {
dbTx.Set(getRaftEntryInvertKey(blocks[i].BlockHash()), types.Uint64ToBytes(entry.Index))
dbTx.Set(schema.KeyRaftEntryInvert(blocks[i].BlockHash()), types.Uint64ToBytes(entry.Index))
}

if entry.Type == consensus.EntryConfChange {
Expand Down Expand Up @@ -246,7 +229,7 @@ func (cdb *ChainDB) writeRaftEntryLastIndex(dbTx db.Transaction, lastIdx uint64)
}

func (cdb *ChainDB) GetRaftEntry(idx uint64) (*consensus.WalEntry, error) {
data := cdb.store.Get(getRaftEntryKey(idx))
data := cdb.store.Get(schema.KeyRaftEntry(idx))
if len(data) == 0 {
return nil, ErrNoWalEntry
}
Expand All @@ -268,7 +251,7 @@ func (cdb *ChainDB) GetRaftEntry(idx uint64) (*consensus.WalEntry, error) {
}

func (cdb *ChainDB) GetRaftEntryIndexOfBlock(hash []byte) (uint64, error) {
data := cdb.store.Get(getRaftEntryInvertKey(hash))
data := cdb.store.Get(schema.KeyRaftEntryInvert(hash))
if len(data) == 0 {
return 0, ErrNoWalEntryForBlock
}
Expand Down Expand Up @@ -487,23 +470,12 @@ func (cdb *ChainDB) WriteConfChangeProgress(id uint64, progress *types.ConfChang
return nil
}

func getConfChangeProgressKey(idx uint64) []byte {
var key bytes.Buffer
key.Write([]byte(schema.RaftConfChangeProgressPrefix))
l := make([]byte, 8)
binary.LittleEndian.PutUint64(l[:], idx)
key.Write(l)
return key.Bytes()
}

func (cdb *ChainDB) writeConfChangeProgress(dbTx db.Transaction, id uint64, progress *types.ConfChangeProgress) error {
if id == 0 {
// it's for intial member's for startup
return nil
}

ccKey := getConfChangeProgressKey(id)

// Make CC Data
var data []byte
var err error
Expand All @@ -513,15 +485,13 @@ func (cdb *ChainDB) writeConfChangeProgress(dbTx db.Transaction, id uint64, prog
return err
}

dbTx.Set(ccKey, data)
dbTx.Set(schema.KeyRaftConfChangeProgress(id), data)

return nil
}

func (cdb *ChainDB) GetConfChangeProgress(id uint64) (*types.ConfChangeProgress, error) {
ccKey := getConfChangeProgressKey(id)

data := cdb.store.Get(ccKey)
data := cdb.store.Get(schema.KeyRaftConfChangeProgress(id))
if len(data) == 0 {
return nil, nil
}
Expand Down
4 changes: 2 additions & 2 deletions contract/enterprise/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func enableConf(scs *state.ContractState, key []byte, value bool) (*Conf, error)
}

func getConf(scs *state.ContractState, key []byte) (*Conf, error) {
data, err := scs.GetData(append([]byte(schema.EnterpriseConfPrefix), genKey(key)...))
data, err := scs.GetData(schema.KeyEnterpriseConf(genKey(key)))
if err != nil || data == nil {
return nil, err
}
Expand All @@ -133,7 +133,7 @@ func setConfValues(scs *state.ContractState, key []byte, values []string) (*Conf
}

func setConf(scs *state.ContractState, key []byte, conf *Conf) error {
return scs.SetData(append([]byte(schema.EnterpriseConfPrefix), genKey(key)...), serializeConf(conf))
return scs.SetData(schema.KeyEnterpriseConf(genKey(key)), serializeConf(conf))
}

func serializeConf(c *Conf) []byte {
Expand Down
10 changes: 3 additions & 7 deletions contract/name/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,12 @@ func getOwner(scs *state.ContractState, name []byte, useInitial bool) []byte {
}

func getNameMap(scs *state.ContractState, name []byte, useInitial bool) *NameMap {
lowerCaseName := strings.ToLower(string(name))
key := append([]byte(schema.NamePrefix), lowerCaseName...)
var err error
var ownerdata []byte
if useInitial {
ownerdata, err = scs.GetInitialData(key)
ownerdata, err = scs.GetInitialData(schema.KeyName(name))
} else {
ownerdata, err = scs.GetData(key)
ownerdata, err = scs.GetData(schema.KeyName(name))
}
if err != nil {
return nil
Expand All @@ -167,9 +165,7 @@ func registerOwner(scs *state.ContractState, name, owner, destination []byte) er
}

func setNameMap(scs *state.ContractState, name []byte, n *NameMap) error {
lowerCaseName := strings.ToLower(string(name))
key := append([]byte(schema.NamePrefix), lowerCaseName...)
return scs.SetData(key, serializeNameMap(n))
return scs.SetData(schema.KeyName(name), serializeNameMap(n))
}

func serializeNameMap(n *NameMap) []byte {
Expand Down
11 changes: 3 additions & 8 deletions contract/system/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package system

import (
"math/big"
"strings"

"github.com/aergoio/aergo/v2/internal/schema"
"github.com/aergoio/aergo/v2/state"
Expand Down Expand Up @@ -42,15 +41,11 @@ func InitSystemParams(g dataGetter, bpCount int) {
systemParams = loadParam(g)
}

func genParamKey(id string) []byte {
return []byte(schema.SystemParam + strings.ToUpper(id))
}

func loadParam(g dataGetter) parameters {
ret := map[string]*big.Int{}
for i := sysParamIndex(0); i < sysParamMax; i++ {
id := i.ID()
data, err := g.GetData(genParamKey(id))
data, err := g.GetData(schema.KeyParam([]byte(id)))
if err != nil {
panic("could not load blockchain parameter")
}
Expand All @@ -76,7 +71,7 @@ func (p parameters) setLastParam(proposalID string, value *big.Int) *big.Int {
}

func updateParam(s dataSetter, id string, value *big.Int) (*big.Int, error) {
if err := s.SetData(genParamKey(id), value.Bytes()); err != nil {
if err := s.SetData(schema.KeyParam([]byte(id)), value.Bytes()); err != nil {
return nil, err
}
ret := systemParams.setLastParam(id, value)
Expand Down Expand Up @@ -112,7 +107,7 @@ func GetGasPriceFromState(ar AccountStateReader) *big.Int {
}

func getParamFromState(scs *state.ContractState, id sysParamIndex) *big.Int {
data, err := scs.GetInitialData(genParamKey(id.ID()))
data, err := scs.GetInitialData(schema.KeyParam([]byte(id.ID())))
if err != nil {
panic("could not get blockchain parameter")
}
Expand Down
6 changes: 2 additions & 4 deletions contract/system/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,11 @@ func (c *unstakeCmd) run() (*types.Event, error) {
}

func setStaking(scs *state.ContractState, who []byte, staking *types.Staking) error {
key := append([]byte(schema.SystemStaking), who...)
return scs.SetData(key, serializeStaking(staking))
return scs.SetData(schema.KeyStaking(who), serializeStaking(staking))
}

func getStaking(scs *state.ContractState, who []byte) (*types.Staking, error) {
key := append([]byte(schema.SystemStaking), who...)
data, err := scs.GetData(key)
data, err := scs.GetData(schema.KeyStaking(who))
if err != nil {
return nil, err
}
Expand Down
8 changes: 3 additions & 5 deletions contract/system/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,7 @@ func GetVote(scs *state.ContractState, voter []byte, issue []byte) (*types.Vote,
}

func getVote(scs *state.ContractState, key, voter []byte) (*types.Vote, error) {
dataKey := append(append([]byte(schema.SystemVote), key...), voter...)
data, err := scs.GetData(dataKey)
data, err := scs.GetData(schema.KeyVote(key, voter))
if err != nil {
return nil, err
}
Expand All @@ -317,11 +316,10 @@ func getVote(scs *state.ContractState, key, voter []byte) (*types.Vote, error) {
}

func setVote(scs *state.ContractState, key, voter []byte, vote *types.Vote) error {
dataKey := append(append([]byte(schema.SystemVote), key...), voter...)
if bytes.Equal(key, defaultVoteKey) {
return scs.SetData(dataKey, serializeVote(vote))
return scs.SetData(schema.KeyVote(key, voter), serializeVote(vote))
} else {
return scs.SetData(dataKey, serializeVoteEx(vote))
return scs.SetData(schema.KeyVote(key, voter), serializeVoteEx(vote))
}
}

Expand Down
10 changes: 5 additions & 5 deletions contract/system/voteresult.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ func (vr *VoteResult) Sync() error {
return err
}
}
if err := vr.scs.SetData(append([]byte(schema.SystemVoteTotal), vr.key...), vr.total.Bytes()); err != nil {
if err := vr.scs.SetData(schema.KeyVoteTotal(vr.key), vr.total.Bytes()); err != nil {
return err
}
}
return vr.scs.SetData(append([]byte(schema.SystemVoteSort), vr.key...), serializeVoteList(resultList, vr.ex))
return vr.scs.SetData(schema.KeyVoteSort(vr.key), serializeVoteList(resultList, vr.ex))
}

func (vr *VoteResult) threshold(power *big.Int) bool {
Expand All @@ -144,11 +144,11 @@ func (vr *VoteResult) threshold(power *big.Int) bool {
}

func loadVoteResult(scs *state.ContractState, key []byte) (*VoteResult, error) {
data, err := scs.GetData(append([]byte(schema.SystemVoteSort), key...))
data, err := scs.GetData(schema.KeyVoteSort(key))
if err != nil {
return nil, err
}
total, err := scs.GetData(append([]byte(schema.SystemVoteTotal), key...))
total, err := scs.GetData(schema.KeyVoteTotal(key))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -182,7 +182,7 @@ func InitVoteResult(scs *state.ContractState, voteResult map[string]*big.Int) er
}

func getVoteResult(scs *state.ContractState, key []byte, n int) (*types.VoteList, error) {
data, err := scs.GetData(append([]byte(schema.SystemVoteSort), key...))
data, err := scs.GetData(schema.KeyVoteSort(key))
if err != nil {
return nil, err
}
Expand Down
9 changes: 2 additions & 7 deletions contract/system/vprt.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ func (b *vprStore) write(s dataSetter, i uint8) error {
buf.Write(toVotingPower(e).marshal())
}

return s.SetData(vprKey(i), buf.Bytes())
return s.SetData(schema.KeyVpr(i), buf.Bytes())
}

func (b *vprStore) read(s dataGetter, i uint8) ([]*votingPower, error) {
buf, err := s.GetData(vprKey(i))
buf, err := s.GetData(schema.KeyVpr(i))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -723,11 +723,6 @@ func (v *vpr) pickVotingRewardWinner(seed int64) (types.Address, error) {
return nil, ErrNoVotingRewardWinner
}

func vprKey(i uint8) []byte {
var vk []byte = []byte(schema.SystemVpr)
return append(vk, []byte(fmt.Sprintf("%v", i))...)
}

func toVotingPower(e *list.Element) *votingPower {
return e.Value.(*votingPower)
}
Expand Down
Loading

0 comments on commit 4dfbcaf

Please sign in to comment.