/
registry.go
117 lines (104 loc) · 3.42 KB
/
registry.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package ethtxs
import (
"context"
"log"
bridgeRegistry "github.com/33cn/plugin/plugin/dapp/cross2eth/contracts/contracts4eth/generated"
"github.com/33cn/plugin/plugin/dapp/cross2eth/ebrelayer/relayer/ethereum/ethinterface"
ebrelayerTypes "github.com/33cn/plugin/plugin/dapp/cross2eth/ebrelayer/types"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
)
// ContractRegistry :
type ContractRegistry byte
const (
// Valset : valset contract
Valset ContractRegistry = iota + 1
// Oracle : oracle contract
Oracle
// BridgeBank : bridgeBank contract
BridgeBank
// Chain33Bridge : chain33Bridge contract
Chain33Bridge
)
// String : returns the event type as a string
func (d ContractRegistry) String() string {
return [...]string{"valset", "oracle", "bridgebank", "chain33bridge", "notsupport"}[d-1]
}
// GetAddressFromBridgeRegistry : utility method which queries the requested contract address from the BridgeRegistry
func GetAddressFromBridgeRegistry(client ethinterface.EthClientSpec, sender, registry common.Address, target ContractRegistry) (address *common.Address, err error) {
header, err := client.HeaderByNumber(context.Background(), nil)
if err != nil {
txslog.Error("GetAddressFromBridgeRegistry", "Failed to get HeaderByNumber due to:", err.Error())
return nil, err
}
// Set up CallOpts auth
auth := bind.CallOpts{
Pending: true,
From: sender,
BlockNumber: header.Number,
Context: context.Background(),
}
// Initialize BridgeRegistry instance
registryInstance, err := bridgeRegistry.NewBridgeRegistry(registry, client)
if err != nil {
txslog.Error("GetAddressFromBridgeRegistry", "Failed to NewBridgeRegistry to:", err.Error())
return nil, err
}
switch target {
case Valset:
valsetAddress, err := registryInstance.Valset(&auth)
if err != nil {
log.Fatal(err)
}
return &valsetAddress, nil
case Oracle:
oracleAddress, err := registryInstance.Oracle(&auth)
if err != nil {
log.Fatal(err)
}
return &oracleAddress, nil
case BridgeBank:
bridgeBankAddress, err := registryInstance.BridgeBank(&auth)
if err != nil {
log.Fatal(err)
}
return &bridgeBankAddress, nil
case Chain33Bridge:
chain33BridgeAddress, err := registryInstance.Chain33Bridge(&auth)
if err != nil {
log.Fatal(err)
}
return &chain33BridgeAddress, nil
default:
txslog.Error("GetAddressFromBridgeRegistry", "invalid target contract type:", target)
return nil, ebrelayerTypes.ErrInvalidContractAddress
}
}
// GetDeployHeight : 获取合约部署高度
func GetDeployHeight(client ethinterface.EthClientSpec, sender, registry common.Address) (height int64, err error) {
header, err := client.HeaderByNumber(context.Background(), nil)
if err != nil {
txslog.Error("GetAddressFromBridgeRegistry", "Failed to get HeaderByNumber due to:", err.Error())
return 0, err
}
// Set up CallOpts auth
callOpts := &bind.CallOpts{
Pending: true,
From: sender,
BlockNumber: header.Number,
Context: context.Background(),
}
// Initialize BridgeRegistry instance
registryInstance, err := bridgeRegistry.NewBridgeRegistry(registry, client)
if err != nil {
txslog.Error("GetAddressFromBridgeRegistry", "Failed to NewBridgeRegistry to:", err.Error())
return 0, err
}
bgInt, err := registryInstance.DeployHeight(callOpts)
if nil != err {
return 0, err
}
height = bgInt.Int64()
txslog.Info("GetDeployHeight", "deploy height:", height)
return
}