Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ func makeFullNode(ctx *cli.Context) *node.Node {
}

backend, eth := utils.RegisterEthService(stack, &cfg.Eth)
stack.APIBackend = backend

// Create gauge with geth system and build information
if eth != nil { // The 'eth' backend may be nil in light mode
Expand Down
3 changes: 3 additions & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,11 @@ var (

rpcFlags = []cli.Flag{
utils.HTTPEnabledFlag,
utils.HTTPSGTEnabledFlag,
utils.HTTPListenAddrFlag,
utils.HTTPSGTListenAddrFlag,
utils.HTTPPortFlag,
utils.HTTPSGTPortFlag,
utils.HTTPCORSDomainFlag,
utils.AuthListenFlag,
utils.AuthPortFlag,
Expand Down
27 changes: 27 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,18 +675,35 @@ var (
Usage: "Enable the HTTP-RPC server",
Category: flags.APICategory,
}
HTTPSGTEnabledFlag = &cli.BoolFlag{
Name: "httpsgt",
Usage: "Enable the HTTP-RPC server for Soul Gas Token",
Category: flags.APICategory,
}
HTTPListenAddrFlag = &cli.StringFlag{
Name: "http.addr",
Usage: "HTTP-RPC server listening interface",
Value: node.DefaultHTTPHost,
Category: flags.APICategory,
}
HTTPSGTListenAddrFlag = &cli.StringFlag{
Name: "httpsgt.addr",
Usage: "HTTP-RPC server listening interface for Soul Gas Token",
Value: node.DefaultHTTPSGTHost,
Category: flags.APICategory,
}
HTTPPortFlag = &cli.IntFlag{
Name: "http.port",
Usage: "HTTP-RPC server listening port",
Value: node.DefaultHTTPPort,
Category: flags.APICategory,
}
HTTPSGTPortFlag = &cli.IntFlag{
Name: "httpsgt.port",
Usage: "HTTP-RPC server listening port for Soul Gas Token",
Value: node.DefaultHTTPSGTPort,
Category: flags.APICategory,
}
HTTPCORSDomainFlag = &cli.StringFlag{
Name: "http.corsdomain",
Usage: "Comma separated list of domains from which to accept cross origin requests (browser enforced)",
Expand Down Expand Up @@ -1271,10 +1288,20 @@ func setHTTP(ctx *cli.Context, cfg *node.Config) {
cfg.HTTPHost = ctx.String(HTTPListenAddrFlag.Name)
}
}
if ctx.Bool(HTTPSGTEnabledFlag.Name) {
if ctx.IsSet(HTTPSGTListenAddrFlag.Name) {
cfg.HTTPSGTHost = ctx.String(HTTPSGTListenAddrFlag.Name)
} else if cfg.HTTPSGTHost == "" {
cfg.HTTPSGTHost = "127.0.0.1"
}
}

if ctx.IsSet(HTTPPortFlag.Name) {
cfg.HTTPPort = ctx.Int(HTTPPortFlag.Name)
}
if ctx.IsSet(HTTPSGTPortFlag.Name) {
cfg.HTTPSGTPort = ctx.Int(HTTPSGTPortFlag.Name)
}

if ctx.IsSet(AuthListenFlag.Name) {
cfg.AuthAddr = ctx.String(AuthListenFlag.Name)
Expand Down
18 changes: 14 additions & 4 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,12 +638,18 @@ func (api *PersonalAccountAPI) Unpair(ctx context.Context, url string, pin strin

// BlockChainAPI provides an API to access Ethereum blockchain data.
type BlockChainAPI struct {
b Backend
b Backend
sgt bool
}

// NewBlockChainAPI creates a new Ethereum blockchain API.
func NewBlockChainAPI(b Backend) *BlockChainAPI {
return &BlockChainAPI{b}
return &BlockChainAPI{b, false}
}

// NewBlockChainAPIForSGT creates a new Ethereum blockchain API for SGT.
func NewBlockChainAPIForSGT(b Backend) *BlockChainAPI {
return &BlockChainAPI{b, true}
}

// ChainId is the EIP-155 replay-protection chain id for the current Ethereum chain config.
Expand Down Expand Up @@ -688,8 +694,12 @@ func (api *BlockChainAPI) GetBalance(ctx context.Context, address common.Address
if state == nil || err != nil {
return nil, err
}
b := state.GetBalance(address).ToBig()
return (*hexutil.Big)(b), state.Error()
if !api.sgt {
b := state.GetBalance(address).ToBig()
return (*hexutil.Big)(b), state.Error()
}
nativeBalance, sgtBalance := core.GetGasBalancesInBig(state, api.b.ChainConfig(), address)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use a flag to turn it on or off? Or another idea is to use a flag with different port number for SGT balance RPC

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in a254afb.

return (*hexutil.Big)(new(big.Int).Add(nativeBalance, sgtBalance)), state.Error()
}

// AccountResult structs for GetProof
Expand Down
3 changes: 3 additions & 0 deletions node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ type Config struct {
// field is empty, no HTTP API endpoint will be started.
HTTPHost string

HTTPSGTHost string
HTTPSGTPort int `toml:",omitempty"`

// HTTPPort is the TCP port number on which to start the HTTP RPC server. The
// default zero value is/ valid and will pick a port number randomly (useful
// for ephemeral nodes).
Expand Down
3 changes: 3 additions & 0 deletions node/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const (
DefaultWSPort = 8546 // Default TCP port for the websocket RPC server
DefaultAuthHost = "localhost" // Default host interface for the authenticated apis
DefaultAuthPort = 8551 // Default port for the authenticated apis

DefaultHTTPSGTHost = "localhost" // Default host interface for the HTTPSGT RPC server
DefaultHTTPSGTPort = 8645 // Default TCP port for the HTTPSGT RPC server
)

const (
Expand Down
36 changes: 36 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/ethdb/memorydb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rpc"
Expand All @@ -60,13 +61,16 @@ type Node struct {
lifecycles []Lifecycle // All registered backends, services, and auxiliary services that have a lifecycle
rpcAPIs []rpc.API // List of APIs currently provided by the node
http *httpServer //
httpSGT *httpServer //
ws *httpServer //
httpAuth *httpServer //
wsAuth *httpServer //
ipc *ipcServer // Stores information about the ipc http server
inprocHandler *rpc.Server // In-process RPC request handler to process the API requests

databases map[*closeTrackingDB]struct{} // All open databases

APIBackend ethapi.Backend // Ethereum API backend, used for SGT
}

const (
Expand Down Expand Up @@ -151,6 +155,7 @@ func New(conf *Config) (*Node, error) {

// Configure RPC servers.
node.http = newHTTPServer(node.log, conf.HTTPTimeouts)
node.httpSGT = newHTTPServer(node.log, conf.HTTPTimeouts)
node.httpAuth = newHTTPServer(node.log, conf.HTTPTimeouts)
node.ws = newHTTPServer(node.log, rpc.DefaultHTTPTimeouts)
node.wsAuth = newHTTPServer(node.log, rpc.DefaultHTTPTimeouts)
Expand Down Expand Up @@ -423,6 +428,30 @@ func (n *Node) startRPC() error {
servers = append(servers, server)
return nil
}
initHttpSGT := func(server *httpServer) error {
if n.APIBackend == nil {
panic("bug: Node.APIBackend is nil when initHttpSGT is called")
}
if err := server.setListenAddr(n.config.HTTPSGTHost, n.config.HTTPSGTPort); err != nil {
return err
}
// appended API will override the existing one
updatedOpenAPIs := append(openAPIs, rpc.API{
Namespace: "eth",
Service: ethapi.NewBlockChainAPIForSGT(n.APIBackend),
})
if err := server.enableRPC(updatedOpenAPIs, httpConfig{
CorsAllowedOrigins: n.config.HTTPCors,
Vhosts: n.config.HTTPVirtualHosts,
Modules: n.config.HTTPModules,
prefix: n.config.HTTPPathPrefix,
rpcEndpointConfig: rpcConfig,
}); err != nil {
return err
}
servers = append(servers, server)
return nil
}

initWS := func(port int) error {
server := n.wsServerForPort(port, false)
Expand Down Expand Up @@ -489,6 +518,12 @@ func (n *Node) startRPC() error {
return err
}
}
if n.config.HTTPSGTHost != "" {
// Configure unauthenticated HTTP for SGT.
if err := initHttpSGT(n.httpSGT); err != nil {
return err
}
}
// Configure WebSocket.
if n.config.WSHost != "" {
// legacy unauthenticated
Expand Down Expand Up @@ -528,6 +563,7 @@ func (n *Node) wsServerForPort(port int, authenticated bool) *httpServer {

func (n *Node) stopRPC() {
n.http.stop()
n.httpSGT.stop()
n.ws.stop()
n.httpAuth.stop()
n.wsAuth.stop()
Expand Down