From 321bb40121cd312ef377c1b847d5b4e6d3fcf3d8 Mon Sep 17 00:00:00 2001 From: Calvin Kim Date: Tue, 19 Nov 2024 16:15:54 +0900 Subject: [PATCH] main: fix txtotals count TxTotals were only updating for the bridge nodes and it was generating proofs on the fly to calculate the size. Now the totals are kept track with CSNs and the size of the received MsgUtreexoTx is recorded. --- rpcserverhelp.go | 4 +-- server.go | 91 +++++++++++++----------------------------------- 2 files changed, 26 insertions(+), 69 deletions(-) diff --git a/rpcserverhelp.go b/rpcserverhelp.go index 8c47badd..1e6f3f61 100644 --- a/rpcserverhelp.go +++ b/rpcserverhelp.go @@ -516,8 +516,8 @@ var helpDescsEnUS = map[string]string{ "getnettotalsresult-timemillis": "Number of milliseconds since 1 Jan 1970 GMT", // GetTxTotalsResult help. - "gettxtotalsresult-totaltxbytesrecv": "Total tx bytes received", - "gettxtotalsresult-totaltxbytessent": "Total tx bytes sent", + "gettxtotalsresult-totaltxbytesrecv": "Total tx bytes received (includes all utreexo data)", + "gettxtotalsresult-totaltxbytessent": "Total tx bytes sent (includes all utreexo data)", "gettxtotalsresult-totalproofbytesrecv": "Total utxo data bytes received", "gettxtotalsresult-totalproofbytessent": "Total utxo data bytes sent", "gettxtotalsresult-totalaccbytesrecv": "Total accumulator proof bytes received", diff --git a/server.go b/server.go index e890543a..6454c881 100644 --- a/server.go +++ b/server.go @@ -1409,13 +1409,9 @@ func (sp *serverPeer) OnAddr(_ *peer.Peer, msg *wire.MsgAddr) { func (sp *serverPeer) OnRead(_ *peer.Peer, bytesRead int, msg wire.Message, err error) { sp.server.AddBytesReceived(uint64(bytesRead)) - switch msgTx := msg.(type) { - case *wire.MsgTx: - err := sp.server.UpdateProofBytesRead(msgTx) - if err != nil { - srvrLog.Debugf("Couldn't update proof bytes read. err: %s", - err.Error()) - } + switch msg := msg.(type) { + case *wire.MsgUtreexoTx: + sp.server.UpdateProofBytesRead(msg) sp.server.addTxBytesReceived(uint64(bytesRead)) } } @@ -1425,13 +1421,9 @@ func (sp *serverPeer) OnRead(_ *peer.Peer, bytesRead int, msg wire.Message, err func (sp *serverPeer) OnWrite(_ *peer.Peer, bytesWritten int, msg wire.Message, err error) { sp.server.AddBytesSent(uint64(bytesWritten)) - switch msgTx := msg.(type) { - case *wire.MsgTx: - err := sp.server.UpdateProofBytesWritten(msgTx) - if err != nil { - srvrLog.Debugf("Couldn't update proof bytes written. err: %s", - err.Error()) - } + switch msg := msg.(type) { + case *wire.MsgUtreexoTx: + sp.server.UpdateProofBytesWritten(msg) sp.server.addTxBytesSent(uint64(bytesWritten)) } } @@ -2750,69 +2742,34 @@ func (s *server) addAccBytesSent(bytesSent uint64) { atomic.AddUint64(&s.txBytes.accBytesSent, bytesSent) } -// GetProofSizeforTx calculates the size of the raw proof that would needed for -// proving the tx to an utreexo node. -func (s *server) GetProofSizeforTx(msgTx *wire.MsgTx) (int, int, error) { - // If utreexo proof index is not present, we can't calculate the - // proof size as we can't grab the proof for the tx. - if s.utreexoProofIndex == nil && s.flatUtreexoProofIndex == nil { - err := fmt.Errorf("UtreexoProofIndex and FlatUtreexoProofIndex is nil. "+ - "Cannot calculate proof size for tx %s.", msgTx.TxHash().String()) - return 0, 0, err - } - - tx := btcutil.NewTx(msgTx) - leafDatas, err := blockchain.TxToDelLeaves(tx, s.chain) - if err != nil { - return 0, 0, err - } - - var ud *wire.UData - - // We already checked that at least one is active. Pick one and - // generate the UData. - if s.utreexoProofIndex != nil { - ud, err = s.utreexoProofIndex.GenerateUData(leafDatas) - } else { - ud, err = s.flatUtreexoProofIndex.GenerateUData(leafDatas) - } - if err != nil { - return 0, 0, err - } - - return ud.SerializeAccSize(), ud.SerializeUtxoDataSize(), nil -} - // UpdateProofBytesRead updates the bytes for utreexo proofs that would have // been received from all peers for tx messages. -func (s *server) UpdateProofBytesRead(msgTx *wire.MsgTx) error { - // If utreexo proof index is present, also grab the proof size. - if s.utreexoProofIndex != nil || s.flatUtreexoProofIndex != nil { - accSize, utxoDataSize, err := s.GetProofSizeforTx(msgTx) - if err != nil { - return err +func (s *server) UpdateProofBytesRead(msgUtreexoTx *wire.MsgUtreexoTx) { + if s.chain.IsUtreexoViewActive() { + var utxoDataSize uint64 + for _, ld := range msgUtreexoTx.LeafDatas { + utxoDataSize += uint64(ld.SerializeSizeCompact()) } - s.addProofBytesReceived(uint64(utxoDataSize)) - s.addAccBytesReceived(uint64(accSize)) - } + accSize := uint64(wire.BatchProofSerializeAccProofSize(&msgUtreexoTx.AccProof)) - return nil + s.addProofBytesReceived(utxoDataSize) + s.addAccBytesReceived(accSize) + } } // UpdateProofBytesWritten updates the bytes for utreexo proofs that would have // been sent to all peers for tx messages. -func (s *server) UpdateProofBytesWritten(msgTx *wire.MsgTx) error { - // If utreexo proof index is present, also grab the proof size. - if s.utreexoProofIndex != nil || s.flatUtreexoProofIndex != nil { - accSize, utxoDataSize, err := s.GetProofSizeforTx(msgTx) - if err != nil { - return err +func (s *server) UpdateProofBytesWritten(msgUtreexoTx *wire.MsgUtreexoTx) { + if s.chain.IsUtreexoViewActive() { + var utxoDataSize uint64 + for _, ld := range msgUtreexoTx.LeafDatas { + utxoDataSize += uint64(ld.SerializeSizeCompact()) } - s.addProofBytesSent(uint64(utxoDataSize)) - s.addAccBytesSent(uint64(accSize)) - } + accSize := uint64(wire.BatchProofSerializeAccProofSize(&msgUtreexoTx.AccProof)) - return nil + s.addProofBytesReceived(utxoDataSize) + s.addAccBytesReceived(accSize) + } } // TxTotals returns the sum of all bytes received and sent across the network