light,les: fix most golint warnings

This commit is contained in:
Mike Kinney 2019-12-09 21:40:38 -08:00
parent f5cf260309
commit 967a7b4339
8 changed files with 56 additions and 49 deletions

View file

@ -197,15 +197,15 @@ func (r *TrieRequest) GetCost(peer *peer) uint64 {
// CanSend tells if a certain peer is suitable for serving the given request // CanSend tells if a certain peer is suitable for serving the given request
func (r *TrieRequest) CanSend(peer *peer) bool { 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) // Request sends an ODR request to the LES network (implementation of LesOdrRequest)
func (r *TrieRequest) Request(reqID uint64, peer *peer) error { 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{ req := ProofReq{
BHash: r.Id.BlockHash, BHash: r.ID.BlockHash,
AccKey: r.Id.AccKey, AccKey: r.ID.AccKey,
Key: r.Key, Key: r.Key,
} }
return peer.RequestProofs(reqID, r.GetCost(peer), []ProofReq{req}) 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 // returns true and stores results in memory if the message was a valid reply
// to the request (implementation of LesOdrRequest) // to the request (implementation of LesOdrRequest)
func (r *TrieRequest) Validate(db ethdb.Database, msg *Msg) error { 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 { if msg.MsgType != MsgProofsV2 {
return errInvalidMessageType return errInvalidMessageType
@ -224,7 +224,7 @@ func (r *TrieRequest) Validate(db ethdb.Database, msg *Msg) error {
// Verify the proof and store if checks out // Verify the proof and store if checks out
nodeSet := proofs.NodeSet() nodeSet := proofs.NodeSet()
reads := &readTraceDB{db: 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) return fmt.Errorf("merkle proof verification failed: %v", err)
} }
// check if all nodes have been read by VerifyProof // 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 // CanSend tells if a certain peer is suitable for serving the given request
func (r *CodeRequest) CanSend(peer *peer) bool { 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) // Request sends an ODR request to the LES network (implementation of LesOdrRequest)
func (r *CodeRequest) Request(reqID uint64, peer *peer) error { func (r *CodeRequest) Request(reqID uint64, peer *peer) error {
peer.Log().Debug("Requesting code data", "hash", r.Hash) peer.Log().Debug("Requesting code data", "hash", r.Hash)
req := CodeReq{ req := CodeReq{
BHash: r.Id.BlockHash, BHash: r.ID.BlockHash,
AccKey: r.Id.AccKey, AccKey: r.ID.AccKey,
} }
return peer.RequestCode(reqID, r.GetCost(peer), []CodeReq{req}) return peer.RequestCode(reqID, r.GetCost(peer), []CodeReq{req})
} }
@ -326,7 +326,7 @@ func (r *ChtRequest) CanSend(peer *peer) bool {
defer peer.lock.RUnlock() defer peer.lock.RUnlock()
if r.Untrusted { if r.Untrusted {
return peer.headInfo.Number >= r.BlockNum && peer.id == r.PeerId return peer.headInfo.Number >= r.BlockNum && peer.id == r.PeerID
} else { } else {
return peer.headInfo.Number >= r.Config.ChtConfirms && r.ChtNum <= (peer.headInfo.Number-r.Config.ChtConfirms)/r.Config.ChtSize return peer.headInfo.Number >= r.Config.ChtConfirms && r.ChtNum <= (peer.headInfo.Number-r.Config.ChtConfirms)/r.Config.ChtSize
} }

View file

@ -216,6 +216,7 @@ func (lc *LightChain) Genesis() *types.Block {
return lc.genesisBlock return lc.genesisBlock
} }
// StateCache is not implemented
func (lc *LightChain) StateCache() state.Database { func (lc *LightChain) StateCache() state.Database {
panic("not implemented") 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 // GetCanonicalHash returns the canonical hash for a given block number
func (bc *LightChain) GetCanonicalHash(number uint64) common.Hash { func (lc *LightChain) GetCanonicalHash(number uint64) common.Hash {
return bc.hc.GetCanonicalHash(number) return lc.hc.GetCanonicalHash(number)
} }
// GetBlockHashesFromHash retrieves a number of block hashes starting at a given // GetBlockHashesFromHash retrieves a number of block hashes starting at a given

View file

@ -83,7 +83,7 @@ func StorageTrieID(state *TrieID, addrHash, root common.Hash) *TrieID {
// TrieRequest is the ODR request type for state/storage trie entries // TrieRequest is the ODR request type for state/storage trie entries
type TrieRequest struct { type TrieRequest struct {
OdrRequest OdrRequest
Id *TrieID ID *TrieID
Key []byte Key []byte
Proof *NodeSet Proof *NodeSet
} }
@ -96,7 +96,7 @@ func (req *TrieRequest) StoreResult(db ethdb.Database) {
// CodeRequest is the ODR request type for retrieving contract code // CodeRequest is the ODR request type for retrieving contract code
type CodeRequest struct { type CodeRequest struct {
OdrRequest OdrRequest
Id *TrieID // references storage trie of the account ID *TrieID // references storage trie of the account
Hash common.Hash Hash common.Hash
Data []byte Data []byte
} }
@ -140,7 +140,7 @@ func (req *ReceiptsRequest) StoreResult(db ethdb.Database) {
type ChtRequest struct { type ChtRequest struct {
OdrRequest OdrRequest
Untrusted bool // Indicator whether the result retrieved is trusted or not 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 Config *IndexerConfig
ChtNum, BlockNum uint64 ChtNum, BlockNum uint64
ChtRoot common.Hash ChtRoot common.Hash

View file

@ -30,6 +30,7 @@ import (
var sha3Nil = crypto.Keccak256Hash(nil) 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) { func GetHeaderByNumber(ctx context.Context, odr OdrBackend, number uint64) (*types.Header, error) {
db := odr.Database() db := odr.Database()
hash := rawdb.ReadCanonicalHash(db, number) 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. // GetUntrustedHeaderByNumber fetches specified block header without correctness checking.
// Note this function should only be used in light client checkpoint syncing. // 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) { 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()} r := &ChtRequest{BlockNum: number, ChtNum: number / odr.IndexerConfig().ChtSize, Untrusted: true, PeerID: peerID, Config: odr.IndexerConfig()}
if err := odr.Retrieve(ctx, r); err != nil { if err := odr.Retrieve(ctx, r); err != nil {
return nil, err return nil, err
} }
return r.Header, nil return r.Header, nil
} }
// GetCanonicalHash will return the canonical hash
func GetCanonicalHash(ctx context.Context, odr OdrBackend, number uint64) (common.Hash, error) { func GetCanonicalHash(ctx context.Context, odr OdrBackend, number uint64) (common.Hash, error) {
hash := rawdb.ReadCanonicalHash(odr.Database(), number) hash := rawdb.ReadCanonicalHash(odr.Database(), number)
if (hash != common.Hash{}) { if (hash != common.Hash{}) {
@ -97,11 +99,11 @@ func GetBodyRLP(ctx context.Context, odr OdrBackend, hash common.Hash, number ui
return data, nil return data, nil
} }
r := &BlockRequest{Hash: hash, Number: number} 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 return nil, err
} else {
return r.Rlp, nil
} }
return r.Rlp, nil
} }
// GetBody retrieves the block body (transactons, uncles) corresponding to the // 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, r := &BloomRequest{BloomTrieRoot: GetBloomTrieRoot(db, bloomTrieCount-1, sectionHead), BloomTrieNum: bloomTrieCount - 1,
BitIdx: bitIdx, SectionIndexList: reqList, Config: odr.IndexerConfig()} 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 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 // 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) { func GetTransaction(ctx context.Context, odr OdrBackend, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
r := &TxStatusRequest{Hashes: []common.Hash{txHash}} 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 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
} }

View file

@ -102,6 +102,7 @@ var (
errNoTrustedBloomTrie = errors.New("no trusted bloom trie") errNoTrustedBloomTrie = errors.New("no trusted bloom trie")
errNoHeader = errors.New("header not found") errNoHeader = errors.New("header not found")
chtPrefix = []byte("chtRootV2-") // chtPrefix + chtNum (uint64 big endian) -> trie root hash chtPrefix = []byte("chtRootV2-") // chtPrefix + chtNum (uint64 big endian) -> trie root hash
// ChtTablePrefix is the prefix used "cht-"
ChtTablePrefix = "cht-" ChtTablePrefix = "cht-"
) )
@ -225,6 +226,7 @@ func (c *ChtIndexerBackend) Commit() error {
var ( var (
bloomTriePrefix = []byte("bltRoot-") // bloomTriePrefix + bloomTrieNum (uint64 big endian) -> trie root hash bloomTriePrefix = []byte("bltRoot-") // bloomTriePrefix + bloomTrieNum (uint64 big endian) -> trie root hash
// BloomTrieTablePrefix is the prefix used "blt-"
BloomTrieTablePrefix = "blt-" BloomTrieTablePrefix = "blt-"
) )

View file

@ -29,11 +29,13 @@ import (
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
) )
// NewState will return the new state
func NewState(ctx context.Context, head *types.Header, odr OdrBackend) *state.StateDB { func NewState(ctx context.Context, head *types.Header, odr OdrBackend) *state.StateDB {
state, _ := state.New(head.Root, NewStateDatabase(ctx, head, odr)) state, _ := state.New(head.Root, NewStateDatabase(ctx, head, odr))
return state return state
} }
// NewStateDatabase will return the new state of the database
func NewStateDatabase(ctx context.Context, head *types.Header, odr OdrBackend) state.Database { func NewStateDatabase(ctx context.Context, head *types.Header, odr OdrBackend) state.Database {
return &odrDatabase{ctx, StateTrieID(head), odr} return &odrDatabase{ctx, StateTrieID(head), odr}
} }
@ -75,7 +77,7 @@ func (db *odrDatabase) ContractCode(addrHash, codeHash common.Hash) ([]byte, err
} }
id := *db.id id := *db.id
id.AccKey = addrHash[:] id.AccKey = addrHash[:]
req := &CodeRequest{Id: &id, Hash: codeHash} req := &CodeRequest{ID: &id, Hash: codeHash}
err := db.backend.Retrieve(db.ctx, req) err := db.backend.Retrieve(db.ctx, req)
return req.Data, err return req.Data, err
} }
@ -159,7 +161,7 @@ func (t *odrTrie) do(key []byte, fn func() error) error {
if _, ok := err.(*trie.MissingNodeError); !ok { if _, ok := err.(*trie.MissingNodeError); !ok {
return err 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 { if err := t.db.backend.Retrieve(t.db.ctx, r); err != nil {
return err return err
} }
@ -214,7 +216,7 @@ func (it *nodeIterator) do(fn func() error) {
return return
} }
lasthash = missing.NodeHash 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 { if it.err = it.t.db.backend.Retrieve(it.t.db.ctx, r); it.err != nil {
return return
} }

View file

@ -446,7 +446,7 @@ func (pool *TxPool) Add(ctx context.Context, tx *types.Transaction) error {
return nil 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 // the tx relay backend
func (pool *TxPool) AddBatch(ctx context.Context, txs []*types.Transaction) { func (pool *TxPool) AddBatch(ctx context.Context, txs []*types.Transaction) {
pool.mu.Lock() pool.mu.Lock()

View file

@ -36,19 +36,19 @@ type testTxRelay struct {
send, discard, mined chan int send, discard, mined chan int
} }
func (self *testTxRelay) Send(txs types.Transactions) { func (tr *testTxRelay) Send(txs types.Transactions) {
self.send <- len(txs) 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) m := len(mined)
if m != 0 { if m != 0 {
self.mined <- m tr.mined <- m
} }
} }
func (self *testTxRelay) Discard(hashes []common.Hash) { func (tr *testTxRelay) Discard(hashes []common.Hash) {
self.discard <- len(hashes) tr.discard <- len(hashes)
} }
const poolTestTxs = 1000 const poolTestTxs = 1000