Skip to content

Commit

Permalink
P-Chain merkledb -- include txs in merkleized state (#2398)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Laine authored Dec 5, 2023
1 parent 093647c commit 0b875ab
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
12 changes: 5 additions & 7 deletions vms/platformvm/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var (
merkleSingletonPrefix = []byte{0x01}
merkleBlockPrefix = []byte{0x02}
merkleBlockIDsPrefix = []byte{0x03}
merkleTxPrefix = []byte{0x04}
merkleTxsPrefix = []byte{0x04}
merkleIndexUTXOsPrefix = []byte{0x05} // to serve UTXOIDs(addr)
merkleUptimesPrefix = []byte{0x06} // locally measured uptimes
merkleWeightDiffPrefix = []byte{0x07} // non-merkleized validators weight diff. TODO: should we merkleize them?
Expand Down Expand Up @@ -277,7 +277,6 @@ type state struct {
// FIND a way to reduce use of these. No use in verification of addedTxs
// a limited windows to support APIs
addedTxs map[ids.ID]*txAndStatus // map of txID -> {*txs.Tx, Status}
txDB database.Database

indexedUTXOsDB database.Database

Expand Down Expand Up @@ -365,7 +364,6 @@ func newState(
singletonDB = prefixdb.New(merkleSingletonPrefix, baseDB)
blockDB = prefixdb.New(merkleBlockPrefix, baseDB)
blockIDsDB = prefixdb.New(merkleBlockIDsPrefix, baseDB)
txDB = prefixdb.New(merkleTxPrefix, baseDB)
indexedUTXOsDB = prefixdb.New(merkleIndexUTXOsPrefix, baseDB)
localUptimesDB = prefixdb.New(merkleUptimesPrefix, baseDB)
flatValidatorWeightDiffsDB = prefixdb.New(merkleWeightDiffPrefix, baseDB)
Expand Down Expand Up @@ -425,7 +423,6 @@ func newState(
blockIDDB: blockIDsDB,

addedTxs: make(map[ids.ID]*txAndStatus),
txDB: txDB,

indexedUTXOsDB: indexedUTXOsDB,

Expand Down Expand Up @@ -640,7 +637,8 @@ func (s *state) GetTx(txID ids.ID) (*txs.Tx, status.Status, error) {
return tx.tx, tx.status, nil
}

txBytes, err := s.txDB.Get(txID[:])
key := merkleTxKey(txID)
txBytes, err := s.merkleDB.Get(key)
if err != nil {
return nil, status.Unknown, err
}
Expand Down Expand Up @@ -1189,7 +1187,6 @@ func (s *state) Close() error {
s.flatValidatorPublicKeyDiffsDB.Close(),
s.localUptimesDB.Close(),
s.indexedUTXOsDB.Close(),
s.txDB.Close(),
s.blockDB.Close(),
s.blockIDDB.Close(),
s.merkleDB.Close(),
Expand Down Expand Up @@ -1635,7 +1632,8 @@ func (s *state) writeTxs() error {
// Note: Evict is used rather than Put here because stx may end up
// referencing additional data (because of shared byte slices) that
// would not be properly accounted for in the cache sizing.
if err := s.txDB.Put(txID[:], txBytes); err != nil {
key := merkleTxKey(txID)
if err := s.merkleDB.Put(key, txBytes); err != nil {
return fmt.Errorf("failed to add tx: %w", err)
}
}
Expand Down
7 changes: 7 additions & 0 deletions vms/platformvm/state/state_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,10 @@ func merkleSubnetOwnersKey(subnetID ids.ID) []byte {
copy(key[len(delegateeRewardsPrefix):], subnetID[:])
return key
}

func merkleTxKey(txID ids.ID) []byte {
key := make([]byte, len(merkleTxsPrefix)+ids.IDLen)
copy(key, merkleTxsPrefix)
copy(key[len(merkleTxsPrefix):], txID[:])
return key
}
10 changes: 10 additions & 0 deletions vms/platformvm/state/state_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,13 @@ func TestDelegateeRewardsKey(t *testing.T) {
require.Equal(nodeID[:], key[len(prefix):len(prefix)+len(nodeID[:])])
require.Equal(subnetID[:], key[len(prefix)+len(nodeID[:]):])
}

func TestMerkleTxKey(t *testing.T) {
require := require.New(t)
txID := ids.GenerateTestID()

key := merkleTxKey(txID)
require.Len(key, len(merkleTxsPrefix)+len(txID[:]))
require.Equal(merkleTxsPrefix, key[:len(merkleTxsPrefix)])
require.Equal(txID[:], key[len(merkleTxsPrefix):])
}

0 comments on commit 0b875ab

Please sign in to comment.