Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
55 changes: 55 additions & 0 deletions database/datum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2025 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package database

import (
"github.com/blinklabs-io/dingo/database/plugin/metadata/sqlite/models"
lcommon "github.com/blinklabs-io/gouroboros/ledger/common"
)

// GetDatum returns the datum for a given Blake2b256 hash
func (d *Database) GetDatumByHash(
hash lcommon.Blake2b256,
txn *Txn,
) ([]byte, error) {
var datum models.Datum
var err error

if txn == nil {
datum, err = d.metadata.GetDatum(hash, nil)
} else {
datum, err = d.metadata.GetDatum(hash, txn.Metadata())
}
if err != nil {
return nil, err
}
return datum.RawDatum, nil
}

// SetDatum saves the raw datum into the database by computing the hash before inserting.
func (d *Database) SetDatum(
rawDatum []byte,
addedSlot uint64,
txn *Txn,
) error {
// Compute Blake2b-256 hash
datumHash := lcommon.Blake2b256Hash(rawDatum)

if txn == nil {
return d.metadata.SetDatum(datumHash, rawDatum, addedSlot, nil)
} else {
return d.metadata.SetDatum(datumHash, rawDatum, addedSlot, txn.Metadata())
}
}
69 changes: 69 additions & 0 deletions database/plugin/metadata/sqlite/datum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2025 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sqlite

import (
"errors"

"github.com/blinklabs-io/dingo/database/plugin/metadata/sqlite/models"
lcommon "github.com/blinklabs-io/gouroboros/ledger/common"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

// GetDatum returns a datum by its hash
func (d *MetadataStoreSqlite) GetDatum(
hash lcommon.Blake2b256,
txn *gorm.DB,
) (models.Datum, error) {
var datum models.Datum
var result *gorm.DB
if txn != nil {
result = txn.Where("hash = ?", hash[:]).First(&datum)
} else {
result = d.DB().Where("hash = ?", hash[:]).First(&datum)
}
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return models.Datum{}, nil
}
return models.Datum{}, result.Error
}
return datum, nil
}

// SetDatum saves a datum into the database, or updates it if it already exists
func (d *MetadataStoreSqlite) SetDatum(
hash lcommon.Blake2b256,
rawDatum []byte,
addedSlot uint64,
txn *gorm.DB,
) error {
tmpItem := models.Datum{
Hash: hash[:],
RawDatum: rawDatum,
AddedSlot: addedSlot,
}
if txn != nil {
if result := txn.Clauses(clause.OnConflict{UpdateAll: true}).Create(&tmpItem); result.Error != nil {
return result.Error
}
} else {
if result := d.DB().Clauses(clause.OnConflict{UpdateAll: true}).Create(&tmpItem); result.Error != nil {
return result.Error
}
}
return nil
}
26 changes: 26 additions & 0 deletions database/plugin/metadata/sqlite/models/datum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2025 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package models

type Datum struct {
ID uint `gorm:"primarykey"`
Hash []byte `gorm:"index;not null;unique"`
RawDatum []byte `gorm:"not null"`
AddedSlot uint64 `gorm:"not null"`
}

func (Datum) TableName() string {
return "datum"
}
1 change: 1 addition & 0 deletions database/plugin/metadata/sqlite/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package models
var MigrateModels = []any{
&Account{},
&AuthCommitteeHot{},
&Datum{},
&Deregistration{},
&DeregistrationDrep{},
&Drep{},
Expand Down
10 changes: 10 additions & 0 deletions database/plugin/metadata/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ type MetadataStore interface {
[]byte, // stakeKey
*gorm.DB,
) (models.Account, error)
GetDatum(
lcommon.Blake2b256,
*gorm.DB,
) (models.Datum, error)
GetPParams(
uint64, // epoch
*gorm.DB,
Expand All @@ -75,6 +79,12 @@ type MetadataStore interface {
bool, // active
*gorm.DB,
) error
SetDatum(
lcommon.Blake2b256,
[]byte,
uint64, // slot
*gorm.DB,
) error
SetDeregistration(
*lcommon.DeregistrationCertificate,
uint64, // slot
Expand Down
Loading