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

commitment: use Update for domain IO #11445

Merged
merged 8 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
3,626 changes: 1,802 additions & 1,824 deletions erigon-lib/commitment/bin_patricia_hashed.go

Large diffs are not rendered by default.

524 changes: 262 additions & 262 deletions erigon-lib/commitment/bin_patricia_hashed_test.go

Large diffs are not rendered by default.

66 changes: 32 additions & 34 deletions erigon-lib/commitment/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ type PatriciaContext interface {
// For each cell, it sets the cell type, clears the modified flag, fills the hash,
// and for the extension, account, and leaf type, the `l` and `k`
GetBranch(prefix []byte) ([]byte, uint64, error)
// fetch account with given plain key
GetAccount(plainKey []byte, cell *Cell) error
// fetch storage with given plain key
GetStorage(plainKey []byte, cell *Cell) error
// store branch data
PutBranch(prefix []byte, data []byte, prevData []byte, prevStep uint64) error
// fetch account with given plain key
GetAccount(plainKey []byte) (*Update, error)
// fetch storage with given plain key
GetStorage(plainKey []byte) (*Update, error)
}

type TrieVariant string
Expand All @@ -94,10 +94,11 @@ const (
func InitializeTrieAndUpdates(tv TrieVariant, mode Mode, tmpdir string) (Trie, *Updates) {
switch tv {
case VariantBinPatriciaTrie:
trie := NewBinPatriciaHashed(length.Addr, nil, tmpdir)
fn := func(key []byte) []byte { return hexToBin(key) }
tree := NewUpdates(mode, tmpdir, fn)
return trie, tree
//trie := NewBinPatriciaHashed(length.Addr, nil, tmpdir)
//fn := func(key []byte) []byte { return hexToBin(key) }
//tree := NewUpdateTree(mode, tmpdir, fn)
//return trie, tree
panic("omg its not supported")
case VariantHexPatriciaTrie:
fallthrough
default:
Expand Down Expand Up @@ -157,8 +158,8 @@ func (branchData BranchData) String() string {
fmt.Fprintf(&sb, "%sstoragePlainKey=[%x]", comma, cell.storagePlainKey[:cell.storagePlainKeyLen])
comma = ","
}
if cell.HashLen > 0 {
fmt.Fprintf(&sb, "%shash=[%x]", comma, cell.hash[:cell.HashLen])
if cell.hashLen > 0 {
fmt.Fprintf(&sb, "%shash=[%x]", comma, cell.hash[:cell.hashLen])
}
sb.WriteString("}\n")
}
Expand Down Expand Up @@ -311,7 +312,7 @@ func (be *BranchEncoder) EncodeBranch(bitmap, touchMap, afterMap uint16, readCel
if cell.storagePlainKeyLen > 0 {
fieldBits |= StoragePlainPart
}
if cell.HashLen > 0 {
if cell.hashLen > 0 {
fieldBits |= HashPart
}
if err := be.buf.WriteByte(byte(fieldBits)); err != nil {
Expand All @@ -333,7 +334,7 @@ func (be *BranchEncoder) EncodeBranch(bitmap, touchMap, afterMap uint16, readCel
}
}
if fieldBits&HashPart != 0 {
if err := putUvarAndVal(uint64(cell.HashLen), cell.hash[:cell.HashLen]); err != nil {
if err := putUvarAndVal(uint64(cell.hashLen), cell.hash[:cell.hashLen]); err != nil {
return nil, 0, err
}
}
Expand Down Expand Up @@ -768,8 +769,8 @@ func DecodeBranchAndCollectStat(key, branch []byte, tv TrieVariant) *BranchStat
case c.storagePlainKeyLen > 0:
stat.SPKSize += uint64(c.storagePlainKeyLen)
stat.SPKCount++
case c.HashLen > 0:
stat.HashSize += uint64(c.HashLen)
case c.hashLen > 0:
stat.HashSize += uint64(c.hashLen)
stat.HashCount++
default:
panic("no plain key" + fmt.Sprintf("#+%v", c))
Expand Down Expand Up @@ -882,7 +883,7 @@ func (t *Updates) initCollector() {
func (t *Updates) TouchPlainKey(key, val []byte, fn func(c *KeyUpdate, val []byte)) {
switch t.mode {
case ModeUpdate:
pivot, updated := &KeyUpdate{plainKey: key}, false
pivot, updated := &KeyUpdate{plainKey: key, update: new(Update)}, false

t.tree.DescendLessOrEqual(pivot, func(item *KeyUpdate) bool {
if bytes.Equal(item.plainKey, pivot.plainKey) {
Expand Down Expand Up @@ -925,41 +926,38 @@ func (t *Updates) TouchAccount(c *KeyUpdate, val []byte) {
c.update.Balance.Set(balance)
c.update.Flags |= BalanceUpdate
}
if !bytes.Equal(chash, c.update.CodeHashOrStorage[:]) {
if !bytes.Equal(chash, c.update.CodeHash[:]) {
if len(chash) == 0 {
c.update.ValLength = length.Hash
copy(c.update.CodeHashOrStorage[:], EmptyCodeHash)
copy(c.update.CodeHash[:], EmptyCodeHash)
} else {
copy(c.update.CodeHashOrStorage[:], chash)
c.update.ValLength = length.Hash
c.update.Flags |= CodeUpdate
copy(c.update.CodeHash[:], chash)
}
}
}

func (t *Updates) TouchStorage(c *KeyUpdate, val []byte) {
c.update.ValLength = len(val)
c.update.StorageLen = len(val)
if len(val) == 0 {
c.update.Flags = DeleteUpdate
} else {
c.update.Flags |= StorageUpdate
copy(c.update.CodeHashOrStorage[:], val)
copy(c.update.Storage[:], val)
}
}

func (t *Updates) TouchCode(c *KeyUpdate, val []byte) {
t.keccak.Reset()
t.keccak.Write(val)
t.keccak.Read(c.update.CodeHashOrStorage[:])
if c.update.Flags == DeleteUpdate && len(val) == 0 {
c.update.Flags = DeleteUpdate
c.update.ValLength = 0
c.update.Flags |= CodeUpdate
if len(val) == 0 {
if c.update.Flags == 0 || c.update.Flags == DeleteUpdate {
c.update.Flags = DeleteUpdate
}
copy(c.update.CodeHash[:], EmptyCodeHash)
return
}
c.update.ValLength = length.Hash
if len(val) != 0 {
c.update.Flags |= CodeUpdate
}
t.keccak.Reset()
t.keccak.Write(val)
t.keccak.Read(c.update.CodeHash[:])
}

func (t *Updates) Close() {
Expand Down Expand Up @@ -1029,7 +1027,7 @@ func (t *Updates) List(clear bool) ([][]byte, []Update) {
updates := make([]Update, t.tree.Len())
i := 0
t.tree.Ascend(func(item *KeyUpdate) bool {
plainKeys[i], updates[i] = item.plainKey, item.update
plainKeys[i], updates[i] = item.plainKey, *item.update
i++
return true
})
Expand All @@ -1044,7 +1042,7 @@ func (t *Updates) List(clear bool) ([][]byte, []Update) {

type KeyUpdate struct {
plainKey []byte
update Update
update *Update
}

func keyUpdateLessFn(i, j *KeyUpdate) bool {
Expand Down
6 changes: 3 additions & 3 deletions erigon-lib/commitment/commitment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ func generateCellRow(tb testing.TB, size int) (row []*Cell, bitmap uint16) {
var bm uint16
for i := 0; i < len(row); i++ {
row[i] = new(Cell)
row[i].HashLen = 32
row[i].hashLen = 32
n, err := rand.Read(row[i].hash[:])
require.NoError(tb, err)
require.EqualValues(tb, row[i].HashLen, n)
require.EqualValues(tb, row[i].hashLen, n)

th := rand.Intn(120)
switch {
Expand Down Expand Up @@ -373,7 +373,7 @@ func TestUpdates_TouchPlainKey(t *testing.T) {

for i := 0; i < len(sortedUniqUpds); i++ {
require.EqualValues(t, sortedUniqUpds[i].key, pk[i])
require.EqualValues(t, sortedUniqUpds[i].val, upd[i].CodeHashOrStorage[:upd[i].ValLength])
require.EqualValues(t, sortedUniqUpds[i].val, upd[i].Storage[:upd[i].StorageLen])
}

pk, upd = utDirect.List(true)
Expand Down
Loading
Loading