Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P-Chain merkledb -- don't merkleize last accepted block ID #2397

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 19 additions & 23 deletions vms/platformvm/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ var (
merkleBlsKeyDiffPrefix = []byte{0x08}
merkleRewardUtxosPrefix = []byte{0x09}

initializedKey = []byte("initialized")
initializedKey = []byte{0x00}
lastAcceptedBlockIDKey = []byte{0x01}

// merkle db sections
metadataSectionPrefix = byte(0x00)
merkleChainTimeKey = []byte{metadataSectionPrefix, 0x00}
merkleLastAcceptedBlkIDKey = []byte{metadataSectionPrefix, 0x01}
merkleSuppliesPrefix = []byte{metadataSectionPrefix, 0x02}
metadataSectionPrefix = byte(0x00)
merkleChainTimeKey = []byte{metadataSectionPrefix, 0x00}
merkleSuppliesPrefix = []byte{metadataSectionPrefix, 0x01}

permissionedSubnetSectionPrefix = []byte{0x01}
elasticSubnetSectionPrefix = []byte{0x02}
Expand Down Expand Up @@ -986,6 +986,19 @@ func (s *state) syncGenesis(genesisBlk block.Block, genesis *genesis.Genesis) er

// Load pulls data previously stored on disk that is expected to be in memory.
func (s *state) load(hasSynced bool) error {
// load last accepted block
lastAcceptedBlkIDBytes, err := s.singletonDB.Get(lastAcceptedBlockIDKey)
if err != nil {
return err
}

lastAcceptedBlkID, err := ids.ToID(lastAcceptedBlkIDBytes)
if err != nil {
return err
}
s.SetLastAccepted(lastAcceptedBlkID)
s.latestCommittedLastAcceptedBlkID = lastAcceptedBlkID

return utils.Err(
s.loadMerkleMetadata(),
s.loadCurrentStakers(),
Expand All @@ -1011,16 +1024,6 @@ func (s *state) loadMerkleMetadata() error {
s.latestComittedChainTime = chainTime
s.SetTimestamp(chainTime)

// load last accepted block
blkIDBytes, err := s.merkleDB.Get(merkleLastAcceptedBlkIDKey)
if err != nil {
return err
}
lastAcceptedBlkID := ids.Empty
copy(lastAcceptedBlkID[:], blkIDBytes)
s.latestCommittedLastAcceptedBlkID = lastAcceptedBlkID
s.SetLastAccepted(lastAcceptedBlkID)

// We don't need to load supplies. Unlike chain time and last block ID,
// which have the persisted* attribute, we signify that a supply hasn't
// been modified by making it nil.
Expand Down Expand Up @@ -1172,6 +1175,7 @@ func (s *state) write(updateValidators bool, height uint64) error {
s.writeBlsKeyDiffs(height, blsKeyDiffs),
s.writeRewardUTXOs(),
s.updateValidatorSet(updateValidators, valSetDiff, weightDiffs),
s.singletonDB.Put(lastAcceptedBlockIDKey, s.lastAcceptedBlkID[:]), // Write last accepted block ID
)
}

Expand Down Expand Up @@ -1803,14 +1807,6 @@ func (s *state) writeMetadata(batchOps *[]database.BatchOp) error {
s.latestComittedChainTime = s.chainTime
}

if s.lastAcceptedBlkID != s.latestCommittedLastAcceptedBlkID {
*batchOps = append(*batchOps, database.BatchOp{
Key: merkleLastAcceptedBlkIDKey,
Value: s.lastAcceptedBlkID[:],
})
s.latestCommittedLastAcceptedBlkID = s.lastAcceptedBlkID
}

// lastAcceptedBlockHeight not persisted yet in merkleDB state.
// TODO: Consider if it should be

Expand Down
Loading