Skip to content

Commit

Permalink
Merge commit '80f5a0ffdf363cfff27d550f9e38aa262667a7f1'
Browse files Browse the repository at this point in the history
  • Loading branch information
losh11 committed Jan 31, 2024
2 parents 07772e3 + 80f5a0f commit dbd57a5
Show file tree
Hide file tree
Showing 182 changed files with 3,036 additions and 1,245 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ coverage.txt
btcec/coverage.txt
ltcutil/coverage.txt
ltcutil/psbt/coverage.txt

# vim
*.swp
12 changes: 1 addition & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
3. [Wallet](#Wallet)
4. [Contact](#Contact)
1. [Email](#ContactEmail)
2. [Mailing Lists](#MailingLists)
5. [Developer Resources](#DeveloperResources)
1. [Code Contribution Guidelines](#ContributionGuidelines)
2. [JSON-RPC Reference](#JSONRPCReference)
Expand Down Expand Up @@ -63,7 +62,7 @@ directly with ltcd. That functionality is provided by the

<a name="GettingStarted" />

### 2. Getting Started
[Go](http://golang.org) 1.17 or newer.

<a name="Installation" />

Expand Down Expand Up @@ -242,15 +241,6 @@ information.

- losh11@litecoin.net - maintainers email.

<a name="MailingLists" />

**4.2 Mailing Lists**

- <a href="mailto:ltcd+subscribe@opensource.conformal.com">btcd</a>: discussion
of btcd and its packages.
- <a href="mailto:ltcd-commits+subscribe@opensource.conformal.com">btcd-commits</a>:
readonly mail-out of source code changes.

<a name="DeveloperResources" />

### 5. Developer Resources
Expand Down
14 changes: 7 additions & 7 deletions addrmgr/addrmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

// AddrManager provides a concurrency safe address manager for caching potential
// peers on the bitcoin network.
// peers on the litecoin network.
type AddrManager struct {
mtx sync.RWMutex
peersFile string
Expand Down Expand Up @@ -244,7 +244,7 @@ func (a *AddrManager) updateAddress(netAddr, srcAddr *wire.NetAddressV2) {
func (a *AddrManager) expireNew(bucket int) {
// First see if there are any entries that are so bad we can just throw
// them away. otherwise we throw away the oldest entry in the cache.
// Bitcoind here chooses four random and just throws the oldest of
// Litecoind here chooses four random and just throws the oldest of
// those away, but we keep track of oldest in the initial traversal and
// use that information instead.
var oldest *KnownAddress
Expand Down Expand Up @@ -280,7 +280,7 @@ func (a *AddrManager) expireNew(bucket int) {
}

// pickTried selects an address from the tried bucket to be evicted.
// We just choose the eldest. Bitcoind selects 4 random entries and throws away
// We just choose the eldest. Litecoind selects 4 random entries and throws away
// the older of them.
func (a *AddrManager) pickTried(bucket int) *list.Element {
var oldest *KnownAddress
Expand All @@ -297,7 +297,7 @@ func (a *AddrManager) pickTried(bucket int) *list.Element {
}

func (a *AddrManager) getNewBucket(netAddr, srcAddr *wire.NetAddressV2) int {
// bitcoind:
// litecoind:
// doublesha256(key + sourcegroup + int64(doublesha256(key + group + sourcegroup))%bucket_per_source_group) % num_new_buckets

data1 := []byte{}
Expand All @@ -319,7 +319,7 @@ func (a *AddrManager) getNewBucket(netAddr, srcAddr *wire.NetAddressV2) int {
}

func (a *AddrManager) getTriedBucket(netAddr *wire.NetAddressV2) int {
// bitcoind hashes this as:
// litecoind hashes this as:
// doublesha256(key + group + truncate_to_64bits(doublesha256(key)) % buckets_per_group) % num_buckets
data1 := []byte{}
data1 = append(data1, a.key[:]...)
Expand Down Expand Up @@ -733,7 +733,7 @@ func (a *AddrManager) HostToNetAddress(host string, port uint16,
// Tor v2 address is 16 char base32 + ".onion"
if len(host) == wire.TorV2EncodedSize && host[wire.TorV2EncodedSize-6:] == ".onion" {
// go base32 encoding uses capitals (as does the rfc
// but Tor and bitcoind tend to user lowercase, so we switch
// but Tor and litecoind tend to user lowercase, so we switch
// case here.
data, err := base32.StdEncoding.DecodeString(
strings.ToUpper(host[:wire.TorV2EncodedSize-6]))
Expand Down Expand Up @@ -1202,7 +1202,7 @@ func (a *AddrManager) GetBestLocalAddress(remoteAddr *wire.NetAddressV2) *wire.N
return bestAddress
}

// New returns a new bitcoin address manager.
// New returns a new litecoin address manager.
// Use Start to begin processing asynchronous address updates.
func New(dataDir string, lookupFunc func(string) ([]net.IP, error)) *AddrManager {
am := AddrManager{
Expand Down
8 changes: 4 additions & 4 deletions addrmgr/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
// license that can be found in the LICENSE file.

/*
Package addrmgr implements concurrency safe Bitcoin address manager.
Package addrmgr implements concurrency safe Litecoin address manager.
Address Manager Overview
# Address Manager Overview
In order maintain the peer-to-peer Bitcoin network, there needs to be a source
of addresses to connect to as nodes come and go. The Bitcoin protocol provides
In order maintain the peer-to-peer Litecoin network, there needs to be a source
of addresses to connect to as nodes come and go. The Litecoin protocol provides
the getaddr and addr messages to allow peers to communicate known addresses with
each other. However, there needs to a mechanism to store those results and
select peers from them. It is also important to note that remote peers can't
Expand Down
8 changes: 4 additions & 4 deletions addrmgr/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
ipNet("192.168.0.0", 16, 32),
}

// rfc2544Net specifies the the IPv4 block as defined by RFC2544
// rfc2544Net specifies the IPv4 block as defined by RFC2544
// (198.18.0.0/15)
rfc2544Net = ipNet("198.18.0.0", 15, 32)

Expand Down Expand Up @@ -72,7 +72,7 @@ var (
rfc6598Net = ipNet("100.64.0.0", 10, 32)

// onionCatNet defines the IPv6 address block used to support Tor.
// bitcoind encodes a .onion address as a 16 byte number by decoding the
// litecoind encodes a .onion address as a 16 byte number by decoding the
// address prior to the .onion (i.e. the key hash) base32 into a ten
// byte number. It then stores the first 6 bytes of the address as
// 0xfd, 0x87, 0xd8, 0x7e, 0xeb, 0x43.
Expand Down Expand Up @@ -110,7 +110,7 @@ func IsLocal(na *wire.NetAddress) bool {
}

// IsOnionCatTor returns whether or not the passed address is in the IPv6 range
// used by bitcoin to support Tor (fd87:d87e:eb43::/48). Note that this range
// used by litecoin to support Tor (fd87:d87e:eb43::/48). Note that this range
// is the same range used by OnionCat, which is part of the RFC4193 unique local
// IPv6 range.
func IsOnionCatTor(na *wire.NetAddress) bool {
Expand Down Expand Up @@ -287,7 +287,7 @@ func GroupKey(na *wire.NetAddressV2) string {
}

// OK, so now we know ourselves to be a IPv6 address.
// bitcoind uses /32 for everything, except for Hurricane Electric's
// litecoind uses /32 for everything, except for Hurricane Electric's
// (he.net) IP range, which it uses /36 for.
bits := 32
if heNet.Contains(lna.IP) {
Expand Down
6 changes: 3 additions & 3 deletions blockchain/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ blockchain
[![ISC License](http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org)
[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)](https://pkg.go.dev/github.com/ltcsuite/ltcd/blockchain)

Package blockchain implements bitcoin block handling and chain selection rules.
Package blockchain implements litecoin block handling and chain selection rules.
The test coverage is currently only around 60%, but will be increasing over
time. See `test_coverage.txt` for the gocov coverage report. Alternatively, if
you are running a POSIX OS, you can run the `cov_report.sh` script for a
Expand All @@ -15,7 +15,7 @@ There is an associated blog post about the release of this package
[here](https://blog.conformal.com/btcchain-the-bitcoin-chain-package-from-bctd/).

This package has intentionally been designed so it can be used as a standalone
package for any projects needing to handle processing of blocks into the bitcoin
package for any projects needing to handle processing of blocks into the litecoin
block chain.

## Installation and Updating
Expand All @@ -24,7 +24,7 @@ block chain.
$ go get -u github.com/ltcsuite/ltcd/blockchain
```

## Bitcoin Chain Processing Overview
## Litecoin Chain Processing Overview

Before a block is allowed into the block chain, it must go through an intensive
series of validation rules. The following list serves as a general outline of
Expand Down
31 changes: 31 additions & 0 deletions blockchain/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/ltcsuite/ltcd/ltcutil"
"github.com/ltcsuite/ltcd/wire"
)

// BenchmarkIsCoinBase performs a simple benchmark against the IsCoinBase
Expand All @@ -29,3 +30,33 @@ func BenchmarkIsCoinBaseTx(b *testing.B) {
IsCoinBaseTx(tx)
}
}

func BenchmarkUtxoFetchMap(b *testing.B) {
block := Block100000
transactions := block.Transactions
b.ResetTimer()

for i := 0; i < b.N; i++ {
needed := make(map[wire.OutPoint]struct{}, len(transactions))
for _, tx := range transactions[1:] {
for _, txIn := range tx.TxIn {
needed[txIn.PreviousOutPoint] = struct{}{}
}
}
}
}

func BenchmarkUtxoFetchSlices(b *testing.B) {
block := Block100000
transactions := block.Transactions
b.ResetTimer()

for i := 0; i < b.N; i++ {
needed := make([]wire.OutPoint, 0, len(transactions))
for _, tx := range transactions[1:] {
for _, txIn := range tx.TxIn {
needed = append(needed, txIn.PreviousOutPoint)
}
}
}
}
64 changes: 61 additions & 3 deletions blockchain/blockindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,60 @@ func (node *blockNode) Ancestor(height int32) *blockNode {
return n
}

// Height returns the blockNode's height in the chain.
//
// NOTE: Part of the HeaderCtx interface.
func (node *blockNode) Height() int32 {
return node.height
}

// Bits returns the blockNode's nBits.
//
// NOTE: Part of the HeaderCtx interface.
func (node *blockNode) Bits() uint32 {
return node.bits
}

// Timestamp returns the blockNode's timestamp.
//
// NOTE: Part of the HeaderCtx interface.
func (node *blockNode) Timestamp() int64 {
return node.timestamp
}

// Parent returns the blockNode's parent.
//
// NOTE: Part of the HeaderCtx interface.
func (node *blockNode) Parent() HeaderCtx {
if node.parent == nil {
// This is required since node.parent is a *blockNode and if we
// do not explicitly return nil here, the caller may fail when
// nil-checking this.
return nil
}

return node.parent
}

// RelativeAncestorCtx returns the blockNode's ancestor that is distance blocks
// before it in the chain. This is equivalent to the RelativeAncestor function
// below except that the return type is different.
//
// This function is safe for concurrent access.
//
// NOTE: Part of the HeaderCtx interface.
func (node *blockNode) RelativeAncestorCtx(distance int32) HeaderCtx {
ancestor := node.RelativeAncestor(distance)
if ancestor == nil {
// This is required since RelativeAncestor returns a *blockNode
// and if we do not explicitly return nil here, the caller may
// fail when nil-checking this.
return nil
}

return ancestor
}

// RelativeAncestor returns the ancestor block node a relative 'distance' blocks
// before this node. This is equivalent to calling Ancestor with the node's
// height minus provided distance.
Expand All @@ -182,17 +236,17 @@ func (node *blockNode) RelativeAncestor(distance int32) *blockNode {
// prior to, and including, the block node.
//
// This function is safe for concurrent access.
func (node *blockNode) CalcPastMedianTime() time.Time {
func CalcPastMedianTime(node HeaderCtx) time.Time {
// Create a slice of the previous few block timestamps used to calculate
// the median per the number defined by the constant medianTimeBlocks.
timestamps := make([]int64, medianTimeBlocks)
numNodes := 0
iterNode := node
for i := 0; i < medianTimeBlocks && iterNode != nil; i++ {
timestamps[i] = iterNode.timestamp
timestamps[i] = iterNode.Timestamp()
numNodes++

iterNode = iterNode.parent
iterNode = iterNode.Parent()
}

// Prune the slice to the actual number of available timestamps which
Expand All @@ -217,6 +271,10 @@ func (node *blockNode) CalcPastMedianTime() time.Time {
return time.Unix(medianTimestamp, 0)
}

// A compile-time assertion to ensure blockNode implements the HeaderCtx
// interface.
var _ HeaderCtx = (*blockNode)(nil)

// blockIndex provides facilities for keeping track of an in-memory index of the
// block chain. Although the name block chain suggests a single chain of
// blocks, it is actually a tree-shaped structure where any node can have
Expand Down

0 comments on commit dbd57a5

Please sign in to comment.