[DEV-240] Style fixes to satisfy golint (#112)

* [DEV-240] Unexport BlockDAG/SelectedTip, since it returns unexported *blockNode

* [DEV-240] Fix blockdag package comment to satisfy golint

* [DEV-240] Add comment explaining blank import of ffldb in blockdag/test_utils.go

* [DEV-240] Add comment to FullUTXOSet.Get

* [DEV-240] Unexported config.DefaultHomeDir

* [DEV-240] Remove blank import of ffldb from config/config.go

* [DEV-240] Added comment to daghash.Strings()

* [DEV-240] Added missing comments in hdkeychain/extendedkey.go

* [DEV-240] Re-activate goline in CI Dockerfile

* [DEV-240] Fixed some typos in comments

* [DEV-240] Typo fix in comment
This commit is contained in:
Svarog 2018-11-05 12:58:17 +02:00 committed by GitHub
parent 78e8c6084c
commit 35546b62d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 68 additions and 51 deletions

View File

@ -160,9 +160,9 @@ func newBlockNode(blockHeader *wire.BlockHeader, parents blockSet, phantomK uint
// Header constructs a block header from the node and returns it.
//
// This function is safe for concurrent access.
func (node *blockNode) Header() wire.BlockHeader {
func (node *blockNode) Header() *wire.BlockHeader {
// No lock is needed because all accessed fields are immutable.
return wire.BlockHeader{
return &wire.BlockHeader{
Version: node.version,
NumPrevBlocks: byte(len(node.parents)),
PrevBlocks: node.PrevHashes(),

View File

@ -130,7 +130,7 @@ func (dag *BlockDAG) findPreviousCheckpoint() (*blockNode, error) {
// When there is a next checkpoint and the height of the current best
// chain does not exceed it, the current checkpoint lockin is still
// the latest known checkpoint.
if dag.SelectedTip().height < dag.nextCheckpoint.Height {
if dag.selectedTip().height < dag.nextCheckpoint.Height {
return dag.checkpointNode, nil
}
@ -218,7 +218,7 @@ func (dag *BlockDAG) IsCheckpointCandidate(block *util.Block) (bool, error) {
// A checkpoint must be at least CheckpointConfirmations blocks
// before the end of the main chain.
dagHeight := dag.SelectedTip().height
dagHeight := dag.selectedTip().height
if node.height > (dagHeight - CheckpointConfirmations) {
return false, nil
}

View File

@ -322,7 +322,7 @@ func (dag *BlockDAG) CalcSequenceLock(tx *util.Tx, utxoSet UTXOSet, mempool bool
dag.dagLock.Lock()
defer dag.dagLock.Unlock()
return dag.calcSequenceLock(dag.SelectedTip(), utxoSet, tx, mempool)
return dag.calcSequenceLock(dag.selectedTip(), utxoSet, tx, mempool)
}
// calcSequenceLock computes the relative lock-times for the passed
@ -924,7 +924,7 @@ func (dag *BlockDAG) isCurrent() bool {
// Not current if the latest main (best) chain height is before the
// latest known good checkpoint (when checkpoints are enabled).
checkpoint := dag.LatestCheckpoint()
if checkpoint != nil && dag.SelectedTip().height < checkpoint.Height {
if checkpoint != nil && dag.selectedTip().height < checkpoint.Height {
return false
}
@ -934,7 +934,7 @@ func (dag *BlockDAG) isCurrent() bool {
// The chain appears to be current if none of the checks reported
// otherwise.
minus24Hours := dag.timeSource.AdjustedTime().Add(-24 * time.Hour).Unix()
return dag.SelectedTip().timestamp >= minus24Hours
return dag.selectedTip().timestamp >= minus24Hours
}
// IsCurrent returns whether or not the chain believes it is current. Several
@ -951,14 +951,27 @@ func (dag *BlockDAG) IsCurrent() bool {
return dag.isCurrent()
}
// SelectedTip returns the current selected tip for the DAG.
// selectedTip returns the current selected tip for the DAG.
// It will return nil if there is no tip.
//
// This function is safe for concurrent access.
func (dag *BlockDAG) SelectedTip() *blockNode {
func (dag *BlockDAG) selectedTip() *blockNode {
return dag.virtual.selectedParent
}
// SelectedTipHeader returns the header of the current selected tip for the DAG.
// It will return nil if there is no tip.
//
// This function is safe for concurrent access.
func (dag *BlockDAG) SelectedTipHeader() *wire.BlockHeader {
selectedTip := dag.selectedTip()
if selectedTip == nil {
return nil
}
return selectedTip.Header()
}
// UTXOSet returns the DAG's UTXO set
func (dag *BlockDAG) UTXOSet() *FullUTXOSet {
return dag.virtual.utxoSet
@ -1027,11 +1040,11 @@ func (dag *BlockDAG) CurrentBits() uint32 {
// HeaderByHash returns the block header identified by the given hash or an
// error if it doesn't exist.
func (dag *BlockDAG) HeaderByHash(hash *daghash.Hash) (wire.BlockHeader, error) {
func (dag *BlockDAG) HeaderByHash(hash *daghash.Hash) (*wire.BlockHeader, error) {
node := dag.index.LookupNode(hash)
if node == nil {
err := fmt.Errorf("block %s is not known", hash)
return wire.BlockHeader{}, err
return &wire.BlockHeader{}, err
}
return node.Header(), nil
@ -1277,7 +1290,7 @@ func (dag *BlockDAG) locateInventory(locator BlockLocator, hashStop *daghash.Has
}
// Calculate how many entries are needed.
total := uint32((dag.SelectedTip().height - startNode.height) + 1)
total := uint32((dag.selectedTip().height - startNode.height) + 1)
if stopNode != nil && stopNode.height >= startNode.height {
total = uint32((stopNode.height - startNode.height) + 1)
}
@ -1340,7 +1353,7 @@ func (dag *BlockDAG) LocateBlocks(locator BlockLocator, hashStop *daghash.Hash,
// See the comment on the exported function for more details on special cases.
//
// This function MUST be called with the chain state lock held (for reads).
func (dag *BlockDAG) locateHeaders(locator BlockLocator, hashStop *daghash.Hash, maxHeaders uint32) []wire.BlockHeader {
func (dag *BlockDAG) locateHeaders(locator BlockLocator, hashStop *daghash.Hash, maxHeaders uint32) []*wire.BlockHeader {
// Find the node after the first known block in the locator and the
// total number of nodes after it needed while respecting the stop hash
// and max entries.
@ -1350,7 +1363,7 @@ func (dag *BlockDAG) locateHeaders(locator BlockLocator, hashStop *daghash.Hash,
}
// Populate and return the found headers.
headers := make([]wire.BlockHeader, 0, total)
headers := make([]*wire.BlockHeader, 0, total)
for i := uint32(0); i < total; i++ {
headers = append(headers, node.Header())
node = node.diffChild
@ -1381,7 +1394,7 @@ func (dag *BlockDAG) UTXORUnlock() {
// after the genesis block will be returned
//
// This function is safe for concurrent access.
func (dag *BlockDAG) LocateHeaders(locator BlockLocator, hashStop *daghash.Hash) []wire.BlockHeader {
func (dag *BlockDAG) LocateHeaders(locator BlockLocator, hashStop *daghash.Hash) []*wire.BlockHeader {
dag.dagLock.RLock()
headers := dag.locateHeaders(locator, hashStop, wire.MaxBlockHeadersPerMsg)
dag.dagLock.RUnlock()
@ -1539,7 +1552,7 @@ func New(config *Config) (*BlockDAG, error) {
return nil, err
}
selectedTip := dag.SelectedTip()
selectedTip := dag.selectedTip()
log.Infof("DAG state (height %d, hash %v, work %v)",
selectedTip.height, selectedTip.hash, selectedTip.workSum)

View File

@ -220,7 +220,7 @@ func TestCalcSequenceLock(t *testing.T) {
// Generate enough synthetic blocks for the rest of the test
dag := newTestDAG(netParams)
node := dag.SelectedTip()
node := dag.selectedTip()
blockTime := node.Header().Timestamp
numBlocksToGenerate := uint32(5)
for i := uint32(0); i < numBlocksToGenerate; i++ {

View File

@ -290,7 +290,7 @@ func (dag *BlockDAG) calcNextRequiredDifficulty(lastNode *blockNode, newBlockTim
// This function is safe for concurrent access.
func (dag *BlockDAG) CalcNextRequiredDifficulty(timestamp time.Time) (uint32, error) {
dag.dagLock.Lock()
difficulty, err := dag.calcNextRequiredDifficulty(dag.SelectedTip(), timestamp)
difficulty, err := dag.calcNextRequiredDifficulty(dag.selectedTip(), timestamp)
dag.dagLock.Unlock()
return difficulty, err
}

View File

@ -3,7 +3,7 @@
// license that can be found in the LICENSE file.
/*
Package blockchain implements bitcoin block handling and chain selection rules.
Package blockdag implements bitcoin block handling and chain selection rules.
The bitcoin block handling and chain selection rules are an integral, and quite
likely the most important, part of bitcoin. Unfortunately, at the time of

View File

@ -7,7 +7,7 @@ import (
"github.com/daglabs/btcd/dagconfig"
"github.com/daglabs/btcd/database"
_ "github.com/daglabs/btcd/database/ffldb"
_ "github.com/daglabs/btcd/database/ffldb" // blank import ffldb so that its init() function runs before tests
"github.com/daglabs/btcd/txscript"
"github.com/daglabs/btcd/wire"
)

View File

@ -265,7 +265,7 @@ func (dag *BlockDAG) thresholdState(prevNode *blockNode, checker thresholdCondit
// This function is safe for concurrent access.
func (dag *BlockDAG) ThresholdState(deploymentID uint32) (ThresholdState, error) {
dag.dagLock.Lock()
state, err := dag.deploymentState(dag.SelectedTip(), deploymentID)
state, err := dag.deploymentState(dag.selectedTip(), deploymentID)
dag.dagLock.Unlock()
return state, err
@ -277,7 +277,7 @@ func (dag *BlockDAG) ThresholdState(deploymentID uint32) (ThresholdState, error)
// This function is safe for concurrent access.
func (dag *BlockDAG) IsDeploymentActive(deploymentID uint32) (bool, error) {
dag.dagLock.Lock()
state, err := dag.deploymentState(dag.SelectedTip(), deploymentID)
state, err := dag.deploymentState(dag.selectedTip(), deploymentID)
dag.dagLock.Unlock()
if err != nil {
return false, err
@ -316,7 +316,7 @@ func (dag *BlockDAG) initThresholdCaches() error {
// threshold state for each of them. This will ensure the caches are
// populated and any states that needed to be recalculated due to
// definition changes is done now.
prevNode := dag.SelectedTip().selectedParent
prevNode := dag.selectedTip().selectedParent
for bit := uint32(0); bit < vbNumBits; bit++ {
checker := bitConditionChecker{bit: bit, chain: dag}
cache := &dag.warningCaches[bit]
@ -340,7 +340,7 @@ func (dag *BlockDAG) initThresholdCaches() error {
if dag.isCurrent() {
// Warn if a high enough percentage of the last blocks have
// unexpected versions.
bestNode := dag.SelectedTip()
bestNode := dag.selectedTip()
if err := dag.warnUnknownVersions(bestNode); err != nil {
return err
}

View File

@ -461,6 +461,7 @@ func (fus *FullUTXOSet) clone() UTXOSet {
return &FullUTXOSet{utxoCollection: fus.utxoCollection.clone()}
}
// Get returns the UTXOEntry associated with the given OutPoint, and a boolean indicating if such entry was found
func (fus *FullUTXOSet) Get(outPoint wire.OutPoint) (*UTXOEntry, bool) {
utxoEntry, ok := fus.utxoCollection[outPoint]
return utxoEntry, ok

View File

@ -1093,7 +1093,7 @@ func (dag *BlockDAG) CheckConnectBlockTemplate(block *util.Block) error {
return err
}
err = dag.checkBlockContext(block, parents, dag.SelectedTip(), flags)
err = dag.checkBlockContext(block, parents, dag.selectedTip(), flags)
if err != nil {
return err
}

View File

@ -526,7 +526,7 @@ func TestPastMedianTime(t *testing.T) {
dagconfig.MainNetParams.K)
header := node.Header()
err := dag.checkBlockHeaderContext(&header, node.parents.bluest(), height, BFNone)
err := dag.checkBlockHeaderContext(header, node.parents.bluest(), height, BFNone)
if err != nil {
t.Errorf("TestPastMedianTime: unexpected error from checkBlockHeaderContext: %v"+
"(a block with timestamp equals to past median time should be valid)", err)
@ -541,7 +541,7 @@ func TestPastMedianTime(t *testing.T) {
dagconfig.MainNetParams.K)
header = node.Header()
err = dag.checkBlockHeaderContext(&header, node.parents.bluest(), height, BFNone)
err = dag.checkBlockHeaderContext(header, node.parents.bluest(), height, BFNone)
if err != nil {
t.Errorf("TestPastMedianTime: unexpected error from checkBlockHeaderContext: %v"+
"(a block with timestamp bigger than past median time should be valid)", err)
@ -556,7 +556,7 @@ func TestPastMedianTime(t *testing.T) {
dagconfig.MainNetParams.K)
header = node.Header()
err = dag.checkBlockHeaderContext(&header, node.parents.bluest(), height, BFNone)
err = dag.checkBlockHeaderContext(header, node.parents.bluest(), height, BFNone)
if err == nil {
t.Errorf("TestPastMedianTime: unexpected success: block should be invalid if its timestamp is before past median time")
}

View File

@ -221,7 +221,7 @@ func (dag *BlockDAG) calcNextBlockVersion(prevNode *blockNode) (int32, error) {
// This function is safe for concurrent access.
func (dag *BlockDAG) CalcNextBlockVersion() (int32, error) {
dag.dagLock.Lock()
version, err := dag.calcNextBlockVersion(dag.SelectedTip())
version, err := dag.calcNextBlockVersion(dag.selectedTip())
dag.dagLock.Unlock()
return version, err
}

View File

@ -24,7 +24,6 @@ import (
"github.com/daglabs/btcd/dagconfig"
"github.com/daglabs/btcd/dagconfig/daghash"
"github.com/daglabs/btcd/database"
_ "github.com/daglabs/btcd/database/ffldb"
"github.com/daglabs/btcd/logger"
"github.com/daglabs/btcd/mempool"
"github.com/daglabs/btcd/util"
@ -65,13 +64,13 @@ const (
)
var (
DefaultHomeDir = util.AppDataDir("btcd", false)
defaultConfigFile = filepath.Join(DefaultHomeDir, defaultConfigFilename)
defaultDataDir = filepath.Join(DefaultHomeDir, defaultDataDirname)
defaultHomeDir = util.AppDataDir("btcd", false)
defaultConfigFile = filepath.Join(defaultHomeDir, defaultConfigFilename)
defaultDataDir = filepath.Join(defaultHomeDir, defaultDataDirname)
knownDbTypes = database.SupportedDrivers()
defaultRPCKeyFile = filepath.Join(DefaultHomeDir, "rpc.key")
defaultRPCCertFile = filepath.Join(DefaultHomeDir, "rpc.cert")
defaultLogDir = filepath.Join(DefaultHomeDir, defaultLogDirname)
defaultRPCKeyFile = filepath.Join(defaultHomeDir, "rpc.key")
defaultRPCCertFile = filepath.Join(defaultHomeDir, "rpc.cert")
defaultLogDir = filepath.Join(defaultHomeDir, defaultLogDirname)
)
// activeNetParams is a pointer to the parameters specific to the
@ -191,7 +190,7 @@ type serviceOptions struct {
func cleanAndExpandPath(path string) string {
// Expand initial ~ to OS specific home directory.
if strings.HasPrefix(path, "~") {
homeDir := filepath.Dir(DefaultHomeDir)
homeDir := filepath.Dir(defaultHomeDir)
path = strings.Replace(path, "~", homeDir, 1)
}
@ -405,7 +404,7 @@ func loadConfig() (*Config, []string, error) {
// Create the home directory if it doesn't already exist.
funcName := "loadConfig"
err = os.MkdirAll(DefaultHomeDir, 0700)
err = os.MkdirAll(defaultHomeDir, 0700)
if err != nil {
// Show a nicer error message if it's because a symlink is
// linked to a directory that does not exist (probably because

View File

@ -35,6 +35,7 @@ func (hash Hash) String() string {
return hex.EncodeToString(hash[:])
}
// Strings returns a slice of strings representing the hashes in the given slice of hashes
func Strings(hashes []Hash) []string {
strings := make([]string, len(hashes))
for i, hash := range hashes {

View File

@ -22,7 +22,7 @@ COPY . .
RUN TEST_DIRS=`go list -f {{.Dir}} ./... | grep -v /vendor/`
RUN GOFMT_RESULT=`gofmt -l $TEST_DIRS`; echo $GOFMT_RESULT; test -z "$GOFMT_RESULT"
RUN go vet ./...
# RUN golint -set_exit_status $TEST_DIRS
RUN golint -set_exit_status $TEST_DIRS
# RUN aligncheck ./...
# RUN structcheck -e ./...
# RUN varcheck -e ./...
@ -42,4 +42,4 @@ COPY --from=build /go/src/github.com/daglabs/btcd/btcd /app/
COPY --from=build /go/src/github.com/daglabs/btcd/version.txt /app/
USER nobody
ENTRYPOINT [ "/sbin/tini", "--" ]
ENTRYPOINT [ "/sbin/tini", "--" ]

View File

@ -698,7 +698,7 @@ mempoolLoop:
// Calculate the required difficulty for the block. The timestamp
// is potentially adjusted to ensure it comes after the median time of
// the last several blocks per the chain consensus rules.
ts := medianAdjustedTime(g.dag.SelectedTip().CalcPastMedianTime(), g.timeSource)
ts := medianAdjustedTime(g.dag.CalcPastMedianTime(), g.timeSource)
reqDifficulty, err := g.dag.CalcNextRequiredDifficulty(ts)
if err != nil {
return nil, err

View File

@ -721,7 +721,7 @@ func (sp *Peer) OnGetHeaders(_ *peer.Peer, msg *wire.MsgGetHeaders) {
// Send found headers to the requesting peer.
blockHeaders := make([]*wire.BlockHeader, len(headers))
for i := range headers {
blockHeaders[i] = &headers[i]
blockHeaders[i] = headers[i]
}
sp.QueueMessage(&wire.MsgHeaders{Headers: blockHeaders}, nil)
}
@ -2437,7 +2437,7 @@ func NewServer(listenAddrs []string, db database.DB, dagParams *dagconfig.Params
},
DAGParams: dagParams,
BestHeight: func() int32 { return s.DAG.Height() }, //TODO: (Ori) This is probably wrong. Done only for compilation
MedianTimePast: func() time.Time { return s.DAG.SelectedTip().CalcPastMedianTime() },
MedianTimePast: func() time.Time { return s.DAG.CalcPastMedianTime() },
CalcSequenceLock: func(tx *util.Tx, utxoSet blockdag.UTXOSet) (*blockdag.SequenceLock, error) {
return s.DAG.CalcSequenceLock(tx, utxoSet, true)
},

View File

@ -275,6 +275,6 @@ func (b *rpcSyncMgr) SyncPeerID() int32 {
//
// This function is safe for concurrent access and is part of the
// rpcserverSyncManager interface implementation.
func (b *rpcSyncMgr) LocateHeaders(locators []*daghash.Hash, hashStop *daghash.Hash) []wire.BlockHeader {
func (b *rpcSyncMgr) LocateHeaders(locators []*daghash.Hash, hashStop *daghash.Hash) []*wire.BlockHeader {
return b.server.DAG.LocateHeaders(locators, hashStop)
}

View File

@ -1206,7 +1206,7 @@ func handleGetBlockDAGInfo(s *Server, cmd interface{}, closeChan <-chan struct{}
Headers: dag.Height(), //TODO: (Ori) This is wrong. Done only for compilation
TipHashes: daghash.Strings(dag.TipHashes()),
Difficulty: getDifficultyRatio(dag.CurrentBits(), params),
MedianTime: dag.SelectedTip().CalcPastMedianTime().Unix(),
MedianTime: dag.CalcPastMedianTime().Unix(),
Pruned: false,
Bip9SoftForks: make(map[string]*btcjson.Bip9SoftForkDescription),
}
@ -1559,7 +1559,7 @@ func (state *gbtWorkState) updateBlockTemplate(s *Server, useCoinbaseValue bool)
// Get the minimum allowed timestamp for the block based on the
// median timestamp of the last several blocks per the chain
// consensus rules.
minTimestamp := mining.MinimumMedianTime(s.cfg.DAG.SelectedTip().CalcPastMedianTime())
minTimestamp := mining.MinimumMedianTime(s.cfg.DAG.CalcPastMedianTime())
// Update work state to ensure another block template isn't
// generated until needed.
@ -2202,7 +2202,7 @@ func handleGetCurrentNet(s *Server, cmd interface{}, closeChan <-chan struct{})
// handleGetDifficulty implements the getDifficulty command.
func handleGetDifficulty(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
return getDifficultyRatio(s.cfg.DAG.SelectedTip().Header().Bits, s.cfg.DAGParams), nil
return getDifficultyRatio(s.cfg.DAG.SelectedTipHeader().Bits, s.cfg.DAGParams), nil
}
// handleGetGenerate implements the getGenerate command.
@ -2516,7 +2516,7 @@ func handleGetRawTransaction(s *Server, cmd interface{}, closeChan <-chan struct
return nil, internalRPCError(err.Error(), context)
}
blkHeader = &header
blkHeader = header
blkHashStr = blkHash.String()
dagHeight = s.cfg.DAG.Height()
}
@ -3146,7 +3146,7 @@ func handleSearchRawTransactions(s *Server, cmd interface{}, closeChan <-chan st
return nil, internalRPCError(err.Error(), context)
}
blkHeader = &header
blkHeader = header
blkHashStr = blkHash.String()
blkHeight = height
}
@ -4070,7 +4070,7 @@ type rpcserverSyncManager interface {
// block in the provided locators until the provided stop hash or the
// current tip is reached, up to a max of wire.MaxBlockHeadersPerMsg
// hashes.
LocateHeaders(locators []*daghash.Hash, hashStop *daghash.Hash) []wire.BlockHeader
LocateHeaders(locators []*daghash.Hash, hashStop *daghash.Hash) []*wire.BlockHeader
}
// rpcserverConfig is a descriptor containing the RPC server configuration.
@ -4213,7 +4213,7 @@ func NewRPCServer(
gbtWorkState: newGbtWorkState(cfg.TimeSource),
helpCacher: newHelpCacher(),
requestProcessShutdown: make(chan struct{}),
quit: make(chan int),
quit: make(chan int),
}
if config.MainConfig().RPCUser != "" && config.MainConfig().RPCPass != "" {
login := config.MainConfig().RPCUser + ":" + config.MainConfig().RPCPass

View File

@ -363,11 +363,13 @@ func (k *ExtendedKey) Neuter() (*ExtendedKey, error) {
k.depth, k.childNum, false), nil
}
// HDKeyIDPair represents a pair of public and private key IDs
type HDKeyIDPair struct {
PrivateKeyID [4]byte
PublicKeyID [4]byte
}
// HDKeyPairs for all kinds of nets
var (
HDKeyPairMainNet = HDKeyIDPair{
PrivateKeyID: [4]byte{0x04, 0x88, 0xad, 0xe4}, // starts with xprv
@ -387,6 +389,7 @@ var (
}
)
// RegisterHDKeyIDPair registers an HDKeyIDPair in the private to public key map
func RegisterHDKeyIDPair(hdKeyIDPair HDKeyIDPair) {
hdPrivToPubKeyIDs[hdKeyIDPair.PrivateKeyID] = hdKeyIDPair.PublicKeyID[:]
}