Skip to content

Commit

Permalink
Adjust accounts to new stream package and remove marshalutil usage
Browse files Browse the repository at this point in the history
  • Loading branch information
jonastheis committed Oct 27, 2023
1 parent 5383f66 commit bb8d582
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 174 deletions.
50 changes: 30 additions & 20 deletions pkg/core/account/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"github.com/iotaledger/hive.go/ds/shrinkingmap"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/runtime/syncutils"
"github.com/iotaledger/hive.go/serializer/v2/marshalutil"
"github.com/iotaledger/hive.go/serializer/v2"
"github.com/iotaledger/hive.go/serializer/v2/stream"
iotago "github.com/iotaledger/iota.go/v4"
)

Expand Down Expand Up @@ -134,6 +135,7 @@ func AccountsFromReader(readSeeker io.ReadSeeker) (*Accounts, int, error) {
}

func (a *Accounts) readFromReadSeeker(reader io.ReadSeeker) (n int, err error) {
// TODO: improve this
a.mutex.Lock()
defer a.mutex.Unlock()

Expand Down Expand Up @@ -181,31 +183,39 @@ func (a *Accounts) readFromReadSeeker(reader io.ReadSeeker) (n int, err error) {
return n, nil
}

func (a *Accounts) Bytes() (bytes []byte, err error) {
func (a *Accounts) Bytes() ([]byte, error) {
a.mutex.RLock()
defer a.mutex.RUnlock()

m := marshalutil.New()

m.WriteUint32(uint32(a.accountPools.Size()))
var innerErr error
a.ForEach(func(id iotago.AccountID, pool *Pool) bool {
m.WriteBytes(id[:])
poolBytes, err := pool.Bytes()
if err != nil {
innerErr = err
return false
byteBuffer := stream.NewByteBuffer()

if err := stream.WriteCollection(byteBuffer, serializer.SeriLengthPrefixTypeAsUint32, func() (elementsCount int, err error) {
var innerErr error
a.ForEach(func(id iotago.AccountID, pool *Pool) bool {
if innerErr = stream.Write(byteBuffer, id); innerErr != nil {
return false
}

if innerErr = stream.WriteFixedSizeObject(byteBuffer, pool, poolBytesLength, func(pool *Pool) ([]byte, error) {
return pool.Bytes()
}); innerErr != nil {
return false
}

return true
})
if innerErr != nil {
return 0, innerErr
}
m.WriteBytes(poolBytes)

return true
})

m.WriteBool(a.reused.Load())
return a.accountPools.Size(), nil
}); err != nil {
return nil, ierrors.Wrap(err, "failed to write accounts")
}

if innerErr != nil {
return nil, innerErr
if err := stream.Write(byteBuffer, a.reused.Load()); err != nil {
return nil, ierrors.Wrap(err, "failed to write reused flag")
}

return m.Bytes(), nil
return byteBuffer.Bytes()
}
51 changes: 27 additions & 24 deletions pkg/core/account/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package account

import (
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/serializer/v2/marshalutil"
"github.com/iotaledger/hive.go/serializer/v2"
"github.com/iotaledger/hive.go/serializer/v2/stream"
iotago "github.com/iotaledger/iota.go/v4"
)

const poolBytesLength = 3 * marshalutil.Uint64Size
const poolBytesLength = 3 * serializer.UInt64ByteSize

// Pool represents all the data we need for a given validator and epoch to calculate its rewards data.
type Pool struct {
Expand All @@ -19,33 +20,35 @@ type Pool struct {

func PoolFromBytes(bytes []byte) (*Pool, int, error) {
p := new(Pool)
m := marshalutil.New(bytes)
poolStake, err := m.ReadUint64()
if err != nil {
return nil, m.ReadOffset(), ierrors.Wrap(err, "failed to parse pool stake")
}
p.PoolStake = iotago.BaseToken(poolStake)

validatorStake, err := m.ReadUint64()
if err != nil {
return nil, m.ReadOffset(), ierrors.Wrap(err, "failed to parse validator stake")
}
p.ValidatorStake = iotago.BaseToken(validatorStake)
var err error
byteReader := stream.NewByteReader(bytes)

fixedCost, err := m.ReadUint64()
if err != nil {
return nil, m.ReadOffset(), ierrors.Wrap(err, "failed to parse fixed cost")
if p.PoolStake, err = stream.Read[iotago.BaseToken](byteReader); err != nil {
return nil, 0, ierrors.Wrap(err, "failed to read PoolStake")
}
if p.ValidatorStake, err = stream.Read[iotago.BaseToken](byteReader); err != nil {
return nil, 0, ierrors.Wrap(err, "failed to read ValidatorStake")
}
if p.FixedCost, err = stream.Read[iotago.Mana](byteReader); err != nil {
return nil, 0, ierrors.Wrap(err, "failed to read FixedCost")
}
p.FixedCost = iotago.Mana(fixedCost)

return p, m.ReadOffset(), nil
return p, byteReader.BytesRead(), nil
}

func (p *Pool) Bytes() (bytes []byte, err error) {
m := marshalutil.New()
m.WriteUint64(uint64(p.PoolStake))
m.WriteUint64(uint64(p.ValidatorStake))
m.WriteUint64(uint64(p.FixedCost))
func (p *Pool) Bytes() ([]byte, error) {
byteBuffer := stream.NewByteBuffer(poolBytesLength)

if err := stream.Write(byteBuffer, p.PoolStake); err != nil {
return nil, ierrors.Wrap(err, "failed to write PoolStake")
}
if err := stream.Write(byteBuffer, p.ValidatorStake); err != nil {
return nil, ierrors.Wrap(err, "failed to write ValidatorStake")
}
if err := stream.Write(byteBuffer, p.FixedCost); err != nil {
return nil, ierrors.Wrap(err, "failed to write FixedCost")
}

return m.Bytes(), nil
return byteBuffer.Bytes()
}
80 changes: 55 additions & 25 deletions pkg/model/account_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/serializer/v2"
"github.com/iotaledger/hive.go/serializer/v2/marshalutil"
"github.com/iotaledger/hive.go/serializer/v2/stream"
iotago "github.com/iotaledger/iota.go/v4"
)
Expand All @@ -18,7 +17,7 @@ import (
type AccountDiff struct {
BICChange iotago.BlockIssuanceCredits

PreviousUpdatedTime iotago.SlotIndex
PreviousUpdatedSlot iotago.SlotIndex

NewExpirySlot iotago.SlotIndex
PreviousExpirySlot iotago.SlotIndex
Expand All @@ -44,7 +43,7 @@ type AccountDiff struct {
func NewAccountDiff() *AccountDiff {
return &AccountDiff{
BICChange: 0,
PreviousUpdatedTime: 0,
PreviousUpdatedSlot: 0,
NewExpirySlot: 0,
PreviousExpirySlot: 0,
NewOutputID: iotago.EmptyOutputID,
Expand All @@ -61,36 +60,60 @@ func NewAccountDiff() *AccountDiff {
}

func (d AccountDiff) Bytes() ([]byte, error) {
m := marshalutil.New()
byteBuffer := stream.NewByteBuffer()

m.WriteInt64(int64(d.BICChange))
m.WriteUint32(uint32(d.PreviousUpdatedTime))
m.WriteUint32(uint32(d.NewExpirySlot))
m.WriteUint32(uint32(d.PreviousExpirySlot))
m.WriteBytes(lo.PanicOnErr(d.NewOutputID.Bytes()))
m.WriteBytes(lo.PanicOnErr(d.PreviousOutputID.Bytes()))
if err := stream.Write(byteBuffer, d.BICChange); err != nil {
return nil, ierrors.Wrap(err, "unable to write BICChange value in the diff")
}
if err := stream.Write(byteBuffer, d.PreviousUpdatedSlot); err != nil {
return nil, ierrors.Wrap(err, "unable to write PreviousUpdatedSlot in the diff")
}
if err := stream.Write(byteBuffer, d.NewExpirySlot); err != nil {
return nil, ierrors.Wrap(err, "unable to write NewExpirySlot in the diff")
}
if err := stream.Write(byteBuffer, d.PreviousExpirySlot); err != nil {
return nil, ierrors.Wrap(err, "unable to write PreviousExpirySlot in the diff")
}
if err := stream.Write(byteBuffer, d.NewOutputID); err != nil {
return nil, ierrors.Wrap(err, "unable to write NewOutputID in the diff")
}
if err := stream.Write(byteBuffer, d.PreviousOutputID); err != nil {
return nil, ierrors.Wrap(err, "unable to write PreviousOutputID in the diff")
}

if err := writeBlockIssuerKeys(m, d.BlockIssuerKeysAdded); err != nil {
if err := writeBlockIssuerKeys(byteBuffer, d.BlockIssuerKeysAdded); err != nil {
return nil, err
}
if err := writeBlockIssuerKeys(m, d.BlockIssuerKeysRemoved); err != nil {
if err := writeBlockIssuerKeys(byteBuffer, d.BlockIssuerKeysRemoved); err != nil {
return nil, err
}

m.WriteInt64(d.ValidatorStakeChange)
m.WriteInt64(d.DelegationStakeChange)
m.WriteInt64(d.FixedCostChange)
m.WriteUint64(uint64(d.StakeEndEpochChange))
m.WriteBytes(lo.PanicOnErr(d.NewLatestSupportedVersionAndHash.Bytes()))
m.WriteBytes(lo.PanicOnErr(d.PrevLatestSupportedVersionAndHash.Bytes()))
if err := stream.Write(byteBuffer, d.ValidatorStakeChange); err != nil {
return nil, ierrors.Wrap(err, "unable to write ValidatorStakeChange in the diff")
}
if err := stream.Write(byteBuffer, d.DelegationStakeChange); err != nil {
return nil, ierrors.Wrap(err, "unable to write DelegationStakeChange in the diff")
}
if err := stream.Write(byteBuffer, d.FixedCostChange); err != nil {
return nil, ierrors.Wrap(err, "unable to write FixedCostChange in the diff")
}
if err := stream.Write(byteBuffer, d.StakeEndEpochChange); err != nil {
return nil, ierrors.Wrap(err, "unable to write StakeEndEpochChange in the diff")
}
if err := stream.WriteFixedSizeObject(byteBuffer, d.NewLatestSupportedVersionAndHash, VersionAndHashSize, VersionAndHash.Bytes); err != nil {
return nil, ierrors.Wrap(err, "unable to write NewLatestSupportedVersionAndHash in the diff")
}
if err := stream.WriteFixedSizeObject(byteBuffer, d.PrevLatestSupportedVersionAndHash, VersionAndHashSize, VersionAndHash.Bytes); err != nil {
return nil, ierrors.Wrap(err, "unable to write PrevLatestSupportedVersionAndHash in the diff")
}

return m.Bytes(), nil
return byteBuffer.Bytes()
}

func (d *AccountDiff) Clone() *AccountDiff {
return &AccountDiff{
BICChange: d.BICChange,
PreviousUpdatedTime: d.PreviousUpdatedTime,
PreviousUpdatedSlot: d.PreviousUpdatedSlot,
NewExpirySlot: d.NewExpirySlot,
PreviousExpirySlot: d.PreviousExpirySlot,
NewOutputID: d.NewOutputID,
Expand All @@ -107,20 +130,23 @@ func (d *AccountDiff) Clone() *AccountDiff {
}

func (d *AccountDiff) FromBytes(b []byte) (int, error) {
// TODO: remove
return d.readFromReadSeeker(bytes.NewReader(b))
}

func (d *AccountDiff) FromReader(readSeeker io.ReadSeeker) error {
// TODO: remove
return lo.Return2(d.readFromReadSeeker(readSeeker))
}

func (d *AccountDiff) readFromReadSeeker(reader io.ReadSeeker) (offset int, err error) {
// TODO: adjust to new stream API
if err = binary.Read(reader, binary.LittleEndian, &d.BICChange); err != nil {
return offset, ierrors.Wrap(err, "unable to read account BIC balance value in the diff")
}
offset += 8

if err = binary.Read(reader, binary.LittleEndian, &d.PreviousUpdatedTime); err != nil {
if err = binary.Read(reader, binary.LittleEndian, &d.PreviousUpdatedSlot); err != nil {
return offset, ierrors.Wrap(err, "unable to read previous updated time in the diff")
}
offset += iotago.SlotIndexLength
Expand Down Expand Up @@ -204,22 +230,26 @@ func (d *AccountDiff) readFromReadSeeker(reader io.ReadSeeker) (offset int, err
return offset, nil
}

func writeBlockIssuerKeys(m *marshalutil.MarshalUtil, blockIssuerKeys iotago.BlockIssuerKeys) error {
func writeBlockIssuerKeys(byteBuffer *stream.ByteBuffer, blockIssuerKeys iotago.BlockIssuerKeys) error {
// TODO: improve this

blockIssuerKeysBytes, err := iotago.CommonSerixAPI().Encode(context.TODO(), blockIssuerKeys)
if err != nil {
return ierrors.Wrap(err, "unable to encode blockIssuerKeys in the diff")
}

m.WriteUint64(uint64(len(blockIssuerKeysBytes)))
m.WriteBytes(blockIssuerKeysBytes)
if err := stream.WriteByteSlice(byteBuffer, blockIssuerKeysBytes, serializer.SeriLengthPrefixTypeAsUint64); err != nil {
return ierrors.Wrap(err, "unable to write blockIssuerKeysBytes in the diff")
}

return nil
}

func readBlockIssuerKeys(reader io.ReadSeeker) (iotago.BlockIssuerKeys, int, error) {
// TODO: improve this
var bytesConsumed int

blockIssuerKeysBytes, err := stream.ReadBlob(reader)
blockIssuerKeysBytes, err := stream.ReadByteSlice(reader, serializer.SeriLengthPrefixTypeAsUint64)
if err != nil {
return nil, bytesConsumed, ierrors.Wrap(err, "unable to read blockIssuerKeysBytes in the diff")
}
Expand Down
Loading

0 comments on commit bb8d582

Please sign in to comment.