internal/era: fix rlp decode of header and correctly read total difficulty

This commit is contained in:
lightclient 2023-06-08 10:11:21 +02:00
parent 67defc417c
commit e9cb2074d3
No known key found for this signature in database
GPG key ID: 75C916AFEE20183E
2 changed files with 5 additions and 4 deletions

View file

@ -181,7 +181,7 @@ func (e *Era) InitialTD() (*big.Int, error) {
if r, n, err = newSnappyReader(e.s, TypeCompressedHeader, off); err != nil { if r, n, err = newSnappyReader(e.s, TypeCompressedHeader, off); err != nil {
return nil, err return nil, err
} }
if err := rlp.Decode(r, header); err != nil { if err := rlp.Decode(r, &header); err != nil {
return nil, err return nil, err
} }
off += n off += n
@ -199,7 +199,8 @@ func (e *Era) InitialTD() (*big.Int, error) {
if r, _, err = e.s.ReaderAt(TypeTotalDifficulty, off); err != nil { if r, _, err = e.s.ReaderAt(TypeTotalDifficulty, off); err != nil {
return nil, err return nil, err
} }
if err := rlp.Decode(r, rawTd); err != nil { rawTd, err = io.ReadAll(r)
if err != nil {
return nil, err return nil, err
} }
td := new(big.Int).SetBytes(reverseOrder(rawTd)) td := new(big.Int).SetBytes(reverseOrder(rawTd))

View file

@ -103,8 +103,8 @@ func (it *Iterator) BlockAndReceipts() (*types.Block, types.Receipts, error) {
// TotalDifficulty returns the total difficulty for the iterator's current // TotalDifficulty returns the total difficulty for the iterator's current
// position. // position.
func (it *Iterator) TotalDifficulty() (*big.Int, error) { func (it *Iterator) TotalDifficulty() (*big.Int, error) {
var td []byte td, err := io.ReadAll(it.inner.TotalDifficulty)
if err := rlp.Decode(it.inner.TotalDifficulty, td); err != nil { if err != nil {
return nil, err return nil, err
} }
return new(big.Int).SetBytes(reverseOrder(td)), nil return new(big.Int).SetBytes(reverseOrder(td)), nil