Merge branch 'master' into bootstrap-heap

This commit is contained in:
Stephen Buttolph 2020-06-17 20:57:56 -04:00 committed by GitHub
commit 9543966d44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 244 additions and 110 deletions

View File

@ -10,129 +10,35 @@ import (
"github.com/ava-labs/gecko/api"
"github.com/ava-labs/gecko/chains"
"github.com/ava-labs/gecko/genesis"
"github.com/ava-labs/gecko/ids"
"github.com/ava-labs/gecko/network"
"github.com/ava-labs/gecko/snow/engine/common"
"github.com/ava-labs/gecko/utils/logging"
"github.com/ava-labs/gecko/version"
cjson "github.com/ava-labs/gecko/utils/json"
)
// Admin is the API service for node admin management
type Admin struct {
version version.Version
nodeID ids.ShortID
networkID uint32
log logging.Logger
networking network.Network
performance Performance
chainManager chains.Manager
httpServer *api.Server
}
// NewService returns a new admin API service
func NewService(version version.Version, nodeID ids.ShortID, networkID uint32, log logging.Logger, chainManager chains.Manager, peers network.Network, httpServer *api.Server) *common.HTTPHandler {
func NewService(log logging.Logger, chainManager chains.Manager, peers network.Network, httpServer *api.Server) *common.HTTPHandler {
newServer := rpc.NewServer()
codec := cjson.NewCodec()
newServer.RegisterCodec(codec, "application/json")
newServer.RegisterCodec(codec, "application/json;charset=UTF-8")
newServer.RegisterService(&Admin{
version: version,
nodeID: nodeID,
networkID: networkID,
log: log,
chainManager: chainManager,
networking: peers,
httpServer: httpServer,
}, "admin")
return &common.HTTPHandler{Handler: newServer}
}
// GetNodeVersionReply are the results from calling GetNodeVersion
type GetNodeVersionReply struct {
Version string `json:"version"`
}
// GetNodeVersion returns the version this node is running
func (service *Admin) GetNodeVersion(_ *http.Request, _ *struct{}, reply *GetNodeVersionReply) error {
service.log.Info("Admin: GetNodeVersion called")
reply.Version = service.version.String()
return nil
}
// GetNodeIDReply are the results from calling GetNodeID
type GetNodeIDReply struct {
NodeID ids.ShortID `json:"nodeID"`
}
// GetNodeID returns the node ID of this node
func (service *Admin) GetNodeID(_ *http.Request, _ *struct{}, reply *GetNodeIDReply) error {
service.log.Info("Admin: GetNodeID called")
reply.NodeID = service.nodeID
return nil
}
// GetNetworkIDReply are the results from calling GetNetworkID
type GetNetworkIDReply struct {
NetworkID cjson.Uint32 `json:"networkID"`
}
// GetNetworkID returns the network ID this node is running on
func (service *Admin) GetNetworkID(_ *http.Request, _ *struct{}, reply *GetNetworkIDReply) error {
service.log.Info("Admin: GetNetworkID called")
reply.NetworkID = cjson.Uint32(service.networkID)
return nil
}
// GetNetworkNameReply is the result from calling GetNetworkName
type GetNetworkNameReply struct {
NetworkName string `json:"networkName"`
}
// GetNetworkName returns the network name this node is running on
func (service *Admin) GetNetworkName(_ *http.Request, _ *struct{}, reply *GetNetworkNameReply) error {
service.log.Info("Admin: GetNetworkName called")
reply.NetworkName = genesis.NetworkName(service.networkID)
return nil
}
// GetBlockchainIDArgs are the arguments for calling GetBlockchainID
type GetBlockchainIDArgs struct {
Alias string `json:"alias"`
}
// GetBlockchainIDReply are the results from calling GetBlockchainID
type GetBlockchainIDReply struct {
BlockchainID string `json:"blockchainID"`
}
// GetBlockchainID returns the blockchain ID that resolves the alias that was supplied
func (service *Admin) GetBlockchainID(_ *http.Request, args *GetBlockchainIDArgs, reply *GetBlockchainIDReply) error {
service.log.Info("Admin: GetBlockchainID called")
bID, err := service.chainManager.Lookup(args.Alias)
reply.BlockchainID = bID.String()
return err
}
// PeersReply are the results from calling Peers
type PeersReply struct {
Peers []network.PeerID `json:"peers"`
}
// Peers returns the list of current validators
func (service *Admin) Peers(_ *http.Request, _ *struct{}, reply *PeersReply) error {
service.log.Info("Admin: Peers called")
reply.Peers = service.networking.Peers()
return nil
}
// StartCPUProfilerArgs are the arguments for calling StartCPUProfiler
type StartCPUProfilerArgs struct {
Filename string `json:"filename"`

131
api/info/service.go Normal file
View File

@ -0,0 +1,131 @@
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package info
import (
"net/http"
"github.com/gorilla/rpc/v2"
"github.com/ava-labs/gecko/chains"
"github.com/ava-labs/gecko/genesis"
"github.com/ava-labs/gecko/ids"
"github.com/ava-labs/gecko/network"
"github.com/ava-labs/gecko/snow/engine/common"
"github.com/ava-labs/gecko/utils/logging"
"github.com/ava-labs/gecko/version"
cjson "github.com/ava-labs/gecko/utils/json"
)
// Info is the API service for unprivileged info on a node
type Info struct {
version version.Version
nodeID ids.ShortID
networkID uint32
log logging.Logger
networking network.Network
chainManager chains.Manager
}
// NewService returns a new admin API service
func NewService(log logging.Logger, version version.Version, nodeID ids.ShortID, networkID uint32, chainManager chains.Manager, peers network.Network) *common.HTTPHandler {
newServer := rpc.NewServer()
codec := cjson.NewCodec()
newServer.RegisterCodec(codec, "application/json")
newServer.RegisterCodec(codec, "application/json;charset=UTF-8")
newServer.RegisterService(&Info{
version: version,
nodeID: nodeID,
networkID: networkID,
log: log,
chainManager: chainManager,
networking: peers,
}, "info")
return &common.HTTPHandler{Handler: newServer}
}
// GetNodeVersionReply are the results from calling GetNodeVersion
type GetNodeVersionReply struct {
Version string `json:"version"`
}
// GetNodeVersion returns the version this node is running
func (service *Info) GetNodeVersion(_ *http.Request, _ *struct{}, reply *GetNodeVersionReply) error {
service.log.Info("Info: GetNodeVersion called")
reply.Version = service.version.String()
return nil
}
// GetNodeIDReply are the results from calling GetNodeID
type GetNodeIDReply struct {
NodeID ids.ShortID `json:"nodeID"`
}
// GetNodeID returns the node ID of this node
func (service *Info) GetNodeID(_ *http.Request, _ *struct{}, reply *GetNodeIDReply) error {
service.log.Info("Info: GetNodeID called")
reply.NodeID = service.nodeID
return nil
}
// GetNetworkIDReply are the results from calling GetNetworkID
type GetNetworkIDReply struct {
NetworkID cjson.Uint32 `json:"networkID"`
}
// GetNetworkID returns the network ID this node is running on
func (service *Info) GetNetworkID(_ *http.Request, _ *struct{}, reply *GetNetworkIDReply) error {
service.log.Info("Info: GetNetworkID called")
reply.NetworkID = cjson.Uint32(service.networkID)
return nil
}
// GetNetworkNameReply is the result from calling GetNetworkName
type GetNetworkNameReply struct {
NetworkName string `json:"networkName"`
}
// GetNetworkName returns the network name this node is running on
func (service *Info) GetNetworkName(_ *http.Request, _ *struct{}, reply *GetNetworkNameReply) error {
service.log.Info("Info: GetNetworkName called")
reply.NetworkName = genesis.NetworkName(service.networkID)
return nil
}
// GetBlockchainIDArgs are the arguments for calling GetBlockchainID
type GetBlockchainIDArgs struct {
Alias string `json:"alias"`
}
// GetBlockchainIDReply are the results from calling GetBlockchainID
type GetBlockchainIDReply struct {
BlockchainID string `json:"blockchainID"`
}
// GetBlockchainID returns the blockchain ID that resolves the alias that was supplied
func (service *Info) GetBlockchainID(_ *http.Request, args *GetBlockchainIDArgs, reply *GetBlockchainIDReply) error {
service.log.Info("Info: GetBlockchainID called")
bID, err := service.chainManager.Lookup(args.Alias)
reply.BlockchainID = bID.String()
return err
}
// PeersReply are the results from calling Peers
type PeersReply struct {
Peers []network.PeerID `json:"peers"`
}
// Peers returns the list of current validators
func (service *Info) Peers(_ *http.Request, _ *struct{}, reply *PeersReply) error {
service.log.Info("Info: Peers called")
reply.Peers = service.networking.Peers()
return nil
}

View File

@ -226,6 +226,7 @@ func init() {
// Enable/Disable APIs:
fs.BoolVar(&Config.AdminAPIEnabled, "api-admin-enabled", true, "If true, this node exposes the Admin API")
fs.BoolVar(&Config.InfoAPIEnabled, "api-info-enabled", true, "If true, this node exposes the Info API")
fs.BoolVar(&Config.KeystoreAPIEnabled, "api-keystore-enabled", true, "If true, this node exposes the Keystore API")
fs.BoolVar(&Config.MetricsAPIEnabled, "api-metrics-enabled", true, "If true, this node exposes the Metrics API")
fs.BoolVar(&Config.HealthAPIEnabled, "api-health-enabled", true, "If true, this node exposes the Health API")

View File

@ -51,6 +51,7 @@ type Config struct {
// Enable/Disable APIs
AdminAPIEnabled bool
InfoAPIEnabled bool
KeystoreAPIEnabled bool
MetricsAPIEnabled bool
HealthAPIEnabled bool

View File

@ -18,6 +18,7 @@ import (
"github.com/ava-labs/gecko/api"
"github.com/ava-labs/gecko/api/admin"
"github.com/ava-labs/gecko/api/health"
"github.com/ava-labs/gecko/api/info"
"github.com/ava-labs/gecko/api/ipcs"
"github.com/ava-labs/gecko/api/keystore"
"github.com/ava-labs/gecko/api/metrics"
@ -461,11 +462,19 @@ func (n *Node) initMetricsAPI() {
func (n *Node) initAdminAPI() {
if n.Config.AdminAPIEnabled {
n.Log.Info("initializing Admin API")
service := admin.NewService(Version, n.ID, n.Config.NetworkID, n.Log, n.chainManager, n.Net, &n.APIServer)
service := admin.NewService(n.Log, n.chainManager, n.Net, &n.APIServer)
n.APIServer.AddRoute(service, &sync.RWMutex{}, "admin", "", n.HTTPLog)
}
}
func (n *Node) initInfoAPI() {
if n.Config.InfoAPIEnabled {
n.Log.Info("initializing Info API")
service := info.NewService(n.Log, Version, n.ID, n.Config.NetworkID, n.chainManager, n.Net)
n.APIServer.AddRoute(service, &sync.RWMutex{}, "info", "", n.HTTPLog)
}
}
// initHealthAPI initializes the Health API service
// Assumes n.Log, n.ConsensusAPI, and n.ValidatorAPI already initialized
func (n *Node) initHealthAPI() {
@ -562,6 +571,7 @@ func (n *Node) Initialize(Config *Config, logger logging.Logger, logFactory logg
n.initChainManager() // Set up the chain manager
n.initAdminAPI() // Start the Admin API
n.initInfoAPI() // Start the Info API
n.initHealthAPI() // Start the Health API
n.initIPCAPI() // Start the IPC API

View File

@ -34,8 +34,7 @@ type Block struct {
func (b *Block) Initialize(bytes []byte, vm *SnowmanVM) {
b.VM = vm
b.Metadata.Initialize(bytes)
status := b.VM.State.GetStatus(vm.DB, b.ID())
b.SetStatus(status)
b.SetStatus(choices.Unknown) // don't set status until it is queried
}
// ParentID returns [b]'s parent's ID
@ -55,7 +54,6 @@ func (b *Block) Parent() snowman.Block {
// Recall that b.vm.DB.Commit() must be called to persist to the DB
func (b *Block) Accept() error {
b.SetStatus(choices.Accepted) // Change state of this block
blkID := b.ID()
// Persist data

View File

@ -0,0 +1,48 @@
package core
import (
"testing"
"github.com/ava-labs/gecko/snow/choices"
"github.com/ava-labs/gecko/snow/consensus/snowman"
"github.com/ava-labs/gecko/ids"
"github.com/ava-labs/gecko/database/memdb"
"github.com/ava-labs/gecko/database/versiondb"
)
func TestBlock(t *testing.T) {
parentID := ids.NewID([32]byte{1, 2, 3, 4, 5})
db := versiondb.New(memdb.New())
state, err := NewSnowmanState(func([]byte) (snowman.Block, error) { return nil, nil })
if err != nil {
t.Fatal(err)
}
b := NewBlock(parentID)
b.Initialize([]byte{1, 2, 3}, &SnowmanVM{
DB: db,
State: state,
})
// should be unknown until someone queries for it
if status := b.Metadata.status; status != choices.Unknown {
t.Fatalf("status should be unknown but is %s", status)
}
// querying should change status to processing
if status := b.Status(); status != choices.Processing {
t.Fatalf("status should be processing but is %s", status)
}
b.Accept()
if status := b.Status(); status != choices.Accepted {
t.Fatalf("status should be accepted but is %s", status)
}
b.Reject()
if status := b.Status(); status != choices.Rejected {
t.Fatalf("status should be rejected but is %s", status)
}
}

View File

@ -128,19 +128,10 @@ func (s *state) Get(db database.Database, typeID uint64, key ids.ID) (interface{
// The unique ID of this key/typeID pair
uID := s.uniqueID(key, typeID)
// See if exists in database
exists, err := db.Has(uID.Bytes())
if err != nil {
return nil, err
}
if !exists {
return nil, database.ErrNotFound
}
// Get the value from the database
valueBytes, err := db.Get(uID.Bytes())
if err != nil {
return nil, fmt.Errorf("problem getting value from database: %w", err)
return nil, err
}
// Unmarshal the value from bytes and return it

View File

@ -128,7 +128,7 @@ func (tx *addDefaultSubnetDelegatorTx) SemanticVerify(db database.Database) (*ve
// The account if this block's proposal is committed and the validator is
// added to the pending validator set. (Increase the account's nonce;
// decrease its balance.)
newAccount, err := account.Remove(0, tx.Nonce) // Remove also removes the fee
newAccount, err := account.Remove(tx.Wght, tx.Nonce) // Remove also removes the fee
if err != nil {
return nil, nil, nil, nil, permError{err}
}

View File

@ -386,4 +386,52 @@ func TestAddDefaultSubnetDelegatorTxSemanticVerify(t *testing.T) {
t.Fatal("should have failed verification because payer account has no $AVA to pay fee")
}
txFee = txFeeSaved // Reset tx fee
// Case 8: fail verification for spending more funds than it has
tx, err = vm.newAddDefaultSubnetDelegatorTx(
defaultNonce+1,
defaultBalance*2, // weight
uint64(defaultValidateStartTime.Unix()), // start time
uint64(defaultValidateEndTime.Unix()), // end time
defaultKey.PublicKey().Address(), // node ID
defaultKey.PublicKey().Address(), // destination
testNetworkID, // network ID
defaultKey, // tx fee payer
)
if err != nil {
t.Fatal(err)
}
_, _, _, _, err = tx.SemanticVerify(vm.DB)
if err == nil {
t.Fatal("should have failed verification because payer account spent twice the account's balance")
}
// Case 9: Confirm balance is correct
tx, err = vm.newAddDefaultSubnetDelegatorTx(
defaultNonce+1,
defaultStakeAmount, // weight
uint64(defaultValidateStartTime.Unix()), // start time
uint64(defaultValidateEndTime.Unix()), // end time
defaultKey.PublicKey().Address(), // node ID
defaultKey.PublicKey().Address(), // destination
testNetworkID, // network ID
defaultKey, // tx fee payer
)
if err != nil {
t.Fatal(err)
}
onCommitDB, _, _, _, err := tx.SemanticVerify(vm.DB)
if err != nil {
t.Fatal(err)
}
account, err := tx.vm.getAccount(onCommitDB, defaultKey.PublicKey().Address())
if err != nil {
t.Fatal(err)
}
balance := account.Balance
if balance != defaultBalance-(defaultStakeAmount+txFee) {
t.Fatalf("balance was not updated correctly after subnet delegator tx")
}
}