Skip to content

Commit

Permalink
Add encode/decode keystone test
Browse files Browse the repository at this point in the history
  • Loading branch information
marcopeereboom committed Jan 30, 2025
1 parent c027c32 commit be3175a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
41 changes: 41 additions & 0 deletions database/tbcd/level/encodedecode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package level

import (
"reflect"
"testing"

btcchainhash "github.com/btcsuite/btcd/chaincfg/chainhash"

"github.com/hemilabs/heminetwork/database/tbcd"
"github.com/hemilabs/heminetwork/hemi"
)

func fillOutBytes(prefix string, size int) []byte {
result := []byte(prefix)
for len(result) < size {
result = append(result, '_')
}
return result
}

func TestKeystoneEncodeDecode(t *testing.T) {
hks := hemi.L2Keystone{
Version: 1,
L1BlockNumber: 5,
L2BlockNumber: 44,
ParentEPHash: fillOutBytes("v1parentephash", 32),
PrevKeystoneEPHash: fillOutBytes("v1prevkeystoneephash", 32),
StateRoot: fillOutBytes("v1stateroot", 32),
EPHash: fillOutBytes("v1ephash", 32),
}
abrvKs := hemi.L2KeystoneAbbreviate(hks).Serialize()
ks := tbcd.Keystone{
BlockHash: btcchainhash.Hash(fillOutBytes("blockhash", 32)),
AbbreviatedKeystone: abrvKs,
}
eks := encodeKeystone(ks)
nks := decodeKeystone(eks[:])
if !reflect.DeepEqual(nks, ks) {
t.Fatal("decoded keystone not equal")
}
}
1 change: 0 additions & 1 deletion database/tbcd/level/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -1778,7 +1778,6 @@ func (l *ldb) BlockKeystoneUpdate(ctx context.Context, direction int, keystones
// Only delete keystone if it is in the previously found block.
if ks.BlockHash.IsEqual(&v.BlockHash) {
kssBatch.Delete(k[:])
} else {
}
case 1:
has, err := kssTx.Has(k[:], nil)
Expand Down
13 changes: 8 additions & 5 deletions database/tbcd/level/level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/go-test/deep"

btcchainhash "github.com/btcsuite/btcd/chaincfg/chainhash"

"github.com/hemilabs/heminetwork/database"
"github.com/hemilabs/heminetwork/database/tbcd"
"github.com/hemilabs/heminetwork/database/tbcd/level"
Expand Down Expand Up @@ -132,7 +133,7 @@ func makeKssMap(kssList []hemi.L2Keystone) map[chainhash.Hash]tbcd.Keystone {
abrvKs := hemi.L2KeystoneAbbreviate(l2Keystone).Serialize()
kssMap[*hemi.L2KeystoneAbbreviate(l2Keystone).Hash()] = tbcd.Keystone{
BlockHash: btcchainhash.Hash(fillOutBytes("blockhash", 32)),
AbbreviatedKeystone: abrvKs[:],
AbbreviatedKeystone: abrvKs,
}
}
return kssMap
Expand All @@ -151,7 +152,8 @@ func TestKeystoneUpdate(t *testing.T) {
PrevKeystoneEPHash: fillOutBytes("v1prevkeystoneephash", 32),
StateRoot: fillOutBytes("v1stateroot", 32),
EPHash: fillOutBytes("v1ephash", 32),
}, {
},
{
Version: 1,
L1BlockNumber: 6,
L2BlockNumber: 44,
Expand All @@ -168,15 +170,17 @@ func TestKeystoneUpdate(t *testing.T) {
PrevKeystoneEPHash: fillOutBytes("i1prevkeystoneephash", 32),
StateRoot: fillOutBytes("i1stateroot", 32),
EPHash: fillOutBytes("i1ephash", 32),
}, {
},
{
Version: 1,
L1BlockNumber: 6,
L2BlockNumber: 44,
ParentEPHash: fillOutBytes("i2parentephash", 32),
PrevKeystoneEPHash: fillOutBytes("i2prevkeystoneephash", 32),
StateRoot: fillOutBytes("i2stateroot", 32),
EPHash: fillOutBytes("i2ephash", 32),
}}
},
}

type testTableItem struct {
name string
Expand Down Expand Up @@ -254,7 +258,6 @@ func TestKeystoneUpdate(t *testing.T) {

for _, tti := range testTable {
t.Run(tti.name, func(t *testing.T) {

home := t.TempDir()
t.Logf("temp: %v", home)

Expand Down

0 comments on commit be3175a

Please sign in to comment.