Skip to content

Commit 13126f3

Browse files
Enable subsecond block times using unix nano for Time in BaseHeader (#1146)
<!-- Please read and fill out this form before submitting your PR. Please make sure you have reviewed our contributors guide before submitting your first PR. --> ## Overview Closes: #1147 <!-- Please provide an explanation of the PR, including the appropriate context, background, goal, and rationale. If there is an issue with this information, please provide a tl;dr and link the issue. --> ## Checklist <!-- Please complete the checklist to ensure that the PR is ready to be reviewed. IMPORTANT: PRs should be left in Draft until the below checklist is completed. --> - [x] New and updated code has appropriate documentation - [x] New and updated code has new and/or updated testing - [x] Required CI checks are passing - [ ] Visual proof for any user facing features like CLI or documentation updates - [x] Linked issues closed with keywords
1 parent 13987f9 commit 13126f3

File tree

8 files changed

+24
-17
lines changed

8 files changed

+24
-17
lines changed

block/manager.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ const defaultBlockTime = 1 * time.Second
3838
// This is temporary solution. It will be removed in future versions.
3939
const maxSubmitAttempts = 30
4040

41-
// Applies to all channels, 100 is a large enough buffer to avoid blocking
41+
// Applies to most channels, 100 is a large enough buffer to avoid blocking
4242
const channelLength = 100
4343

44+
// Applies to the blockInCh, 10000 is a large enough number for blocks per DA block.
45+
const blockInChLength = 10000
46+
4447
// initialBackoff defines initial value for block submission backoff
4548
var initialBackoff = 100 * time.Millisecond
4649

@@ -177,7 +180,7 @@ func NewManager(
177180
// channels are buffered to avoid blocking on input/output operations, buffer sizes are arbitrary
178181
HeaderCh: make(chan *types.SignedHeader, channelLength),
179182
BlockCh: make(chan *types.Block, channelLength),
180-
blockInCh: make(chan newBlockEvent, channelLength),
183+
blockInCh: make(chan newBlockEvent, blockInChLength),
181184
blockStoreMtx: new(sync.Mutex),
182185
blockStore: blockStore,
183186
retrieveMtx: new(sync.Mutex),
@@ -436,9 +439,8 @@ func (m *Manager) BlockStoreRetrieveLoop(ctx context.Context) {
436439
}
437440
daHeight := atomic.LoadUint64(&m.daHeight)
438441
for _, block := range blocks {
439-
m.blockInCh <- newBlockEvent{block, daHeight}
440442
m.logger.Debug("block retrieved from p2p block sync", "blockHeight", block.Height(), "daHeight", daHeight)
441-
443+
m.blockInCh <- newBlockEvent{block, daHeight}
442444
}
443445
}
444446
lastBlockStoreHeight = blockStoreHeight
@@ -518,7 +520,10 @@ func (m *Manager) processNextDABlock(ctx context.Context) error {
518520
for _, block := range blockResp.Blocks {
519521
blockHash := block.Hash().String()
520522
m.blockCache.setHardConfirmed(blockHash)
521-
m.blockInCh <- newBlockEvent{block, daHeight}
523+
m.logger.Info("block marked as hard confirmed", "blockHeight", block.Height(), "blockHash", blockHash)
524+
if !m.blockCache.isSeen(blockHash) {
525+
m.blockInCh <- newBlockEvent{block, daHeight}
526+
}
522527
}
523528
return nil
524529
}

config/defaults.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ var DefaultNodeConfig = NodeConfig{
2121
Aggregator: false,
2222
LazyAggregator: false,
2323
BlockManagerConfig: BlockManagerConfig{
24-
BlockTime: 30 * time.Second,
24+
BlockTime: 1 * time.Second,
25+
DABlockTime: 30 * time.Second,
2526
NamespaceID: types.NamespaceID{},
2627
},
2728
DALayer: "mock",

da/mock/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func RandShares(total int) ([]share.Share, error) {
152152
return nil, fmt.Errorf("total must be power of 2: %d", total)
153153
}
154154

155-
var r = rand.New(rand.NewSource(time.Now().Unix())) //nolint:gosec
155+
var r = rand.New(rand.NewSource(time.Now().UnixNano())) //nolint:gosec
156156
shares := make([]share.Share, total)
157157
for i := range shares {
158158
shr := make([]byte, appconsts.ShareSize)
@@ -174,7 +174,7 @@ func RandShares(total int) ([]share.Share, error) {
174174

175175
// RandNamespace generates random valid data namespace for testing purposes.
176176
func RandNamespace() share.Namespace {
177-
var r = rand.New(rand.NewSource(time.Now().Unix())) //nolint:gosec
177+
var r = rand.New(rand.NewSource(time.Now().UnixNano())) //nolint:gosec
178178
rb := make([]byte, namespace.NamespaceVersionZeroIDSize)
179179
r.Read(rb) // nolint:gosec
180180
for {

state/executor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (e *BlockExecutor) CreateBlock(height uint64, lastCommit *types.Commit, las
104104
BaseHeader: types.BaseHeader{
105105
ChainID: e.chainID,
106106
Height: height,
107-
Time: uint64(time.Now().Unix()), // TODO(tzdybal): how to get TAI64?
107+
Time: uint64(time.Now().UnixNano()), // TODO(tzdybal): how to get TAI64?
108108
},
109109
//LastHeaderHash: lastHeaderHash,
110110
//LastCommitHash: lastCommitHash,

state/indexer/query_range.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (qr QueryRange) LowerBoundValue() interface{} {
4848
tmp := new(big.Int)
4949
return tmp.Add(t, big.NewInt(1))
5050
case time.Time:
51-
return t.Unix() + 1
51+
return t.UnixNano() + 1
5252

5353
default:
5454
panic("not implemented")
@@ -73,7 +73,7 @@ func (qr QueryRange) UpperBoundValue() interface{} {
7373
tmp := new(big.Int)
7474
return tmp.Sub(t, big.NewInt(1))
7575
case time.Time:
76-
return t.Unix() - 1
76+
return t.UnixNano() - 1
7777

7878
default:
7979
panic("not implemented")

types/header.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type Hash = header.Hash
1515
type BaseHeader struct {
1616
// Height represents the block height (aka block number) of a given header
1717
Height uint64
18-
// Time contains Unix time of a block
18+
// Time contains Unix nanotime of a block
1919
Time uint64
2020
// The Chain ID
2121
ChainID string
@@ -71,8 +71,9 @@ func (h *Header) LastHeader() Hash {
7171
return h.LastHeaderHash[:]
7272
}
7373

74+
// Returns unix time with nanosecond precision
7475
func (h *Header) Time() time.Time {
75-
return time.Unix(int64(h.BaseHeader.Time), 0)
76+
return time.Unix(0, int64(h.BaseHeader.Time))
7677
}
7778

7879
func (h *Header) Verify(untrst header.Header) error {

types/signed_header_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ func TestVerify(t *testing.T) {
5656
},
5757
{
5858
prepare: func() *SignedHeader {
59-
untrustedAdj.Header.BaseHeader.Time = uint64(untrustedAdj.Header.Time().Truncate(time.Hour).Unix())
59+
untrustedAdj.Header.BaseHeader.Time = uint64(untrustedAdj.Header.Time().Truncate(time.Hour).UnixNano())
6060
return untrustedAdj
6161
},
6262
err: true,
6363
},
6464
{
6565
prepare: func() *SignedHeader {
66-
untrustedAdj.Header.BaseHeader.Time = uint64(untrustedAdj.Header.Time().Add(time.Minute).Unix())
66+
untrustedAdj.Header.BaseHeader.Time = uint64(untrustedAdj.Header.Time().Add(time.Minute).UnixNano())
6767
return untrustedAdj
6868
},
6969
err: true,

types/test_utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func GetRandomSignedHeader() (*SignedHeader, ed25519.PrivKey, error) {
3737
BaseHeader: BaseHeader{
3838
ChainID: "test",
3939
Height: rand.Uint64(), //nolint:gosec,
40-
Time: uint64(time.Now().Unix()),
40+
Time: uint64(time.Now().UnixNano()),
4141
},
4242
LastHeaderHash: GetRandomBytes(32),
4343
LastCommitHash: GetRandomBytes(32),
@@ -70,7 +70,7 @@ func GetNextRandomHeader(signedHeader *SignedHeader, privKey ed25519.PrivKey) (*
7070
BaseHeader: BaseHeader{
7171
ChainID: "test",
7272
Height: uint64(signedHeader.Height() + 1),
73-
Time: uint64(time.Now().Unix()),
73+
Time: uint64(time.Now().UnixNano()),
7474
},
7575
LastHeaderHash: signedHeader.Hash(),
7676
DataHash: GetRandomBytes(32),

0 commit comments

Comments
 (0)