Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into malfeasance-v2-fou…
Browse files Browse the repository at this point in the history
…ndations
  • Loading branch information
fasmat committed Aug 14, 2024
2 parents 614d79a + 477fdbc commit cbab84b
Show file tree
Hide file tree
Showing 27 changed files with 138 additions and 617 deletions.
5 changes: 3 additions & 2 deletions bootstrap/types.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package bootstrap

import (
"go.uber.org/zap/zapcore"

"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/log"
)

type Update struct {
Expand Down Expand Up @@ -31,7 +32,7 @@ type EpochOverride struct {
ActiveSet []types.ATXID
}

func (vd *VerifiedUpdate) MarshalLogObject(encoder log.ObjectEncoder) error {
func (vd *VerifiedUpdate) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddString("persisted", vd.Persisted)
encoder.AddString("epoch", vd.Data.Epoch.String())
encoder.AddString("beacon", vd.Data.Beacon.String())
Expand Down
6 changes: 2 additions & 4 deletions common/types/account.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package types

import (
"github.com/spacemeshos/go-spacemesh/log"
)
import "go.uber.org/zap/zapcore"

//go:generate scalegen

Expand All @@ -17,7 +15,7 @@ type Account struct {
}

// MarshalLogObject implements encoding for the account state.
func (a *Account) MarshalLogObject(encoder log.ObjectEncoder) error {
func (a *Account) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddString("layer", a.Layer.String())
encoder.AddString("principal", a.Address.String())
encoder.AddUint64("next nonce", a.NextNonce)
Expand Down
16 changes: 7 additions & 9 deletions common/types/activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/spacemeshos/go-scale"
"github.com/spacemeshos/post/shared"
"go.uber.org/zap/zapcore"

"github.com/spacemeshos/go-spacemesh/common/util"
"github.com/spacemeshos/go-spacemesh/log"
Expand Down Expand Up @@ -56,9 +57,6 @@ func (t ATXID) Bytes() []byte {
return Hash32(t).Bytes()
}

// Field returns a log field. Implements the LoggableField interface.
func (t ATXID) Field() log.Field { return log.FieldNamed("atx_id", t.Hash32()) }

// EncodeScale implements scale codec interface.
func (t *ATXID) EncodeScale(e *scale.Encoder) (int, error) {
return scale.EncodeByteArray(e, t[:])
Expand All @@ -83,7 +81,7 @@ var EmptyATXID = ATXID{}
type ATXIDs []ATXID

// impl zap's ArrayMarshaler interface.
func (ids ATXIDs) MarshalLogArray(enc log.ArrayEncoder) error {
func (ids ATXIDs) MarshalLogArray(enc zapcore.ArrayEncoder) error {
for _, id := range ids {
enc.AppendString(id.String())
}
Expand All @@ -107,7 +105,7 @@ type NIPostChallenge struct {
InitialPost *Post
}

func (c *NIPostChallenge) MarshalLogObject(encoder log.ObjectEncoder) error {
func (c *NIPostChallenge) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
if c == nil {
return nil
}
Expand Down Expand Up @@ -136,7 +134,7 @@ type ATXMetadata struct {
MsgHash Hash32 // Hash of InnerActivationTx (returned by HashInnerBytes)
}

func (m *ATXMetadata) MarshalLogObject(encoder log.ObjectEncoder) error {
func (m *ATXMetadata) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddUint32("epoch", uint32(m.PublishEpoch))
encoder.AddString("hash", m.MsgHash.ShortString())
return nil
Expand Down Expand Up @@ -222,7 +220,7 @@ func (atx *ActivationTx) TickHeight() uint64 {
}

// MarshalLogObject implements logging interface.
func (atx *ActivationTx) MarshalLogObject(encoder log.ObjectEncoder) error {
func (atx *ActivationTx) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddString("atx_id", atx.id.String())
encoder.AddString("smesher", atx.SmesherID.String())
encoder.AddUint32("publish_epoch", atx.PublishEpoch.Uint32())
Expand Down Expand Up @@ -321,7 +319,7 @@ func (v VRFPostIndex) Field() log.Field { return log.Uint64("vrf_nonce", uint64(
// Post is an alias to postShared.Proof.
type Post shared.Proof

func (p *Post) MarshalLogObject(encoder log.ObjectEncoder) error {
func (p *Post) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
if p == nil {
return nil
}
Expand All @@ -337,7 +335,7 @@ type PostMetadata struct {
LabelsPerUnit uint64
}

func (m *PostMetadata) MarshalLogObject(encoder log.ObjectEncoder) error {
func (m *PostMetadata) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
if m == nil {
return nil
}
Expand Down
29 changes: 11 additions & 18 deletions common/types/ballot.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/spacemeshos/go-scale"
"go.uber.org/zap/zapcore"

"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/common/util"
"github.com/spacemeshos/go-spacemesh/hash"
"github.com/spacemeshos/go-spacemesh/log"
)

const (
Expand Down Expand Up @@ -103,7 +103,7 @@ type BallotMetadata struct {
MsgHash Hash32 // Hash of InnerBallot (returned by HashInnerBytes)
}

func (m *BallotMetadata) MarshalLogObject(encoder log.ObjectEncoder) error {
func (m *BallotMetadata) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddUint32("layer", m.Layer.Uint32())
encoder.AddString("msgHash", m.MsgHash.String())
return nil
Expand Down Expand Up @@ -186,21 +186,21 @@ type Votes struct {
}

// MarshalLogObject implements logging interface.
func (v *Votes) MarshalLogObject(encoder log.ObjectEncoder) error {
func (v *Votes) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddString("base", v.Base.String())
encoder.AddArray("support", log.ArrayMarshalerFunc(func(encoder log.ArrayEncoder) error {
encoder.AddArray("support", zapcore.ArrayMarshalerFunc(func(encoder zapcore.ArrayEncoder) error {
for _, vote := range v.Support {
encoder.AppendObject(&vote)
}
return nil
}))
encoder.AddArray("against", log.ArrayMarshalerFunc(func(encoder log.ArrayEncoder) error {
encoder.AddArray("against", zapcore.ArrayMarshalerFunc(func(encoder zapcore.ArrayEncoder) error {
for _, vote := range v.Against {
encoder.AppendObject(&vote)
}
return nil
}))
encoder.AddArray("abstain", log.ArrayMarshalerFunc(func(encoder log.ArrayEncoder) error {
encoder.AddArray("abstain", zapcore.ArrayMarshalerFunc(func(encoder zapcore.ArrayEncoder) error {
for _, lid := range v.Abstain {
encoder.AppendString(lid.String())
}
Expand All @@ -216,7 +216,7 @@ type BlockHeader struct {
}

// MarshalLogObject implements logging interface.
func (header *BlockHeader) MarshalLogObject(encoder log.ObjectEncoder) error {
func (header *BlockHeader) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddString("id", header.ID.String())
encoder.AddUint32("layer", header.LayerID.Uint32())
encoder.AddUint64("height", header.Height)
Expand All @@ -234,7 +234,7 @@ type Opinion struct {
}

// MarshalLogObject implements logging interface.
func (o *Opinion) MarshalLogObject(encoder log.ObjectEncoder) error {
func (o *Opinion) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddString("hash", o.Hash.ShortString())
return o.Votes.MarshalLogObject(encoder)
}
Expand All @@ -261,24 +261,17 @@ func (b *Ballot) Initialize() error {

// SignedBytes returns the serialization of the BallotMetadata for signing.
func (b *Ballot) SignedBytes() []byte {
data, err := codec.Encode(&BallotMetadata{
return codec.MustEncode(&BallotMetadata{
Layer: b.Layer,
MsgHash: BytesToHash(b.HashInnerBytes()),
})
if err != nil {
log.With().Fatal("failed to serialize BallotMetadata", log.Err(err))
}
return data
}

// HashInnerBytes returns the hash of the InnerBallot.
func (b *Ballot) HashInnerBytes() []byte {
h := hash.GetHasher()
defer hash.PutHasher(h)
_, err := codec.EncodeTo(h, &b.InnerBallot)
if err != nil {
log.With().Fatal("failed to encode InnerBallot for hashing", log.Err(err))
}
codec.MustEncodeTo(h, &b.InnerBallot)
return h.Sum(nil)
}

Expand All @@ -303,7 +296,7 @@ func (b *Ballot) IsMalicious() bool {
}

// MarshalLogObject implements logging encoder for Ballot.
func (b *Ballot) MarshalLogObject(encoder log.ObjectEncoder) error {
func (b *Ballot) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
var (
activeHash Hash32
beacon Beacon
Expand Down
8 changes: 4 additions & 4 deletions common/types/eligibility.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package types

import (
"github.com/spacemeshos/go-spacemesh/log"
"go.uber.org/zap/zapcore"
)

//go:generate scalegen
Expand All @@ -22,7 +22,7 @@ type HareEligibilityGossip struct {
Eligibility HareEligibility
}

func (hg *HareEligibilityGossip) MarshalLogObject(encoder log.ObjectEncoder) error {
func (hg *HareEligibilityGossip) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddUint32("layer", hg.Layer.Uint32())
encoder.AddUint32("round", hg.Round)
encoder.AddString("smesher", hg.NodeID.String())
Expand All @@ -41,7 +41,7 @@ type HareEligibility struct {
}

// MarshalLogObject implements logging interface.
func (e *HareEligibility) MarshalLogObject(encoder log.ObjectEncoder) error {
func (e *HareEligibility) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddUint16("count", e.Count)
encoder.AddString("proof", e.Proof.String())
return nil
Expand All @@ -59,7 +59,7 @@ type VotingEligibility struct {
}

// MarshalLogObject implements logging interface.
func (v *VotingEligibility) MarshalLogObject(encoder log.ObjectEncoder) error {
func (v *VotingEligibility) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddUint32("j", v.J)
encoder.AddString("sig", v.Sig.String())
return nil
Expand Down
10 changes: 5 additions & 5 deletions common/types/poet.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"time"

poetShared "github.com/spacemeshos/poet/shared"
"go.uber.org/zap/zapcore"

"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/log"
)

//go:generate scalegen -types PoetProof,PoetProofMessage
Expand All @@ -35,20 +35,20 @@ type PoetProof struct {
LeafCount uint64
}

func (p *PoetProof) MarshalLogObject(encoder log.ObjectEncoder) error {
func (p *PoetProof) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
if p == nil {
return nil
}
encoder.AddUint64("LeafCount", p.LeafCount)

encoder.AddString("MerkleProof.Root", hex.EncodeToString(p.Root))
encoder.AddArray("MerkleProof.ProvenLeaves", log.ArrayMarshalerFunc(func(encoder log.ArrayEncoder) error {
encoder.AddArray("MerkleProof.ProvenLeaves", zapcore.ArrayMarshalerFunc(func(encoder zapcore.ArrayEncoder) error {
for _, v := range p.ProvenLeaves {
encoder.AppendString(hex.EncodeToString(v))
}
return nil
}))
encoder.AddArray("MerkleProof.ProofNodes", log.ArrayMarshalerFunc(func(encoder log.ArrayEncoder) error {
encoder.AddArray("MerkleProof.ProofNodes", zapcore.ArrayMarshalerFunc(func(encoder zapcore.ArrayEncoder) error {
for _, v := range p.ProofNodes {
encoder.AppendString(hex.EncodeToString(v))
}
Expand All @@ -70,7 +70,7 @@ type PoetProofMessage struct {
Signature EdSignature
}

func (p *PoetProofMessage) MarshalLogObject(encoder log.ObjectEncoder) error {
func (p *PoetProofMessage) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
if p == nil {
return nil
}
Expand Down
15 changes: 4 additions & 11 deletions common/types/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/spacemeshos/go-scale"
"go.uber.org/zap/zapcore"

"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/hash"
"github.com/spacemeshos/go-spacemesh/log"
)

const (
Expand Down Expand Up @@ -107,24 +107,17 @@ func (p *Proposal) MustInitialize() {

// SignedBytes returns the serialization of the InnerProposal.
func (p *Proposal) SignedBytes() []byte {
data, err := codec.Encode(&BallotMetadata{
return codec.MustEncode(&BallotMetadata{
Layer: p.Layer,
MsgHash: BytesToHash(p.HashInnerProposal()),
})
if err != nil {
log.With().Fatal("failed to serialize BallotMetadata for proposal", log.Err(err))
}
return data
}

// HashInnerProposal returns the hash of the InnerProposal.
func (p *Proposal) HashInnerProposal() []byte {
h := hash.GetHasher()
defer hash.PutHasher(h)
_, err := codec.EncodeTo(h, &p.InnerProposal)
if err != nil {
log.With().Fatal("failed to encode InnerProposal for hashing", log.Err(err))
}
codec.MustEncodeTo(h, &p.InnerProposal)
return h.Sum(nil)
}

Expand All @@ -147,7 +140,7 @@ func (p *Proposal) SetBeacon(beacon Beacon) {
}

// MarshalLogObject implements logging interface.
func (p *Proposal) MarshalLogObject(encoder log.ObjectEncoder) error {
func (p *Proposal) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddString("proposal_id", p.ID().String())
encoder.AddInt("transactions", len(p.TxIDs))
encoder.AddString("mesh_hash", p.MeshHash.ShortString())
Expand Down
11 changes: 6 additions & 5 deletions common/types/result/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package result
import (
"fmt"

"go.uber.org/zap/zapcore"

"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/log"
)

type Layer struct {
Expand Down Expand Up @@ -34,13 +35,13 @@ func (l Layer) String() string {
return fmt.Sprintf("%d %+v", l.Layer, l.Blocks)
}

func (l *Layer) MarshalLogObject(encoder log.ObjectEncoder) error {
func (l *Layer) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddUint32("layer", l.Layer.Uint32())
encoder.AddString("opinion", l.Opinion.ShortString())
encoder.AddBool("verified", l.Verified)
encoder.AddArray("blocks", log.ArrayMarshalerFunc(func(aencoder log.ArrayEncoder) error {
encoder.AddArray("blocks", zapcore.ArrayMarshalerFunc(func(arrayEncoder zapcore.ArrayEncoder) error {
for i := range l.Blocks {
aencoder.AppendObject(&l.Blocks[i])
arrayEncoder.AppendObject(&l.Blocks[i])
}
return nil
}))
Expand All @@ -56,7 +57,7 @@ type Block struct {
Data bool `json:"d"`
}

func (b *Block) MarshalLogObject(encoder log.ObjectEncoder) error {
func (b *Block) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
b.Header.MarshalLogObject(encoder)
encoder.AddBool("valid", b.Valid)
encoder.AddBool("invalid", b.Invalid)
Expand Down
Loading

0 comments on commit cbab84b

Please sign in to comment.