/
contract.go
67 lines (56 loc) · 1.89 KB
/
contract.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
package client
import (
"context"
"fmt"
"math/big"
mocktypes "github.com/datachainlab/ibc-mock-client/modules/light-clients/xx-mock/types"
"github.com/ethereum/go-ethereum/common"
gethtypes "github.com/ethereum/go-ethereum/core/types"
trustedethereumtypes "github.com/datachainlab/ibc-trusted-ethereum-client/modules/light-clients/trusted-ethereum/types"
)
type ContractState interface {
Header() *gethtypes.Header
ETHProof() *ETHProof
}
func (cl ChainClient) GetContractState(ctx context.Context, address common.Address, storageKeys [][]byte, bn *big.Int, clientType string) (ContractState, error) {
switch clientType {
case trustedethereumtypes.TrustedEthereum:
return cl.GetEthContractState(ctx, address, storageKeys, bn)
case mocktypes.Mock:
return cl.GetMockContractState(ctx, address, storageKeys, bn)
default:
panic(fmt.Sprintf("unknown client type '%v'", clientType))
}
}
func (cl ChainClient) GetEthContractState(ctx context.Context, address common.Address, storageKeys [][]byte, bn *big.Int) (ContractState, error) {
block, err := cl.BlockByNumber(ctx, bn)
if err != nil {
return nil, err
}
proof, err := cl.GetETHProof(address, storageKeys, block.Number())
if err != nil {
return nil, err
}
return ETHContractState{header: block.Header(), ethProof: proof}, nil
}
func (cl ChainClient) GetMockContractState(ctx context.Context, address common.Address, storageKeys [][]byte, bn *big.Int) (ContractState, error) {
block, err := cl.BlockByNumber(ctx, bn)
if err != nil {
return nil, err
}
// this is dummy
proof := ÐProof{
StorageProofRLP: make([][]byte, len(storageKeys)),
}
return ETHContractState{header: block.Header(), ethProof: proof}, nil
}
type ETHContractState struct {
header *gethtypes.Header
ethProof *ETHProof
}
func (cs ETHContractState) Header() *gethtypes.Header {
return cs.header
}
func (cs ETHContractState) ETHProof() *ETHProof {
return cs.ethProof
}