core: use reflect.TypeFor (#32320)

https://github.com/golang/go/issues/60088
This commit is contained in:
cui 2025-08-07 20:53:36 +08:00 committed by GitHub
parent 2e3971aed1
commit f9f85d0227
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 6 additions and 7 deletions

View file

@ -241,9 +241,8 @@ func checksumToBytes(hash uint32) [4]byte {
// them, one for the block number based forks and the second for the timestamps.
func gatherForks(config *params.ChainConfig, genesis uint64) ([]uint64, []uint64) {
// Gather all the fork block numbers via reflection
kind := reflect.TypeOf(params.ChainConfig{})
kind := reflect.TypeFor[params.ChainConfig]()
conf := reflect.ValueOf(config).Elem()
x := uint64(0)
var (
forksByBlock []uint64
forksByTime []uint64
@ -258,12 +257,12 @@ func gatherForks(config *params.ChainConfig, genesis uint64) ([]uint64, []uint64
}
// Extract the fork rule block number or timestamp and aggregate it
if field.Type == reflect.TypeOf(&x) {
if field.Type == reflect.TypeFor[*uint64]() {
if rule := conf.Field(i).Interface().(*uint64); rule != nil {
forksByTime = append(forksByTime, *rule)
}
}
if field.Type == reflect.TypeOf(new(big.Int)) {
if field.Type == reflect.TypeFor[*big.Int]() {
if rule := conf.Field(i).Interface().(*big.Int); rule != nil {
forksByBlock = append(forksByBlock, rule.Uint64())
}

View file

@ -293,7 +293,7 @@ func newTracerAllHooks() *tracerAllHooks {
t := &tracerAllHooks{hooksCalled: make(map[string]bool)}
// Initialize all hooks to false. We will use this to
// get total count of hooks.
hooksType := reflect.TypeOf((*Hooks)(nil)).Elem()
hooksType := reflect.TypeFor[Hooks]()
for i := 0; i < hooksType.NumField(); i++ {
t.hooksCalled[hooksType.Field(i).Name] = false
}

View file

@ -128,7 +128,7 @@ func (h *Header) Hash() common.Hash {
return rlpHash(h)
}
var headerSize = common.StorageSize(reflect.TypeOf(Header{}).Size())
var headerSize = common.StorageSize(reflect.TypeFor[Header]().Size())
// Size returns the approximate memory used by all internal contents. It is used
// to approximate and limit the memory consumption of various caches.

View file

@ -49,7 +49,7 @@ type Withdrawals []*Withdrawal
// Len returns the length of s.
func (s Withdrawals) Len() int { return len(s) }
var withdrawalSize = int(reflect.TypeOf(Withdrawal{}).Size())
var withdrawalSize = int(reflect.TypeFor[Withdrawal]().Size())
func (s Withdrawals) Size() int {
return withdrawalSize * len(s)