Skip to content

Commit e7f83c1

Browse files
jkawanJenita
andauthored
feat: track witness set for transaction (#1027)
* feat: track witness set for transaction Signed-off-by: Jenita <jkawan@blinklabs.io> * feat: track witness set for transaction Signed-off-by: Jenita <jkawan@blinklabs.io> * feat: track witness set for transaction Signed-off-by: Jenita <jkawan@blinklabs.io> * feat: track witness set for transaction Signed-off-by: Jenita <jkawan@blinklabs.io> * feat: track witness set for transaction Signed-off-by: Jenita <jkawan@blinklabs.io> * feat: track witness set for transaction Signed-off-by: Jenita <jkawan@blinklabs.io> * feat: addressed review comments Signed-off-by: Jenita <jkawan@blinklabs.io> * ci: retrigger nilaway check Signed-off-by: Jenita <jkawan@blinklabs.io> * ci: retrigger nilaway check Signed-off-by: Jenita <jkawan@blinklabs.io> * feat: attempt to fix nilaway issue Signed-off-by: Jenita <jkawan@blinklabs.io> * feat: addressed review comments Signed-off-by: Jenita <jkawan@blinklabs.io> * feat: resolved merge conflict Signed-off-by: Jenita <jkawan@blinklabs.io> * feat: addressed review comments Signed-off-by: Jenita <jkawan@blinklabs.io> --------- Signed-off-by: Jenita <jkawan@blinklabs.io> Signed-off-by: jkawan <kawanjenita@outlook.com> Co-authored-by: Jenita <jkawan@blinklabs.io>
1 parent 4e2fcc3 commit e7f83c1

File tree

11 files changed

+344
-8
lines changed

11 files changed

+344
-8
lines changed

.github/workflows/nilaway.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
nilaway:
1515
name: nilaway
1616
runs-on: ubuntu-latest
17+
timeout-minutes: 10
1718
steps:
1819
- uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0 https://github.com/actions/checkout/releases/tag/v6.0.0
1920
- uses: actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6.1.0 https://github.com/actions/setup-go/releases/tag/v6.1.0

database/models/datum.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,15 @@ type Datum struct {
2424
func (Datum) TableName() string {
2525
return "datum"
2626
}
27+
28+
// PlutusData represents a Plutus data value in the witness set
29+
type PlutusData struct {
30+
ID uint `gorm:"primaryKey"`
31+
TransactionID uint `gorm:"index"`
32+
Data []byte
33+
Transaction *Transaction
34+
}
35+
36+
func (PlutusData) TableName() string {
37+
return "plutus_data"
38+
}

database/models/models.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var MigrateModels = []any{
2626
&DeregistrationDrep{},
2727
&Drep{},
2828
&Epoch{},
29+
&KeyWitness{},
2930
&MoveInstantaneousRewards{},
3031
&MoveInstantaneousRewardsReward{},
3132
&Pool{},
@@ -35,9 +36,12 @@ var MigrateModels = []any{
3536
&PoolRetirement{},
3637
&PParams{},
3738
&PParamUpdate{},
39+
&PlutusData{},
3840
&Registration{},
3941
&RegistrationDrep{},
42+
&Redeemer{},
4043
&ResignCommitteeCold{},
44+
&Script{},
4145
&StakeDelegation{},
4246
&StakeDeregistration{},
4347
&StakeRegistration{},
@@ -50,4 +54,5 @@ var MigrateModels = []any{
5054
&Utxo{},
5155
&VoteDelegation{},
5256
&VoteRegistrationDelegation{},
57+
&WitnessScripts{},
5358
}

database/models/redeemer.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2025 Blink Labs Software
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package models
16+
17+
// Redeemer represents a redeemer in the witness set
18+
type Redeemer struct {
19+
ID uint `gorm:"primaryKey"`
20+
TransactionID uint `gorm:"index"`
21+
Tag uint8 `gorm:"index"` // Redeemer tag
22+
Index uint32 `gorm:"index"`
23+
Data []byte // Plutus data
24+
ExUnitsMemory uint64
25+
ExUnitsCPU uint64
26+
}
27+
28+
func (Redeemer) TableName() string {
29+
return "redeemer"
30+
}

database/models/script.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2025 Blink Labs Software
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package models
16+
17+
// Script represents the content of a script, indexed by its hash
18+
// This avoids storing duplicate script data when the same script appears
19+
// in multiple transactions
20+
type Script struct {
21+
ID uint `gorm:"primaryKey"`
22+
Hash []byte `gorm:"index;unique"` // Script hash
23+
Type uint8 `gorm:"index"` // Script type
24+
Content []byte // Script content
25+
CreatedSlot uint64 // Slot when this script was first seen
26+
}
27+
28+
func (Script) TableName() string {
29+
return "script"
30+
}

database/models/transaction.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@ import "github.com/blinklabs-io/dingo/database/types"
1818

1919
// Transaction represents a transaction record
2020
type Transaction struct {
21-
Hash []byte `gorm:"uniqueIndex"`
22-
BlockHash []byte `gorm:"index"`
23-
Inputs []Utxo `gorm:"foreignKey:SpentAtTxId;references:Hash"`
24-
Outputs []Utxo `gorm:"foreignKey:TransactionID;references:ID"`
25-
ReferenceInputs []Utxo `gorm:"foreignKey:ReferencedByTxId;references:Hash"`
26-
Collateral []Utxo `gorm:"foreignKey:CollateralByTxId;references:Hash"`
27-
CollateralReturn *Utxo `gorm:"foreignKey:TransactionID;references:ID"`
28-
ID uint `gorm:"primaryKey"`
21+
Hash []byte `gorm:"uniqueIndex"`
22+
BlockHash []byte `gorm:"index"`
23+
Inputs []Utxo `gorm:"foreignKey:SpentAtTxId;references:Hash"`
24+
Outputs []Utxo `gorm:"foreignKey:TransactionID;references:ID"`
25+
ReferenceInputs []Utxo `gorm:"foreignKey:ReferencedByTxId;references:Hash"`
26+
Collateral []Utxo `gorm:"foreignKey:CollateralByTxId;references:Hash"`
27+
CollateralReturn *Utxo `gorm:"foreignKey:TransactionID;references:ID"`
28+
KeyWitnesses []KeyWitness `gorm:"foreignKey:TransactionID;references:ID"`
29+
WitnessScripts []WitnessScripts `gorm:"foreignKey:TransactionID;references:ID"`
30+
Redeemers []Redeemer `gorm:"foreignKey:TransactionID;references:ID"`
31+
PlutusData []PlutusData `gorm:"foreignKey:TransactionID;references:ID"`
32+
ID uint `gorm:"primaryKey"`
2933
Type int
3034
BlockIndex uint32
3135
Metadata []byte

database/models/witness.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2025 Blink Labs Software
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package models
16+
17+
const (
18+
// KeyWitnessTypeVkey represents a Vkey witness
19+
KeyWitnessTypeVkey uint8 = 0
20+
// KeyWitnessTypeBootstrap represents a Bootstrap witness
21+
KeyWitnessTypeBootstrap uint8 = 1
22+
)
23+
24+
// KeyWitness represents a key witness entry (Vkey or Bootstrap)
25+
// Type: KeyWitnessTypeVkey = VkeyWitness, KeyWitnessTypeBootstrap = BootstrapWitness
26+
type KeyWitness struct {
27+
ID uint `gorm:"primaryKey"`
28+
TransactionID uint `gorm:"index"`
29+
Type uint8 `gorm:"index"` // See KeyWitnessType* constants
30+
Vkey []byte // Vkey witness key
31+
Signature []byte // Witness signature
32+
PublicKey []byte // For Bootstrap witness
33+
ChainCode []byte // For Bootstrap witness
34+
Attributes []byte // For Bootstrap witness
35+
}
36+
37+
func (KeyWitness) TableName() string {
38+
return "key_witness"
39+
}

database/models/witness_script.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2025 Blink Labs Software
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package models
16+
17+
// WitnessScripts represents a reference to a script in the witness set
18+
// Type corresponds to ScriptRefType constants from gouroboros/ledger/common:
19+
// 0=NativeScript (ScriptRefTypeNativeScript)
20+
// 1=PlutusV1 (ScriptRefTypePlutusV1)
21+
// 2=PlutusV2 (ScriptRefTypePlutusV2)
22+
// 3=PlutusV3 (ScriptRefTypePlutusV3)
23+
//
24+
// To avoid storing duplicate script data for the same script used in multiple
25+
// transactions, we store only the script hash here. The actual script content
26+
// is stored separately in Script table, indexed by hash.
27+
type WitnessScripts struct {
28+
ID uint `gorm:"primaryKey"`
29+
TransactionID uint `gorm:"index"`
30+
Type uint8 `gorm:"index"` // Script type
31+
ScriptHash []byte `gorm:"index"` // Hash of the script
32+
}
33+
34+
func (WitnessScripts) TableName() string {
35+
return "witness_scripts"
36+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2025 Blink Labs Software
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package sqlite
16+
17+
import (
18+
"errors"
19+
20+
"github.com/blinklabs-io/dingo/database/models"
21+
lcommon "github.com/blinklabs-io/gouroboros/ledger/common"
22+
"gorm.io/gorm"
23+
)
24+
25+
// GetScript returns the script content by its hash
26+
func (d *MetadataStoreSqlite) GetScript(
27+
hash lcommon.ScriptHash,
28+
txn *gorm.DB,
29+
) (*models.Script, error) {
30+
ret := &models.Script{}
31+
if txn == nil {
32+
txn = d.DB()
33+
}
34+
result := txn.First(ret, "hash = ?", hash[:])
35+
if result.Error != nil {
36+
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
37+
return nil, nil
38+
}
39+
return nil, result.Error
40+
}
41+
return ret, nil
42+
}

0 commit comments

Comments
 (0)