Use strongly-typed objects

poc8
Taylor Gerring 10 years ago
parent c14900dbb0
commit d53e5646ec
  1. 15
      eth/backend.go
  2. 17
      logger/loggers.go
  3. 294
      logger/types.go
  4. 55
      p2p/server.go

@ -222,14 +222,13 @@ func (s *Ethereum) MaxPeers() int {
// Start the ethereum
func (s *Ethereum) Start(seed bool) error {
evd := map[string]interface{}{
"version_string": s.ClientIdentity().String(),
"guid": ethutil.Bytes2Hex(s.ClientIdentity().Pubkey()),
"level": "debug",
"coinbase": ethutil.Bytes2Hex(s.KeyManager().Address()),
"eth_version": ProtocolVersion,
}
jsonlogger.LogJson("starting", evd)
jsonlogger.LogJson("starting", &ethlogger.LogStarting{
ClientString: s.ClientIdentity().String(),
Guid: ethutil.Bytes2Hex(s.ClientIdentity().Pubkey()),
Coinbase: ethutil.Bytes2Hex(s.KeyManager().Address()),
ProtocolVersion: ProtocolVersion,
})
err := s.net.Start()
if err != nil {
return err

@ -16,7 +16,6 @@ import (
"encoding/json"
"fmt"
"os"
"time"
)
type LogLevel uint32
@ -121,20 +120,12 @@ func NewJsonLogger() *JsonLogger {
return &JsonLogger{}
}
func (logger *JsonLogger) LogJson(msgname string, dict map[string]interface{}) {
if _, ok := dict["ts"]; !ok {
dict["ts"] = time.Now().Local().Format(time.RFC3339Nano)
}
// FIX
if _, ok := dict["level"]; !ok {
dict["level"] = "debug"
}
func (logger *JsonLogger) LogJson(msgname string, v interface{}) {
obj := map[string]interface{}{
msgname: dict,
msgname: v,
}
jsontxt, _ := json.Marshal(obj)
logMessageC <- message{JsonLevel, fmt.Sprintf("%s", jsontxt)}
logMessageC <- message{JsonLevel, string(jsontxt)}
}

@ -0,0 +1,294 @@
package logger
import (
"time"
)
type utctime8601 struct{}
func (utctime8601) MarshalJSON() ([]byte, error) {
// FIX This should be re-formated for proper ISO 8601
return []byte(`"` + time.Now().UTC().Format(time.RFC3339Nano)[:26] + `Z"`), nil
}
//"starting"
type LogStarting struct {
ClientString string `json:"version_string"`
Guid string `json:"guid"`
Coinbase string `json:"coinbase"`
ProtocolVersion int `json:"eth_version"`
Ts utctime8601 `json:"ts"`
}
//"p2p.connecting"
type P2PConnecting struct {
RemoteId string `json:"remote_id"`
RemoteEndpoint string `json:"remote_endpoint"`
Guid string `json:"guid"`
NumConnections int `json:"num_connections"`
Ts utctime8601 `json:"ts"`
}
//"p2p.connected"
type P2PConnected struct {
Guid string `json:"guid"`
NumConnections int `json:"num_connections"`
RemoteId string `json:"remote_id"`
Ts utctime8601 `json:"ts"`
}
//"p2p.handshaked"
type P2PHandshaked struct {
RemoteCapabilities []string `json:"remote_capabilities"`
RemoteId string `json:"remote_id"`
Guid string `json:"guid"`
NumConnections int `json:"num_connections"`
Ts string `json:"ts"`
}
//"p2p.disconnected"
type P2PDisconnected struct {
Guid string `json:"guid"`
NumConnections int `json:"num_connections"`
RemoteId string `json:"remote_id"`
Ts utctime8601 `json:"ts"`
}
//"p2p.disconnecting"
type P2PDisconnecting struct {
Reason string `json:"reason"`
RemoteId string `json:"remote_id"`
Guid string `json:"guid"`
NumConnections int `json:"num_connections"`
Ts utctime8601 `json:"ts"`
}
//"p2p.disconnecting.bad_handshake"
type P2PDisconnectingBadHandshake struct {
Reason string `json:"reason"`
RemoteId string `json:"remote_id"`
Guid string `json:"guid"`
NumConnections int `json:"num_connections"`
Ts utctime8601 `json:"ts"`
}
//"p2p.disconnecting.bad_protocol"
type P2PDisconnectingBadProtocol struct {
Reason string `json:"reason"`
RemoteId string `json:"remote_id"`
Guid string `json:"guid"`
NumConnections int `json:"num_connections"`
Ts utctime8601 `json:"ts"`
}
//"p2p.disconnecting.reputation"
type P2PDisconnectingReputation struct {
Reason string `json:"reason"`
RemoteId string `json:"remote_id"`
Guid string `json:"guid"`
NumConnections int `json:"num_connections"`
Ts utctime8601 `json:"ts"`
}
//"p2p.disconnecting.dht"
type P2PDisconnectingDHT struct {
Reason string `json:"reason"`
RemoteId string `json:"remote_id"`
Guid string `json:"guid"`
NumConnections int `json:"num_connections"`
Ts utctime8601 `json:"ts"`
}
//"p2p.eth.disconnecting.bad_block"
type P2PEthDisconnectingBadBlock struct {
Reason string `json:"reason"`
RemoteId string `json:"remote_id"`
Guid string `json:"guid"`
NumConnections int `json:"num_connections"`
Ts utctime8601 `json:"ts"`
}
//"p2p.eth.disconnecting.bad_tx"
type P2PEthDisconnectingBadTx struct {
Reason string `json:"reason"`
RemoteId string `json:"remote_id"`
Guid string `json:"guid"`
NumConnections int `json:"num_connections"`
Ts utctime8601 `json:"ts"`
}
//"eth.newblock.mined"
type EthNewBlockMined struct {
BlockNumber int `json:"block_number"`
HeadHash string `json:"head_hash"`
BlockHash string `json:"block_hash"`
BlockHexRlp string `json:"block_hexrlp"`
BlockDifficulty int `json:"block_difficulty"`
Guid string `json:"guid"`
BlockPrevHash string `json:"block_prev_hash"`
Ts utctime8601 `json:"ts"`
}
//"eth.newblock.broadcasted"
type EthNewBlockBroadcasted struct {
BlockNumber int `json:"block_number"`
HeadHash string `json:"head_hash"`
BlockHash string `json:"block_hash"`
BlockDifficulty int `json:"block_difficulty"`
Guid string `json:"guid"`
BlockPrevHash string `json:"block_prev_hash"`
Ts utctime8601 `json:"ts"`
}
//"eth.newblock.received"
type EthNewBlockReceived struct {
BlockNumber int `json:"block_number"`
HeadHash string `json:"head_hash"`
BlockHash string `json:"block_hash"`
BlockDifficulty int `json:"block_difficulty"`
Guid string `json:"guid"`
BlockPrevHash string `json:"block_prev_hash"`
Ts utctime8601 `json:"ts"`
}
//"eth.newblock.is_known"
type EthNewBlockIsKnown struct {
BlockNumber int `json:"block_number"`
HeadHash string `json:"head_hash"`
BlockHash string `json:"block_hash"`
BlockDifficulty int `json:"block_difficulty"`
Guid string `json:"guid"`
BlockPrevHash string `json:"block_prev_hash"`
Ts utctime8601 `json:"ts"`
}
//"eth.newblock.is_new"
type EthNewBlockIsNew struct {
BlockNumber int `json:"block_number"`
HeadHash string `json:"head_hash"`
BlockHash string `json:"block_hash"`
BlockDifficulty int `json:"block_difficulty"`
Guid string `json:"guid"`
BlockPrevHash string `json:"block_prev_hash"`
Ts utctime8601 `json:"ts"`
}
//"eth.newblock.missing_parent"
type EthNewBlockMissingParent struct {
BlockNumber int `json:"block_number"`
HeadHash string `json:"head_hash"`
BlockHash string `json:"block_hash"`
BlockDifficulty int `json:"block_difficulty"`
Guid string `json:"guid"`
BlockPrevHash string `json:"block_prev_hash"`
Ts utctime8601 `json:"ts"`
}
//"eth.newblock.is_invalid"
type EthNewBlockIsInvalid struct {
BlockNumber int `json:"block_number"`
HeadHash string `json:"head_hash"`
BlockHash string `json:"block_hash"`
BlockDifficulty int `json:"block_difficulty"`
Guid string `json:"guid"`
BlockPrevHash string `json:"block_prev_hash"`
Ts utctime8601 `json:"ts"`
}
//"eth.newblock.chain.is_older"
type EthNewBlockChainIsOlder struct {
BlockNumber int `json:"block_number"`
HeadHash string `json:"head_hash"`
BlockHash string `json:"block_hash"`
BlockDifficulty int `json:"block_difficulty"`
Guid string `json:"guid"`
BlockPrevHash string `json:"block_prev_hash"`
Ts utctime8601 `json:"ts"`
}
//"eth.newblock.chain.is_cannonical"
type EthNewBlockChainIsCanonical struct {
BlockNumber int `json:"block_number"`
HeadHash string `json:"head_hash"`
BlockHash string `json:"block_hash"`
BlockDifficulty int `json:"block_difficulty"`
Guid string `json:"guid"`
BlockPrevHash string `json:"block_prev_hash"`
Ts utctime8601 `json:"ts"`
}
//"eth.newblock.chain.not_cannonical"
type EthNewBlockChainNotCanonical struct {
BlockNumber int `json:"block_number"`
HeadHash string `json:"head_hash"`
BlockHash string `json:"block_hash"`
BlockDifficulty int `json:"block_difficulty"`
Guid string `json:"guid"`
BlockPrevHash string `json:"block_prev_hash"`
Ts utctime8601 `json:"ts"`
}
//"eth.newblock.chain.switched"
type EthNewBlockChainSwitched struct {
BlockNumber int `json:"block_number"`
HeadHash string `json:"head_hash"`
OldHeadHash string `json:"old_head_hash"`
BlockHash string `json:"block_hash"`
BlockDifficulty int `json:"block_difficulty"`
Guid string `json:"guid"`
BlockPrevHash string `json:"block_prev_hash"`
Ts utctime8601 `json:"ts"`
}
//"eth.tx.created"
type EthTxCreated struct {
TxHash string `json:"tx_hash"`
TxSender string `json:"tx_sender"`
TxAddress string `json:"tx_address"`
TxHexRLP string `json:"tx_hexrlp"`
TxNonce int `json:"tx_nonce"`
Guid string `json:"guid"`
Ts utctime8601 `json:"ts"`
}
//"eth.tx.received"
type EthTxReceived struct {
TxHash string `json:"tx_hash"`
TxAddress string `json:"tx_address"`
TxHexRLP string `json:"tx_hexrlp"`
RemoteId string `json:"remote_id"`
TxNonce int `json:"tx_nonce"`
Guid string `json:"guid"`
Ts utctime8601 `json:"ts"`
}
//"eth.tx.broadcasted"
type EthTxBroadcasted struct {
TxHash string `json:"tx_hash"`
TxSender string `json:"tx_sender"`
TxAddress string `json:"tx_address"`
TxNonce int `json:"tx_nonce"`
Guid string `json:"guid"`
Ts utctime8601 `json:"ts"`
}
//"eth.tx.validated"
type EthTxValidated struct {
TxHash string `json:"tx_hash"`
TxSender string `json:"tx_sender"`
TxAddress string `json:"tx_address"`
TxNonce int `json:"tx_nonce"`
Guid string `json:"guid"`
Ts utctime8601 `json:"ts"`
}
//"eth.tx.is_invalid"
type EthTxIsInvalid struct {
TxHash string `json:"tx_hash"`
TxSender string `json:"tx_sender"`
TxAddress string `json:"tx_address"`
Reason string `json:"reason"`
TxNonce int `json:"tx_nonce"`
Guid string `json:"guid"`
Ts utctime8601 `json:"ts"`
}

@ -8,7 +8,6 @@ import (
"sync"
"time"
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/logger"
)
@ -356,25 +355,9 @@ func (srv *Server) dialLoop() {
// connect to peer via dial out
func (srv *Server) dialPeer(desc *peerAddr, slot int) {
srvlog.Debugf("Dialing %v (slot %d)\n", desc, slot)
evd := map[string]interface{}{
"remote_id": ethutil.Bytes2Hex(desc.Pubkey),
"remote_endpoint": desc.String(),
"level": "debug",
"guid": ethutil.Bytes2Hex(srv.Identity.Pubkey()),
"num_connections": srv.PeerCount(),
}
jsonlogger.LogJson("p2p.connecting", evd)
conn, err := srv.Dialer.Dial(desc.Network(), desc.String())
if err != nil {
srvlog.DebugDetailf("dial error: %v", err)
evd := map[string]interface{}{
"reason": "dial error",
"remote_id": desc.String(),
"level": "debug",
"guid": ethutil.Bytes2Hex(srv.Identity.Pubkey()),
"num_connections": srv.PeerCount(),
}
jsonlogger.LogJson("p2p.disconnecting", evd)
srv.peerSlots <- slot
return
}
@ -395,13 +378,6 @@ func (srv *Server) addPeer(conn net.Conn, desc *peerAddr, slot int) *Peer {
srv.peers[slot] = peer
srv.peerCount++
go func() {
evd := map[string]interface{}{
"guid": ethutil.Bytes2Hex(srv.Identity.Pubkey()),
"num_connections": srv.PeerCount(),
"remote_id": desc.String(),
"level": "debug",
}
jsonlogger.LogJson("p2p.connected", evd)
peer.loop()
srv.peerDisconnect <- peer
}()
@ -422,36 +398,13 @@ func (srv *Server) removePeer(peer *Peer) {
srv.peers[peer.slot] = nil
// release slot to signal need for a new peer, last!
srv.peerSlots <- peer.slot
evd := map[string]interface{}{
"guid": ethutil.Bytes2Hex(srv.Identity.Pubkey()),
"num_connections": srv.PeerCount(),
"remote_id": ethutil.Bytes2Hex(peer.Identity().Pubkey()),
"level": "debug",
}
jsonlogger.LogJson("p2p.disconnected", evd)
}
func (srv *Server) verifyPeer(addr *peerAddr) error {
if srv.Blacklist.Exists(addr.Pubkey) {
evd := map[string]interface{}{
"reason": "blacklisted",
"remote_id": addr.String(),
"level": "debug",
"guid": ethutil.Bytes2Hex(srv.Identity.Pubkey()),
"num_connections": srv.PeerCount(),
}
jsonlogger.LogJson("p2p.disconnecting.reputation", evd)
return errors.New("blacklisted")
}
if bytes.Equal(srv.Identity.Pubkey()[1:], addr.Pubkey) {
evd := map[string]interface{}{
"reason": "not allowed to connect to srv",
"remote_id": addr.String(),
"level": "debug",
"guid": ethutil.Bytes2Hex(srv.Identity.Pubkey()),
"num_connections": srv.PeerCount(),
}
jsonlogger.LogJson("p2p.disconnecting", evd)
return newPeerError(errPubkeyForbidden, "not allowed to connect to srv")
}
srv.lock.RLock()
@ -460,14 +413,6 @@ func (srv *Server) verifyPeer(addr *peerAddr) error {
if peer != nil {
id := peer.Identity()
if id != nil && bytes.Equal(id.Pubkey(), addr.Pubkey) {
evd := map[string]interface{}{
"reason": "already connected",
"remote_id": addr.String(),
"level": "debug",
"guid": ethutil.Bytes2Hex(srv.Identity.Pubkey()),
"num_connections": srv.PeerCount(),
}
jsonlogger.LogJson("p2p.disconnecting", evd)
return errors.New("already connected")
}
}

Loading…
Cancel
Save