Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
9a8ba84
Add v4 keystone height hash index
marcopeereboom Jun 10, 2025
93c50e9
decode all keystones
marcopeereboom Jun 10, 2025
6725cbd
Decode the various databases
marcopeereboom Jun 10, 2025
33b0add
encode height to keystone hash
marcopeereboom Jun 10, 2025
cf9ff92
Use abbreviated hash
marcopeereboom Jun 10, 2025
7b14ab6
correct spot
marcopeereboom Jun 10, 2025
19f441c
64 entire bits
marcopeereboom Jun 10, 2025
9f8e579
test this shit real quick
marcopeereboom Jun 10, 2025
33d1de3
remove test code and put results in database
marcopeereboom Jun 10, 2025
93b7a33
Let's start putting this together
marcopeereboom Jun 10, 2025
d0b1651
Show antonio
marcopeereboom Jun 11, 2025
bf14da2
remove fixed XXX
marcopeereboom Jun 11, 2025
f6c6d6f
return height as well with a shortcut
marcopeereboom Jun 11, 2025
efb37e6
add height hash index encoding test
AL-CT Jun 11, 2025
953d45a
fix block missing height error and linter
AL-CT Jun 11, 2025
f73790a
Do we want this antonio?
marcopeereboom Jun 11, 2025
2b36211
oops remove panic
marcopeereboom Jun 11, 2025
73c165d
Here is a better version antonio
marcopeereboom Jun 11, 2025
9a9b734
oops, fix error
marcopeereboom Jun 11, 2025
1d09dc4
minor test fixes for db upgrade
AL-CT Jun 11, 2025
3ba098e
add keystone by height and tests
AL-CT Jun 11, 2025
bcb0fc2
Don't use CloneBytes
marcopeereboom Jun 12, 2025
430fa1b
Cleanup
marcopeereboom Jun 12, 2025
211f1fb
verify height and use found height
marcopeereboom Jun 12, 2025
b236647
Attempt at depth
marcopeereboom Jun 12, 2025
1a2f959
fix height seeking on keystone by hash
AL-CT Jun 12, 2025
8cd2b60
off by one fix and tests
AL-CT Jun 12, 2025
def77d6
add keystonesbyheight to hemictl
AL-CT Jun 12, 2025
ac36984
add keystonesbyheight rpc call
AL-CT Jun 12, 2025
c467c79
try to please codeql for all the unnecessary reasons
marcopeereboom Jun 13, 2025
eaa20aa
Just stop being clever to save 4 bytes per keystone
marcopeereboom Jun 13, 2025
adc9c70
fix upgrade and add testing
AL-CT Jun 13, 2025
e3bff87
Revert "fix upgrade and add testing"
AL-CT Jun 16, 2025
5acc2e6
fix v4 upgrade
AL-CT Jun 16, 2025
6acc0fb
add v4 upgrade test and new v3 test database
AL-CT Jun 16, 2025
668434f
Cleanup a little
marcopeereboom Jun 17, 2025
5cf28d2
fix keystones by height
AL-CT Jun 17, 2025
825ac2c
add extra test
AL-CT Jun 19, 2025
5e9552b
forgot one
AL-CT Jun 19, 2025
4e9684f
fix comments and suggestions
AL-CT Jun 19, 2025
b7ce764
cleanup
AL-CT Jun 24, 2025
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
Prev Previous commit
Next Next commit
add extra test
  • Loading branch information
AL-CT committed Jun 24, 2025
commit 825ac2c512d0823b5502da37d0b1cb652edef79c
6 changes: 5 additions & 1 deletion database/tbcd/level/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -2045,13 +2045,17 @@ func (l *ldb) KeystonesByHeight(ctx context.Context, height uint32, depth int) (
deks := decodeKeystone(eks)
kssList = append(kssList, deks)
}
if i.Error() != nil {
return nil, fmt.Errorf("keystones iterator: %w", i.Error())
}

if len(kssList) == 0 {
return nil, database.NotFoundError(fmt.Sprintf("no first occurrence "+
"keystones range: %v < %v",
min(start, end), max(start, end)))
}

return kssList, i.Error()
return kssList, nil
}

func (l *ldb) BlockKeystoneUpdate(ctx context.Context, direction int, keystones map[chainhash.Hash]tbcd.Keystone, keystoneIndexHash chainhash.Hash) error {
Expand Down
118 changes: 118 additions & 0 deletions database/tbcd/level/level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/go-test/deep"

"github.com/hemilabs/heminetwork/database"
"github.com/hemilabs/heminetwork/database/level"
"github.com/hemilabs/heminetwork/database/tbcd"
"github.com/hemilabs/heminetwork/hemi"
"github.com/hemilabs/heminetwork/testutil"
Expand Down Expand Up @@ -720,3 +721,120 @@ func TestHeightHashEncoding(t *testing.T) {
t.Fatalf("decoded hash != kss hash (%v != %v)", uhash, hash)
}
}

func TestDbUpgradeV4Errors(t *testing.T) {
type testTableItem struct {
name string
key []byte
value []byte
pass bool
}

ks := hemi.L2Keystone{
Version: 1,
ParentEPHash: testutil.FillBytes("v1parentephash", 32),
PrevKeystoneEPHash: testutil.FillBytes("v1prevkeystoneephash", 32),
StateRoot: testutil.FillBytes("v1stateroot", 32),
EPHash: testutil.FillBytes("v1ephash", 32),
}
abrevKss := hemi.L2KeystoneAbbreviate(ks)

fakeHash, err := chainhash.NewHashFromStr("1000000050ff3053ada24e6ad581fa0295297f20a2747d034997ffc899aa931e")
if err != nil {
t.Fatal(err)
}
realHash, err := chainhash.NewHashFromStr("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943")
if err != nil {
t.Fatal(err)
}

invalidBlockKss := tbcd.Keystone{
BlockHash: *fakeHash,
BlockHeight: 10,
AbbreviatedKeystone: abrevKss.Serialize(),
}
validKss := tbcd.Keystone{
BlockHash: *realHash,
BlockHeight: 10,
AbbreviatedKeystone: abrevKss.Serialize(),
}

testTable := []testTableItem{
{
name: "invalid blockheader",
key: fakeHash[:],
value: encodeKeystoneToSliceV1(invalidBlockKss),
},
{
name: "invalid keystone hash",
key: realHash[:15],
value: encodeKeystoneToSliceV1(validKss),
},
{
name: "no errors",
pass: true,
},
}

for _, tti := range testTable {
t.Run(tti.name, func(t *testing.T) {
home := t.TempDir()
network := "upgradetest"
t.Logf("temp: %v", home)

ctx, cancel := context.WithCancel(t.Context())
defer func() {
cancel()
}()

cfg, err := NewConfig(network, home, "0mb", "0mb")
if err != nil {
t.Fatal(err)
}

cfg.SetUpgradeOpen(true)
dbTemp, err := New(ctx, cfg)
if err != nil {
t.Fatal(err)
}

if err := dbTemp.insertTable(level.KeystonesDB, tti.key, tti.value); err != nil {
panic(err)
}

// Write new version
v := make([]byte, 8)
binary.BigEndian.PutUint64(v, 1)
if err := dbTemp.MetadataPut(ctx, versionKey, v); err != nil {
t.Fatal(err)
}

if err := dbTemp.Close(); err != nil {
t.Fatal(err)
}

cfg.SetUpgradeOpen(false)
// upgrade
_, err = New(ctx, cfg)
if !tti.pass && err == nil {
t.Fatal("expected error")
}
if tti.pass && err != nil {
t.Fatal(err)
}

t.Log(err)
})
}
}

func encodeKeystoneV1(ks tbcd.Keystone) (eks [chainhash.HashSize + hemi.L2KeystoneAbrevSize]byte) {
copy(eks[0:32], ks.BlockHash[:])
copy(eks[32:], ks.AbbreviatedKeystone[:])
return
}

func encodeKeystoneToSliceV1(ks tbcd.Keystone) []byte {
eks := encodeKeystoneV1(ks)
return eks[:]
}
3 changes: 1 addition & 2 deletions database/tbcd/level/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,14 +555,13 @@ func (l *ldb) v3(ctx context.Context) error {
// v4 upgrade the database from v3 to v4.
// Changes:
// Add heighthash (L2 height - Keystone Hash) to keystone database.
// XXX antonio add test
func (l *ldb) v4(ctx context.Context) error {
log.Tracef("v4")
defer log.Tracef("v4 exit")

log.Infof("Upgrading database from v3 to v4")

// Index all keystones to M[height][hash] format.
// Index all keystones to H[height][hash] format.
bhs := l.pool[level.BlockHeadersDB]
ksdb := l.pool[level.KeystonesDB]
i := ksdb.NewIterator(&util.Range{Start: nil, Limit: nil}, nil)
Expand Down
Loading