internal/era: refactor to use slices.Reverse

This commit is contained in:
cuiweixie 2025-08-12 10:32:19 +08:00
parent cbbf686ecc
commit a3b822d7f2
4 changed files with 11 additions and 12 deletions

View file

@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"math/big"
"slices"
"github.com/ethereum/go-ethereum/common"
ssz "github.com/ferranbt/fastssz"
@ -78,14 +79,6 @@ func (h *headerRecord) HashTreeRootWith(hh ssz.HashWalker) (err error) {
// bigToBytes32 converts a big.Int into a little-endian 32-byte array.
func bigToBytes32(n *big.Int) (b [32]byte) {
n.FillBytes(b[:])
reverseOrder(b[:])
slices.Reverse(b[:])
return
}
// reverseOrder reverses the byte order of a slice.
func reverseOrder(b []byte) []byte {
for i := 0; i < 16; i++ {
b[i], b[32-i-1] = b[32-i-1], b[i]
}
return b
}

View file

@ -23,6 +23,7 @@ import (
"math/big"
"os"
"path"
"slices"
"strconv"
"strings"
"sync"
@ -244,7 +245,8 @@ func (e *Era) InitialTD() (*big.Int, error) {
if err != nil {
return nil, err
}
td := new(big.Int).SetBytes(reverseOrder(rawTd))
slices.Reverse(rawTd)
td := new(big.Int).SetBytes(rawTd)
return td.Sub(td, header.Difficulty), nil
}

View file

@ -22,6 +22,7 @@ import (
"io"
"math/big"
"os"
"slices"
"testing"
"github.com/ethereum/go-ethereum/common"
@ -136,7 +137,8 @@ func TestEra1Builder(t *testing.T) {
if err != nil {
t.Fatalf("error reading td: %v", err)
}
td := new(big.Int).SetBytes(reverseOrder(rawTd))
slices.Reverse(rawTd)
td := new(big.Int).SetBytes(rawTd)
if td.Cmp(chain.tds[i]) != 0 {
t.Fatalf("mismatched tds: want %s, got %s", chain.tds[i], td)
}

View file

@ -20,6 +20,7 @@ import (
"errors"
"io"
"math/big"
"slices"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
@ -107,7 +108,8 @@ func (it *Iterator) TotalDifficulty() (*big.Int, error) {
if err != nil {
return nil, err
}
return new(big.Int).SetBytes(reverseOrder(td)), nil
slices.Reverse(td)
return new(big.Int).SetBytes(td), nil
}
// RawIterator reads an RLP-encode Era1 entries.