@@ -72,7 +72,7 @@ type BlockChain interface {
7272 GetHeaderByHash (hash common.Hash ) * types.Header
7373 CurrentHeader () * types.Header
7474 GetTd (hash common.Hash , number uint64 ) * big.Int
75- State () ( * state.StateDB , error )
75+ StateCache () state.Database
7676 InsertHeaderChain (chain []* types.Header , checkFreq int ) (int , error )
7777 Rollback (chain []common.Hash )
7878 GetHeaderByNumber (number uint64 ) * types.Header
@@ -642,24 +642,33 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
642642 if i != 0 && ! task .waitOrStop () {
643643 return
644644 }
645- // Retrieve the requested state entry, stopping if enough was found
646- if number := rawdb .ReadHeaderNumber (pm .chainDb , req .BHash ); number != nil {
647- if header := rawdb . ReadHeader ( pm . chainDb , req . BHash , * number ); header ! = nil {
648- statedb , err := pm . blockchain . State ( )
649- if err != nil {
650- continue
651- }
652- account , err := pm . getAccount ( statedb , header . Root , common . BytesToHash ( req . AccKey ))
653- if err != nil {
654- continue
655- }
656- code , _ := statedb . Database ().TrieDB (). Node ( common . BytesToHash ( account . CodeHash ) )
645+ // Look up the root hash belonging to the request
646+ number := rawdb .ReadHeaderNumber (pm .chainDb , req .BHash )
647+ if number = = nil {
648+ p . Log (). Warn ( "Failed to retrieve block num for code" , "hash" , req . BHash )
649+ continue
650+ }
651+ header := rawdb . ReadHeader ( pm . chainDb , req . BHash , * number )
652+ if header == nil {
653+ p . Log (). Warn ( "Failed to retrieve header for code" , "block" , * number , "hash" , req . BHash )
654+ continue
655+ }
656+ triedb := pm . blockchain . StateCache ().TrieDB ()
657657
658- data = append (data , code )
659- if bytes += len (code ); bytes >= softResponseLimit {
660- break
661- }
662- }
658+ account , err := pm .getAccount (triedb , header .Root , common .BytesToHash (req .AccKey ))
659+ if err != nil {
660+ p .Log ().Warn ("Failed to retrieve account for code" , "block" , header .Number , "hash" , header .Hash (), "account" , common .BytesToHash (req .AccKey ), "err" , err )
661+ continue
662+ }
663+ code , err := triedb .Node (common .BytesToHash (account .CodeHash ))
664+ if err != nil {
665+ p .Log ().Warn ("Failed to retrieve account code" , "block" , header .Number , "hash" , header .Hash (), "account" , common .BytesToHash (req .AccKey ), "codehash" , common .BytesToHash (account .CodeHash ), "err" , err )
666+ continue
667+ }
668+ // Accumulate the code and abort if enough data was retrieved
669+ data = append (data , code )
670+ if bytes += len (code ); bytes >= softResponseLimit {
671+ break
663672 }
664673 }
665674 sendResponse (req .ReqID , uint64 (reqCnt ), p .ReplyCode (req .ReqID , data ), task .done ())
@@ -779,34 +788,52 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
779788 if i != 0 && ! task .waitOrStop () {
780789 return
781790 }
782- // Retrieve the requested state entry, stopping if enough was found
783- if number := rawdb .ReadHeaderNumber (pm .chainDb , req .BHash ); number != nil {
784- if header := rawdb .ReadHeader (pm .chainDb , req .BHash , * number ); header != nil {
785- statedb , err := pm .blockchain .State ()
786- if err != nil {
787- continue
788- }
789- var trie state.Trie
790- if len (req .AccKey ) > 0 {
791- account , err := pm .getAccount (statedb , header .Root , common .BytesToHash (req .AccKey ))
792- if err != nil {
793- continue
794- }
795- trie , _ = statedb .Database ().OpenStorageTrie (common .BytesToHash (req .AccKey ), account .Root )
796- } else {
797- trie , _ = statedb .Database ().OpenTrie (header .Root )
798- }
799- if trie != nil {
800- var proof light.NodeList
801- trie .Prove (req .Key , 0 , & proof )
791+ // Look up the root hash belonging to the request
792+ number := rawdb .ReadHeaderNumber (pm .chainDb , req .BHash )
793+ if number == nil {
794+ p .Log ().Warn ("Failed to retrieve block num for proof" , "hash" , req .BHash )
795+ continue
796+ }
797+ header := rawdb .ReadHeader (pm .chainDb , req .BHash , * number )
798+ if header == nil {
799+ p .Log ().Warn ("Failed to retrieve header for proof" , "block" , * number , "hash" , req .BHash )
800+ continue
801+ }
802+ // Open the account or storage trie for the request
803+ statedb := pm .blockchain .StateCache ()
802804
803- proofs = append (proofs , proof )
804- if bytes += proof .DataSize (); bytes >= softResponseLimit {
805- break
806- }
807- }
805+ var trie state.Trie
806+ switch len (req .AccKey ) {
807+ case 0 :
808+ // No account key specified, open an account trie
809+ trie , err = statedb .OpenTrie (header .Root )
810+ if trie == nil || err != nil {
811+ p .Log ().Warn ("Failed to open storage trie for proof" , "block" , header .Number , "hash" , header .Hash (), "root" , header .Root , "err" , err )
812+ continue
813+ }
814+ default :
815+ // Account key specified, open a storage trie
816+ account , err := pm .getAccount (statedb .TrieDB (), header .Root , common .BytesToHash (req .AccKey ))
817+ if err != nil {
818+ p .Log ().Warn ("Failed to retrieve account for proof" , "block" , header .Number , "hash" , header .Hash (), "account" , common .BytesToHash (req .AccKey ), "err" , err )
819+ continue
820+ }
821+ trie , err = statedb .OpenStorageTrie (common .BytesToHash (req .AccKey ), account .Root )
822+ if trie == nil || err != nil {
823+ p .Log ().Warn ("Failed to open storage trie for proof" , "block" , header .Number , "hash" , header .Hash (), "account" , common .BytesToHash (req .AccKey ), "root" , account .Root , "err" , err )
824+ continue
808825 }
809826 }
827+ // Prove the user's request from the account or stroage trie
828+ var proof light.NodeList
829+ if err := trie .Prove (req .Key , 0 , & proof ); err != nil {
830+ p .Log ().Warn ("Failed to prove state request" , "block" , header .Number , "hash" , header .Hash (), "err" , err )
831+ continue
832+ }
833+ proofs = append (proofs , proof )
834+ if bytes += proof .DataSize (); bytes >= softResponseLimit {
835+ break
836+ }
810837 }
811838 sendResponse (req .ReqID , uint64 (reqCnt ), p .ReplyProofs (req .ReqID , proofs ), task .done ())
812839 }()
@@ -824,51 +851,67 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
824851 // Gather state data until the fetch or network limits is reached
825852 var (
826853 lastBHash common.Hash
827- statedb * state.StateDB
828854 root common.Hash
829855 )
830856 reqCnt := len (req .Reqs )
831857 if ! accept (req .ReqID , uint64 (reqCnt ), MaxProofsFetch ) {
832858 return errResp (ErrRequestRejected , "" )
833859 }
834860 go func () {
835-
836861 nodes := light .NewNodeSet ()
837862
838863 for i , req := range req .Reqs {
839864 if i != 0 && ! task .waitOrStop () {
840865 return
841866 }
842- // Look up the state belonging to the request
843- if statedb == nil || req .BHash != lastBHash {
844- statedb , root , lastBHash = nil , common.Hash {}, req .BHash
845-
846- if number := rawdb .ReadHeaderNumber (pm .chainDb , req .BHash ); number != nil {
847- if header := rawdb .ReadHeader (pm .chainDb , req .BHash , * number ); header != nil {
848- statedb , _ = pm .blockchain .State ()
849- root = header .Root
850- }
867+ // Look up the root hash belonging to the request
868+ var (
869+ number * uint64
870+ header * types.Header
871+ trie state.Trie
872+ )
873+ if req .BHash != lastBHash {
874+ root , lastBHash = common.Hash {}, req .BHash
875+
876+ if number = rawdb .ReadHeaderNumber (pm .chainDb , req .BHash ); number == nil {
877+ p .Log ().Warn ("Failed to retrieve block num for proof" , "hash" , req .BHash )
878+ continue
851879 }
880+ if header = rawdb .ReadHeader (pm .chainDb , req .BHash , * number ); header == nil {
881+ p .Log ().Warn ("Failed to retrieve header for proof" , "block" , * number , "hash" , req .BHash )
882+ continue
883+ }
884+ root = header .Root
852885 }
853- if statedb == nil {
854- continue
855- }
856- // Pull the account or storage trie of the request
857- var trie state.Trie
858- if len (req .AccKey ) > 0 {
859- account , err := pm .getAccount (statedb , root , common .BytesToHash (req .AccKey ))
886+ // Open the account or storage trie for the request
887+ statedb := pm .blockchain .StateCache ()
888+
889+ switch len (req .AccKey ) {
890+ case 0 :
891+ // No account key specified, open an account trie
892+ trie , err = statedb .OpenTrie (root )
893+ if trie == nil || err != nil {
894+ p .Log ().Warn ("Failed to open storage trie for proof" , "block" , header .Number , "hash" , header .Hash (), "root" , root , "err" , err )
895+ continue
896+ }
897+ default :
898+ // Account key specified, open a storage trie
899+ account , err := pm .getAccount (statedb .TrieDB (), root , common .BytesToHash (req .AccKey ))
860900 if err != nil {
901+ p .Log ().Warn ("Failed to retrieve account for proof" , "block" , header .Number , "hash" , header .Hash (), "account" , common .BytesToHash (req .AccKey ), "err" , err )
902+ continue
903+ }
904+ trie , err = statedb .OpenStorageTrie (common .BytesToHash (req .AccKey ), account .Root )
905+ if trie == nil || err != nil {
906+ p .Log ().Warn ("Failed to open storage trie for proof" , "block" , header .Number , "hash" , header .Hash (), "account" , common .BytesToHash (req .AccKey ), "root" , account .Root , "err" , err )
861907 continue
862908 }
863- trie , _ = statedb .Database ().OpenStorageTrie (common .BytesToHash (req .AccKey ), account .Root )
864- } else {
865- trie , _ = statedb .Database ().OpenTrie (root )
866909 }
867- if trie == nil {
910+ // Prove the user's request from the account or stroage trie
911+ if err := trie .Prove (req .Key , req .FromLevel , nodes ); err != nil {
912+ p .Log ().Warn ("Failed to prove state request" , "block" , header .Number , "hash" , header .Hash (), "err" , err )
868913 continue
869914 }
870- // Prove the user's request from the account or stroage trie
871- trie .Prove (req .Key , req .FromLevel , nodes )
872915 if nodes .DataSize () >= softResponseLimit {
873916 break
874917 }
@@ -1190,8 +1233,8 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
11901233}
11911234
11921235// getAccount retrieves an account from the state based at root.
1193- func (pm * ProtocolManager ) getAccount (statedb * state. StateDB , root , hash common.Hash ) (state.Account , error ) {
1194- trie , err := trie .New (root , statedb . Database (). TrieDB () )
1236+ func (pm * ProtocolManager ) getAccount (triedb * trie. Database , root , hash common.Hash ) (state.Account , error ) {
1237+ trie , err := trie .New (root , triedb )
11951238 if err != nil {
11961239 return state.Account {}, err
11971240 }
0 commit comments