Skip to content

Commit

Permalink
tidy state package
Browse files Browse the repository at this point in the history
  • Loading branch information
gokch committed Nov 26, 2023
1 parent 0cdc6cb commit 2f630f4
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 22 deletions.
File renamed without changes.
File renamed without changes.
20 changes: 8 additions & 12 deletions state/statebuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ type entry interface {
Value() interface{}
}

type cached interface {
cache() *stateBuffer
}

type valueEntry struct {
key types.HashID
value interface{}
Expand Down Expand Up @@ -160,7 +156,7 @@ func (buffer *stateBuffer) export() ([][]byte, [][]byte) {
bufs = append(bufs, et)
}
sort.Slice(bufs, func(i, j int) bool {
return -1 == (bufs[i].KeyID().Compare(bufs[j].KeyID()))
return (bufs[i].KeyID().Compare(bufs[j].KeyID())) == -1
})
size := len(bufs)
keys := make([][]byte, size)
Expand Down Expand Up @@ -197,15 +193,15 @@ func (buffer *stateBuffer) stage(txn trie.DbTx) error {
}

func marshal(data interface{}) ([]byte, error) {
switch data.(type) {
switch msg := data.(type) {
case ([]byte):
return data.([]byte), nil
return msg, nil
case (*[]byte):
return *(data.(*[]byte)), nil
return *msg, nil
case (types.ImplMarshal):
return data.(types.ImplMarshal).Marshal()
return msg.Marshal()
case (proto.Message):
return proto.Encode(data.(proto.Message))
return proto.Encode(msg)
}
return nil, nil
}
Expand All @@ -214,9 +210,9 @@ func getHashBytes(data interface{}) []byte {
if data == nil {
return nil
}
switch data.(type) {
switch msg := data.(type) {
case (types.ImplHashBytes):
return data.(types.ImplHashBytes).Hash()
return msg.Hash()
default:
}
buf, err := marshal(data)
Expand Down
16 changes: 8 additions & 8 deletions state/statedata.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ func saveData(store db.DB, key []byte, data interface{}) error {
}
var err error
var raw []byte
switch data.(type) {
switch msg := data.(type) {
case ([]byte):
raw = data.([]byte)
raw = msg
case proto.Message:
raw, err = proto.Encode(data.(proto.Message))
raw, err = proto.Encode(msg)
if err != nil {
return err
}
default:
raw, err = gob.Encode(data)
raw, err = gob.Encode(msg)
if err != nil {
return err
}
Expand All @@ -41,13 +41,13 @@ func loadData(store db.DB, key []byte, data interface{}) error {
return nil
}
var err error
switch data.(type) {
switch msg := data.(type) {
case *[]byte:
*(data).(*[]byte) = raw
*msg = raw
case proto.Message:
err = proto.Decode(raw, data.(proto.Message))
err = proto.Decode(raw, msg)
default:
err = gob.Decode(raw, data)
err = gob.Decode(raw, msg)
}
return err
}
Expand Down
3 changes: 1 addition & 2 deletions state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ type StateDB struct {
cache *storageCache
trie *trie.Trie
store db.DB
batchtx db.Transaction
testmode bool
}

Expand Down Expand Up @@ -195,7 +194,7 @@ func (states *StateDB) getTrieState(id types.AccountID) (*types.State, error) {
if err != nil {
return nil, err
}
if key == nil || len(key) == 0 {
if len(key) == 0 {
return nil, nil
}
return states.loadStateData(key)
Expand Down

0 comments on commit 2f630f4

Please sign in to comment.