-
Notifications
You must be signed in to change notification settings - Fork 100
/
odr_provable.go
65 lines (53 loc) · 2.05 KB
/
odr_provable.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* @file
* @copyright defined in go-seele/LICENSE
*/
package light
import (
"github.com/seeleteam/go-seele/api"
"github.com/seeleteam/go-seele/common"
"github.com/seeleteam/go-seele/common/errors"
"github.com/seeleteam/go-seele/core/store"
"github.com/seeleteam/go-seele/core/types"
"github.com/seeleteam/go-seele/trie"
)
// OdrProvableResponse represents all provable ODR response.
type OdrProvableResponse struct {
OdrItem
BlockIndex *api.BlockIndex `rlp:"nil"`
Proof []proofNode
}
// proveHeader proves the response is valid with the specified blockchain store,
// and returns the corresponding block heaer in canonical chain. If the retrieved
// block index is nil, then return nil block header.
func (response *OdrProvableResponse) proveHeader(bcStore store.BlockchainStore) (*types.BlockHeader, error) {
if response.BlockIndex == nil {
return nil, nil
}
header, err := bcStore.GetBlockHeader(response.BlockIndex.BlockHash)
if err != nil {
return nil, errors.NewStackedErrorf(err, "failed to get block header by hash %v", response.BlockIndex.BlockHash)
}
canonicalHash, err := bcStore.GetBlockHash(response.BlockIndex.BlockHeight)
if err != nil {
return nil, errors.NewStackedErrorf(err, "failed to get block hash by height %v", response.BlockIndex.BlockHeight)
}
if !canonicalHash.Equal(response.BlockIndex.BlockHash) {
return nil, types.ErrBlockHashMismatch
}
return header, nil
}
// proveMerkleTrie proves the merkle trie in the response with specified root and key.
// If proved, decode the retrieved ODR object to obj (pointer type) from the value
// of leaf node in merkle proof.
func (response *OdrProvableResponse) proveMerkleTrie(root common.Hash, key []byte, obj interface{}) error {
proof := arrayToMap(response.Proof)
value, err := trie.VerifyProof(root, key, proof)
if err != nil {
return errors.NewStackedError(err, "failed to verify the merkle trie proof")
}
if err = common.Deserialize(value, obj); err != nil {
return errors.NewStackedError(err, "failed to decode the value of leaf node in merkle proof")
}
return nil
}