diff --git a/les/odr_requests.go b/les/odr_requests.go index 3c4dd7090f..7a0d998280 100644 --- a/les/odr_requests.go +++ b/les/odr_requests.go @@ -197,15 +197,15 @@ func (r *TrieRequest) GetCost(peer *peer) uint64 { // CanSend tells if a certain peer is suitable for serving the given request func (r *TrieRequest) CanSend(peer *peer) bool { - return peer.HasBlock(r.Id.BlockHash, r.Id.BlockNumber, true) + return peer.HasBlock(r.ID.BlockHash, r.ID.BlockNumber, true) } // Request sends an ODR request to the LES network (implementation of LesOdrRequest) func (r *TrieRequest) Request(reqID uint64, peer *peer) error { - peer.Log().Debug("Requesting trie proof", "root", r.Id.Root, "key", r.Key) + peer.Log().Debug("Requesting trie proof", "root", r.ID.Root, "key", r.Key) req := ProofReq{ - BHash: r.Id.BlockHash, - AccKey: r.Id.AccKey, + BHash: r.ID.BlockHash, + AccKey: r.ID.AccKey, Key: r.Key, } return peer.RequestProofs(reqID, r.GetCost(peer), []ProofReq{req}) @@ -215,7 +215,7 @@ func (r *TrieRequest) Request(reqID uint64, peer *peer) error { // returns true and stores results in memory if the message was a valid reply // to the request (implementation of LesOdrRequest) func (r *TrieRequest) Validate(db ethdb.Database, msg *Msg) error { - log.Debug("Validating trie proof", "root", r.Id.Root, "key", r.Key) + log.Debug("Validating trie proof", "root", r.ID.Root, "key", r.Key) if msg.MsgType != MsgProofsV2 { return errInvalidMessageType @@ -224,7 +224,7 @@ func (r *TrieRequest) Validate(db ethdb.Database, msg *Msg) error { // Verify the proof and store if checks out nodeSet := proofs.NodeSet() reads := &readTraceDB{db: nodeSet} - if _, _, err := trie.VerifyProof(r.Id.Root, r.Key, reads); err != nil { + if _, _, err := trie.VerifyProof(r.ID.Root, r.Key, reads); err != nil { return fmt.Errorf("merkle proof verification failed: %v", err) } // check if all nodes have been read by VerifyProof @@ -251,15 +251,15 @@ func (r *CodeRequest) GetCost(peer *peer) uint64 { // CanSend tells if a certain peer is suitable for serving the given request func (r *CodeRequest) CanSend(peer *peer) bool { - return peer.HasBlock(r.Id.BlockHash, r.Id.BlockNumber, true) + return peer.HasBlock(r.ID.BlockHash, r.ID.BlockNumber, true) } // Request sends an ODR request to the LES network (implementation of LesOdrRequest) func (r *CodeRequest) Request(reqID uint64, peer *peer) error { peer.Log().Debug("Requesting code data", "hash", r.Hash) req := CodeReq{ - BHash: r.Id.BlockHash, - AccKey: r.Id.AccKey, + BHash: r.ID.BlockHash, + AccKey: r.ID.AccKey, } return peer.RequestCode(reqID, r.GetCost(peer), []CodeReq{req}) } @@ -326,7 +326,7 @@ func (r *ChtRequest) CanSend(peer *peer) bool { defer peer.lock.RUnlock() if r.Untrusted { - return peer.headInfo.Number >= r.BlockNum && peer.id == r.PeerId + return peer.headInfo.Number >= r.BlockNum && peer.id == r.PeerID } else { return peer.headInfo.Number >= r.Config.ChtConfirms && r.ChtNum <= (peer.headInfo.Number-r.Config.ChtConfirms)/r.Config.ChtSize } diff --git a/light/lightchain.go b/light/lightchain.go index 90ea263237..5ff99b5423 100644 --- a/light/lightchain.go +++ b/light/lightchain.go @@ -216,6 +216,7 @@ func (lc *LightChain) Genesis() *types.Block { return lc.genesisBlock } +// StateCache is not implemented func (lc *LightChain) StateCache() state.Database { panic("not implemented") } @@ -432,8 +433,8 @@ func (lc *LightChain) HasHeader(hash common.Hash, number uint64) bool { } // GetCanonicalHash returns the canonical hash for a given block number -func (bc *LightChain) GetCanonicalHash(number uint64) common.Hash { - return bc.hc.GetCanonicalHash(number) +func (lc *LightChain) GetCanonicalHash(number uint64) common.Hash { + return lc.hc.GetCanonicalHash(number) } // GetBlockHashesFromHash retrieves a number of block hashes starting at a given diff --git a/light/odr.go b/light/odr.go index 907712ede7..5b21e9345c 100644 --- a/light/odr.go +++ b/light/odr.go @@ -83,7 +83,7 @@ func StorageTrieID(state *TrieID, addrHash, root common.Hash) *TrieID { // TrieRequest is the ODR request type for state/storage trie entries type TrieRequest struct { OdrRequest - Id *TrieID + ID *TrieID Key []byte Proof *NodeSet } @@ -96,7 +96,7 @@ func (req *TrieRequest) StoreResult(db ethdb.Database) { // CodeRequest is the ODR request type for retrieving contract code type CodeRequest struct { OdrRequest - Id *TrieID // references storage trie of the account + ID *TrieID // references storage trie of the account Hash common.Hash Data []byte } @@ -140,7 +140,7 @@ func (req *ReceiptsRequest) StoreResult(db ethdb.Database) { type ChtRequest struct { OdrRequest Untrusted bool // Indicator whether the result retrieved is trusted or not - PeerId string // The specified peer id from which to retrieve data. + PeerID string // The specified peer id from which to retrieve data. Config *IndexerConfig ChtNum, BlockNum uint64 ChtRoot common.Hash diff --git a/light/odr_util.go b/light/odr_util.go index 2c820d40c7..eecf4f5049 100644 --- a/light/odr_util.go +++ b/light/odr_util.go @@ -30,6 +30,7 @@ import ( var sha3Nil = crypto.Keccak256Hash(nil) +// GetHeaderByNumber will return the header by the provided number func GetHeaderByNumber(ctx context.Context, odr OdrBackend, number uint64) (*types.Header, error) { db := odr.Database() hash := rawdb.ReadCanonicalHash(db, number) @@ -71,14 +72,15 @@ func GetHeaderByNumber(ctx context.Context, odr OdrBackend, number uint64) (*typ // GetUntrustedHeaderByNumber fetches specified block header without correctness checking. // Note this function should only be used in light client checkpoint syncing. -func GetUntrustedHeaderByNumber(ctx context.Context, odr OdrBackend, number uint64, peerId string) (*types.Header, error) { - r := &ChtRequest{BlockNum: number, ChtNum: number / odr.IndexerConfig().ChtSize, Untrusted: true, PeerId: peerId, Config: odr.IndexerConfig()} +func GetUntrustedHeaderByNumber(ctx context.Context, odr OdrBackend, number uint64, peerID string) (*types.Header, error) { + r := &ChtRequest{BlockNum: number, ChtNum: number / odr.IndexerConfig().ChtSize, Untrusted: true, PeerID: peerID, Config: odr.IndexerConfig()} if err := odr.Retrieve(ctx, r); err != nil { return nil, err } return r.Header, nil } +// GetCanonicalHash will return the canonical hash func GetCanonicalHash(ctx context.Context, odr OdrBackend, number uint64) (common.Hash, error) { hash := rawdb.ReadCanonicalHash(odr.Database(), number) if (hash != common.Hash{}) { @@ -97,11 +99,11 @@ func GetBodyRLP(ctx context.Context, odr OdrBackend, hash common.Hash, number ui return data, nil } r := &BlockRequest{Hash: hash, Number: number} - if err := odr.Retrieve(ctx, r); err != nil { + err := odr.Retrieve(ctx, r) + if err != nil { return nil, err - } else { - return r.Rlp, nil } + return r.Rlp, nil } // GetBody retrieves the block body (transactons, uncles) corresponding to the @@ -253,32 +255,32 @@ func GetBloomBits(ctx context.Context, odr OdrBackend, bitIdx uint, sectionIdxLi r := &BloomRequest{BloomTrieRoot: GetBloomTrieRoot(db, bloomTrieCount-1, sectionHead), BloomTrieNum: bloomTrieCount - 1, BitIdx: bitIdx, SectionIndexList: reqList, Config: odr.IndexerConfig()} - if err := odr.Retrieve(ctx, r); err != nil { + err := odr.Retrieve(ctx, r) + if err != nil { return nil, err - } else { - for i, idx := range reqIdx { - result[idx] = r.BloomBits[i] - } - return result, nil } + for i, idx := range reqIdx { + result[idx] = r.BloomBits[i] + } + return result, nil } // GetTransaction retrieves a canonical transaction by hash and also returns its position in the chain func GetTransaction(ctx context.Context, odr OdrBackend, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) { r := &TxStatusRequest{Hashes: []common.Hash{txHash}} - if err := odr.Retrieve(ctx, r); err != nil || r.Status[0].Status != core.TxStatusIncluded { + err := odr.Retrieve(ctx, r) + if err != nil || r.Status[0].Status != core.TxStatusIncluded { return nil, common.Hash{}, 0, 0, err - } else { - pos := r.Status[0].Lookup - // first ensure that we have the header, otherwise block body retrieval will fail - // also verify if this is a canonical block by getting the header by number and checking its hash - if header, err := GetHeaderByNumber(ctx, odr, pos.BlockIndex); err != nil || header.Hash() != pos.BlockHash { - return nil, common.Hash{}, 0, 0, err - } - if body, err := GetBody(ctx, odr, pos.BlockHash, pos.BlockIndex); err != nil || uint64(len(body.Transactions)) <= pos.Index || body.Transactions[pos.Index].Hash() != txHash { - return nil, common.Hash{}, 0, 0, err - } else { - return body.Transactions[pos.Index], pos.BlockHash, pos.BlockIndex, pos.Index, nil - } } + pos := r.Status[0].Lookup + // first ensure that we have the header, otherwise block body retrieval will fail + // also verify if this is a canonical block by getting the header by number and checking its hash + if header, err := GetHeaderByNumber(ctx, odr, pos.BlockIndex); err != nil || header.Hash() != pos.BlockHash { + return nil, common.Hash{}, 0, 0, err + } + body, err := GetBody(ctx, odr, pos.BlockHash, pos.BlockIndex) + if err != nil || uint64(len(body.Transactions)) <= pos.Index || body.Transactions[pos.Index].Hash() != txHash { + return nil, common.Hash{}, 0, 0, err + } + return body.Transactions[pos.Index], pos.BlockHash, pos.BlockIndex, pos.Index, nil } diff --git a/light/postprocess.go b/light/postprocess.go index af3b257923..3a2c442276 100644 --- a/light/postprocess.go +++ b/light/postprocess.go @@ -102,6 +102,7 @@ var ( errNoTrustedBloomTrie = errors.New("no trusted bloom trie") errNoHeader = errors.New("header not found") chtPrefix = []byte("chtRootV2-") // chtPrefix + chtNum (uint64 big endian) -> trie root hash + // ChtTablePrefix is the prefix used "cht-" ChtTablePrefix = "cht-" ) @@ -225,6 +226,7 @@ func (c *ChtIndexerBackend) Commit() error { var ( bloomTriePrefix = []byte("bltRoot-") // bloomTriePrefix + bloomTrieNum (uint64 big endian) -> trie root hash + // BloomTrieTablePrefix is the prefix used "blt-" BloomTrieTablePrefix = "blt-" ) diff --git a/light/trie.go b/light/trie.go index e512bf6f95..18db8a9932 100644 --- a/light/trie.go +++ b/light/trie.go @@ -29,11 +29,13 @@ import ( "github.com/ethereum/go-ethereum/trie" ) +// NewState will return the new state func NewState(ctx context.Context, head *types.Header, odr OdrBackend) *state.StateDB { state, _ := state.New(head.Root, NewStateDatabase(ctx, head, odr)) return state } +// NewStateDatabase will return the new state of the database func NewStateDatabase(ctx context.Context, head *types.Header, odr OdrBackend) state.Database { return &odrDatabase{ctx, StateTrieID(head), odr} } @@ -75,7 +77,7 @@ func (db *odrDatabase) ContractCode(addrHash, codeHash common.Hash) ([]byte, err } id := *db.id id.AccKey = addrHash[:] - req := &CodeRequest{Id: &id, Hash: codeHash} + req := &CodeRequest{ID: &id, Hash: codeHash} err := db.backend.Retrieve(db.ctx, req) return req.Data, err } @@ -159,7 +161,7 @@ func (t *odrTrie) do(key []byte, fn func() error) error { if _, ok := err.(*trie.MissingNodeError); !ok { return err } - r := &TrieRequest{Id: t.id, Key: key} + r := &TrieRequest{ID: t.id, Key: key} if err := t.db.backend.Retrieve(t.db.ctx, r); err != nil { return err } @@ -214,7 +216,7 @@ func (it *nodeIterator) do(fn func() error) { return } lasthash = missing.NodeHash - r := &TrieRequest{Id: it.t.id, Key: nibblesToKey(missing.Path)} + r := &TrieRequest{ID: it.t.id, Key: nibblesToKey(missing.Path)} if it.err = it.t.db.backend.Retrieve(it.t.db.ctx, r); it.err != nil { return } diff --git a/light/txpool.go b/light/txpool.go index 11a0e76ae0..6b2eb3cf30 100644 --- a/light/txpool.go +++ b/light/txpool.go @@ -446,7 +446,7 @@ func (pool *TxPool) Add(ctx context.Context, tx *types.Transaction) error { return nil } -// AddTransactions adds all valid transactions to the pool and passes them to +// AddBatch adds all valid transactions to the pool and passes them to // the tx relay backend func (pool *TxPool) AddBatch(ctx context.Context, txs []*types.Transaction) { pool.mu.Lock() diff --git a/light/txpool_test.go b/light/txpool_test.go index 0996bd7c9c..dedf78d3f3 100644 --- a/light/txpool_test.go +++ b/light/txpool_test.go @@ -36,19 +36,19 @@ type testTxRelay struct { send, discard, mined chan int } -func (self *testTxRelay) Send(txs types.Transactions) { - self.send <- len(txs) +func (tr *testTxRelay) Send(txs types.Transactions) { + tr.send <- len(txs) } -func (self *testTxRelay) NewHead(head common.Hash, mined []common.Hash, rollback []common.Hash) { +func (tr *testTxRelay) NewHead(head common.Hash, mined []common.Hash, rollback []common.Hash) { m := len(mined) if m != 0 { - self.mined <- m + tr.mined <- m } } -func (self *testTxRelay) Discard(hashes []common.Hash) { - self.discard <- len(hashes) +func (tr *testTxRelay) Discard(hashes []common.Hash) { + tr.discard <- len(hashes) } const poolTestTxs = 1000