Skip to content

Commit

Permalink
Try #2071:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] committed Aug 26, 2020
2 parents 58e742e + 29cd4ca commit 93f623c
Show file tree
Hide file tree
Showing 46 changed files with 4,829 additions and 327 deletions.
9 changes: 7 additions & 2 deletions activation/activation.go
Expand Up @@ -148,6 +148,11 @@ func (b *Builder) Stop() {
close(b.stop)
}

// GetSmesherID returns the ID of the smesher that created this activation
func (b *Builder) GetSmesherID() types.NodeID {
return b.nodeID
}

// SignAtx signs the atx and assigns the signature into atx.Sig
// this function returns an error if atx could not be converted to bytes
func (b *Builder) SignAtx(atx *types.ActivationTx) error {
Expand Down Expand Up @@ -191,7 +196,7 @@ func (b *Builder) loop() {
if _, stopRequested := err.(StopRequestedError); stopRequested {
return
}
events.Publish(events.AtxCreated{Created: false, Layer: uint64(b.currentEpoch())})
events.ReportAtxCreated(false, uint64(b.currentEpoch()), "")
<-b.layerClock.AwaitLayer(b.layerClock.GetCurrentLayer() + 1)
}
}
Expand Down Expand Up @@ -424,7 +429,7 @@ func (b *Builder) PublishActivationTx() error {
}

b.log.Event().Info("atx published!", atx.Fields(size)...)
events.Publish(events.AtxCreated{Created: true, ID: atx.ShortString(), Layer: uint64(b.currentEpoch())})
events.ReportAtxCreated(true, uint64(b.currentEpoch()), atx.ShortString())

select {
case <-atxReceived:
Expand Down
11 changes: 11 additions & 0 deletions activation/activationdb_test.go
Expand Up @@ -84,6 +84,17 @@ func (MockState) AddressExists(types.Address) bool {
return true
}

func (MockState) GetLayerStateRoot(layer types.LayerID) (types.Hash32, error) {
panic("implement me")
}

func (MockState) GetBalance(addr types.Address) uint64 {
panic("implement me")
}
func (MockState) GetNonce(addr types.Address) uint64 {
panic("implement me")
}

type ATXDBMock struct {
mock.Mock
counter int
Expand Down
4 changes: 2 additions & 2 deletions activation/atxdb.go
Expand Up @@ -338,7 +338,7 @@ func (db *DB) deleteLock(viewHash types.Hash12) {
// - ATX LayerID is NipstLayerTime or less after the PositioningATX LayerID.
// - The ATX view of the previous epoch contains ActiveSetSize activations.
func (db *DB) SyntacticallyValidateAtx(atx *types.ActivationTx) error {
events.Publish(events.NewAtx{ID: atx.ShortString(), LayerID: uint64(atx.PubLayerID.GetEpoch())})
events.ReportNewActivation(atx)
pub, err := ExtractPublicKey(atx)
if err != nil {
return fmt.Errorf("cannot validate atx sig atx id %v err %v", atx.ShortString(), err)
Expand Down Expand Up @@ -760,7 +760,7 @@ func (db *DB) HandleGossipAtx(data service.GossipMessage, syncer service.Syncer)
}

err = db.SyntacticallyValidateAtx(atx)
events.Publish(events.ValidAtx{ID: atx.ShortString(), Valid: err == nil})
events.ReportValidActivation(atx, err == nil)
if err != nil {
db.log.Warning("received syntactically invalid ATX %v: %v", atx.ShortString(), err)
// TODO: blacklist peer
Expand Down
46 changes: 40 additions & 6 deletions api/api_test.go
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/common/util"
config2 "github.com/spacemeshos/go-spacemesh/config"
"github.com/spacemeshos/go-spacemesh/mesh"
"github.com/spacemeshos/go-spacemesh/p2p/p2pcrypto"
"github.com/spacemeshos/go-spacemesh/p2p/service"
"github.com/spacemeshos/go-spacemesh/priorityq"
Expand Down Expand Up @@ -181,6 +180,33 @@ func (t *TxAPIMock) AddressExists(types.Address) bool {
return true
}

func (t *TxAPIMock) GetATXs([]types.ATXID) (map[types.ATXID]*types.ActivationTx, []types.ATXID) {
return nil, nil
}

func (t *TxAPIMock) GetTransactions([]types.TransactionID) ([]*types.Transaction, map[types.TransactionID]struct{}) {
return nil, nil
}

func (t *TxAPIMock) GetLayer(types.LayerID) (*types.Layer, error) {
return nil, nil
}

func (t *TxAPIMock) ProcessedLayer() types.LayerID {
panic("implement me")
}

func (t *TxAPIMock) GetLayerStateRoot(layer types.LayerID) (types.Hash32, error) {
panic("implement me")
}

func (t *TxAPIMock) GetBalance(addr types.Address) uint64 {
panic("implement me")
}
func (t *TxAPIMock) GetNonce(addr types.Address) uint64 {
panic("implement me")
}

// MiningAPIMock is a mock for mining API
type MiningAPIMock struct{}

Expand All @@ -199,6 +225,14 @@ func (*MiningAPIMock) StartPost(types.Address, string, uint64) error {

func (*MiningAPIMock) SetCoinbaseAccount(types.Address) {}

func (*MiningAPIMock) GetSmesherID() types.NodeID {
panic("implement me")
}

func (*MiningAPIMock) Stop() {
panic("implement me")
}

type OracleMock struct{}

func (*OracleMock) GetEligibleLayers() []types.LayerID {
Expand Down Expand Up @@ -399,22 +433,22 @@ func TestJsonWalletApi(t *testing.T) {
// test get txs per account:

// add incoming tx to mempool
mempoolTxIn, err := mesh.NewSignedTx(1337, addr, 420, 3, 42, signing.NewEdSigner())
mempoolTxIn, err := types.NewSignedTx(1337, addr, 420, 3, 42, signing.NewEdSigner())
r.NoError(err)
txMempool.Put(mempoolTxIn.ID(), mempoolTxIn)

// add outgoing tx to mempool
mempoolTxOut, err := mesh.NewSignedTx(1337, types.BytesToAddress([]byte{1}), 420, 3, 42, signer)
mempoolTxOut, err := types.NewSignedTx(1337, types.BytesToAddress([]byte{1}), 420, 3, 42, signer)
r.NoError(err)
txMempool.Put(mempoolTxOut.ID(), mempoolTxOut)

// add incoming tx to mesh
meshTxIn, err := mesh.NewSignedTx(1337, addr, 420, 3, 42, signing.NewEdSigner())
meshTxIn, err := types.NewSignedTx(1337, addr, 420, 3, 42, signing.NewEdSigner())
r.NoError(err)
txAPI.returnTx[meshTxIn.ID()] = meshTxIn

// add outgoing tx to mesh
meshTxOut, err := mesh.NewSignedTx(1337, types.BytesToAddress([]byte{1}), 420, 3, 42, signer)
meshTxOut, err := types.NewSignedTx(1337, types.BytesToAddress([]byte{1}), 420, 3, 42, signer)
r.NoError(err)
txAPI.returnTx[meshTxOut.ID()] = meshTxOut

Expand Down Expand Up @@ -597,7 +631,7 @@ func genTx(t *testing.T) *types.Transaction {
require.NoError(t, err)
signer, err := signing.NewEdSignerFromBuffer(key)
require.NoError(t, err)
tx, err := mesh.NewSignedTx(1111, [20]byte{}, 1234, 11, 321, signer)
tx, err := types.NewSignedTx(1111, [20]byte{}, 1234, 11, 321, signer)
require.NoError(t, err)

return tx
Expand Down
79 changes: 49 additions & 30 deletions api/config/config.go
Expand Up @@ -6,30 +6,38 @@ import (
)

const (
defaultStartGRPCServer = false
defaultGRPCServerPort = 9091
defaultNewGRPCServerPort = 9092
defaultStartJSONServer = false
defaultStartNewJSONServer = false
defaultJSONServerPort = 9090
defaultNewJSONServerPort = 9093
defaultStartNodeService = false
defaultStartMeshService = false
defaultStartGRPCServer = false
defaultGRPCServerPort = 9091
defaultNewGRPCServerPort = 9092
defaultNewGRPCServerInterface = ""
defaultStartJSONServer = false
defaultStartNewJSONServer = false
defaultJSONServerPort = 9090
defaultNewJSONServerPort = 9093
defaultStartNodeService = false
defaultStartMeshService = false
defaultStartGlobalStateService = false
defaultStartTransactionService = false
defaultStartSmesherService = false
)

// Config defines the api config params
type Config struct {
StartGrpcServer bool `mapstructure:"grpc-server"`
StartGrpcServices []string `mapstructure:"grpc"`
GrpcServerPort int `mapstructure:"grpc-port"`
NewGrpcServerPort int `mapstructure:"grpc-port-new"`
StartJSONServer bool `mapstructure:"json-server"`
StartNewJSONServer bool `mapstructure:"json-server-new"`
JSONServerPort int `mapstructure:"json-port"`
NewJSONServerPort int `mapstructure:"json-port-new"`
StartGrpcServer bool `mapstructure:"grpc-server"`
StartGrpcServices []string `mapstructure:"grpc"`
GrpcServerPort int `mapstructure:"grpc-port"`
NewGrpcServerPort int `mapstructure:"grpc-port-new"`
NewGrpcServerInterface string `mapstructure:"grpc-interface-new"`
StartJSONServer bool `mapstructure:"json-server"`
StartNewJSONServer bool `mapstructure:"json-server-new"`
JSONServerPort int `mapstructure:"json-port"`
NewJSONServerPort int `mapstructure:"json-port-new"`
// no direct command line flags for these
StartNodeService bool
StartMeshService bool
StartNodeService bool
StartMeshService bool
StartGlobalStateService bool
StartTransactionService bool
StartSmesherService bool
}

func init() {
Expand All @@ -39,16 +47,20 @@ func init() {
// DefaultConfig defines the default configuration options for api
func DefaultConfig() Config {
return Config{
StartGrpcServer: defaultStartGRPCServer, // note: all bool flags default to false so don't set one of these to true here
StartGrpcServices: nil, // note: cannot configure an array as a const
GrpcServerPort: defaultGRPCServerPort,
NewGrpcServerPort: defaultNewGRPCServerPort,
StartJSONServer: defaultStartJSONServer,
StartNewJSONServer: defaultStartNewJSONServer,
JSONServerPort: defaultJSONServerPort,
NewJSONServerPort: defaultNewJSONServerPort,
StartNodeService: defaultStartNodeService,
StartMeshService: defaultStartMeshService,
StartGrpcServer: defaultStartGRPCServer, // note: all bool flags default to false so don't set one of these to true here
StartGrpcServices: nil, // note: cannot configure an array as a const
GrpcServerPort: defaultGRPCServerPort,
NewGrpcServerPort: defaultNewGRPCServerPort,
NewGrpcServerInterface: defaultNewGRPCServerInterface,
StartJSONServer: defaultStartJSONServer,
StartNewJSONServer: defaultStartNewJSONServer,
JSONServerPort: defaultJSONServerPort,
NewJSONServerPort: defaultNewJSONServerPort,
StartNodeService: defaultStartNodeService,
StartMeshService: defaultStartMeshService,
StartGlobalStateService: defaultStartGlobalStateService,
StartTransactionService: defaultStartTransactionService,
StartSmesherService: defaultStartSmesherService,
}
}

Expand All @@ -61,14 +73,21 @@ func (s *Config) ParseServicesList() error {
s.StartMeshService = true
case "node":
s.StartNodeService = true
case "globalstate":
s.StartGlobalStateService = true
case "transaction":
s.StartTransactionService = true
case "smesher":
s.StartSmesherService = true
default:
return errors.New("unrecognized GRPC service requested: " + svc)
}
}

// If JSON gateway server is enabled, make sure at least one
// GRPC service is also enabled
if s.StartNewJSONServer && !s.StartNodeService {
if s.StartNewJSONServer && !s.StartNodeService && !s.StartMeshService &&
!s.StartGlobalStateService && !s.StartTransactionService && !s.StartSmesherService {
return errors.New("must enable at least one GRPC service along with JSON gateway service")
}

Expand Down

0 comments on commit 93f623c

Please sign in to comment.