Skip to content
Merged
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
2 changes: 1 addition & 1 deletion core/state/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ type Trie interface {

// Commit writes all nodes to the trie's memory database, tracking the internal
// and external (for account tries) references.
Commit(onleaf trie.LeafCallback) (common.Hash, error)
Commit(onleaf trie.LeafCallback) (common.Hash, int, error)

// NodeIterator returns an iterator that returns nodes of the trie. Iteration
// starts at the key after the given start key.
Expand Down
28 changes: 28 additions & 0 deletions core/state/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2021 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 state

import "github.com/tomochain/tomochain/metrics"

var (
accountUpdatedMeter = metrics.NewRegisteredMeter("state/update/account", nil)
storageUpdatedMeter = metrics.NewRegisteredMeter("state/update/storage", nil)
accountDeletedMeter = metrics.NewRegisteredMeter("state/delete/account", nil)
storageDeletedMeter = metrics.NewRegisteredMeter("state/delete/storage", nil)
accountCommittedMeter = metrics.NewRegisteredMeter("state/commit/account", nil)
storageCommittedMeter = metrics.NewRegisteredMeter("state/commit/storage", nil)
)
18 changes: 10 additions & 8 deletions core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,13 @@ func (self *stateObject) updateTrie(db Database) Trie {
delete(self.dirtyStorage, key)
if (value == common.Hash{}) {
self.setError(tr.TryDelete(key[:]))
self.db.StorageDeleted += 1
continue
}
// Encoding []byte cannot fail, ok to ignore the error.
v, _ := rlp.EncodeToBytes(bytes.TrimLeft(value[:], "\x00"))
self.setError(tr.TryUpdate(key[:], v))
self.db.StorageUpdated += 1
}
return tr
}
Expand All @@ -272,22 +274,22 @@ func (self *stateObject) updateRoot(db Database) {

// CommitTrie the storage trie of the object to dwb.
// This updates the trie root.
func (self *stateObject) CommitTrie(db Database) error {
self.updateTrie(db)
if self.dbErr != nil {
return self.dbErr
func (s *stateObject) CommitTrie(db Database) (int, error) {
s.updateTrie(db)
if s.dbErr != nil {
return 0, s.dbErr
}

// Track the amount of time wasted on committing the storage trie
if metrics.EnabledExpensive {
defer func(start time.Time) { self.db.StorageCommits += time.Since(start) }(time.Now())
defer func(start time.Time) { s.db.StorageCommits += time.Since(start) }(time.Now())
}

root, err := self.trie.Commit(nil)
root, committed, err := s.trie.Commit(nil)
if err == nil {
self.data.Root = root
s.data.Root = root
}
return err
return committed, err
}

// AddBalance removes amount from c's balance.
Expand Down
36 changes: 33 additions & 3 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ type StateDB struct {
StorageUpdates time.Duration
StorageCommits time.Duration

AccountUpdated int
StorageUpdated int
AccountDeleted int
StorageDeleted int

lock sync.Mutex
}

Expand Down Expand Up @@ -583,9 +588,11 @@ func (s *StateDB) Finalise(deleteEmptyObjects bool) {
stateObject := s.stateObjects[addr]
if stateObject.suicided || (deleteEmptyObjects && stateObject.empty()) {
s.deleteStateObject(stateObject)
s.AccountDeleted += 1
} else {
stateObject.updateRoot(s.db)
s.updateStateObject(stateObject)
s.AccountUpdated += 1
}
}
// Invalidate journal because reverting across transactions is not allowed.
Expand Down Expand Up @@ -646,34 +653,41 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (root common.Hash, err error)
defer s.clearJournalAndRefund()

// Commit objects to the trie, measuring the elapsed time
var storageCommitted int
for addr, stateObject := range s.stateObjects {
_, isDirty := s.stateObjectsDirty[addr]
switch {
case stateObject.suicided || (isDirty && deleteEmptyObjects && stateObject.empty()):
// If the object has been removed, don't bother syncing it
// and just mark it for deletion in the trie.
s.deleteStateObject(stateObject)
s.AccountDeleted += 1
case isDirty:
// Write any contract code associated with the state object
if stateObject.code != nil && stateObject.dirtyCode {
s.db.TrieDB().InsertBlob(common.BytesToHash(stateObject.CodeHash()), stateObject.code)
stateObject.dirtyCode = false
}
// Write any storage changes in the state object to its storage trie.
if err := stateObject.CommitTrie(s.db); err != nil {
committed, err := stateObject.CommitTrie(s.db)
if err != nil {
return common.Hash{}, err
}
storageCommitted += committed
// Update the object in the main account trie.
s.updateStateObject(stateObject)
s.AccountUpdated += 1
}
delete(s.stateObjectsDirty, addr)
}
// Write the account trie changes, measuring the amount of wasted time
var start time.Time
if metrics.EnabledExpensive {
defer func(start time.Time) { s.AccountCommits += time.Since(start) }(time.Now())
start = time.Now()
}
// Write trie changes.
root, err = s.trie.Commit(func(leaf []byte, parent common.Hash) error {
var accountCommitted int
root, accountCommitted, err = s.trie.Commit(func(leaf []byte, parent common.Hash) error {
var account Account
if err := rlp.DecodeBytes(leaf, &account); err != nil {
return nil
Expand All @@ -687,6 +701,22 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (root common.Hash, err error)
}
return nil
})
if err != nil {
return common.Hash{}, err
}

if metrics.EnabledExpensive {
s.AccountCommits += time.Since(start)

accountUpdatedMeter.Mark(int64(s.AccountUpdated))
storageUpdatedMeter.Mark(int64(s.StorageUpdated))
accountDeletedMeter.Mark(int64(s.AccountDeleted))
storageDeletedMeter.Mark(int64(s.StorageDeleted))
accountCommittedMeter.Mark(int64(accountCommitted))
storageCommittedMeter.Mark(int64(storageCommitted))
s.AccountUpdated, s.AccountDeleted = 0, 0
s.StorageUpdated, s.StorageDeleted = 0, 0
}
return root, err
}

Expand Down
7 changes: 4 additions & 3 deletions light/postprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ package light
import (
"encoding/binary"
"errors"
"github.com/tomochain/tomochain/core/rawdb"
"math/big"
"time"

"github.com/tomochain/tomochain/core/rawdb"

"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/common/bitutil"
"github.com/tomochain/tomochain/core"
Expand Down Expand Up @@ -174,7 +175,7 @@ func (c *ChtIndexerBackend) Process(header *types.Header) {

// Commit implements core.ChainIndexerBackend
func (c *ChtIndexerBackend) Commit() error {
root, err := c.trie.Commit(nil)
root, _, err := c.trie.Commit(nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -293,7 +294,7 @@ func (b *BloomTrieIndexerBackend) Commit() error {
b.trie.Delete(encKey[:])
}
}
root, err := b.trie.Commit(nil)
root, _, err := b.trie.Commit(nil)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions light/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ func (t *odrTrie) TryDelete(key []byte) error {
})
}

func (t *odrTrie) Commit(onleaf trie.LeafCallback) (common.Hash, error) {
func (t *odrTrie) Commit(onleaf trie.LeafCallback) (common.Hash, int, error) {
if t.trie == nil {
return t.id.Root, nil
return t.id.Root, 0, nil
}
return t.trie.Commit(onleaf)
}
Expand Down
2 changes: 1 addition & 1 deletion tomox/tradingstate/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type Trie interface {
TryGetBestRightKeyAndValue() ([]byte, []byte, error)
TryUpdate(key, value []byte) error
TryDelete(key []byte) error
Commit(onleaf trie.LeafCallback) (common.Hash, error)
Commit(onleaf trie.LeafCallback) (common.Hash, int, error)
Hash() common.Hash
NodeIterator(startKey []byte) trie.NodeIterator
GetKey([]byte) []byte // TODO(fjl): remove this when TomoXTrie is removed
Expand Down
7 changes: 4 additions & 3 deletions tomox/tradingstate/state_lendingbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ package tradingstate
import (
"bytes"
"fmt"
"io"
"math/big"

"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/rlp"
"github.com/tomochain/tomochain/trie"
"io"
"math/big"
)

type stateLendingBook struct {
Expand Down Expand Up @@ -172,7 +173,7 @@ func (self *stateLendingBook) updateRoot(db Database) error {
if self.dbErr != nil {
return self.dbErr
}
root, err := self.trie.Commit(nil)
root, _, err := self.trie.Commit(nil)
if err == nil {
self.data.Root = root
}
Expand Down
7 changes: 4 additions & 3 deletions tomox/tradingstate/state_liquidationprice.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ package tradingstate

import (
"fmt"
"io"
"math/big"

"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/log"
"github.com/tomochain/tomochain/rlp"
"github.com/tomochain/tomochain/trie"
"io"
"math/big"
)

type liquidationPriceState struct {
Expand Down Expand Up @@ -130,7 +131,7 @@ func (self *liquidationPriceState) updateRoot(db Database) error {
if self.dbErr != nil {
return self.dbErr
}
root, err := self.trie.Commit(func(leaf []byte, parent common.Hash) error {
root, _, err := self.trie.Commit(func(leaf []byte, parent common.Hash) error {
var orderList orderList
if err := rlp.DecodeBytes(leaf, &orderList); err != nil {
return nil
Expand Down
2 changes: 1 addition & 1 deletion tomox/tradingstate/state_orderList.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (self *stateOrderList) updateRoot(db Database) error {
if self.dbErr != nil {
return self.dbErr
}
root, err := self.trie.Commit(nil)
root, _, err := self.trie.Commit(nil)
if err == nil {
self.data.Root = root
}
Expand Down
13 changes: 7 additions & 6 deletions tomox/tradingstate/state_orderbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ package tradingstate

import (
"fmt"
"io"
"math/big"

"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/log"
"github.com/tomochain/tomochain/rlp"
"io"
"math/big"
)

// stateObject represents an Ethereum orderId which is being modified.
Expand Down Expand Up @@ -240,7 +241,7 @@ func (self *tradingExchanges) CommitAsksTrie(db Database) error {
if self.dbErr != nil {
return self.dbErr
}
root, err := self.asksTrie.Commit(func(leaf []byte, parent common.Hash) error {
root, _, err := self.asksTrie.Commit(func(leaf []byte, parent common.Hash) error {
var orderList orderList
if err := rlp.DecodeBytes(leaf, &orderList); err != nil {
return nil
Expand Down Expand Up @@ -299,7 +300,7 @@ func (self *tradingExchanges) CommitBidsTrie(db Database) error {
if self.dbErr != nil {
return self.dbErr
}
root, err := self.bidsTrie.Commit(func(leaf []byte, parent common.Hash) error {
root, _, err := self.bidsTrie.Commit(func(leaf []byte, parent common.Hash) error {
var orderList orderList
if err := rlp.DecodeBytes(leaf, &orderList); err != nil {
return nil
Expand Down Expand Up @@ -593,7 +594,7 @@ func (self *tradingExchanges) CommitOrdersTrie(db Database) error {
if self.dbErr != nil {
return self.dbErr
}
root, err := self.ordersTrie.Commit(nil)
root, _, err := self.ordersTrie.Commit(nil)
if err == nil {
self.data.OrderRoot = root
}
Expand Down Expand Up @@ -771,7 +772,7 @@ func (self *tradingExchanges) CommitLiquidationPriceTrie(db Database) error {
if self.dbErr != nil {
return self.dbErr
}
root, err := self.liquidationPriceTrie.Commit(func(leaf []byte, parent common.Hash) error {
root, _, err := self.liquidationPriceTrie.Commit(func(leaf []byte, parent common.Hash) error {
var orderList orderList
if err := rlp.DecodeBytes(leaf, &orderList); err != nil {
return nil
Expand Down
2 changes: 1 addition & 1 deletion tomox/tradingstate/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ func (s *TradingStateDB) Commit() (root common.Hash, err error) {
}
}
// Write trie changes.
root, err = s.trie.Commit(func(leaf []byte, parent common.Hash) error {
root, _, err = s.trie.Commit(func(leaf []byte, parent common.Hash) error {
var exchange tradingExchangeObject
if err := rlp.DecodeBytes(leaf, &exchange); err != nil {
return nil
Expand Down
3 changes: 2 additions & 1 deletion tomox/tradingstate/tomox_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package tradingstate

import (
"fmt"

"github.com/tomochain/tomochain/ethdb"
"github.com/tomochain/tomochain/trie"

Expand Down Expand Up @@ -155,7 +156,7 @@ func (t *TomoXTrie) GetKey(shaKey []byte) []byte {
//
// Committing flushes nodes from memory. Subsequent Get calls will load nodes
// from the database.
func (t *TomoXTrie) Commit(onleaf trie.LeafCallback) (root common.Hash, err error) {
func (t *TomoXTrie) Commit(onleaf trie.LeafCallback) (common.Hash, int, error) {
// Write all the pre-images to the actual disk database
if len(t.getSecKeyCache()) > 0 {
t.trie.Db.Lock.Lock()
Expand Down
2 changes: 1 addition & 1 deletion tomoxlending/lendingstate/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type Trie interface {
TryGetBestRightKeyAndValue() ([]byte, []byte, error)
TryUpdate(key, value []byte) error
TryDelete(key []byte) error
Commit(onleaf trie.LeafCallback) (common.Hash, error)
Commit(onleaf trie.LeafCallback) (common.Hash, int, error)
Hash() common.Hash
NodeIterator(startKey []byte) trie.NodeIterator
GetKey([]byte) []byte // TODO(fjl): remove this when TomoXTrie is removed
Expand Down
7 changes: 4 additions & 3 deletions tomoxlending/lendingstate/state_itemList.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ package lendingstate
import (
"bytes"
"fmt"
"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/rlp"
"io"
"math/big"

"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/rlp"
)

type itemListState struct {
Expand Down Expand Up @@ -151,7 +152,7 @@ func (self *itemListState) updateRoot(db Database) error {
if self.dbErr != nil {
return self.dbErr
}
root, err := self.trie.Commit(nil)
root, _, err := self.trie.Commit(nil)
if err == nil {
self.data.Root = root
}
Expand Down
Loading