2020-04-18 20:09:44 +00:00
|
|
|
// Copyright 2018 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package rawdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common/dbutils"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/types/accounts"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ReadAccount retrieves the version number of the database.
|
|
|
|
func ReadAccount(db DatabaseReader, addrHash common.Hash, acc *accounts.Account) (bool, error) {
|
|
|
|
addrHashBytes := addrHash[:]
|
2020-04-19 19:51:32 +00:00
|
|
|
enc, err := db.Get(dbutils.CurrentStateBucket, addrHashBytes)
|
2020-04-18 20:09:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
root, err := db.Get(dbutils.IntermediateTrieHashBucket, addrHashBytes)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if enc == nil || root == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if err = acc.DecodeForStorage(enc); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
acc.Root = common.BytesToHash(root)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteAccount(db DatabaseWriter, addrHash common.Hash, acc accounts.Account) error {
|
|
|
|
addrHashBytes := addrHash[:]
|
|
|
|
value := make([]byte, acc.EncodingLengthForStorage())
|
|
|
|
acc.EncodeForStorage(value)
|
2020-04-19 19:51:32 +00:00
|
|
|
if err := db.Put(dbutils.CurrentStateBucket, addrHashBytes, value); err != nil {
|
2020-04-18 20:09:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := db.Put(dbutils.IntermediateTrieHashBucket, addrHashBytes, acc.Root.Bytes()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteAccount(db DatabaseDeleter, addrHash common.Hash) error {
|
|
|
|
addrHashBytes := addrHash[:]
|
2020-04-19 19:51:32 +00:00
|
|
|
if err := db.Delete(dbutils.CurrentStateBucket, addrHashBytes); err != nil {
|
2020-04-18 20:09:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := db.Delete(dbutils.IntermediateTrieHashBucket, addrHashBytes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|