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 -- include txs in merkleized state #2398

Merged
merged 25 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
20f7007
add newView to merkleState; refactor logMerkleRoot to not return error
Nov 20, 2023
91d9cda
add NewView to State interface
Nov 20, 2023
8785c55
wip add method GetMerkleChanges to diff
Nov 21, 2023
28384fb
WIP add replacement for writeCurrentStakers
Nov 22, 2023
0a92fc2
refactor writeCurrentStakers block
Nov 27, 2023
a3c8258
WIP add replacement for writePendingStakers
Nov 27, 2023
be4aec8
refactor writePendingStakers block
Nov 27, 2023
2760af7
Merge branch 'pchain_merkleDB' into pchain_merkleDB_view
Nov 27, 2023
a7b758b
implement NewView on diff
Nov 27, 2023
afa056b
Merge branch 'pchain_merkleDB' into pchain_merkleDB_view
Nov 28, 2023
192072c
update NewView interface
Nov 28, 2023
256b278
comment
Nov 28, 2023
6f832cc
unexport function
Nov 29, 2023
6fc395e
Merge branch 'pchain_merkleDB' into pchain_merkleDB_view
Nov 29, 2023
98dfdf1
Merge branch 'pchain_merkleDB' into pchain_merkleDB_view
Nov 29, 2023
cadce70
update writeMetadata portion of getMerkleChanges to include last acce…
Nov 29, 2023
d98b52b
include txs in merkleized state
Nov 29, 2023
0ae9ebb
Merge branch 'pchain_merkleDB' into pchain_merkleDB_view
Nov 30, 2023
673985b
Merge branch 'pchain_merkleDB_view' into pchain_merkleDB_merkleize_txs
Nov 30, 2023
1c501d6
appease linter
Nov 30, 2023
7d812e6
Merge branch 'pchain_merkleDB' into pchain_merkleDB_merkleize_txs
Dec 1, 2023
1d6f682
Merge branch 'pchain_merkleDB' into pchain_merkleDB_merkleize_txs
Dec 5, 2023
7cfe789
Merge branch 'pchain_merkleDB' into pchain_merkleDB_merkleize_txs
Dec 5, 2023
3442bca
revert unneeded change
Dec 5, 2023
db27c17
remove txDB
Dec 5, 2023
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
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):])
}
Loading