-
Notifications
You must be signed in to change notification settings - Fork 59
/
client.go
76 lines (65 loc) · 1.92 KB
/
client.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
66
67
68
69
70
71
72
73
74
75
76
package testing
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum/common"
gethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/hyperledger-labs/yui-ibc-solidity/pkg/chains"
"github.com/hyperledger-labs/yui-ibc-solidity/pkg/client"
)
type LightClient struct {
client *client.ETHClient
consensusType chains.ConsensusType
}
func NewLightClient(cl *client.ETHClient, consensusType chains.ConsensusType) *LightClient {
return &LightClient{client: cl, consensusType: consensusType}
}
func (lc LightClient) GenerateInputData(ctx context.Context, address common.Address, storageKeys [][]byte, bn *big.Int) (*LightClientInputData, error) {
var state LightClientInputData
block, err := lc.client.BlockByNumber(ctx, bn)
if err != nil {
return nil, err
}
proof, err := lc.client.GetProof(address, storageKeys, block.Number())
if err != nil {
return nil, err
}
state.StateProof = proof
state.ParsedHeader, err = chains.ParseHeader(block.Header())
if err != nil {
return nil, err
}
state.CommitSeals, err = state.ParsedHeader.ValidateAndGetCommitSeals(lc.consensusType)
if err != nil {
return nil, err
}
return &state, nil
}
type LightClientInputData struct {
ParsedHeader *chains.ParsedHeader
StateProof *client.StateProof
CommitSeals [][]byte
}
func (cs LightClientInputData) Header() *gethtypes.Header {
return cs.ParsedHeader.Base
}
func (cs LightClientInputData) MembershipProof() *client.StateProof {
return cs.StateProof
}
func (cs LightClientInputData) SealingHeaderRLP(consensusType chains.ConsensusType) []byte {
bz, err := cs.ParsedHeader.GetSealingHeaderBytes(consensusType)
if err != nil {
panic(err)
}
return bz
}
func (cs LightClientInputData) GetCommitSeals() [][]byte {
return cs.CommitSeals
}
func (cs LightClientInputData) Validators() [][]byte {
var addrs [][]byte
for _, val := range cs.ParsedHeader.Validators {
addrs = append(addrs, val.Bytes())
}
return addrs
}