odr_util.go 5.3 KB

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