odr_util.go 5.5 KB

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