From fb209f1cf312eb5825f2b927daac3a3ef1fa554f Mon Sep 17 00:00:00 2001 From: Alexey Kiselev Date: Thu, 18 Mar 2021 13:50:46 +0300 Subject: [PATCH] Move error logging to debug (#437) * Log level of errors on appending transaction to UTX set to DEBUG * Network connection errrors moved to DEBUG level * More network log messages moved to DEBUG level. Fixed an issue with invalid blocks added to blocks cache. Naming fixed. * Error on state changing while mining micro block moved to DEBUG level. Handling of micro-block mining task added to Sync FSM. --- pkg/node/blocks_applier/blocks_applier.go | 4 +- pkg/node/node.go | 2 +- pkg/node/peer_manager/peer_manager.go | 12 +- pkg/node/state_fsm/default.go | 6 +- pkg/node/state_fsm/fsm_common.go | 5 +- pkg/node/state_fsm/fsm_idle.go | 2 + pkg/node/state_fsm/fsm_ng.go | 14 +- pkg/node/state_fsm/fsm_sync.go | 7 +- pkg/p2p/incoming/Incoming.go | 4 +- pkg/p2p/outgoing/outgoing.go | 6 +- pkg/proto/microblock.go | 4 +- pkg/proto/proto.go | 193 ++++++++++------------ pkg/proto/proto_test.go | 22 +-- pkg/state/appender.go | 14 +- pkg/state/state_test.go | 31 +++- 15 files changed, 170 insertions(+), 156 deletions(-) diff --git a/pkg/node/blocks_applier/blocks_applier.go b/pkg/node/blocks_applier/blocks_applier.go index be999f767..a63a0bf1c 100644 --- a/pkg/node/blocks_applier/blocks_applier.go +++ b/pkg/node/blocks_applier/blocks_applier.go @@ -72,8 +72,8 @@ func (a *innerBlocksApplier) apply(storage innerState, blocks []*proto.Block) (p } cumulativeScore := forkScore.Add(forkScore, parentScore) if currentScore.Cmp(cumulativeScore) >= 0 { // current score is higher or the same as fork score - do not apply blocks - return 0, errors.Errorf("low fork score: current blockchain score (%s) is higher than or equal to fork's score (%s)", - currentScore.String(), cumulativeScore.String()) + return 0, proto.NewInfoMsg(errors.Errorf("low fork score: current blockchain score (%s) is higher than or equal to fork's score (%s)", + currentScore.String(), cumulativeScore.String())) } // so, new blocks has higher score, try apply it. diff --git a/pkg/node/node.go b/pkg/node/node.go index 799a1c445..42e973b2b 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -94,7 +94,7 @@ func (a *Node) Serve(ctx context.Context) error { go func() { if err := a.peers.SpawnIncomingConnection(ctx, conn); err != nil { - zap.S().Error(err) + zap.S().Debugf("Incoming connection error: %v", err) return } }() diff --git a/pkg/node/peer_manager/peer_manager.go b/pkg/node/peer_manager/peer_manager.go index b76fed81b..63eed1661 100644 --- a/pkg/node/peer_manager/peer_manager.go +++ b/pkg/node/peer_manager/peer_manager.go @@ -185,7 +185,7 @@ func (a *PeerManagerImpl) NewConnection(p peer.Peer) error { ) a.Suspend(p, err.Error()) _ = p.Close() - return err + return proto.NewInfoMsg(err) } in, out := a.InOutCount() @@ -193,7 +193,7 @@ func (a *PeerManagerImpl) NewConnection(p peer.Peer) error { case peer.Incoming: if in >= a.limitConnections { _ = p.Close() - return errors.New("exceed incoming connections limit") + return proto.NewInfoMsg(errors.New("exceed incoming connections limit")) } case peer.Outgoing: if !p.Handshake().DeclaredAddr.Empty() { @@ -201,7 +201,7 @@ func (a *PeerManagerImpl) NewConnection(p peer.Peer) error { } if out >= a.limitConnections { _ = p.Close() - return errors.New("exceed outgoing connections limit") + return proto.NewInfoMsg(errors.New("exceed outgoing connections limit")) } default: _ = p.Close() @@ -300,11 +300,7 @@ func (a *PeerManagerImpl) AddAddress(ctx context.Context, addr string) { _ = a.state.Add([]proto.TCPAddr{proto.NewTCPAddrFromString(addr)}) go func() { if err := a.spawner.SpawnOutgoing(ctx, proto.NewTCPAddrFromString(addr)); err != nil { - if !errors.Is(err, context.Canceled) { - zap.S().Warn(err) - } else { - zap.S().Debug(err) - } + zap.S().Debug(err) } }() } diff --git a/pkg/node/state_fsm/default.go b/pkg/node/state_fsm/default.go index 6a3e55f7b..dd9dd7965 100644 --- a/pkg/node/state_fsm/default.go +++ b/pkg/node/state_fsm/default.go @@ -2,6 +2,7 @@ package state_fsm import ( . "github.com/wavesplatform/gowaves/pkg/p2p/peer" + "github.com/wavesplatform/gowaves/pkg/proto" ) type Default interface { @@ -27,6 +28,9 @@ func (a DefaultImpl) PeerError(fsm FSM, p Peer, baseInfo BaseInfo, _ error) (FSM func (a DefaultImpl) NewPeer(fsm FSM, p Peer, info BaseInfo) (FSM, Async, error) { err := info.peers.NewConnection(p) + if err != nil { + return fsm, nil, proto.NewInfoMsg(err) + } info.Reschedule() - return fsm, nil, err + return fsm, nil, nil } diff --git a/pkg/node/state_fsm/fsm_common.go b/pkg/node/state_fsm/fsm_common.go index 22be6670f..c8d1b8fbd 100644 --- a/pkg/node/state_fsm/fsm_common.go +++ b/pkg/node/state_fsm/fsm_common.go @@ -10,7 +10,10 @@ import ( func newPeer(fsm FSM, p Peer, peers peer_manager.PeerManager) (FSM, Async, error) { err := peers.NewConnection(p) - return fsm, nil, err + if err != nil { + return fsm, nil, proto.NewInfoMsg(err) + } + return fsm, nil, nil } // TODO handle no peers diff --git a/pkg/node/state_fsm/fsm_idle.go b/pkg/node/state_fsm/fsm_idle.go index 24c8382a3..562182e99 100644 --- a/pkg/node/state_fsm/fsm_idle.go +++ b/pkg/node/state_fsm/fsm_idle.go @@ -50,6 +50,8 @@ func (a *IdleFsm) Task(task AsyncTask) (FSM, Async, error) { case AskPeers: a.baseInfo.peers.AskPeers() return a, nil, nil + case MineMicro: // Do nothing + return a, nil, nil default: return a, nil, errors.Errorf("IdleFsm Task: unknown task type %d, data %+v", task.TaskType, task.Data) } diff --git a/pkg/node/state_fsm/fsm_ng.go b/pkg/node/state_fsm/fsm_ng.go index ba5dd44a3..3f55f3dc4 100644 --- a/pkg/node/state_fsm/fsm_ng.go +++ b/pkg/node/state_fsm/fsm_ng.go @@ -74,7 +74,8 @@ func (a *NGFsm) rollbackToStateFromCache(blockFromCache *proto.Block) error { previousBlockID := blockFromCache.Parent err := a.storage.RollbackTo(previousBlockID) if err != nil { - return errors.Wrapf(err, "failed to rollback to height %d", blockFromCache.Height) + return errors.Wrapf(err, "failed to rollback to parent block '%s' of cached block '%s'", + previousBlockID.String(), blockFromCache.ID.String()) } _, err = a.blocksApplier.Apply(a.storage, []*proto.Block{blockFromCache}) if err != nil { @@ -106,8 +107,6 @@ func (a *NGFsm) Block(peer peer.Peer, block *proto.Block) (FSM, Async, error) { } } } - a.blocksCache.Clear() - a.blocksCache.AddBlockState(block) _, err = a.blocksApplier.Apply(a.storage, []*proto.Block{block}) if err != nil { @@ -115,6 +114,10 @@ func (a *NGFsm) Block(peer peer.Peer, block *proto.Block) (FSM, Async, error) { return a, nil, err } metrics.FSMKeyBlockApplied("ng", block) + + a.blocksCache.Clear() + a.blocksCache.AddBlockState(block) + a.Scheduler.Reschedule() a.actions.SendScore(a.storage) a.CleanUtx() @@ -189,10 +192,12 @@ func (a *NGFsm) mineMicro(minedBlock *proto.Block, rest proto.MiningLimits, keyP if err == miner.NoTransactionsErr { return a, Tasks(NewMineMicroTask(5*time.Second, minedBlock, rest, keyPair, vrf)), nil } + if err == miner.StateChangedErr { + return a, nil, proto.NewInfoMsg(err) + } if err != nil { return a, nil, errors.Wrap(err, "NGFsm.mineMicro") } - a.blocksCache.AddBlockState(block) metrics.FSMMicroBlockGenerated("ng", micro) err = a.storage.Map(func(s state.NonThreadSafeState) error { _, err := a.blocksApplier.ApplyMicro(s, block) @@ -201,6 +206,7 @@ func (a *NGFsm) mineMicro(minedBlock *proto.Block, rest proto.MiningLimits, keyP if err != nil { return a, nil, err } + a.blocksCache.AddBlockState(block) a.Reschedule() metrics.FSMMicroBlockApplied("ng", micro) inv := proto.NewUnsignedMicroblockInv( diff --git a/pkg/node/state_fsm/fsm_sync.go b/pkg/node/state_fsm/fsm_sync.go index 15bc3ac9c..e90e9c7eb 100644 --- a/pkg/node/state_fsm/fsm_sync.go +++ b/pkg/node/state_fsm/fsm_sync.go @@ -68,6 +68,8 @@ func (a *SyncFsm) Task(task AsyncTask) (FSM, Async, error) { return NewIdleFsm(a.baseInfo), nil, TimeoutErr } return a, nil, nil + case MineMicro: // Do nothing + return a, nil, nil default: return a, nil, errors.Errorf("SyncFsm Task: unknown task type %d, data %+v", task.TaskType, task.Data) } @@ -119,7 +121,10 @@ func (a *SyncFsm) BlockIDs(peer Peer, signatures []proto.BlockID) (FSM, Async, e func (a *SyncFsm) NewPeer(p Peer) (FSM, Async, error) { err := a.baseInfo.peers.NewConnection(p) - return a, nil, err + if err != nil { + return a, nil, proto.NewInfoMsg(err) + } + return a, nil, nil } func (a *SyncFsm) Score(p Peer, score *proto.Score) (FSM, Async, error) { diff --git a/pkg/p2p/incoming/Incoming.go b/pkg/p2p/incoming/Incoming.go index be864ff0c..3413f5068 100644 --- a/pkg/p2p/incoming/Incoming.go +++ b/pkg/p2p/incoming/Incoming.go @@ -41,7 +41,7 @@ func runIncomingPeer(ctx context.Context, cancel context.CancelFunc, params Inco readHandshake := proto.Handshake{} _, err := readHandshake.ReadFrom(c) if err != nil { - zap.S().Error("failed to read handshake: ", err) + zap.S().Debug("Failed to read handshake: ", err) _ = c.Close() return err } @@ -64,7 +64,7 @@ func runIncomingPeer(ctx context.Context, cancel context.CancelFunc, params Inco _, err = writeHandshake.WriteTo(c) if err != nil { - zap.S().Error("failed to write handshake: ", err) + zap.S().Debug("failed to write handshake: ", err) _ = c.Close() return err } diff --git a/pkg/p2p/outgoing/outgoing.go b/pkg/p2p/outgoing/outgoing.go index 3f0af9a55..8e77e263d 100644 --- a/pkg/p2p/outgoing/outgoing.go +++ b/pkg/p2p/outgoing/outgoing.go @@ -45,7 +45,7 @@ func EstablishConnection(ctx context.Context, params EstablishParams, v proto.Ve connection, handshake, err := p.connect(ctx, c, v) if err != nil { - zap.S().Debug(err, params.Address) + zap.S().Debugf("Outgoing connection to address %s failed with error: %v", params.Address.String(), err) return errors.Wrapf(err, "%q", params.Address) } p.connection = connection @@ -92,7 +92,7 @@ func (a *connector) connect(ctx context.Context, c net.Conn, v proto.Version) (c _, err := handshake.WriteTo(c) if err != nil { - zap.S().Error("failed to send handshake: ", err, a.params.Address) + zap.S().Error("Failed to send handshake: ", err, a.params.Address) return nil, nil, err } @@ -105,7 +105,7 @@ func (a *connector) connect(ctx context.Context, c net.Conn, v proto.Version) (c _, err = handshake.ReadFrom(c) if err != nil { - zap.S().Debugf("failed to read handshake: %s %s", err, a.params.Address) + zap.S().Debugf("Failed to read handshake: %s %s", err, a.params.Address) select { case <-ctx.Done(): return nil, nil, errors.Wrap(ctx.Err(), "connector.connect") diff --git a/pkg/proto/microblock.go b/pkg/proto/microblock.go index 5f4362b3b..779221390 100644 --- a/pkg/proto/microblock.go +++ b/pkg/proto/microblock.go @@ -296,7 +296,7 @@ func (a *MicroBlockInvMessage) WriteTo(w io.Writer) (n int64, err error) { if err != nil { return 0, err } - copy(h.PayloadCsum[:], dig[:4]) + copy(h.PayloadChecksum[:], dig[:4]) n1, err := h.WriteTo(w) if err != nil { return 0, err @@ -339,7 +339,7 @@ func (a *MicroBlockRequestMessage) WriteTo(w io.Writer) (int64, error) { if err != nil { return 0, err } - copy(h.PayloadCsum[:], dig[:4]) + copy(h.PayloadChecksum[:], dig[:4]) n2, err := h.WriteTo(w) if err != nil { return 0, err diff --git a/pkg/proto/proto.go b/pkg/proto/proto.go index aedab5371..d09d4c131 100644 --- a/pkg/proto/proto.go +++ b/pkg/proto/proto.go @@ -17,9 +17,9 @@ import ( ) const ( - MaxHeaderLength = 17 - headerMagic = 0x12345678 - headerCsumLen = 4 + MaxHeaderLength = 17 + headerMagic = 0x12345678 + headerChecksumLen = 4 HeaderSizeWithPayload = 17 HeaderSizeWithoutPayload = 13 @@ -56,11 +56,11 @@ type Message interface { } type Header struct { - Length uint32 - Magic uint32 - ContentID uint8 - PayloadLength uint32 - PayloadCsum [headerCsumLen]byte + Length uint32 + Magic uint32 + ContentID uint8 + PayloadLength uint32 + PayloadChecksum [headerChecksumLen]byte } func (h *Header) MarshalBinary() ([]byte, error) { @@ -126,7 +126,7 @@ func (h *Header) UnmarshalBinary(data []byte) error { if uint32(len(data)) < HeaderSizeWithPayload { return errors.New("Header UnmarshalBinary: invalid data size") } - copy(h.PayloadCsum[:], data[13:17]) + copy(h.PayloadChecksum[:], data[13:17]) } return nil @@ -144,7 +144,7 @@ func (h *Header) Copy(data []byte) (int, error) { if len(data) < 17 { return 0, errors.New("Header Copy: invalid data size") } - copy(data[13:17], h.PayloadCsum[:]) + copy(data[13:17], h.PayloadChecksum[:]) return HeaderSizeWithPayload, nil } return HeaderSizeWithoutPayload, nil @@ -590,12 +590,10 @@ func (a *Handshake) WriteTo(w io.Writer) (int64, error) { // ReadFrom reads Handshake from io.Reader func (a *Handshake) ReadFrom(r io.Reader) (int64, error) { - // max Header size based on fields - //buf := [556]byte{} appName := U8String{} n1, err := appName.ReadFrom(r) if err != nil { - return 0, errors.Wrap(err, "appname") + return 0, errors.Wrap(err, "appName") } a.AppName = appName.S @@ -607,7 +605,7 @@ func (a *Handshake) ReadFrom(r io.Reader) (int64, error) { nodeName := U8String{} n3, err := nodeName.ReadFrom(r) if err != nil { - return 0, errors.Wrap(err, "nodename") + return 0, errors.Wrap(err, "nodeName") } a.NodeName = nodeName.S @@ -632,7 +630,7 @@ func (a *Handshake) ReadFrom(r io.Reader) (int64, error) { } a.Timestamp = uint64(tm) - return int64(n1 + n2 + n3 + n4 + n5 + n6), nil + return n1 + n2 + n3 + n4 + n5 + n6, nil } // GetPeersMessage implements the GetPeers message from the waves protocol @@ -673,7 +671,7 @@ func (m *GetPeersMessage) ReadFrom(r io.Reader) (int64, error) { if err != nil { return nn, err } - return int64(nn), m.UnmarshalBinary(packet) + return nn, m.UnmarshalBinary(packet) } // WriteTo writes GetPeersMessage to io.Writer @@ -700,7 +698,7 @@ func NewIpPortFromTcpAddr(a TCPAddr) IpPort { } func (a IpPort) Addr() net.IP { - return net.IP(a[:net.IPv6len]) + return a[:net.IPv6len] } func (a IpPort) Port() int { @@ -732,10 +730,27 @@ type PeerInfo struct { Port uint16 } -func (a PeerInfo) WriteTo(w io.Writer) (int64, error) { +func NewPeerInfoFromString(addr string) (PeerInfo, error) { + parts := strings.Split(addr, ":") + if len(parts) != 2 { + return PeerInfo{}, errors.Errorf("invalid addr %s", addr) + } + + ip := net.ParseIP(parts[0]) + port, err := strconv.ParseUint(parts[1], 10, 64) + if err != nil { + return PeerInfo{}, errors.Errorf("invalid port %s", parts[1]) + } + return PeerInfo{ + Addr: ip, + Port: uint16(port), + }, nil +} + +func (p PeerInfo) WriteTo(w io.Writer) (int64, error) { b := [8]byte{} - copy(b[:4], a.Addr.To4()) - binary.BigEndian.PutUint32(b[4:8], uint32(a.Port)) + copy(b[:4], p.Addr.To4()) + binary.BigEndian.PutUint32(b[4:8], uint32(p.Port)) n, err := w.Write(b[:]) if err != nil { return int64(n), err @@ -743,86 +758,69 @@ func (a PeerInfo) WriteTo(w io.Writer) (int64, error) { return int64(n), nil } -func (a *PeerInfo) ReadFrom(r io.Reader) (int64, error) { +func (p *PeerInfo) ReadFrom(r io.Reader) (int64, error) { b := [8]byte{} n, err := r.Read(b[:]) if err != nil { return int64(n), err } - a.Addr = net.IPv4(b[0], b[1], b[2], b[3]) - a.Port = uint16(binary.BigEndian.Uint32(b[:4])) + p.Addr = net.IPv4(b[0], b[1], b[2], b[3]) + p.Port = uint16(binary.BigEndian.Uint32(b[:4])) return int64(n), nil } -func NewPeerInfoFromString(addr string) (PeerInfo, error) { - strs := strings.Split(addr, ":") - if len(strs) != 2 { - return PeerInfo{}, errors.Errorf("invalid addr %s", addr) - } - - ip := net.ParseIP(string(strs[0])) - port, err := strconv.ParseUint(strs[1], 10, 64) - if err != nil { - return PeerInfo{}, errors.Errorf("invalid port %s", strs[1]) - } - return PeerInfo{ - Addr: ip, - Port: uint16(port), - }, nil -} - // MarshalBinary encodes PeerInfo message to binary form -func (m *PeerInfo) MarshalBinary() ([]byte, error) { +func (p *PeerInfo) MarshalBinary() ([]byte, error) { buffer := make([]byte, 8) - copy(buffer[0:4], m.Addr.To4()) - binary.BigEndian.PutUint32(buffer[4:8], uint32(m.Port)) + copy(buffer[0:4], p.Addr.To4()) + binary.BigEndian.PutUint32(buffer[4:8], uint32(p.Port)) return buffer, nil } // UnmarshalBinary decodes PeerInfo message from binary form -func (m *PeerInfo) UnmarshalBinary(data []byte) error { +func (p *PeerInfo) UnmarshalBinary(data []byte) error { if len(data) < 8 { return errors.New("too short") } - m.Addr = net.IPv4(data[0], data[1], data[2], data[3]) - m.Port = uint16(binary.BigEndian.Uint32(data[4:8])) + p.Addr = net.IPv4(data[0], data[1], data[2], data[3]) + p.Port = uint16(binary.BigEndian.Uint32(data[4:8])) return nil } // String() implements Stringer interface for PeerInfo -func (m PeerInfo) String() string { +func (p PeerInfo) String() string { var sb strings.Builder - sb.WriteString(m.Addr.String()) + sb.WriteString(p.Addr.String()) sb.WriteRune(':') - sb.WriteString(strconv.Itoa(int(m.Port))) + sb.WriteString(strconv.Itoa(int(p.Port))) return sb.String() } // MarshalJSON writes PeerInfo Value as JSON string -func (m PeerInfo) MarshalJSON() ([]byte, error) { +func (p PeerInfo) MarshalJSON() ([]byte, error) { var sb strings.Builder - if m.Addr == nil { + if p.Addr == nil { return nil, errors.New("invalid addr") } - if m.Port == 0 { + if p.Port == 0 { return nil, errors.New("invalid port") } sb.WriteRune('"') - sb.WriteString(m.Addr.String()) + sb.WriteString(p.Addr.String()) sb.WriteRune(':') - sb.WriteString(strconv.Itoa(int(m.Port))) + sb.WriteString(strconv.Itoa(int(p.Port))) sb.WriteRune('"') return []byte(sb.String()), nil } // UnmarshalJSON reads PeerInfo from JSON string -func (m *PeerInfo) UnmarshalJSON(value []byte) error { +func (p *PeerInfo) UnmarshalJSON(value []byte) error { s := string(value) if s == jsonNull { return nil @@ -833,38 +831,38 @@ func (m *PeerInfo) UnmarshalJSON(value []byte) error { return errors.Wrap(err, "failed to unmarshal PeerInfo from JSON") } - splitted := strings.SplitN(s, "/", 2) - if len(splitted) == 1 { - s = splitted[0] + parts := strings.SplitN(s, "/", 2) + if len(parts) == 1 { + s = parts[0] } else { - s = splitted[1] + s = parts[1] } - splitted = strings.SplitN(s, ":", 2) + parts = strings.SplitN(s, ":", 2) var addr, port string - if len(splitted) == 1 { - addr = splitted[0] + if len(parts) == 1 { + addr = parts[0] port = "0" } else { - addr = splitted[0] - port = splitted[1] + addr = parts[0] + port = parts[1] } - m.Addr = net.ParseIP(addr) + p.Addr = net.ParseIP(addr) port64, err := strconv.ParseUint(port, 10, 16) if err != nil { return errors.Wrap(err, "failed to unmarshal PeerInfo from JSON") } - m.Port = uint16(port64) + p.Port = uint16(port64) return nil } -func (m *PeerInfo) Empty() bool { - if m.Addr == nil || m.Addr.String() == "0.0.0.0" { +func (p *PeerInfo) Empty() bool { + if p.Addr == nil || p.Addr.String() == "0.0.0.0" { return true } - if m.Port == 0 { + if p.Port == 0 { return true } @@ -909,7 +907,7 @@ func (m *PeersMessage) WriteTo(w io.Writer) (int64, error) { if err != nil { return 0, err } - copy(h.PayloadCsum[:], dig[:4]) + copy(h.PayloadChecksum[:], dig[:4]) hdr, err := h.MarshalBinary() if err != nil { @@ -990,23 +988,6 @@ func readPacket(r io.Reader) ([]byte, int64, error) { return packet, int64(nn), nil } -func ReadPacket(buf []byte, r io.Reader) (int64, error) { - packetLen := buf[:4] - nn, err := io.ReadFull(r, packetLen) - if err != nil { - return int64(nn), err - } - l := binary.BigEndian.Uint32(packetLen) - buf = buf[4:] - packet := buf[:l] - n, err := io.ReadFull(r, packet) - if err != nil { - return int64(nn + n), err - } - nn += n - return int64(nn), nil -} - func ReadPayload(buf []byte, r io.Reader) (int64, error) { nn, err := io.ReadFull(r, buf) if err != nil { @@ -1072,7 +1053,7 @@ func (m *GetSignaturesMessage) MarshalBinary() ([]byte, error) { if err != nil { return nil, err } - copy(h.PayloadCsum[:], dig[:4]) + copy(h.PayloadChecksum[:], dig[:4]) hdr, err := h.MarshalBinary() if err != nil { @@ -1163,7 +1144,7 @@ func (m *SignaturesMessage) MarshalBinary() ([]byte, error) { if err != nil { return nil, err } - copy(h.PayloadCsum[:], dig[:4]) + copy(h.PayloadChecksum[:], dig[:4]) hdr, err := h.MarshalBinary() if err != nil { @@ -1250,7 +1231,7 @@ func (m *GetBlockMessage) MarshalBinary() ([]byte, error) { if err != nil { return nil, err } - copy(h.PayloadCsum[:], dig[:4]) + copy(h.PayloadChecksum[:], dig[:4]) hdr, err := h.MarshalBinary() if err != nil { @@ -1345,7 +1326,7 @@ func (m *BlockMessage) MarshalBinary() ([]byte, error) { if err != nil { return nil, err } - copy(h.PayloadCsum[:], dig[:4]) + copy(h.PayloadChecksum[:], dig[:4]) hdr, err := h.MarshalBinary() if err != nil { @@ -1365,7 +1346,7 @@ func MakeHeader(contentID uint8, payload []byte) (Header, error) { if err != nil { return Header{}, err } - copy(h.PayloadCsum[:], dig[:4]) + copy(h.PayloadChecksum[:], dig[:4]) return h, nil } @@ -1428,7 +1409,7 @@ func (m *ScoreMessage) MarshalBinary() ([]byte, error) { if err != nil { return nil, err } - copy(h.PayloadCsum[:], dig[:4]) + copy(h.PayloadChecksum[:], dig[:4]) hdr, err := h.MarshalBinary() if err != nil { @@ -1495,7 +1476,7 @@ func (m *TransactionMessage) MarshalBinary() ([]byte, error) { if err != nil { return nil, err } - copy(h.PayloadCsum[:], dig[:4]) + copy(h.PayloadChecksum[:], dig[:4]) hdr, err := h.MarshalBinary() if err != nil { @@ -1525,8 +1506,8 @@ func (m *TransactionMessage) UnmarshalBinary(data []byte) error { return err } - if !bytes.Equal(dig[:4], h.PayloadCsum[:]) { - return fmt.Errorf("invalid checksum: expected %x, found %x", dig[:4], h.PayloadCsum[:]) + if !bytes.Equal(dig[:4], h.PayloadChecksum[:]) { + return fmt.Errorf("invalid checksum: expected %x, found %x", dig[:4], h.PayloadChecksum[:]) } return nil } @@ -1583,7 +1564,7 @@ func (m *CheckPointMessage) MarshalBinary() ([]byte, error) { if err != nil { return nil, err } - copy(h.PayloadCsum[:], dig[:4]) + copy(h.PayloadChecksum[:], dig[:4]) hdr, err := h.MarshalBinary() if err != nil { @@ -1667,7 +1648,7 @@ func (m *PBBlockMessage) MarshalBinary() ([]byte, error) { if err != nil { return nil, err } - copy(h.PayloadCsum[:], dig[:4]) + copy(h.PayloadChecksum[:], dig[:4]) hdr, err := h.MarshalBinary() if err != nil { @@ -1736,7 +1717,7 @@ func (m *PBTransactionMessage) MarshalBinary() ([]byte, error) { if err != nil { return nil, err } - copy(h.PayloadCsum[:], dig[:4]) + copy(h.PayloadChecksum[:], dig[:4]) hdr, err := h.MarshalBinary() if err != nil { @@ -1766,8 +1747,8 @@ func (m *PBTransactionMessage) UnmarshalBinary(data []byte) error { return err } - if !bytes.Equal(dig[:4], h.PayloadCsum[:]) { - return fmt.Errorf("invalid checksum: expected %x, found %x", dig[:4], h.PayloadCsum[:]) + if !bytes.Equal(dig[:4], h.PayloadChecksum[:]) { + return fmt.Errorf("invalid checksum: expected %x, found %x", dig[:4], h.PayloadChecksum[:]) } return nil } @@ -1868,7 +1849,7 @@ func (m *GetBlockIdsMessage) MarshalBinary() ([]byte, error) { if err != nil { return nil, err } - copy(h.PayloadCsum[:], dig[:4]) + copy(h.PayloadChecksum[:], dig[:4]) hdr, err := h.MarshalBinary() if err != nil { @@ -1964,7 +1945,7 @@ func (m *BlockIdsMessage) MarshalBinary() ([]byte, error) { if err != nil { return nil, err } - copy(h.PayloadCsum[:], dig[:4]) + copy(h.PayloadChecksum[:], dig[:4]) hdr, err := h.MarshalBinary() if err != nil { @@ -2040,15 +2021,15 @@ func (m *BlockIdsMessage) WriteTo(w io.Writer) (int64, error) { type BulkMessage []Message -func (BulkMessage) ReadFrom(r io.Reader) (n int64, err error) { +func (BulkMessage) ReadFrom(_ io.Reader) (n int64, err error) { panic("implement me") } -func (BulkMessage) WriteTo(w io.Writer) (n int64, err error) { +func (BulkMessage) WriteTo(_ io.Writer) (n int64, err error) { panic("implement me") } -func (BulkMessage) UnmarshalBinary(data []byte) error { +func (BulkMessage) UnmarshalBinary(_ []byte) error { panic("implement me") } diff --git a/pkg/proto/proto_test.go b/pkg/proto/proto_test.go index 84e1ac050..c84d2f9f1 100644 --- a/pkg/proto/proto_test.go +++ b/pkg/proto/proto_test.go @@ -162,7 +162,7 @@ func (m *TransactionMessage) Equal(d comparable) bool { var tests = []protocolMarshallingTest{ { &GetPeersMessage{}, - //P. Len | Magic | ContentID | Payload Length | PayloadCsum | Payload + //P. Len | Magic | ContentID | Payload Length | PayloadChecksum | Payload "00000009 12345678 01 00000000 ", }, { @@ -174,52 +174,52 @@ var tests = []protocolMarshallingTest{ {net.IPv4(0x34, 0x33, 0x5c, 0xb6), 0x1acf}, }, }, - //P. Len | Magic | ContentID | Payload Length | PayloadCsum | Payload + //P. Len | Magic | ContentID | Payload Length | PayloadChecksum | Payload "00000039 12345678 02 0000002c 0b9ebfaf 00000005 8e5d2579 00001ad4 344d6fdb 00001acf 341c42d9 00001acf 341e2f43 00001acf 34335cb6 00001acf", }, { &PeersMessage{[]PeerInfo{{net.IPv4(1, 2, 3, 4), 0x8888}}}, - //P. Len | Magic | ContentID | Payload Length | PayloadCsum | Payload + //P. Len | Magic | ContentID | Payload Length | PayloadChecksum | Payload "00000019 12345678 02 0000000c 648fa8c8 00000001 01020304 00008888", }, { &GetSignaturesMessage{[]crypto.Signature{{0x01}}}, - //P. Len | Magic | ContentID | Payload Length | PayloadCsum | Payload + //P. Len | Magic | ContentID | Payload Length | PayloadChecksum | Payload "00000051 12345678 14 00000044 5474fb17 00000001 01000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000", }, { &SignaturesMessage{[]crypto.Signature{{0x13}}}, - //P. Len | Magic | ContentID | Payload Length | PayloadCsum | Payload + //P. Len | Magic | ContentID | Payload Length | PayloadChecksum | Payload "00000051 12345678 15 00000044 5e0c8bee 00000001 13000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000", }, { &GetBlockMessage{NewBlockIDFromSignature(crypto.Signature{0x15, 0x12})}, - //P. Len | Magic | ContentID | Payload Length | PayloadCsum | Payload + //P. Len | Magic | ContentID | Payload Length | PayloadChecksum | Payload "0000004d 12345678 16 00000040 01d5a895 15120000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000", }, { &BlockMessage{[]byte{0x66, 0x42}}, - //P. Len | Magic | ContentID | Payload Length | PayloadCsum | Payload + //P. Len | Magic | ContentID | Payload Length | PayloadChecksum | Payload "0000000f 12345678 17 00000002 c2426c62 6642", }, { &ScoreMessage{[]byte{0x66, 0x42}}, - //P. Len | Magic | ContentID | Payload Length | PayloadCsum | Payload + //P. Len | Magic | ContentID | Payload Length | PayloadChecksum | Payload "0000000f 12345678 18 00000002 c2426c62 6642", }, { &ScoreMessage{[]byte{0x01, 0x47, 0x02, 0x0e, 0x5b, 0x00, 0x75, 0x7a, 0xbe}}, - //P. Len | Magic | ContentID | Payload Length | PayloadCsum | Payload + //P. Len | Magic | ContentID | Payload Length | PayloadChecksum | Payload "00000016 12345678 18 00000009 74580717 01 47 02 0e 5b 00 75 7a be", }, { &TransactionMessage{[]byte{0x66, 0x42}}, - //P. Len | Magic | ContentID | Payload Length | PayloadCsum | Payload + //P. Len | Magic | ContentID | Payload Length | PayloadChecksum | Payload "0000000f 12345678 19 00000002 c2426c62 6642", }, { &CheckPointMessage{[]CheckpointItem{{0xdeadbeef, crypto.Signature{0x10, 0x11}}}}, - //P. Len | Magic | ContentID | Payload Length | PayloadCsum | Payload + //P. Len | Magic | ContentID | Payload Length | PayloadChecksum | Payload "00000059 12345678 64 0000004c fcb6b02a 00000001 00000000 deadbeef 10110000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000", }, } diff --git a/pkg/state/appender.go b/pkg/state/appender.go index f4477feb3..c52bc5bac 100644 --- a/pkg/state/appender.go +++ b/pkg/state/appender.go @@ -106,11 +106,11 @@ func newTxAppender( func (a *txAppender) checkDuplicateTxIdsImpl(id []byte, recentIds map[string]struct{}) error { // Check recent. if _, ok := recentIds[string(id)]; ok { - return proto.NewInfoMsg(errors.Errorf("transaction with ID %v already in state", id)) + return proto.NewInfoMsg(errors.Errorf("transaction with ID %s already in state", base58.Encode(id))) } // Check DB. if _, _, err := a.rw.readTransaction(id); err == nil { - return proto.NewInfoMsg(errors.Errorf("transaction with ID %v already in state", id)) + return proto.NewInfoMsg(errors.Errorf("transaction with ID %s already in state", base58.Encode(id))) } return nil } @@ -266,7 +266,7 @@ func (a *txAppender) checkScriptsLimits(scriptsRuns uint64) error { return nil } -func (a *txAppender) needToCheckOrdersSigs(transaction proto.Transaction, initialisation bool) (bool, bool, error) { +func (a *txAppender) needToCheckOrdersSignatures(transaction proto.Transaction, initialisation bool) (bool, bool, error) { tx, ok := transaction.(proto.Exchange) if !ok { return false, false, nil @@ -340,7 +340,7 @@ func (a *txAppender) verifyTxSigAndData(tx proto.Transaction, params *appendTxPa // Detect what signatures must be checked for this transaction. // For transaction with SmartAccount we don't check signature. checkTxSig := !accountHasVerifierScript - checkOrder1, checkOrder2, err := a.needToCheckOrdersSigs(tx, params.initialisation) + checkOrder1, checkOrder2, err := a.needToCheckOrdersSignatures(tx, params.initialisation) if err != nil { return err } @@ -719,7 +719,11 @@ func (a *txAppender) validateNextTx(tx proto.Transaction, currentTimestamp, pare validatingUtx: true, initialisation: false, } - return a.appendTx(tx, appendTxArgs) + err = a.appendTx(tx, appendTxArgs) + if err != nil { + return proto.NewInfoMsg(err) + } + return nil } func (a *txAppender) reset() { diff --git a/pkg/state/state_test.go b/pkg/state/state_test.go index ee7918379..275b7e7ed 100644 --- a/pkg/state/state_test.go +++ b/pkg/state/state_test.go @@ -9,6 +9,7 @@ import ( "path/filepath" "testing" + "github.com/mr-tron/base58" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/wavesplatform/gowaves/pkg/importer" @@ -29,9 +30,9 @@ type testCase struct { } func bigFromStr(s string) *big.Int { - var big big.Int - big.SetString(s, 10) - return &big + var i big.Int + i.SetString(s, 10) + return &i } func TestGenesisConfig(t *testing.T) { @@ -292,13 +293,19 @@ func TestStateManager_SavePeers(t *testing.T) { if err != nil { t.Fatalf("Failed to create temp dir for data: %v\n", err) } - defer os.RemoveAll(dataDir) + defer func() { + err = os.RemoveAll(dataDir) + require.NoError(t, err) + }() manager, err := newStateManager(dataDir, DefaultTestingStateParams(), settings.MainNetSettings) if err != nil { t.Fatalf("Failed to create state manager: %v.\n", err) } - defer manager.Close() + defer func() { + err := manager.Close() + require.NoError(t, err) + }() peers, err := manager.Peers() require.NoError(t, err) @@ -378,9 +385,9 @@ func TestDisallowDuplicateTxIds(t *testing.T) { tx := existingGenesisTx(t) txID, err := tx.GetID(settings.MainNetSettings.AddressSchemeCharacter) assert.NoError(t, err, "tx.GetID() failed") - expectedErrStr := fmt.Sprintf("check duplicate tx ids: transaction with ID %v already in state", txID) + expectedErrStr := fmt.Sprintf("check duplicate tx ids: transaction with ID %s already in state", base58.Encode(txID)) err = manager.ValidateNextTx(tx, 1460678400000, 1460678400000, 3, true) - assert.Error(t, err, "duplicate transacton ID was accepted by state") + assert.Error(t, err, "duplicate transaction ID was accepted by state") assert.EqualError(t, err, expectedErrStr) } @@ -418,13 +425,19 @@ func TestStateManager_Mutex(t *testing.T) { if err != nil { t.Fatalf("Failed to create temp dir for data: %v\n", err) } - defer os.RemoveAll(dataDir) + defer func() { + err := os.RemoveAll(dataDir) + require.NoError(t, err) + }() manager, err := newStateManager(dataDir, DefaultTestingStateParams(), settings.MainNetSettings) if err != nil { t.Fatalf("Failed to create state manager: %v.\n", err) } - defer manager.Close() + defer func() { + err := manager.Close() + require.NoError(t, err) + }() mu := manager.Mutex() mu.Lock().Unlock()