Skip to content

Commit d0d3278

Browse files
committed
feat(ledger): leios ledger primitives
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent bfd0554 commit d0d3278

File tree

15 files changed

+520
-12
lines changed

15 files changed

+520
-12
lines changed

ledger/allegra/allegra.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,11 @@ func (AllegraTransaction) Type() int {
257257
}
258258

259259
func (t AllegraTransaction) Hash() common.Blake2b256 {
260-
return t.Body.Hash()
260+
return t.Id()
261+
}
262+
263+
func (t AllegraTransaction) Id() common.Blake2b256 {
264+
return t.Body.Id()
261265
}
262266

263267
func (t AllegraTransaction) Inputs() []common.TransactionInput {

ledger/alonzo/alonzo.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,11 @@ func (AlonzoTransaction) Type() int {
609609
}
610610

611611
func (t AlonzoTransaction) Hash() common.Blake2b256 {
612-
return t.Body.Hash()
612+
return t.Id()
613+
}
614+
615+
func (t AlonzoTransaction) Id() common.Blake2b256 {
616+
return t.Body.Id()
613617
}
614618

615619
func (t AlonzoTransaction) Inputs() []common.TransactionInput {

ledger/babbage/babbage.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ type BabbageTransactionPparamUpdate struct {
236236

237237
type BabbageTransactionBody struct {
238238
common.TransactionBodyBase
239+
hash *common.Blake2b256
239240
TxInputs shelley.ShelleyTransactionInputSet `cbor:"0,keyasint,omitempty"`
240241
TxOutputs []BabbageTransactionOutput `cbor:"1,keyasint,omitempty"`
241242
TxFee uint64 `cbor:"2,keyasint,omitempty"`
@@ -266,6 +267,14 @@ func (b *BabbageTransactionBody) UnmarshalCBOR(cborData []byte) error {
266267
return nil
267268
}
268269

270+
func (b *BabbageTransactionBody) Id() common.Blake2b256 {
271+
if b.hash == nil {
272+
tmpHash := common.Blake2b256Hash(b.Cbor())
273+
b.hash = &tmpHash
274+
}
275+
return *b.hash
276+
}
277+
269278
func (b *BabbageTransactionBody) Inputs() []common.TransactionInput {
270279
ret := []common.TransactionInput{}
271280
for _, input := range b.TxInputs.Items() {
@@ -742,7 +751,11 @@ func (BabbageTransaction) Type() int {
742751
}
743752

744753
func (t BabbageTransaction) Hash() common.Blake2b256 {
745-
return t.Body.Hash()
754+
return t.Id()
755+
}
756+
757+
func (t BabbageTransaction) Id() common.Blake2b256 {
758+
return t.Body.Id()
746759
}
747760

748761
func (t BabbageTransaction) Inputs() []common.TransactionInput {

ledger/block.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ func NewBlockFromCbor(blockType uint, data []byte) (Block, error) {
4444
return NewBabbageBlockFromCbor(data)
4545
case BlockTypeConway:
4646
return NewConwayBlockFromCbor(data)
47+
case BlockTypeLeiosEndorser:
48+
return NewLeiosEndorserBlockFromCbor(data)
49+
case BlockTypeLeiosRanking:
50+
return NewLeiosRankingBlockFromCbor(data)
4751
}
4852
return nil, fmt.Errorf("unknown node-to-client block type: %d", blockType)
4953
}
@@ -66,6 +70,8 @@ func NewBlockHeaderFromCbor(blockType uint, data []byte) (BlockHeader, error) {
6670
return NewBabbageBlockHeaderFromCbor(data)
6771
case BlockTypeConway:
6872
return NewConwayBlockHeaderFromCbor(data)
73+
case BlockTypeLeiosRanking:
74+
return NewLeiosBlockHeaderFromCbor(data)
6975
default:
7076
return nil, fmt.Errorf("unknown node-to-node block type: %d", blockType)
7177
}

ledger/byron/byron.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ func (ByronTransaction) Type() int {
161161
}
162162

163163
func (t *ByronTransaction) Hash() common.Blake2b256 {
164+
return t.Id()
165+
}
166+
167+
func (t *ByronTransaction) Id() common.Blake2b256 {
164168
if t.hash == nil {
165169
tmpHash := common.Blake2b256Hash(t.Cbor())
166170
t.hash = &tmpHash
@@ -289,7 +293,7 @@ func (t *ByronTransaction) Produced() []common.Utxo {
289293
ret = append(
290294
ret,
291295
common.Utxo{
292-
Id: NewByronTransactionInput(t.Hash().String(), idx),
296+
Id: NewByronTransactionInput(t.Id().String(), idx),
293297
Output: output,
294298
},
295299
)

ledger/common/certs.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const (
4646
CertificateTypeRegistrationDrep = 16
4747
CertificateTypeDeregistrationDrep = 17
4848
CertificateTypeUpdateDrep = 18
49+
CertificateTypeLeiosEb = 19
4950
)
5051

5152
type CertificateWrapper struct {
@@ -99,6 +100,8 @@ func (c *CertificateWrapper) UnmarshalCBOR(data []byte) error {
99100
tmpCert = &DeregistrationDrepCertificate{}
100101
case CertificateTypeUpdateDrep:
101102
tmpCert = &UpdateDrepCertificate{}
103+
case CertificateTypeLeiosEb:
104+
tmpCert = &LeiosEbCertificate{}
102105
default:
103106
return fmt.Errorf("unknown certificate type: %d", certType)
104107
}
@@ -1361,3 +1364,62 @@ func (c *UpdateDrepCertificate) Utxorpc() (*utxorpc.Certificate, error) {
13611364
func (c *UpdateDrepCertificate) Type() uint {
13621365
return c.CertType
13631366
}
1367+
1368+
// DRep implementation
1369+
func (d *Drep) Utxorpc() (*utxorpc.DRep, error) {
1370+
switch d.Type {
1371+
case DrepTypeAddrKeyHash:
1372+
return &utxorpc.DRep{
1373+
Drep: &utxorpc.DRep_AddrKeyHash{AddrKeyHash: d.Credential},
1374+
}, nil
1375+
case DrepTypeScriptHash:
1376+
return &utxorpc.DRep{
1377+
Drep: &utxorpc.DRep_ScriptHash{ScriptHash: d.Credential},
1378+
}, nil
1379+
case DrepTypeAbstain:
1380+
return &utxorpc.DRep{
1381+
Drep: &utxorpc.DRep_Abstain{Abstain: true},
1382+
}, nil
1383+
case DrepTypeNoConfidence:
1384+
return &utxorpc.DRep{
1385+
Drep: &utxorpc.DRep_NoConfidence{NoConfidence: true},
1386+
}, nil
1387+
default:
1388+
return nil, fmt.Errorf("unknown DRep type: %d", d.Type)
1389+
}
1390+
}
1391+
1392+
type LeiosEbCertificate struct {
1393+
cbor.StructAsArray
1394+
cbor.DecodeStoreCbor
1395+
CertType uint
1396+
ElectionId Blake2b256
1397+
EndorserBlockHash Blake2b256
1398+
PersistentVoters []any
1399+
NonpersistentVoters map[Blake2b256]any
1400+
AggregateEligSig *any
1401+
AggregateVoteSig any
1402+
}
1403+
1404+
func (c LeiosEbCertificate) isCertificate() {}
1405+
1406+
func (c *LeiosEbCertificate) UnmarshalCBOR(
1407+
cborData []byte,
1408+
) error {
1409+
type tLeiosEbCertificate LeiosEbCertificate
1410+
var tmp tLeiosEbCertificate
1411+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
1412+
return err
1413+
}
1414+
*c = LeiosEbCertificate(tmp)
1415+
c.SetCbor(cborData)
1416+
return nil
1417+
}
1418+
1419+
func (c *LeiosEbCertificate) Utxorpc() (*utxorpc.Certificate, error) {
1420+
return &utxorpc.Certificate{}, nil
1421+
}
1422+
1423+
func (c *LeiosEbCertificate) Type() uint {
1424+
return c.CertType
1425+
}

ledger/common/tx.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type Transaction interface {
2626
TransactionBody
2727
Type() int
2828
Cbor() []byte
29+
Hash() Blake2b256
2930
Metadata() *cbor.LazyValue
3031
IsValid() bool
3132
Consumed() []TransactionInput
@@ -36,7 +37,7 @@ type Transaction interface {
3637
type TransactionBody interface {
3738
Cbor() []byte
3839
Fee() uint64
39-
Hash() Blake2b256
40+
Id() Blake2b256
4041
Inputs() []TransactionInput
4142
Outputs() []TransactionOutput
4243
TTL() uint64
@@ -97,6 +98,14 @@ type TransactionWitnessRedeemers interface {
9798
Iter() iter.Seq2[RedeemerKey, RedeemerValue]
9899
}
99100

101+
// TxReference provides a reference to a transaction which includes a hash of the full transaction
102+
// body bytes and the total transaction size in bytes
103+
type TxReference struct {
104+
cbor.StructAsArray
105+
TxHash Blake2b256
106+
TxSize uint16
107+
}
108+
100109
type Utxo struct {
101110
cbor.StructAsArray
102111
Id TransactionInput
@@ -111,7 +120,7 @@ type TransactionBodyBase struct {
111120
hash *Blake2b256
112121
}
113122

114-
func (b *TransactionBodyBase) Hash() Blake2b256 {
123+
func (b *TransactionBodyBase) Id() Blake2b256 {
115124
if b.hash == nil {
116125
tmpHash := Blake2b256Hash(b.Cbor())
117126
b.hash = &tmpHash
@@ -230,7 +239,7 @@ func TransactionBodyToUtxorpc(tx TransactionBody) (*utxorpc.Tx, error) {
230239
// Validity: tx.Validity(),
231240
// Successful: tx.Successful(),
232241
// Auxiliary: tx.AuxData(),
233-
Hash: tx.Hash().Bytes(),
242+
Hash: tx.Id().Bytes(),
234243
// Proposals: tx.ProposalProcedures(),
235244
}
236245
for _, ri := range tx.ReferenceInputs() {

ledger/conway/conway.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,11 @@ func (ConwayTransaction) Type() int {
536536
}
537537

538538
func (t ConwayTransaction) Hash() common.Blake2b256 {
539-
return t.Body.Hash()
539+
return t.Id()
540+
}
541+
542+
func (t ConwayTransaction) Id() common.Blake2b256 {
543+
return t.Body.Id()
540544
}
541545

542546
func (t ConwayTransaction) Inputs() []common.TransactionInput {

ledger/leios.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 ledger
16+
17+
import "github.com/blinklabs-io/gouroboros/ledger/leios"
18+
19+
// The below are compatibility types, constants, and functions for the Leios era
20+
// to keep existing code working after a refactor of the ledger package
21+
22+
// Leios types
23+
type (
24+
LeiosBlockHeader = leios.LeiosBlockHeader
25+
LeiosEndorderBlock = leios.LeiosEndorserBlock
26+
LeiosRankingBlock = leios.LeiosRankingBlock
27+
LeiosTransaction = leios.LeiosTransaction
28+
LeiosTransactionBody = leios.LeiosTransactionBody
29+
LeiosTransactionWitnessSet = leios.LeiosTransactionWitnessSet
30+
LeiosProtocolParameters = leios.LeiosProtocolParameters
31+
LeiosProtocolParameterUpdate = leios.LeiosProtocolParameterUpdate
32+
)
33+
34+
// Leios constants
35+
const (
36+
EraIdLeios = leios.EraIdLeios
37+
BlockTypeLeiosRanking = leios.BlockTypeLeiosRanking
38+
BlockTypeLeiosEndorser = leios.BlockTypeLeiosEndorser
39+
BlockHeaderTypeLeios = leios.BlockHeaderTypeLeios
40+
TxTypeLeios = leios.TxTypeLeios
41+
)
42+
43+
// Leios functions
44+
var (
45+
NewLeiosEndorserBlockFromCbor = leios.NewLeiosEndorserBlockFromCbor
46+
NewLeiosRankingBlockFromCbor = leios.NewLeiosRankingBlockFromCbor
47+
NewLeiosBlockHeaderFromCbor = leios.NewLeiosBlockHeaderFromCbor
48+
NewLeiosTransactionFromCbor = leios.NewLeiosTransactionFromCbor
49+
NewLeiosTransactionBodyFromCbor = leios.NewLeiosTransactionBodyFromCbor
50+
)

0 commit comments

Comments
 (0)