10 jww "github.com/spf13/jwalterweatherman"
11 "github.com/vapor/blockchain/rpc"
12 "github.com/vapor/config"
15 var MainchainConfig *config.MainChainRpcConfig
16 var ValidatePegin bool
18 // Response describes the response standard.
19 type Response struct {
20 Status string `json:"status,omitempty"`
21 Code string `json:"code,omitempty"`
22 Msg string `json:"msg,omitempty"`
23 ErrorDetail string `json:"error_detail,omitempty"`
24 Data interface{} `json:"data,omitempty"`
28 func CallRPC(path string, req ...interface{}) (interface{}, error) {
30 host := MainchainConfig.MainchainRpcHost
31 port, _ := strconv.ParseUint(MainchainConfig.MainchainRpcPort, 10, 16)
32 token := MainchainConfig.MainchainToken
34 var resp = &Response{}
35 var request interface{}
41 // TODO主链的ip port token
42 rpcURL := fmt.Sprintf("http://%s:%d", host, port)
43 client := &rpc.Client{BaseURL: rpcURL}
44 client.AccessToken = token
45 client.Call(context.Background(), path, request, resp)
48 jww.ERROR.Println(resp.Msg)
49 return nil, errors.New("fail")
51 jww.ERROR.Println("Unable to connect to the bytomd")
52 return nil, errors.New("")
57 func IsConfirmedBytomBlock(txHeight uint64, nMinConfirmationDepth uint64) error {
58 data, exitCode := CallRPC("get-block-count")
62 type blockHeight struct {
63 BlockCount uint64 `json:"block_count"`
65 var mainchainHeight blockHeight
66 tmp, _ := json.Marshal(data)
67 json.Unmarshal(tmp, &mainchainHeight)
68 if mainchainHeight.BlockCount < txHeight || (mainchainHeight.BlockCount-txHeight) < nMinConfirmationDepth {
69 return errors.New("Peg-in bytom transaction needs more confirmations to be sent")