Skip to content

Commit e53fe1b

Browse files
authoredJun 13, 2024··
Merge pull request #6050 from onflow/fxamacker/port-pr-5942
Optimize atree migration by porting and modifying PR 5942 to feature/atree-inlining-cadence-v0.42

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed
 

‎cmd/util/cmd/execution-state-extract/execution_state_extract.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/rs/zerolog"
13+
"github.com/rs/zerolog/log"
1314
"go.uber.org/atomic"
1415
"golang.org/x/sync/errgroup"
1516

@@ -547,10 +548,21 @@ func newByAccountRegistersFromPayloadAccountGrouping(
547548
for accountRegisters := range results {
548549
oldAccountRegisters := registersByAccount.SetAccountRegisters(accountRegisters)
549550
if oldAccountRegisters != nil {
550-
return fmt.Errorf(
551-
"duplicate account registers for account %s",
551+
// Account grouping should never create multiple groups for an account.
552+
// In case it does anyway, merge the groups together,
553+
// by merging the existing registers into the new ones.
554+
555+
log.Warn().Msgf(
556+
"account registers already exist for account %x. merging %d existing registers into %d new",
552557
accountRegisters.Owner(),
558+
oldAccountRegisters.Count(),
559+
accountRegisters.Count(),
553560
)
561+
562+
err := accountRegisters.Merge(oldAccountRegisters)
563+
if err != nil {
564+
return fmt.Errorf("failed to merge account registers: %w", err)
565+
}
554566
}
555567
}
556568

‎cmd/util/ledger/util/payload_grouping.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ import (
1313
"github.com/onflow/flow-go/model/flow"
1414
)
1515

16-
// encodedKeyAddressPrefixLength is the length of the address prefix in the encoded key
17-
// 2 for uint16 of number of key parts
18-
// 4 for uint32 of the length of the first key part
19-
// 2 for uint16 of the key part type
20-
// 8 for the address which is the actual length of the first key part
21-
const encodedKeyAddressPrefixLength = 2 + 4 + 2 + flow.AddressLength
22-
2316
// minSizeForSplitSortingIntoGoroutines below this size, no need to split
2417
// the sorting into goroutines
2518
const minSizeForSplitSortingIntoGoroutines = 100_000
@@ -135,11 +128,17 @@ func (s sortablePayloads) Less(i, j int) bool {
135128
}
136129

137130
func (s sortablePayloads) Compare(i, j int) int {
138-
// sort descending to force one of the big accounts to be more at the beginning
139-
return bytes.Compare(
140-
s[j].EncodedKey()[:encodedKeyAddressPrefixLength],
141-
s[i].EncodedKey()[:encodedKeyAddressPrefixLength],
142-
)
131+
a, err := s[i].Address()
132+
if err != nil {
133+
panic(err)
134+
}
135+
136+
b, err := s[j].Address()
137+
if err != nil {
138+
panic(err)
139+
}
140+
141+
return bytes.Compare(a[:], b[:])
143142
}
144143

145144
func (s sortablePayloads) Swap(i, j int) {

‎cmd/util/ledger/util/registers/registers.go

+11
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,17 @@ func (a *AccountRegisters) insertPayloads(payloads []*ledger.Payload) {
263263
}
264264
}
265265

266+
func (a *AccountRegisters) Merge(other *AccountRegisters) error {
267+
for key, value := range other.registers {
268+
_, ok := a.registers[key]
269+
if ok {
270+
return fmt.Errorf("key already exists: %s", key)
271+
}
272+
a.registers[key] = value
273+
}
274+
return nil
275+
}
276+
266277
func NewAccountRegistersFromPayloads(owner string, payloads []*ledger.Payload) (*AccountRegisters, error) {
267278
accountRegisters := NewAccountRegisters(owner)
268279

0 commit comments

Comments
 (0)
Please sign in to comment.