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

ACP-77: Implement validator state #3388

Draft
wants to merge 173 commits into
base: refactor-write-current-stakers
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 110 commits
Commits
Show all changes
173 commits
Select commit Hold shift + click to select a range
9c8835b
remove subnet IDs from uptime pkg
ceyonur Jul 24, 2024
733d5fd
remove subnet uptimes from platforvm
ceyonur Jul 25, 2024
10d8e86
remove subnet uptimes from p2p
ceyonur Jul 25, 2024
a2f69d0
remove subnet uptimes from api
ceyonur Jul 25, 2024
ee287c6
add tracked bool
ceyonur Jul 25, 2024
a42e48c
remove unnecessary err
ceyonur Jul 26, 2024
4c542af
remove connected subnet msg
ceyonur Jul 26, 2024
941f536
Merge branch 'master' into remove-subnetid-uptime-manager
ceyonur Jul 26, 2024
9161864
fix linter
ceyonur Jul 26, 2024
dfded5e
Merge branch 'master' into remove-subnetid-uptime-manager
ceyonur Jul 31, 2024
f275c15
rework on tests and reviews
ceyonur Jul 31, 2024
d9355cc
fix linter
ceyonur Jul 31, 2024
46501ad
Update proto/p2p/p2p.proto
ceyonur Aug 6, 2024
b7459bd
fix comment
ceyonur Aug 6, 2024
b81b737
Update vms/platformvm/service_test.go
ceyonur Aug 6, 2024
f6bb383
use disconnect in stop tracking
ceyonur Aug 6, 2024
7a6f7eb
remove todo comment
ceyonur Aug 6, 2024
64dec9c
Merge branch 'master' into remove-subnetid-uptime-manager
ceyonur Aug 6, 2024
33eb562
Merge branch 'master' into remove-subnetid-uptime-manager
ceyonur Sep 8, 2024
cba7a50
remove unused err
ceyonur Sep 8, 2024
b4955d6
remove subnet connector mock
ceyonur Sep 8, 2024
3c9096e
Merge branch 'master' into remove-subnetid-uptime-manager
ceyonur Sep 10, 2024
1b22842
WIP Add Expiry Replay Protection
StephenButtolph Sep 10, 2024
ad83805
rename
StephenButtolph Sep 10, 2024
7820c17
wip
StephenButtolph Sep 10, 2024
20ed013
Replace iterator equality helper
StephenButtolph Sep 10, 2024
fcb388c
merged
StephenButtolph Sep 10, 2024
a02b51c
Finish diff implementation
StephenButtolph Sep 11, 2024
daf45a3
Merge branch 'master' into implement-acp-77-add-validator-replay-prot…
StephenButtolph Sep 11, 2024
7bda9b1
Finish state implementation
StephenButtolph Sep 11, 2024
f24ca9f
lint
StephenButtolph Sep 11, 2024
463ce02
Implement iterator deduplicator
StephenButtolph Sep 11, 2024
4ce1977
Merge branch 'master' into remove-subnetid-uptime-manager
ceyonur Sep 11, 2024
3cc413a
Reuse Filter
StephenButtolph Sep 11, 2024
4de908e
Merge branch 'master' into implement-deduplication-iterator
StephenButtolph Sep 11, 2024
09f71e6
merged
StephenButtolph Sep 11, 2024
114beeb
nit
StephenButtolph Sep 11, 2024
0cd4f89
Merge branch 'master' into implement-acp-77-add-validator-replay-prot…
StephenButtolph Sep 11, 2024
57810a0
Add comments
StephenButtolph Sep 11, 2024
e89671a
otherExpiries -> unexpectedExpiries
StephenButtolph Sep 12, 2024
72f972c
restructure test
StephenButtolph Sep 12, 2024
e8eea6f
reduce diff
StephenButtolph Sep 12, 2024
d67408d
nit
StephenButtolph Sep 12, 2024
8571163
Merge branch 'master' into implement-acp-77-sov-validators-state
StephenButtolph Sep 13, 2024
6e457fa
Implement acp-77 state diff
StephenButtolph Sep 13, 2024
86754d0
Fix apply reordering
StephenButtolph Sep 13, 2024
c68afec
Implement acp-77 state diff without historical diffs
StephenButtolph Sep 14, 2024
09faa6b
nit
StephenButtolph Sep 14, 2024
51937f7
nit
StephenButtolph Sep 14, 2024
88a310e
Add num SoV validators on a subnet
StephenButtolph Sep 15, 2024
b11ff7e
fix tests
StephenButtolph Sep 15, 2024
6c9e0e0
Merge branch 'master' into implement-acp-77-sov-validators-state
StephenButtolph Sep 15, 2024
b028ada
write historical diffs
StephenButtolph Sep 15, 2024
42d61ef
Implement and test state diffs
StephenButtolph Sep 16, 2024
8ef4fcf
Store weights rather than counts
StephenButtolph Sep 16, 2024
e2045e2
load sov validators on startup
StephenButtolph Sep 16, 2024
863c92e
Merge branch 'master' into remove-subnetid-uptime-manager
ceyonur Sep 16, 2024
e0beab1
update in-memory validator sets
StephenButtolph Sep 17, 2024
410bcda
remove comment
StephenButtolph Sep 17, 2024
149e21e
Add SoV excess
StephenButtolph Sep 17, 2024
6c0945b
Merge branch 'master' into implement-acp-77-sov-validators-state
StephenButtolph Sep 18, 2024
a441d62
Merge branch 'master' into remove-subnetid-uptime-manager
ceyonur Sep 19, 2024
fbc5ce1
Merge branch 'master' into implement-acp-77-sov-validators-state
StephenButtolph Sep 20, 2024
cfc2790
Merge branch 'remove-subnetid-uptime-manager' into implement-acp-77-s…
StephenButtolph Sep 22, 2024
449994a
write subnet public key diffs
StephenButtolph Sep 23, 2024
744f4c7
merged
StephenButtolph Sep 23, 2024
d9108a7
mocks
StephenButtolph Sep 23, 2024
98020f1
Bound GetNextStakerTime
StephenButtolph Sep 24, 2024
cc9a086
fix test
StephenButtolph Sep 24, 2024
a3f0d94
add comment
StephenButtolph Sep 24, 2024
de81518
Add tests
StephenButtolph Sep 24, 2024
c062890
Merge branch 'bound-next-staker-time' into implement-acp-77-sov-valid…
StephenButtolph Sep 24, 2024
b68ed8a
merged
StephenButtolph Sep 25, 2024
8fe987c
ACP-77: Update warp messages to follow new specification
StephenButtolph Sep 30, 2024
30efc6d
ACP-77: Add warp message helpers to follow new specification
StephenButtolph Oct 1, 2024
903f7c3
migrate x/sync to p2p
joshua-kim Jun 11, 2024
34fe3a6
nit
joshua-kim Sep 10, 2024
b9d5507
add acp-118 implementation
joshua-kim Sep 10, 2024
99f0cde
undo diff
joshua-kim Oct 1, 2024
00fc8d1
nit
joshua-kim Oct 1, 2024
c08a1d7
undo
joshua-kim Oct 1, 2024
9d07a45
nit
joshua-kim Oct 1, 2024
f91e0a8
nit
joshua-kim Oct 1, 2024
a35b090
add context
joshua-kim Oct 1, 2024
dd7029c
fix
joshua-kim Oct 1, 2024
8978452
rename attestor -> verifier
joshua-kim Oct 1, 2024
a383209
nit
joshua-kim Oct 1, 2024
8b52ead
nit
joshua-kim Oct 1, 2024
3859d98
Merge branch 'master' into implement-acp-77-update-warp-messages
StephenButtolph Oct 1, 2024
0462fda
Merge branch 'acp-118-handler' into implement-acp-77-temp.0
StephenButtolph Oct 1, 2024
5a362bd
Merge branch 'implement-acp-77-temp.0' into implement-acp-77-sov-vali…
StephenButtolph Oct 1, 2024
a3cc8e8
merged
StephenButtolph Oct 1, 2024
ddb9ba6
Add deactivation owner
StephenButtolph Oct 1, 2024
556f1eb
fix tests
StephenButtolph Oct 1, 2024
891bca4
Store conversionID
StephenButtolph Oct 2, 2024
a33e0df
Include validationID
StephenButtolph Oct 2, 2024
8fee2b4
Merge branch 'master' into implement-acp-77-sov-validators-state
StephenButtolph Oct 2, 2024
0d3d3b9
Merge branch 'master' into implement-acp-77-sov-validators-state
StephenButtolph Oct 3, 2024
01c9072
remove unexpected file
StephenButtolph Oct 3, 2024
01de954
Merge branch 'master' into implement-acp-77-sov-validators-state
StephenButtolph Oct 3, 2024
1ca7524
Merge branch 'master' into implement-acp-77-sov-validators-state
StephenButtolph Oct 7, 2024
237f590
Allow xsvm to sign arbitrary warp messages
StephenButtolph Oct 7, 2024
bc53046
Merge branch 'implement-acp-77-allow-xsvm-signing' into implement-acp…
StephenButtolph Oct 7, 2024
127a3fa
nit
StephenButtolph Oct 7, 2024
1986804
nit cleanup
StephenButtolph Oct 9, 2024
8279972
Remove unexpected block unwrapping
StephenButtolph Oct 9, 2024
3d5d6c1
Merge branch 'fix-block-passing' into implement-acp-77-sov-validators…
StephenButtolph Oct 10, 2024
d7ba4fb
Merge branch 'master' into implement-acp-77-sov-validators-state
StephenButtolph Oct 11, 2024
d550c97
Merge branch 'master' into implement-acp-77-sov-validators-state
StephenButtolph Oct 14, 2024
9226649
Merge branch 'master' into implement-acp-77-sov-validators-state
StephenButtolph Oct 15, 2024
83cc0dc
merged
StephenButtolph Oct 19, 2024
702639a
ACP-77: Add ConversionID to state
StephenButtolph Oct 19, 2024
4b562f0
nit
StephenButtolph Oct 19, 2024
7f3dd5e
nit
StephenButtolph Oct 19, 2024
f40c1b3
nit
StephenButtolph Oct 19, 2024
f69ec51
merged
StephenButtolph Oct 19, 2024
51f009b
nit
StephenButtolph Oct 19, 2024
36714ed
Merge branch 'implement-acp-77-add-conversion-id' into implement-acp-…
StephenButtolph Oct 19, 2024
05a7b13
nit
StephenButtolph Oct 19, 2024
09c5126
Merge branch 'implement-acp-77-add-conversion-id' into implement-acp-…
StephenButtolph Oct 19, 2024
a1db89e
nit
StephenButtolph Oct 19, 2024
2e7595c
Merge branch 'implement-acp-77-add-conversion-id' into implement-acp-…
StephenButtolph Oct 19, 2024
575b36a
remove unneeded assignment
StephenButtolph Oct 19, 2024
d2d0f69
update comment
StephenButtolph Oct 19, 2024
b6b6515
update comment
StephenButtolph Oct 19, 2024
6ffcd1b
update doc
StephenButtolph Oct 19, 2024
ad00180
Address comments
StephenButtolph Oct 20, 2024
f1b07d2
nit
StephenButtolph Oct 20, 2024
7fb99c0
fix unit tests
StephenButtolph Oct 20, 2024
65b5d50
Merge branch 'implement-acp-77-add-conversion-id' into implement-acp-…
StephenButtolph Oct 20, 2024
8791e78
merged
StephenButtolph Oct 20, 2024
f31cfc6
Add SoV Excess to P-chain state
StephenButtolph Oct 20, 2024
fe9a76d
merged
StephenButtolph Oct 20, 2024
58d5b8d
Populate BLS key diffs for subnet validators
StephenButtolph Oct 21, 2024
273fbbe
Populate BLS key diffs for subnet validators
StephenButtolph Oct 21, 2024
290ef97
Update mocks
StephenButtolph Oct 22, 2024
9155e1f
Fix tests
StephenButtolph Oct 22, 2024
cadf8f7
Merge branch 'master' into populate-subnet-public-key-diffs
StephenButtolph Oct 22, 2024
803d0c4
nit
StephenButtolph Oct 22, 2024
76f4eee
Merge branch 'populate-subnet-public-key-diffs' of github.com:ava-lab…
StephenButtolph Oct 22, 2024
a2c7773
Update test and populate public keys during startup
StephenButtolph Oct 22, 2024
95c42a1
comment
StephenButtolph Oct 23, 2024
3d7bd81
Merge branch 'master' into populate-subnet-public-key-diffs
StephenButtolph Oct 23, 2024
bbc395b
merged
StephenButtolph Oct 23, 2024
3a03597
merged
StephenButtolph Oct 23, 2024
21cdc32
Update P-chain state staker tests
StephenButtolph Oct 24, 2024
845f1d2
merged
StephenButtolph Oct 24, 2024
91a7cd7
fix test
StephenButtolph Oct 24, 2024
df02f3c
Merge branch 'update-state-staker-tests' into populate-subnet-public-…
StephenButtolph Oct 24, 2024
c848fee
fix test
StephenButtolph Oct 24, 2024
79c9b40
nit
StephenButtolph Oct 24, 2024
d8819fc
Merge branch 'master' into update-state-staker-tests
StephenButtolph Oct 24, 2024
667002e
Merge branch 'update-state-staker-tests' into populate-subnet-public-…
StephenButtolph Oct 24, 2024
31be1e2
merged
StephenButtolph Oct 24, 2024
8f0a080
merged
StephenButtolph Oct 24, 2024
d482de8
Merge branch 'populate-subnet-public-key-diffs' into implement-acp-77…
StephenButtolph Oct 24, 2024
e8b21ec
Verify no SoV + legacy overlap
StephenButtolph Oct 24, 2024
7f517c7
Fix legacy validator migration
StephenButtolph Oct 24, 2024
7d0d7e5
merged
StephenButtolph Oct 25, 2024
38ee164
Merge branch 'implement-acp-77-add-subnetid-nodeid' into implement-ac…
StephenButtolph Oct 26, 2024
d2137ef
fix merge
StephenButtolph Oct 26, 2024
6c3116a
merged
StephenButtolph Oct 27, 2024
d0d1602
reduce diff
StephenButtolph Oct 27, 2024
d397375
reduce diff
StephenButtolph Oct 27, 2024
5f8a09c
reduce diff
StephenButtolph Oct 27, 2024
3e9dc01
reduce diff
StephenButtolph Oct 27, 2024
da3a726
reduce diff
StephenButtolph Oct 27, 2024
8483ced
cleanup
StephenButtolph Oct 27, 2024
0022a65
Merge branch 'refactor-write-current-stakers' into implement-acp-77-s…
StephenButtolph Oct 27, 2024
8bbeee6
Add comment
StephenButtolph Oct 27, 2024
08dd776
comment
StephenButtolph Oct 27, 2024
255b0bf
nit
StephenButtolph Oct 27, 2024
3bc547d
reduce diff
StephenButtolph Oct 27, 2024
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
8 changes: 8 additions & 0 deletions database/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ func GetUInt64(db KeyValueReader, key []byte) (uint64, error) {
return ParseUInt64(b)
}

func GetOrDefaultUInt64(db KeyValueReader, key []byte, def uint64) (uint64, error) {
v, err := GetUInt64(db, key)
if err == ErrNotFound {
return def, nil
}
return v, err
}

func PackUInt64(val uint64) []byte {
bytes := make([]byte, Uint64Size)
binary.BigEndian.PutUint64(bytes, val)
Expand Down
4 changes: 4 additions & 0 deletions vms/platformvm/block/executor/proposal_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func TestApricotProposalBlockTimeVerification(t *testing.T) {
onParentAccept.EXPECT().GetTimestamp().Return(chainTime).AnyTimes()
onParentAccept.EXPECT().GetFeeState().Return(gas.State{}).AnyTimes()
onParentAccept.EXPECT().GetAccruedFees().Return(uint64(0)).AnyTimes()
onParentAccept.EXPECT().GetSoVExcess().Return(gas.Gas(0)).AnyTimes()
onParentAccept.EXPECT().NumActiveSubnetOnlyValidators().Return(0).AnyTimes()

onParentAccept.EXPECT().GetCurrentStakerIterator().Return(
iterator.FromSlice(&state.Staker{
Expand Down Expand Up @@ -163,6 +165,8 @@ func TestBanffProposalBlockTimeVerification(t *testing.T) {
onParentAccept.EXPECT().GetTimestamp().Return(parentTime).AnyTimes()
onParentAccept.EXPECT().GetFeeState().Return(gas.State{}).AnyTimes()
onParentAccept.EXPECT().GetAccruedFees().Return(uint64(0)).AnyTimes()
onParentAccept.EXPECT().GetSoVExcess().Return(gas.Gas(0)).AnyTimes()
onParentAccept.EXPECT().NumActiveSubnetOnlyValidators().Return(0).AnyTimes()
onParentAccept.EXPECT().GetCurrentSupply(constants.PrimaryNetworkID).Return(uint64(1000), nil).AnyTimes()

env.blkManager.(*manager).blkIDToState[parentID] = &blockState{
Expand Down
4 changes: 4 additions & 0 deletions vms/platformvm/block/executor/standard_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func TestApricotStandardBlockTimeVerification(t *testing.T) {
onParentAccept.EXPECT().GetTimestamp().Return(chainTime).AnyTimes()
onParentAccept.EXPECT().GetFeeState().Return(gas.State{}).AnyTimes()
onParentAccept.EXPECT().GetAccruedFees().Return(uint64(0)).AnyTimes()
onParentAccept.EXPECT().GetSoVExcess().Return(gas.Gas(0)).AnyTimes()
onParentAccept.EXPECT().NumActiveSubnetOnlyValidators().Return(0).AnyTimes()

// wrong height
apricotChildBlk, err := block.NewApricotStandardBlock(
Expand Down Expand Up @@ -135,6 +137,8 @@ func TestBanffStandardBlockTimeVerification(t *testing.T) {
onParentAccept.EXPECT().GetTimestamp().Return(chainTime).AnyTimes()
onParentAccept.EXPECT().GetFeeState().Return(gas.State{}).AnyTimes()
onParentAccept.EXPECT().GetAccruedFees().Return(uint64(0)).AnyTimes()
onParentAccept.EXPECT().GetSoVExcess().Return(gas.Gas(0)).AnyTimes()
onParentAccept.EXPECT().NumActiveSubnetOnlyValidators().Return(0).AnyTimes()

txID := ids.GenerateTestID()
utxo := &avax.UTXO{
Expand Down
10 changes: 10 additions & 0 deletions vms/platformvm/block/executor/verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ func TestVerifierVisitProposalBlock(t *testing.T) {
parentOnAcceptState.EXPECT().GetTimestamp().Return(timestamp).Times(2)
parentOnAcceptState.EXPECT().GetFeeState().Return(gas.State{}).Times(2)
parentOnAcceptState.EXPECT().GetAccruedFees().Return(uint64(0)).Times(2)
parentOnAcceptState.EXPECT().GetSoVExcess().Return(gas.Gas(0)).Times(2)
parentOnAcceptState.EXPECT().NumActiveSubnetOnlyValidators().Return(0).Times(2)

backend := &backend{
lastAccepted: parentID,
Expand Down Expand Up @@ -336,6 +338,8 @@ func TestVerifierVisitStandardBlock(t *testing.T) {
parentState.EXPECT().GetTimestamp().Return(timestamp).Times(1)
parentState.EXPECT().GetFeeState().Return(gas.State{}).Times(1)
parentState.EXPECT().GetAccruedFees().Return(uint64(0)).Times(1)
parentState.EXPECT().GetSoVExcess().Return(gas.Gas(0)).Times(1)
parentState.EXPECT().NumActiveSubnetOnlyValidators().Return(0).Times(1)
parentStatelessBlk.EXPECT().Height().Return(uint64(1)).Times(1)
mempool.EXPECT().Remove(apricotBlk.Txs()).Times(1)

Expand Down Expand Up @@ -598,6 +602,8 @@ func TestBanffAbortBlockTimestampChecks(t *testing.T) {
s.EXPECT().GetTimestamp().Return(parentTime).Times(3)
s.EXPECT().GetFeeState().Return(gas.State{}).Times(3)
s.EXPECT().GetAccruedFees().Return(uint64(0)).Times(3)
s.EXPECT().GetSoVExcess().Return(gas.Gas(0)).Times(3)
s.EXPECT().NumActiveSubnetOnlyValidators().Return(0).Times(3)

onDecisionState, err := state.NewDiff(parentID, backend)
require.NoError(err)
Expand Down Expand Up @@ -696,6 +702,8 @@ func TestBanffCommitBlockTimestampChecks(t *testing.T) {
s.EXPECT().GetTimestamp().Return(parentTime).Times(3)
s.EXPECT().GetFeeState().Return(gas.State{}).Times(3)
s.EXPECT().GetAccruedFees().Return(uint64(0)).Times(3)
s.EXPECT().GetSoVExcess().Return(gas.Gas(0)).Times(3)
s.EXPECT().NumActiveSubnetOnlyValidators().Return(0).Times(3)

onDecisionState, err := state.NewDiff(parentID, backend)
require.NoError(err)
Expand Down Expand Up @@ -812,6 +820,8 @@ func TestVerifierVisitStandardBlockWithDuplicateInputs(t *testing.T) {
parentState.EXPECT().GetTimestamp().Return(timestamp).Times(1)
parentState.EXPECT().GetFeeState().Return(gas.State{}).Times(1)
parentState.EXPECT().GetAccruedFees().Return(uint64(0)).Times(1)
parentState.EXPECT().GetSoVExcess().Return(gas.Gas(0)).Times(1)
parentState.EXPECT().NumActiveSubnetOnlyValidators().Return(0).Times(1)
parentStatelessBlk.EXPECT().Parent().Return(grandParentID).Times(1)

err = verifier.ApricotStandardBlock(blk)
Expand Down
5 changes: 4 additions & 1 deletion vms/platformvm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ type GetSubnetResponse struct {
// subnet transformation tx ID for an elastic subnet
SubnetTransformationTxID ids.ID `json:"subnetTransformationTxID"`
// subnet manager information for a permissionless L1
ConversionID ids.ID `json:"conversionID"`
ManagerChainID ids.ID `json:"managerChainID"`
ManagerAddress types.JSONByteSlice `json:"managerAddress"`
}
Expand Down Expand Up @@ -490,12 +491,14 @@ func (s *Service) GetSubnet(_ *http.Request, args *GetSubnetArgs, response *GetS
return err
}

switch chainID, addr, err := s.vm.state.GetSubnetManager(args.SubnetID); err {
switch conversionID, chainID, addr, err := s.vm.state.GetSubnetConversion(args.SubnetID); err {
case nil:
response.IsPermissioned = false
response.ConversionID = conversionID
response.ManagerChainID = chainID
response.ManagerAddress = addr
case database.ErrNotFound:
response.ConversionID = ids.Empty
response.ManagerChainID = ids.Empty
response.ManagerAddress = []byte(nil)
default:
Expand Down
148 changes: 124 additions & 24 deletions vms/platformvm/state/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ type diff struct {
parentID ids.ID
stateVersions Versions

timestamp time.Time
feeState gas.State
accruedFees uint64
timestamp time.Time
feeState gas.State
sovExcess gas.Gas
accruedFees uint64
parentActiveSOVs int

// Subnet ID --> supply of native asset of the subnet
currentSupply map[ids.ID]uint64

expiryDiff *expiryDiff
sovDiff *subnetOnlyValidatorsDiff

currentStakerDiffs diffStakers
// map of subnetID -> nodeID -> total accrued delegatee rewards
Expand All @@ -52,8 +55,8 @@ type diff struct {
addedSubnetIDs []ids.ID
// Subnet ID --> Owner of the subnet
subnetOwners map[ids.ID]fx.Owner
// Subnet ID --> Manager of the subnet
subnetManagers map[ids.ID]chainIDAndAddr
// Subnet ID --> Conversion of the subnet
subnetConversions map[ids.ID]subnetConversion
// Subnet ID --> Tx that transforms the subnet
transformedSubnets map[ids.ID]*txs.Tx

Expand All @@ -76,14 +79,17 @@ func NewDiff(
return nil, fmt.Errorf("%w: %s", ErrMissingParentState, parentID)
}
return &diff{
parentID: parentID,
stateVersions: stateVersions,
timestamp: parentState.GetTimestamp(),
feeState: parentState.GetFeeState(),
accruedFees: parentState.GetAccruedFees(),
expiryDiff: newExpiryDiff(),
subnetOwners: make(map[ids.ID]fx.Owner),
subnetManagers: make(map[ids.ID]chainIDAndAddr),
parentID: parentID,
stateVersions: stateVersions,
timestamp: parentState.GetTimestamp(),
feeState: parentState.GetFeeState(),
sovExcess: parentState.GetSoVExcess(),
accruedFees: parentState.GetAccruedFees(),
parentActiveSOVs: parentState.NumActiveSubnetOnlyValidators(),
expiryDiff: newExpiryDiff(),
sovDiff: newSubnetOnlyValidatorsDiff(),
subnetOwners: make(map[ids.ID]fx.Owner),
subnetConversions: make(map[ids.ID]subnetConversion),
}, nil
}

Expand Down Expand Up @@ -117,6 +123,14 @@ func (d *diff) SetFeeState(feeState gas.State) {
d.feeState = feeState
}

func (d *diff) GetSoVExcess() gas.Gas {
return d.sovExcess
}

func (d *diff) SetSoVExcess(excess gas.Gas) {
d.sovExcess = excess
}

func (d *diff) GetAccruedFees() uint64 {
return d.accruedFees
}
Expand Down Expand Up @@ -184,6 +198,70 @@ func (d *diff) DeleteExpiry(entry ExpiryEntry) {
d.expiryDiff.DeleteExpiry(entry)
}

func (d *diff) GetActiveSubnetOnlyValidatorsIterator() (iterator.Iterator[SubnetOnlyValidator], error) {
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}

parentIterator, err := parentState.GetActiveSubnetOnlyValidatorsIterator()
if err != nil {
return nil, err
}

return d.sovDiff.getActiveSubnetOnlyValidatorsIterator(parentIterator), nil
}

func (d *diff) NumActiveSubnetOnlyValidators() int {
return d.parentActiveSOVs + d.sovDiff.numAddedActive
}

func (d *diff) WeightOfSubnetOnlyValidators(subnetID ids.ID) (uint64, error) {
if weight, modified := d.sovDiff.modifiedTotalWeight[subnetID]; modified {
return weight, nil
}

parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return 0, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}

return parentState.WeightOfSubnetOnlyValidators(subnetID)
}

func (d *diff) GetSubnetOnlyValidator(validationID ids.ID) (SubnetOnlyValidator, error) {
if sov, modified := d.sovDiff.modified[validationID]; modified {
if sov.Weight == 0 {
ceyonur marked this conversation as resolved.
Show resolved Hide resolved
return SubnetOnlyValidator{}, database.ErrNotFound
}
return sov, nil
}

parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return SubnetOnlyValidator{}, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}

return parentState.GetSubnetOnlyValidator(validationID)
}

func (d *diff) HasSubnetOnlyValidator(subnetID ids.ID, nodeID ids.NodeID) (bool, error) {
if has, modified := d.sovDiff.hasSubnetOnlyValidator(subnetID, nodeID); modified {
return has, nil
}

parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return false, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}

return parentState.HasSubnetOnlyValidator(subnetID, nodeID)
}

func (d *diff) PutSubnetOnlyValidator(sov SubnetOnlyValidator) error {
return d.sovDiff.putSubnetOnlyValidator(d, sov)
}

func (d *diff) GetCurrentValidator(subnetID ids.ID, nodeID ids.NodeID) (*Staker, error) {
// If the validator was modified in this diff, return the modified
// validator.
Expand Down Expand Up @@ -357,23 +435,24 @@ func (d *diff) SetSubnetOwner(subnetID ids.ID, owner fx.Owner) {
d.subnetOwners[subnetID] = owner
}

func (d *diff) GetSubnetManager(subnetID ids.ID) (ids.ID, []byte, error) {
if manager, exists := d.subnetManagers[subnetID]; exists {
return manager.ChainID, manager.Addr, nil
func (d *diff) GetSubnetConversion(subnetID ids.ID) (ids.ID, ids.ID, []byte, error) {
if conversion, exists := d.subnetConversions[subnetID]; exists {
return conversion.ConversionID, conversion.ChainID, conversion.Addr, nil
}

// If the subnet manager was not assigned in this diff, ask the parent state.
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return ids.Empty, nil, ErrMissingParentState
return ids.Empty, ids.Empty, nil, ErrMissingParentState
}
return parentState.GetSubnetManager(subnetID)
return parentState.GetSubnetConversion(subnetID)
}

func (d *diff) SetSubnetManager(subnetID ids.ID, chainID ids.ID, addr []byte) {
d.subnetManagers[subnetID] = chainIDAndAddr{
ChainID: chainID,
Addr: addr,
func (d *diff) SetSubnetConversion(subnetID ids.ID, conversionID ids.ID, chainID ids.ID, addr []byte) {
d.subnetConversions[subnetID] = subnetConversion{
ConversionID: conversionID,
ChainID: chainID,
Addr: addr,
}
}

Expand Down Expand Up @@ -485,6 +564,7 @@ func (d *diff) DeleteUTXO(utxoID ids.ID) {
func (d *diff) Apply(baseState Chain) error {
baseState.SetTimestamp(d.timestamp)
baseState.SetFeeState(d.feeState)
baseState.SetSoVExcess(d.sovExcess)
baseState.SetAccruedFees(d.accruedFees)
for subnetID, supply := range d.currentSupply {
baseState.SetCurrentSupply(subnetID, supply)
Expand All @@ -496,6 +576,26 @@ func (d *diff) Apply(baseState Chain) error {
baseState.DeleteExpiry(entry)
}
}
// Ensure that all sov deletions happen before any sov additions. This
// ensures that a subnetID+nodeID pair that was deleted and then re-added in
// a single diff can't get reordered into the addition happening first;
// which would return an error.
for _, sov := range d.sovDiff.modified {
if sov.Weight != 0 {
continue
}
if err := baseState.PutSubnetOnlyValidator(sov); err != nil {
return err
}
}
for _, sov := range d.sovDiff.modified {
if sov.Weight == 0 {
continue
}
if err := baseState.PutSubnetOnlyValidator(sov); err != nil {
return err
}
}
for _, subnetValidatorDiffs := range d.currentStakerDiffs.validatorDiffs {
for _, validatorDiff := range subnetValidatorDiffs {
switch validatorDiff.validatorStatus {
Expand Down Expand Up @@ -576,8 +676,8 @@ func (d *diff) Apply(baseState Chain) error {
for subnetID, owner := range d.subnetOwners {
baseState.SetSubnetOwner(subnetID, owner)
}
for subnetID, manager := range d.subnetManagers {
baseState.SetSubnetManager(subnetID, manager.ChainID, manager.Addr)
for subnetID, conversion := range d.subnetConversions {
baseState.SetSubnetConversion(subnetID, conversion.ConversionID, conversion.ChainID, conversion.Addr)
}
return nil
}
Loading
Loading