Skip to content

Commit 1efe552

Browse files
authored
Fix geth timestamp incrementing (#14)
* 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
1 parent fa42b78 commit 1efe552

File tree

6 files changed

+70
-14
lines changed

6 files changed

+70
-14
lines changed

.github/workflows/dev-ecr-deploy.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ jobs:
4040
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
4141
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
4242
43-
# TODO: Add this when the DEV env is set up
44-
# - name: Stop existing dev-geth ECS task to auto-start task with new image
45-
# run: |
46-
# ./.github/scripts/stop-ecs-task.sh dev-geth geth
43+
- name: Stop existing dev-geth ECS task to auto-start task with new image
44+
run: |
45+
./.github/scripts/stop-ecs-task.sh dev dev-all-in-one
4746
4847
- name: Logout of Amazon ECR
4948
if: always()

Dockerfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ RUN apk add --no-cache ca-certificates
1313
COPY --from=builder /go-ethereum/build/bin/geth /usr/local/bin/
1414

1515
EXPOSE 8545 8546 8547 30303 30303/udp
16-
# ENTRYPOINT ["geth"]
1716

1817
COPY docker/entrypoint.sh /bin
1918
RUN chmod +x /bin/entrypoint.sh
2019

21-
EXPOSE 9545
2220
ENTRYPOINT ["sh", "/bin/entrypoint.sh"]

consensus/clique/clique.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,10 @@ func (c *Clique) verifyCascadingFields(chain consensus.ChainReader, header *type
321321
if parent == nil || parent.Number.Uint64() != number-1 || parent.Hash() != header.ParentHash {
322322
return consensus.ErrUnknownAncestor
323323
}
324-
if parent.Time+c.config.Period > header.Time {
325-
return ErrInvalidTimestamp
326-
}
324+
// [REMOVED] to account for timestamp changes
325+
//if parent.Time+c.config.Period > header.Time {
326+
// return ErrInvalidTimestamp
327+
//}
327328
// Retrieve the snapshot needed to verify this header and cache it
328329
snap, err := c.snapshot(chain, number-1, header.ParentHash, parents)
329330
if err != nil {
@@ -543,10 +544,11 @@ func (c *Clique) Prepare(chain consensus.ChainReader, header *types.Header) erro
543544
if parent == nil {
544545
return consensus.ErrUnknownAncestor
545546
}
546-
header.Time = parent.Time + c.config.Period
547-
if header.Time < uint64(time.Now().Unix()) {
548-
header.Time = uint64(time.Now().Unix())
549-
}
547+
// [REMOVED] so we can control timestamps
548+
//header.Time = parent.Time + c.config.Period
549+
//if header.Time < uint64(time.Now().Unix()) {
550+
// header.Time = uint64(time.Now().Unix())
551+
//}
550552
return nil
551553
}
552554

core/blockchain.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
237237
bc.currentFastBlock.Store(nilBlock)
238238

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

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

254+
if currentHeader := bc.CurrentHeader(); currentHeader != nil {
255+
log.Debug("Read timestamp from last block. ", "timestamp", bc.CurrentHeader().Time)
256+
bc.SetCurrentTimestamp(int64(bc.CurrentHeader().Time))
257+
} else {
258+
bc.SetCurrentTimestamp(int64(0))
259+
}
260+
255261
if frozen, err := bc.db.Ancients(); err == nil && frozen > 0 {
256262
var (
257263
needRewind bool

docker-compose.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: "3"
2+
3+
services:
4+
geth_l2:
5+
build:
6+
context: .
7+
dockerfile: Dockerfile
8+
volumes:
9+
- l2-node-data:/mnt/l2-node/l2:rw
10+
environment:
11+
- CLEAR_DATA_KEY
12+
- TARGET_GAS_LIMIT
13+
- VOLUME_PATH=/mnt/l2-node/l2
14+
- HOSTNAME=geth_l2
15+
- PORT=8545
16+
- NETWORK_ID=108
17+
ports:
18+
- 8545:8545
19+
20+
volumes:
21+
l2-node-data:

miner/worker.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ const (
7474

7575
// staleThreshold is the maximum depth of the acceptable stale block.
7676
staleThreshold = 7
77+
78+
maxClockSkewSeconds = 600 // 10 mins for now
79+
timestampDelaySeconds = 300 // 5 mins for now
7780
)
7881

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

224228
// Submit first work to initialize pending state.
225229
if init {
@@ -362,6 +366,7 @@ func (w *worker) newWorkLoop(recommit time.Duration) {
362366
timer.Reset(recommit)
363367
continue
364368
}
369+
timestamp = w.chain.CurrentTimestamp()
365370
commit(true, commitInterruptResubmit)
366371
}
367372

@@ -613,6 +618,31 @@ func (w *worker) resultLoop() {
613618
}
614619
}
615620

621+
// timestampLoop is a loop that updates the timestamp used for blocks when it
622+
// is stale by more than a certain threshold.
623+
// TODO: Re-think this as everything comes together more.
624+
func (w *worker) timestampLoop() {
625+
timer := time.NewTimer(0)
626+
627+
for {
628+
select {
629+
case <-timer.C:
630+
currentTime := time.Now().Unix()
631+
skew := currentTime - w.chain.CurrentTimestamp()
632+
if skew > maxClockSkewSeconds {
633+
newTime := currentTime - timestampDelaySeconds
634+
w.chain.SetCurrentTimestamp(newTime)
635+
timer.Reset((maxClockSkewSeconds - timestampDelaySeconds) * time.Second)
636+
log.Debug("timestamp above max clock skew", "maxSkew", maxClockSkewSeconds, "overBy", timestampDelaySeconds, "newTime", newTime)
637+
} else {
638+
timer.Reset(time.Duration(maxClockSkewSeconds-skew) * time.Second)
639+
}
640+
case <-w.exitCh:
641+
return
642+
}
643+
}
644+
}
645+
616646
// makeCurrent creates a new environment for the current cycle.
617647
func (w *worker) makeCurrent(parent *types.Block, header *types.Header) error {
618648
state, err := w.chain.StateAt(parent.Root())

0 commit comments

Comments
 (0)