Ejemplo n.º 1
0
// StartRPC starts the HTTP RPC API server.
func (api *PrivateAdminAPI) StartRPC(host *string, port *rpc.HexNumber, cors *string, apis *string) (bool, error) {
	api.node.lock.Lock()
	defer api.node.lock.Unlock()

	if api.node.httpHandler != nil {
		return false, fmt.Errorf("HTTP RPC already running on %s", api.node.httpEndpoint)
	}

	if host == nil {
		host = &api.node.httpHost
	}
	if port == nil {
		port = rpc.NewHexNumber(api.node.httpPort)
	}
	if cors == nil {
		cors = &api.node.httpCors
	}

	modules := api.node.httpWhitelist
	if apis != nil {
		modules = nil
		for _, m := range strings.Split(*apis, ",") {
			modules = append(modules, strings.TrimSpace(m))
		}
	}

	if err := api.node.startHTTP(fmt.Sprintf("%s:%d", *host, port.Int()), api.node.rpcAPIs, modules, *cors); err != nil {
		return false, err
	}
	return true, nil
}
Ejemplo n.º 2
0
// StartWS starts the websocket RPC API server.
func (api *PrivateAdminAPI) StartWS(host *string, port *rpc.HexNumber, allowedOrigins *string, apis *string) (bool, error) {
	api.node.lock.Lock()
	defer api.node.lock.Unlock()

	if api.node.wsHandler != nil {
		return false, fmt.Errorf("WebSocket RPC already running on %s", api.node.wsEndpoint)
	}

	if host == nil {
		host = &api.node.wsHost
	}
	if port == nil {
		port = rpc.NewHexNumber(api.node.wsPort)
	}
	if allowedOrigins == nil {
		allowedOrigins = &api.node.wsOrigins
	}

	modules := api.node.wsWhitelist
	if apis != nil {
		modules = nil
		for _, m := range strings.Split(*apis, ",") {
			modules = append(modules, strings.TrimSpace(m))
		}
	}

	if err := api.node.startWS(fmt.Sprintf("%s:%d", *host, port.Int()), api.node.rpcAPIs, modules, *allowedOrigins); err != nil {
		return false, err
	}
	return true, nil
}
Ejemplo n.º 3
0
// NewWhisperFilter creates and registers a new message filter to watch for inbound whisper messages.
func (s *PublicWhisperAPI) NewFilter(args NewFilterArgs) (*rpc.HexNumber, error) {
	if s.w == nil {
		return nil, whisperOffLineErr
	}

	var id int
	filter := Filter{
		To:     crypto.ToECDSAPub(common.FromHex(args.To)),
		From:   crypto.ToECDSAPub(common.FromHex(args.From)),
		Topics: NewFilterTopics(args.Topics...),
		Fn: func(message *Message) {
			wmsg := NewWhisperMessage(message)
			s.messagesMu.RLock() // Only read lock to the filter pool
			defer s.messagesMu.RUnlock()
			if s.messages[id] != nil {
				s.messages[id].insert(wmsg)
			}
		},
	}

	id = s.w.Watch(filter)

	s.messagesMu.Lock()
	s.messages[id] = newWhisperFilter(id, s.w)
	s.messagesMu.Unlock()

	return rpc.NewHexNumber(id), nil
}
Ejemplo n.º 4
0
// EstimateGasLimit implements ContractTransactor.EstimateGasLimit, delegating
// the gas estimation to the remote node.
func (b *rpcBackend) EstimateGasLimit(sender common.Address, contract *common.Address, value *big.Int, data []byte) (*big.Int, error) {
	// Pack up the request into an RPC argument
	args := struct {
		From  common.Address  `json:"from"`
		To    *common.Address `json:"to"`
		Value *rpc.HexNumber  `json:"value"`
		Data  string          `json:"data"`
	}{
		From:  sender,
		To:    contract,
		Data:  common.ToHex(data),
		Value: rpc.NewHexNumber(value),
	}
	// Execute the RPC call and retrieve the response
	res, err := b.request("eth_estimateGas", []interface{}{args})
	if err != nil {
		return nil, err
	}
	var hex string
	if err := json.Unmarshal(res, &hex); err != nil {
		return nil, err
	}
	estimate, ok := new(big.Int).SetString(hex, 0)
	if !ok {
		return nil, fmt.Errorf("invalid estimate hex: %s", hex)
	}
	return estimate, nil
}
Ejemplo n.º 5
0
// GetBlockTime retrieves the block of the given number from the canonical chain.
func (api *API) GetBlockTime(num uint64) (time.Time, error) {
	res, err := api.Request("eth_getBlockByNumber", []interface{}{rpc.NewHexNumber(num), true})
	if err != nil {
		return time.Time{}, err
	}
	result := make(map[string]json.RawMessage)
	if err := json.Unmarshal(res, &result); err != nil {
		return time.Time{}, err
	}
	var hex string
	if err := json.Unmarshal(result["timestamp"], &hex); err != nil {
		return time.Time{}, err
	}
	return time.Unix(new(big.Int).SetBytes(common.FromHex(hex)).Int64(), 0), nil
}
Ejemplo n.º 6
0
// Version returns the Whisper version this node offers.
func (s *PublicWhisperAPI) Version() (*rpc.HexNumber, error) {
	if s.w == nil {
		return rpc.NewHexNumber(0), whisperOffLineErr
	}
	return rpc.NewHexNumber(s.w.Version()), nil
}