Merge branch 'ethereum:master' into portal

This commit is contained in:
Chen Kai 2024-04-03 10:37:51 +08:00 committed by GitHub
commit bc7987bd91
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 40 additions and 42 deletions

View file

@ -272,8 +272,17 @@ func runCmd(ctx *cli.Context) error {
output, leftOverGas, stats, err := timedExec(bench, execFunc) output, leftOverGas, stats, err := timedExec(bench, execFunc)
if ctx.Bool(DumpFlag.Name) { if ctx.Bool(DumpFlag.Name) {
statedb.Commit(genesisConfig.Number, true) root, err := statedb.Commit(genesisConfig.Number, true)
fmt.Println(string(statedb.Dump(nil))) if err != nil {
fmt.Printf("Failed to commit changes %v\n", err)
return err
}
dumpdb, err := state.New(root, sdb, nil)
if err != nil {
fmt.Printf("Failed to open statedb %v\n", err)
return err
}
fmt.Println(string(dumpdb.Dump(nil)))
} }
if ctx.Bool(DebugFlag.Name) { if ctx.Bool(DebugFlag.Name) {

View file

@ -115,9 +115,7 @@ func (c *BasicLRU[K, V]) Peek(key K) (value V, ok bool) {
// Purge empties the cache. // Purge empties the cache.
func (c *BasicLRU[K, V]) Purge() { func (c *BasicLRU[K, V]) Purge() {
c.list.init() c.list.init()
for k := range c.items { clear(c.items)
delete(c.items, k)
}
} }
// Remove drops an item from the cache. Returns true if the key was present in cache. // Remove drops an item from the cache. Returns true if the key was present in cache.

View file

@ -17,6 +17,8 @@
package state package state
import ( import (
"maps"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
) )
@ -57,16 +59,10 @@ func newAccessList() *accessList {
// Copy creates an independent copy of an accessList. // Copy creates an independent copy of an accessList.
func (a *accessList) Copy() *accessList { func (a *accessList) Copy() *accessList {
cp := newAccessList() cp := newAccessList()
for k, v := range a.addresses { cp.addresses = maps.Clone(a.addresses)
cp.addresses[k] = v
}
cp.slots = make([]map[common.Hash]struct{}, len(a.slots)) cp.slots = make([]map[common.Hash]struct{}, len(a.slots))
for i, slotMap := range a.slots { for i, slotMap := range a.slots {
newSlotmap := make(map[common.Hash]struct{}, len(slotMap)) cp.slots[i] = maps.Clone(slotMap)
for k := range slotMap {
newSlotmap[k] = struct{}{}
}
cp.slots[i] = newSlotmap
} }
return cp return cp
} }

View file

@ -20,6 +20,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"maps"
"time" "time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -47,11 +48,7 @@ func (s Storage) String() (str string) {
} }
func (s Storage) Copy() Storage { func (s Storage) Copy() Storage {
cpy := make(Storage, len(s)) return maps.Clone(s)
for key, value := range s {
cpy[key] = value
}
return cpy
} }
// stateObject represents an Ethereum account which is being modified. // stateObject represents an Ethereum account which is being modified.

View file

@ -19,6 +19,7 @@ package state
import ( import (
"fmt" "fmt"
"maps"
"math/big" "math/big"
"slices" "slices"
"sort" "sort"
@ -750,9 +751,8 @@ func (s *StateDB) Copy() *StateDB {
state.stateObjectsDirty[addr] = struct{}{} state.stateObjectsDirty[addr] = struct{}{}
} }
// Deep copy the destruction markers. // Deep copy the destruction markers.
for addr, value := range s.stateObjectsDestruct { state.stateObjectsDestruct = maps.Clone(s.stateObjectsDestruct)
state.stateObjectsDestruct[addr] = value
}
// Deep copy the state changes made in the scope of block // Deep copy the state changes made in the scope of block
// along with their original values. // along with their original values.
state.accounts = copySet(s.accounts) state.accounts = copySet(s.accounts)
@ -770,9 +770,7 @@ func (s *StateDB) Copy() *StateDB {
state.logs[hash] = cpy state.logs[hash] = cpy
} }
// Deep copy the preimages occurred in the scope of block // Deep copy the preimages occurred in the scope of block
for hash, preimage := range s.preimages { state.preimages = maps.Clone(s.preimages)
state.preimages[hash] = preimage
}
// Do we need to copy the access list and transient storage? // Do we need to copy the access list and transient storage?
// In practice: No. At the start of a transaction, these two lists are empty. // In practice: No. At the start of a transaction, these two lists are empty.
// In practice, we only ever copy state _between_ transactions/blocks, never // In practice, we only ever copy state _between_ transactions/blocks, never

View file

@ -20,6 +20,7 @@ import (
"errors" "errors"
"math/big" "math/big"
"math/rand" "math/rand"
"slices"
"testing" "testing"
"time" "time"
@ -1823,12 +1824,12 @@ func testTransactionFetcher(t *testing.T, tt txFetcherTest) {
continue continue
} }
for _, hash := range hashes { for _, hash := range hashes {
if !containsHash(request.hashes, hash) { if !slices.Contains(request.hashes, hash) {
t.Errorf("step %d, peer %s: hash %x missing from requests", i, peer, hash) t.Errorf("step %d, peer %s: hash %x missing from requests", i, peer, hash)
} }
} }
for _, hash := range request.hashes { for _, hash := range request.hashes {
if !containsHash(hashes, hash) { if !slices.Contains(hashes, hash) {
t.Errorf("step %d, peer %s: hash %x extra in requests", i, peer, hash) t.Errorf("step %d, peer %s: hash %x extra in requests", i, peer, hash)
} }
} }
@ -1850,7 +1851,7 @@ func testTransactionFetcher(t *testing.T, tt txFetcherTest) {
for hash := range fetcher.fetching { for hash := range fetcher.fetching {
var found bool var found bool
for _, req := range fetcher.requests { for _, req := range fetcher.requests {
if containsHash(req.hashes, hash) { if slices.Contains(req.hashes, hash) {
found = true found = true
break break
} }
@ -1891,12 +1892,12 @@ func testTransactionFetcher(t *testing.T, tt txFetcherTest) {
continue continue
} }
for _, hash := range hashes { for _, hash := range hashes {
if !containsHash(request.hashes, hash) { if !slices.Contains(request.hashes, hash) {
t.Errorf("step %d, peer %s: hash %x missing from requests", i, peer, hash) t.Errorf("step %d, peer %s: hash %x missing from requests", i, peer, hash)
} }
} }
for _, hash := range request.hashes { for _, hash := range request.hashes {
if !containsHash(hashes, hash) { if !slices.Contains(hashes, hash) {
t.Errorf("step %d, peer %s: hash %x extra in requests", i, peer, hash) t.Errorf("step %d, peer %s: hash %x extra in requests", i, peer, hash)
} }
} }
@ -1909,7 +1910,7 @@ func testTransactionFetcher(t *testing.T, tt txFetcherTest) {
for _, ann := range announces { for _, ann := range announces {
var found bool var found bool
for _, hs := range step.fetching { for _, hs := range step.fetching {
if containsHash(hs, ann.hash) { if slices.Contains(hs, ann.hash) {
found = true found = true
break break
} }
@ -1925,7 +1926,7 @@ func testTransactionFetcher(t *testing.T, tt txFetcherTest) {
} }
} }
for hash := range fetcher.announced { for hash := range fetcher.announced {
if !containsHash(queued, hash) { if !slices.Contains(queued, hash) {
t.Errorf("step %d: hash %x extra in announced", i, hash) t.Errorf("step %d: hash %x extra in announced", i, hash)
} }
} }
@ -1984,16 +1985,6 @@ func containsHashInAnnounces(slice []announce, hash common.Hash) bool {
return false return false
} }
// containsHash returns whether a hash is contained within a hash slice.
func containsHash(slice []common.Hash, hash common.Hash) bool {
for _, have := range slice {
if have == hash {
return true
}
}
return false
}
// Tests that a transaction is forgotten after the timeout. // Tests that a transaction is forgotten after the timeout.
func TestTransactionForgotten(t *testing.T) { func TestTransactionForgotten(t *testing.T) {
fetcher := NewTxFetcher( fetcher := NewTxFetcher(

View file

@ -37,7 +37,9 @@ const (
) )
var ( var (
activePeerGauge metrics.Gauge = metrics.NilGauge{} activePeerGauge metrics.Gauge = metrics.NilGauge{}
activeInboundPeerGauge metrics.Gauge = metrics.NilGauge{}
activeOutboundPeerGauge metrics.Gauge = metrics.NilGauge{}
ingressTrafficMeter = metrics.NewRegisteredMeter("p2p/ingress", nil) ingressTrafficMeter = metrics.NewRegisteredMeter("p2p/ingress", nil)
egressTrafficMeter = metrics.NewRegisteredMeter("p2p/egress", nil) egressTrafficMeter = metrics.NewRegisteredMeter("p2p/egress", nil)
@ -65,6 +67,8 @@ func init() {
} }
activePeerGauge = metrics.NewRegisteredGauge("p2p/peers", nil) activePeerGauge = metrics.NewRegisteredGauge("p2p/peers", nil)
activeInboundPeerGauge = metrics.NewRegisteredGauge("p2p/peers/inbound", nil)
activeOutboundPeerGauge = metrics.NewRegisteredGauge("p2p/peers/outbound", nil)
serveMeter = metrics.NewRegisteredMeter("p2p/serves", nil) serveMeter = metrics.NewRegisteredMeter("p2p/serves", nil)
serveSuccessMeter = metrics.NewRegisteredMeter("p2p/serves/success", nil) serveSuccessMeter = metrics.NewRegisteredMeter("p2p/serves/success", nil)
dialMeter = metrics.NewRegisteredMeter("p2p/dials", nil) dialMeter = metrics.NewRegisteredMeter("p2p/dials", nil)

View file

@ -771,8 +771,10 @@ running:
if p.Inbound() { if p.Inbound() {
inboundCount++ inboundCount++
serveSuccessMeter.Mark(1) serveSuccessMeter.Mark(1)
activeInboundPeerGauge.Inc(1)
} else { } else {
dialSuccessMeter.Mark(1) dialSuccessMeter.Mark(1)
activeOutboundPeerGauge.Inc(1)
} }
activePeerGauge.Inc(1) activePeerGauge.Inc(1)
} }
@ -786,6 +788,9 @@ running:
srv.dialsched.peerRemoved(pd.rw) srv.dialsched.peerRemoved(pd.rw)
if pd.Inbound() { if pd.Inbound() {
inboundCount-- inboundCount--
activeInboundPeerGauge.Dec(1)
} else {
activeOutboundPeerGauge.Dec(1)
} }
activePeerGauge.Dec(1) activePeerGauge.Dec(1)
} }