Skip to content

Commit

Permalink
Merge pull request #15 from blinklabs-io/feat/tuna-v2
Browse files Browse the repository at this point in the history
feat: TUNA v2 state datum
  • Loading branch information
agaffney authored Dec 4, 2023
2 parents de59eae + 9be44ac commit 9d41ca5
Showing 1 changed file with 94 additions and 2 deletions.
96 changes: 94 additions & 2 deletions tuna.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,106 @@

package models

import (
"github.com/blinklabs-io/gouroboros/cbor"
)

// TunaV1State represents the datum format used by the $TUNA mining smart contract (v1)
type TunaV1State struct {
// This allows the type to be used with cbor.DecodeGeneric
cbor.StructAsArray
BlockNumber int64
TargetHash []byte
CurrentHash []byte
LeadingZeros int64
DifficultyNumber int64
EpochTime int64
RealTimeNow int64
Message []byte
Extra any
Interlink [][]byte
}

func (t *TunaV1State) MarshalCBOR() ([]byte, error) {
var tmpInterlink []any
for _, item := range t.Interlink {
tmpInterlink = append(tmpInterlink, item)
}
tmp := cbor.NewConstructor(
0,
cbor.IndefLengthList{
Items: []any{
t.BlockNumber,
t.CurrentHash,
t.LeadingZeros,
t.DifficultyNumber,
t.EpochTime,
t.RealTimeNow,
t.Extra,
cbor.IndefLengthList{
Items: tmpInterlink,
},
},
},
)
return cbor.Encode(&tmp)
}

func (t *TunaV1State) UnmarshalCBOR(cborData []byte) error {
var tmpConstr cbor.Constructor
if _, err := cbor.Decode(cborData, &tmpConstr); err != nil {
return err
}
return cbor.DecodeGeneric(
tmpConstr.FieldsCbor(),
t,
)
}

// TunaV2State represents the datum format used by the $TUNA mining smart contract (v2)
type TunaV2State struct {
// This allows the type to be used with cbor.DecodeGeneric
cbor.StructAsArray
BlockNumber int64
CurrentHash []byte
LeadingZeros int64
DifficultyNumber int64
EpochTime int64
RealTimeNow int64
Interlink [][]byte
Extra any
}

func (t *TunaV2State) MarshalCBOR() ([]byte, error) {
var tmpInterlink []any
for _, item := range t.Interlink {
tmpInterlink = append(tmpInterlink, item)
}
tmp := cbor.NewConstructor(
0,
cbor.IndefLengthList{
Items: []any{
t.BlockNumber,
t.CurrentHash,
t.LeadingZeros,
t.DifficultyNumber,
t.EpochTime,
t.RealTimeNow,
cbor.IndefLengthList{
Items: tmpInterlink,
},
t.Extra,
},
},
)
return cbor.Encode(&tmp)
}

func (t *TunaV2State) UnmarshalCBOR(cborData []byte) error {
var tmpConstr cbor.Constructor
if _, err := cbor.Decode(cborData, &tmpConstr); err != nil {
return err
}
return cbor.DecodeGeneric(
tmpConstr.FieldsCbor(),
t,
)
}

0 comments on commit 9d41ca5

Please sign in to comment.