@@ -19,17 +19,20 @@ package light
19
19
import (
20
20
"bytes"
21
21
"context"
22
+ "errors"
22
23
"math/big"
23
24
24
25
"github.com/ethereum/go-ethereum/common"
25
26
"github.com/ethereum/go-ethereum/core"
26
27
"github.com/ethereum/go-ethereum/core/rawdb"
27
28
"github.com/ethereum/go-ethereum/core/types"
28
- "github.com/ethereum/go-ethereum/crypto"
29
29
"github.com/ethereum/go-ethereum/rlp"
30
30
)
31
31
32
- var sha3Nil = crypto .Keccak256Hash (nil )
32
+ // errNonCanonicalHash is returned if the requested chain data doesn't belong
33
+ // to the canonical chain. ODR can only retrieve the canonical chain data covered
34
+ // by the CHT or Bloom trie for verification.
35
+ var errNonCanonicalHash = errors .New ("hash is not currently canonical" )
33
36
34
37
// GetHeaderByNumber retrieves the canonical block header corresponding to the
35
38
// given number. The returned header is proven by local CHT.
@@ -83,10 +86,13 @@ func GetTd(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64)
83
86
if td != nil {
84
87
return td , nil
85
88
}
86
- _ , err := GetHeaderByNumber (ctx , odr , number )
89
+ header , err := GetHeaderByNumber (ctx , odr , number )
87
90
if err != nil {
88
91
return nil , err
89
92
}
93
+ if header .Hash () != hash {
94
+ return nil , errNonCanonicalHash
95
+ }
90
96
// <hash, number> -> td mapping already be stored in db, get it.
91
97
return rawdb .ReadTd (odr .Database (), hash , number ), nil
92
98
}
@@ -101,6 +107,9 @@ func GetBodyRLP(ctx context.Context, odr OdrBackend, hash common.Hash, number ui
101
107
if err != nil {
102
108
return nil , errNoHeader
103
109
}
110
+ if header .Hash () != hash {
111
+ return nil , errNonCanonicalHash
112
+ }
104
113
r := & BlockRequest {Hash : hash , Number : number , Header : header }
105
114
if err := odr .Retrieve (ctx , r ); err != nil {
106
115
return nil , err
@@ -148,6 +157,9 @@ func GetBlockReceipts(ctx context.Context, odr OdrBackend, hash common.Hash, num
148
157
if err != nil {
149
158
return nil , errNoHeader
150
159
}
160
+ if header .Hash () != hash {
161
+ return nil , errNonCanonicalHash
162
+ }
151
163
r := & ReceiptsRequest {Hash : hash , Number : number , Header : header }
152
164
if err := odr .Retrieve (ctx , r ); err != nil {
153
165
return nil , err
0 commit comments