Skip to content

Commit ebec68f

Browse files
authored
Merge pull request #93 from cloudstruct/feature/babbage-era
feat: babbage era support
2 parents bf11c22 + 8acb460 commit ebec68f

File tree

5 files changed

+126
-3
lines changed

5 files changed

+126
-3
lines changed

block/babbage.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package block
2+
3+
import (
4+
"github.com/fxamacker/cbor/v2"
5+
)
6+
7+
const (
8+
BLOCK_TYPE_BABBAGE = 6
9+
10+
BLOCK_HEADER_TYPE_BABBAGE = 5
11+
12+
TX_TYPE_BABBAGE = 5
13+
)
14+
15+
type BabbageBlock struct {
16+
// Tells the CBOR decoder to convert to/from a struct and a CBOR array
17+
_ struct{} `cbor:",toarray"`
18+
Header BabbageBlockHeader
19+
TransactionBodies []BabbageTransaction
20+
TransactionWitnessSets []AlonzoTransactionWitnessSet
21+
// TODO: figure out how to parse properly
22+
// We use RawMessage here because the content is arbitrary and can contain data that
23+
// cannot easily be represented in Go (such as maps with bytestring keys)
24+
TransactionMetadataSet map[uint]cbor.RawMessage
25+
InvalidTransactions []uint
26+
}
27+
28+
func (b *BabbageBlock) Id() string {
29+
return b.Header.Id()
30+
}
31+
32+
type BabbageBlockHeader struct {
33+
// Tells the CBOR decoder to convert to/from a struct and a CBOR array
34+
_ struct{} `cbor:",toarray"`
35+
id string
36+
Body struct {
37+
// Tells the CBOR decoder to convert to/from a struct and a CBOR array
38+
_ struct{} `cbor:",toarray"`
39+
BlockNumber uint64
40+
Slot uint64
41+
PrevHash Blake2b256
42+
IssuerVkey interface{}
43+
VrfKey interface{}
44+
VrfResult interface{}
45+
BlockBodySize uint32
46+
BlockBodyHash Blake2b256
47+
OpCert struct {
48+
// Tells the CBOR decoder to convert to/from a struct and a CBOR array
49+
_ struct{} `cbor:",toarray"`
50+
HotVkey interface{}
51+
SequenceNumber uint32
52+
KesPeriod uint32
53+
Signature interface{}
54+
}
55+
ProtoVersion struct {
56+
// Tells the CBOR decoder to convert to/from a struct and a CBOR array
57+
_ struct{} `cbor:",toarray"`
58+
Major uint64
59+
Minor uint64
60+
}
61+
}
62+
Signature interface{}
63+
}
64+
65+
func (h *BabbageBlockHeader) Id() string {
66+
return h.id
67+
}
68+
69+
type BabbageTransaction struct {
70+
AlonzoTransaction
71+
CollateralReturn ShelleyTransactionOutput `cbor:"16,keyasint,omitempty"`
72+
TotalCollateral uint64 `cbor:"17,keyasint,omitempty"`
73+
ReferenceInputs []ShelleyTransactionInput `cbor:"18,keyasint,omitempty"`
74+
}

block/common.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,15 @@ func NewBlockFromCbor(blockType uint, data []byte) (interface{}, error) {
7575
}
7676
alonzoBlock.Header.id, err = generateBlockHeaderHash(rawBlock[0], nil)
7777
return &alonzoBlock, err
78+
case BLOCK_TYPE_BABBAGE:
79+
var babbageBlock BabbageBlock
80+
if err := cbor.Unmarshal(data, &babbageBlock); err != nil {
81+
return nil, fmt.Errorf("chain-sync: decode error: %s", err)
82+
}
83+
babbageBlock.Header.id, err = generateBlockHeaderHash(rawBlock[0], nil)
84+
return &babbageBlock, err
7885
}
79-
return nil, nil
86+
return nil, fmt.Errorf("unknown node-to-client block type: %d", blockType)
8087
}
8188

8289
func NewBlockHeaderFromCbor(blockType uint, data []byte) (interface{}, error) {
@@ -102,14 +109,22 @@ func NewBlockHeaderFromCbor(blockType uint, data []byte) (interface{}, error) {
102109
// get the correct value
103110
byronMainBlockHeader.id, err = generateBlockHeaderHash(data, []byte{0x82, BLOCK_TYPE_BYRON_MAIN})
104111
return &byronMainBlockHeader, err
105-
default:
112+
case BLOCK_TYPE_SHELLEY, BLOCK_TYPE_ALLEGRA, BLOCK_TYPE_MARY, BLOCK_TYPE_ALONZO:
106113
var shelleyBlockHeader ShelleyBlockHeader
107114
if err := cbor.Unmarshal(data, &shelleyBlockHeader); err != nil {
108115
return nil, fmt.Errorf("chain-sync: decode error: %s", err)
109116
}
110117
shelleyBlockHeader.id, err = generateBlockHeaderHash(data, nil)
111118
return &shelleyBlockHeader, err
119+
case BLOCK_TYPE_BABBAGE:
120+
var babbageBlockHeader BabbageBlockHeader
121+
if err := cbor.Unmarshal(data, &babbageBlockHeader); err != nil {
122+
return nil, fmt.Errorf("chain-sync: decode error: %s", err)
123+
}
124+
babbageBlockHeader.id, err = generateBlockHeaderHash(data, nil)
125+
return &babbageBlockHeader, err
112126
}
127+
return nil, fmt.Errorf("unknown node-to-node block type: %d", blockType)
113128
}
114129

115130
func generateBlockHeaderHash(data []byte, prefix []byte) (string, error) {

cmd/go-ouroboros-network/chainsync.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ var eraIntersect = map[int]map[string][]interface{}{
4747
"mary": []interface{}{18014387, "9914c8da22a833a777d8fc1f735d2dbba70b99f15d765b6c6ee45fe322d92d93"},
4848
// Last block of epoch 153 (Mary era)
4949
"alonzo": []interface{}{36158304, "2b95ce628d36c3f8f37a32c2942b48e4f9295ccfe8190bcbc1f012e1e97c79eb"},
50+
// Last block of epoch 214 (Alonzo era)
51+
"babbage": []interface{}{62510369, "d931221f9bc4cae34de422d9f4281a2b0344e86aac6b31eb54e2ee90f44a09b9"},
5052
},
5153
MAINNET_MAGIC: map[string][]interface{}{
5254
"byron": []interface{}{},
@@ -58,6 +60,7 @@ var eraIntersect = map[int]map[string][]interface{}{
5860
"mary": []interface{}{23068793, "69c44ac1dda2ec74646e4223bc804d9126f719b1c245dadc2ad65e8de1b276d7"},
5961
// Last block of epoch 289 (Mary era)
6062
"alonzo": []interface{}{39916796, "e72579ff89dc9ed325b723a33624b596c08141c7bd573ecfff56a1f7229e4d09"},
63+
// TODO: add Babbage starting point after mainnet hard fork
6164
},
6265
}
6366

@@ -184,10 +187,14 @@ func chainSyncRollForwardHandler(blockType uint, blockData interface{}) error {
184187
syncState.byronEpochSlot = uint64(h.ConsensusData.SlotId.Slot)
185188
blockSlot = syncState.byronEpochBaseSlot + syncState.byronEpochSlot
186189
blockHash, _ = hex.DecodeString(h.Id())
187-
default:
190+
case block.BLOCK_TYPE_SHELLEY, block.BLOCK_TYPE_ALLEGRA, block.BLOCK_TYPE_MARY, block.BLOCK_TYPE_ALONZO:
188191
h := blockData.(*block.ShelleyBlockHeader)
189192
blockSlot = h.Body.Slot
190193
blockHash, _ = hex.DecodeString(h.Id())
194+
case block.BLOCK_TYPE_BABBAGE:
195+
h := blockData.(*block.BabbageBlockHeader)
196+
blockSlot = h.Body.Slot
197+
blockHash, _ = hex.DecodeString(h.Id())
191198
}
192199
if err := syncState.oConn.BlockFetch.RequestRange([]interface{}{blockSlot, blockHash}, []interface{}{blockSlot, blockHash}); err != nil {
193200
fmt.Printf("error calling RequestRange: %s\n", err)
@@ -213,6 +220,9 @@ func chainSyncRollForwardHandler(blockType uint, blockData interface{}) error {
213220
case block.BLOCK_TYPE_ALONZO:
214221
b := blockData.(*block.AlonzoBlock)
215222
fmt.Printf("era = Alonzo, slot = %d, block_no = %d, id = %s\n", b.Header.Body.Slot, b.Header.Body.BlockNumber, b.Id())
223+
case block.BLOCK_TYPE_BABBAGE:
224+
b := blockData.(*block.BabbageBlock)
225+
fmt.Printf("era = Babbage, slot = %d, block_no = %d, id = %s\n", b.Header.Body.Slot, b.Header.Body.BlockNumber, b.Id())
216226
default:
217227
fmt.Printf("unsupported (so far) block type %d\n", blockType)
218228
fmt.Printf("%s\n", utils.DumpCborStructure(blockData, ""))
@@ -267,6 +277,9 @@ func blockFetchBlockHandler(blockType uint, blockData interface{}) error {
267277
case block.BLOCK_TYPE_ALONZO:
268278
b := blockData.(*block.AlonzoBlock)
269279
fmt.Printf("era = Alonzo, slot = %d, block_no = %d, id = %s\n", b.Header.Body.Slot, b.Header.Body.BlockNumber, b.Id())
280+
case block.BLOCK_TYPE_BABBAGE:
281+
b := blockData.(*block.BabbageBlock)
282+
fmt.Printf("era = Babbage, slot = %d, block_no = %d, id = %s\n", b.Header.Body.Slot, b.Header.Body.BlockNumber, b.Id())
270283
default:
271284
fmt.Printf("unsupported (so far) block type %d\n", blockType)
272285
fmt.Printf("%s\n", utils.DumpCborStructure(blockData, ""))

protocol/chainsync/chainsync.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ func (c *ChainSync) handleRollForward(msgGeneric protocol.Message) error {
202202
block.BLOCK_HEADER_TYPE_ALLEGRA: block.BLOCK_TYPE_ALLEGRA,
203203
block.BLOCK_HEADER_TYPE_MARY: block.BLOCK_TYPE_MARY,
204204
block.BLOCK_HEADER_TYPE_ALONZO: block.BLOCK_TYPE_ALONZO,
205+
block.BLOCK_HEADER_TYPE_BABBAGE: block.BLOCK_TYPE_BABBAGE,
205206
}
206207
blockType = blockTypeMap[blockEra]
207208
var err error

versions.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type ProtocolVersionNtC struct {
1313
EnableAllegraEra bool
1414
EnableMaryEra bool
1515
EnableAlonzoEra bool
16+
EnableBabbageEra bool
1617
EnableLocalTxMonitorProtocol bool
1718
}
1819

@@ -47,6 +48,15 @@ var ProtocolVersionMapNtC = map[uint16]ProtocolVersionNtC{
4748
EnableAlonzoEra: true,
4849
EnableLocalTxMonitorProtocol: true,
4950
},
51+
13: ProtocolVersionNtC{
52+
EnableLocalQueryProtocol: true,
53+
EnableShelleyEra: true,
54+
EnableAllegraEra: true,
55+
EnableMaryEra: true,
56+
EnableAlonzoEra: true,
57+
EnableBabbageEra: true,
58+
EnableLocalTxMonitorProtocol: true,
59+
},
5060
}
5161

5262
type ProtocolVersionNtN struct {
@@ -57,6 +67,7 @@ type ProtocolVersionNtN struct {
5767
EnableAllegraEra bool
5868
EnableMaryEra bool
5969
EnableAlonzoEra bool
70+
EnableBabbageEra bool
6071
EnableFullDuplex bool
6172
}
6273

@@ -77,6 +88,15 @@ var ProtocolVersionMapNtN = map[uint16]ProtocolVersionNtN{
7788
EnableAlonzoEra: true,
7889
EnableFullDuplex: true,
7990
},
91+
9: ProtocolVersionNtN{
92+
EnableShelleyEra: true,
93+
EnableKeepAliveProtocol: true,
94+
EnableAllegraEra: true,
95+
EnableMaryEra: true,
96+
EnableAlonzoEra: true,
97+
EnableBabbageEra: true,
98+
EnableFullDuplex: true,
99+
},
80100
}
81101

82102
func GetProtocolVersionsNtC() []uint16 {

0 commit comments

Comments
 (0)