Skip to content

Commit

Permalink
universe: reduce logging, spew usage during sync
Browse files Browse the repository at this point in the history
  • Loading branch information
jharveyb committed Jun 22, 2023
1 parent f89b342 commit 81c564a
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 9 deletions.
7 changes: 5 additions & 2 deletions universe/auto_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,11 @@ func (f *FederationEnvoy) syncUniverseState(ctx context.Context,
}

// If we synced anything from the server, then we'll log that here.
log.Infof("Synced new Universe leaves from server=%v, diff=%v",
spew.Sdump(addr), spew.Sdump(diff))
log.Infof("Synced new Universe leaves from server=%v", spew.Sdump(addr))
for _, d := range diff {
log.Infof("diff:\n%v", d.StringForInfoLog())
log.Debugf(d.StringForDebugLog())
}

// Log a new sync event in the background now that we know we were able
// to contract the remote server.
Expand Down
3 changes: 2 additions & 1 deletion universe/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ func withBaseUni[T any](fetcher uniFetcher, id Identifier,
func (a *MintingArchive) RootNode(ctx context.Context,
id Identifier) (BaseRoot, error) {

log.Debugf("Looking up root node for base Universe %v", spew.Sdump(id))
log.Debugf("Looking up root node for base Universe %v",
id.StringForLog())

return withBaseUni(a, id, func(baseUni BaseBackend) (BaseRoot, error) {
smtNode, assetName, err := baseUni.RootNode(ctx)
Expand Down
71 changes: 71 additions & 0 deletions universe/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net"
"strconv"
"strings"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
Expand Down Expand Up @@ -71,6 +72,35 @@ type GenesisWithGroup struct {
*asset.GroupKey
}

// StringForLog returns a string representation of a GenesisWithGroup for
// logging.
func (g GenesisWithGroup) StringForLog() string {
groupKey := "<nil>"
groupSig := "<nil>"
if g.GroupKey != nil {
groupKey = hex.EncodeToString(
schnorr.SerializePubKey(&g.GroupPubKey),
)
groupSig = hex.EncodeToString(g.Sig.Serialize())
}

var genesisGroupStr strings.Builder
genesisGroupStr.WriteString(
fmt.Sprintln("FirstPrevOut:", g.Genesis.FirstPrevOut.String()),
)
genesisGroupStr.WriteString(fmt.Sprintln("Tag:", g.Genesis.Tag))
genesisGroupStr.WriteString(
fmt.Sprintf("Metahash: %x\n", g.Genesis.MetaHash[:]),
)
genesisGroupStr.WriteString(
fmt.Sprintln("OutputIndex:", g.Genesis.OutputIndex),
)
genesisGroupStr.WriteString(fmt.Sprintf("Type: %v\n", g.Genesis.Type))
genesisGroupStr.WriteString(fmt.Sprintln("Group Key:", groupKey))
genesisGroupStr.WriteString(fmt.Sprintln("Group Sig:", groupSig))
return genesisGroupStr.String()
}

// MintingLeaf is a leaf node in the SMT that represents a minting output. For
// each new asset created for a given asset/universe, a new minting leaf is
// created.
Expand All @@ -87,6 +117,13 @@ type MintingLeaf struct {
Amt uint64
}

// StringForLog returns a string representation of a MintingLeaf for
// logging, omitting the genesis proof.
func (m *MintingLeaf) StringForLog() string {
return fmt.Sprintf("MintingLeaf:\n%vAmt: %d",
m.GenesisWithGroup.StringForLog(), m.Amt)
}

// SmtLeafNode returns the SMT leaf node for the given minting leaf.
func (m *MintingLeaf) SmtLeafNode() *mssmt.LeafNode {
return mssmt.NewLeafNode(m.GenesisProof[:], m.Amt)
Expand Down Expand Up @@ -206,6 +243,21 @@ type BaseRoot struct {
AssetName string
}

// StringForLog returns a string representation of a BaseRoot for logging.
func (b *BaseRoot) StringForLog() string {
var baseRootStr strings.Builder
baseRootStr.WriteString(fmt.Sprintln("ID:", b.ID.StringForLog()))
nodeStr := "Node: <nil>"
if b.Node != nil {
nodeStr = fmt.Sprintf("NodeHash: %v Sum: %d\n",
b.Node.NodeHash(), b.NodeSum())
}
baseRootStr.WriteString(nodeStr)
baseRootStr.WriteString(fmt.Sprintln(b.AssetName))

return baseRootStr.String()
}

// BaseForest is an interface used to keep track of the set of base universe
// roots that we know of. The BaseBackend interface is used to interact with a
// particular base universe, while this is used to obtain aggregate information
Expand Down Expand Up @@ -365,6 +417,25 @@ type AssetSyncDiff struct {
// * can used a sealed interface to return the error
}

// StringForInfoLog returns a string representation of an AssetSyncDiff for
// logging at the info level, omitting all leaf proofs.
func (d *AssetSyncDiff) StringForInfoLog() string {
oldRoot := d.OldUniverseRoot.StringForLog()
newRoot := d.NewUniverseRoot.StringForLog()
return fmt.Sprintf("old:\n%vnew:\n%v", oldRoot, newRoot)
}

// StringForDebugLog returns a string representation of an AssetSyncDiff for
// logging at the debug level, intended to be printed after StringForInfoLog.
func (d *AssetSyncDiff) StringForDebugLog() string {
var syncDiffStr strings.Builder
for _, leaf := range d.NewLeafProofs {
syncDiffStr.WriteString(fmt.Sprintln(leaf.StringForLog()))
}

return syncDiffStr.String()
}

// Syncer is used to synchronize the state of two Universe instances: a local
// instance and a remote instance. As a Universe is a tree based structure,
// tree based bisection can be used to find the point of divergence with
Expand Down
14 changes: 8 additions & 6 deletions universe/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ func (s *SimpleSyncer) executeSync(ctx context.Context, diffEngine DiffEngine,
// TODO(roasbeef): inclusion w/ root here, also that
// it's the expected asset ID

log.Infof("UniverseRoot(%v): inserting new leaf",
uniID.String())
log.Debugf("UniverseRoot(%v): inserting new leaf",
uniID.StringForLog())
log.Tracef("UniverseRoot(%v): inserting new leaf for "+
"key=%v", uniID.String(), spew.Sdump(key))
"key=%v", uniID.StringForLog(), spew.Sdump(key))

// TODO(roasbeef): this is actually giving a lagging
// proof for each of them
Expand All @@ -202,7 +202,8 @@ func (s *SimpleSyncer) executeSync(ctx context.Context, diffEngine DiffEngine,
}

log.Infof("Universe sync for UniverseRoot(%v) complete, %d "+
"new leaves inserted", uniID.String(), len(keysToFetch))
"new leaves inserted", uniID.StringForLog(),
len(keysToFetch))

// TODO(roabseef): sanity check local and remote roots match
// now?
Expand All @@ -215,9 +216,10 @@ func (s *SimpleSyncer) executeSync(ctx context.Context, diffEngine DiffEngine,
NewLeafProofs: fn.Collect(newLeaves),
}

log.Infof("Sync for UniverseRoot(%v) complete!", uniID.String())
log.Infof("Sync for UniverseRoot(%v) complete!",
uniID.StringForLog())
log.Tracef("Sync for UniverseRoot(%v) complete! New "+
"universe_root=%v", uniID.String(),
"universe_root=%v", uniID.StringForLog(),
spew.Sdump(remoteRoot))

return nil
Expand Down

0 comments on commit 81c564a

Please sign in to comment.