Skip to content

Commit

Permalink
Fix geth timestamp incrementing (ethereum#14)
Browse files Browse the repository at this point in the history
* Making geth only update timestamp when it is over 600s older than current timestamp.
* Commenting out logic check to validate timestamp works the way it no longer works
  • Loading branch information
willmeister authored Aug 19, 2020
1 parent fa42b78 commit 1efe552
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 14 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/dev-ecr-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ jobs:
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
# TODO: Add this when the DEV env is set up
# - name: Stop existing dev-geth ECS task to auto-start task with new image
# run: |
# ./.github/scripts/stop-ecs-task.sh dev-geth geth
- name: Stop existing dev-geth ECS task to auto-start task with new image
run: |
./.github/scripts/stop-ecs-task.sh dev dev-all-in-one
- name: Logout of Amazon ECR
if: always()
Expand Down
2 changes: 0 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ RUN apk add --no-cache ca-certificates
COPY --from=builder /go-ethereum/build/bin/geth /usr/local/bin/

EXPOSE 8545 8546 8547 30303 30303/udp
# ENTRYPOINT ["geth"]

COPY docker/entrypoint.sh /bin
RUN chmod +x /bin/entrypoint.sh

EXPOSE 9545
ENTRYPOINT ["sh", "/bin/entrypoint.sh"]
16 changes: 9 additions & 7 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,10 @@ func (c *Clique) verifyCascadingFields(chain consensus.ChainReader, header *type
if parent == nil || parent.Number.Uint64() != number-1 || parent.Hash() != header.ParentHash {
return consensus.ErrUnknownAncestor
}
if parent.Time+c.config.Period > header.Time {
return ErrInvalidTimestamp
}
// [REMOVED] to account for timestamp changes
//if parent.Time+c.config.Period > header.Time {
// return ErrInvalidTimestamp
//}
// Retrieve the snapshot needed to verify this header and cache it
snap, err := c.snapshot(chain, number-1, header.ParentHash, parents)
if err != nil {
Expand Down Expand Up @@ -543,10 +544,11 @@ func (c *Clique) Prepare(chain consensus.ChainReader, header *types.Header) erro
if parent == nil {
return consensus.ErrUnknownAncestor
}
header.Time = parent.Time + c.config.Period
if header.Time < uint64(time.Now().Unix()) {
header.Time = uint64(time.Now().Unix())
}
// [REMOVED] so we can control timestamps
//header.Time = parent.Time + c.config.Period
//if header.Time < uint64(time.Now().Unix()) {
// header.Time = uint64(time.Now().Unix())
//}
return nil
}

Expand Down
8 changes: 7 additions & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
bc.currentFastBlock.Store(nilBlock)

// TODO: Make default current timestamp configurable & make 0 if genesis else load from last block?
bc.SetCurrentTimestamp(int64(0))

// Initialize the chain with ancient data if it isn't empty.
if bc.empty() {
Expand All @@ -252,6 +251,13 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
// it in advance.
bc.engine.VerifyHeader(bc, bc.CurrentHeader(), true)

if currentHeader := bc.CurrentHeader(); currentHeader != nil {
log.Debug("Read timestamp from last block. ", "timestamp", bc.CurrentHeader().Time)
bc.SetCurrentTimestamp(int64(bc.CurrentHeader().Time))
} else {
bc.SetCurrentTimestamp(int64(0))
}

if frozen, err := bc.db.Ancients(); err == nil && frozen > 0 {
var (
needRewind bool
Expand Down
21 changes: 21 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: "3"

services:
geth_l2:
build:
context: .
dockerfile: Dockerfile
volumes:
- l2-node-data:/mnt/l2-node/l2:rw
environment:
- CLEAR_DATA_KEY
- TARGET_GAS_LIMIT
- VOLUME_PATH=/mnt/l2-node/l2
- HOSTNAME=geth_l2
- PORT=8545
- NETWORK_ID=108
ports:
- 8545:8545

volumes:
l2-node-data:
30 changes: 30 additions & 0 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ const (

// staleThreshold is the maximum depth of the acceptable stale block.
staleThreshold = 7

maxClockSkewSeconds = 600 // 10 mins for now
timestampDelaySeconds = 300 // 5 mins for now
)

// environment is the worker's current environment and holds all of the current state information.
Expand Down Expand Up @@ -220,6 +223,7 @@ func newWorker(config *Config, chainConfig *params.ChainConfig, engine consensus
go worker.newWorkLoop(recommit)
go worker.resultLoop()
go worker.taskLoop()
go worker.timestampLoop()

// Submit first work to initialize pending state.
if init {
Expand Down Expand Up @@ -362,6 +366,7 @@ func (w *worker) newWorkLoop(recommit time.Duration) {
timer.Reset(recommit)
continue
}
timestamp = w.chain.CurrentTimestamp()
commit(true, commitInterruptResubmit)
}

Expand Down Expand Up @@ -613,6 +618,31 @@ func (w *worker) resultLoop() {
}
}

// timestampLoop is a loop that updates the timestamp used for blocks when it
// is stale by more than a certain threshold.
// TODO: Re-think this as everything comes together more.
func (w *worker) timestampLoop() {
timer := time.NewTimer(0)

for {
select {
case <-timer.C:
currentTime := time.Now().Unix()
skew := currentTime - w.chain.CurrentTimestamp()
if skew > maxClockSkewSeconds {
newTime := currentTime - timestampDelaySeconds
w.chain.SetCurrentTimestamp(newTime)
timer.Reset((maxClockSkewSeconds - timestampDelaySeconds) * time.Second)
log.Debug("timestamp above max clock skew", "maxSkew", maxClockSkewSeconds, "overBy", timestampDelaySeconds, "newTime", newTime)
} else {
timer.Reset(time.Duration(maxClockSkewSeconds-skew) * time.Second)
}
case <-w.exitCh:
return
}
}
}

// makeCurrent creates a new environment for the current cycle.
func (w *worker) makeCurrent(parent *types.Block, header *types.Header) error {
state, err := w.chain.StateAt(parent.Root())
Expand Down

0 comments on commit 1efe552

Please sign in to comment.