mirror of
https://github.com/LBRYFoundation/lbcd.git
synced 2025-08-23 17:47:24 +00:00
Make use of the new btcchain MedianTimeSource.
This commit uses the new MedianTimeSource API in btcchain to create a median time source which is stored in the server and is fed time samples from all remote nodes that are connected. It also modifies all call sites which now require the the time source to pass it in.
This commit is contained in:
parent
1bbd1e9cba
commit
c3065d32f4
4 changed files with 16 additions and 6 deletions
|
@ -519,7 +519,7 @@ func (b *blockManager) handleTxMsg(tmsg *txMsg) {
|
||||||
// current returns true if we believe we are synced with our peers, false if we
|
// current returns true if we believe we are synced with our peers, false if we
|
||||||
// still have blocks to check
|
// still have blocks to check
|
||||||
func (b *blockManager) current() bool {
|
func (b *blockManager) current() bool {
|
||||||
if !b.blockChain.IsCurrent() {
|
if !b.blockChain.IsCurrent(b.server.timeSource) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -591,7 +591,8 @@ func (b *blockManager) handleBlockMsg(bmsg *blockMsg) {
|
||||||
|
|
||||||
// Process the block to include validation, best chain selection, orphan
|
// Process the block to include validation, best chain selection, orphan
|
||||||
// handling, etc.
|
// handling, etc.
|
||||||
isOrphan, err := b.blockChain.ProcessBlock(bmsg.block, behaviorFlags)
|
isOrphan, err := b.blockChain.ProcessBlock(bmsg.block,
|
||||||
|
b.server.timeSource, behaviorFlags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// When the error is a rule error, it means the block was simply
|
// When the error is a rule error, it means the block was simply
|
||||||
// rejected as opposed to something actually going wrong, so log
|
// rejected as opposed to something actually going wrong, so log
|
||||||
|
@ -1065,7 +1066,8 @@ out:
|
||||||
|
|
||||||
case processBlockMsg:
|
case processBlockMsg:
|
||||||
isOrphan, err := b.blockChain.ProcessBlock(
|
isOrphan, err := b.blockChain.ProcessBlock(
|
||||||
msg.block, msg.flags)
|
msg.block, b.server.timeSource,
|
||||||
|
msg.flags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg.reply <- processBlockResponse{
|
msg.reply <- processBlockResponse{
|
||||||
isOrphan: false,
|
isOrphan: false,
|
||||||
|
|
4
peer.go
4
peer.go
|
@ -444,6 +444,10 @@ func (p *peer) handleVersionMsg(msg *btcwire.MsgVersion) {
|
||||||
p.updateAddresses(msg)
|
p.updateAddresses(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add the remote peer time as a sample for creating an offset against
|
||||||
|
// the local clock to keep the network time in sync.
|
||||||
|
p.server.timeSource.AddTimeSample(p.addr, msg.Timestamp)
|
||||||
|
|
||||||
// Signal the block manager this peer is a new sync candidate.
|
// Signal the block manager this peer is a new sync candidate.
|
||||||
p.server.blockManager.NewPeer(p)
|
p.server.blockManager.NewPeer(p)
|
||||||
|
|
||||||
|
|
|
@ -3071,7 +3071,7 @@ func handleSubmitBlock(s *rpcServer, cmd btcjson.Cmd, closeChan <-chan struct{})
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func verifyChain(db btcdb.Db, level, depth int32) error {
|
func verifyChain(db btcdb.Db, level, depth int32, timeSource btcchain.MedianTimeSource) error {
|
||||||
_, curHeight64, err := db.NewestSha()
|
_, curHeight64, err := db.NewestSha()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rpcsLog.Errorf("Verify is unable to fetch current block "+
|
rpcsLog.Errorf("Verify is unable to fetch current block "+
|
||||||
|
@ -3105,7 +3105,7 @@ func verifyChain(db btcdb.Db, level, depth int32) error {
|
||||||
// Level 1 does basic chain sanity checks.
|
// Level 1 does basic chain sanity checks.
|
||||||
if level > 0 {
|
if level > 0 {
|
||||||
err := btcchain.CheckBlockSanity(block,
|
err := btcchain.CheckBlockSanity(block,
|
||||||
activeNetParams.PowLimit)
|
activeNetParams.PowLimit, timeSource)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rpcsLog.Errorf("Verify is unable to "+
|
rpcsLog.Errorf("Verify is unable to "+
|
||||||
"validate block at sha %v height "+
|
"validate block at sha %v height "+
|
||||||
|
@ -3140,7 +3140,8 @@ func handleValidateAddress(s *rpcServer, cmd btcjson.Cmd, closeChan <-chan struc
|
||||||
func handleVerifyChain(s *rpcServer, cmd btcjson.Cmd, closeChan <-chan struct{}) (interface{}, error) {
|
func handleVerifyChain(s *rpcServer, cmd btcjson.Cmd, closeChan <-chan struct{}) (interface{}, error) {
|
||||||
c := cmd.(*btcjson.VerifyChainCmd)
|
c := cmd.(*btcjson.VerifyChainCmd)
|
||||||
|
|
||||||
err := verifyChain(s.server.db, c.CheckLevel, c.CheckDepth)
|
err := verifyChain(s.server.db, c.CheckLevel, c.CheckDepth,
|
||||||
|
s.server.timeSource)
|
||||||
return err == nil, nil
|
return err == nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@ import (
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/conformal/btcchain"
|
||||||
"github.com/conformal/btcd/addrmgr"
|
"github.com/conformal/btcd/addrmgr"
|
||||||
"github.com/conformal/btcdb"
|
"github.com/conformal/btcdb"
|
||||||
"github.com/conformal/btcjson"
|
"github.com/conformal/btcjson"
|
||||||
|
@ -90,6 +91,7 @@ type server struct {
|
||||||
quit chan struct{}
|
quit chan struct{}
|
||||||
nat NAT
|
nat NAT
|
||||||
db btcdb.Db
|
db btcdb.Db
|
||||||
|
timeSource btcchain.MedianTimeSource
|
||||||
}
|
}
|
||||||
|
|
||||||
type peerState struct {
|
type peerState struct {
|
||||||
|
@ -1229,6 +1231,7 @@ func newServer(listenAddrs []string, db btcdb.Db, netParams *btcnet.Params) (*se
|
||||||
modifyRebroadcastInv: make(chan interface{}),
|
modifyRebroadcastInv: make(chan interface{}),
|
||||||
nat: nat,
|
nat: nat,
|
||||||
db: db,
|
db: db,
|
||||||
|
timeSource: btcchain.NewMedianTime(),
|
||||||
}
|
}
|
||||||
bm, err := newBlockManager(&s)
|
bm, err := newBlockManager(&s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Add table
Reference in a new issue