odr_util.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright 2016 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. package light
  17. import (
  18. "bytes"
  19. "errors"
  20. "math/big"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. "github.com/ethereum/go-ethereum/ethdb"
  26. "github.com/ethereum/go-ethereum/logger"
  27. "github.com/ethereum/go-ethereum/logger/glog"
  28. "github.com/ethereum/go-ethereum/rlp"
  29. "golang.org/x/net/context"
  30. )
  31. var sha3_nil = crypto.Keccak256Hash(nil)
  32. var (
  33. ErrNoTrustedCht = errors.New("No trusted canonical hash trie")
  34. ErrNoHeader = errors.New("Header not found")
  35. ChtFrequency = uint64(4096)
  36. trustedChtKey = []byte("TrustedCHT")
  37. )
  38. type ChtNode struct {
  39. Hash common.Hash
  40. Td *big.Int
  41. }
  42. type TrustedCht struct {
  43. Number uint64
  44. Root common.Hash
  45. }
  46. func GetTrustedCht(db ethdb.Database) TrustedCht {
  47. data, _ := db.Get(trustedChtKey)
  48. var res TrustedCht
  49. if err := rlp.DecodeBytes(data, &res); err != nil {
  50. return TrustedCht{0, common.Hash{}}
  51. }
  52. return res
  53. }
  54. func WriteTrustedCht(db ethdb.Database, cht TrustedCht) {
  55. data, _ := rlp.EncodeToBytes(cht)
  56. db.Put(trustedChtKey, data)
  57. }
  58. func DeleteTrustedCht(db ethdb.Database) {
  59. db.Delete(trustedChtKey)
  60. }
  61. func GetHeaderByNumber(ctx context.Context, odr OdrBackend, number uint64) (*types.Header, error) {
  62. db := odr.Database()
  63. hash := core.GetCanonicalHash(db, number)
  64. if (hash != common.Hash{}) {
  65. // if there is a canonical hash, there is a header too
  66. header := core.GetHeader(db, hash, number)
  67. if header == nil {
  68. panic("Canonical hash present but header not found")
  69. }
  70. return header, nil
  71. }
  72. cht := GetTrustedCht(db)
  73. if number >= cht.Number*ChtFrequency {
  74. return nil, ErrNoTrustedCht
  75. }
  76. r := &ChtRequest{ChtRoot: cht.Root, ChtNum: cht.Number, BlockNum: number}
  77. if err := odr.Retrieve(ctx, r); err != nil {
  78. return nil, err
  79. } else {
  80. return r.Header, nil
  81. }
  82. }
  83. func GetCanonicalHash(ctx context.Context, odr OdrBackend, number uint64) (common.Hash, error) {
  84. hash := core.GetCanonicalHash(odr.Database(), number)
  85. if (hash != common.Hash{}) {
  86. return hash, nil
  87. }
  88. header, err := GetHeaderByNumber(ctx, odr, number)
  89. if header != nil {
  90. return header.Hash(), nil
  91. }
  92. return common.Hash{}, err
  93. }
  94. // retrieveContractCode tries to retrieve the contract code of the given account
  95. // with the given hash from the network (id points to the storage trie belonging
  96. // to the same account)
  97. func retrieveContractCode(ctx context.Context, odr OdrBackend, id *TrieID, hash common.Hash) ([]byte, error) {
  98. if hash == sha3_nil {
  99. return nil, nil
  100. }
  101. res, _ := odr.Database().Get(hash[:])
  102. if res != nil {
  103. return res, nil
  104. }
  105. r := &CodeRequest{Id: id, Hash: hash}
  106. if err := odr.Retrieve(ctx, r); err != nil {
  107. return nil, err
  108. } else {
  109. return r.Data, nil
  110. }
  111. }
  112. // GetBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
  113. func GetBodyRLP(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (rlp.RawValue, error) {
  114. if data := core.GetBodyRLP(odr.Database(), hash, number); data != nil {
  115. return data, nil
  116. }
  117. r := &BlockRequest{Hash: hash, Number: number}
  118. if err := odr.Retrieve(ctx, r); err != nil {
  119. return nil, err
  120. } else {
  121. return r.Rlp, nil
  122. }
  123. }
  124. // GetBody retrieves the block body (transactons, uncles) corresponding to the
  125. // hash.
  126. func GetBody(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (*types.Body, error) {
  127. data, err := GetBodyRLP(ctx, odr, hash, number)
  128. if err != nil {
  129. return nil, err
  130. }
  131. body := new(types.Body)
  132. if err := rlp.Decode(bytes.NewReader(data), body); err != nil {
  133. glog.V(logger.Error).Infof("invalid block body RLP for hash %x: %v", hash, err)
  134. return nil, err
  135. }
  136. return body, nil
  137. }
  138. // GetBlock retrieves an entire block corresponding to the hash, assembling it
  139. // back from the stored header and body.
  140. func GetBlock(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (*types.Block, error) {
  141. // Retrieve the block header and body contents
  142. header := core.GetHeader(odr.Database(), hash, number)
  143. if header == nil {
  144. return nil, ErrNoHeader
  145. }
  146. body, err := GetBody(ctx, odr, hash, number)
  147. if err != nil {
  148. return nil, err
  149. }
  150. // Reassemble the block and return
  151. return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles), nil
  152. }
  153. // GetBlockReceipts retrieves the receipts generated by the transactions included
  154. // in a block given by its hash.
  155. func GetBlockReceipts(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (types.Receipts, error) {
  156. receipts := core.GetBlockReceipts(odr.Database(), hash, number)
  157. if receipts != nil {
  158. return receipts, nil
  159. }
  160. r := &ReceiptsRequest{Hash: hash, Number: number}
  161. if err := odr.Retrieve(ctx, r); err != nil {
  162. return nil, err
  163. } else {
  164. return r.Receipts, nil
  165. }
  166. }