github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/intprotocol/api.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package intprotocol 18 19 import ( 20 "compress/gzip" 21 "context" 22 "errors" 23 "fmt" 24 "io" 25 "math/big" 26 "os" 27 "strings" 28 29 "github.com/intfoundation/intchain/common" 30 "github.com/intfoundation/intchain/common/hexutil" 31 "github.com/intfoundation/intchain/core" 32 "github.com/intfoundation/intchain/core/datareduction" 33 "github.com/intfoundation/intchain/core/rawdb" 34 "github.com/intfoundation/intchain/core/state" 35 "github.com/intfoundation/intchain/core/types" 36 "github.com/intfoundation/intchain/crypto" 37 "github.com/intfoundation/intchain/log" 38 "github.com/intfoundation/intchain/miner" 39 "github.com/intfoundation/intchain/params" 40 "github.com/intfoundation/intchain/rlp" 41 "github.com/intfoundation/intchain/rpc" 42 "github.com/intfoundation/intchain/trie" 43 ) 44 45 // PublicEthereumAPI provides an API to access Ethereum full node-related 46 // information. 47 type PublicEthereumAPI struct { 48 e *IntChain 49 } 50 51 // NewPublicEthereumAPI creates a new Ethereum protocol API for full nodes. 52 func NewPublicEthereumAPI(e *IntChain) *PublicEthereumAPI { 53 return &PublicEthereumAPI{e} 54 } 55 56 // Etherbase is the address that mining rewards will be send to 57 func (api *PublicEthereumAPI) Etherbase() (string, error) { 58 eb, err := api.e.Coinbase() 59 return eb.String(), err 60 } 61 62 // Coinbase is the address that mining rewards will be send to (alias for Etherbase) 63 func (api *PublicEthereumAPI) Coinbase() (string, error) { 64 return api.Etherbase() 65 } 66 67 // PublicMinerAPI provides an API to control the miner. 68 // It offers only methods that operate on data that pose no security risk when it is publicly accessible. 69 type PublicMinerAPI struct { 70 e *IntChain 71 agent *miner.RemoteAgent 72 } 73 74 // NewPublicMinerAPI create a new PublicMinerAPI instance. 75 func NewPublicMinerAPI(e *IntChain) *PublicMinerAPI { 76 agent := miner.NewRemoteAgent(e.BlockChain(), e.Engine()) 77 if e.Miner() != nil { 78 e.Miner().Register(agent) 79 } 80 81 return &PublicMinerAPI{e, agent} 82 } 83 84 // Mining returns an indication if this node is currently mining. 85 func (api *PublicMinerAPI) Mining() bool { 86 if api.e.Miner() != nil { 87 return api.e.IsMining() 88 } 89 return false 90 } 91 92 // SubmitWork can be used by external miner to submit their POW solution. It returns an indication if the work was 93 // accepted. Note, this is not an indication if the provided work was valid! 94 func (api *PublicMinerAPI) SubmitWork(nonce types.BlockNonce, solution, digest common.Hash) bool { 95 return api.agent.SubmitWork(nonce, digest, solution) 96 } 97 98 // GetWork returns a work package for external miner. The work package consists of 3 strings 99 // result[0], 32 bytes hex encoded current block header pow-hash 100 // result[1], 32 bytes hex encoded seed hash used for DAG 101 // result[2], 32 bytes hex encoded boundary condition ("target"), 2^256/difficulty 102 func (api *PublicMinerAPI) GetWork() ([3]string, error) { 103 if !api.e.IsMining() { 104 if err := api.e.StartMining(false); err != nil { 105 return [3]string{}, err 106 } 107 } 108 work, err := api.agent.GetWork() 109 if err != nil { 110 return work, fmt.Errorf("mining not ready: %v", err) 111 } 112 return work, nil 113 } 114 115 // SubmitHashrate can be used for remote miners to submit their hash rate. This enables the node to report the combined 116 // hash rate of all miners which submit work through this node. It accepts the miner hash rate and an identifier which 117 // must be unique between nodes. 118 func (api *PublicMinerAPI) SubmitHashrate(hashrate hexutil.Uint64, id common.Hash) bool { 119 api.agent.SubmitHashrate(id, uint64(hashrate)) 120 return true 121 } 122 123 // PrivateMinerAPI provides private RPC methods to control the miner. 124 // These methods can be abused by external users and must be considered insecure for use by untrusted users. 125 type PrivateMinerAPI struct { 126 e *IntChain 127 } 128 129 // NewPrivateMinerAPI create a new RPC service which controls the miner of this node. 130 func NewPrivateMinerAPI(e *IntChain) *PrivateMinerAPI { 131 return &PrivateMinerAPI{e: e} 132 } 133 134 // Start the miner with the given number of threads. If threads is nil the number 135 // of workers started is equal to the number of logical CPUs that are usable by 136 // this process. If mining is already running, this method adjust the number of 137 // threads allowed to use. 138 func (api *PrivateMinerAPI) Start(threads *int) error { 139 // Set the number of threads if the seal engine supports it 140 if threads == nil { 141 threads = new(int) 142 } else if *threads == 0 { 143 *threads = -1 // Disable the miner from within 144 } 145 type threaded interface { 146 SetThreads(threads int) 147 } 148 if th, ok := api.e.engine.(threaded); ok { 149 log.Info("Updated mining threads", "threads", *threads) 150 th.SetThreads(*threads) 151 } 152 // Start the miner and return 153 if !api.e.IsMining() { 154 // Propagate the initial price point to the transaction pool 155 api.e.lock.RLock() 156 price := api.e.gasPrice 157 api.e.lock.RUnlock() 158 159 api.e.txPool.SetGasPrice(price) 160 return api.e.StartMining(true) 161 } 162 return nil 163 } 164 165 // Stop the miner 166 func (api *PrivateMinerAPI) Stop() bool { 167 type threaded interface { 168 SetThreads(threads int) 169 } 170 if th, ok := api.e.engine.(threaded); ok { 171 th.SetThreads(-1) 172 } 173 api.e.StopMining() 174 return true 175 } 176 177 // SetExtra sets the extra data string that is included when this miner mines a block. 178 func (api *PrivateMinerAPI) SetExtra(extra string) (bool, error) { 179 if err := api.e.Miner().SetExtra([]byte(extra)); err != nil { 180 return false, err 181 } 182 return true, nil 183 } 184 185 // SetGasPrice sets the minimum accepted gas price for the miner. 186 func (api *PrivateMinerAPI) SetGasPrice(gasPrice hexutil.Big) bool { 187 api.e.lock.Lock() 188 api.e.gasPrice = (*big.Int)(&gasPrice) 189 api.e.lock.Unlock() 190 191 api.e.txPool.SetGasPrice((*big.Int)(&gasPrice)) 192 return true 193 } 194 195 // SetEtherbase sets the etherbase of the miner 196 func (api *PrivateMinerAPI) SetCoinbase(coinbase common.Address) bool { 197 api.e.SetCoinbase(coinbase) 198 return true 199 } 200 201 // PrivateAdminAPI is the collection of IntChain full node-related APIs 202 // exposed over the private admin endpoint. 203 type PrivateAdminAPI struct { 204 eth *IntChain 205 } 206 207 // NewPrivateAdminAPI creates a new API definition for the full node private 208 // admin methods of the IntChain service. 209 func NewPrivateAdminAPI(eth *IntChain) *PrivateAdminAPI { 210 return &PrivateAdminAPI{eth: eth} 211 } 212 213 // ExportChain exports the current blockchain into a local file. 214 func (api *PrivateAdminAPI) ExportChain(file string) (bool, error) { 215 // Make sure we can create the file to export into 216 out, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm) 217 if err != nil { 218 return false, err 219 } 220 defer out.Close() 221 222 var writer io.Writer = out 223 if strings.HasSuffix(file, ".gz") { 224 writer = gzip.NewWriter(writer) 225 defer writer.(*gzip.Writer).Close() 226 } 227 228 // Export the blockchain 229 if err := api.eth.BlockChain().Export(writer); err != nil { 230 return false, err 231 } 232 return true, nil 233 } 234 235 func hasAllBlocks(chain *core.BlockChain, bs []*types.Block) bool { 236 for _, b := range bs { 237 if !chain.HasBlock(b.Hash(), b.NumberU64()) { 238 return false 239 } 240 } 241 242 return true 243 } 244 245 // ImportChain imports a blockchain from a local file. 246 func (api *PrivateAdminAPI) ImportChain(file string) (bool, error) { 247 // Make sure the can access the file to import 248 in, err := os.Open(file) 249 if err != nil { 250 return false, err 251 } 252 defer in.Close() 253 254 var reader io.Reader = in 255 if strings.HasSuffix(file, ".gz") { 256 if reader, err = gzip.NewReader(reader); err != nil { 257 return false, err 258 } 259 } 260 261 // Run actual the import in pre-configured batches 262 stream := rlp.NewStream(reader, 0) 263 264 blocks, index := make([]*types.Block, 0, 2500), 0 265 for batch := 0; ; batch++ { 266 // Load a batch of blocks from the input file 267 for len(blocks) < cap(blocks) { 268 block := new(types.Block) 269 if err := stream.Decode(block); err == io.EOF { 270 break 271 } else if err != nil { 272 return false, fmt.Errorf("block %d: failed to parse: %v", index, err) 273 } 274 blocks = append(blocks, block) 275 index++ 276 } 277 if len(blocks) == 0 { 278 break 279 } 280 281 if hasAllBlocks(api.eth.BlockChain(), blocks) { 282 blocks = blocks[:0] 283 continue 284 } 285 // Import the batch and reset the buffer 286 if _, err := api.eth.BlockChain().InsertChain(blocks); err != nil { 287 return false, fmt.Errorf("batch %d: failed to insert: %v", batch, err) 288 } 289 blocks = blocks[:0] 290 } 291 return true, nil 292 } 293 294 func (api *PrivateAdminAPI) PruneStateData(height *hexutil.Uint64) (bool, error) { 295 var blockNumber uint64 296 if height != nil && *height > 0 { 297 blockNumber = uint64(*height) 298 } 299 300 go api.eth.StartScanAndPrune(blockNumber) 301 return true, nil 302 } 303 304 func (api *PrivateAdminAPI) LatestPruneState() (*datareduction.PruneStatus, error) { 305 status := datareduction.GetLatestStatus(api.eth.pruneDb) 306 status.LatestBlockNumber = api.eth.blockchain.CurrentHeader().Number.Uint64() 307 return status, nil 308 } 309 310 // PublicDebugAPI is the collection of IntChain full node APIs exposed 311 // over the public debugging endpoint. 312 type PublicDebugAPI struct { 313 eth *IntChain 314 } 315 316 // NewPublicDebugAPI creates a new API definition for the full node- 317 // related public debug methods of the IntChain service. 318 func NewPublicDebugAPI(eth *IntChain) *PublicDebugAPI { 319 return &PublicDebugAPI{eth: eth} 320 } 321 322 // DumpBlock retrieves the entire state of the database at a given block. 323 func (api *PublicDebugAPI) DumpBlock(blockNr rpc.BlockNumber) (state.Dump, error) { 324 if blockNr == rpc.PendingBlockNumber { 325 // If we're dumping the pending state, we need to request 326 // both the pending block as well as the pending state from 327 // the miner and operate on those 328 _, stateDb := api.eth.miner.Pending() 329 return stateDb.RawDump(), nil 330 } 331 var block *types.Block 332 if blockNr == rpc.LatestBlockNumber { 333 block = api.eth.blockchain.CurrentBlock() 334 } else { 335 block = api.eth.blockchain.GetBlockByNumber(uint64(blockNr)) 336 } 337 if block == nil { 338 return state.Dump{}, fmt.Errorf("block #%d not found", blockNr) 339 } 340 stateDb, err := api.eth.BlockChain().StateAt(block.Root()) 341 if err != nil { 342 return state.Dump{}, err 343 } 344 return stateDb.RawDump(), nil 345 } 346 347 // PrivateDebugAPI is the collection of IntChain full node APIs exposed over 348 // the private debugging endpoint. 349 type PrivateDebugAPI struct { 350 config *params.ChainConfig 351 eth *IntChain 352 } 353 354 // NewPrivateDebugAPI creates a new API definition for the full node-related 355 // private debug methods of the IntChain service. 356 func NewPrivateDebugAPI(config *params.ChainConfig, eth *IntChain) *PrivateDebugAPI { 357 return &PrivateDebugAPI{config: config, eth: eth} 358 } 359 360 // Preimage is a debug API function that returns the preimage for a sha3 hash, if known. 361 func (api *PrivateDebugAPI) Preimage(ctx context.Context, hash common.Hash) (hexutil.Bytes, error) { 362 if preimage := rawdb.ReadPreimage(api.eth.ChainDb(), hash); preimage != nil { 363 return preimage, nil 364 } 365 return nil, errors.New("unknown preimage") 366 } 367 368 // RemotePreimage is a debug API function that start to sync the preimage for a sha3 hash from the best remote peer. 369 func (api *PrivateDebugAPI) RemotePreimage(ctx context.Context, hash common.Hash) (hexutil.Bytes, error) { 370 peer := api.eth.protocolManager.peers.BestPeer() 371 372 hashes := make([]common.Hash, 0) 373 return nil, peer.RequestPreimages(append(hashes, hash)) 374 } 375 376 // RemovePreimage is a debug API function that remove the preimage for a sha3 hash, if known. 377 func (api *PrivateDebugAPI) RemovePreimage(ctx context.Context, hash common.Hash) (hexutil.Bytes, error) { 378 rawdb.DeletePreimage(api.eth.ChainDb(), hash) 379 return nil, nil 380 } 381 382 // Testing method 383 func (api *PrivateDebugAPI) BrokenPreimage(ctx context.Context, hash common.Hash, preimage hexutil.Bytes) (hexutil.Bytes, error) { 384 // Broken the preimage 385 rawdb.WritePreimages(api.eth.ChainDb(), map[common.Hash][]byte{hash: preimage}) 386 // try to read it from db 387 if read_preimage := rawdb.ReadPreimage(api.eth.ChainDb(), hash); read_preimage != nil { 388 return read_preimage, nil 389 } 390 return nil, errors.New("broken preimage failed") 391 } 392 393 func (api *PrivateDebugAPI) FindBadPreimage(ctx context.Context) (interface{}, error) { 394 395 images := make(map[common.Hash]string) 396 397 // Iterate the entire sha3 preimages for checking 398 db := api.eth.ChainDb() //.blockchain.StateCache().TrieDB().DiskDB().(ethdb.Database) 399 it := db.NewIteratorWithPrefix([]byte("secure-key-")) 400 for it.Next() { 401 keyHash := common.BytesToHash(it.Key()) 402 valueHash := crypto.Keccak256Hash(it.Value()) 403 if keyHash != valueHash { 404 // Add bad preimages 405 images[keyHash] = common.Bytes2Hex(it.Value()) 406 } 407 } 408 it.Release() 409 410 return images, nil 411 } 412 413 // GetBadBLocks returns a list of the last 'bad blocks' that the client has seen on the network 414 // and returns them as a JSON list of block-hashes 415 func (api *PrivateDebugAPI) GetBadBlocks(ctx context.Context) ([]core.BadBlockArgs, error) { 416 return api.eth.BlockChain().BadBlocks() 417 } 418 419 // StorageRangeResult is the result of a debug_storageRangeAt API call. 420 type StorageRangeResult struct { 421 Storage storageMap `json:"storage"` 422 NextKey *common.Hash `json:"nextKey"` // nil if Storage includes the last key in the trie. 423 } 424 425 type storageMap map[common.Hash]storageEntry 426 427 type storageEntry struct { 428 Key *common.Hash `json:"key"` 429 Value common.Hash `json:"value"` 430 } 431 432 // StorageRangeAt returns the storage at the given block height and transaction index. 433 func (api *PrivateDebugAPI) StorageRangeAt(ctx context.Context, blockHash common.Hash, txIndex int, contractAddress common.Address, keyStart hexutil.Bytes, maxResult int) (StorageRangeResult, error) { 434 _, _, statedb, err := api.computeTxEnv(blockHash, txIndex, 0) 435 if err != nil { 436 return StorageRangeResult{}, err 437 } 438 st := statedb.StorageTrie(contractAddress) 439 if st == nil { 440 return StorageRangeResult{}, fmt.Errorf("account %x doesn't exist", contractAddress) 441 } 442 return storageRangeAt(st, keyStart, maxResult) 443 } 444 445 func storageRangeAt(st state.Trie, start []byte, maxResult int) (StorageRangeResult, error) { 446 it := trie.NewIterator(st.NodeIterator(start)) 447 result := StorageRangeResult{Storage: storageMap{}} 448 for i := 0; i < maxResult && it.Next(); i++ { 449 _, content, _, err := rlp.Split(it.Value) 450 if err != nil { 451 return StorageRangeResult{}, err 452 } 453 e := storageEntry{Value: common.BytesToHash(content)} 454 if preimage := st.GetKey(it.Key); preimage != nil { 455 preimage := common.BytesToHash(preimage) 456 e.Key = &preimage 457 } 458 result.Storage[common.BytesToHash(it.Key)] = e 459 } 460 // Add the 'next key' so clients can continue downloading. 461 if it.Next() { 462 next := common.BytesToHash(it.Key) 463 result.NextKey = &next 464 } 465 return result, nil 466 } 467 468 // GetModifiedAccountsByumber returns all accounts that have changed between the 469 // two blocks specified. A change is defined as a difference in nonce, balance, 470 // code hash, or storage hash. 471 // 472 // With one parameter, returns the list of accounts modified in the specified block. 473 func (api *PrivateDebugAPI) GetModifiedAccountsByNumber(startNum uint64, endNum *uint64) ([]common.Address, error) { 474 var startBlock, endBlock *types.Block 475 476 startBlock = api.eth.blockchain.GetBlockByNumber(startNum) 477 if startBlock == nil { 478 return nil, fmt.Errorf("start block %x not found", startNum) 479 } 480 481 if endNum == nil { 482 endBlock = startBlock 483 startBlock = api.eth.blockchain.GetBlockByHash(startBlock.ParentHash()) 484 if startBlock == nil { 485 return nil, fmt.Errorf("block %x has no parent", endBlock.Number()) 486 } 487 } else { 488 endBlock = api.eth.blockchain.GetBlockByNumber(*endNum) 489 if endBlock == nil { 490 return nil, fmt.Errorf("end block %d not found", *endNum) 491 } 492 } 493 return api.getModifiedAccounts(startBlock, endBlock) 494 } 495 496 // GetModifiedAccountsByHash returns all accounts that have changed between the 497 // two blocks specified. A change is defined as a difference in nonce, balance, 498 // code hash, or storage hash. 499 // 500 // With one parameter, returns the list of accounts modified in the specified block. 501 func (api *PrivateDebugAPI) GetModifiedAccountsByHash(startHash common.Hash, endHash *common.Hash) ([]common.Address, error) { 502 var startBlock, endBlock *types.Block 503 startBlock = api.eth.blockchain.GetBlockByHash(startHash) 504 if startBlock == nil { 505 return nil, fmt.Errorf("start block %x not found", startHash) 506 } 507 508 if endHash == nil { 509 endBlock = startBlock 510 startBlock = api.eth.blockchain.GetBlockByHash(startBlock.ParentHash()) 511 if startBlock == nil { 512 return nil, fmt.Errorf("block %x has no parent", endBlock.Number()) 513 } 514 } else { 515 endBlock = api.eth.blockchain.GetBlockByHash(*endHash) 516 if endBlock == nil { 517 return nil, fmt.Errorf("end block %x not found", *endHash) 518 } 519 } 520 return api.getModifiedAccounts(startBlock, endBlock) 521 } 522 523 func (api *PrivateDebugAPI) getModifiedAccounts(startBlock, endBlock *types.Block) ([]common.Address, error) { 524 if startBlock.Number().Uint64() >= endBlock.Number().Uint64() { 525 return nil, fmt.Errorf("start block height (%d) must be less than end block height (%d)", startBlock.Number().Uint64(), endBlock.Number().Uint64()) 526 } 527 triedb := api.eth.BlockChain().StateCache().TrieDB() 528 529 oldTrie, err := trie.NewSecure(startBlock.Root(), triedb) 530 if err != nil { 531 return nil, err 532 } 533 newTrie, err := trie.NewSecure(endBlock.Root(), triedb) 534 if err != nil { 535 return nil, err 536 } 537 538 diff, _ := trie.NewDifferenceIterator(oldTrie.NodeIterator([]byte{}), newTrie.NodeIterator([]byte{})) 539 iter := trie.NewIterator(diff) 540 541 var dirty []common.Address 542 for iter.Next() { 543 key := newTrie.GetKey(iter.Key) 544 if key == nil { 545 return nil, fmt.Errorf("no preimage found for hash %x", iter.Key) 546 } 547 dirty = append(dirty, common.BytesToAddress(key)) 548 } 549 return dirty, nil 550 } 551 552 // ReadRawDBNode is a debug API function that returns the node rlp data for a hash key, if known. 553 func (api *PrivateDebugAPI) ReadRawDBNode(ctx context.Context, hash common.Hash) (hexutil.Bytes, error) { 554 if exist, _ := api.eth.ChainDb().Has(hash.Bytes()); exist { 555 return api.eth.ChainDb().Get(hash.Bytes()) 556 } 557 return nil, errors.New("key not exist") 558 } 559 560 func (api *PrivateDebugAPI) BroadcastRawDBNode(ctx context.Context, hash common.Hash) (map[string]error, error) { 561 result := make(map[string]error) 562 if exist, _ := api.eth.ChainDb().Has(hash.Bytes()); exist { 563 data, _ := api.eth.chainDb.Get(hash.Bytes()) 564 // Broadcast the node to other peers 565 for _, peer := range api.eth.protocolManager.peers.Peers() { 566 result[peer.id] = peer.SendTrieNodeData([][]byte{data}) 567 } 568 } 569 return result, nil 570 } 571 572 type resultNode struct { 573 Key common.Hash `json:"hash"` 574 Val hexutil.Bytes `json:"value"` 575 } 576 577 func (api *PrivateDebugAPI) PrintTrieNode(ctx context.Context, root common.Hash) ([]resultNode, error) { 578 result := make([]resultNode, 0) 579 t, _ := trie.NewSecure(root, trie.NewDatabase(api.eth.chainDb)) 580 it := t.NodeIterator(nil) 581 for it.Next(true) { 582 if !it.Leaf() { 583 h := it.Hash() 584 v, _ := api.eth.chainDb.Get(h.Bytes()) 585 result = append(result, resultNode{h, v}) 586 } 587 } 588 return result, it.Error() 589 }