Skip to content
Merged
Changes from 4 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
44 changes: 41 additions & 3 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ package ethapi

import (
"context"
"encoding/json"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -56,6 +57,8 @@ import (
"github.com/tyler-smith/go-bip39"
)

const maxJSONLen = 64 * 1024 * 1024 // 64MB

// EthereumAPI provides an API to access Ethereum related information.
type EthereumAPI struct {
b Backend
Expand Down Expand Up @@ -140,16 +143,51 @@ func (s *EthereumAPI) Syncing() (interface{}, error) {

type GetChainConfigResponse struct {
*params.ChainConfig
params.UpgradeConfig `json:"upgrades"`
UpgradeConfig params.UpgradeConfig
}

// MarshalJSON implements json.Marshaler. This is a workaround for the fact that
// the embedded ChainConfig struct has a MarshalJSON method, which prevents
// the default JSON marshalling from working for UpgradeConfig.
// TODO: consider removing this method by allowing external tag for the embedded
// ChainConfig struct.
func (s *GetChainConfigResponse) MarshalJSON() ([]byte, error) {
// embed the ChainConfig struct into the response
chainConfigJSON, err := json.Marshal(s.ChainConfig)
if err != nil {
return nil, err
}
if len(chainConfigJSON) > maxJSONLen {
return nil, errors.New("value too large")
}

type upgrades struct {
UpgradeConfig params.UpgradeConfig `json:"upgrades"`
}

upgradeJSON, err := json.Marshal(upgrades{s.UpgradeConfig})
if err != nil {
return nil, err
}
if len(upgradeJSON) > maxJSONLen {
return nil, errors.New("value too large")
}

// merge the two JSON objects
mergedJSON := make([]byte, 0, len(chainConfigJSON)+len(upgradeJSON)+1)
mergedJSON = append(mergedJSON, chainConfigJSON[:len(chainConfigJSON)-1]...)
mergedJSON = append(mergedJSON, ',')
mergedJSON = append(mergedJSON, upgradeJSON[1:]...)
return mergedJSON, nil
}

func (s *BlockChainAPI) GetChainConfig(ctx context.Context) GetChainConfigResponse {
func (s *BlockChainAPI) GetChainConfig(ctx context.Context) *GetChainConfigResponse {
config := s.b.ChainConfig()
resp := GetChainConfigResponse{
ChainConfig: config,
UpgradeConfig: config.UpgradeConfig,
}
return resp
return &resp
}

// TxPoolAPI offers and API for the transaction pool. It only operates on data that is non confidential.
Expand Down