Skip to content

Commit b9aedea

Browse files
committed
Merge pull request bnb-chain#2072 from karalabe/admin-debug-apis
core, eth, node, rpc: port the admin and debug API
2 parents 9c636d2 + d8370a4 commit b9aedea

7 files changed

Lines changed: 494 additions & 26 deletions

File tree

‎cmd/gethrpctest/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func StartIPC(stack *node.Node) error {
214214
server := rpc.NewServer()
215215

216216
// register package API's this node provides
217-
offered := stack.RPCAPIs()
217+
offered := stack.APIs()
218218
for _, api := range offered {
219219
server.RegisterName(api.Namespace, api.Service)
220220
glog.V(logger.Debug).Infof("Register %T@%s for IPC service\n", api.Service, api.Namespace)

‎cmd/utils/flags.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ func StartIPC(stack *node.Node, ctx *cli.Context) error {
798798
server := rpc.NewServer()
799799

800800
// register package API's this node provides
801-
offered := stack.RPCAPIs()
801+
offered := stack.APIs()
802802
for _, api := range offered {
803803
server.RegisterName(api.Namespace, api.Service)
804804
glog.V(logger.Debug).Infof("Register %T under namespace '%s' for IPC service\n", api.Service, api.Namespace)

‎eth/api.go‎

Lines changed: 202 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import (
2121
"encoding/json"
2222
"errors"
2323
"fmt"
24+
"io"
2425
"math/big"
26+
"os"
2527
"sync"
2628
"time"
2729

@@ -46,7 +48,7 @@ import (
4648

4749
const (
4850
defaultGasPrice = uint64(10000000000000)
49-
defaultGas = uint64(90000)
51+
defaultGas = uint64(90000)
5052
)
5153

5254
// PublicEthereumAPI provides an API to access Ethereum related information.
@@ -471,12 +473,12 @@ type callmsg struct {
471473

472474
// accessor boilerplate to implement core.Message
473475
func (m callmsg) From() (common.Address, error) { return m.from.Address(), nil }
474-
func (m callmsg) Nonce() uint64 { return m.from.Nonce() }
475-
func (m callmsg) To() *common.Address { return m.to }
476-
func (m callmsg) GasPrice() *big.Int { return m.gasPrice }
477-
func (m callmsg) Gas() *big.Int { return m.gas }
478-
func (m callmsg) Value() *big.Int { return m.value }
479-
func (m callmsg) Data() []byte { return m.data }
476+
func (m callmsg) Nonce() uint64 { return m.from.Nonce() }
477+
func (m callmsg) To() *common.Address { return m.to }
478+
func (m callmsg) GasPrice() *big.Int { return m.gasPrice }
479+
func (m callmsg) Gas() *big.Int { return m.gas }
480+
func (m callmsg) Value() *big.Int { return m.value }
481+
func (m callmsg) Data() []byte { return m.data }
480482

481483
type CallArgs struct {
482484
From common.Address `json:"from"`
@@ -972,20 +974,20 @@ func (s *PublicTransactionPoolAPI) Sign(address common.Address, data string) (st
972974
}
973975

974976
type SignTransactionArgs struct {
975-
From common.Address
976-
To common.Address
977-
Nonce *rpc.HexNumber
978-
Value *rpc.HexNumber
979-
Gas *rpc.HexNumber
980-
GasPrice *rpc.HexNumber
981-
Data string
977+
From common.Address
978+
To common.Address
979+
Nonce *rpc.HexNumber
980+
Value *rpc.HexNumber
981+
Gas *rpc.HexNumber
982+
GasPrice *rpc.HexNumber
983+
Data string
982984

983985
BlockNumber int64
984986
}
985987

986988
// Tx is a helper object for argument and return values
987989
type Tx struct {
988-
tx *types.Transaction
990+
tx *types.Transaction
989991

990992
To *common.Address `json:"to"`
991993
From common.Address `json:"from"`
@@ -1214,3 +1216,188 @@ func (s *PublicTransactionPoolAPI) Resend(tx *Tx, gasPrice, gasLimit *rpc.HexNum
12141216

12151217
return common.Hash{}, fmt.Errorf("Transaction %#x not found", tx.Hash)
12161218
}
1219+
1220+
// PrivateAdminAPI is the collection of Etheruem APIs exposed over the private
1221+
// admin endpoint.
1222+
type PrivateAdminAPI struct {
1223+
eth *Ethereum
1224+
}
1225+
1226+
// NewPrivateAdminAPI creates a new API definition for the private admin methods
1227+
// of the Ethereum service.
1228+
func NewPrivateAdminAPI(eth *Ethereum) *PrivateAdminAPI {
1229+
return &PrivateAdminAPI{eth: eth}
1230+
}
1231+
1232+
// SetSolc sets the Solidity compiler path to be used by the node.
1233+
func (api *PrivateAdminAPI) SetSolc(path string) (string, error) {
1234+
solc, err := api.eth.SetSolc(path)
1235+
if err != nil {
1236+
return "", err
1237+
}
1238+
return solc.Info(), nil
1239+
}
1240+
1241+
// ExportChain exports the current blockchain into a local file.
1242+
func (api *PrivateAdminAPI) ExportChain(file string) (bool, error) {
1243+
// Make sure we can create the file to export into
1244+
out, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
1245+
if err != nil {
1246+
return false, err
1247+
}
1248+
defer out.Close()
1249+
1250+
// Export the blockchain
1251+
if err := api.eth.BlockChain().Export(out); err != nil {
1252+
return false, err
1253+
}
1254+
return true, nil
1255+
}
1256+
1257+
// ImportChain imports a blockchain from a local file.
1258+
func (api *PrivateAdminAPI) ImportChain(file string) (bool, error) {
1259+
// Make sure the can access the file to import
1260+
in, err := os.Open(file)
1261+
if err != nil {
1262+
return false, err
1263+
}
1264+
defer in.Close()
1265+
1266+
// Run actual the import in pre-configured batches
1267+
stream := rlp.NewStream(in, 0)
1268+
1269+
blocks, index := make([]*types.Block, 0, 2500), 0
1270+
for batch := 0; ; batch++ {
1271+
// Load a batch of blocks from the input file
1272+
for len(blocks) < cap(blocks) {
1273+
block := new(types.Block)
1274+
if err := stream.Decode(block); err == io.EOF {
1275+
break
1276+
} else if err != nil {
1277+
return false, fmt.Errorf("block %d: failed to parse: %v", index, err)
1278+
}
1279+
blocks = append(blocks, block)
1280+
index++
1281+
}
1282+
if len(blocks) == 0 {
1283+
break
1284+
}
1285+
// Import the batch and reset the buffer
1286+
if _, err := api.eth.BlockChain().InsertChain(blocks); err != nil {
1287+
return false, fmt.Errorf("batch %d: failed to insert: %v", batch, err)
1288+
}
1289+
blocks = blocks[:0]
1290+
}
1291+
return true, nil
1292+
}
1293+
1294+
// PublicDebugAPI is the collection of Etheruem APIs exposed over the public
1295+
// debugging endpoint.
1296+
type PublicDebugAPI struct {
1297+
eth *Ethereum
1298+
}
1299+
1300+
// NewPublicDebugAPI creates a new API definition for the public debug methods
1301+
// of the Ethereum service.
1302+
func NewPublicDebugAPI(eth *Ethereum) *PublicDebugAPI {
1303+
return &PublicDebugAPI{eth: eth}
1304+
}
1305+
1306+
// DumpBlock retrieves the entire state of the database at a given block.
1307+
func (api *PublicDebugAPI) DumpBlock(number uint64) (state.World, error) {
1308+
block := api.eth.BlockChain().GetBlockByNumber(number)
1309+
if block == nil {
1310+
return state.World{}, fmt.Errorf("block #%d not found", number)
1311+
}
1312+
stateDb, err := state.New(block.Root(), api.eth.ChainDb())
1313+
if err != nil {
1314+
return state.World{}, err
1315+
}
1316+
return stateDb.RawDump(), nil
1317+
}
1318+
1319+
// GetBlockRlp retrieves the RLP encoded for of a single block.
1320+
func (api *PublicDebugAPI) GetBlockRlp(number uint64) (string, error) {
1321+
block := api.eth.BlockChain().GetBlockByNumber(number)
1322+
if block == nil {
1323+
return "", fmt.Errorf("block #%d not found", number)
1324+
}
1325+
encoded, err := rlp.EncodeToBytes(block)
1326+
if err != nil {
1327+
return "", err
1328+
}
1329+
return fmt.Sprintf("%x", encoded), nil
1330+
}
1331+
1332+
// PrintBlock retrieves a block and returns its pretty printed form.
1333+
func (api *PublicDebugAPI) PrintBlock(number uint64) (string, error) {
1334+
block := api.eth.BlockChain().GetBlockByNumber(number)
1335+
if block == nil {
1336+
return "", fmt.Errorf("block #%d not found", number)
1337+
}
1338+
return fmt.Sprintf("%s", block), nil
1339+
}
1340+
1341+
// SeedHash retrieves the seed hash of a block.
1342+
func (api *PublicDebugAPI) SeedHash(number uint64) (string, error) {
1343+
block := api.eth.BlockChain().GetBlockByNumber(number)
1344+
if block == nil {
1345+
return "", fmt.Errorf("block #%d not found", number)
1346+
}
1347+
hash, err := ethash.GetSeedHash(number)
1348+
if err != nil {
1349+
return "", err
1350+
}
1351+
return fmt.Sprintf("0x%x", hash), nil
1352+
}
1353+
1354+
// PrivateDebugAPI is the collection of Etheruem APIs exposed over the private
1355+
// debugging endpoint.
1356+
type PrivateDebugAPI struct {
1357+
eth *Ethereum
1358+
}
1359+
1360+
// NewPrivateDebugAPI creates a new API definition for the private debug methods
1361+
// of the Ethereum service.
1362+
func NewPrivateDebugAPI(eth *Ethereum) *PrivateDebugAPI {
1363+
return &PrivateDebugAPI{eth: eth}
1364+
}
1365+
1366+
// ProcessBlock reprocesses an already owned block.
1367+
func (api *PrivateDebugAPI) ProcessBlock(number uint64) (bool, error) {
1368+
// Fetch the block that we aim to reprocess
1369+
block := api.eth.BlockChain().GetBlockByNumber(number)
1370+
if block == nil {
1371+
return false, fmt.Errorf("block #%d not found", number)
1372+
}
1373+
// Temporarily enable debugging
1374+
defer func(old bool) { vm.Debug = old }(vm.Debug)
1375+
vm.Debug = true
1376+
1377+
// Validate and reprocess the block
1378+
var (
1379+
blockchain = api.eth.BlockChain()
1380+
validator = blockchain.Validator()
1381+
processor = blockchain.Processor()
1382+
)
1383+
if err := core.ValidateHeader(blockchain.AuxValidator(), block.Header(), blockchain.GetHeader(block.ParentHash()), true, false); err != nil {
1384+
return false, err
1385+
}
1386+
statedb, err := state.New(blockchain.GetBlock(block.ParentHash()).Root(), api.eth.ChainDb())
1387+
if err != nil {
1388+
return false, err
1389+
}
1390+
receipts, _, usedGas, err := processor.Process(block, statedb)
1391+
if err != nil {
1392+
return false, err
1393+
}
1394+
if err := validator.ValidateState(block, blockchain.GetBlock(block.ParentHash()), statedb, receipts, usedGas); err != nil {
1395+
return false, err
1396+
}
1397+
return true, nil
1398+
}
1399+
1400+
// SetHead rewinds the head of the blockchain to a previous block.
1401+
func (api *PrivateDebugAPI) SetHead(number uint64) {
1402+
api.eth.BlockChain().SetHead(number)
1403+
}

‎eth/backend.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,19 @@ func (s *Ethereum) APIs() []rpc.API {
295295
Version: "1.0",
296296
Service: filters.NewPublicFilterAPI(s.ChainDb(), s.EventMux()),
297297
Public: true,
298+
}, {
299+
Namespace: "admin",
300+
Version: "1.0",
301+
Service: NewPrivateAdminAPI(s),
302+
}, {
303+
Namespace: "debug",
304+
Version: "1.0",
305+
Service: NewPublicDebugAPI(s),
306+
Public: true,
307+
}, {
308+
Namespace: "debug",
309+
Version: "1.0",
310+
Service: NewPrivateDebugAPI(s),
298311
},
299312
}
300313
}

0 commit comments

Comments
 (0)